How to extract a subimage using PixelReader and JavaFX? - java

I have a .png image, and I want to extract one part of that image using the PixelReader class, and rebuild it as an image :
Image image = new Image("file:ressources/spritesheets/Zelda_Overworld.png");
byte[] buffer = new byte[1024];
PixelReader pr = image.getPixelReader();
pr.getPixels(0, 0, 16, 16, PixelFormat.getByteBgraInstance(), buffer, 0, 64);
Image tile = new Image(new ByteArrayInputStream(buffer));
I can display image and buffer seems to contain values, but I can't display tile, tile.getPixelReader() returns null, tile.getWidth() and tile.getHeight() return 0.0.
Do you know what I am doing wrong?
Paul

Let WritableImage do this for you:
Image image = new Image("file:ressources/spritesheets/Zelda_Overworld.png");
Image tile = new WritableImage(image.getPixelReader(), x, y, width, height);
Depending on the use of tile doing this may not be necessary at all. ImageView has a viewport property that allows you to choose the part of the image to display and GraphicsContext provides an overloaded version of the drawImage method to draw a part of the image to the Canvas.

Related

How to make a TIFF transparent in Java using JAI?

I'm trying to write a TIFF from a BufferedImage using Java Advanced Imaging (JAI), and am unsure of how to make it transparent. The following method works for making PNGs and GIFs transparent:
private static BufferedImage makeTransparent(BufferedImage image, int x, int y) {
ColorModel cm = image.getColorModel();
if (!(cm instanceof IndexColorModel)) {
return image;
}
IndexColorModel icm = (IndexColorModel) cm;
WritableRaster raster = image.getRaster();
int pixel = raster.getSample(x, y, 0);
// pixel is offset in ICM's palette
int size = icm.getMapSize();
byte[] reds = new byte[size];
byte[] greens = new byte[size];
byte[] blues = new byte[size];
icm.getReds(reds);
icm.getGreens(greens);
icm.getBlues(blues);
IndexColorModel icm2 = new IndexColorModel(8, size, reds, greens, blues, pixel);
return new BufferedImage(icm2, raster, image.isAlphaPremultiplied(), null);
}
But when writing a TIFF, the background is always white. Below is my code used for writing TIFF:
BufferedImage destination = new BufferedImage(sourceImage.getWidth(), sourceImage.getHeight(), BufferedImage.TYPE_BYTE_INDEXED);
Graphics imageGraphics = destination.getGraphics();
imageGraphics.drawImage(sourceImage, 0, 0, backgroundColor, null);
if (isTransparent) {
destination = makeTransparent(destination, 0, 0);
}
destination.createGraphics().drawImage(sourceImage, 0, 0, null);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
TIFFImageWriter writer = new TIFFImageWriter(new TIFFImageWriterSpi());
writer.setOutput(ios);
writer.write(destination);
I also do some metadata manipulation later as I'm actually dealing with GeoTIFF. But still the images are white at this point. While debugging, I can view the BufferedImage and it is transparent, but when i write the image, the file has a white background. Do I need to do something specific with TiffImageWriteParam? Thanks for any help you can provide.
TIFF has no option of storing transparency info (alpha channel) in the palette (as in your IndexedColorModel). The palette only supports RGB triplets. Thus, the fact that you set a color index to transparent is lost when you write the image to a TIFF.
If you need a transparent TIFF, your options are:
Use normal RGBA instead of indexed color (RGB, 4 samples/pixel, unassociated alpha). Just use BufferedImage.TYPE_INT_ARGB or TYPE_4BYTE_ABGR, probably. This will make the output file bigger, but is easy to implement, and should be more compatible. Supported by almost all TIFF software.
Save a separate transparency mask (a 1 bit image with photometric interpretation set to 4) with the palette image. Not sure if it supported by much software, some may display the mask as a separate b/w image. Not sure how this can be achieved in JAI/ImageIO, might require writing a sequence and setting some extra metadata.
Store a custom field containing the transparent index. Will not be supported by anything but your own software, but the file will still be compatible and displayed with the white (solid) background in other software. You should be able to set this using the TIFF metadata.

Apache POI-HSSF distorts image size when adding picture into Excel cell

I am adding a picture into a cell using Apache POI-HSSF. The image is 120x100 but no matter what I do and how I resize it, the Excel spreadsheet always shows it spanning multiple rows and distorts it to a much bigger height than width.
How do I keep the original size?
My code:
InputStream is = new FileInputStream(getImageURL());
byte[] bytes = IOUtils.toByteArray(is);
int pictureIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG);
is.close();
//add a picture shape
CreationHelper helper = wb.getCreationHelper();
ClientAnchor anchor = helper.createClientAnchor();
// Create the drawing patriarch. This is the top level container for all shapes.
Drawing drawing = sheet1.createDrawingPatriarch();
//set top-left corner of the picture,
//subsequent call of Picture#resize() will operate relative to it
anchor.setAnchorType(0);
anchor.setCol1(1);
anchor.setRow1(1);
Picture pict = drawing.createPicture(anchor, pictureIdx);
//auto-size picture relative to its top-left corner
pict.resize();
I've tried all dx/dy coordinates and Col/Row. The position doesn't matter, the problem it stretches the image horizontally.
I had been facing the similar issue where the image I added was getting distorted. I tried pict.resize() and sheet.autoSizeColumn() but it didn't work. Finally I found the below URL:-
https://svn.apache.org/repos/asf/poi/trunk/src/examples/src/org/apache/poi/examples/ss/AddDimensionedImage.java
I added the above class into my code and used it's method to add the image into excel. I was able to add the image with little distortion. Hope this helps to you also. I wrote below code:-
BufferedImage imageIO = ImageIO.read(new URL(image));
int height= imageIO.getHeight();
int width=imageIO.getWidth();
int relativeHeight=(int)(((double)height/width)*28.5);
new AddDimensionedImage().addImageToSheet(2, sheet.getPhysicalNumberOfRows()-1 , sheet, sheet.createDrawingPatriarch(),new URL(image), 30, relativeHeight, AddDimensionedImage.EXPAND_ROW);
As far as i understood from documentation of Apache POI, it is because of pict.resize();
,as it says here that if the default font size for the workbook was changed, the picture might get stretched vertically or horizontally.
I've the same problem and my solution was copy this class in my project
AddDimensionedImage and then used this method.
protected void addImageInCell(Sheet sheet, URL url, Drawing<?> drawing, int colNumber, int rowNumber) {
BufferedImage imageIO = ImageIO.read(url);
int height = imageIO.getHeight();
int width = imageIO.getWidth();
int relativeHeight = (int) (((double) height / width) * 28.5);
new AddDimensionedImage().addImageToSheet(colNumber, rowNumber, sheet, drawing, url, 30, relativeHeight,
AddDimensionedImage.EXPAND_ROW_AND_COLUMN);
}
you can call this method with follow line:
URL url = new URL("https://blog.expedia.mx/por-que-los-viajeros-internacionales-visitan-mexico/");
addImageInCell(sheet, url, sheet.createDrawingPatriarch(), 0, 0);
I created another empty image with the width taken from sheet.getColumnWidthInPixels, draw the necessary image over it and used AddDimensionedImage.EXPAND_ROW_AND_COLUMN to fit it exactly into the cell:
HSSFRow imageRow = sheet.createRow(row);
BufferedImage image = ImageIO.read(Paths.get(pathToImage).toUri().toURL());
AddDimensionedImage addImage = new AddDimensionedImage();
float columnWidth = sheet.getColumnWidthInPixels(0);
BufferedImage off_Image = new BufferedImage((int) columnWidth, actualImageHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = off_Image.createGraphics();
g2.setColor(new Color(192,192,192));
g2.fillRect(0, 0, off_Image.getWidth(), off_Image.getHeight());
g2.drawImage(image, 0, 0, null);
g2.dispose();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(off_Image, "jpg", baos);
byte[] bytes = baos.toByteArray();
double reqImageWidthMM = ((double) off_Image.getWidth()) / ConvertImageUnits.PIXELS_PER_MILLIMETRES;
double reqImageHeightMM = ((double) off_Image.getHeight()) / ConvertImageUnits.PIXELS_PER_MILLIMETRES;
addImage.addImageToSheet("A1", sheet, bytes, reqImageWidthMM, reqImageHeightMM, AddDimensionedImage.EXPAND_ROW_AND_COLUMN);
I had to copy AddDimensionedImage.java to my classpath and overload addImageToSheet to accept byte array.
Before that I tried other options mentioned here but it didn't help. Hope my answer will be useful to someone.

How to control the pixel size of the color model of an ErrorDiffusionDescriptor?

I am trying to convert a direct color model image to a bitonal indexed image (1 bit per pixel) and save the indexed image as a BMP.
As stated on the Java Advanced Imaging API Home Page:
The bit depth of the encoded output is determined by that of the source image.
From looking through the source code of BMPImageWriter, the mechanism of this is the return value of ColorModel#getPixelSize().
Using a scaled-down copy of an image from Wikimedia Commons, I first perform color quantization to get a color lookup table and then error diffusion to apply Floyd–Steinberg dithering:
PlanarImage surrogateImage = PlanarImage.wrapRenderedImage(image);
PlanarImage op = ColorQuantizerDescriptor.create(surrogateImage, ColorQuantizerDescriptor.OCTTREE, 2, null, null, null, null, null);
LookupTableJAI lut = (LookupTableJAI)op.getProperty("LUT");
IndexColorModel cm = new IndexColorModel(1, lut.getByteData()[0].length, lut.getByteData()[0], lut.getByteData()[1], lut.getByteData()[2]);
op = ErrorDiffusionDescriptor.create(surrogateImage, lut, KernelJAI.ERROR_FILTER_FLOYD_STEINBERG, null);
image = op.getAsBufferedImage();
The problem is, image.getColorModel().getPixelSize() returns 8, so the image is saved as an 8bpp bitmap:
The size of this image is 167 KiB.
I saw somewhere that one way of passing a color model to error diffusion is to set a JAI.KEY_IMAGE_LAYOUT rendering hint:
ImageLayout layout = new ImageLayout();
layout.setTileWidth(image.getWidth());
layout.setTileHeight(image.getHeight());
layout.setColorModel(cm);
layout.setSampleModel(op.getSampleModel());
RenderingHints rh = new RenderingHints(JAI.KEY_IMAGE_LAYOUT, layout);
op = ErrorDiffusionDescriptor.create(surrogateImage, lut, KernelJAI.ERROR_FILTER_FLOYD_STEINBERG, rh);
image.getColorModel().getPixelSize() now returns 1, but the resulting image is altered significantly:
However, the size of this image is 21 KiB, about what it is when I use MS Paint to convert the sample image to a monochrome bitmap. So, it looks like JAI's BMPImageWriter is using the correct encoding, but if you look closely at the second image, adjacent columns of pixels are eight pixels apart. In fact, you can kind of see the first image, only each column of pixels from the first image is expanded to 8 columns of pixels.
Is this a bug in JAI? Is there something that I can do to collapse these 8-wide columns of pixels to single-column pixels?
This should work with a 24 BPP png:
String filename = "jEEDL.png";
PlanarImage image = PlanarImage.wrapRenderedImage(JAI.create("fileload", filename));
LookupTableJAI lut = new LookupTableJAI(new byte[][] {{(byte)0x00, (byte)0xff}, {(byte)0x00, (byte)0xff}, {(byte)0x00, (byte)0xff}});
ImageLayout layout = new ImageLayout();
byte[] map = new byte[] {(byte)0x00, (byte)0xff};
ColorModel cm = new IndexColorModel(1, 2, map, map, map);
layout.setColorModel(cm);
SampleModel sm = new MultiPixelPackedSampleModel(DataBuffer.TYPE_BYTE,
image.getWidth(),
image.getHeight(),
1);
layout.setSampleModel(sm);
RenderingHints hints = new RenderingHints(JAI.KEY_IMAGE_LAYOUT, layout);
PlanarImage op = ErrorDiffusionDescriptor.create(image, lut, KernelJAI.ERROR_FILTER_FLOYD_STEINBERG, hints);
BufferedImage dst = op.getAsBufferedImage();
JAI.create("filestore", dst, "jEEDL.bmp", "BMP");

Drawing two overlayed images

I'm trying to draw 2 images, one on top of the other. The 1'st image is an arrow (that should appear like a header in the final image). The 1'st image (arrow) is 32x32 px while the 2'nd is 24x24.
Ideally I would like to draw the 2'nd image on top of the 1'st, starting from the right-bottom corner of the 1'st image.
Currently I'm using such code
// load source images
BufferedImage baseImage = ImageIO.read(new File(baseImg.getFileLocation()));
BufferedImage backgroundImage = ImageIO.read(new File(backgroundImg.getFileLocation()));
// create the new image, canvas size is the max. of both image sizes
int w = Math.max(baseImage.getWidth(), backgroundImage.getWidth());
int h = Math.max(baseImage.getHeight(), backgroundImage.getHeight());
BufferedImage combined = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
// paint both images, preserving the alpha channels
Graphics g = combined.getGraphics();
g.drawImage(baseImage, 0, 0, null);
g.drawImage(backgroundImage, 0, 0, null);
int index = baseImg.getFileLocation().lastIndexOf(".png");
String newFileName = baseImg.getFileLocation().substring(0, index);
// Save as new image
ImageIO.write(combined, "PNG", new File(newFileName + "_combined.png"));
but this won't quite work for me because the end result is a 32x32 image with the 2nd image being drawn only.
Any help is appreciated.
Thanks !
It looks like the issue here is you are drawing the 32x32 background image last, meaning it will be printed on top of the other image making it seem as if the 24x24 image was never drawn at all.
If you swap these two lines around, you should see both images. From:
g.drawImage(baseImage, 0, 0, null);
g.drawImage(backgroundImage, 0, 0, null);
to:
g.drawImage(backgroundImage, 0, 0, null);
g.drawImage(baseImage, 0, 0, null);
However this will draw the 24x24 image in the top-left corner, and you said you'd like it in the bottom-right. This can be done with some basic subtraction:
g.drawImage(baseImage, w - baseImage.getWidth(), h - baseImage.getHeight(), null);

How can I overlay images over one another in Java?

So I have been posting all over and have yet to get a solid answer:
I have created an image resizing class, with a crop method. The cropping works great. The issue that I am having is the background color that I specify in the drawImage function of Graphics is not working correctly. It defaults to black as the background regardless of what I supply (in this case Color.WHITE).
Also, the overlaying image or top most image (comes from a file) is being inverted (I think it is) or otherwise discolored. Just so you can conceptualize this a little bit better, I am taking a jpeg and overlaying it on top of a new BufferedImage, the new buffered image's background is not being set. Here is the code below that I am working with:
public void Crop(int Height, int Width, int SourceX, int SourceY) throws Exception {
//output height and width
int OutputWidth = this.OutputImage.getWidth();
int OutputHeight = this.OutputImage.getHeight();
//create output streams
ByteArrayOutputStream MyByteArrayOutputStream = new ByteArrayOutputStream();
MemoryCacheImageOutputStream MyMemoryCacheImageOutputStream = new MemoryCacheImageOutputStream(MyByteArrayOutputStream);
//Create a new BufferedImage
BufferedImage NewImage = new BufferedImage(Width, Height, BufferedImage.TYPE_INT_RGB);
Graphics MyGraphics = NewImage.createGraphics();
MyGraphics.drawImage(this.OutputImage, -SourceX, -SourceY, OutputWidth, OutputHeight, Color.WHITE, null);
// Get Writer and set compression
Iterator MyIterator = ImageIO.getImageWritersByFormatName("png");
if (MyIterator.hasNext()) {
//get image writer
ImageWriter MyImageWriter = (ImageWriter)MyIterator.next();
//get params
ImageWriteParam MyImageWriteParam = MyImageWriter.getDefaultWriteParam();
//set outputstream
MyImageWriter.setOutput(MyMemoryCacheImageOutputStream);
//create new ioimage
IIOImage MyIIOImage = new IIOImage(NewImage, null, null);
//write new image
MyImageWriter.write(null, MyIIOImage, MyImageWriteParam);
}
//convert output stream back to inputstream
ByteArrayInputStream MyByteArrayInputStream = new ByteArrayInputStream(MyByteArrayOutputStream.toByteArray());
MemoryCacheImageInputStream MyMemoryCacheImageInputStream = new MemoryCacheImageInputStream(MyByteArrayInputStream);
//resassign as a buffered image
this.OutputImage = ImageIO.read(MyMemoryCacheImageInputStream);
}
Can you isolate whether it's the Graphics methods or the ImageIO methods that are mangling your image? It looks like you could test this by short-circuiting the entire ImageIO process and simply assigning
this.OutputImage = NewImage;
For that matter, I assume there's something gained by the ImageIO operations? As the sample is written, it appears to be (ideally) a no-op.
Also, you don't dispose your Graphics2D before you begin the ImageIO process. It often doesn't make a difference, but you don't want to assume that.
On the overlay color distortion problem, make sure your graphics context is in paint mode and not xor mode. (Graphics.setPaintMode()). Otherwise the color bits are XOR'd together.

Categories

Resources