Java Code Examples for java.awt.image.IndexColorModel#getMapSize()

The following examples show how to use java.awt.image.IndexColorModel#getMapSize() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ShortHistogramTest.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private IIOMetadataNode gethISTNode(BufferedImage bi) {
    IndexColorModel icm = (IndexColorModel)bi.getColorModel();
    int mapSize = icm.getMapSize();

    int[] hist = new int[mapSize];
    Arrays.fill(hist, 0);

    Raster r = bi.getData();
    for (int y = 0; y < bi.getHeight(); y++) {
        for (int x = 0; x < bi.getWidth(); x++) {
            int s = r.getSample(x, y, 0);
            hist[s] ++;
        }
    }

    IIOMetadataNode hIST = new IIOMetadataNode("hIST");
    for (int i = 0; i < hist.length; i++) {
        IIOMetadataNode n = new IIOMetadataNode("hISTEntry");
        n.setAttribute("index", "" + i);
        n.setAttribute("value", "" + hist[i]);
        hIST.appendChild(n);
    }

    return hIST;
}
 
Example 2
Source File: ShortHistogramTest.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private IIOMetadataNode gethISTNode(BufferedImage bi) {
    IndexColorModel icm = (IndexColorModel)bi.getColorModel();
    int mapSize = icm.getMapSize();

    int[] hist = new int[mapSize];
    Arrays.fill(hist, 0);

    Raster r = bi.getData();
    for (int y = 0; y < bi.getHeight(); y++) {
        for (int x = 0; x < bi.getWidth(); x++) {
            int s = r.getSample(x, y, 0);
            hist[s] ++;
        }
    }

    IIOMetadataNode hIST = new IIOMetadataNode("hIST");
    for (int i = 0; i < hist.length; i++) {
        IIOMetadataNode n = new IIOMetadataNode("hISTEntry");
        n.setAttribute("index", "" + i);
        n.setAttribute("value", "" + hist[i]);
        hIST.appendChild(n);
    }

    return hIST;
}
 
Example 3
Source File: GIFImageReader.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
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;
}
 
Example 4
Source File: GIFImageReader.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
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;
}
 
Example 5
Source File: GIFImageReader.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
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;
}
 
Example 6
Source File: GIFImageReader.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
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;
}
 
Example 7
Source File: TexturePaintContext.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
public static boolean isFilterableICM(ColorModel cm) {
    if (cm instanceof IndexColorModel) {
        IndexColorModel icm = (IndexColorModel) cm;
        if (icm.getMapSize() <= 256) {
            return true;
        }
    }
    return false;
}
 
Example 8
Source File: WPrinterJob.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
protected void drawDIBImage(byte[] image,
                            float destX, float destY,
                            float destWidth, float destHeight,
                            float srcX, float srcY,
                            float srcWidth, float srcHeight,
                            int sampleBitsPerPixel,
                            IndexColorModel icm) {
    int bitCount = 24;
    byte[] bmiColors = null;

    if (icm != null) {
        bitCount = sampleBitsPerPixel;
        bmiColors = new byte[(1<<icm.getPixelSize())*4];
        for (int i=0;i<icm.getMapSize(); i++) {
            bmiColors[i*4+0]=(byte)(icm.getBlue(i)&0xff);
            bmiColors[i*4+1]=(byte)(icm.getGreen(i)&0xff);
            bmiColors[i*4+2]=(byte)(icm.getRed(i)&0xff);
        }
    }

    drawDIBImage(getPrintDC(), image,
                 destX, destY,
                 destWidth, destHeight,
                 srcX, srcY,
                 srcWidth, srcHeight,
                 bitCount, bmiColors);
}
 
Example 9
Source File: TexturePaintContext.java    From jdk-1.7-annotated with Apache License 2.0 5 votes vote down vote up
public static boolean isFilterableICM(ColorModel cm) {
    if (cm instanceof IndexColorModel) {
        IndexColorModel icm = (IndexColorModel) cm;
        if (icm.getMapSize() <= 256) {
            return true;
        }
    }
    return false;
}
 
Example 10
Source File: TexturePaintContext.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public static boolean isFilterableICM(ColorModel cm) {
    if (cm instanceof IndexColorModel) {
        IndexColorModel icm = (IndexColorModel) cm;
        if (icm.getMapSize() <= 256) {
            return true;
        }
    }
    return false;
}
 
Example 11
Source File: AWTImageTools.java    From scifio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/** Converts an IndexColorModel to a 2D byte array. */
public static byte[][] get8BitLookupTable(final ColorModel model) {
	if (!(model instanceof IndexColorModel)) return null;
	final IndexColorModel m = (IndexColorModel) model;
	final byte[][] lut = new byte[3][m.getMapSize()];
	m.getReds(lut[0]);
	m.getGreens(lut[1]);
	m.getBlues(lut[2]);
	return lut;
}
 
Example 12
Source File: TexturePaintContext.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
public static boolean isFilterableICM(ColorModel cm) {
    if (cm instanceof IndexColorModel) {
        IndexColorModel icm = (IndexColorModel) cm;
        if (icm.getMapSize() <= 256) {
            return true;
        }
    }
    return false;
}
 
Example 13
Source File: WPrinterJob.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
protected void drawDIBImage(byte[] image,
                            float destX, float destY,
                            float destWidth, float destHeight,
                            float srcX, float srcY,
                            float srcWidth, float srcHeight,
                            int sampleBitsPerPixel,
                            IndexColorModel icm) {
    int bitCount = 24;
    byte[] bmiColors = null;

    if (icm != null) {
        bitCount = sampleBitsPerPixel;
        bmiColors = new byte[(1<<icm.getPixelSize())*4];
        for (int i=0;i<icm.getMapSize(); i++) {
            bmiColors[i*4+0]=(byte)(icm.getBlue(i)&0xff);
            bmiColors[i*4+1]=(byte)(icm.getGreen(i)&0xff);
            bmiColors[i*4+2]=(byte)(icm.getRed(i)&0xff);
        }
    }

    drawDIBImage(getPrintDC(), image,
                 destX, destY,
                 destWidth, destHeight,
                 srcX, srcY,
                 srcWidth, srcHeight,
                 bitCount, bmiColors);
}
 
Example 14
Source File: JFIFMarkerSegment.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
JFIFThumbPalette(BufferedImage thumb) throws IllegalThumbException {
    super(thumb);
    IndexColorModel icm = (IndexColorModel) thumbnail.getColorModel();
    if (icm.getMapSize() > 256) {
        throw new IllegalThumbException();
    }
}
 
Example 15
Source File: GIFImageWriter.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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;
}
 
Example 16
Source File: GIFImageWriter.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
/**
 * 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;
}
 
Example 17
Source File: GIFImageWriter.java    From jdk1.8-source-analysis with Apache License 2.0 4 votes vote down vote up
/**
 * 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;
}
 
Example 18
Source File: ImageRepresentation.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
public void setColorModel(ColorModel model) {
    if (src != null) {
        src.checkSecurity(null, false);
    }
    srcModel = model;

    // Check to see if model is INT_RGB
    if (model instanceof IndexColorModel) {
        if (model.getTransparency() == model.TRANSLUCENT) {
            // REMIND:
            // Probably need to composite anyway so force ARGB
            cmodel = ColorModel.getRGBdefault();
            srcLUT = null;
        }
        else {
            IndexColorModel icm = (IndexColorModel) model;
            numSrcLUT = icm.getMapSize();
            srcLUT = new int[Math.max(numSrcLUT, 256)];
            icm.getRGBs(srcLUT);
            srcLUTtransIndex = icm.getTransparentPixel();
            cmodel = model;
        }
    }
    else {
        if (cmodel == null) {
            cmodel = model;
            srcLUT   = null;
        }
        else if (model instanceof DirectColorModel) {
            // If it is INT_RGB or INT_ARGB, use the model
            DirectColorModel dcm = (DirectColorModel) model;
            if ((dcm.getRedMask() == 0xff0000) &&
                (dcm.getGreenMask() == 0xff00) &&
                (dcm.getBlueMask()  == 0x00ff)) {
                cmodel   = model;
                srcLUT   = null;
            }
        }
    }

    isSameCM = (cmodel == model);
}
 
Example 19
Source File: ImageRepresentation.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
public void setColorModel(ColorModel model) {
    if (src != null) {
        src.checkSecurity(null, false);
    }
    srcModel = model;

    // Check to see if model is INT_RGB
    if (model instanceof IndexColorModel) {
        if (model.getTransparency() == model.TRANSLUCENT) {
            // REMIND:
            // Probably need to composite anyway so force ARGB
            cmodel = ColorModel.getRGBdefault();
            srcLUT = null;
        }
        else {
            IndexColorModel icm = (IndexColorModel) model;
            numSrcLUT = icm.getMapSize();
            srcLUT = new int[Math.max(numSrcLUT, 256)];
            icm.getRGBs(srcLUT);
            srcLUTtransIndex = icm.getTransparentPixel();
            cmodel = model;
        }
    }
    else {
        if (cmodel == null) {
            cmodel = model;
            srcLUT   = null;
        }
        else if (model instanceof DirectColorModel) {
            // If it is INT_RGB or INT_ARGB, use the model
            DirectColorModel dcm = (DirectColorModel) model;
            if ((dcm.getRedMask() == 0xff0000) &&
                (dcm.getGreenMask() == 0xff00) &&
                (dcm.getBlueMask()  == 0x00ff)) {
                cmodel   = model;
                srcLUT   = null;
            }
        }
    }

    isSameCM = (cmodel == model);
}
 
Example 20
Source File: MinimalGifEncoder.java    From pumpernickel with MIT License 4 votes vote down vote up
@Override
public void writeImage(OutputStream out, BufferedImage image,
		int frameDurationInCentiseconds, IndexColorModel globalColorModel,
		boolean writeLocalColorTable) throws IOException {

	if (frameDurationInCentiseconds >= 0
			|| globalColorModel.getTransparentPixel() != -1) {
		if (frameDurationInCentiseconds < 0)
			frameDurationInCentiseconds = 0;
		GifGraphicControlExtension gce = new GifGraphicControlExtension(
				frameDurationInCentiseconds,
				DisposalMethod.RESTORE_BACKGROUND,
				globalColorModel.getTransparentPixel());
		gce.write(out);
	}
	int localColorSize = 0;
	if (writeLocalColorTable) {
		localColorSize = globalColorModel.getMapSize();
		int k = 2;
		while (localColorSize > k) {
			k *= 2;
		}
		if (k > 256)
			throw new IllegalArgumentException("Illegal number of colors ("
					+ localColorSize + ").  There can only be 256 at most.");
		localColorSize = k;
	}

	Dimension d = new Dimension(image.getWidth(), image.getHeight());

	GifImageDescriptor id = new GifImageDescriptor(0, 0, d.width, d.height,
			false, localColorSize);
	id.write(out);
	if (localColorSize > 0) {
		GifLocalColorTable ct = new GifLocalColorTable(globalColorModel);
		ct.write(out);
	}
	GifImageDataBlock dataBlock = new GifImageDataBlock(image,
			globalColorModel);
	dataBlock.write(out);
}