if (quality > 0 && quality < 1) { imgWriteParams = writer.getDefaultWriteParam(); if (imgWriteParams.canWriteCompressed()) { imgWriteParams.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); imgWriteParams.setCompressionQuality(quality); final ColorModel colorModel = renderedImage.getColorModel();// ColorModel.getRGBdefault(); imgWriteParams.setDestinationType(new ImageTypeSpecifier(colorModel, colorModel.createCompatibleSampleModel(16, 16)));
private ImageTypeSpecifier getRawImageTypeForLayer(final int layerIndex) throws IOException { ImageTypeSpecifier compositeType = getRawImageTypeForCompositeLayer(); PSDLayerInfo layerInfo = metadata.layerInfo.get(layerIndex); // If layer has more channels than composite data, it's normally extra alpha... if (layerInfo.channelInfo.length > compositeType.getNumBands()) { // ...but, it could also be just one of the user masks... int newBandNum = 0; for (PSDChannelInfo channelInfo : layerInfo.channelInfo) { // -2 = user supplied layer mask, -3 real user supplied layer mask (when both a user mask and a vector mask are present) if (channelInfo.channelId >= -1) { newBandNum++; } } // If there really is more channels, then create new imageTypeSpec if (newBandNum > compositeType.getNumBands()) { int[] indices = new int[newBandNum]; for (int i = 0, indicesLength = indices.length; i < indicesLength; i++) { indices[i] = i; } int[] offs = new int[newBandNum]; for (int i = 0, offsLength = offs.length; i < offsLength; i++) { offs[i] = 0; } return ImageTypeSpecifiers.createBanded(compositeType.getColorModel().getColorSpace(), indices, offs, compositeType.getSampleModel().getDataType(), true, false); } } return compositeType; }
while (imageTypes.hasNext()) { ImageTypeSpecifier imageTypeSpecifier = imageTypes.next(); int bufferedImageType = imageTypeSpecifier.getBufferedImageType(); if (bufferedImageType == BufferedImage.TYPE_BYTE_GRAY) { param.setDestinationType(imageTypeSpecifier); break; if (imageTypes.hasNext()) { ImageTypeSpecifier imageTypeSpecifier = imageTypes.next(); img = imageTypeSpecifier.createBufferedImage(width, height); param.setDestination(img); } else { lastException = new IOException("unable to load even a truncated version of the image."); reader.read(0, param); } finally { if (img.getType() != BufferedImage.TYPE_INT_ARGB) { int y = findTruncatedBlackBox(img, width, height); if (y < height) { BufferedImage argbImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics2D g = argbImg.createGraphics(); g.clipRect(0, 0, width, y); g.drawImage(img, 0, 0, null);
@Override public Iterator<ImageTypeSpecifier> getImageTypes(int imageIndex) throws IOException { readIFD(imageIndex); ImageTypeSpecifier rawType = getRawImageType(imageIndex); Set<ImageTypeSpecifier> specs = new LinkedHashSet<>(5); // TODO: Based on raw type, we can probably convert to most RGB types at least, maybe gray etc if (rawType.getColorModel().getColorSpace().getType() == ColorSpace.TYPE_RGB) { if (rawType.getNumBands() == 3 && rawType.getBitsPerBand(0) == 8) { specs.add(ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_3BYTE_BGR)); // specs.add(ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_BGR)); // specs.add(ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB)); } else if (rawType.getNumBands() == 4 && rawType.getBitsPerBand(0) == 8) { specs.add(ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_4BYTE_ABGR)); // specs.add(ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_ARGB)); specs.add(ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_4BYTE_ABGR_PRE)); } } specs.add(rawType); return specs.iterator(); }
@Override public boolean canEncodeImage(final ImageTypeSpecifier type) { // Fast case, it's a known, supported type switch (type.getBufferedImageType()) { case BufferedImage.TYPE_INT_RGB: case BufferedImage.TYPE_INT_ARGB: case BufferedImage.TYPE_INT_ARGB_PRE: case BufferedImage.TYPE_3BYTE_BGR: case BufferedImage.TYPE_4BYTE_ABGR: case BufferedImage.TYPE_4BYTE_ABGR_PRE: case BufferedImage.TYPE_USHORT_555_RGB: case BufferedImage.TYPE_BYTE_GRAY: case BufferedImage.TYPE_BYTE_INDEXED: return true; case BufferedImage.TYPE_BYTE_BINARY: // Could be supported? Uncertain if the format allows < 8 bit/sample for color map entries case BufferedImage.TYPE_INT_BGR: // TODO: Should be supported, just needs to invert band indices/offsets case BufferedImage.TYPE_USHORT_565_RGB: case BufferedImage.TYPE_USHORT_GRAY: return false; default: // Fall through } // Inspect color model etc. ColorSpace colorSpace = type.getColorModel().getColorSpace(); if (!(colorSpace.getType() == ColorSpace.TYPE_RGB || colorSpace.getType() == ColorSpace.TYPE_GRAY)) { return false; } // TODO! return true; }
@Test public void testGetDestinationParamSourceRegionWider() throws IIOException { ImageReadParam param = new ImageReadParam(); param.setSourceRegion(new Rectangle(42, 1)); BufferedImage destination = ImageReaderBase.getDestination(param, TYPES.iterator(), 3, 3); assertEquals(3, destination.getWidth()); assertEquals(1, destination.getHeight()); assertEquals(TYPES.get(0).getBufferedImageType(), destination.getType()); }
param.setDestinationType(reader.getRawImageType(0)); if (param.getDestinationType() == null) { Iterator<ImageTypeSpecifier> types = reader.getImageTypes(0); ImageTypeSpecifier typeSpecifier = types.next(); if (typeSpecifier.getColorModel().getColorSpace().getType() == ColorSpace.TYPE_CMYK) { param.setDestinationType(typeSpecifier); if (type <= 0 || type == original.getType()) { image = original; image = new BufferedImage(original.getWidth(), original.getHeight(), type); Graphics2D graphics = image.createGraphics(); param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); param.setCompressionType(param.getCompressionTypes()[compression]);
ImageTypeSpecifier rawType = getRawImageType(imageIndex); if (rawType.getColorModel() instanceof IndexColorModel && rawType.getBufferedImageType() != image.getType()) { checkReadParamBandSettings(param, 4, image.getSampleModel().getNumBands()); checkReadParamBandSettings(param, rawType.getNumBands(), image.getSampleModel().getNumBands()); final Rectangle source = new Rectangle(); final Rectangle dest = new Rectangle(); computeRegions(param, width, height, image, source, dest); WritableRaster raster = Raster.createPackedRaster(buffer, width, height, resource.depth(), null); if (image.getType() == rawType.getBufferedImageType() && ((IndexColorModel) image.getColorModel()).getMapSize() == 2) { image.setData(raster); BufferedImage temp = new BufferedImage(rawType.getColorModel(), raster, false, null); graphics.drawImage(temp, 0, 0, null); WritableRaster raster = Raster.createPackedRaster(buffer, width, height, resource.depth(), null); if (image.getType() == rawType.getBufferedImageType()) { image.setData(raster); BufferedImage temp = new BufferedImage(rawType.getColorModel(), raster, false, null); graphics.drawImage(temp, 0, 0, null);
ImageTypeSpecifier type = types.next(); int csType = type.getColorModel().getColorSpace().getType(); if (rgbType == null && csType == ColorSpace.TYPE_RGB) { rgbType = type; param.setSourceRegion(new Rectangle(reader.getWidth(0), 8)); // We don't really need to read it all param.setDestinationType(cmykType); BufferedImage imageCMYK = reader.read(0, param); param.setDestinationType(rgbType); BufferedImage imageRGB = reader.read(0, param); assertEquals(ColorSpace.TYPE_CMYK, imageCMYK.getColorModel().getColorSpace().getType()); assertEquals(ColorSpace.TYPE_RGB, imageRGB.getColorModel().getColorSpace().getType()); for (int y = 0; y < imageCMYK.getHeight(); y++) { for (int x = 0; x < imageCMYK.getWidth(); x++) { int cmykAsRGB = imageCMYK.getRGB(x, y);
public BufferedImage readBufferedImage() throws IOException { colorModel = getColorModel(); sampleModel = getSampleModel(); WritableRaster raster = null; BufferedImage image = j2krparam.getDestination(); int x = destinationRegion.x; int y = destinationRegion.y; destinationRegion.setLocation(j2krparam.getDestinationOffset()); if (image == null) { // If the destination type is specified, use the color model of it. ImageTypeSpecifier type = j2krparam.getDestinationType(); if (type != null) colorModel = type.getColorModel(); raster = Raster.createWritableRaster( sampleModel.createCompatibleSampleModel(destinationRegion.x + destinationRegion.width, destinationRegion.y + destinationRegion.height), new Point(0, 0)); image = new BufferedImage(colorModel, raster, colorModel.isAlphaPremultiplied(), new Hashtable()); } else raster = image.getWritableTile(0, 0); destImage = image; readSubsampledRaster(raster); destinationRegion.setLocation(x, y); destImage = null; return image; }
@Test public void testGetDestinationParamGoodDestinationType() throws IIOException { ImageReadParam param = new ImageReadParam(); param.setDestinationType(ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_ARGB)); BufferedImage destination = ImageReaderBase.getDestination(param, TYPES.iterator(), 6, 7); assertEquals(BufferedImage.TYPE_INT_ARGB, destination.getType()); assertEquals(6, destination.getWidth()); assertEquals(7, destination.getHeight()); }
Rectangle srcRegion = new Rectangle(); Rectangle destRegion = new Rectangle(); computeRegions(param, width, height, destination, srcRegion, destRegion); WritableRaster destRaster = clipToRect(destination.getRaster(), destRegion, param != null ? param.getDestinationBands() : null); checkReadParamBandSettings(param, rawType.getNumBands(), destRaster.getNumBands()); WritableRaster rowRaster = rawType.createBufferedImage(width, 1).getRaster(); param != null ? param.getSourceBands() : null, param != null ? param.getSourceXSubsampling() : 1); int xSub = param != null ? param.getSourceXSubsampling() : 1;
@Test public void testFilterRotateBICustom() { BufferedImageOp jreOp = new java.awt.image.AffineTransformOp(AffineTransform.getQuadrantRotateInstance(1, Math.min(width, height) / 2, Math.min(width, height) / 2), null); BufferedImageOp tmOp = new com.twelvemonkeys.image.AffineTransformOp(AffineTransform.getQuadrantRotateInstance(1, Math.min(width, height) / 2, Math.min(width, height) / 2), null); for (ImageTypeSpecifier spec : SPECS) { BufferedImage image = spec.createBufferedImage(width, height); BufferedImage tmResult = tmOp.filter(image, null); assertNotNull("No result!", tmResult); BufferedImage jreResult = null; try { jreResult = jreOp.filter(image, null); } catch (ImagingOpException ignore) { // We expect this to fail for certain cases, that's why we crated the class in the first place } if (jreResult != null) { assertEquals("Bad type", jreResult.getType(), tmResult.getType()); assertEquals("Incorrect color model", jreResult.getColorModel(), tmResult.getColorModel()); assertEquals("Incorrect width", jreResult.getWidth(), tmResult.getWidth()); assertEquals("Incorrect height", jreResult.getHeight(), tmResult.getHeight()); } else { assertEquals("Bad type", spec.getBufferedImageType(), tmResult.getType()); assertEquals("Incorrect color model", spec.getColorModel(), tmResult.getColorModel()); assertEquals("Incorrect width", height, tmResult.getWidth()); assertEquals("Incorrect height", width, tmResult.getHeight()); } } }
new BufferedImage( (int) (chart.getWidth() * scaleFactor), (int) (chart.getHeight() * scaleFactor), BufferedImage.TYPE_INT_RGB); Graphics2D graphics2D = bufferedImage.createGraphics(); ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB); IIOMetadata metadata = writer.getDefaultImageMetadata(typeSpecifier, iwp); if (metadata.isReadOnly() || !metadata.isStandardMetadataFormatSupported()) {
checkReadParamBandSettings(param, rawType.getNumBands(), destination.getSampleModel().getNumBands()); final Rectangle srcRegion = new Rectangle(); final Rectangle dstRegion = new Rectangle(); computeRegions(param, width, height, destination, srcRegion, dstRegion); int xSub = param != null ? param.getSourceXSubsampling() : 1; int ySub = param != null ? param.getSourceYSubsampling() : 1; WritableRaster destRaster = clipToRect(destination.getRaster(), dstRegion, param != null ? param.getDestinationBands() : null); final int predictor = getValueAsIntWithDefault(TIFF.TAG_PREDICTOR, 1); final int planarConfiguration = getValueAsIntWithDefault(TIFF.TAG_PLANAR_CONFIGURATION, TIFFBaseline.PLANARCONFIG_CHUNKY); final int numBands = planarConfiguration == TIFFExtension.PLANARCONFIG_PLANAR ? 1 : rawType.getNumBands(); WritableRaster rowRaster = rawType.createBufferedImage(stripTileWidth, 1).getRaster(); Rectangle clip = new Rectangle(srcRegion); int srcRow = 0; Boolean needsCSConversion = null; int bands = planarConfiguration == TIFFExtension.PLANARCONFIG_PLANAR ? rawType.getNumBands() : 1; int bitsPerSample = getBitsPerSample(); boolean needsBitPadding = bitsPerSample > 16 && bitsPerSample % 16 != 0 || bitsPerSample > 8 && bitsPerSample % 8 != 0 || bitsPerSample == 6;
Rectangle srcRegion = new Rectangle(); Rectangle destRegion = new Rectangle(); computeRegions(param, width, height, destination, srcRegion, destRegion); WritableRaster destRaster = clipToRect(destination.getRaster(), destRegion, param != null ? param.getDestinationBands() : null); checkReadParamBandSettings(param, rawType.getNumBands(), destRaster.getNumBands()); WritableRaster rowRaster = rawType.createBufferedImage(width, 1).getRaster(); param != null ? param.getSourceBands() : null, param != null ? param.getSourceXSubsampling() : 1); if (!destination.getColorModel().isCompatibleRaster(rowRaster)) { colorConvert = new ColorConvertOp(rawType.getColorModel().getColorSpace(), destination.getColorModel().getColorSpace(), null);
SampleModel sampleModel = imageType.getSampleModel(); Rectangle sourceBounds = new Rectangle(sampleModel.getWidth(), sampleModel.getHeight()); Dimension destSize = new Dimension(); computeRegions(sourceBounds, destSize, param); if (param != null && param.canWriteProgressive() && param.getProgressiveMode() == ImageWriteParam.MODE_DISABLED) { imageMetadata.interlaceFlag = false; } else { ColorModel colorModel = imageType.getColorModel();
public IIOMetadata getDefaultImageMetadata(ImageTypeSpecifier imageType,ImageWriteParam param) { final GDALWritableCommonIIOImageMetadata imageMetadata = new GDALWritableCommonIIOImageMetadata(); SampleModel sm = imageType.getSampleModel(); final int sourceWidth = sm.getWidth(); final int sourceHeight = sm.getHeight(); final int sourceMinX = 0; final int sourceMinY = 0; final int dataType = GDALUtilities.retrieveGDALDataBufferType(sm.getDataType()); final int nBands = sm.getNumBands(); // // // // Setting regions and sizes and retrieving parameters // // // Rectangle imageBounds = new Rectangle(sourceMinX, sourceMinY, sourceWidth, sourceHeight); Dimension destSize = new Dimension(); computeRegions(imageBounds, destSize, param); imageMetadata.setBasicInfo(destSize.width, destSize.height, nBands); // TODO:provides additional settings return imageMetadata; }
BufferedImage dest = param.getDestination(); int bufferedImageType = specifier.getBufferedImageType(); if (bufferedImageType != 0 && bufferedImageType == dest.getType()) { if (specifier.getSampleModel().getTransferType() == dest.getSampleModel().getTransferType() && specifier.getNumBands() <= dest.getSampleModel().getNumBands()) { found = true; break; imageType = param.getDestinationType(); ImageTypeSpecifier type = types.next(); if (type.equals(imageType)) { foundIt = true; break; Rectangle srcRegion = new Rectangle(0, 0, 0, 0); Rectangle destRegion = new Rectangle(0, 0, 0, 0); computeRegions(param, width, height, null, srcRegion, destRegion); return imageType.createBufferedImage(destWidth, destHeight);
@Test public void testGray() { ImageTypeSpecifier spec = UInt32ImageTypeSpecifier.createInterleaved(GRAY, new int [] {0}, false, false); assertEquals(1, spec.getNumBands()); assertEquals(1, spec.getNumComponents()); assertEquals(32, spec.getBitsPerBand(0)); assertThat(spec.getColorModel(), is(ComponentColorModel.class)); assertFalse(spec.getColorModel().hasAlpha()); assertFalse(spec.getColorModel().isAlphaPremultiplied()); assertEquals(1, spec.getColorModel().getNumComponents()); assertEquals(1, spec.getColorModel().getNumColorComponents()); assertThat(spec.getSampleModel(), is(PixelInterleavedSampleModel.class)); assertEquals(1, spec.getSampleModel().getNumBands()); assertEquals(1, spec.getSampleModel().getNumDataElements()); }