Mirroring image horizontally in java

In our previous article we discussed about mirroring an image vertically left to right and right to left. Today we are going to discuss mirroring an image from horizontally from top to bottom and bottom to top respectively. The idea is same. We have to find a mirroring point that can be center point of an image horizontally.

Original Image

Original Image

Horizontal mirroring an image example 1

// Horizontal mirroring of picture from top to bottom
		public void mirrorHorizonalTB() {
			int height = this.getHeight();
			int mirrorPoint = height / 2;
			Pixel topPixel = null;
			Pixel bottomPixel = null;

			// loop through all the cols
			for (int x = 0; x < getWidth(); x++) {
				// loop from 0 to the middle (mirror point)
				for (int y = 0; y < mirrorPoint; y++) {
					topPixel = getPixel(x, y);
					bottomPixel = getPixel(x,height - 1 - y);
					bottomPixel.setColor(topPixel.getColor());
				}
			}
		}
Mirroring horizontally

Mirroring horizontally example 1

Horizontal mirroring an image example 2

// Horizontal mirroring of picture from bottom to top
		public void mirrorHorizonalBT() {
			int height = this.getHeight();
			int mirrorPoint = height / 2;
			Pixel topPixel = null;
			Pixel bottomPixel = null;

			// loop through all the cols
			for (int x = 0; x < getWidth(); x++) {
				// loop from 0 to the middle (mirror point)
				for (int y = 0; y < mirrorPoint; y++) {
					topPixel = getPixel(x, y);
					bottomPixel = getPixel(x,height - 1 - y);
					topPixel.setColor(bottomPixel.getColor());
				}
			}
		}
Mirroring horizontally

Mirroring horizontally example 2

Add above methods in MyPicture class and call them from main method. Results are shared above.

No Responses