r,g,b of any type image

J

jimgardener

hi
i am getting a BufferedImage and i need to extract the r,g,b
components of each pixel.I will not know which type the image is until
runtime(whether type 5(TYPE_3BYTE_BGR),type 1(TYPE_INT_RGB) or type
10(TYPE_BYTE_GRAY).i can get the pixel value using
BufferedImage.getRGB(...) as an int[].I want to know if i can get the
components the following way for any type of image.

BufferedImage img=ImageIO.read(new File("mypic.jpg"));
int wd=img.getWidth();
int ht=img.getHeight();
int[] rgbArray=new int[wd*ht];
img.getRGB(0, 0,wd,ht,rgbArray,0,wd);

int pix=rgbArray[10][12];//some pixel
int alpha=(pix >> 24) & 0x0ff;
int r=(pix>>16)& 0x0ff;
int g=(pix>>8)& 0x0ff;
int b=pix & 0x0ff;

System.out.println("r="+r+" g="+g+" b="+b);

is this valid for any type of image?or is there any byte order issue
in this?
if anyone can point out errors /alternatives it would be helpful
thanks
jim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top