Delivering an Alpha Channel to a Buffered Image

I stumbled upon this code while watching the online development of java games.

Link to video

package gfx; import java.awt.image.BufferedImage; import java.io.IOException; import javax.imageio.ImageIO; public class SpriteSheet { public String path; public int width; public int height; public int[] pixels; public SpriteSheet(String path){ BufferedImage image=null; try { image = ImageIO.read(SpriteSheet.class.getResourceAsStream(path)); } catch (IOException e) { e.printStackTrace(); } if(image==null){ return; } this.path=path; this.width=image.getWidth(); this.height=image.getHeight(); pixels=image.getRGB(0, 0,width,height,null,0,width); for(int i=0;i<pixels.length;i++){ pixels[i] = (pixels[i] & 0xff)/64; } } } 

I'm still pretty new to java, so please forgive me if this is obvious. The video shows that

 pixels[i] = (pixels[i] & 0xff) 

gets rid of the alpha channel in pixel notation 0xAARRGGBB to make it 0xRRGGBB. From what I understand, what 0xff does is to capture the last byte of information from an integer. It is also explained that the number 64 in

 pixels[i] = (pixels[i] & 0xff)/64; 

obtained by dividing 255 by 4 (I believe this is a limitation on the number of colors).

so essentially my question is: how does 0xff get rid of the alpha channel?

I am quite sure that my confusion is the lack of experience with numbers in hexadecimal form.

+4
source share
1 answer

Your understanding is correct, and the explanation in the video about removing the alpha channel is incorrect.

This does not mean that everything is displayed as blue, because this image will not be displayed as it is now, it only needs the numbers 0, 1, 2, 3 (I did not watch the end of the video, so I don’t know how it is going make colors from 0, 1, 2, 3, but this should be a different step).

However, if these colors were displayed, they would be very dark blue, almost black. 0 is pure black, and the rest of the colors are indistinguishable from black.

For positive numbers (we know that (pixels [i] and 0xff) are positive), dividing by 64 is the same as a shift of 6 bits to the right. See https://en.wikipedia.org/wiki/Arithmetic_shift

In any case, the code would be much clearer if it used the shift operator:

 pixels[i] = (pixels[i] & 0xff) >>> 6; 
+1
source

All Articles