Search in sources :

Example 21 with SampleModel

use of java.awt.image.SampleModel in project jdk8u_jdk by JetBrains.

the class RenderableImageProducer method run.

/**
     * The runnable method for this class. This will produce an image using
     * the current RenderableImage and RenderContext and send it to all the
     * ImageConsumer currently registered with this class.
     */
public void run() {
    // First get the rendered image
    RenderedImage rdrdImage;
    if (rc != null) {
        rdrdImage = rdblImage.createRendering(rc);
    } else {
        rdrdImage = rdblImage.createDefaultRendering();
    }
    // And its ColorModel
    ColorModel colorModel = rdrdImage.getColorModel();
    Raster raster = rdrdImage.getData();
    SampleModel sampleModel = raster.getSampleModel();
    DataBuffer dataBuffer = raster.getDataBuffer();
    if (colorModel == null) {
        colorModel = ColorModel.getRGBdefault();
    }
    int minX = raster.getMinX();
    int minY = raster.getMinY();
    int width = raster.getWidth();
    int height = raster.getHeight();
    Enumeration icList;
    ImageConsumer ic;
    // Set up the ImageConsumers
    icList = ics.elements();
    while (icList.hasMoreElements()) {
        ic = (ImageConsumer) icList.nextElement();
        ic.setDimensions(width, height);
        ic.setHints(ImageConsumer.TOPDOWNLEFTRIGHT | ImageConsumer.COMPLETESCANLINES | ImageConsumer.SINGLEPASS | ImageConsumer.SINGLEFRAME);
    }
    // Get RGB pixels from the raster scanline by scanline and
    // send to consumers.
    int[] pix = new int[width];
    int i, j;
    int numBands = sampleModel.getNumBands();
    int[] tmpPixel = new int[numBands];
    for (j = 0; j < height; j++) {
        for (i = 0; i < width; i++) {
            sampleModel.getPixel(i, j, tmpPixel, dataBuffer);
            pix[i] = colorModel.getDataElement(tmpPixel, 0);
        }
        // Now send the scanline to the Consumers
        icList = ics.elements();
        while (icList.hasMoreElements()) {
            ic = (ImageConsumer) icList.nextElement();
            ic.setPixels(0, j, width, 1, colorModel, pix, 0, width);
        }
    }
    // Now tell the consumers we're done.
    icList = ics.elements();
    while (icList.hasMoreElements()) {
        ic = (ImageConsumer) icList.nextElement();
        ic.imageComplete(ImageConsumer.STATICIMAGEDONE);
    }
}
Also used : SampleModel(java.awt.image.SampleModel) Enumeration(java.util.Enumeration) ColorModel(java.awt.image.ColorModel) DirectColorModel(java.awt.image.DirectColorModel) ImageConsumer(java.awt.image.ImageConsumer) Raster(java.awt.image.Raster) RenderedImage(java.awt.image.RenderedImage) DataBuffer(java.awt.image.DataBuffer)

Example 22 with SampleModel

use of java.awt.image.SampleModel in project jdk8u_jdk by JetBrains.

the class LCMSTransform method colorConvert.

public void colorConvert(Raster src, WritableRaster dst, float[] srcMinVal, float[] srcMaxVal, float[] dstMinVal, float[] dstMaxVal) {
    LCMSImageLayout srcIL, dstIL;
    // Can't pass src and dst directly to CMM, so process per scanline
    SampleModel srcSM = src.getSampleModel();
    SampleModel dstSM = dst.getSampleModel();
    int srcTransferType = src.getTransferType();
    int dstTransferType = dst.getTransferType();
    boolean srcIsFloat, dstIsFloat;
    if ((srcTransferType == DataBuffer.TYPE_FLOAT) || (srcTransferType == DataBuffer.TYPE_DOUBLE)) {
        srcIsFloat = true;
    } else {
        srcIsFloat = false;
    }
    if ((dstTransferType == DataBuffer.TYPE_FLOAT) || (dstTransferType == DataBuffer.TYPE_DOUBLE)) {
        dstIsFloat = true;
    } else {
        dstIsFloat = false;
    }
    int w = src.getWidth();
    int h = src.getHeight();
    int srcNumBands = src.getNumBands();
    int dstNumBands = dst.getNumBands();
    float[] srcScaleFactor = new float[srcNumBands];
    float[] dstScaleFactor = new float[dstNumBands];
    float[] srcUseMinVal = new float[srcNumBands];
    float[] dstUseMinVal = new float[dstNumBands];
    for (int i = 0; i < srcNumBands; i++) {
        if (srcIsFloat) {
            srcScaleFactor[i] = 65535.0f / (srcMaxVal[i] - srcMinVal[i]);
            srcUseMinVal[i] = srcMinVal[i];
        } else {
            if (srcTransferType == DataBuffer.TYPE_SHORT) {
                srcScaleFactor[i] = 65535.0f / 32767.0f;
            } else {
                srcScaleFactor[i] = 65535.0f / ((float) ((1 << srcSM.getSampleSize(i)) - 1));
            }
            srcUseMinVal[i] = 0.0f;
        }
    }
    for (int i = 0; i < dstNumBands; i++) {
        if (dstIsFloat) {
            dstScaleFactor[i] = (dstMaxVal[i] - dstMinVal[i]) / 65535.0f;
            dstUseMinVal[i] = dstMinVal[i];
        } else {
            if (dstTransferType == DataBuffer.TYPE_SHORT) {
                dstScaleFactor[i] = 32767.0f / 65535.0f;
            } else {
                dstScaleFactor[i] = ((float) ((1 << dstSM.getSampleSize(i)) - 1)) / 65535.0f;
            }
            dstUseMinVal[i] = 0.0f;
        }
    }
    int ys = src.getMinY();
    int yd = dst.getMinY();
    int xs, xd;
    float sample;
    short[] srcLine = new short[w * srcNumBands];
    short[] dstLine = new short[w * dstNumBands];
    int idx;
    try {
        srcIL = new LCMSImageLayout(srcLine, srcLine.length / getNumInComponents(), LCMSImageLayout.CHANNELS_SH(getNumInComponents()) | LCMSImageLayout.BYTES_SH(2), getNumInComponents() * 2);
        dstIL = new LCMSImageLayout(dstLine, dstLine.length / getNumOutComponents(), LCMSImageLayout.CHANNELS_SH(getNumOutComponents()) | LCMSImageLayout.BYTES_SH(2), getNumOutComponents() * 2);
    } catch (ImageLayoutException e) {
        throw new CMMException("Unable to convert rasters");
    }
    // process each scanline
    for (int y = 0; y < h; y++, ys++, yd++) {
        // get src scanline
        xs = src.getMinX();
        idx = 0;
        for (int x = 0; x < w; x++, xs++) {
            for (int i = 0; i < srcNumBands; i++) {
                sample = src.getSampleFloat(xs, ys, i);
                srcLine[idx++] = (short) ((sample - srcUseMinVal[i]) * srcScaleFactor[i] + 0.5f);
            }
        }
        // color convert srcLine to dstLine
        doTransform(srcIL, dstIL);
        // store dst scanline
        xd = dst.getMinX();
        idx = 0;
        for (int x = 0; x < w; x++, xd++) {
            for (int i = 0; i < dstNumBands; i++) {
                sample = ((dstLine[idx++] & 0xffff) * dstScaleFactor[i]) + dstUseMinVal[i];
                dst.setSample(xd, yd, i, sample);
            }
        }
    }
}
Also used : ComponentSampleModel(java.awt.image.ComponentSampleModel) SampleModel(java.awt.image.SampleModel) SinglePixelPackedSampleModel(java.awt.image.SinglePixelPackedSampleModel) ImageLayoutException(sun.java2d.cmm.lcms.LCMSImageLayout.ImageLayoutException) CMMException(java.awt.color.CMMException)

Example 23 with SampleModel

use of java.awt.image.SampleModel in project jdk8u_jdk by JetBrains.

the class GetDataElementsTest method main.

public static void main(String[] args) {
    SampleModel sm = new ComponentSampleModel(dataType, width, height, 4, width * 4, new int[] { 0, 1, 2, 3 });
    DataBuffer db = sm.createDataBuffer();
    Object o = null;
    boolean testPassed = false;
    try {
        o = sm.getDataElements(Integer.MAX_VALUE, 0, 1, 1, o, db);
    } catch (ArrayIndexOutOfBoundsException e) {
        System.out.println(e.getMessage());
        testPassed = true;
    }
    if (!testPassed) {
        throw new RuntimeException("Excpected excprion was not thrown.");
    }
}
Also used : ComponentSampleModel(java.awt.image.ComponentSampleModel) SampleModel(java.awt.image.SampleModel) ComponentSampleModel(java.awt.image.ComponentSampleModel) DataBuffer(java.awt.image.DataBuffer)

Example 24 with SampleModel

use of java.awt.image.SampleModel in project jdk8u_jdk by JetBrains.

the class GetSamplesTest method doTest.

private static void doTest(Class<? extends SampleModel> c) {
    System.out.println("Test for: " + c.getName());
    SampleModel sm = createSampleModel(c);
    DataBuffer db = sm.createDataBuffer();
    int[] iArray = new int[width * height + numBands];
    float[] fArray = new float[width * height + numBands];
    double[] dArray = new double[width * height + numBands];
    boolean iOk = false;
    boolean fOk = false;
    boolean dOk = false;
    try {
        sm.getSamples(Integer.MAX_VALUE, 0, 1, 1, 0, iArray, db);
        sm.setSamples(Integer.MAX_VALUE, 0, 1, 1, 0, iArray, db);
    } catch (ArrayIndexOutOfBoundsException e) {
        System.out.println(e.getMessage());
        iOk = true;
    }
    try {
        sm.getSamples(Integer.MAX_VALUE, 0, 1, 1, 0, fArray, db);
        sm.setSamples(Integer.MAX_VALUE, 0, 1, 1, 0, fArray, db);
    } catch (ArrayIndexOutOfBoundsException e) {
        System.out.println(e.getMessage());
        fOk = true;
    }
    try {
        sm.getSamples(0, Integer.MAX_VALUE, 1, 1, 0, dArray, db);
        sm.setSamples(0, Integer.MAX_VALUE, 1, 1, 0, dArray, db);
    } catch (ArrayIndexOutOfBoundsException e) {
        System.out.println(e.getMessage());
        dOk = true;
    }
    if (!iOk || !fOk || !dOk) {
        throw new RuntimeException("Test for " + c.getSimpleName() + " failed: iOk=" + iOk + "; fOk=" + fOk + "; dOk=" + dOk);
    }
}
Also used : BandedSampleModel(java.awt.image.BandedSampleModel) ComponentSampleModel(java.awt.image.ComponentSampleModel) MultiPixelPackedSampleModel(java.awt.image.MultiPixelPackedSampleModel) SinglePixelPackedSampleModel(java.awt.image.SinglePixelPackedSampleModel) PixelInterleavedSampleModel(java.awt.image.PixelInterleavedSampleModel) SampleModel(java.awt.image.SampleModel) DataBuffer(java.awt.image.DataBuffer)

Example 25 with SampleModel

use of java.awt.image.SampleModel in project jdk8u_jdk by JetBrains.

the class ByteBandedRaster method createWritableChild.

/**
     * Creates a Writable subraster given a region of the raster.  The x and y
     * coordinates specify the horizontal and vertical offsets
     * from the upper-left corner of this raster to the upper-left corner
     * of the subraster.  A subset of the bands of the parent Raster may
     * be specified.  If this is null, then all the bands are present in the
     * subRaster. A translation to the subRaster may also be specified.
     * Note that the subraster will reference the same
     * DataBuffers as the parent raster, but using different offsets.
     * @param x               X offset.
     * @param y               Y offset.
     * @param width           Width of the subraster.
     * @param height          Height of the subraster.
     * @param x0              Translated X origin of the subraster.
     * @param y0              Translated Y origin of the subraster.
     * @param bandList        Array of band indices.
     * @exception RasterFormatException
     *            if the specified bounding box is outside of the parent raster.
     */
public WritableRaster createWritableChild(int x, int y, int width, int height, int x0, int y0, int[] bandList) {
    if (x < this.minX) {
        throw new RasterFormatException("x lies outside raster");
    }
    if (y < this.minY) {
        throw new RasterFormatException("y lies outside raster");
    }
    if ((x + width < x) || (x + width > this.width + this.minX)) {
        throw new RasterFormatException("(x + width) is outside raster");
    }
    if ((y + height < y) || (y + height > this.height + this.minY)) {
        throw new RasterFormatException("(y + height) is outside raster");
    }
    SampleModel sm;
    if (bandList != null)
        sm = sampleModel.createSubsetSampleModel(bandList);
    else
        sm = sampleModel;
    int deltaX = x0 - x;
    int deltaY = y0 - y;
    return new ByteBandedRaster(sm, dataBuffer, new Rectangle(x0, y0, width, height), new Point(sampleModelTranslateX + deltaX, sampleModelTranslateY + deltaY), this);
}
Also used : BandedSampleModel(java.awt.image.BandedSampleModel) SampleModel(java.awt.image.SampleModel) Rectangle(java.awt.Rectangle) Point(java.awt.Point) RasterFormatException(java.awt.image.RasterFormatException) Point(java.awt.Point)

Aggregations

SampleModel (java.awt.image.SampleModel)48 SinglePixelPackedSampleModel (java.awt.image.SinglePixelPackedSampleModel)23 Point (java.awt.Point)21 ComponentSampleModel (java.awt.image.ComponentSampleModel)21 ColorModel (java.awt.image.ColorModel)17 Rectangle (java.awt.Rectangle)15 DataBuffer (java.awt.image.DataBuffer)14 MultiPixelPackedSampleModel (java.awt.image.MultiPixelPackedSampleModel)14 IndexColorModel (java.awt.image.IndexColorModel)13 WritableRaster (java.awt.image.WritableRaster)10 RasterFormatException (java.awt.image.RasterFormatException)9 BufferedImage (java.awt.image.BufferedImage)8 DataBufferByte (java.awt.image.DataBufferByte)8 DataBufferInt (java.awt.image.DataBufferInt)8 PixelInterleavedSampleModel (java.awt.image.PixelInterleavedSampleModel)8 DirectColorModel (java.awt.image.DirectColorModel)7 BandedSampleModel (java.awt.image.BandedSampleModel)6 Raster (java.awt.image.Raster)6 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)6 DataBufferShort (java.awt.image.DataBufferShort)5