Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (533)
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  
  PNG Transparency error  (Read 2566 times)
0 Members and 1 Guest are viewing this topic.
Offline Renoria

Junior Member




...


« Posted 2009-01-08 06:59:45 »

Hello, I got a PNG image in my game ->



When it is rendered in java, it appears like this:



Why is that? It also looks like that in paint, but in windows explorer it looks fine...

Thank you in advance.

Offline woogley
« Reply #1 - Posted 2009-01-08 08:14:47 »

It would help if you were to show us the code you're using..

Anyway, mspaint only supports bitmask transparency, i.e. either a pixel is 100% transparent or it is 100% visible. The java code you are using is probably loading this image with bitmask transparency.. you need to be using "translucent" transparency..

Loading an image with ImageIO usually does this automatically for you. This code shows the image correctly:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
import javax.imageio.*;
import javax.swing.*;
import java.awt.image.*;
import java.awt.*;
import java.io.*;
class ImgPanel extends JPanel {
   BufferedImage img;
   public ImgPanel() {
      img = null;
      try {
         img = ImageIO.read(getClass().getResource("swirl.png"));
      }
      catch (IOException e) {
         e.printStackTrace();
      }
      setPreferredSize(new Dimension(img.getWidth(),img.getHeight()));
   }
   public void paintComponent(Graphics g) {
      /* draw red rectangle to show transparency works */
      g.setColor(Color.red);
      g.fillRect(0,0,getWidth(),getHeight());

      g.drawImage(img,0,0,null);
   }
}
public class Test extends JFrame {
   public Test() {
      super("Test");
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      getContentPane().add(new ImgPanel());
      pack();
      setVisible(true);
   }
   public static void main(String args[]) {
      new Test();
   }
}


The Transparency class outlines the different transarency options for Images
Offline Abuse

JGO Coder


Medals: 11


falling into the abyss of reality


« Reply #2 - Posted 2009-01-08 18:54:02 »

Here's a very useful png examining utility - so you know exactly what your paint tool is outputing.

http://entropymine.com/jason/tweakpng/

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Renoria

Junior Member




...


« Reply #3 - Posted 2009-01-09 13:04:39 »

Ok, I load it using this :

1  
2  
3  
4  
5  
6  
    public Picture(URL $locator) {
        img = p.getToolkit().createImage($locator);
        x = img.getWidth(null)/2;
        y = img.getHeight(null)/2;
        img = ImageTransparencyTransformer.getInstance().filter(img);
    }
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11


Game Engineer


« Reply #4 - Posted 2009-01-11 03:20:41 »

Yeah don't use that. Instead:

1  
2  
3  
4  
5  
public Picture(URL $locator) {
        img = ImageIO.read(@locator);
        x = img.getWidth()/2;
        y = img.getHeight()/2;
    }


ImageIO will return a BufferedImage, so instead of storing an Image, store a BufferedImage.

See my work:
OTC Software
Offline Renoria

Junior Member




...


« Reply #5 - Posted 2009-01-11 05:16:51 »

Okay Demonpants, I will try that Cheesy
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11


Game Engineer


« Reply #6 - Posted 2009-01-11 09:54:07 »

Oops @locator should be $locator, that's a typo. Just whatever the String/URL reference to your image file is.  Smiley

See my work:
OTC Software
Offline Renoria

Junior Member




...


« Reply #7 - Posted 2009-01-12 03:11:54 »

Hmm, one more question, how do I read it from an InputStream? I tried ImageIO.read(stream) but it reads it with bitmask transparency.

I use this but doesn't work.

1  
2  
3  
4  
5  
6  
        BufferedImage bi = null;
        try {
            bi = ImageIO.read(this.getClass().getClassLoader().getResourceAsStream("renoria/" + key));
        } catch (IOException ex) {
            Logger.getLogger(ImageLoader.class.getName()).log(Level.SEVERE, null, ex);
        }


Thanks.
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11


Game Engineer


« Reply #8 - Posted 2009-01-12 06:55:17 »

That should work. What sort of image are you loading? You can also try to draw the image into a new BufferedImage.

1  
2  
3  
4  
5  
BufferedImage temp = ImageIO.read(...);
BufferedImage useableImage = new BufferedImage(temp.getWidth(),temp.getHeight(),BufferedImage.TYPE_INT_ARGB);
Graphics2D g = useableImage.getGraphics();
g.drawImage(temp,0,0,null);
g.dispose();


After you do that, useableImage will contain the image from the file but it will be TYPE_INT_ARGB, which will have "cleaner" alpha access.

See my work:
OTC Software
Offline Renoria

Junior Member




...


« Reply #9 - Posted 2009-01-12 09:10:51 »

Ahh yes I found the error, the filter was returning an opaqe pixel, I fixed it now Cheesy Thanks everyone!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Abuse

JGO Coder


Medals: 11


falling into the abyss of reality


« Reply #10 - Posted 2009-01-12 12:05:55 »

That should work. What sort of image are you loading? You can also try to draw the image into a new BufferedImage.

1  
2  
3  
4  
5  
6  
BufferedImage temp = ImageIO.read(...);
BufferedImage useableImage = new BufferedImage(temp.getWidth(),temp.getHeight(),BufferedImage.TYPE_INT_ARGB);
Graphics2D g = useableImage.getGraphics();
g.setComposite(AlphaComposite.Src); // added line
g.drawImage(temp,0,0,null);
g.dispose();



After you do that, useableImage will contain the image from the file but it will be TYPE_INT_ARGB, which will have "cleaner" alpha access.

Your code example assumes the pixels of the newly constructed BufferedImage are initialised to 0x00000000, an assumption you would expect to be safe. However as far as i'm aware the BufferedImage javadoc makes no such guarantee, and I know of atleast 1 Apple VM release that did not conform to this assumption. (it initialised the pixels to an opaque grey)

So for completeness you should set the compositing rule so as to only use the alpha component of the source. (see above quoted code)

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline Renoria

Junior Member




...


« Reply #11 - Posted 2009-01-12 13:38:18 »

yeah I found the error, I had to change

1  
2  
3  
4  
5  
6  
7  
8  
9  
    public int filterRGB(int x, int y, int rgb) {
        int r = (rgb >> 16) & 0xFF;
        int g = (rgb >> 8) & 0xFF;
        int b = (rgb) & 0xFF;
        if (r == 255 && g == 21 && b == 255) {
            return 0x00000000;
        }
        return 0xff000000 | r >> 16 | g >> 8 | b;
    }


to:

1  
2  
3  
4  
5  
6  
7  
8  
9  
    public int filterRGB(int x, int y, int rgb) {
        int r = (rgb >> 16) & 0xFF;
        int g = (rgb >> 8) & 0xFF;
        int b = (rgb) & 0xFF;
        if (r == 255 && g == 21 && b == 255) {
            return 0x00000000;
        }
        return rgb;
    }


Thanks alot guys, it was my own fault Cheesy
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11


Game Engineer


« Reply #12 - Posted 2009-01-12 18:55:23 »

Huh that's interesting. I've never encountered that not working but it's good to know.

See my work:
OTC Software
Offline Renoria

Junior Member




...


« Reply #13 - Posted 2009-01-13 00:12:04 »

I'm pretty sure its because

0xFF000000

Alpha/Red/Green/Blue

Then FF means fully opque alpha.

Had to change it so it returned the same value as the one inputted.
Offline trembovetski

Senior Member




If only I knew what I'm talking about!


« Reply #14 - Posted 2009-01-13 08:11:04 »

Your code example assumes the pixels of the newly constructed BufferedImage are initialised to 0x00000000, an assumption you would expect to be safe. However as far as i'm aware the BufferedImage javadoc makes no such guarantee, and I know of atleast 1 Apple VM release that did not conform to this assumption. (it initialised the pixels to an opaque grey)

So for completeness you should set the compositing rule so as to only use the alpha component of the source. (see above quoted code)

BufferedImages are guaranteed to be initialized with 0s (even if the spec doesn't say it), they are backed by java arrays - at least in Sun implementation. I would bet that that Apple bug that had BIs initialized to gray (could you give a link to the report?) was pre-1.5, which is for all intended purposes long gone..

Dmitri
Offline Abuse

JGO Coder


Medals: 11


falling into the abyss of reality


« Reply #15 - Posted 2009-01-13 14:51:21 »

BufferedImages are guaranteed to be initialized with 0s (even if the spec doesn't say it), they are backed by java arrays - at least in Sun implementation. I would bet that that Apple bug that had BIs initialized to gray (could you give a link to the report?) was pre-1.5, which is for all intended purposes long gone..

Dmitri


It was indeed a looooong time ago - someone reported it as a bug against something i'd written, can't remember what it was, or even if it was on this forum! (might have been on the Sun forums)

I'll have a quick hunt for it.

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline Abuse

JGO Coder


Medals: 11


falling into the abyss of reality


« Reply #16 - Posted 2009-01-13 16:13:14 »

It was indeed a looooong time ago - someone reported it as a bug against something i'd written, can't remember what it was, or even if it was on this forum! (might have been on the Sun forums)

I'll have a quick hunt for it.
Had a search & couldn't find it.

Incidentally, while your implicit guarantee that the DataBuffer will be initialized to zeros is good to know, it doesn't catch all scenarios.

One situation is when a value of 0 in the DataBuffer doesn't translate to the pixel being considered transparent black.
e.g.

1  
BufferedImage bi = new BufferedImage(400,400, BufferedImage.TYPE_BYTE_INDEXED, new IndexColorModel(8, 2, new byte[] {0x7F,0,0,0x7F,0,0,0,0},0,true));


I admit it's a rather contrived example Wink

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11


Game Engineer


« Reply #17 - Posted 2009-01-13 19:37:24 »

Well I've been assuming it's transparent and never had a problem reported by anyone playing my game. And because I always used TYPE_INT_ARGB I don't see that there will be a problem.

See my work:
OTC Software
Offline trembovetski

Senior Member




If only I knew what I'm talking about!


« Reply #18 - Posted 2009-01-13 22:18:06 »

Had a search & couldn't find it.

Incidentally, while your implicit guarantee that the DataBuffer will be initialized to zeros is good to know, it doesn't catch all scenarios.

One situation is when a value of 0 in the DataBuffer doesn't translate to the pixel being considered transparent black.
e.g.

1  
BufferedImage bi = new BufferedImage(400,400, BufferedImage.TYPE_BYTE_INDEXED, new IndexColorModel(8, 2, new byte[] {0x7F,0,0,0x7F,0,0,0,0},0,true));


I admit it's a rather contrived example Wink


I didn't say that the image will be initialized to transparent pixel, I said it will be init-ed with 0s. What 0 means obviously depends on the color model.

For INT_ARGB it means that pixels are transparent.

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

pw (22 views)
2014-07-24 01:59:36

Riven (20 views)
2014-07-23 21:16:32

Riven (17 views)
2014-07-23 21:07:15

Riven (20 views)
2014-07-23 20:56:16

ctomni231 (48 views)
2014-07-18 06:55:21

Zero Volt (44 views)
2014-07-17 23:47:54

danieldean (35 views)
2014-07-17 23:41:23

MustardPeter (38 views)
2014-07-16 23:30:00

Cero (53 views)
2014-07-16 00:42:17

Riven (52 views)
2014-07-14 18:02:53
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

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24: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!