Search in sources :

Example 31 with SampleModel

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

the class ColCvtAlpha method main.

public static void main(String[] args) {
    BufferedImage src = new BufferedImage(1, 10, BufferedImage.TYPE_INT_ARGB);
    // Set src pixel values
    Color pelColor = new Color(100, 100, 100, 128);
    for (int i = 0; i < 10; i++) {
        src.setRGB(0, i, pelColor.getRGB());
    }
    ColorModel cm = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_GRAY), new int[] { 8, 8 }, true, src.getColorModel().isAlphaPremultiplied(), Transparency.TRANSLUCENT, DataBuffer.TYPE_BYTE);
    SampleModel sm = new PixelInterleavedSampleModel(DataBuffer.TYPE_BYTE, 100, 100, 2, 200, new int[] { 0, 1 });
    WritableRaster wr = Raster.createWritableRaster(sm, new Point(0, 0));
    BufferedImage dst = new BufferedImage(cm, wr, cm.isAlphaPremultiplied(), null);
    dst = dst.getSubimage(0, 0, 1, 10);
    ColorConvertOp op = new ColorConvertOp(null);
    op.filter(src, dst);
    for (int i = 0; i < 10; i++) {
        if (((dst.getRGB(0, i) >> 24) & 0xff) != 128) {
            throw new RuntimeException("Incorrect destination alpha value.");
        }
    }
}
Also used : PixelInterleavedSampleModel(java.awt.image.PixelInterleavedSampleModel) PixelInterleavedSampleModel(java.awt.image.PixelInterleavedSampleModel) SampleModel(java.awt.image.SampleModel) ColorConvertOp(java.awt.image.ColorConvertOp) ComponentColorModel(java.awt.image.ComponentColorModel) ColorModel(java.awt.image.ColorModel) ComponentColorModel(java.awt.image.ComponentColorModel) WritableRaster(java.awt.image.WritableRaster) Color(java.awt.Color) Point(java.awt.Point) BufferedImage(java.awt.image.BufferedImage) Point(java.awt.Point)

Example 32 with SampleModel

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

the class GIFImageWriteParam method writeRasterData.

private void writeRasterData(RenderedImage image, Rectangle sourceBounds, Dimension destSize, ImageWriteParam param, boolean interlaceFlag) throws IOException {
    int sourceXOffset = sourceBounds.x;
    int sourceYOffset = sourceBounds.y;
    int sourceWidth = sourceBounds.width;
    int sourceHeight = sourceBounds.height;
    int destWidth = destSize.width;
    int destHeight = destSize.height;
    int periodX;
    int periodY;
    if (param == null) {
        periodX = 1;
        periodY = 1;
    } else {
        periodX = param.getSourceXSubsampling();
        periodY = param.getSourceYSubsampling();
    }
    SampleModel sampleModel = image.getSampleModel();
    int bitsPerPixel = sampleModel.getSampleSize()[0];
    int initCodeSize = bitsPerPixel;
    if (initCodeSize == 1) {
        initCodeSize++;
    }
    stream.write(initCodeSize);
    LZWCompressor compressor = new LZWCompressor(stream, initCodeSize, false);
    /* At this moment we know that input image is indexed image.
         * We can directly copy data iff:
         *   - no subsampling required (periodX = 1, periodY = 0)
         *   - we can access data directly (image is non-tiled,
         *     i.e. image data are in single block)
         *   - we can calculate offset in data buffer (next 3 lines)
         */
    boolean isOptimizedCase = periodX == 1 && periodY == 1 && image.getNumXTiles() == 1 && image.getNumYTiles() == 1 && sampleModel instanceof ComponentSampleModel && image.getTile(0, 0) instanceof ByteComponentRaster && image.getTile(0, 0).getDataBuffer() instanceof DataBufferByte;
    int numRowsWritten = 0;
    int progressReportRowPeriod = Math.max(destHeight / 20, 1);
    processImageStarted(imageIndex);
    if (interlaceFlag) {
        if (DEBUG)
            System.out.println("Writing interlaced");
        if (isOptimizedCase) {
            ByteComponentRaster tile = (ByteComponentRaster) image.getTile(0, 0);
            byte[] data = ((DataBufferByte) tile.getDataBuffer()).getData();
            ComponentSampleModel csm = (ComponentSampleModel) tile.getSampleModel();
            int offset = csm.getOffset(sourceXOffset, sourceYOffset, 0);
            // take into account the raster data offset
            offset += tile.getDataOffset(0);
            int lineStride = csm.getScanlineStride();
            writeRowsOpt(data, offset, lineStride, compressor, 0, 8, destWidth, destHeight, numRowsWritten, progressReportRowPeriod);
            if (abortRequested()) {
                return;
            }
            numRowsWritten += destHeight / 8;
            writeRowsOpt(data, offset, lineStride, compressor, 4, 8, destWidth, destHeight, numRowsWritten, progressReportRowPeriod);
            if (abortRequested()) {
                return;
            }
            numRowsWritten += (destHeight - 4) / 8;
            writeRowsOpt(data, offset, lineStride, compressor, 2, 4, destWidth, destHeight, numRowsWritten, progressReportRowPeriod);
            if (abortRequested()) {
                return;
            }
            numRowsWritten += (destHeight - 2) / 4;
            writeRowsOpt(data, offset, lineStride, compressor, 1, 2, destWidth, destHeight, numRowsWritten, progressReportRowPeriod);
        } else {
            writeRows(image, compressor, sourceXOffset, periodX, sourceYOffset, 8 * periodY, sourceWidth, 0, 8, destWidth, destHeight, numRowsWritten, progressReportRowPeriod);
            if (abortRequested()) {
                return;
            }
            numRowsWritten += destHeight / 8;
            writeRows(image, compressor, sourceXOffset, periodX, sourceYOffset + 4 * periodY, 8 * periodY, sourceWidth, 4, 8, destWidth, destHeight, numRowsWritten, progressReportRowPeriod);
            if (abortRequested()) {
                return;
            }
            numRowsWritten += (destHeight - 4) / 8;
            writeRows(image, compressor, sourceXOffset, periodX, sourceYOffset + 2 * periodY, 4 * periodY, sourceWidth, 2, 4, destWidth, destHeight, numRowsWritten, progressReportRowPeriod);
            if (abortRequested()) {
                return;
            }
            numRowsWritten += (destHeight - 2) / 4;
            writeRows(image, compressor, sourceXOffset, periodX, sourceYOffset + periodY, 2 * periodY, sourceWidth, 1, 2, destWidth, destHeight, numRowsWritten, progressReportRowPeriod);
        }
    } else {
        if (DEBUG)
            System.out.println("Writing non-interlaced");
        if (isOptimizedCase) {
            Raster tile = image.getTile(0, 0);
            byte[] data = ((DataBufferByte) tile.getDataBuffer()).getData();
            ComponentSampleModel csm = (ComponentSampleModel) tile.getSampleModel();
            int offset = csm.getOffset(sourceXOffset, sourceYOffset, 0);
            int lineStride = csm.getScanlineStride();
            writeRowsOpt(data, offset, lineStride, compressor, 0, 1, destWidth, destHeight, numRowsWritten, progressReportRowPeriod);
        } else {
            writeRows(image, compressor, sourceXOffset, periodX, sourceYOffset, periodY, sourceWidth, 0, 1, destWidth, destHeight, numRowsWritten, progressReportRowPeriod);
        }
    }
    if (abortRequested()) {
        return;
    }
    processImageProgress(100.0F);
    compressor.flush();
    stream.write(0x00);
    processImageComplete();
}
Also used : LZWCompressor(com.sun.imageio.plugins.common.LZWCompressor) ComponentSampleModel(java.awt.image.ComponentSampleModel) SampleModel(java.awt.image.SampleModel) ByteComponentRaster(sun.awt.image.ByteComponentRaster) Raster(java.awt.image.Raster) ByteComponentRaster(sun.awt.image.ByteComponentRaster) WritableRaster(java.awt.image.WritableRaster) ComponentSampleModel(java.awt.image.ComponentSampleModel) DataBufferByte(java.awt.image.DataBufferByte)

Example 33 with SampleModel

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

the class GIFImageWriteParam method write.

/**
     * Writes any extension blocks, the Image Descriptor, the image data,
     * and optionally the header (Signature and Logical Screen Descriptor)
     * and trailer (Block Terminator).
     *
     * @param writeHeader Whether to write the header.
     * @param writeTrailer Whether to write the trailer.
     * @param sm The stream metadata or <code>null</code> if
     * <code>writeHeader</code> is <code>false</code>.
     * @param iioimage The image and image metadata.
     * @param p The write parameters.
     *
     * @throws IllegalArgumentException if the number of bands is not 1.
     * @throws IllegalArgumentException if the number of bits per sample is
     * greater than 8.
     * @throws IllegalArgumentException if the color component size is
     * greater than 8.
     * @throws IllegalArgumentException if <code>writeHeader</code> is
     * <code>true</code> and <code>sm</code> is <code>null</code>.
     * @throws IllegalArgumentException if <code>writeHeader</code> is
     * <code>false</code> and a sequence is not being written.
     */
private void write(boolean writeHeader, boolean writeTrailer, IIOMetadata sm, IIOImage iioimage, ImageWriteParam p) throws IOException {
    clearAbortRequest();
    RenderedImage image = iioimage.getRenderedImage();
    // Check for ability to encode image.
    if (needToCreateIndex(image)) {
        image = PaletteBuilder.createIndexedImage(image);
        iioimage.setRenderedImage(image);
    }
    ColorModel colorModel = image.getColorModel();
    SampleModel sampleModel = image.getSampleModel();
    // Determine source region and destination dimensions.
    Rectangle sourceBounds = new Rectangle(image.getMinX(), image.getMinY(), image.getWidth(), image.getHeight());
    Dimension destSize = new Dimension();
    computeRegions(sourceBounds, destSize, p);
    // Convert any provided image metadata.
    GIFWritableImageMetadata imageMetadata = null;
    if (iioimage.getMetadata() != null) {
        imageMetadata = new GIFWritableImageMetadata();
        convertMetadata(IMAGE_METADATA_NAME, iioimage.getMetadata(), imageMetadata);
        // gray-scale representations
        if (imageMetadata.localColorTable == null) {
            imageMetadata.localColorTable = createColorTable(colorModel, sampleModel);
            // transparent pixels
            if (colorModel instanceof IndexColorModel) {
                IndexColorModel icm = (IndexColorModel) colorModel;
                int index = icm.getTransparentPixel();
                imageMetadata.transparentColorFlag = (index != -1);
                if (imageMetadata.transparentColorFlag) {
                    imageMetadata.transparentColorIndex = index;
                }
            /* NB: transparentColorFlag might have not beed reset for
                       greyscale images but explicitly reseting it here
                       is potentially not right thing to do until we have way
                       to find whether current value was explicitly set by
                       the user.
                    */
            }
        }
    }
    // Global color table values.
    byte[] globalColorTable = null;
    // Global Color Table).
    if (writeHeader) {
        if (sm == null) {
            throw new IllegalArgumentException("Cannot write null header!");
        }
        GIFWritableStreamMetadata streamMetadata = (GIFWritableStreamMetadata) sm;
        // Set the version if not set.
        if (streamMetadata.version == null) {
            streamMetadata.version = "89a";
        }
        // Set the Logical Screen Desriptor if not set.
        if (streamMetadata.logicalScreenWidth == GIFMetadata.UNDEFINED_INTEGER_VALUE) {
            streamMetadata.logicalScreenWidth = destSize.width;
        }
        if (streamMetadata.logicalScreenHeight == GIFMetadata.UNDEFINED_INTEGER_VALUE) {
            streamMetadata.logicalScreenHeight = destSize.height;
        }
        if (streamMetadata.colorResolution == GIFMetadata.UNDEFINED_INTEGER_VALUE) {
            streamMetadata.colorResolution = colorModel != null ? colorModel.getComponentSize()[0] : sampleModel.getSampleSize()[0];
        }
        // provided in the stream metadata.
        if (streamMetadata.globalColorTable == null) {
            if (isWritingSequence && imageMetadata != null && imageMetadata.localColorTable != null) {
                // Writing a sequence and a local color table was
                // provided in the metadata of the first image: use it.
                streamMetadata.globalColorTable = imageMetadata.localColorTable;
            } else if (imageMetadata == null || imageMetadata.localColorTable == null) {
                // Create a color table.
                streamMetadata.globalColorTable = createColorTable(colorModel, sampleModel);
            }
        }
        // Set the Global Color Table. At this point it should be
        // A) the global color table provided in stream metadata, if any;
        // B) the local color table of the image metadata, if any, if
        //    writing a sequence;
        // C) a table created on the basis of the first image ColorModel
        //    and SampleModel if no local color table is available; or
        // D) null if none of the foregoing conditions obtain (which
        //    should only be if a sequence is not being written and
        //    a local color table is provided in image metadata).
        globalColorTable = streamMetadata.globalColorTable;
        // Write the header.
        int bitsPerPixel;
        if (globalColorTable != null) {
            bitsPerPixel = getNumBits(globalColorTable.length / 3);
        } else if (imageMetadata != null && imageMetadata.localColorTable != null) {
            bitsPerPixel = getNumBits(imageMetadata.localColorTable.length / 3);
        } else {
            bitsPerPixel = sampleModel.getSampleSize(0);
        }
        writeHeader(streamMetadata, bitsPerPixel);
    } else if (isWritingSequence) {
        globalColorTable = theStreamMetadata.globalColorTable;
    } else {
        throw new IllegalArgumentException("Must write header for single image!");
    }
    // Write extension blocks, Image Descriptor, and image data.
    writeImage(iioimage.getRenderedImage(), imageMetadata, p, globalColorTable, sourceBounds, destSize);
    // Write the trailer.
    if (writeTrailer) {
        writeTrailer();
    }
}
Also used : ComponentSampleModel(java.awt.image.ComponentSampleModel) SampleModel(java.awt.image.SampleModel) IndexColorModel(java.awt.image.IndexColorModel) ColorModel(java.awt.image.ColorModel) Rectangle(java.awt.Rectangle) Dimension(java.awt.Dimension) RenderedImage(java.awt.image.RenderedImage) IndexColorModel(java.awt.image.IndexColorModel)

Example 34 with SampleModel

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

the class GIFImageWriteParam method getDefaultImageMetadata.

public IIOMetadata getDefaultImageMetadata(ImageTypeSpecifier imageType, ImageWriteParam param) {
    GIFWritableImageMetadata imageMetadata = new GIFWritableImageMetadata();
    // Image dimensions
    SampleModel sampleModel = imageType.getSampleModel();
    Rectangle sourceBounds = new Rectangle(sampleModel.getWidth(), sampleModel.getHeight());
    Dimension destSize = new Dimension();
    computeRegions(sourceBounds, destSize, param);
    imageMetadata.imageWidth = destSize.width;
    imageMetadata.imageHeight = destSize.height;
    if (param != null && param.canWriteProgressive() && param.getProgressiveMode() == ImageWriteParam.MODE_DISABLED) {
        imageMetadata.interlaceFlag = false;
    } else {
        imageMetadata.interlaceFlag = true;
    }
    // Local color table
    ColorModel colorModel = imageType.getColorModel();
    imageMetadata.localColorTable = createColorTable(colorModel, sampleModel);
    if (colorModel instanceof IndexColorModel) {
        int transparentIndex = ((IndexColorModel) colorModel).getTransparentPixel();
        if (transparentIndex != -1) {
            imageMetadata.transparentColorFlag = true;
            imageMetadata.transparentColorIndex = transparentIndex;
        }
    }
    return imageMetadata;
}
Also used : ComponentSampleModel(java.awt.image.ComponentSampleModel) SampleModel(java.awt.image.SampleModel) IndexColorModel(java.awt.image.IndexColorModel) ColorModel(java.awt.image.ColorModel) Rectangle(java.awt.Rectangle) Dimension(java.awt.Dimension) IndexColorModel(java.awt.image.IndexColorModel)

Example 35 with SampleModel

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

the class GIFImageWriteParam method needToCreateIndex.

private boolean needToCreateIndex(RenderedImage image) {
    SampleModel sampleModel = image.getSampleModel();
    ColorModel colorModel = image.getColorModel();
    return sampleModel.getNumBands() != 1 || sampleModel.getSampleSize()[0] > 8 || colorModel.getComponentSize()[0] > 8;
}
Also used : ComponentSampleModel(java.awt.image.ComponentSampleModel) SampleModel(java.awt.image.SampleModel) IndexColorModel(java.awt.image.IndexColorModel) ColorModel(java.awt.image.ColorModel)

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