Essentially my question is 2 parts.
I want to know the preferred/fast method for being about to manipulate pixels and turn a certain color into a transparent pixel.
I want to know if I am able to use this "BufferedImage" without having to save it to a file format that supports transparency like "png."
I found a method for setting an individual pixel
here
BufferedImage layer = ImageIO.read(new File(file));
System.out.println(layer.getWidth());
BufferedImage image = new BufferedImage(layer.getWidth(), layer.getHeight(), BufferedImage.TYPE_INT_ARGB);
WritableRaster raster = image.getRaster();
int width = layer.getWidth();
int height = layer.getHeight();
// Slow method: scan all input (layer) image pixels, plotting only those which are not the transparency color
int lPixel,red,green,blue;
for(int w=0;w<width;w++)
for(int h=0;h<height;h++)
{
lPixel = layer.getRGB(w,h);
if ((lPixel&0x00FFFFFF) != trans) //transparent color
{
red = (int)((lPixel&0x00FF0000)>>>16); // Red level
green = (int)((lPixel&0x0000FF00)>>>8); // Green level
blue = (int) (lPixel&0x000000FF); // Blue level
// Set the pixel on the output image's raster.
raster.setPixel(w,h,new int[]{red,green,blue,255});
}
}
which as it mentions is a "slow method."
I had found this thread Java: Filling a BufferedImage with transparent pixels
Which one comment talks about the "int[]" and manipulating pixels there.
- Essentially I want to take an image, remove the transparency of a certain color, and then be able to use that image.
I notice that I could just set each pixel with "setRGB" in the bufferedImage, but then there is "setpixel" in the "WritableRaster..."
Why exactly would you use the "WritableRaster's" "setpixel," instead of "setRGB" in the "BufferedImage?"
I'm also curious how I would access the "int[]" and if that is the preferred way to go about this?
Is there a way to search for certain colored pixels, instead of going through each and every pixel to find the right color myself?
Just curious what the preferred/fastest method would be.
The method with the int[] claimed it wouldn't use "hardware acceleration" so I'm curious if that is important as well?
- I want to ask about this image in general. I know that to use a transparent image, you need a special file format like "gif," "png," or "tga," but I'm curious if I can just use this new bufferedImage that I created instead of saving it, and then re-reading it back in? I'm not sure if once it's buffered if I can do anything with it, and it will display, or if it has to be a certain format?
I would assume once it's buffered in, I could do anything to it, but I'm curious what other people think?
Thoughts?
Thanks a lot for any help!