Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.63 KB | None | 0 0
  1. package edu.cg;
  2.  
  3. import java.awt.*;
  4. import java.awt.image.BufferedImage;
  5. import java.util.ArrayList;
  6.  
  7. public class ImageProcessor extends FunctioalForEachLoops {
  8.     // MARK: fields
  9.     public final Logger logger;
  10.     public final BufferedImage workingImage;
  11.     public final RGBWeights rgbWeights;
  12.     public final int inWidth;
  13.     public final int inHeight;
  14.     public final int workingImageType;
  15.     public final int outWidth;
  16.     public final int outHeight;
  17.  
  18.     // MARK: constructors
  19.     public ImageProcessor(Logger logger, BufferedImage workingImage, RGBWeights rgbWeights, int outWidth,
  20.             int outHeight) {
  21.         super(); // initializing for each loops...
  22.  
  23.         this.logger = logger;
  24.         this.workingImage = workingImage;
  25.         this.rgbWeights = rgbWeights;
  26.         inWidth = workingImage.getWidth();
  27.         inHeight = workingImage.getHeight();
  28.         workingImageType = workingImage.getType();
  29.         this.outWidth = outWidth;
  30.         this.outHeight = outHeight;
  31.         setForEachInputParameters();
  32.     }
  33.  
  34.     public ImageProcessor(Logger logger, BufferedImage workingImage, RGBWeights rgbWeights) {
  35.         this(logger, workingImage, rgbWeights, workingImage.getWidth(), workingImage.getHeight());
  36.     }
  37.  
  38.     // Changes the picture's hue - example
  39.     public BufferedImage changeHue() {
  40.         logger.log("Prepareing for hue changing...");
  41.  
  42.         int r = rgbWeights.redWeight;
  43.         int g = rgbWeights.greenWeight;
  44.         int b = rgbWeights.blueWeight;
  45.         int max = rgbWeights.maxWeight;
  46.  
  47.         BufferedImage ans = newEmptyInputSizedImage();
  48.  
  49.         forEach((y, x) -> {
  50.             Color c = new Color(workingImage.getRGB(x, y));
  51.             int red = r * c.getRed() / max;
  52.             int green = g * c.getGreen() / max;
  53.             int blue = b * c.getBlue() / max;
  54.             Color color = new Color(red, green, blue);
  55.             ans.setRGB(x, y, color.getRGB());
  56.         });
  57.  
  58.         logger.log("Changing hue done!");
  59.  
  60.         return ans;
  61.     }
  62.  
  63.     // Sets the ForEach parameters with the input dimensions
  64.     public final void setForEachInputParameters() {
  65.         setForEachParameters(inWidth, inHeight);
  66.     }
  67.  
  68.     // Sets the ForEach parameters with the output dimensions
  69.     public final void setForEachOutputParameters() {
  70.         setForEachParameters(outWidth, outHeight);
  71.     }
  72.  
  73.     // A helper method that creates an empty image with the specified input dimensions.
  74.     public final BufferedImage newEmptyInputSizedImage() {
  75.         return newEmptyImage(inWidth, inHeight);
  76.     }
  77.  
  78.     // A helper method that creates an empty image with the specified output dimensions.
  79.     public final BufferedImage newEmptyOutputSizedImage() {
  80.         return newEmptyImage(outWidth, outHeight);
  81.     }
  82.  
  83.     // A helper method that creates an empty image with the specified dimensions.
  84.     public final BufferedImage newEmptyImage(int width, int height) {
  85.         return new BufferedImage(width, height, workingImageType);
  86.     }
  87.  
  88.     // A helper method that deep copies the current working image.
  89.     public final BufferedImage duplicateWorkingImage() {
  90.         BufferedImage output = newEmptyInputSizedImage();
  91.  
  92.         forEach((y, x) -> output.setRGB(x, y, workingImage.getRGB(x, y)));
  93.  
  94.         return output;
  95.     }
  96.    
  97.     public BufferedImage greyscale() {
  98.         logger.log("Preparing for hue changing...");
  99.  
  100.         int r = rgbWeights.redWeight;
  101.         int g = rgbWeights.greenWeight;
  102.         int b = rgbWeights.blueWeight;
  103.         int weightsSum = r + g + b;
  104.  
  105.         BufferedImage ans = newEmptyInputSizedImage();
  106.  
  107.         forEach((y, x) -> {
  108.             Color c = new Color(workingImage.getRGB(x, y));
  109.             int red = r * c.getRed() / weightsSum;
  110.             int green = g * c.getGreen() / weightsSum;
  111.             int blue = b * c.getBlue() / weightsSum;
  112.             Color color = new Color(red, green, blue);
  113.             ans.setRGB(x, y, color.getRGB());
  114.         });
  115.  
  116.         logger.log("Greyscale conversion done!");
  117.  
  118.         return ans;
  119.     }
  120.  
  121.     public BufferedImage nearestNeighbor() {
  122.         logger.log("Preparing for nearestNeighbor...");
  123.  
  124.         float widthRatio =  (float) inWidth / outWidth;
  125.         float heightRatio = (float) inHeight / outHeight;
  126.  
  127.         BufferedImage outputImage = newEmptyOutputSizedImage();
  128.         setForEachOutputParameters();
  129.         // y and x are new image size pixels (outHeight x outWidth)
  130.         forEach((newImageY, newImageX) -> {
  131.             int originalImageX = Math.round(newImageX * widthRatio);
  132.             int originalImageY = Math.round(newImageY * heightRatio);
  133.  
  134.             int newCorrectedX = Math.min(originalImageX, inWidth - 1);
  135.             int newCorrectedY = Math.min(originalImageY, inHeight - 1);
  136.  
  137.             outputImage.setRGB(newImageX, newImageY, getNearestNeighborRGB(newCorrectedY, newCorrectedX));
  138.         });
  139.  
  140.         logger.log("NearestNeighbor done!");
  141.  
  142.         return outputImage;
  143.     }
  144.  
  145.     private int getNearestNeighborRGB(int y, int x) {
  146.         ArrayList<Integer> values = new ArrayList();
  147.  
  148.         if(y == 0 && x == 0) {
  149.             values.add(this.workingImage.getRGB(x+1, y + 1));
  150.             values.add(this.workingImage.getRGB(x, y + 1));
  151.             values.add(this.workingImage.getRGB(x+1, y + 1));
  152.         } else if(y == 0 && x <= inWidth - 1) {
  153.             values.add(this.workingImage.getRGB(x-1, y));
  154.             values.add(this.workingImage.getRGB(x, y + 1));
  155.             values.add(this.workingImage.getRGB(x-1, y + 1));
  156.         } else if (x == 0 && y <= inHeight - 1) {
  157.             values.add(this.workingImage.getRGB(x+1, y));
  158.             values.add(this.workingImage.getRGB(x, y - 1));
  159.             values.add(this.workingImage.getRGB(x+1, y - 1));
  160.         } else if(y == inHeight - 1 && x <= inWidth - 1) {
  161.             values.add(this.workingImage.getRGB(x+1, y-1));
  162.             values.add(this.workingImage.getRGB(x+1, y));
  163.             values.add(this.workingImage.getRGB(x, y - 1));
  164.         } else if (x == inWidth - 1 && y <= inHeight - 1) {
  165.             values.add(this.workingImage.getRGB(x-1, y));
  166.             values.add(this.workingImage.getRGB(x, y + 1));
  167.             values.add(this.workingImage.getRGB(x-1, y + 1));
  168.         } else if (x == inWidth - 1 && y == inHeight - 1) {
  169.             values.add(this.workingImage.getRGB(x-1, y));
  170.             values.add(this.workingImage.getRGB(x, y - 1));
  171.             values.add(this.workingImage.getRGB(x-1, y - 1));
  172.         } else {
  173.             values.add(this.workingImage.getRGB(x-1, y));
  174.             values.add(this.workingImage.getRGB(x-1, y - 1));
  175.             values.add(this.workingImage.getRGB(x, y-1));
  176.             values.add(this.workingImage.getRGB(x+1, y-1));
  177.             values.add(this.workingImage.getRGB(x+1, y));
  178.             values.add(this.workingImage.getRGB(x+1, y +1));
  179.             values.add(this.workingImage.getRGB(x, y+1));
  180.             values.add(this.workingImage.getRGB(x-1, y + 1));
  181.         }
  182.  
  183.         int currentPixelRGB = this.workingImage.getRGB(x, y);
  184.         int minDifferenceValue = Math.abs(currentPixelRGB - values.get(0));
  185.         int nearestNeighborRGBValue = values.get(0);
  186.  
  187.         for(int i = 1;i < values.size(); i++) {
  188.             int neighborRGBValue = values.get(i);
  189.             int currentDifference = Math.abs(currentPixelRGB - neighborRGBValue);
  190.  
  191.             if(currentDifference < minDifferenceValue) {
  192.                 minDifferenceValue = currentDifference;
  193.                 nearestNeighborRGBValue = neighborRGBValue;
  194.             }
  195.         }
  196.  
  197.         return nearestNeighborRGBValue;
  198.     }
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement