Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (494)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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  
  Drawing a transparent image  (Read 3997 times)
0 Members and 1 Guest are viewing this topic.
Offline Vey

Senior Newbie





« Posted 2009-05-12 12:03:12 »

I'm trying to make a certain colour of a picture transparent by looping through every pixel and if its value is equal to the color to be replaced, I replace it with another value that I think is transparent. However, when I draw the image with my g2d instance it doesn't come out transparent. The color is changed from the original colour to something else. I'm guessing that I either have the wrong RGB value for the alpha, or that I have to do something with the g2d instance in order to make it draw transparent colors? If that is the case, what should I do?

The RGB values I have tried are 0x00000000, 0 and 0x8F1C1C.

Help please!!

Thanks!

edit: I've checked google and used the search function, but they have not taught me anything new.
Offline Bonbon-Chan

JGO Coder


Medals: 12



« Reply #1 - Posted 2009-05-12 12:30:21 »

If your picture has no transparent, the image type may not be a transparent type.
First copy your image to INT_RGBA image (for exemple). Then replace your color to 0


Really quickly done dirty code :
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
  BufferedImage setTransparent(Image img,int color) // Don't forget to put the alpha value FF in the color
 {
     BufferedImage result = new BuffereImage(img.getWidth(),img.getHeight,BufferedImage.INT_RGBA);
     Graphics g = result.getGraphics();
     g.draw(img,0,0,null);
     g.dispose();
 
     for(int i=0;i<result .getWidth();i++)
     {
         for(int j=0;j<result .getHeight();j++)
        {
           int v = result.getRGB(i,j);

          if(v == color) {  result .setRGB(i,j,0); }
        }
     }

     return result;
  }
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 793
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #2 - Posted 2009-05-12 12:30:55 »

You cannot set alpha values using bufimg.setRGB()

Use bufimg.getAlphaRaster() to write the alpha values.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 793
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2009-05-12 12:33:10 »

this is my code to search a color with and replace it with a specified ARGB value.

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  
   public static BufferedImage replaceColor(BufferedImage srcBuf, int cSearch, int cReplace)
   {
      int[] srcRGB = srcBuf.getRGB(0, 0, srcBuf.getWidth(), srcBuf.getHeight(), null, 0, srcBuf.getWidth());

      int[] dstRGB = new int[srcRGB.length];
      int[] dstA = new int[dstRGB.length];

      for (int i = 0; i < srcRGB.length; i++)
      {
         if ((srcRGB[i] & 0x00FFFFFF) == cSearch) // match RGB
        {
            dstRGB[i] = cReplace & 0x00FFFFFF; // set RGB
           dstA[i] = (cReplace >> 24) & 0xFF; // set A
        }
         else
         {
            dstRGB[i] = srcRGB[i]; // copy RGB
           dstA[i] = 0xFF; // make A opaque
        }
      }

      BufferedImage dstBuf = new BufferedImage(srcBuf.getWidth(), srcBuf.getHeight(), BufferedImage.TYPE_INT_ARGB);
      dstBuf.setRGB(0, 0, dstBuf.getWidth(), dstBuf.getHeight(), dstRGB, 0, dstBuf.getWidth());
      dstBuf.getAlphaRaster().setPixels(0, 0, dstBuf.getWidth(), dstBuf.getHeight(), dstA);

      return dstBuf;
   }

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Cero
« Reply #4 - Posted 2009-05-12 12:51:13 »

I'm just using transparent png files instead of doing this...
is there much less memory usage doing it like that or any other advantages ?

Offline mh114

Junior Member





« Reply #5 - Posted 2009-05-12 13:24:32 »

I'm just using transparent png files instead of doing this...
is there much less memory usage doing it like that or any other advantages ?
The runtime memory consumption will be same, afterall an RGBA image is an RGBA image, no matter how it was loaded/generated. Disk space requirements are obviously smaller if you use RGB images, but unless you're doing applets or something else where disk space counts it's completely fine to use RGBA images like .pngs. Smiley

Offline Vey

Senior Newbie





« Reply #6 - Posted 2009-05-12 16:42:06 »

Thanks, but I still can't get this to work. I stole the code posted by Riven, and I'm calling it like this:

spriteImage = Cam.replaceColor(spriteImage, spriteImage.getRGB(0,spriteImage.getHeight()-1), Color.YELLOW.getRGB());

And I've made sure that spriteImage is of the type TYPE_INT_ARGB. I know it's not supposed to be yellow, but no colors work. What am I doing wrong here? Thanks!
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 793
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #7 - Posted 2009-05-12 16:47:51 »

You are supposed to pass a ARGB value in the last parameter.

replaceColor(img, RGB, ARGB)

To replace red with opaque yellow:
1  
2  
BufferedImage sprite = ...;
sprite = replaceColor(sprite, 0xFF000, 0xFFFFFF00);


To replace purple with fully transparant white:
1  
2  
BufferedImage sprite = ...;
sprite = replaceColor(sprite, 0xFF00FF 0x00FFFFFF);

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #8 - Posted 2009-05-12 16:51:15 »

I'm just using transparent png files instead of doing this...
is there much less memory usage doing it like that or any other advantages ?
I did this specifically for a sprite sheet loader I put into the level editor of a game. That was because it would accept sprite sheets from the internet, which could be fairly random in terms of whether they had transparency or not.

See my work:
OTC Software
Offline Vey

Senior Newbie





« Reply #9 - Posted 2009-05-12 16:51:51 »

Okay, but how do I get the ARGB value of a certain color?

Cheers
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 793
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #10 - Posted 2009-05-12 16:58:28 »

Sorry, I was wrong, color.getRGB() does return the ARGB value!

So passing Color.YELLOW should work.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline ddyer

Senior Member


Medals: 5



« Reply #11 - Posted 2009-05-13 18:18:53 »

Remember that images which start out life as JPGs don't have exact values for colors.  Jpeg
encode/decode changes color values, and changes solid color areas into salt-n-pepper of
similar values.
Offline Vey

Senior Newbie





« Reply #12 - Posted 2009-05-14 08:06:03 »

In that case what am I doing wrong?

Do I need to utilize a certain composite for this? And the image has been a .png all the time.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 793
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #13 - Posted 2009-05-15 09:04:35 »

No need to do any fancy stuff.

Just make your test case simpler.


Create an image that's fully red.
Load from disk, replace red to yellow, write to disk.
View the image in your favourite app, and see if it works.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
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.

Dwinin (21 views)
2014-09-12 09:08:26

Norakomi (55 views)
2014-09-10 13:57:51

TehJavaDev (65 views)
2014-09-10 06:39:09

Tekkerue (33 views)
2014-09-09 02:24:56

mitcheeb (54 views)
2014-09-08 06:06:29

BurntPizza (38 views)
2014-09-07 01:13:42

Longarmx (24 views)
2014-09-07 01:12:14

Longarmx (30 views)
2014-09-07 01:11:22

Longarmx (28 views)
2014-09-07 01:10:19

mitcheeb (36 views)
2014-09-04 23:08:59
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

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

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!