Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #1318 > unrolled thread

Making color image with t

Started by"gswarthout" <gswarthout@THRWHITE.remove-dii-this>
First post2011-04-27 15:32 +0000
Last post2011-04-27 15:32 +0000
Articles 6 — 3 participants

Back to article view | Back to comp.lang.java.gui


Contents

  Making color image with t "gswarthout" <gswarthout@THRWHITE.remove-dii-this> - 2011-04-27 15:32 +0000
    Re: Making color image wi "Knute Johnson" <knute.johnson@THRWHITE.remove-dii-this> - 2011-04-27 15:32 +0000
      Re: Making color image wi "Knute Johnson" <knute.johnson@THRWHITE.remove-dii-this> - 2011-04-27 15:32 +0000
      Re: Making color image wi "gswarthout" <gswarthout@THRWHITE.remove-dii-this> - 2011-04-27 15:32 +0000
      Re: Making color image wi "tar" <tar@THRWHITE.remove-dii-this> - 2011-04-27 15:32 +0000
    Re: Making color image wi "gswarthout" <gswarthout@THRWHITE.remove-dii-this> - 2011-04-27 15:32 +0000

#1318 — Making color image with t

From"gswarthout" <gswarthout@THRWHITE.remove-dii-this>
Date2011-04-27 15:32 +0000
SubjectMaking color image with t
Message-ID<1175187576.725465.228260@n59g2000hsh.googlegroups.com>
  To: comp.lang.java.gui
I need to turn a series of icon images into black & white equivalents
programattically in a paint method.

I'm starting with roughly circular images which are colored within and
everything outside of the circle is transparent.  The transparency
displays properly when the images are used in their normal form.

On the fly, however, I want to recolor select images into gray-scale
reflections of their original form, which I do with the following
code:

<Code>
BufferedImage grayImage = new BufferedImage(usableWidth, usableHeight,
BufferedImage.TYPE_BYTE_GRAY);
Graphics2D grayGraphics = grayImage.createGraphics();
grayGraphics.drawImage(originalImage, 0, 0, usableWidth, usableHeight,
this);
</Code>

This produces the effect I want with one problem, the previously
transparent corners of the image are now black.  I then draw this gray-
scale image into a new image which allows for transparency thusly:

<Code>
BufferedImage bufferedImage = new BufferedImage(usableWidth,
usableHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = bufferedImage.createGraphics();
g2d.drawImage(grayImage, 0, 0, usableWidth, usableHeight, this);
</Code>

>From here I have been unable to find a way to change the corners back
to being transparent (ignoring that it would turn all other instances
of black transparent as well).  I've tried XORing black with white
and, sure enough, this turns black into white and vice versa, but not
transparent.  I've tried it with new (Color 255, 255, 255, 0) which
would be a fully transparent white and it still doesn't work.

How can I accomplish this feat with either A) the way I started or, B)
a better way that I haven't explored?

Any help would be appreciated.

Greg

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [next] | [standalone]


#1319 — Re: Making color image wi

From"Knute Johnson" <knute.johnson@THRWHITE.remove-dii-this>
Date2011-04-27 15:32 +0000
SubjectRe: Making color image wi
Message-ID<7wTOh.46622$A53.25962@newsfe13.lga>
In reply to#1318
  To: comp.lang.java.gui
gswarthout@gmail.com wrote:
> I need to turn a series of icon images into black & white equivalents
> programattically in a paint method.
> 
> I'm starting with roughly circular images which are colored within and
> everything outside of the circle is transparent.  The transparency
> displays properly when the images are used in their normal form.
> 
> On the fly, however, I want to recolor select images into gray-scale
> reflections of their original form, which I do with the following
> code:
> 
> <Code>
> BufferedImage grayImage = new BufferedImage(usableWidth, usableHeight,
> BufferedImage.TYPE_BYTE_GRAY);
> Graphics2D grayGraphics = grayImage.createGraphics();
> grayGraphics.drawImage(originalImage, 0, 0, usableWidth, usableHeight,
> this);
> </Code>
> 
> This produces the effect I want with one problem, the previously
> transparent corners of the image are now black.  I then draw this gray-
> scale image into a new image which allows for transparency thusly:
> 
> <Code>
> BufferedImage bufferedImage = new BufferedImage(usableWidth,
> usableHeight, BufferedImage.TYPE_INT_ARGB);
> Graphics2D g2d = bufferedImage.createGraphics();
> g2d.drawImage(grayImage, 0, 0, usableWidth, usableHeight, this);
> </Code>
> 
>>From here I have been unable to find a way to change the corners back
> to being transparent (ignoring that it would turn all other instances
> of black transparent as well).  I've tried XORing black with white
> and, sure enough, this turns black into white and vice versa, but not
> transparent.  I've tried it with new (Color 255, 255, 255, 0) which
> would be a fully transparent white and it still doesn't work.
> 
> How can I accomplish this feat with either A) the way I started or, B)
> a better way that I haven't explored?
> 
> Any help would be appreciated.
> 
> Greg
> 

Greg:

Try this and see if it works for you.  You might have to play with the 
transparency.  If you want send me one of your images with the 
transparent pixels and I'll try it.

import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;

public class test {
     public static void main (String[] args) throws Exception {
         GraphicsEnvironment ge =
          GraphicsEnvironment.getLocalGraphicsEnvironment();
         GraphicsDevice gd = ge.getDefaultScreenDevice();
         GraphicsConfiguration gc = gd.getDefaultConfiguration();

         BufferedImage src = ImageIO.read(new File("saturn.jpg"));
         BufferedImage dest =
          new BufferedImage(src.getWidth(),src.getHeight(),
          BufferedImage.TYPE_BYTE_GRAY);

         ColorConvertOp op = new ColorConvertOp(
          src.getColorModel().getColorSpace(),
          dest.getColorModel().getColorSpace(),
          null);

         op.filter(src,dest);

         ImageIO.write(dest,"JPG",new File("bw.jpg"));
     }
}

-- 

Knute Johnson
email s/nospam/knute/

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1320 — Re: Making color image wi

From"Knute Johnson" <knute.johnson@THRWHITE.remove-dii-this>
Date2011-04-27 15:32 +0000
SubjectRe: Making color image wi
Message-ID<byTOh.46623$A53.42095@newsfe13.lga>
In reply to#1319
  To: comp.lang.java.gui
Knute Johnson wrote:
> gswarthout@gmail.com wrote:
>> I need to turn a series of icon images into black & white equivalents
>> programattically in a paint method.
>>
>> I'm starting with roughly circular images which are colored within and
>> everything outside of the circle is transparent.  The transparency
>> displays properly when the images are used in their normal form.
>>
>> On the fly, however, I want to recolor select images into gray-scale
>> reflections of their original form, which I do with the following
>> code:
>>
>> <Code>
>> BufferedImage grayImage = new BufferedImage(usableWidth, usableHeight,
>> BufferedImage.TYPE_BYTE_GRAY);
>> Graphics2D grayGraphics = grayImage.createGraphics();
>> grayGraphics.drawImage(originalImage, 0, 0, usableWidth, usableHeight,
>> this);
>> </Code>
>>
>> This produces the effect I want with one problem, the previously
>> transparent corners of the image are now black.  I then draw this gray-
>> scale image into a new image which allows for transparency thusly:
>>
>> <Code>
>> BufferedImage bufferedImage = new BufferedImage(usableWidth,
>> usableHeight, BufferedImage.TYPE_INT_ARGB);
>> Graphics2D g2d = bufferedImage.createGraphics();
>> g2d.drawImage(grayImage, 0, 0, usableWidth, usableHeight, this);
>> </Code>
>>
>>> From here I have been unable to find a way to change the corners back
>> to being transparent (ignoring that it would turn all other instances
>> of black transparent as well).  I've tried XORing black with white
>> and, sure enough, this turns black into white and vice versa, but not
>> transparent.  I've tried it with new (Color 255, 255, 255, 0) which
>> would be a fully transparent white and it still doesn't work.
>>
>> How can I accomplish this feat with either A) the way I started or, B)
>> a better way that I haven't explored?
>>
>> Any help would be appreciated.
>>
>> Greg
>>
> 
> Greg:
> 
> Try this and see if it works for you.  You might have to play with the 
> transparency.  If you want send me one of your images with the 
> transparent pixels and I'll try it.
> 
> import java.awt.*;
> import java.awt.image.*;
> import java.io.*;
> import javax.imageio.*;
> 
> public class test {
>     public static void main (String[] args) throws Exception {
>         GraphicsEnvironment ge =
>          GraphicsEnvironment.getLocalGraphicsEnvironment();
>         GraphicsDevice gd = ge.getDefaultScreenDevice();
>         GraphicsConfiguration gc = gd.getDefaultConfiguration();
> 
>         BufferedImage src = ImageIO.read(new File("saturn.jpg"));
>         BufferedImage dest =
>          new BufferedImage(src.getWidth(),src.getHeight(),
>          BufferedImage.TYPE_BYTE_GRAY);
> 
>         ColorConvertOp op = new ColorConvertOp(
>          src.getColorModel().getColorSpace(),
>          dest.getColorModel().getColorSpace(),
>          null);
> 
>         op.filter(src,dest);
> 
>         ImageIO.write(dest,"JPG",new File("bw.jpg"));
>     }
> }
> 

And ignore the GraphicsEnvironment, GraphicsDevice and 
GraphicsConfiguration code in the above.  I just didn't rip that out of 
what I started with.

-- 

Knute Johnson
email s/nospam/knute/

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1321 — Re: Making color image wi

From"gswarthout" <gswarthout@THRWHITE.remove-dii-this>
Date2011-04-27 15:32 +0000
SubjectRe: Making color image wi
Message-ID<1175195431.031212.162000@o5g2000hsb.googlegroups.com>
In reply to#1319
  To: comp.lang.java.gui
On Mar 29, 12:25 pm, Knute Johnson <nos...@rabbitbrush.frazmtn.com>
wrote:
> gswarth...@gmail.com wrote:
> > I need to turn a series of icon images into black & white equivalents
> > programattically in a paint method.
>
> > I'm starting with roughly circular images which are colored within and
> > everything outside of the circle is transparent.  The transparency
> > displays properly when the images are used in their normal form.
>
> > On the fly, however, I want to recolor select images into gray-scale
> > reflections of their original form, which I do with the following
> > code:
>
> > <Code>
> > BufferedImage grayImage = new BufferedImage(usableWidth, usableHeight,
> > BufferedImage.TYPE_BYTE_GRAY);
> > Graphics2D grayGraphics = grayImage.createGraphics();
> > grayGraphics.drawImage(originalImage, 0, 0, usableWidth, usableHeight,
> > this);
> > </Code>
>
> > This produces the effect I want with one problem, the previously
> > transparent corners of the image are now black.  I then draw this gray-
> > scale image into a new image which allows for transparency thusly:
>
> > <Code>
> > BufferedImage bufferedImage = new BufferedImage(usableWidth,
> > usableHeight, BufferedImage.TYPE_INT_ARGB);
> > Graphics2D g2d = bufferedImage.createGraphics();
> > g2d.drawImage(grayImage, 0, 0, usableWidth, usableHeight, this);
> > </Code>
>
> >>From here I have been unable to find a way to change the corners back
> > to being transparent (ignoring that it would turn all other instances
> > of black transparent as well).  I've tried XORing black with white
> > and, sure enough, this turns black into white and vice versa, but not
> > transparent.  I've tried it with new (Color 255, 255, 255, 0) which
> > would be a fully transparent white and it still doesn't work.
>
> > How can I accomplish this feat with either A) the way I started or, B)
> > a better way that I haven't explored?
>
> > Any help would be appreciated.
>
> > Greg
>
> Greg:
>
> Try this and see if it works for you.  You might have to play with the
> transparency.  If you want send me one of your images with the
> transparent pixels and I'll try it.
>
> import java.awt.*;
> import java.awt.image.*;
> import java.io.*;
> import javax.imageio.*;
>
> public class test {
>      public static void main (String[] args) throws Exception {
>          GraphicsEnvironment ge =
>           GraphicsEnvironment.getLocalGraphicsEnvironment();
>          GraphicsDevice gd = ge.getDefaultScreenDevice();
>          GraphicsConfiguration gc = gd.getDefaultConfiguration();
>
>          BufferedImage src = ImageIO.read(new File("saturn.jpg"));
>          BufferedImage dest =
>           new BufferedImage(src.getWidth(),src.getHeight(),
>           BufferedImage.TYPE_BYTE_GRAY);
>
>          ColorConvertOp op = new ColorConvertOp(
>           src.getColorModel().getColorSpace(),
>           dest.getColorModel().getColorSpace(),
>           null);
>
>          op.filter(src,dest);
>
>          ImageIO.write(dest,"JPG",new File("bw.jpg"));
>      }
>
> }
>
> --
>
> Knute Johnson
> email s/nospam/knute/

Unfortunately that, too, leaves the black corners intact.  I will look
into these filter ops further, though.

Greg

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1333 — Re: Making color image wi

From"tar" <tar@THRWHITE.remove-dii-this>
Date2011-04-27 15:32 +0000
SubjectRe: Making color image wi
Message-ID<ymiodma3d4k.fsf@sevak.isi.edu>
In reply to#1319
  To: comp.lang.java.gui

Maybe you could look at java.awt.image.RGBImageFilter and write your own
RGB => Grayscale conversion.  Or maybe using the existing
javax.swing.GrayFilter will do what you want.  I'm not sure if the
latter one preserves the alpha channel, though.


-- 
Thomas A. Russ,  USC/Information Sciences Institute

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1322 — Re: Making color image wi

From"gswarthout" <gswarthout@THRWHITE.remove-dii-this>
Date2011-04-27 15:32 +0000
SubjectRe: Making color image wi
Message-ID<1175196837.253678.108840@y66g2000hsf.googlegroups.com>
In reply to#1318
  To: comp.lang.java.gui
Solution found, with help from Knute:

BufferedImage bufferedImage = new BufferedImage(usableWidth,
usableHeight, BufferedImage.TYPE_4BYTE_ABGR_PRE);
bufferedImage.createGraphics().drawImage(img, 0, 0, usableWidth,
usableHeight, this);

ColorSpace grayColorSpace =
ColorSpace.getInstance(ColorSpace.CS_GRAY);
ColorConvertOp op = new ColorConvertOp(grayColorSpace,
bufferedImage.getColorModel().getColorSpace(), null);
op.filter(bufferedImage, bufferedImage);

g.drawImage(bufferedImage, MARGIN, MARGIN, usableWidth, usableHeight,
this);

Greg

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.gui


csiph-web