Search in sources :

Example 6 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 7 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 8 with SampleModel

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

the class BufImgSurfaceData method createData.

public static SurfaceData createData(BufferedImage bufImg, double scaleX, double scaleY) {
    if (bufImg == null) {
        throw new NullPointerException("BufferedImage cannot be null");
    }
    SurfaceData sData;
    ColorModel cm = bufImg.getColorModel();
    int type = bufImg.getType();
    // REMIND: Check the image type and pick an appropriate subclass
    switch(type) {
        case BufferedImage.TYPE_INT_BGR:
            sData = createDataIC(bufImg, SurfaceType.IntBgr, scaleX, scaleY);
            break;
        case BufferedImage.TYPE_INT_RGB:
            sData = createDataIC(bufImg, SurfaceType.IntRgb, scaleX, scaleY);
            break;
        case BufferedImage.TYPE_INT_ARGB:
            sData = createDataIC(bufImg, SurfaceType.IntArgb, scaleX, scaleY);
            break;
        case BufferedImage.TYPE_INT_ARGB_PRE:
            sData = createDataIC(bufImg, SurfaceType.IntArgbPre, scaleX, scaleY);
            break;
        case BufferedImage.TYPE_3BYTE_BGR:
            sData = createDataBC(bufImg, SurfaceType.ThreeByteBgr, 2, scaleX, scaleY);
            break;
        case BufferedImage.TYPE_4BYTE_ABGR:
            sData = createDataBC(bufImg, SurfaceType.FourByteAbgr, 3, scaleX, scaleY);
            break;
        case BufferedImage.TYPE_4BYTE_ABGR_PRE:
            sData = createDataBC(bufImg, SurfaceType.FourByteAbgrPre, 3, scaleX, scaleY);
            break;
        case BufferedImage.TYPE_USHORT_565_RGB:
            sData = createDataSC(bufImg, SurfaceType.Ushort565Rgb, null, scaleX, scaleY);
            break;
        case BufferedImage.TYPE_USHORT_555_RGB:
            sData = createDataSC(bufImg, SurfaceType.Ushort555Rgb, null, scaleX, scaleY);
            break;
        case BufferedImage.TYPE_BYTE_INDEXED:
            {
                SurfaceType sType;
                switch(cm.getTransparency()) {
                    case OPAQUE:
                        if (isOpaqueGray((IndexColorModel) cm)) {
                            sType = SurfaceType.Index8Gray;
                        } else {
                            sType = SurfaceType.ByteIndexedOpaque;
                        }
                        break;
                    case BITMASK:
                        sType = SurfaceType.ByteIndexedBm;
                        break;
                    case TRANSLUCENT:
                        sType = SurfaceType.ByteIndexed;
                        break;
                    default:
                        throw new InternalError("Unrecognized transparency");
                }
                sData = createDataBC(bufImg, sType, 0, scaleX, scaleY);
            }
            break;
        case BufferedImage.TYPE_BYTE_GRAY:
            sData = createDataBC(bufImg, SurfaceType.ByteGray, 0, scaleX, scaleY);
            break;
        case BufferedImage.TYPE_USHORT_GRAY:
            sData = createDataSC(bufImg, SurfaceType.UshortGray, null, scaleX, scaleY);
            break;
        case BufferedImage.TYPE_BYTE_BINARY:
            {
                SurfaceType sType;
                SampleModel sm = bufImg.getRaster().getSampleModel();
                switch(sm.getSampleSize(0)) {
                    case 1:
                        sType = SurfaceType.ByteBinary1Bit;
                        break;
                    case 2:
                        sType = SurfaceType.ByteBinary2Bit;
                        break;
                    case 4:
                        sType = SurfaceType.ByteBinary4Bit;
                        break;
                    default:
                        throw new InternalError("Unrecognized pixel size");
                }
                sData = createDataBP(bufImg, sType, scaleX, scaleY);
            }
            break;
        case BufferedImage.TYPE_CUSTOM:
        default:
            {
                Raster raster = bufImg.getRaster();
                int numBands = raster.getNumBands();
                if (raster instanceof IntegerComponentRaster && raster.getNumDataElements() == 1 && ((IntegerComponentRaster) raster).getPixelStride() == 1) {
                    SurfaceType sType = SurfaceType.AnyInt;
                    if (cm instanceof DirectColorModel) {
                        DirectColorModel dcm = (DirectColorModel) cm;
                        int aMask = dcm.getAlphaMask();
                        int rMask = dcm.getRedMask();
                        int gMask = dcm.getGreenMask();
                        int bMask = dcm.getBlueMask();
                        if (numBands == 3 && aMask == 0 && rMask == DCM_RGBX_RED_MASK && gMask == DCM_RGBX_GREEN_MASK && bMask == DCM_RGBX_BLUE_MASK) {
                            sType = SurfaceType.IntRgbx;
                        } else if (numBands == 4 && aMask == DCM_ARGBBM_ALPHA_MASK && rMask == DCM_ARGBBM_RED_MASK && gMask == DCM_ARGBBM_GREEN_MASK && bMask == DCM_ARGBBM_BLUE_MASK) {
                            sType = SurfaceType.IntArgbBm;
                        } else {
                            sType = SurfaceType.AnyDcm;
                        }
                    }
                    sData = createDataIC(bufImg, sType, scaleX, scaleY);
                    break;
                } else if (raster instanceof ShortComponentRaster && raster.getNumDataElements() == 1 && ((ShortComponentRaster) raster).getPixelStride() == 1) {
                    SurfaceType sType = SurfaceType.AnyShort;
                    IndexColorModel icm = null;
                    if (cm instanceof DirectColorModel) {
                        DirectColorModel dcm = (DirectColorModel) cm;
                        int aMask = dcm.getAlphaMask();
                        int rMask = dcm.getRedMask();
                        int gMask = dcm.getGreenMask();
                        int bMask = dcm.getBlueMask();
                        if (numBands == 3 && aMask == 0 && rMask == DCM_555X_RED_MASK && gMask == DCM_555X_GREEN_MASK && bMask == DCM_555X_BLUE_MASK) {
                            sType = SurfaceType.Ushort555Rgbx;
                        } else if (numBands == 4 && aMask == DCM_4444_ALPHA_MASK && rMask == DCM_4444_RED_MASK && gMask == DCM_4444_GREEN_MASK && bMask == DCM_4444_BLUE_MASK) {
                            sType = SurfaceType.Ushort4444Argb;
                        }
                    } else if (cm instanceof IndexColorModel) {
                        icm = (IndexColorModel) cm;
                        if (icm.getPixelSize() == 12) {
                            if (isOpaqueGray(icm)) {
                                sType = SurfaceType.Index12Gray;
                            } else {
                                sType = SurfaceType.UshortIndexed;
                            }
                        } else {
                            icm = null;
                        }
                    }
                    sData = createDataSC(bufImg, sType, icm, scaleX, scaleY);
                    break;
                }
                sData = new BufImgSurfaceData(raster.getDataBuffer(), bufImg, SurfaceType.Custom, scaleX, scaleY);
            }
            break;
    }
    ((BufImgSurfaceData) sData).initSolidLoops();
    return sData;
}
Also used : Raster(java.awt.image.Raster) SurfaceType(sun.java2d.loops.SurfaceType) SampleModel(java.awt.image.SampleModel) SurfaceData(sun.java2d.SurfaceData) DirectColorModel(java.awt.image.DirectColorModel) ColorModel(java.awt.image.ColorModel) IndexColorModel(java.awt.image.IndexColorModel) DirectColorModel(java.awt.image.DirectColorModel) IndexColorModel(java.awt.image.IndexColorModel)

Example 9 with SampleModel

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

the class ByteInterleavedRaster 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
     * DataBuffer as the parent Raster, but using different offsets.
     * @param x               X offset.
     * @param y               Y offset.
     * @param width           Width (in pixels) of the subraster.
     * @param height          Height (in pixels) 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 the raster");
    }
    if (y < this.minY) {
        throw new RasterFormatException("y lies outside the raster");
    }
    if ((x + width < x) || (x + width > this.minX + this.width)) {
        throw new RasterFormatException("(x + width) is outside of Raster");
    }
    if ((y + height < y) || (y + height > this.minY + this.height)) {
        throw new RasterFormatException("(y + height) is outside of Raster");
    }
    SampleModel sm;
    if (bandList != null)
        sm = sampleModel.createSubsetSampleModel(bandList);
    else
        sm = sampleModel;
    int deltaX = x0 - x;
    int deltaY = y0 - y;
    return new ByteInterleavedRaster(sm, dataBuffer, new Rectangle(x0, y0, width, height), new Point(sampleModelTranslateX + deltaX, sampleModelTranslateY + deltaY), this);
}
Also used : ComponentSampleModel(java.awt.image.ComponentSampleModel) SinglePixelPackedSampleModel(java.awt.image.SinglePixelPackedSampleModel) PixelInterleavedSampleModel(java.awt.image.PixelInterleavedSampleModel) SampleModel(java.awt.image.SampleModel) Rectangle(java.awt.Rectangle) Point(java.awt.Point) RasterFormatException(java.awt.image.RasterFormatException) Point(java.awt.Point)

Example 10 with SampleModel

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

the class ByteComponentRaster 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
     * DataBuffer as the parent Raster, but using different offsets.
     * @param x               X offset.
     * @param y               Y offset.
     * @param width           Width (in pixels) of the subraster.
     * @param height          Height (in pixels) 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 the raster");
    }
    if (y < this.minY) {
        throw new RasterFormatException("y lies outside the raster");
    }
    if ((x + width < x) || (x + width > this.minX + this.width)) {
        throw new RasterFormatException("(x + width) is outside of Raster");
    }
    if ((y + height < y) || (y + height > this.minY + this.height)) {
        throw new RasterFormatException("(y + height) is outside of Raster");
    }
    SampleModel sm;
    if (bandList != null)
        sm = sampleModel.createSubsetSampleModel(bandList);
    else
        sm = sampleModel;
    int deltaX = x0 - x;
    int deltaY = y0 - y;
    return new ByteComponentRaster(sm, dataBuffer, new Rectangle(x0, y0, width, height), new Point(sampleModelTranslateX + deltaX, sampleModelTranslateY + deltaY), this);
}
Also used : ComponentSampleModel(java.awt.image.ComponentSampleModel) SinglePixelPackedSampleModel(java.awt.image.SinglePixelPackedSampleModel) 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)47 SinglePixelPackedSampleModel (java.awt.image.SinglePixelPackedSampleModel)22 ComponentSampleModel (java.awt.image.ComponentSampleModel)21 Point (java.awt.Point)20 ColorModel (java.awt.image.ColorModel)16 Rectangle (java.awt.Rectangle)15 DataBuffer (java.awt.image.DataBuffer)14 MultiPixelPackedSampleModel (java.awt.image.MultiPixelPackedSampleModel)14 IndexColorModel (java.awt.image.IndexColorModel)12 RasterFormatException (java.awt.image.RasterFormatException)9 WritableRaster (java.awt.image.WritableRaster)9 DataBufferByte (java.awt.image.DataBufferByte)8 PixelInterleavedSampleModel (java.awt.image.PixelInterleavedSampleModel)8 BufferedImage (java.awt.image.BufferedImage)7 DataBufferInt (java.awt.image.DataBufferInt)7 BandedSampleModel (java.awt.image.BandedSampleModel)6 DirectColorModel (java.awt.image.DirectColorModel)6 Raster (java.awt.image.Raster)6 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)6 DataBufferShort (java.awt.image.DataBufferShort)5