app.utils.ImageUtilities.java Source code

Java tutorial

Introduction

Here is the source code for app.utils.ImageUtilities.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package app.utils;

import app.model.Image;
import app.model.Pixel;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import static java.awt.image.ImageObserver.WIDTH;
import java.io.File;
import java.io.IOException;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;

/**
 *
 * @author agnieszka.kluska
 */
public class ImageUtilities {

    private static final Logger LOG = LoggerFactory.getLogger(ImageUtilities.class);
    private static final String DEFAULT_FILE_SAVE_PATH = "images\\";
    private static final String DEFAULT_FILE_EXTENSION = "png";

    public static Image readImageFromFile(File imgFile) throws IOException {
        String imgName = imgFile.getName();
        BufferedImage img = ImageIO.read(imgFile);
        Pixel[][] pixels = new Pixel[img.getWidth()][img.getHeight()];
        for (int x = 0; x < img.getWidth(); x++) {
            for (int y = 0; y < img.getHeight(); y++) {
                int redValue = new Color(img.getRGB(x, y)).getRed();
                int greenValue = new Color(img.getRGB(x, y)).getGreen();
                int blueValue = new Color(img.getRGB(x, y)).getBlue();
                pixels[x][y] = new Pixel(redValue, greenValue, blueValue);
            }
        }
        return new Image(imgName, pixels, img.getRaster().getNumDataElements());
    }

    public static void saveImageToFile(Image image, String filterName) throws IOException {
        //TODO logger
        File imgFile = new File(DEFAULT_FILE_SAVE_PATH + image.getName().split("\\.")[0] + "_" + filterName + "."
                + DEFAULT_FILE_EXTENSION);
        BufferedImage bufferedImage = convertImageToBufferedImage(image, BufferedImage.TYPE_INT_RGB);
        ImageIO.write(bufferedImage, DEFAULT_FILE_EXTENSION, imgFile);
    }

    public static BufferedImage convertImageToBufferedImage(Image image, int IMAGE_TYPE) {
        final int width = image.getWidth();
        final int height = image.getHeight();
        BufferedImage result = new BufferedImage(width, height, IMAGE_TYPE);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                int rgbValue = getRgbValue(image.getPixels()[x][y]);
                result.setRGB(x, y, rgbValue);
            }
        }
        return result;
    }

    public static int getRgbValue(Pixel pixel) {
        return pixel.getRed() << 16 | pixel.getGreen() << 8 | pixel.getBlue();
    }

    public static int getGrayScaleValue(Pixel pixel) {
        return (int) (0.299 * pixel.getRed() + 0.587 * pixel.getGreen() + 0.114 * pixel.getBlue());
    }

    public static int[] convertRBA(int pixel) {
        int[] result = new int[4];
        result[0] = (pixel >> 16) & 0xff;//red
        result[1] = (pixel >> 8) & 0xff;//green
        result[2] = (pixel) & 0xff;//blue
        result[3] = (pixel >> 24) & 0xff;//alpha
        return result;
    }

    public static int carculateLut(int value) {
        return Math.max(Math.min(value, 255), 0);
    }

    public static void saveHistogramToFile(JFreeChart chart) {

        try {
            File chartFile = new File(DEFAULT_FILE_SAVE_PATH + "histogram" + "." + DEFAULT_FILE_EXTENSION);
            ChartUtilities.saveChartAsJPEG(chartFile, chart, 400, 250);
        } catch (IOException ioe) {
            LOG.error("Error while saving chart to file.\n" + ioe.getMessage());
        }
    }
}