Package java.awt.image

Examples of java.awt.image.IndexColorModel


            colorModel instanceof IndexColorModel &&
            dataType != DataBuffer.TYPE_BYTE) {
            // Don't support (unsigned) short palette-color images.
            throw new RuntimeException(PropertyUtil.getString("TIFFImageEncoder6"));
        }
        IndexColorModel icm = null;
        int sizeOfColormap = 0;
        char[] colormap = null;

        // Set image type.
        int imageType = TIFF_UNSUPPORTED;
        int numExtraSamples = 0;
        int extraSampleType = EXTRA_SAMPLE_UNSPECIFIED;
        if (colorModel instanceof IndexColorModel) { // Bilevel or palette
            icm = (IndexColorModel)colorModel;
            int mapSize = icm.getMapSize();

            if (sampleSize[0] == 1 && numBands == 1) { // Bilevel image

                if (mapSize != 2) {
                    throw new IllegalArgumentException(PropertyUtil.getString("TIFFImageEncoder7"));
                }

                byte[] r = new byte[mapSize];
                icm.getReds(r);
                byte[] g = new byte[mapSize];
                icm.getGreens(g);
                byte[] b = new byte[mapSize];
                icm.getBlues(b);

                if ((r[0] & 0xff) == 0 &&
                    (r[1] & 0xff) == 255 &&
                    (g[0] & 0xff) == 0 &&
                    (g[1] & 0xff) == 255 &&
                    (b[0] & 0xff) == 0 &&
                    (b[1] & 0xff) == 255) {

                    imageType = TIFF_BILEVEL_BLACK_IS_ZERO;

                } else if ((r[0] & 0xff) == 255 &&
                           (r[1] & 0xff) == 0 &&
                           (g[0] & 0xff) == 255 &&
                           (g[1] & 0xff) == 0 &&
                           (b[0] & 0xff) == 255 &&
                           (b[1] & 0xff) == 0) {

                    imageType = TIFF_BILEVEL_WHITE_IS_ZERO;

                } else {
                    imageType = TIFF_PALETTE;
                }

            } else if (numBands == 1) { // Non-bilevel image.
                // Palette color image.
                imageType = TIFF_PALETTE;
            }
        } else if (colorModel == null) {

            if (sampleSize[0] == 1 && numBands == 1) { // bilevel
                imageType = TIFF_BILEVEL_BLACK_IS_ZERO;
            } else { // generic image
                imageType = TIFF_GENERIC;
                if (numBands > 1) {
                    numExtraSamples = numBands - 1;
                }
            }

        } else { // colorModel is non-null but not an IndexColorModel
            ColorSpace colorSpace = colorModel.getColorSpace();

            switch(colorSpace.getType()) {
            case ColorSpace.TYPE_CMYK:
                imageType = TIFF_CMYK;
                break;
            case ColorSpace.TYPE_GRAY:
                imageType = TIFF_GRAY;
                break;
            case ColorSpace.TYPE_Lab:
                imageType = TIFF_CIELAB;
                break;
            case ColorSpace.TYPE_RGB:
                if (compression == COMP_JPEG_TTN2
                        && encodeParam.getJPEGCompressRGBToYCbCr()) {
                    imageType = TIFF_YCBCR;
                } else {
                    imageType = TIFF_RGB;
                }
                break;
            case ColorSpace.TYPE_YCbCr:
                imageType = TIFF_YCBCR;
                break;
            default:
                imageType = TIFF_GENERIC; // generic
                break;
            }

            if (imageType == TIFF_GENERIC) {
                numExtraSamples = numBands - 1;
            } else if (numBands > 1) {
                numExtraSamples = numBands - colorSpace.getNumComponents();
            }

            if (numExtraSamples == 1 && colorModel.hasAlpha()) {
                extraSampleType = colorModel.isAlphaPremultiplied() ?
                    EXTRA_SAMPLE_ASSOCIATED_ALPHA :
                    EXTRA_SAMPLE_UNASSOCIATED_ALPHA;
            }
        }

        if (imageType == TIFF_UNSUPPORTED) {
            throw new RuntimeException(PropertyUtil.getString("TIFFImageEncoder8"));
        }

        int photometricInterpretation = -1;
        switch (imageType) {

        case TIFF_BILEVEL_WHITE_IS_ZERO:
            photometricInterpretation = 0;
            break;

        case TIFF_BILEVEL_BLACK_IS_ZERO:
            photometricInterpretation = 1;
            break;

        case TIFF_GRAY:
        case TIFF_GENERIC:
            // Since the CS_GRAY colorspace is always of type black_is_zero
            photometricInterpretation = 1;
            break;

        case TIFF_PALETTE:
            photometricInterpretation = 3;

            icm = (IndexColorModel)colorModel;
            sizeOfColormap = icm.getMapSize();

            byte[] r = new byte[sizeOfColormap];
            icm.getReds(r);
            byte[] g = new byte[sizeOfColormap];
            icm.getGreens(g);
            byte[] b = new byte[sizeOfColormap];
            icm.getBlues(b);

            int redIndex = 0, greenIndex = sizeOfColormap;
            int blueIndex = 2 * sizeOfColormap;
            colormap = new char[sizeOfColormap * 3];
            for (int i = 0; i < sizeOfColormap; i++) {
View Full Code Here


                                                    tileHeight,
                                                    sampleSize);
                if (imageType == TYPE_BILEVEL) {
                    byte[] map = new byte[] {(byte)(isWhiteZero ? 255 : 0),
                                             (byte)(isWhiteZero ? 0 : 255)};
                    colorModel = new IndexColorModel(1, 2, map, map, map);
                } else {
                    byte [] map = new byte[16];
                    if (isWhiteZero) {
                        for (int i = 0; i < map.length; i++) {
                            map[i] = (byte)(255 - (16 * i));
                        }
                    } else {
                        for (int i = 0; i < map.length; i++) {
                            map[i] = (byte)(16 * i);
                        }
                    }
                    colorModel = new IndexColorModel(4, 16, map, map, map);
                }
                break;

            case TYPE_GRAY:
            case TYPE_GRAY_ALPHA:
            case TYPE_RGB:
            case TYPE_RGB_ALPHA:
                // Create a pixel interleaved SampleModel with decreasing
                // band offsets.
                int[] reverseOffsets = new int[numBands];
                for (int i = 0; i < numBands; i++) {
                    reverseOffsets[i] = numBands - 1 - i;
                }
                sampleModel = new PixelInterleavedSampleModel
                    (dataType, tileWidth, tileHeight,
                     numBands, numBands * tileWidth, reverseOffsets);

                if (imageType == TYPE_GRAY) {
                  colorModel = new ComponentColorModel
                    (ColorSpace.getInstance(ColorSpace.CS_GRAY),
                     new int[] {sampleSize}, false, false,
                     Transparency.OPAQUE, dataType);
                } else if (imageType == TYPE_RGB) {
                  colorModel = new ComponentColorModel
                    (ColorSpace.getInstance(ColorSpace.CS_sRGB),
                     new int[] {sampleSize, sampleSize, sampleSize},
                     false, false, Transparency.OPAQUE, dataType);
                } else { // hasAlpha
                    // Transparency.OPAQUE signifies image data that is
                    // completely opaque, meaning that all pixels have an alpha
                    // value of 1.0. So the extra band gets ignored, which is
                    // what we want.
                    int transparency = Transparency.OPAQUE;
                    if (extraSamples == 1) { // associated (premultiplied) alpha
                        transparency = Transparency.TRANSLUCENT;
                    } else if (extraSamples == 2) { // unassociated alpha
                        transparency = Transparency.BITMASK;
                    }

                    colorModel =
                        createAlphaComponentColorModel(dataType,
                                                       numBands,
                                                       extraSamples == 1,
                                                       transparency);
                }
                break;

            case TYPE_GENERIC:
            case TYPE_YCBCR_SUB:
                // For this case we can't display the image, so we create a
                // SampleModel with increasing bandOffsets, and keep the
                // ColorModel as null, as there is no appropriate ColorModel.

                int[] bandOffsets = new int[numBands];
                for (int i = 0; i < numBands; i++) {
                    bandOffsets[i] = i;
                }

                sampleModel = new PixelInterleavedSampleModel
                    (dataType, tileWidth, tileHeight,
                     numBands, numBands * tileWidth, bandOffsets);
                colorModel = null;
                break;

            case TYPE_PALETTE:
                // Get the colormap
                TIFFField cfield = dir.getField(TIFFImageDecoder.TIFF_COLORMAP);
                if (cfield == null) {
                    throw new RuntimeException(PropertyUtil.getString("TIFFImage11"));
                } else {
                    colormap = cfield.getAsChars();
                }

                // Could be either 1 or 3 bands depending on whether we use
                // IndexColorModel or not.
                if (decodePaletteAsShorts) {
                    numBands = 3;

                    // If no SampleFormat tag was specified and if the
                    // sampleSize is less than or equal to 8, then the
                    // dataType was initially set to byte, but now we want to
                    // expand the palette as shorts, so the dataType should
                    // be ushort.
                    if (dataType == DataBuffer.TYPE_BYTE) {
                        dataType = DataBuffer.TYPE_USHORT;
                    }

                    // Data will have to be unpacked into a 3 band short image
                    // as we do not have a IndexColorModel that can deal with
                    // a colormodel whose entries are of short data type.
                    sampleModel = createPixelInterleavedSampleModel
                        (dataType, tileWidth, tileHeight, numBands);

                  colorModel = new ComponentColorModel
                    (ColorSpace.getInstance(ColorSpace.CS_sRGB),
                     new int[] {16, 16, 16}, false, false,
                     Transparency.OPAQUE, dataType);

                } else {

                    numBands = 1;

                    if (sampleSize == 4) {
                        // Pixel data will not be unpacked, will use
                        // MPPSM to store packed data and
                        // IndexColorModel to do the unpacking.
                        sampleModel = new MultiPixelPackedSampleModel
                            (DataBuffer.TYPE_BYTE, tileWidth, tileHeight,
                             sampleSize);
                    } else if (sampleSize == 8) {

                        sampleModel = createPixelInterleavedSampleModel
                            (DataBuffer.TYPE_BYTE, tileWidth, tileHeight,
                             numBands);
                    } else if (sampleSize == 16) {

                        // Here datatype has to be unsigned since we
                        // are storing indices into the
                        // IndexColorModel palette. Ofcourse the
                        // actual palette entries are allowed to be
                        // negative.
                        dataType = DataBuffer.TYPE_USHORT;
                        sampleModel = createPixelInterleavedSampleModel
                            (DataBuffer.TYPE_USHORT, tileWidth, tileHeight,
                             numBands);
                    }

                    int bandLength = colormap.length / 3;
                    byte[] r = new byte[bandLength];
                    byte[] g = new byte[bandLength];
                    byte[] b = new byte[bandLength];

                    int gIndex = bandLength;
                    int bIndex = bandLength * 2;

                    if (dataType == DataBuffer.TYPE_SHORT) {

                        for (int i = 0; i < bandLength; i++) {
                            r[i] = param.decodeSigned16BitsTo8Bits
                                ((short)colormap[i]);
                            g[i] = param.decodeSigned16BitsTo8Bits
                                ((short)colormap[gIndex + i]);
                            b[i] = param.decodeSigned16BitsTo8Bits
                                ((short)colormap[bIndex + i]);
                        }

                    } else {

                        for (int i = 0; i < bandLength; i++) {
                            r[i] = param.decode16BitsTo8Bits
                                (colormap[i] & 0xffff);
                            g[i] = param.decode16BitsTo8Bits
                                (colormap[gIndex + i] & 0xffff);
                            b[i] = param.decode16BitsTo8Bits
                                (colormap[bIndex + i] & 0xffff);
                        }

                    }

                    colorModel = new IndexColorModel(sampleSize,
                                                     bandLength, r, g, b);
                }
                break;

            default:
View Full Code Here

    private static void prepareColorSpace(PSGenerator gen, ColorModel cm) throws IOException {
        //Prepare color space
        if ((cm instanceof IndexColorModel)) {
            ColorSpace cs = cm.getColorSpace();
            IndexColorModel im = (IndexColorModel)cm;
            gen.write("[/Indexed " + getColorSpaceName(cs));
            int c = im.getMapSize();
            int hival = c - 1;
            if (hival > 4095) {
                throw new UnsupportedOperationException("hival must not go beyond 4095");
            }
            gen.writeln(" " + Integer.toString(hival));
            gen.write("  <");
            int[] palette = new int[c];
            im.getRGBs(palette);
            for (int i = 0; i < c; i++) {
                if (i > 0) {
                    if ((i % 8) == 0) {
                        gen.newLine();
                        gen.write("   ");
View Full Code Here

                    for (int x = 0; x < w; x++) {
                        dst_raster.setPixel(x, y, raster.getPixel(x, y, samples));
                    }
                }
            } else if (model instanceof IndexColorModel) {
                final IndexColorModel icm = (IndexColorModel) model;
                for (int y = 0; y < h; y++) {
                    for (int x = 0; x < w; x++) {
                        int sample = raster.getSample(x, y, 0);
                        samples[0] = icm.getRed(sample);
                        samples[1] = icm.getGreen(sample);
                        samples[2] = icm.getBlue(sample);
                        samples[3] = icm.getAlpha(sample);
                        dst_raster.setPixel(x, y, samples);
                    }
                }
            } else {
                // TODO implement special case of a custom ColorModel not based on jdk concrete classes
View Full Code Here

            } else if (model instanceof ComponentColorModel) {
                for (int y = 0; y < h; y++)
                    for (int x = 0; x < w; x++)
                        dst_raster.setPixel(x, y, raster.getPixel(x, y, samples));
            } else if (model instanceof IndexColorModel) {
                final IndexColorModel icm = (IndexColorModel) model;
                for (int y = 0; y < h; y++)
                    for (int x = 0; x < w; x++) {
                        int sample = raster.getSample(x, y, 0);
                        samples[0] = icm.getRed(sample);
                        samples[1] = icm.getGreen(sample);
                        samples[2] = icm.getBlue(sample);
                        samples[3] = icm.getAlpha(sample);
                        dst_raster.setPixel(x, y, samples);
                    }
            } else {
                log.error("Unimplemented raster conversion (required: " + model + " + available: " + dst_model);
                return raster;
View Full Code Here

            if (model instanceof ComponentColorModel) {
                for (int y = 0; y < h; y++)
                    for (int x = 0; x < w; x++)
                        dst_raster.setPixel(x, y, raster.getPixel(x, y, samples));
            } else if (model instanceof IndexColorModel) {
                final IndexColorModel icm = (IndexColorModel) model;
                for (int y = 0; y < h; y++)
                    for (int x = 0; x < w; x++) {
                        int sample = raster.getSample(x, y, 0);
                        samples[0] = icm.getRed(sample);
                        samples[1] = icm.getGreen(sample);
                        samples[2] = icm.getBlue(sample);
                        samples[3] = icm.getAlpha(sample);
                        dst_raster.setPixel(x, y, samples);
                    }
            } else {
                log.error("Unimplemented raster conversion");
                return raster;
View Full Code Here

    int bitCount = 1;
    while (added.size() >> bitCount != 0) {
      bitCount *= 2;
    }

    IndexColorModel icm = new IndexColorModel(bitCount,
        added.size(), cmap, 0, DataBuffer.TYPE_BYTE, null);

    // Check if generated palette matched original
    if (img.getColorModel() instanceof IndexColorModel) {
      IndexColorModel originalModel = (IndexColorModel)img.getColorModel();
      if (originalModel.getPixelSize() == icm.getPixelSize() &&
          originalModel.getMapSize() == icm.getMapSize()) {
        // Old model already had efficient palette
        return null;
      }
    }

View Full Code Here

      // Image is too large to depalettize
      return null;
    }
    ColorModel colorModel = img.getColorModel();
    if (colorModel instanceof IndexColorModel) {
      IndexColorModel indexModel = (IndexColorModel)colorModel;
      return indexModel.convertToIntDiscrete(img.getData(), false);
    }
    return null;
  }
View Full Code Here

            baseXOffset += curGlyph.getGlyphPointMetrics().getAdvanceX();
        }

        byte[] blackWhite = new byte[] { 0, (byte) 0xff };
        IndexColorModel colorModel = new IndexColorModel(1, 2, blackWhite,
                blackWhite, blackWhite);

        BufferedImage stringImage = new BufferedImage(colorModel, wr, false,
                null);
        return stringImage;
View Full Code Here

        }

        final byte[] r = new byte[] { 0, 127 };
        final byte[] g = new byte[] { 0, 127 };
        final byte[] b = new byte[] { 0, 127 };
        final IndexColorModel colorModel = new IndexColorModel(1, 2, r, g, b);

        final MemoryImageSource producer = new MemoryImageSource(128, 128, colorModel, pixels, 0, 128);
        final Image image = Toolkit.getDefaultToolkit().createImage(producer);

        return image;
View Full Code Here

TOP

Related Classes of java.awt.image.IndexColorModel

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.