Search in sources :

Example 36 with SampleModel

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

the class WBMPImageWriter method write.

public void write(IIOMetadata streamMetadata, IIOImage image, ImageWriteParam param) throws IOException {
    if (stream == null) {
        throw new IllegalStateException(I18N.getString("WBMPImageWriter3"));
    }
    if (image == null) {
        throw new IllegalArgumentException(I18N.getString("WBMPImageWriter4"));
    }
    clearAbortRequest();
    processImageStarted(0);
    if (param == null)
        param = getDefaultWriteParam();
    RenderedImage input = null;
    Raster inputRaster = null;
    boolean writeRaster = image.hasRaster();
    Rectangle sourceRegion = param.getSourceRegion();
    SampleModel sampleModel = null;
    if (writeRaster) {
        inputRaster = image.getRaster();
        sampleModel = inputRaster.getSampleModel();
    } else {
        input = image.getRenderedImage();
        sampleModel = input.getSampleModel();
        inputRaster = input.getData();
    }
    checkSampleModel(sampleModel);
    if (sourceRegion == null)
        sourceRegion = inputRaster.getBounds();
    else
        sourceRegion = sourceRegion.intersection(inputRaster.getBounds());
    if (sourceRegion.isEmpty())
        throw new RuntimeException(I18N.getString("WBMPImageWriter1"));
    int scaleX = param.getSourceXSubsampling();
    int scaleY = param.getSourceYSubsampling();
    int xOffset = param.getSubsamplingXOffset();
    int yOffset = param.getSubsamplingYOffset();
    sourceRegion.translate(xOffset, yOffset);
    sourceRegion.width -= xOffset;
    sourceRegion.height -= yOffset;
    int minX = sourceRegion.x / scaleX;
    int minY = sourceRegion.y / scaleY;
    int w = (sourceRegion.width + scaleX - 1) / scaleX;
    int h = (sourceRegion.height + scaleY - 1) / scaleY;
    Rectangle destinationRegion = new Rectangle(minX, minY, w, h);
    sampleModel = sampleModel.createCompatibleSampleModel(w, h);
    SampleModel destSM = sampleModel;
    // If the data are not formatted nominally then reformat.
    if (sampleModel.getDataType() != DataBuffer.TYPE_BYTE || !(sampleModel instanceof MultiPixelPackedSampleModel) || ((MultiPixelPackedSampleModel) sampleModel).getDataBitOffset() != 0) {
        destSM = new MultiPixelPackedSampleModel(DataBuffer.TYPE_BYTE, w, h, 1, w + 7 >> 3, 0);
    }
    if (!destinationRegion.equals(sourceRegion)) {
        if (scaleX == 1 && scaleY == 1)
            inputRaster = inputRaster.createChild(inputRaster.getMinX(), inputRaster.getMinY(), w, h, minX, minY, null);
        else {
            WritableRaster ras = Raster.createWritableRaster(destSM, new Point(minX, minY));
            byte[] data = ((DataBufferByte) ras.getDataBuffer()).getData();
            for (int j = minY, y = sourceRegion.y, k = 0; j < minY + h; j++, y += scaleY) {
                for (int i = 0, x = sourceRegion.x; i < w; i++, x += scaleX) {
                    int v = inputRaster.getSample(x, y, 0);
                    data[k + (i >> 3)] |= v << (7 - (i & 7));
                }
                k += w + 7 >> 3;
            }
            inputRaster = ras;
        }
    }
    // If the data are not formatted nominally then reformat.
    if (!destSM.equals(inputRaster.getSampleModel())) {
        WritableRaster raster = Raster.createWritableRaster(destSM, new Point(inputRaster.getMinX(), inputRaster.getMinY()));
        raster.setRect(inputRaster);
        inputRaster = raster;
    }
    // Check whether the image is white-is-zero.
    boolean isWhiteZero = false;
    if (!writeRaster && input.getColorModel() instanceof IndexColorModel) {
        IndexColorModel icm = (IndexColorModel) input.getColorModel();
        isWhiteZero = icm.getRed(0) > icm.getRed(1);
    }
    // Get the line stride, bytes per row, and data array.
    int lineStride = ((MultiPixelPackedSampleModel) destSM).getScanlineStride();
    int bytesPerRow = (w + 7) / 8;
    byte[] bdata = ((DataBufferByte) inputRaster.getDataBuffer()).getData();
    // Write WBMP header.
    // TypeField
    stream.write(0);
    // FixHeaderField
    stream.write(0);
    // width
    stream.write(intToMultiByte(w));
    // height
    stream.write(intToMultiByte(h));
    // Write the data.
    if (!isWhiteZero && lineStride == bytesPerRow) {
        // Write the entire image.
        stream.write(bdata, 0, h * bytesPerRow);
        processImageProgress(100.0F);
    } else {
        // Write the image row-by-row.
        int offset = 0;
        if (!isWhiteZero) {
            // Black-is-zero
            for (int row = 0; row < h; row++) {
                if (abortRequested())
                    break;
                stream.write(bdata, offset, bytesPerRow);
                offset += lineStride;
                processImageProgress(100.0F * row / h);
            }
        } else {
            // White-is-zero: need to invert data.
            byte[] inverted = new byte[bytesPerRow];
            for (int row = 0; row < h; row++) {
                if (abortRequested())
                    break;
                for (int col = 0; col < bytesPerRow; col++) {
                    inverted[col] = (byte) (~(bdata[col + offset]));
                }
                stream.write(inverted, 0, bytesPerRow);
                offset += lineStride;
                processImageProgress(100.0F * row / h);
            }
        }
    }
    if (abortRequested())
        processWriteAborted();
    else {
        processImageComplete();
        stream.flushBefore(stream.getStreamPosition());
    }
}
Also used : Raster(java.awt.image.Raster) WritableRaster(java.awt.image.WritableRaster) Rectangle(java.awt.Rectangle) MultiPixelPackedSampleModel(java.awt.image.MultiPixelPackedSampleModel) Point(java.awt.Point) DataBufferByte(java.awt.image.DataBufferByte) Point(java.awt.Point) SampleModel(java.awt.image.SampleModel) MultiPixelPackedSampleModel(java.awt.image.MultiPixelPackedSampleModel) WritableRaster(java.awt.image.WritableRaster) RenderedImage(java.awt.image.RenderedImage) IndexColorModel(java.awt.image.IndexColorModel)

Example 37 with SampleModel

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

the class ImageTypeSpecifierTest method test4430993.

private static void test4430993() {
    ImageTypeSpecifier itspecifier;
    int bits = 32;
    int rmask = 0x00ff0000;
    int gmask = 0x0000ff00;
    int bmask = 0x000000ff;
    ColorModel dcm = new java.awt.image.DirectColorModel(bits, rmask, gmask, bmask);
    int[] bandOffsets = new int[2];
    bandOffsets[1] = 1;
    SampleModel sm = new java.awt.image.ComponentSampleModel(DataBuffer.TYPE_SHORT, 1, 1, 2, 2, bandOffsets);
    try {
        itspecifier = new ImageTypeSpecifier(dcm, sm);
        fail("Failed to get IAE!");
    } catch (IllegalArgumentException e) {
    }
}
Also used : SampleModel(java.awt.image.SampleModel) ColorModel(java.awt.image.ColorModel) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier)

Example 38 with SampleModel

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

the class BytePackedRaster 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.  The bandList is ignored.
     * 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 BytePackedRaster(sm, dataBuffer, new Rectangle(x0, y0, width, height), new Point(sampleModelTranslateX + deltaX, sampleModelTranslateY + deltaY), this);
}
Also used : MultiPixelPackedSampleModel(java.awt.image.MultiPixelPackedSampleModel) SampleModel(java.awt.image.SampleModel) Rectangle(java.awt.Rectangle) Point(java.awt.Point) RasterFormatException(java.awt.image.RasterFormatException) Point(java.awt.Point)

Example 39 with SampleModel

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

the class WritableRasterNative method createNativeRaster.

public static WritableRasterNative createNativeRaster(ColorModel cm, SurfaceData sd, int width, int height) {
    SampleModel smHw = null;
    int dataType = 0;
    int scanStride = width;
    switch(cm.getPixelSize()) {
        case 8:
        case 12:
            // 8-bits uses PixelInterleavedSampleModel
            if (cm.getPixelSize() == 8) {
                dataType = DataBuffer.TYPE_BYTE;
            } else {
                dataType = DataBuffer.TYPE_USHORT;
            }
            int[] bandOffsets = new int[1];
            bandOffsets[0] = 0;
            smHw = new PixelInterleavedSampleModel(dataType, width, height, 1, scanStride, bandOffsets);
            break;
        // all others use SinglePixelPackedSampleModel
        case 15:
        case 16:
            dataType = DataBuffer.TYPE_USHORT;
            int[] bitMasks = new int[3];
            DirectColorModel dcm = (DirectColorModel) cm;
            bitMasks[0] = dcm.getRedMask();
            bitMasks[1] = dcm.getGreenMask();
            bitMasks[2] = dcm.getBlueMask();
            smHw = new SinglePixelPackedSampleModel(dataType, width, height, scanStride, bitMasks);
            break;
        case 24:
        case 32:
            dataType = DataBuffer.TYPE_INT;
            bitMasks = new int[3];
            dcm = (DirectColorModel) cm;
            bitMasks[0] = dcm.getRedMask();
            bitMasks[1] = dcm.getGreenMask();
            bitMasks[2] = dcm.getBlueMask();
            smHw = new SinglePixelPackedSampleModel(dataType, width, height, scanStride, bitMasks);
            break;
        default:
            throw new InternalError("Unsupported depth " + cm.getPixelSize());
    }
    DataBuffer dbn = new DataBufferNative(sd, dataType, width, height);
    return new WritableRasterNative(smHw, dbn);
}
Also used : PixelInterleavedSampleModel(java.awt.image.PixelInterleavedSampleModel) SinglePixelPackedSampleModel(java.awt.image.SinglePixelPackedSampleModel) PixelInterleavedSampleModel(java.awt.image.PixelInterleavedSampleModel) SampleModel(java.awt.image.SampleModel) DirectColorModel(java.awt.image.DirectColorModel) SinglePixelPackedSampleModel(java.awt.image.SinglePixelPackedSampleModel) Point(java.awt.Point) DataBuffer(java.awt.image.DataBuffer)

Example 40 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) {
    LCMSImageLayout srcIL, dstIL;
    dstIL = LCMSImageLayout.createImageLayout(dst);
    if (dstIL != null) {
        srcIL = LCMSImageLayout.createImageLayout(src);
        if (srcIL != null) {
            doTransform(srcIL, dstIL);
            return;
        }
    }
    // 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();
    int w = src.getWidth();
    int h = src.getHeight();
    int srcNumBands = src.getNumBands();
    int dstNumBands = dst.getNumBands();
    int precision = 8;
    float maxNum = 255.0f;
    for (int i = 0; i < srcNumBands; i++) {
        if (srcSM.getSampleSize(i) > 8) {
            precision = 16;
            maxNum = 65535.0f;
        }
    }
    for (int i = 0; i < dstNumBands; i++) {
        if (dstSM.getSampleSize(i) > 8) {
            precision = 16;
            maxNum = 65535.0f;
        }
    }
    float[] srcScaleFactor = new float[srcNumBands];
    float[] dstScaleFactor = new float[dstNumBands];
    for (int i = 0; i < srcNumBands; i++) {
        if (srcTransferType == DataBuffer.TYPE_SHORT) {
            srcScaleFactor[i] = maxNum / 32767.0f;
        } else {
            srcScaleFactor[i] = maxNum / ((float) ((1 << srcSM.getSampleSize(i)) - 1));
        }
    }
    for (int i = 0; i < dstNumBands; i++) {
        if (dstTransferType == DataBuffer.TYPE_SHORT) {
            dstScaleFactor[i] = 32767.0f / maxNum;
        } else {
            dstScaleFactor[i] = ((float) ((1 << dstSM.getSampleSize(i)) - 1)) / maxNum;
        }
    }
    int ys = src.getMinY();
    int yd = dst.getMinY();
    int xs, xd;
    int sample;
    if (precision == 8) {
        byte[] srcLine = new byte[w * srcNumBands];
        byte[] dstLine = new byte[w * dstNumBands];
        int idx;
        // TODO check for src npixels = dst npixels
        try {
            srcIL = new LCMSImageLayout(srcLine, srcLine.length / getNumInComponents(), LCMSImageLayout.CHANNELS_SH(getNumInComponents()) | LCMSImageLayout.BYTES_SH(1), getNumInComponents());
            dstIL = new LCMSImageLayout(dstLine, dstLine.length / getNumOutComponents(), LCMSImageLayout.CHANNELS_SH(getNumOutComponents()) | LCMSImageLayout.BYTES_SH(1), getNumOutComponents());
        } 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.getSample(xs, ys, i);
                    srcLine[idx++] = (byte) ((sample * 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 = (int) (((dstLine[idx++] & 0xff) * dstScaleFactor[i]) + 0.5f);
                    dst.setSample(xd, yd, i, sample);
                }
            }
        }
    } else {
        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.getSample(xs, ys, i);
                    srcLine[idx++] = (short) ((sample * 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 = (int) (((dstLine[idx++] & 0xffff) * dstScaleFactor[i]) + 0.5f);
                    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)

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