Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package edu.cg;
- import java.awt.*;
- import java.awt.image.BufferedImage;
- import java.util.ArrayList;
- public class ImageProcessor extends FunctioalForEachLoops {
- // MARK: fields
- public final Logger logger;
- public final BufferedImage workingImage;
- public final RGBWeights rgbWeights;
- public final int inWidth;
- public final int inHeight;
- public final int workingImageType;
- public final int outWidth;
- public final int outHeight;
- // MARK: constructors
- public ImageProcessor(Logger logger, BufferedImage workingImage, RGBWeights rgbWeights, int outWidth,
- int outHeight) {
- super(); // initializing for each loops...
- this.logger = logger;
- this.workingImage = workingImage;
- this.rgbWeights = rgbWeights;
- inWidth = workingImage.getWidth();
- inHeight = workingImage.getHeight();
- workingImageType = workingImage.getType();
- this.outWidth = outWidth;
- this.outHeight = outHeight;
- setForEachInputParameters();
- }
- public ImageProcessor(Logger logger, BufferedImage workingImage, RGBWeights rgbWeights) {
- this(logger, workingImage, rgbWeights, workingImage.getWidth(), workingImage.getHeight());
- }
- // Changes the picture's hue - example
- public BufferedImage changeHue() {
- logger.log("Prepareing for hue changing...");
- int r = rgbWeights.redWeight;
- int g = rgbWeights.greenWeight;
- int b = rgbWeights.blueWeight;
- int max = rgbWeights.maxWeight;
- BufferedImage ans = newEmptyInputSizedImage();
- forEach((y, x) -> {
- Color c = new Color(workingImage.getRGB(x, y));
- int red = r * c.getRed() / max;
- int green = g * c.getGreen() / max;
- int blue = b * c.getBlue() / max;
- Color color = new Color(red, green, blue);
- ans.setRGB(x, y, color.getRGB());
- });
- logger.log("Changing hue done!");
- return ans;
- }
- // Sets the ForEach parameters with the input dimensions
- public final void setForEachInputParameters() {
- setForEachParameters(inWidth, inHeight);
- }
- // Sets the ForEach parameters with the output dimensions
- public final void setForEachOutputParameters() {
- setForEachParameters(outWidth, outHeight);
- }
- // A helper method that creates an empty image with the specified input dimensions.
- public final BufferedImage newEmptyInputSizedImage() {
- return newEmptyImage(inWidth, inHeight);
- }
- // A helper method that creates an empty image with the specified output dimensions.
- public final BufferedImage newEmptyOutputSizedImage() {
- return newEmptyImage(outWidth, outHeight);
- }
- // A helper method that creates an empty image with the specified dimensions.
- public final BufferedImage newEmptyImage(int width, int height) {
- return new BufferedImage(width, height, workingImageType);
- }
- // A helper method that deep copies the current working image.
- public final BufferedImage duplicateWorkingImage() {
- BufferedImage output = newEmptyInputSizedImage();
- forEach((y, x) -> output.setRGB(x, y, workingImage.getRGB(x, y)));
- return output;
- }
- public BufferedImage greyscale() {
- logger.log("Preparing for hue changing...");
- int r = rgbWeights.redWeight;
- int g = rgbWeights.greenWeight;
- int b = rgbWeights.blueWeight;
- int weightsSum = r + g + b;
- BufferedImage ans = newEmptyInputSizedImage();
- forEach((y, x) -> {
- Color c = new Color(workingImage.getRGB(x, y));
- int red = r * c.getRed() / weightsSum;
- int green = g * c.getGreen() / weightsSum;
- int blue = b * c.getBlue() / weightsSum;
- Color color = new Color(red, green, blue);
- ans.setRGB(x, y, color.getRGB());
- });
- logger.log("Greyscale conversion done!");
- return ans;
- }
- public BufferedImage nearestNeighbor() {
- logger.log("Preparing for nearestNeighbor...");
- float widthRatio = (float) inWidth / outWidth;
- float heightRatio = (float) inHeight / outHeight;
- BufferedImage outputImage = newEmptyOutputSizedImage();
- setForEachOutputParameters();
- // y and x are new image size pixels (outHeight x outWidth)
- forEach((newImageY, newImageX) -> {
- int originalImageX = Math.round(newImageX * widthRatio);
- int originalImageY = Math.round(newImageY * heightRatio);
- int newCorrectedX = Math.min(originalImageX, inWidth - 1);
- int newCorrectedY = Math.min(originalImageY, inHeight - 1);
- outputImage.setRGB(newImageX, newImageY, getNearestNeighborRGB(newCorrectedY, newCorrectedX));
- });
- logger.log("NearestNeighbor done!");
- return outputImage;
- }
- private int getNearestNeighborRGB(int y, int x) {
- ArrayList<Integer> values = new ArrayList();
- if(y == 0 && x == 0) {
- values.add(this.workingImage.getRGB(x+1, y + 1));
- values.add(this.workingImage.getRGB(x, y + 1));
- values.add(this.workingImage.getRGB(x+1, y + 1));
- } else if(y == 0 && x <= inWidth - 1) {
- values.add(this.workingImage.getRGB(x-1, y));
- values.add(this.workingImage.getRGB(x, y + 1));
- values.add(this.workingImage.getRGB(x-1, y + 1));
- } else if (x == 0 && y <= inHeight - 1) {
- values.add(this.workingImage.getRGB(x+1, y));
- values.add(this.workingImage.getRGB(x, y - 1));
- values.add(this.workingImage.getRGB(x+1, y - 1));
- } else if(y == inHeight - 1 && x <= inWidth - 1) {
- values.add(this.workingImage.getRGB(x+1, y-1));
- values.add(this.workingImage.getRGB(x+1, y));
- values.add(this.workingImage.getRGB(x, y - 1));
- } else if (x == inWidth - 1 && y <= inHeight - 1) {
- values.add(this.workingImage.getRGB(x-1, y));
- values.add(this.workingImage.getRGB(x, y + 1));
- values.add(this.workingImage.getRGB(x-1, y + 1));
- } else if (x == inWidth - 1 && y == inHeight - 1) {
- values.add(this.workingImage.getRGB(x-1, y));
- values.add(this.workingImage.getRGB(x, y - 1));
- values.add(this.workingImage.getRGB(x-1, y - 1));
- } else {
- values.add(this.workingImage.getRGB(x-1, y));
- values.add(this.workingImage.getRGB(x-1, y - 1));
- values.add(this.workingImage.getRGB(x, y-1));
- values.add(this.workingImage.getRGB(x+1, y-1));
- values.add(this.workingImage.getRGB(x+1, y));
- values.add(this.workingImage.getRGB(x+1, y +1));
- values.add(this.workingImage.getRGB(x, y+1));
- values.add(this.workingImage.getRGB(x-1, y + 1));
- }
- int currentPixelRGB = this.workingImage.getRGB(x, y);
- int minDifferenceValue = Math.abs(currentPixelRGB - values.get(0));
- int nearestNeighborRGBValue = values.get(0);
- for(int i = 1;i < values.size(); i++) {
- int neighborRGBValue = values.get(i);
- int currentDifference = Math.abs(currentPixelRGB - neighborRGBValue);
- if(currentDifference < minDifferenceValue) {
- minDifferenceValue = currentDifference;
- nearestNeighborRGBValue = neighborRGBValue;
- }
- }
- return nearestNeighborRGBValue;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement