gif to png?

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 :


      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

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:


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

sorry for the confusing subject, :stuck_out_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?? ???

[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.
[/quote]
what’s 1-bit transparent? i’m getting totally confuse in this transparent stuff :frowning:

[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;
[/quote]
can’t compile. is that really Translucency? or Transparency?
and where to set the transparent color in that code?