ICOEntry(final int width, final int height, final ColorModel colorModel, int size, final int offset) { this.width = width; this.height = height; this.colorCount = colorModel instanceof IndexColorModel ? ((IndexColorModel) colorModel).getMapSize() : 0; this.planes = 1; this.bitCount = colorModel.getPixelSize(); this.size = size; this.offset = offset; } }
/** * Sets the ImageIO parameter compression type based on the given image. * @param image buffered image used to decide compression type * @param param ImageIO write parameter to update */ public static void setCompressionType(ImageWriteParam param, BufferedImage image) { // avoid error: first compression type is RLE, not optimal and incorrect for color images // TODO expose this choice to the user? if (image.getType() == BufferedImage.TYPE_BYTE_BINARY && image.getColorModel().getPixelSize() == 1) { param.setCompressionType("CCITT T.6"); } else { param.setCompressionType("LZW"); } }
private static PDImageXObject createFromGrayImage(BufferedImage image, PDDocument document) throws IOException { int height = image.getHeight(); int width = image.getWidth(); int[] rgbLineBuffer = new int[width]; int bpc = image.getColorModel().getPixelSize(); ByteArrayOutputStream baos = new ByteArrayOutputStream(((width*bpc/8)+(width*bpc%8 != 0 ? 1:0))*height); try (MemoryCacheImageOutputStream mcios = new MemoryCacheImageOutputStream(baos)) { for (int y = 0; y < height; ++y) { for (int pixel : image.getRGB(0, y, width, 1, rgbLineBuffer, 0, width)) { mcios.writeBits(pixel & 0xFF, bpc); } int bitOffset = mcios.getBitOffset(); if (bitOffset != 0) { mcios.writeBits(0, 8 - bitOffset); } } mcios.flush(); } return prepareImageXObject(document, baos.toByteArray(), image.getWidth(), image.getHeight(), bpc, PDDeviceGray.INSTANCE); }
/** * A wrapper for {@link #setImage(int, int, ByteBuffer, Rectangle, int)}. * * @param image a rendered image * @param rect region of interest * @throws java.io.IOException */ protected void setImage(RenderedImage image, Rectangle rect) throws IOException { ByteBuffer buff = ImageIOHelper.getImageByteBuffer(image); int bpp; DataBuffer dbuff = image.getData(new Rectangle(1,1)).getDataBuffer(); if (dbuff instanceof DataBufferByte) { bpp = image.getColorModel().getPixelSize(); } else { bpp = 8; // BufferedImage.TYPE_BYTE_GRAY image } setImage(image.getWidth(), image.getHeight(), buff, rect, bpp); }
/** * A wrapper for {@link #setImage(int, int, ByteBuffer, Rectangle, int)}. * * @param image a rendered image * @param rect region of interest * @throws java.io.IOException */ protected void setImage(RenderedImage image, Rectangle rect) throws IOException { ByteBuffer buff = ImageIOHelper.getImageByteBuffer(image); int bpp; DataBuffer dbuff = image.getData(new Rectangle(1,1)).getDataBuffer(); if (dbuff instanceof DataBufferByte) { bpp = image.getColorModel().getPixelSize(); } else { bpp = 8; // BufferedImage.TYPE_BYTE_GRAY image } setImage(image.getWidth(), image.getHeight(), buff, rect, bpp); }
private BufferedImage getStartImage(BufferedImage copyFrom) { Dimension dim = getStartDimension(); Hashtable<String, Object> properties = null; if (copyFrom.getPropertyNames() != null) { properties = new Hashtable<String, Object>(); for (String name : copyFrom.getPropertyNames()) { properties.put(name, copyFrom.getProperty(name)); } } SampleModel sm = copyFrom.getSampleModel() .createCompatibleSampleModel((int) dim.getWidth(), (int) dim.getHeight()); WritableRaster raster = Raster.createWritableRaster(sm, null); ColorModel colorModel = copyFrom.getColorModel(); boolean alphaPremultiplied = copyFrom.isAlphaPremultiplied(); DataBuffer dataBuffer = createDataBufferFilledWithNoDataValues(raster, colorModel.getPixelSize()); raster = Raster.createWritableRaster(sm, dataBuffer, null); BufferedImage image = new BufferedImage(colorModel, raster, alphaPremultiplied, properties); if (levelInfo.getNoDataValue() == null) { Graphics2D g2D = (Graphics2D) image.getGraphics(); Color save = g2D.getColor(); g2D.setColor(backgroundColor); g2D.fillRect(0, 0, image.getWidth(), image.getHeight()); g2D.setColor(save); } return image; }
static TGAHeader from(final RenderedImage image, final ImageWriteParam param) { notNull(image, "image"); ColorModel colorModel = image.getColorModel(); IndexColorModel colorMap = colorModel instanceof IndexColorModel ? (IndexColorModel) colorModel : null; TGAHeader header = new TGAHeader(); header.colorMapType = colorMap != null ? 1 : 0; header.imageType = getImageType(colorModel, param); header.colorMapStart = 0; header.colorMapSize = colorMap != null ? colorMap.getMapSize() : 0; header.colorMapDepth = colorMap != null ? (colorMap.hasAlpha() ? 32 : 24) : 0; header.x = 0; header.y = 0; header.width = image.getWidth(); // TODO: Param source region/subsampling might affect this header.height = image.getHeight(); // // TODO: Param source region/subsampling might affect this header.pixelDepth = colorModel.getPixelSize() == 15 ? 16 : colorModel.getPixelSize(); header.origin = TGA.ORIGIN_UPPER_LEFT; // TODO: Allow parameter to control this? header.attributeBits = colorModel.hasAlpha() ? 8 : 0; // TODO: FixMe header.identification = null; header.colorMap = colorMap; return header; }
throws IOException if (image.getType() != BufferedImage.TYPE_BYTE_BINARY && image.getColorModel().getPixelSize() != 1)
private static void assertSameType(BufferedImage expected, BufferedImage actual) { if (expected.getType() != actual.getType()) { if (expected.getType() == BufferedImage.TYPE_INT_RGB || expected.getType() == BufferedImage.TYPE_INT_BGR) { assertEquals(BufferedImage.TYPE_3BYTE_BGR, actual.getType()); } else if (expected.getType() == BufferedImage.TYPE_INT_ARGB || expected.getType() == BufferedImage.TYPE_INT_ARGB_PRE) { assertEquals(BufferedImage.TYPE_4BYTE_ABGR, actual.getType()); } else if (expected.getType() == BufferedImage.TYPE_BYTE_INDEXED && expected.getColorModel().getPixelSize() <= 16) { assertEquals(BufferedImage.TYPE_BYTE_BINARY, actual.getType()); } // NOTE: Actually, TYPE_GRAY may be converted to TYPE_BYTE_INDEXED with linear gray color-map, // without being a problem (just a waste of time and space). } } }
final int channels = (model.getPixelSize() + 7) / 8; final int planesPerChannel = channels == 1 ? model.getPixelSize() : 8; int[] pixels = new int[8 * planeWidth];
if (image.getColorModel().getPixelSize() != bits)
private int getPhotometricInterpretation(final ColorModel colorModel, int compression) { if (colorModel.getPixelSize() == 1) { if (colorModel instanceof IndexColorModel) { if (colorModel.getRGB(0) == 0xFFFFFFFF && colorModel.getRGB(1) == 0xFF000000) { return TIFFBaseline.PHOTOMETRIC_WHITE_IS_ZERO; } else if (colorModel.getRGB(0) != 0xFF000000 || colorModel.getRGB(1) != 0xFFFFFFFF) { return TIFFBaseline.PHOTOMETRIC_PALETTE; } // Else, fall through to default, BLACK_IS_ZERO } return TIFFBaseline.PHOTOMETRIC_BLACK_IS_ZERO; } else if (colorModel instanceof IndexColorModel) { return TIFFBaseline.PHOTOMETRIC_PALETTE; } switch (colorModel.getColorSpace().getType()) { case ColorSpace.TYPE_GRAY: return TIFFBaseline.PHOTOMETRIC_BLACK_IS_ZERO; case ColorSpace.TYPE_RGB: return compression == TIFFExtension.COMPRESSION_JPEG ? TIFFExtension.PHOTOMETRIC_YCBCR : TIFFBaseline.PHOTOMETRIC_RGB; case ColorSpace.TYPE_CMYK: return TIFFExtension.PHOTOMETRIC_SEPARATED; } throw new IllegalArgumentException("Can't determine PhotometricInterpretation for color model: " + colorModel); }
throws IOException if ((image.getType() == BufferedImage.TYPE_BYTE_GRAY && image.getColorModel().getPixelSize() <= 8) || (image.getType() == BufferedImage.TYPE_BYTE_BINARY && image.getColorModel().getPixelSize() == 1))
writeDIBHeader(infoHeaderSize, img.getWidth(), img.getHeight(), isTopDown, img.getColorModel().getPixelSize(), compression);
false, img.getColorModel().getPixelSize(), DIB.COMPRESSION_RGB); writeUncompressed(false, (BufferedImage) img, img.getWidth(), img.getHeight());
header = new BMHDChunk(pImage.getWidth(), pImage.getHeight(), cm.getPixelSize(), BMHDChunk.MASK_NONE, compression, 0);
image.getColorModel().getPixelSize() == 1)
cm = image.getColorModel(); if (!(cm instanceof IndexColorModel) || cm.getPixelSize() > 8) rescaleToBytes();
@Test public void testCreatePacked8() { // "BYTE 332 RGB" assertEquals( createPacked(sRGB, 0xe0, 0x1c, 0x03, 0x0, DataBuffer.TYPE_BYTE, false), ImageTypeSpecifiers.createPacked(sRGB, 0xe0, 0x1c, 0x3, 0x0, DataBuffer.TYPE_BYTE, false) ); // "BYTE 2222 ARGB" assertEquals( createPacked(sRGB, 0xc0, 0x30, 0x0c, 0x03, DataBuffer.TYPE_BYTE, false), ImageTypeSpecifiers.createPacked(sRGB, 0xc0, 0x30, 0x0c, 0x03, DataBuffer.TYPE_BYTE, false) ); // "BYTE 2222 ARGB PRE" assertEquals( createPacked(sRGB, 0xc0, 0x30, 0x0c, 0x03, DataBuffer.TYPE_BYTE, true), ImageTypeSpecifiers.createPacked(sRGB, 0xc0, 0x30, 0x0c, 0x03, DataBuffer.TYPE_BYTE, true) ); // Extra: Make sure color models bits is actually 8 (ImageTypeSpecifiers equivalent returns 32) assertEquals(8, ImageTypeSpecifiers.createPacked(sRGB, 0xc0, 0x30, 0x0c, 0x03, DataBuffer.TYPE_BYTE, false).getColorModel().getPixelSize()); }
@Test public void testCreatePacked16() { // TYPE_USHORT_555_RGB assertEquals( createPacked(sRGB, DCM_555_RED_MASK, DCM_555_GRN_MASK, DCM_555_BLU_MASK, 0, DataBuffer.TYPE_USHORT, false), ImageTypeSpecifiers.createPacked(sRGB, DCM_555_RED_MASK, DCM_555_GRN_MASK, DCM_555_BLU_MASK, 0, DataBuffer.TYPE_USHORT, false) ); // "SHORT 555 RGB" (impossible, only BYTE, USHORT, INT supported) // TYPE_USHORT_565_RGB assertEquals( createPacked(sRGB, DCM_565_RED_MASK, DCM_565_GRN_MASK, DCM_565_BLU_MASK, 0, DataBuffer.TYPE_USHORT, false), ImageTypeSpecifiers.createPacked(sRGB, DCM_565_RED_MASK, DCM_565_GRN_MASK, DCM_565_BLU_MASK, 0, DataBuffer.TYPE_USHORT, false) ); // "USHORT 4444 ARGB" assertEquals( createPacked(sRGB, 0xf00, 0xf0, 0xf, 0xf000, DataBuffer.TYPE_USHORT, false), ImageTypeSpecifiers.createPacked(sRGB, 0xf00, 0xf0, 0xf, 0xf000, DataBuffer.TYPE_USHORT, false) ); // "USHORT 4444 ARGB PRE" assertEquals( createPacked(sRGB, 0xf00, 0xf0, 0xf, 0xf000, DataBuffer.TYPE_USHORT, true), ImageTypeSpecifiers.createPacked(sRGB, 0xf00, 0xf0, 0xf, 0xf000, DataBuffer.TYPE_USHORT, true) ); // Extra: Make sure color models bits is actually 16 (ImageTypeSpecifier equivalent returns 32) assertEquals(16, ImageTypeSpecifiers.createPacked(sRGB, DCM_565_RED_MASK, DCM_565_GRN_MASK, DCM_565_BLU_MASK, 0, DataBuffer.TYPE_USHORT, false).getColorModel().getPixelSize()); }