Example usage for java.awt.image Raster createCompatibleWritableRaster

List of usage examples for java.awt.image Raster createCompatibleWritableRaster

Introduction

In this page you can find the example usage for java.awt.image Raster createCompatibleWritableRaster.

Prototype

public WritableRaster createCompatibleWritableRaster(int x, int y, int w, int h) 

Source Link

Document

Create a compatible WritableRaster with the specified location (minX, minY) and size (width, height), a new SampleModel, and a new initialized DataBuffer.

Usage

From source file:org.photovault.image.RawConvOpImage.java

/**
 * Compute single tile of the image//from  w  ww.  j  ava 2  s. c om
 * @param x
 * @param y
 * @return
 */
@Override
public Raster computeTile(int x, int y) {
    if (contrastLut == null) {
        createLumLut();
    }
    Raster r = source.getTile(x, y);
    WritableRaster w = r.createCompatibleWritableRaster(r.getMinX(), r.getMinY(), r.getWidth(), r.getHeight());
    int startX = r.getMinX();
    int startY = r.getMinY();
    for (int l = startY; l < startY + r.getHeight(); l++) {
        for (int c = startX; c < startX + r.getWidth(); c++) {
            long sr = r.getSample(c, l, 0);
            long sg = r.getSample(c, l, 1);
            long sb = r.getSample(c, l, 2);
            long avg = (sr + sg + sb) / 3;
            long m = contrastLut[(int) avg];
            long[] pixel = new long[3];
            pixel[0] = (sr * m) >> 16;
            pixel[1] = (sg * m) >> 16;
            pixel[2] = (sb * m) >> 16;
            long clippedSum = 0;
            long totalHeadroom = 0;
            boolean clipped[] = new boolean[3];
            int channelsClipped = 0;
            int channelsUnclipped = 0;
            for (int n = 0; n < 3; n++) {
                if (pixel[n] > 65535) {
                    channelsClipped++;
                    clipped[n] = true;
                    clippedSum += pixel[n] - 65535;
                } else {
                    clipped[n] = false;
                    totalHeadroom += 65536 - pixel[n];
                    channelsUnclipped++;
                }
            }
            if (channelsClipped > 0) {
                for (int n = 0; n < 3; n++) {
                    if (!clipped[n]) {
                        // Spread the clipped energy to other channels so that
                        // they reach saturation at the same time
                        long headroom = 65536 - pixel[n];
                        pixel[n] += clippedSum * headroom / totalHeadroom;
                    }
                }
            }
            //                while ( channelsClipped > 0 && clippedSum > 0 &&
            //                        channelsUnclipped > 0 ) {
            //                    long spreaded = 0;
            //                    long spreadPerChan = clippedSum / channelsUnclipped +1;
            //                    for ( int n = 0; n < 3; n++ ) {
            //                        if ( !clipped[n] ) {
            //                            long add = Math.min( spreadPerChan, 65536 - pixel[n] );
            //                            pixel[n] += add;
            //                            spreaded += add;
            //                            if ( pixel[n] > 65535 ) {
            //                                channelsUnclipped--;
            //                            }
            //                        }
            //                    }
            //                    clippedSum -= spreaded;
            //                }
            try {
                w.setSample(c, l, 0, Math.min(65535, pixel[0]));
                w.setSample(c, l, 1, Math.min(65535, pixel[1]));
                w.setSample(c, l, 2, Math.min(65535, pixel[2]));
            } catch (ArrayIndexOutOfBoundsException e) {
                log.error(e);
            }
        }
    }
    return w;
}