Example usage for java.awt.image WritableRaster setPixels

List of usage examples for java.awt.image WritableRaster setPixels

Introduction

In this page you can find the example usage for java.awt.image WritableRaster setPixels.

Prototype

public void setPixels(int x, int y, int w, int h, double[] dArray) 

Source Link

Document

Sets all samples for a rectangle of pixels from a double array containing one sample per array element.

Usage

From source file:org.apache.pdfbox.pdmodel.graphics.shading.Type1ShadingContext.java

@Override
public Raster getRaster(int x, int y, int w, int h) {
    WritableRaster raster = getColorModel().createCompatibleWritableRaster(w, h);
    int[] data = new int[w * h * 4];
    for (int j = 0; j < h; j++) {
        int currentY = y + j;
        if (bboxRect != null && (currentY < minBBoxY || currentY > maxBBoxY)) {
            continue;
        }//from  w  ww .  java 2 s. c o  m

        for (int i = 0; i < w; i++) {
            int currentX = x + i;
            if (bboxRect != null && (currentX < minBBoxX || currentX > maxBBoxX)) {
                continue;
            }

            int index = (j * w + i) * 4;
            boolean useBackground = false;
            float[] values = new float[] { x + i, y + j };
            rat.transform(values, 0, values, 0, 1);
            if (values[0] < domain[0] || values[0] > domain[1] || values[1] < domain[2]
                    || values[1] > domain[3]) {
                if (getBackground() == null) {
                    continue;
                }
                useBackground = true;
            }

            // evaluate function
            if (useBackground) {
                values = getBackground();
            } else {
                try {
                    values = type1ShadingType.evalFunction(values);
                } catch (IOException e) {
                    LOG.error("error while processing a function", e);
                }
            }

            // convert color values from shading color space to RGB
            PDColorSpace shadingColorSpace = getShadingColorSpace();
            if (shadingColorSpace != null) {
                try {
                    values = shadingColorSpace.toRGB(values);
                } catch (IOException e) {
                    LOG.error("error processing color space", e);
                }
            }
            data[index] = (int) (values[0] * 255);
            data[index + 1] = (int) (values[1] * 255);
            data[index + 2] = (int) (values[2] * 255);
            data[index + 3] = 255;
        }
    }
    raster.setPixels(0, 0, w, h, data);
    return raster;
}

From source file:com.AandR.beans.plotting.imagePlotPanel.CanvasPanel.java

/**
 *
 * @param dataLocal/*from   w  w w  .j a  va2s .  com*/
 * @param isAutoScaled
 * @return
 */
public BufferedImage convertDataToImage(double[][] data, double dataMin, double dataMax) {
    BufferedImage image = new BufferedImage(data.length, data[0].length, BufferedImage.TYPE_INT_RGB);
    WritableRaster raster = image.getRaster();

    double thisValue;
    int[] thisRowOfPixels = new int[3 * data.length];
    int[] thisPixelValue = new int[3];
    int i, j;
    double scale = 255.0 / (dataMax - dataMin);
    for (j = 0; j < data[0].length; j++) {
        for (i = 0; i < data.length; i++) {
            thisValue = scale * (data[i][j] - dataMin);
            //if(thisValue<0.0)
            //  thisValue = 0.0;
            //else if(thisValue>255.0)
            //  thisValue = 255.0;

            thisPixelValue = colorMap.getColorValue((int) thisValue);
            thisRowOfPixels[3 * i + 0] = thisPixelValue[0];
            thisRowOfPixels[3 * i + 1] = thisPixelValue[1];
            thisRowOfPixels[3 * i + 2] = thisPixelValue[2];
        }
        raster.setPixels(0, j, data.length, 1, thisRowOfPixels);
    }
    return image;
}

From source file:org.apache.pdfbox.pdmodel.graphics.shading.RadialShadingContext.java

/**
 * {@inheritDoc}/*from w ww .  j  av a2  s .  c o m*/
 */
public Raster getRaster(int x, int y, int w, int h) {
    // create writable raster
    WritableRaster raster = getColorModel().createCompatibleWritableRaster(w, h);
    float[] input = new float[1];
    float inputValue;
    int[] data = new int[w * h * 3];
    for (int j = 0; j < h; j++) {
        for (int i = 0; i < w; i++) {
            float[] inputValues = calculateInputValues(x + i, y + j);
            // choose 1 of the 2 values
            if (inputValues[0] >= domain[0] && inputValues[0] <= domain[1]) {
                // both values are in the domain -> choose the larger one 
                if (inputValues[1] >= domain[0] && inputValues[1] <= domain[1]) {
                    inputValue = Math.max(inputValues[0], inputValues[1]);
                }
                // first value is in the domain, the second not -> choose first value
                else {
                    inputValue = inputValues[0];
                }
            } else {
                // first value is not in the domain, but the second -> choose second value
                if (inputValues[1] >= domain[0] && inputValues[1] <= domain[1]) {
                    inputValue = inputValues[1];
                }
                // TODO
                // both are not in the domain -> choose the first as I don't know it better
                else {
                    inputValue = inputValues[0];
                }
            }
            // input value is out of range
            if (inputValue < domain[0]) {
                // the shading has to be extended if extend[0] == true
                if (extend[0]) {
                    inputValue = domain[0];
                } else {
                    continue;
                }
            }
            // input value is out of range
            else if (inputValue > domain[1]) {
                // the shading has to be extended if extend[1] == true
                if (extend[1]) {
                    inputValue = domain[1];
                } else {
                    continue;
                }
            }
            input[0] = (float) (domain[0] + (d1d0 * inputValue));
            float[] values = null;
            try {
                values = function.eval(input);
            } catch (IOException exception) {
                LOG.error("error while processing a function", exception);
            }
            int index = (j * w + i) * 3;
            // convert color values from shading colorspace to RGB 
            if (shadingColorSpace != null) {
                values = shadingColorSpace.toRGB(values);
            }
            data[index] = (int) (values[0] * 255);
            data[index + 1] = (int) (values[1] * 255);
            data[index + 2] = (int) (values[2] * 255);
        }
    }
    raster.setPixels(0, 0, w, h, data);
    return raster;
}