Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Image Manipulator Class
- *
- * A class that contains several methods to manipulate images
- *
- * Author: Anon
- **/
- import java.io.File;
- import java.util.Arrays;
- public class ImageManipulator {
- /**
- * Data members
- **/
- /**
- * Methods
- **/
- // Helper method for generating an array of Picture objects for use in other methods
- private Picture[] generatePictureList(String directory) {
- System.out.println("Generating removePhotobomb image array from input directory...");
- // TODO: Rework method so length of Picture objects array is generated based on number of JPEGs present in directory if time permits
- File[] files = (new File(directory)).listFiles();
- // TODO: Add if statement to generate array with length based on number of JPEGs rather than just total number of files
- Picture[] images = new Picture[files.length];
- int count = 0;
- // Generates an array of Picture objects from the files list and disregards any non-JPEGs
- for (int i = 0; i < files.length; i++) {
- String absPath = files[i].getAbsolutePath();
- if (absPath.endsWith(".jpg")) {
- images[count++] = new Picture(absPath);
- }
- }
- return images;
- }
- // Helper method to get the number of valid Picture objects
- private int getPictureCount(Picture[] inputArray) {
- int count = 0;
- for (int i = 0; i < inputArray.length; i++) {
- if (inputArray[i] != null) {
- count++;
- }
- }
- return count;
- }
- // Helper method for finding the median of a given array
- private int getMedian(int[] inputArray) {
- Arrays.sort(inputArray);
- int length = inputArray.length;
- if (inputArray.length % 2 == 0) {
- int median = (inputArray[length / 2] + inputArray[(length / 2) - 1]) / 2;
- return median;
- }
- int median = inputArray[(length - 1) / 2];
- return median;
- }
- public void removePhotobomb(String directory, String outputfile) {
- Picture[] images = this.generatePictureList(directory);
- if (images[0] != null) {
- int count = this.getPictureCount(images);
- // Gets dimensions of images and creates new Picture object for method output
- int masterWidth = images[0].getWidth();
- int masterHeight = images[0].getHeight();
- Picture outputImage = new Picture(masterWidth, masterHeight);
- // 2D arrays that store the median RGB values for each pixel of the output image
- int[][] medianRed = new int[masterHeight][masterWidth];
- int[][] medianGreen = new int[masterHeight][masterWidth];
- int[][] medianBlue = new int[masterHeight][masterWidth];
- // for loop that gets the RGB values of each pixel from the image and stores the color value from that pixel of each image in an array and computes the median color values
- System.out.println("Processing color values of all images and computing output median values...");
- for (int r = 0; r < masterHeight; r++) {
- for (int c = 0; c < masterWidth; c++) {
- // New arrays are initialized to store the RGB values of the specified pixel taken from the set of images
- int[] redValues = new int[count];
- int[] greenValues = new int[count];
- int[] blueValues = new int[count];
- for (int i = 0; i < count; i++) {
- redValues[i] = images[i].getPixel(r, c).getRed();
- greenValues[i] = images[i].getPixel(r, c).getGreen();
- blueValues[i] = images[i].getPixel(r, c).getBlue();
- }
- // 2D arrays that store the median color values for each pixel
- medianRed[r][c] = this.getMedian(redValues);
- medianGreen[r][c] = this.getMedian(greenValues);
- medianBlue[r][c] = this.getMedian(blueValues);
- }
- }
- // Assigns new median color values to each pixel of output image
- System.out.println("Rendering removePhotobomb output image...");
- for (int r = 0; r < masterHeight; r++) {
- for (int c = 0; c < masterWidth; c++) {
- outputImage.setPixel(r, c, new Pixel(medianRed[r][c], medianGreen[r][c], medianBlue[r][c]));
- }
- }
- System.out.println("Saving removePhotobomb output image...");
- outputImage.store(outputfile);
- System.out.println("removePhotobomb completed");
- } else {
- System.out.println("No entries in image list for removePhotobomb method");
- }
- }
- public void zoomMiddle(String inputfile, String outputfile) {
- // Original picture, variables for the original's dimensions and method output picture
- System.out.println("Getting image info for zoomMiddle...");
- Picture original = new Picture(inputfile);
- int origHeight = original.getHeight();
- int origWidth = original.getWidth();
- Picture zoomOutput = new Picture(origWidth, origHeight);
- // Variables that get the dimensions of the area to be zoomed in and coordinates of area start and end points
- System.out.println("Computing dimensions of area to be zoomed in...");
- int zoomAreaHeightStart = origHeight / 4;
- int zoomAreaHeightEnd = origHeight - zoomAreaHeightStart;
- int zoomAreaHeight = zoomAreaHeightEnd - zoomAreaHeightStart;
- int zoomAreaWidthStart = origWidth / 4;
- int zoomAreaWidthEnd = origWidth - zoomAreaWidthStart;
- int zoomAreaWidth = zoomAreaWidthEnd - zoomAreaWidthStart;
- // Array containing the pixels of the area to be zoomed in on
- Pixel[][] zoomedArea = new Pixel[zoomAreaHeight][zoomAreaWidth];
- for (int r = 0; r < zoomedArea.length; r++) {
- for (int c = 0; c < zoomedArea[r].length; c++) {
- zoomedArea[r][c] = original.getPixel(zoomAreaHeightStart + r, zoomAreaWidthStart + c);
- }
- }
- // Assigns appropriate values from the zoomedArea array to all pixels in the output image
- System.out.println("Rendering zoomMiddle output image...");
- for (int r = 0; r < origHeight; r++) {
- for (int c = 0; c < origWidth; c++) {
- Pixel zoomReplacement = zoomedArea[r / 2][c / 2];
- zoomOutput.setPixel(r, c, zoomReplacement);
- }
- }
- System.out.println("Saving zoomMiddle output image...");
- zoomOutput.store(outputfile);
- System.out.println("zoomMiddle completed");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement