Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Raster Data Copying Messing Up Alpha Values?  (Read 1376 times)
0 Members and 1 Guest are viewing this topic.
Offline ghostsoldier23

Junior Member


Medals: 1



« Posted 2012-05-21 01:22:24 »

I have the following code that renders the pixel data from one buffered image on to the main canvas (also a BufferedImage) via their DataBuffers.

Both images are of TYPE_INT_ARGB. I can't remember when this started happening or what I did to cause it, but the image that is being rendered on screen now has a white background even though the BufferdImage itself says the background pixel data is 0,0,0,0 (transparent).

It's being read from a PNG file through ImageIO and converted to TYPE_INT_ARGB.

When the image(s) (it isn't isolated to one image either) are displayed in Firefox or Chrome, the background is transparent, as it is supposed to be, not white.

Any idea as to how the alpha is getting screwed up?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
      if(rdata.img.getType() != pri.getType())
         rdata.img = ImageUtils.convertBufferedImage(rdata.img, pri.getType());

      Rectangle r = new Rectangle(0,0,pri.getWidth(),pri.getHeight());
      Rectangle r2 = new Rectangle(rdata.x, rdata.y, rdata.img.getWidth(), rdata.img.getHeight());
      if(!r.contains(r2)) {
         Rectangle ri = r2.createIntersection(r).getBounds();
         int x = (int) Math.round(ri.getX());
         int y = (int) Math.round(ri.getY());
         int wt = (int) Math.round(ri.getWidth());
         int ht = (int) Math.round(ri.getHeight());
         int tx = x - rdata.x;
         int ty = y - rdata.y;
         if(wt > 0 && ht > 0)
            pri.getRaster().setDataElements(x, y, rdata.img.getSubimage(tx, ty, wt, ht).getRaster());
      } else {
         pri.getRaster().setDataElements(rdata.x, rdata.y, rdata.img.getRaster());  
      }


EDIT:
I think I may have figured out the problem but not the answer...

Setting the Raster data in that manner sets EVERY pixel in the main canvas to the source image's values, including transparent pixels, so the canvas has a sort of "hole" as a result, showing through to the back buffer canvas.

Perhaps I should use for loops instead of setDataElements?
Offline Rorkien
« Reply #1 - Posted 2012-05-21 02:01:14 »

I guess you are checking if the pixel has transparency (If it has, ignore; If not, draw).
In this case, you should check what color the transparency is. It's supposed to be 0, but sometimes they are negative numbers

A question: Are you, in any point, editing the pixel data?


Offline ghostsoldier23

Junior Member


Medals: 1



« Reply #2 - Posted 2012-05-21 02:19:06 »

No.  Not currently.

Question though... I fixed the problem (it now ignores pixel values of 0)... but what would you suggest for rendering the transparency?  What if something is only partially transparent, but I want it to be transparent over the previous pixel value set?  Do I need a bitwise op?

1  
int newPixel = oldColor & newColor;
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Rorkien
« Reply #3 - Posted 2012-05-21 03:20:03 »

Yep, you need bitwise on almost everything on colors Tongue
Offline ghostsoldier23

Junior Member


Medals: 1



« Reply #4 - Posted 2012-05-21 03:35:12 »

Yeah I figured.

Although for the purpose of blending translucent/opaque or translucent/translucent pixels can't you just do the average?

1  
int blended = (oldColor + newColor) / 2;
Offline Rorkien
« Reply #5 - Posted 2012-05-21 04:11:51 »

Remember you are working with integers:

One blue tone equals 1 (0x1)
One green tone equals 256 (0x100)
One red tone equals 65536 (0x10000)

So if you divide full green (0xFF00) by two, you'll have half green and half blue (around 0x8080)
If you want to blend colors, you'll need to bitwise AND to separate the colors and blend them individually, then shift left 16 bits for red and 8 for green
Offline ghostsoldier23

Junior Member


Medals: 1



« Reply #6 - Posted 2012-05-25 16:57:58 »

Yeah I am able to use bitwise AND to separate the RGBA values, but what do you mean by blending them individually?
Offline Rorkien
« Reply #7 - Posted 2012-05-25 19:09:21 »

Let's say something like this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
//You don't need that much integers, you can pretty much do all of this in a few lines of code

int oldcolor = 0xFFFF00; //Plain yellow
int blendcolor = 0xFF; //Blue

//Splitting the colors
int oldred = oldcolor & 0xFF0000;
int oldgreen = oldcolor & 0xFF00;
int oldblue = oldcolor & 0xFF;

int blendred = blendcolor & 0xFF0000;
int blendgreen = blendcolor & 0xFF00;
int blendblue = blendcolor & 0xFF;

//Blending colors individually
int newred = (oldred + blendred) / 2;
int newgreen = (oldgreen + blendgreen) / 2;
int newblue = (oldblue + blendblue) / 2;

return newred << 16 + newgreen << 8 + newblue;


Would return 0x7F7F7F
Now if you blend the colors without splitting them:

1  
2  
3  
4  
int oldcolor = 0xFFFF00; //Plain yellow
int blendcolor = 0xFF; //Blue

return (oldcolor + blendcolor) / 2;


Would return 0x7FFFFF, light blue.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CogWheelz (15 views)
2014-08-01 22:53:16

CogWheelz (15 views)
2014-08-01 22:51:43

CopyableCougar4 (16 views)
2014-08-01 19:37:19

CogWheelz (19 views)
2014-07-30 21:08:39

Riven (27 views)
2014-07-29 18:09:19

Riven (16 views)
2014-07-29 18:08:52

Dwinin (14 views)
2014-07-29 10:59:34

E.R. Fleming (35 views)
2014-07-29 03:07:13

E.R. Fleming (13 views)
2014-07-29 03:06:25

pw (44 views)
2014-07-24 01:59:36
Resources for WIP games
by CogWheelz
2014-08-01 18:20:17

Resources for WIP games
by CogWheelz
2014-08-01 18:19:50

List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

List of Learning Resources
by SilverTiger
2014-07-31 18:26:06

List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!