Search in sources :

Example 51 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 52 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 53 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)

Example 54 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 55 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)

Aggregations

SampleModel (java.awt.image.SampleModel)97 ColorModel (java.awt.image.ColorModel)43 ComponentSampleModel (java.awt.image.ComponentSampleModel)38 Point (java.awt.Point)33 SinglePixelPackedSampleModel (java.awt.image.SinglePixelPackedSampleModel)33 DataBuffer (java.awt.image.DataBuffer)32 Rectangle (java.awt.Rectangle)30 IndexColorModel (java.awt.image.IndexColorModel)29 BufferedImage (java.awt.image.BufferedImage)27 MultiPixelPackedSampleModel (java.awt.image.MultiPixelPackedSampleModel)26 WritableRaster (java.awt.image.WritableRaster)26 PixelInterleavedSampleModel (java.awt.image.PixelInterleavedSampleModel)23 DataBufferByte (java.awt.image.DataBufferByte)20 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)18 ComponentColorModel (java.awt.image.ComponentColorModel)17 DataBufferInt (java.awt.image.DataBufferInt)17 ColorSpace (java.awt.color.ColorSpace)15 Raster (java.awt.image.Raster)15 DataBufferUShort (java.awt.image.DataBufferUShort)13 BandedSampleModel (java.awt.image.BandedSampleModel)12