Search in sources :

Example 21 with IndexColorModel

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

the class GIFImageReader method createIndexed.

// We don't check all parameters as ImageTypeSpecifier.createIndexed do
// since this method is private and we pass consistent data here
private ImageTypeSpecifier createIndexed(byte[] r, byte[] g, byte[] b, int bits) {
    ColorModel colorModel;
    if (imageMetadata.transparentColorFlag) {
        // Some files erroneously have a transparent color index
        // of 255 even though there are fewer than 256 colors.
        int idx = Math.min(imageMetadata.transparentColorIndex, r.length - 1);
        colorModel = new IndexColorModel(bits, r.length, r, g, b, idx);
    } else {
        colorModel = new IndexColorModel(bits, r.length, r, g, b);
    }
    SampleModel sampleModel;
    if (bits == 8) {
        int[] bandOffsets = { 0 };
        sampleModel = new PixelInterleavedSampleModel(DataBuffer.TYPE_BYTE, 1, 1, 1, 1, bandOffsets);
    } else {
        sampleModel = new MultiPixelPackedSampleModel(DataBuffer.TYPE_BYTE, 1, 1, bits);
    }
    return new ImageTypeSpecifier(colorModel, sampleModel);
}
Also used : PixelInterleavedSampleModel(java.awt.image.PixelInterleavedSampleModel) PixelInterleavedSampleModel(java.awt.image.PixelInterleavedSampleModel) SampleModel(java.awt.image.SampleModel) MultiPixelPackedSampleModel(java.awt.image.MultiPixelPackedSampleModel) ColorModel(java.awt.image.ColorModel) IndexColorModel(java.awt.image.IndexColorModel) MultiPixelPackedSampleModel(java.awt.image.MultiPixelPackedSampleModel) Point(java.awt.Point) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) IndexColorModel(java.awt.image.IndexColorModel)

Example 22 with IndexColorModel

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

the class PaletteBuilder method getIndexColorModel.

protected IndexColorModel getIndexColorModel() {
    int size = currSize;
    if (transColor != null) {
        // we need place for transparent color;
        size++;
    }
    byte[] red = new byte[size];
    byte[] green = new byte[size];
    byte[] blue = new byte[size];
    int index = 0;
    palette = new ColorNode[size];
    if (transColor != null) {
        index++;
    }
    if (root != null) {
        findPaletteEntry(root, index, red, green, blue);
    }
    IndexColorModel icm = null;
    if (transColor != null) {
        icm = new IndexColorModel(8, size, red, green, blue, 0);
    } else {
        icm = new IndexColorModel(8, currSize, red, green, blue);
    }
    return icm;
}
Also used : IndexColorModel(java.awt.image.IndexColorModel)

Example 23 with IndexColorModel

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

the class PNGImageWriterSpi method canEncodeImage.

public boolean canEncodeImage(ImageTypeSpecifier type) {
    SampleModel sampleModel = type.getSampleModel();
    ColorModel colorModel = type.getColorModel();
    // Find the maximum bit depth across all channels
    int[] sampleSize = sampleModel.getSampleSize();
    int bitDepth = sampleSize[0];
    for (int i = 1; i < sampleSize.length; i++) {
        if (sampleSize[i] > bitDepth) {
            bitDepth = sampleSize[i];
        }
    }
    // Ensure bitDepth is between 1 and 16
    if (bitDepth < 1 || bitDepth > 16) {
        return false;
    }
    // Check number of bands, alpha
    int numBands = sampleModel.getNumBands();
    if (numBands < 1 || numBands > 4) {
        return false;
    }
    boolean hasAlpha = colorModel.hasAlpha();
    // the check below to fail and return false.
    if (colorModel instanceof IndexColorModel) {
        return true;
    }
    if ((numBands == 1 || numBands == 3) && hasAlpha) {
        return false;
    }
    if ((numBands == 2 || numBands == 4) && !hasAlpha) {
        return false;
    }
    return true;
}
Also used : SampleModel(java.awt.image.SampleModel) ColorModel(java.awt.image.ColorModel) IndexColorModel(java.awt.image.IndexColorModel) IndexColorModel(java.awt.image.IndexColorModel)

Example 24 with IndexColorModel

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

the class PNGMetadata method initialize.

/**
     * Sets the IHDR_bitDepth and IHDR_colorType variables.
     * The <code>numBands</code> parameter is necessary since
     * we may only be writing a subset of the image bands.
     */
public void initialize(ImageTypeSpecifier imageType, int numBands) {
    ColorModel colorModel = imageType.getColorModel();
    SampleModel sampleModel = imageType.getSampleModel();
    // Initialize IHDR_bitDepth
    int[] sampleSize = sampleModel.getSampleSize();
    int bitDepth = sampleSize[0];
    // Fixes bug 4413109
    for (int i = 1; i < sampleSize.length; i++) {
        if (sampleSize[i] > bitDepth) {
            bitDepth = sampleSize[i];
        }
    }
    // Multi-channel images must have a bit depth of 8 or 16
    if (sampleSize.length > 1 && bitDepth < 8) {
        bitDepth = 8;
    }
    // Round bit depth up to a power of 2
    if (bitDepth > 2 && bitDepth < 4) {
        bitDepth = 4;
    } else if (bitDepth > 4 && bitDepth < 8) {
        bitDepth = 8;
    } else if (bitDepth > 8 && bitDepth < 16) {
        bitDepth = 16;
    } else if (bitDepth > 16) {
        throw new RuntimeException("bitDepth > 16!");
    }
    IHDR_bitDepth = bitDepth;
    // Initialize IHDR_colorType
    if (colorModel instanceof IndexColorModel) {
        IndexColorModel icm = (IndexColorModel) colorModel;
        int size = icm.getMapSize();
        byte[] reds = new byte[size];
        icm.getReds(reds);
        byte[] greens = new byte[size];
        icm.getGreens(greens);
        byte[] blues = new byte[size];
        icm.getBlues(blues);
        // Determine whether the color tables are actually a gray ramp
        // if the color type has not been set previously
        boolean isGray = false;
        if (!IHDR_present || (IHDR_colorType != PNGImageReader.PNG_COLOR_PALETTE)) {
            isGray = true;
            int scale = 255 / ((1 << IHDR_bitDepth) - 1);
            for (int i = 0; i < size; i++) {
                byte red = reds[i];
                if ((red != (byte) (i * scale)) || (red != greens[i]) || (red != blues[i])) {
                    isGray = false;
                    break;
                }
            }
        }
        // Determine whether transparency exists
        boolean hasAlpha = colorModel.hasAlpha();
        byte[] alpha = null;
        if (hasAlpha) {
            alpha = new byte[size];
            icm.getAlphas(alpha);
        }
        if (isGray && hasAlpha && (bitDepth == 8 || bitDepth == 16)) {
            IHDR_colorType = PNGImageReader.PNG_COLOR_GRAY_ALPHA;
        } else if (isGray && !hasAlpha) {
            IHDR_colorType = PNGImageReader.PNG_COLOR_GRAY;
        } else {
            IHDR_colorType = PNGImageReader.PNG_COLOR_PALETTE;
            PLTE_present = true;
            PLTE_order = null;
            PLTE_red = (byte[]) reds.clone();
            PLTE_green = (byte[]) greens.clone();
            PLTE_blue = (byte[]) blues.clone();
            if (hasAlpha) {
                tRNS_present = true;
                tRNS_colorType = PNGImageReader.PNG_COLOR_PALETTE;
                PLTE_order = new int[alpha.length];
                // Reorder the palette so that non-opaque entries
                // come first.  Since the tRNS chunk does not have
                // to store trailing 255's, this can save a
                // considerable amount of space when encoding
                // images with only one transparent pixel value,
                // e.g., images from GIF sources.
                byte[] newAlpha = new byte[alpha.length];
                // Scan for non-opaque entries and assign them
                // positions starting at 0.
                int newIndex = 0;
                for (int i = 0; i < alpha.length; i++) {
                    if (alpha[i] != (byte) 255) {
                        PLTE_order[i] = newIndex;
                        newAlpha[newIndex] = alpha[i];
                        ++newIndex;
                    }
                }
                int numTransparent = newIndex;
                // positions following the non-opaque entries.
                for (int i = 0; i < alpha.length; i++) {
                    if (alpha[i] == (byte) 255) {
                        PLTE_order[i] = newIndex++;
                    }
                }
                // Reorder the palettes
                byte[] oldRed = PLTE_red;
                byte[] oldGreen = PLTE_green;
                byte[] oldBlue = PLTE_blue;
                // All have the same length
                int len = oldRed.length;
                PLTE_red = new byte[len];
                PLTE_green = new byte[len];
                PLTE_blue = new byte[len];
                for (int i = 0; i < len; i++) {
                    PLTE_red[PLTE_order[i]] = oldRed[i];
                    PLTE_green[PLTE_order[i]] = oldGreen[i];
                    PLTE_blue[PLTE_order[i]] = oldBlue[i];
                }
                // Copy only the transparent entries into tRNS_alpha
                tRNS_alpha = new byte[numTransparent];
                System.arraycopy(newAlpha, 0, tRNS_alpha, 0, numTransparent);
            }
        }
    } else {
        if (numBands == 1) {
            IHDR_colorType = PNGImageReader.PNG_COLOR_GRAY;
        } else if (numBands == 2) {
            IHDR_colorType = PNGImageReader.PNG_COLOR_GRAY_ALPHA;
        } else if (numBands == 3) {
            IHDR_colorType = PNGImageReader.PNG_COLOR_RGB;
        } else if (numBands == 4) {
            IHDR_colorType = PNGImageReader.PNG_COLOR_RGB_ALPHA;
        } else {
            throw new RuntimeException("Number of bands not 1-4!");
        }
    }
    IHDR_present = true;
}
Also used : SampleModel(java.awt.image.SampleModel) ColorModel(java.awt.image.ColorModel) IndexColorModel(java.awt.image.IndexColorModel) IndexColorModel(java.awt.image.IndexColorModel)

Example 25 with IndexColorModel

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

the class TransparencyTest method main.

public static void main(String[] args) {
    System.out.println("Test indexed image...");
    IndexColorModel icm = createIndexedBitmaskColorModel();
    BufferedImage img = createIndexedImage(200, 200, icm);
    TransparencyTest t = new TransparencyTest(img);
    try {
        t.doTest();
    } catch (Exception e) {
        throw new RuntimeException("Test failed!", e);
    }
    System.out.println("Test passed.");
}
Also used : BufferedImage(java.awt.image.BufferedImage) IOException(java.io.IOException) 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