1

Essentially my question is 2 parts.

  1. I want to know the preferred/fast method for being about to manipulate pixels and turn a certain color into a transparent pixel.

  2. 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.


  1. 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?


  1. 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!

Community
  • 1
  • 1
XaolingBao
  • 1,034
  • 1
  • 18
  • 34
  • The whole point of `BufferedImage` is to be able to manipulate the image. It is an image with a buffer that you can access – Vince Nov 23 '15 at 19:08
  • Thanks, that's what I thought. I had issues in the past reading the image and such, so I figured that writing it off was the answer, but then I realized that I should be able to just use the image if I've already buffered it in.... Granted I'm changing it, and technically making a new buffered image. – XaolingBao Nov 23 '15 at 19:32

1 Answers1

1

Well about your second question: you just need to load the File into a BufferedImage with a right format that supports Transparency, for example:BufferedImage.TYPE_INT_ARGB. ARGB means "Alpha, Red , Green , Blue" where alpha stands for transparency. You can manipulate that BufferedImage however you want, File format is just how you intend to write it in the end.

As for your first question, I'm not sure how you would optimize this, as you are going to have to access each pixel(no matter which method you use), and change the value.I would go for the slow method, since (for me) seems unavoidable.

EDIT: The int[] method is that you basically take the values in the WritableRaster and put them in an array. So instead of calling setRGB() you would just call array[i][j]=someValue. This would theoretically increase performance as you would be doing low level things (accessing a value in some array) instead of a higher level method call like setRGB().But you would still have to convert that array back into a Raster and then back into a BufferedImage. To be honest, the profit of doing this seems negligeable as you are not making a high performance game.I just tried the "slow" method on a 4K picture ,the highest res you would usually encounter, and the program finished in 10 seconds (knowing that I made ALL the pixels transparent for a worst case scenario).A usual HD picture would take about 4 times less time.

vlatkozelka
  • 909
  • 1
  • 12
  • 27
  • Thank you very much for your advice! :) So I guess I can just display it once it has the correct buffer type, awesome! I was re-saving these images and using those. For one project I upload the files to the web, so I guess saving it into a png file and then uploading that would be preferred? I don't know if I could upload a file directly to a web-site as a bufferedimage???? As for the first question, I'm curious about the int-array/int[] and the difference between setPixel and setRGB. It seems to be an extra step to use the WritableRaster... Thanks! – XaolingBao Nov 23 '15 at 18:55
  • how are you uploading them? is this a client-server application you are making ? or just manually uploading images? – vlatkozelka Nov 23 '15 at 19:01
  • Thanks for the info on the int[]. So why do we even called "WritableRaster" if I could use "setRGB?" Is it because I'm re-writing an entire image? 10 seconds, not bad, so I guess 2 or so for a single image, but I have many. As for uploading them, I'm using Apache FileUpload to upload to a server. It essentially is a file uploader that uploads to the server for use with another application that will then read the data on the site. I'm just curious if I have to save the file, but I'm pretty sure I do.. thank you :). – XaolingBao Nov 23 '15 at 19:20