Advertisement
chloelwt

Untitled

Jan 19th, 2020
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.95 KB | None | 0 0
  1. package picture;
  2.  
  3. import com.google.common.base.Preconditions;
  4. import java.util.List;
  5. import static picture.Utils.*;
  6.  
  7. public class Process {
  8.  
  9.   /**
  10.    * @param inputpath the path where the input is from
  11.    * @param outputpath the path where the file is exported to
  12.    */
  13.   public static void invert(String inputpath, String outputpath) {
  14.     final int colorSize = 255;
  15.     Picture picture = loadPicture(inputpath);
  16.     for (int i = 0; i < picture.getWidth(); i++) {
  17.       for (int j = 0; j < picture.getHeight(); j++) {
  18.         int red = picture.getPixel(i, j).getRed();
  19.         int green = picture.getPixel(i, j).getGreen();
  20.         int blue = picture.getPixel(i, j).getBlue();
  21.         // reversing the colours
  22.         int newRed = colorSize - red;
  23.         int newGreen = colorSize - green;
  24.         int newBlue = colorSize - blue;
  25.  
  26.         Color color = picture.getPixel(i, j);
  27.         color.setRed(newRed);
  28.         color.setGreen(newGreen);
  29.         color.setBlue(newBlue);
  30.         picture.setPixel(i, j, color);
  31.       }
  32.     }
  33.     savePicture(picture, outputpath);
  34.   }
  35.  
  36.   /**
  37.    * @param inputpath the path where the input is from
  38.    * @param outputpath the path where the file is exported to
  39.    */
  40.   public static void grayscale(String inputpath, String outputpath) {
  41.     Picture picture = loadPicture(inputpath);
  42.     for (int i = 0; i < picture.getWidth(); i++) {
  43.       for (int j = 0; j < picture.getHeight(); j++) {
  44.         int red = picture.getPixel(i, j).getRed();
  45.         int green = picture.getPixel(i, j).getGreen();
  46.         int blue = picture.getPixel(i, j).getBlue();
  47.  
  48.         int avg = (red + green + blue) / 3;
  49.         // taking the average colour to produce a grayscale
  50.         Color color = picture.getPixel(i, j);
  51.         color.setRed(avg);
  52.         color.setGreen(avg);
  53.         color.setBlue(avg);
  54.         picture.setPixel(i, j, color);
  55.       }
  56.     }
  57.     savePicture(picture, outputpath);
  58.   }
  59.  
  60.   /**
  61.    * @param angle angle to rotate the image by, in the option of 9, 180 and 270
  62.    * @param inputpath the path where the input is from
  63.    * @param outputpath the path where the file is exported to
  64.    */
  65.   public static void rotate(int angle, String inputpath, String outputpath) {
  66.     Preconditions.checkArgument(angle == 90 || angle == 180 || angle == 270, "angle not valid");
  67.     // ensures angle is 90, 180 or 270
  68.     Picture pic = loadPicture(inputpath);
  69.     switch (angle) {
  70.         // It could be possible to apply 90 degree turn to the other cases
  71.         // but it deemed less efficient as you would have to loop through
  72.         // multiple times
  73.       case 90:
  74.         Picture rPic = createPicture(pic.getHeight(), pic.getWidth());
  75.         for (int i = 0; i < pic.getWidth(); i++) {
  76.           for (int j = 0; j < pic.getHeight(); j++) {
  77.  
  78.             Color color = pic.getPixel(i, j);
  79.             // rotating to the right 90 degs would simply shift the
  80.             // horizontal to the vertical and the vertical flipped
  81.             // then mapped to horizontal
  82.             int newi = pic.getHeight() - 1 - j;
  83.             int newj = i;
  84.  
  85.             rPic.setPixel(newi, newj, color);
  86.           }
  87.         }
  88.         savePicture(rPic, outputpath);
  89.         break;
  90.  
  91.       case 180:
  92.         Picture fPic = createPicture(pic.getWidth(), pic.getHeight());
  93.         for (int i = 0; i < pic.getWidth(); i++) {
  94.           for (int j = 0; j < pic.getHeight(); j++) {
  95.  
  96.             Color color = pic.getPixel(i, j);
  97.             // turning 180 degs is just flipping the vertical index
  98.             // and the horizontal index
  99.             int newi = pic.getWidth() - 1 - i;
  100.             int newj = pic.getHeight() - 1 - j;
  101.  
  102.             fPic.setPixel(newi, newj, color);
  103.           }
  104.         }
  105.         savePicture(fPic, outputpath);
  106.         break;
  107.  
  108.       case 270:
  109.         Picture lPic = createPicture(pic.getHeight(), pic.getWidth());
  110.         for (int i = 0; i < pic.getWidth(); i++) {
  111.           for (int j = 0; j < pic.getHeight(); j++) {
  112.  
  113.             Color color = pic.getPixel(i, j);
  114.             // rotating to the right 270 degs would simply shift be
  115.             // equal to shifting to the left 90 degs so the vertical
  116.             // shifted to the horizontal and horizontal flipped then
  117.             // mapped to vertical
  118.             int newi = j;
  119.             int newj = pic.getWidth() - 1 - i;
  120.  
  121.             lPic.setPixel(newi, newj, color);
  122.           }
  123.         }
  124.         savePicture(lPic, outputpath);
  125.         break;
  126.     }
  127.   }
  128.  
  129.   /**
  130.    * @param dir direction of flip, in either (H)orizontal or (V)ertical
  131.    * @param inputpath the path where the input is from
  132.    * @param outputpath the path where the file is exported to
  133.    */
  134.   public static void flip(String dir, String inputpath, String outputpath) {
  135.     Picture picture = loadPicture(inputpath);
  136.     Picture pic = createPicture(picture.getWidth(), picture.getHeight());
  137.     switch (dir) {
  138.       case "H":
  139.         for (int i = 0; i < picture.getWidth(); i++) {
  140.           for (int j = 0; j < picture.getHeight(); j++) {
  141.  
  142.             Color color = picture.getPixel(i, j);
  143.             // horizontal flips simply flip the horizontal indices
  144.             // and maintain the vertical
  145.             int flipi = picture.getWidth() - 1 - i;
  146.             int flipj = j;
  147.             // flipj is a useless assignment but it makes the
  148.             // coordinates clearer
  149.  
  150.             pic.setPixel(flipi, flipj, color);
  151.           }
  152.         }
  153.        savePicture(pic, outputpath);
  154.         break;
  155.       case "V":
  156.         for (int i = 0; i < picture.getWidth(); i++) {
  157.           for (int j = 0; j < picture.getHeight(); j++) {
  158.  
  159.             Color color = picture.getPixel(i, j);
  160.             // similarly vertical flips simply flip the vertiical
  161.             // indices and maintain the horizontal
  162.             int flipi = i;
  163.             // flipi is a useless assignment but it makes the
  164.             // coordinates clearer
  165.             int flipj = picture.getHeight() - 1 - j;
  166.  
  167.             pic.setPixel(flipi, flipj, color);
  168.           }
  169.         }
  170.         savePicture(pic, outputpath);
  171.         break;
  172.     }
  173.   }
  174.  
  175.   /**
  176.    * @param inputpaths the path where the inputs are from
  177.    * @param outputpath the path where the file is exported to
  178.    */
  179.   public static void blend(List<String> inputpaths, String outputpath) {
  180.     Picture picture = loadPicture(inputpaths.get(0));
  181.     int minWidth = picture.getWidth();
  182.     int minHeight = picture.getHeight();
  183.     for (String inputpath : inputpaths) {
  184.       Picture pic = loadPicture(inputpath);
  185.       // obtaining the min width
  186.       if (pic.getWidth() < minWidth) {
  187.         minWidth = pic.getWidth();
  188.       }
  189.       // obtaining the min height
  190.       if (pic.getHeight() < minHeight) {
  191.         minHeight = pic.getHeight();
  192.       }
  193.     }
  194.     Picture result = createPicture(minWidth, minHeight);
  195.     // this creates a proper canvas with the min size bounded by the given
  196.     // files in the array
  197.     for (int i = 0; i < minWidth; i++) {
  198.       for (int j = 0; j < minHeight; j++) {
  199.         int sumRed = 0;
  200.         int sumGreen = 0;
  201.         int sumBlue = 0;
  202.         int counter = 0;
  203.         for (String inputpath : inputpaths) {
  204.           Picture pic = loadPicture(inputpath);
  205.           Color color = pic.getPixel(i, j);
  206.           // summing the colours to obtain mean for blend
  207.           sumRed += color.getRed();
  208.           sumGreen += color.getGreen();
  209.           sumBlue += color.getBlue();
  210.           counter++;
  211.         }
  212.         // calculating the means
  213.         int avgRed = sumRed / counter;
  214.         int avgGreen = sumGreen / counter;
  215.         int avgBlue = sumBlue / counter;
  216.  
  217.         Color color = picture.getPixel(i, j);
  218.         color.setRed(avgRed);
  219.         color.setGreen(avgGreen);
  220.         color.setBlue(avgBlue);
  221.  
  222.         result.setPixel(i, j, color);
  223.       }
  224.     }
  225.     savePicture(result, outputpath);
  226.   }
  227.  
  228.   /**
  229.    * @param inputpath the path where the input is from
  230.    * @param outputpath the path where the file is exported to
  231.    */
  232.   public static void blur(String inputpath, String outputpath) {
  233.     Picture picture = loadPicture(inputpath);
  234.     Picture pic = createPicture(picture.getWidth(), picture.getHeight());
  235.     for (int i = 1; i < picture.getWidth() - 1; i++) {
  236.       for (int j = 1; j < picture.getHeight() - 1; j++) {
  237.         // produces blur for non-edge pixels by collecting the side pixel's
  238.         // RGB values and finding the mean of each colour
  239.         int redSum = 0;
  240.         int greenSum = 0;
  241.         int blueSum = 0;
  242.         int counter = 0;
  243.         for (int x = i - 1; x < (i + 2); x++) {
  244.           for (int y = j - 1; y < (j + 2); y++) {
  245.             redSum += picture.getPixel(x, y).getRed();
  246.             greenSum += picture.getPixel(x, y).getGreen();
  247.             blueSum += picture.getPixel(x, y).getBlue();
  248.             counter++;
  249.           }
  250.         }
  251.         int avgRed = redSum / counter;
  252.         int avgGreen = greenSum / counter;
  253.         int avgBlue = blueSum / counter;
  254.         Color color = picture.getPixel(i, j);
  255.         color.setRed(avgRed);
  256.         color.setBlue(avgBlue);
  257.         color.setGreen(avgGreen);
  258.         pic.setPixel(i, j, color);
  259.       }
  260.     }
  261.     for (int x = 0; x < picture.getWidth() - 1; x++) {
  262.       // initialising the top and bottom rows
  263.       Color top = picture.getPixel(x, 0);
  264.       Color bottom = picture.getPixel(x, picture.getHeight() - 1);
  265.       pic.setPixel(x, 0, top);
  266.       pic.setPixel(x, picture.getHeight() - 1, bottom);
  267.     }
  268.  
  269.     for (int y = 0; y < picture.getHeight(); y++) {
  270.       // initialising the left and right columns
  271.       Color left = picture.getPixel(0, y);
  272.       Color right = picture.getPixel(picture.getWidth() - 1, y);
  273.       pic.setPixel(0, y, left);
  274.       pic.setPixel(picture.getWidth() - 1, y, right);
  275.     }
  276.     savePicture(pic, outputpath);
  277.   }
  278.  
  279.   /**
  280.    * @param inputps the path where the inputs are from
  281.    * @param outputpath the path where the file is exported to
  282.    */
  283.   public static void mosaic(List<String> inputps, String outputpath) {
  284.     Picture picture = loadPicture(inputps.get(0));
  285.     int size = inputps.size();
  286.     int min = 0;
  287.     int minWidth = picture.getWidth();
  288.     int minHeight = picture.getHeight();
  289.     for (String inputp : inputps) {
  290.       Picture pic = loadPicture(inputp);
  291.       // obtaining the min width
  292.       if (pic.getWidth() < minWidth) {
  293.         minWidth = pic.getWidth();
  294.       }
  295.       // obtaining the min height
  296.       if (pic.getHeight() < minHeight) {
  297.         minHeight = pic.getHeight();
  298.       }
  299.     }
  300.     if (minHeight < minWidth){
  301.       min = minHeight;
  302.     } else {
  303.       min = minWidth;
  304.     }
  305.     Picture result = createPicture(min, min);
  306.  
  307.     for (int i = 0; i < min; i++) {
  308.       for (int j = 0; j < min; j++) {
  309.           // this allows the pixels to alternatively horizontally
  310.           Color color = loadPicture(inputps.get((((i + j - 2) % size) + size) % size)).getPixel(i, j);
  311.           result.setPixel(i, j, color);
  312.       }
  313.     }
  314.     savePicture(result, outputpath);
  315.   }
  316. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement