Search in sources :

Example 51 with IndexColorModel

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

the class GIFImageWriteParam method createColorTable.

/**
     * Create a color table from the image ColorModel and SampleModel.
     */
private static byte[] createColorTable(ColorModel colorModel, SampleModel sampleModel) {
    byte[] colorTable;
    if (colorModel instanceof IndexColorModel) {
        IndexColorModel icm = (IndexColorModel) colorModel;
        int mapSize = icm.getMapSize();
        /**
             * The GIF image format assumes that size of image palette
             * is power of two. We will use closest larger power of two
             * as size of color table.
             */
        int ctSize = getGifPaletteSize(mapSize);
        byte[] reds = new byte[ctSize];
        byte[] greens = new byte[ctSize];
        byte[] blues = new byte[ctSize];
        icm.getReds(reds);
        icm.getGreens(greens);
        icm.getBlues(blues);
        /**
             * fill tail of color component arrays by replica of first color
             * in order to avoid appearance of extra colors in the color table
             */
        for (int i = mapSize; i < ctSize; i++) {
            reds[i] = reds[0];
            greens[i] = greens[0];
            blues[i] = blues[0];
        }
        colorTable = new byte[3 * ctSize];
        int idx = 0;
        for (int i = 0; i < ctSize; i++) {
            colorTable[idx++] = reds[i];
            colorTable[idx++] = greens[i];
            colorTable[idx++] = blues[i];
        }
    } else if (sampleModel.getNumBands() == 1) {
        // create gray-scaled color table for single-banded images
        int numBits = sampleModel.getSampleSize()[0];
        if (numBits > 8) {
            numBits = 8;
        }
        int colorTableLength = 3 * (1 << numBits);
        colorTable = new byte[colorTableLength];
        for (int i = 0; i < colorTableLength; i++) {
            colorTable[i] = (byte) (i / 3);
        }
    } else {
        // We do not have enough information here
        // to create well-fit color table for RGB image.
        colorTable = null;
    }
    return colorTable;
}
Also used : IndexColorModel(java.awt.image.IndexColorModel)

Example 52 with IndexColorModel

use of java.awt.image.IndexColorModel 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 53 with IndexColorModel

use of java.awt.image.IndexColorModel 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 54 with IndexColorModel

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

the class GIFImageReader method getDefaultPalette.

private static synchronized byte[] getDefaultPalette() {
    if (defaultPalette == null) {
        BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_BYTE_INDEXED);
        IndexColorModel icm = (IndexColorModel) img.getColorModel();
        final int size = icm.getMapSize();
        byte[] r = new byte[size];
        byte[] g = new byte[size];
        byte[] b = new byte[size];
        icm.getReds(r);
        icm.getGreens(g);
        icm.getBlues(b);
        defaultPalette = new byte[size * 3];
        for (int i = 0; i < size; i++) {
            defaultPalette[3 * i + 0] = r[i];
            defaultPalette[3 * i + 1] = g[i];
            defaultPalette[3 * i + 2] = b[i];
        }
    }
    return defaultPalette;
}
Also used : BufferedImage(java.awt.image.BufferedImage) Point(java.awt.Point) IndexColorModel(java.awt.image.IndexColorModel)

Example 55 with IndexColorModel

use of java.awt.image.IndexColorModel 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)

Aggregations

IndexColorModel (java.awt.image.IndexColorModel)74 ColorModel (java.awt.image.ColorModel)30 BufferedImage (java.awt.image.BufferedImage)29 DirectColorModel (java.awt.image.DirectColorModel)17 Point (java.awt.Point)14 WritableRaster (java.awt.image.WritableRaster)12 SampleModel (java.awt.image.SampleModel)10 ComponentColorModel (java.awt.image.ComponentColorModel)8 Rectangle (java.awt.Rectangle)7 DataBufferByte (java.awt.image.DataBufferByte)7 MultiPixelPackedSampleModel (java.awt.image.MultiPixelPackedSampleModel)7 Raster (java.awt.image.Raster)7 ComponentSampleModel (java.awt.image.ComponentSampleModel)6 Graphics2D (java.awt.Graphics2D)5 ColorSpace (java.awt.color.ColorSpace)5 Color (java.awt.Color)4 Graphics (java.awt.Graphics)4 ICC_ColorSpace (java.awt.color.ICC_ColorSpace)4 SinglePixelPackedSampleModel (java.awt.image.SinglePixelPackedSampleModel)4 IOException (java.io.IOException)4