aspose file tools*
The moose likes Java in General and the fly likes Pixel Transparency Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Pixel Transparency" Watch "Pixel Transparency" New topic
Author

Pixel Transparency

Brendan Jones
Greenhorn

Joined: Aug 04, 2009
Posts: 4
I'm working on a program that allows me to edit raw pixel data on an image, but am having issues with transparency. Currently, in order to change a pixel's color, I use this method. It works fine and sets the pixel color correctly, but does not account for alpha.

I've attempted to implement alpha/transparency, but have not been successful. My current version of the method is as follows.

I'm not exactly sure what to do with the new color, old color, and alpha value in order to get the new color. Any ideas?

[If you need any more information, feel free to ask]
Fiorenza Oppici
Ranch Hand

Joined: Jul 22, 2010
Posts: 38
so, let me understand, your question is how to create a new color instance using both the old color info and the new alpha value, or it's more a problem regarding code for doing that? : )

[I study here]
Regards, FiĆ²
Brendan Jones
Greenhorn

Joined: Aug 04, 2009
Posts: 4
Fiorenza Oppici wrote:so, let me understand, your question is how to create a new color instance using both the old color info and the new alpha value, or it's more a problem regarding code for doing that? : )


My question is how would I go about getting the value of the new color. I have no problem actually setting the pixel color and then drawing the image to the screen, I just don't know how to implement transparency into the pixels.
Fiorenza Oppici
Ranch Hand

Joined: Jul 22, 2010
Posts: 38
Brendan Jones wrote:
Fiorenza Oppici wrote:so, let me understand, your question is how to create a new color instance using both the old color info and the new alpha value, or it's more a problem regarding code for doing that? : )


My question is how would I go about getting the value of the new color. I have no problem actually setting the pixel color and then drawing the image to the screen, I just don't know how to implement transparency into the pixels.

as far as i know the int oldColor contains the info for red component in bits 16-23, for green component in bits 8-15 and for the blue one in bits 0-7, as the Javadoc says.
so, you can get from that single int three other ints: red, green, and blue, just truncating the bits and then shift dividing each chunk for the right weight. then you can instanciate the Color e.g "newcolor" with the constructor new Color (int red, int green, int blue, int alpha).

all this work can be done simply calling the Color methods getGreen(), getRed() e getBlue(). I'm not comfortable with shifting so I'd go for this option.
but you already used shifting and bitwise operations in your code, so you can do the first way.

maybe someone can be more explicative on the exact code for converting the int into separate components not using the "get" methods; I found some examples on the web but I never suggest something it's not my own code... however, I hope i've been helpful.
Brendan Jones
Greenhorn

Joined: Aug 04, 2009
Posts: 4
Fiorenza Oppici wrote:
Brendan Jones wrote:
Fiorenza Oppici wrote:so, let me understand, your question is how to create a new color instance using both the old color info and the new alpha value, or it's more a problem regarding code for doing that? : )


My question is how would I go about getting the value of the new color. I have no problem actually setting the pixel color and then drawing the image to the screen, I just don't know how to implement transparency into the pixels.

as far as i know the int oldColor contains the info for red component in bits 16-23, for green component in bits 8-15 and for the blue ones in bits 0-7, as the Javadoc says.
so, you can get from that single int three other ints red, green, and blue truncating the bits and then shift divide each chunk with the right weight, so then you can instanciate the Color e.g "newcolor" with the constructor (int red, int g, int b, int alpha).

all this work can be done simply calling the Color methods getGreen(), getRed() e getBlue(). I'm not comfortable with shifting so I'd go for this option.
but you already used shifting and bitwise operations in your below code, so you can do the first way.

maybe someone can be more explicative on the exact code for converting the int into separate components, i found some examples on the web but I never suggest something it's not my own code... however, I hope i've been hepful.


blue = bits 0 - 7
green = bits 8-15
red = bits 16-23
alpha = bits 24-31

I know how the color data is stored, so my issue isn't getting the RGBA values of the colors (That's quite simple).

Say, for example, I had a black background and wrote 'Transparency' in white letters on it. Now, if I were to draw a transparent red rectangle over it, you would be able to see the letters through the red. While if it were fully opaque, the red would cover everything (Black & White) up and you would see nothing but a red rectangle.

What I need to know is how I'd calculate what color I should actually paint the pixel. If I were to set the color to a transparent value, it would just be that color (It won't 'blend' with the colors below it, it would just overwrite them).

-----------------------------------

Edit: Nevermind, after an hour or so of searching on Google, I managed to find a formula for calculating colors. For anyone that was wondering, it looks like this.



Note: All of the float values must be between the value of 0.0 and 1.0.

Hope this helps someone
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39768
    
  28
Moving thread as too difficult for "beginning".
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Erm... did you try searching for alpha blending implementations? There are a lot of examples.
Fiorenza Oppici
Ranch Hand

Joined: Jul 22, 2010
Posts: 38
Sorry Brendan, I didn't get that overwriting colors was not your intention.
Happy that you found a possible solution, it could be useful in future.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Pixel Transparency