Example usage for java.awt.image BufferedImage setData

List of usage examples for java.awt.image BufferedImage setData

Introduction

In this page you can find the example usage for java.awt.image BufferedImage setData.

Prototype

public void setData(Raster r) 

Source Link

Document

Sets a rectangular region of the image to the contents of the specified Raster r , which is assumed to be in the same coordinate space as the BufferedImage .

Usage

From source file:tilt.image.Picture.java

/**
 * Convert from greyscale to twotone (black and white)
 * Adapted from OCRopus//  ww w  .ja v a2s . c o  m
 * Copyright 2006-2008 Deutsches Forschungszentrum fuer Kuenstliche 
 * Intelligenz or its licensors, as applicable.
 * http://ocropus.googlecode.com/svn/trunk/ocr-binarize/ocr-binarize-sauvola.cc
 * @throws Exception 
 */
void convertToTwoTone() throws ImageException {
    try {
        int MAXVAL = 256;
        double k = 0.34;
        if (greyscale == null)
            convertToGreyscale();
        BufferedImage grey = ImageIO.read(greyscale);
        WritableRaster grey_image = grey.getRaster();
        WritableRaster bin_image = grey.copyData(null);
        int square = (int) Math.floor(grey_image.getWidth() * 0.025);
        if (square == 0)
            square = Math.min(20, grey_image.getWidth());
        if (square > grey_image.getHeight())
            square = grey_image.getHeight();
        int whalf = square >> 1;
        if (whalf == 0)
            throw new Exception("whalf==0!");
        int image_width = grey_image.getWidth();
        int image_height = grey_image.getHeight();
        // Calculate the integral image, and integral of the squared image
        // original algorithm ate up too much memory, use floats for longs
        float[][] integral_image = new float[image_width][image_height];
        float[][] rowsum_image = new float[image_width][image_height];
        float[][] integral_sqimg = new float[image_width][image_height];
        float[][] rowsum_sqimg = new float[image_width][image_height];
        int xmin, ymin, xmax, ymax;
        double diagsum, idiagsum, diff, sqdiagsum, sqidiagsum, sqdiff, area;
        double mean, std, threshold;
        // for get/setPixel
        int[] iArray = new int[1];
        int[] oArray = new int[1];
        for (int j = 0; j < image_height; j++) {
            grey_image.getPixel(0, j, iArray);
            rowsum_image[0][j] = iArray[0];
            rowsum_sqimg[0][j] = iArray[0] * iArray[0];
        }
        for (int i = 1; i < image_width; i++) {
            for (int j = 0; j < image_height; j++) {
                grey_image.getPixel(i, j, iArray);
                rowsum_image[i][j] = rowsum_image[i - 1][j] + iArray[0];
                rowsum_sqimg[i][j] = rowsum_sqimg[i - 1][j] + iArray[0] * iArray[0];
            }
        }
        for (int i = 0; i < image_width; i++) {
            integral_image[i][0] = rowsum_image[i][0];
            integral_sqimg[i][0] = rowsum_sqimg[i][0];
        }
        for (int i = 0; i < image_width; i++) {
            for (int j = 1; j < image_height; j++) {
                integral_image[i][j] = integral_image[i][j - 1] + rowsum_image[i][j];
                integral_sqimg[i][j] = integral_sqimg[i][j - 1] + rowsum_sqimg[i][j];
            }
        }
        // compute mean and std.dev. using the integral image
        for (int i = 0; i < image_width; i++) {
            for (int j = 0; j < image_height; j++) {
                xmin = Math.max(0, i - whalf);
                ymin = Math.max(0, j - whalf);
                xmax = Math.min(image_width - 1, i + whalf);
                ymax = Math.min(image_height - 1, j + whalf);
                area = (xmax - xmin + 1) * (ymax - ymin + 1);
                grey_image.getPixel(i, j, iArray);
                // area can't be 0 here
                if (area == 0)
                    throw new Exception("area can't be 0 here!");
                if (xmin == 0 && ymin == 0) {
                    // Point at origin
                    diff = integral_image[xmax][ymax];
                    sqdiff = integral_sqimg[xmax][ymax];
                } else if (xmin == 0 && ymin != 0) {
                    // first column
                    diff = integral_image[xmax][ymax] - integral_image[xmax][ymin - 1];
                    sqdiff = integral_sqimg[xmax][ymax] - integral_sqimg[xmax][ymin - 1];
                } else if (xmin != 0 && ymin == 0) {
                    // first row
                    diff = integral_image[xmax][ymax] - integral_image[xmin - 1][ymax];
                    sqdiff = integral_sqimg[xmax][ymax] - integral_sqimg[xmin - 1][ymax];
                } else {
                    // rest of the image
                    diagsum = integral_image[xmax][ymax] + integral_image[xmin - 1][ymin - 1];
                    idiagsum = integral_image[xmax][ymin - 1] + integral_image[xmin - 1][ymax];
                    diff = diagsum - idiagsum;
                    sqdiagsum = integral_sqimg[xmax][ymax] + integral_sqimg[xmin - 1][ymin - 1];
                    sqidiagsum = integral_sqimg[xmax][ymin - 1] + integral_sqimg[xmin - 1][ymax];
                    sqdiff = sqdiagsum - sqidiagsum;
                }
                mean = diff / area;
                std = Math.sqrt((sqdiff - diff * diff / area) / (area - 1));
                threshold = mean * (1 + k * ((std / 128) - 1));
                if (iArray[0] < threshold)
                    oArray[0] = 0;
                else
                    oArray[0] = MAXVAL - 1;
                bin_image.setPixel(i, j, oArray);
            }
        }
        twotone = File.createTempFile(PictureRegistry.PREFIX, PictureRegistry.SUFFIX);
        grey.setData(bin_image);
        ImageIO.write(grey, "png", twotone);
    } catch (Exception e) {
        throw new ImageException(e);
    }
}