Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (120)
games submitted by our members
Games in WIP (577)
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  
  gif to png?  (Read 1151 times)
0 Members and 1 Guest are viewing this topic.
Offline role

Junior Duke




Java manmoth is coming!


« Posted 2003-03-08 12:23:23 »

hello everyone......
i'm getting sick of this graphics stuff
i just want to change my gif image (256 color with transparent) to png image (24bit with alphamask)
i rip all this code to make transparent png
but it makes the game very very slow!!

then i use BufferedImage, and the game fps back to normal (same as like using the gif with automatic transparent image)
but the images loading took very long time!! (not the game fps, only the init images)

anybody can share any png masking code?
and if u don't mind, tell me what's wrong with this code!

okay, here is the code :
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  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
      public static Image getBitmaskImage(GraphicsConfiguration gc, MediaTracker tracker,
                                                            String imageName,

Color keyColor) {
            Image image = Toolkit.getDefaultToolkit().getImage(imageName);
            tracker.addImage(image, 0);
            try { tracker.waitForID(0);
            } catch (Exception e) { }

            // works fine below 5 images
            Image alphaImage = applyMask(image, keyColor);

            // with BufferedImage the game fps back to normal
            // but takes very long time to load the images
            /*BufferedImage bufferedImage = gc.createCompatibleImage(
                  alphaImage.getWidth(null), alphaImage.getHeight(null),

Transparency.BITMASK);
            Graphics g = (Graphics2D) bufferedImage.getGraphics();
            g.drawImage(alphaImage, 0,0,null, null);
            g.dispose();*/


            return alphaImage;
            // return bufferedImage;
      }

      public static final int[] getPixels(Image img) {
            final int[] pix = new int[img.getWidth(null) * img.getHeight(null)];
            final PixelGrabber pg = new PixelGrabber
                  (img, 0, 0, img.getWidth(null), img.getHeight(null), pix, 0,

img.getWidth(null));
            try { pg.grabPixels();
            } catch (InterruptedException e) { }
            return pix;
      }
      public static final Image getImage(int[] pixels, int w, int h) {
            return Toolkit.getDefaultToolkit().createImage(
                  new MemoryImageSource(w, h, pixels, 0, w));
      }
      public static final Image applyMask(Image img, Color keyColor) {
            final int w = img.getWidth(null);
            final int h = img.getHeight(null);
            final int[] pxls = getPixels(img);
            final int blank = new Color(255, 0, 0, 0).getRGB();
            for (int i = 0; i < pxls.length; i++) {
                  if (pxls[i] == keyColor.getRGB()) {
                        pxls[i] = pxls[i] & 0x00ffffff;
                        //pxls[i] = blank;
                  }
            }
            return getImage(pxls, w, h);
      }


thanxx

Golden T Game Engine - Java2D/OpenGL Game Engine
Golden T Website | Golden T Forum
Offline trembovetski

Senior Duke




If only I knew what I'm talking about!


« Reply #1 - Posted 2003-03-10 04:26:44 »

The code does a lot of stuff per each pixel, that's why it's slow.

May be I'm missing something, but why not just copy your original image to a 1-bit transparent image? It'll be much more efficient.

The code should look something like this:
1  
2  
3  
4  
5  
Image newImage = gc.createCompatibleImage(width, height, Translucency.BITMASK);
Graphics2D g = (Graphics2D)newImage.getGraphics();
g.setComposite(AlphaComposite.Src);
g.drawImage(origImage, 0, 0, null);
return newImage;


BITMASK is a 25-bit image, 24 bits for RGB and 1-bit for transparency.

Also, you can use ImageIO to convert your gif image to png - just read it and then write as png. For example, you can read your gif images directly into a 1-bit trtanslucent 24 bit image - take a look at javax.imageio.ImageReader.readl(int imageIndex, ImageReadParam param) method. In the param you can specify a destination buffered image.
Then you can write that image in png format to a file (if that's your goal - it's still not clear to me what are you trying to achieve).

Offline role

Junior Duke




Java manmoth is coming!


« Reply #2 - Posted 2003-03-10 05:04:23 »

Quote

Also, you can use ImageIO to convert your gif image to png - just read it and then write as png.
For example, you can read your gif images directly into a 1-bit trtanslucent 24 bit image - take a look at javax.imageio.ImageReader.readl(int imageIndex, ImageReadParam param) method. In the param you can specify a destination buffered image. Then you can write that image in png format to a file (if that's your goal - it's still not clear to me what are you trying to achieve).


sorry for the confusing subject,  Tongue
i mean gif to png is not convert my gif image into png using java

the one i want to do is using 24-bit png with one transparent color in my game
i need the code to convert green color in my png image to transparent color
btw, what's the difference mask, transparent, and alpha??  Huh

Quote
May be I'm missing something, but why not just copy your original image to a 1-bit transparent image? It'll be much more efficient.


what's 1-bit transparent? i'm getting totally confuse in this transparent stuff Sad

Quote
The code should look something like this:
Code:
Image newImage = gc.createCompatibleImage(width, height, Translucency.BITMASK);
Graphics2D g = (Graphics2D)newImage.getGraphics();
g.setComposite(AlphaComposite.Src);
g.drawImage(origImage, 0, 0, null);
return newImage;


can't compile. is that really Translucency? or Transparency?
and where to set the transparent color in that code?

Golden T Game Engine - Java2D/OpenGL Game Engine
Golden T Website | Golden T Forum
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.

Longarmx (52 views)
2014-10-17 03:59:02

Norakomi (43 views)
2014-10-16 15:22:06

Norakomi (33 views)
2014-10-16 15:20:20

lcass (37 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (67 views)
2014-10-14 00:35:47

TehJavaDev (60 views)
2014-10-14 00:32:37

BurntPizza (73 views)
2014-10-11 23:24:42

BurntPizza (45 views)
2014-10-11 23:10:45

BurntPizza (86 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

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

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

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!