Advertisement
Guest User

Untitled

a guest
Oct 19th, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.92 KB | None | 0 0
  1. /**
  2. * Image Manipulator Class
  3. *
  4. * A class that contains several methods to manipulate images
  5. *
  6. * Author: Anon
  7. **/
  8.  
  9. import java.io.File;
  10. import java.util.Arrays;
  11.  
  12. public class ImageManipulator {
  13.  
  14. /**
  15. * Data members
  16. **/
  17.  
  18. /**
  19. * Methods
  20. **/
  21.  
  22.     // Helper method for generating an array of Picture objects for use in other methods
  23.     private Picture[] generatePictureList(String directory) {
  24.  
  25.         System.out.println("Generating removePhotobomb image array from input directory...");
  26.  
  27.         // TODO: Rework method so length of Picture objects array is generated based on number of JPEGs present in directory if time permits
  28.  
  29.         File[] files = (new File(directory)).listFiles();
  30.  
  31.         // TODO: Add if statement to generate array with length based on number of JPEGs rather than just total number of files
  32.  
  33.         Picture[] images = new Picture[files.length];
  34.  
  35.         int count = 0;
  36.  
  37.         // Generates an array of Picture objects from the files list and disregards any non-JPEGs
  38.         for (int i = 0; i < files.length; i++) {
  39.             String absPath = files[i].getAbsolutePath();
  40.             if (absPath.endsWith(".jpg")) {
  41.                 images[count++] = new Picture(absPath);
  42.             }
  43.            
  44.         }
  45.  
  46.         return images;
  47.  
  48.     }
  49.  
  50.     // Helper method to get the number of valid Picture objects
  51.     private int getPictureCount(Picture[] inputArray) {
  52.         int count = 0;
  53.  
  54.         for (int i = 0; i < inputArray.length; i++) {
  55.             if (inputArray[i] != null) {
  56.                 count++;
  57.             }
  58.         }
  59.         return count;
  60.     }
  61.  
  62.  
  63.     // Helper method for finding the median of a given array
  64.     private int getMedian(int[] inputArray) {
  65.         Arrays.sort(inputArray);
  66.         int length = inputArray.length;
  67.         if (inputArray.length % 2 == 0) {
  68.             int median = (inputArray[length / 2] + inputArray[(length / 2) - 1]) / 2;
  69.             return median;
  70.         }
  71.  
  72.         int median = inputArray[(length - 1) / 2];
  73.         return median;
  74.     }
  75.    
  76.     public void removePhotobomb(String directory, String outputfile) {
  77.  
  78.         Picture[] images = this.generatePictureList(directory);
  79.  
  80.         if (images[0] != null) {
  81.  
  82.             int count = this.getPictureCount(images);
  83.  
  84.             // Gets dimensions of images and creates new Picture object for method output
  85.             int masterWidth = images[0].getWidth();
  86.             int masterHeight = images[0].getHeight();
  87.             Picture outputImage = new Picture(masterWidth, masterHeight);
  88.  
  89.             // 2D arrays that store the median RGB values for each pixel of the output image
  90.             int[][] medianRed = new int[masterHeight][masterWidth];
  91.             int[][] medianGreen = new int[masterHeight][masterWidth];
  92.             int[][] medianBlue = new int[masterHeight][masterWidth];
  93.  
  94.            
  95.             // 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
  96.             System.out.println("Processing color values of all images and computing output median values...");
  97.             for (int r = 0; r < masterHeight; r++) {
  98.                 for (int c = 0; c < masterWidth; c++) {
  99.  
  100.                     // New arrays are initialized to store the RGB values of the specified pixel taken from the set of images
  101.                     int[] redValues = new int[count];
  102.                     int[] greenValues = new int[count];
  103.                     int[] blueValues = new int[count];
  104.  
  105.                     for (int i = 0; i < count; i++) {
  106.                         redValues[i] = images[i].getPixel(r, c).getRed();
  107.                         greenValues[i] = images[i].getPixel(r, c).getGreen();
  108.                         blueValues[i] = images[i].getPixel(r, c).getBlue();
  109.                     }
  110.  
  111.                     // 2D arrays that store the median color values for each pixel
  112.                     medianRed[r][c] = this.getMedian(redValues);
  113.                     medianGreen[r][c] = this.getMedian(greenValues);
  114.                     medianBlue[r][c] = this.getMedian(blueValues);
  115.  
  116.                 }
  117.             }
  118.  
  119.             // Assigns new median color values to each pixel of output image
  120.             System.out.println("Rendering removePhotobomb output image...");
  121.             for (int r = 0; r < masterHeight; r++) {
  122.                 for (int c = 0; c < masterWidth; c++) {
  123.                     outputImage.setPixel(r, c, new Pixel(medianRed[r][c], medianGreen[r][c], medianBlue[r][c]));
  124.                 }
  125.             }
  126.  
  127.             System.out.println("Saving removePhotobomb output image...");
  128.             outputImage.store(outputfile);
  129.             System.out.println("removePhotobomb completed");
  130.  
  131.  
  132.         } else {
  133.             System.out.println("No entries in image list for removePhotobomb method");
  134.         }
  135.  
  136.     }
  137.  
  138.     public void zoomMiddle(String inputfile, String outputfile) {
  139.  
  140.         // Original picture, variables for the original's dimensions and method output picture
  141.         System.out.println("Getting image info for zoomMiddle...");
  142.  
  143.         Picture original = new Picture(inputfile);
  144.  
  145.         int origHeight = original.getHeight();
  146.  
  147.         int origWidth = original.getWidth();
  148.  
  149.         Picture zoomOutput = new Picture(origWidth, origHeight);
  150.  
  151.         // Variables that get the dimensions of the area to be zoomed in and coordinates of area start and end points
  152.         System.out.println("Computing dimensions of area to be zoomed in...");
  153.  
  154.         int zoomAreaHeightStart = origHeight / 4;
  155.  
  156.         int zoomAreaHeightEnd = origHeight - zoomAreaHeightStart;
  157.  
  158.         int zoomAreaHeight = zoomAreaHeightEnd - zoomAreaHeightStart;
  159.  
  160.         int zoomAreaWidthStart = origWidth / 4;
  161.  
  162.         int zoomAreaWidthEnd = origWidth - zoomAreaWidthStart;
  163.  
  164.         int zoomAreaWidth = zoomAreaWidthEnd - zoomAreaWidthStart;
  165.  
  166.        
  167.         // Array containing the pixels of the area to be zoomed in on
  168.         Pixel[][] zoomedArea = new Pixel[zoomAreaHeight][zoomAreaWidth];
  169.  
  170.         for (int r = 0; r < zoomedArea.length; r++) {
  171.             for (int c = 0; c < zoomedArea[r].length; c++) {
  172.                 zoomedArea[r][c] = original.getPixel(zoomAreaHeightStart + r, zoomAreaWidthStart + c);
  173.             }
  174.         }
  175.  
  176.         // Assigns appropriate values from the zoomedArea array to all pixels in the output image
  177.         System.out.println("Rendering zoomMiddle output image...");
  178.         for (int r = 0; r < origHeight; r++) {
  179.             for (int c = 0; c < origWidth; c++) {
  180.                 Pixel zoomReplacement = zoomedArea[r / 2][c / 2];
  181.                 zoomOutput.setPixel(r, c, zoomReplacement);
  182.             }
  183.         }
  184.  
  185.         System.out.println("Saving zoomMiddle output image...");
  186.         zoomOutput.store(outputfile);
  187.         System.out.println("zoomMiddle completed");
  188.  
  189.        
  190.     }
  191.  
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement