Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Replacing colors in a BufferedImage

1,044 views
Skip to first unread message

PickleMan

unread,
Mar 6, 2010, 6:37:52 PM3/6/10
to
I have an image as a BufferedImage. That image contains some white,
and I want to replace the white with orange. How do I do that?

Jeff Higgins

unread,
Mar 6, 2010, 6:59:54 PM3/6/10
to
PickleMan wrote:
> I have an image as a BufferedImage. That image contains some white,
> and I want to replace the white with orange. How do I do that?

What does yourBufferedImage.getColorModel() return?

Jeff Higgins

unread,
Mar 6, 2010, 7:03:40 PM3/6/10
to

Jeff Higgins

unread,
Mar 8, 2010, 2:29:19 PM3/8/10
to

Bringing this back to the group:

In an email message PickleMan wrote:

<http://helpdesk.objects.com.au/java/changing-the-colormodel-of-a-buff...>

Thanks, but I still don't completely understand how ColorModels work.
I mean, I don't get how to specify the colors. If I wanted to have all
white pixels (255,255,255) be represented as say...green pixels
(0,255,0); how would I set up the color model?

Study the javadocs, most of the classes of interest are here:
<http://java.sun.com/javase/6/docs/api/java/awt/image/package-frame.html>,

here is a pointer to a short tutorial:
<http://docs.rinet.ru/JaTricks/ch12.htm>

Jeff Higgins

unread,
Mar 8, 2010, 3:08:43 PM3/8/10
to

From the example pointed to above:

Note changes:

private static ColorModel createColorModel(int n) {
// Create a simple color model with all values mapping to
// a single shade of gray
// nb. this could be improved by reusing the byte arrays

byte[] r = new byte[16];
byte[] g = new byte[16];
byte[] b = new byte[16];
for (int i = 0; i < r.length; i++) {
r[i] = (byte) n;
g[i] = (byte) 255;
b[i] = (byte) n;
}
return new IndexColorModel(4, 16, r, g, b);
}

John B. Matthews

unread,
Mar 8, 2010, 5:49:34 PM3/8/10
to
In article <hn3lgp$eaq$1...@news.eternal-september.org>,
Jeff Higgins <oohi...@yahoo.com> wrote:

> > PickleMan wrote:

> > Thanks, but I still don't completely understand how ColorModels
> > work. I mean, I don't get how to specify the colors. If I wanted to
> > have all white pixels (255,255,255) be represented as say...green
> > pixels (0,255,0); how would I set up the color model?

If the default RGB ColorModel is OK, one simple approach might be to
use an RGBImageFilter [1] as described in the work cited [2]. If not,
LookupOp [3], a BufferedImageOp, with a suitable LookupTable [4] might
do. There's an example here [5].

[1]<http://java.sun.com/javase/6/docs/api/java/awt/image/RGBImageFilter.html>
[2]<http://www.webbasedprogramming.com/Tricks-of-the-Java-Programming-Gurus/>
[3]<http://java.sun.com/javase/6/docs/api/java/awt/image/LookupOp.html>
[4]<http://java.sun.com/javase/6/docs/api/java/awt/image/LookupTable.html>
[5]<http://www.javaworld.com/javaworld/jw-09-1998/jw-09-media.html>

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>

Jeff Higgins

unread,
Mar 8, 2010, 8:06:09 PM3/8/10
to
John B. Matthews wrote:

>>> PickleMan wrote:
>
>>> Thanks, but I still don't completely understand how ColorModels
>>> work.

Huh. This one seems to be all over the web.
> [2]<http://www.webbasedprogramming.com/Tricks-of-the-Java-Programming-Gurus/>


I wish for a freely available, comprehensive tutorial on AWT images.
Here's one that covers SampleModels & Rasters.
<http://javaboutique.internet.com/tutorials/rasters/>

John B. Matthews

unread,
Mar 8, 2010, 8:54:24 PM3/8/10
to
In article <hn46uj$b3k$1...@news.eternal-september.org>,
Jeff Higgins <oohi...@yahoo.com> wrote:

> John B. Matthews wrote:
>
> >>> PickleMan wrote:
> >
> >>> Thanks, but I still don't completely understand how ColorModels
> >>> work.

This prompts me to return to your question, "What does getColorModel()
return?" I sense that I missed some discussion.

> Huh. This one seems to be all over the web.

Indeed, I should be more careful about linking obviously unauthorized
copy of Vanderburg's 1996 work "Tricks of the Java Programming Gurus":

<http://www.amazon.com/Tricks-Programming-Gurus-Glenn-Vanderburg/dp/1575211025>

Interestingly, the link to a Moscow host that you provided had been
black-listed, and it was refused by my news host. I (rather carelessly)
Googled another.

> I wish for a freely available, comprehensive tutorial on AWT images.
> Here's one that covers SampleModels & Rasters.
>
> <http://javaboutique.internet.com/tutorials/rasters/>

I confess an enduring fascination. I had some fun with the JavaWorld
example, adding a revert button and replacing Hashtable with a Map:

Map<String,BufferedImageOp> mOps new TreeMap<String,BufferedImageOp>();

<http://www.javaworld.com/javaworld/jw-09-1998/media/ImageDicer.java>

Jeff Higgins

unread,
Mar 10, 2010, 1:19:26 PM3/10/10
to
John B. Matthews wrote:

> This prompts me to return to your question, "What does getColorModel()
> return?" I sense that I missed some discussion.
>

No, there was no prior discussion.
My question was intended as a prompt.

Shortly after posting,
It dawned on me that there is no way to mutate a
BufferedImage with a new ColorModel, hence my follow-up
with a pointer to an example.

Over the years I've had to take a lot of instruction
given in this manner, and while it is frustrating
sometimes when you're in a hurry to get answers, I've
learned a lot from being prompted to find my own answers.

John B. Matthews

unread,
Mar 10, 2010, 5:41:40 PM3/10/10
to
In article <hn8nsv$b9o$1...@news.eternal-september.org>,
Jeff Higgins <oohi...@yahoo.com> wrote:

> John B. Matthews wrote:
>
> > This prompts me to return to your question, "What does
> > getColorModel() return?" I sense that I missed some discussion.
>
> No, there was no prior discussion.
> My question was intended as a prompt.
>
> Shortly after posting,
> It dawned on me that there is no way to mutate a
> BufferedImage with a new ColorModel, hence my follow-up
> with a pointer to an example.

Ah, that makes sense. The BufferedImageOp, LookupOp, seems
closest to meeting the OP's needs. A the same time, I'm always
intrigued by the possibility of using ColorConvertOp, which
filters on ColorSpace, as seen in this pithy example:

<http://groups.google.com/group/comp.lang.java.programmer/msg/d4c436ab64d4ced4>

[...]

Jeff Higgins

unread,
Mar 10, 2010, 6:08:05 PM3/10/10
to
John B. Matthews wrote:
> In article <hn8nsv$b9o$1...@news.eternal-september.org>,
> Jeff Higgins <oohi...@yahoo.com> wrote:
>
>> John B. Matthews wrote:
>>
>>> This prompts me to return to your question, "What does
>>> getColorModel() return?" I sense that I missed some discussion.
>> No, there was no prior discussion.
>> My question was intended as a prompt.
>>
>> Shortly after posting,
>> It dawned on me that there is no way to mutate a
>> BufferedImage with a new ColorModel, hence my follow-up
>> with a pointer to an example.
>
> Ah, that makes sense. The BufferedImageOp, LookupOp, seems
> closest to meeting the OP's needs. A the same time, I'm always
> intrigued by the possibility of using ColorConvertOp, which
> filters on ColorSpace, as seen in this pithy example:
>
> <http://groups.google.com/group/comp.lang.java.programmer/msg/d4c436ab64d4ced4>
>
> [...]
How would you use a ColorConvertOp to change all white pixels in an
image to green(orange) per the OR?

Jeff Higgins

unread,
Mar 10, 2010, 6:35:15 PM3/10/10
to
Jeff Higgins wrote:
> John B. Matthews wrote:

I'm going to take this opportunty to plug Dick Baldwin:
<http://www.dickbaldwin.com/toc.htm>
the 400 series - Processing Image Pixels
<http://www.dickbaldwin.com/tocadv.htm>

Jeff Higgins

unread,
Mar 10, 2010, 7:14:20 PM3/10/10
to
John B. Matthews wrote:

> Ah, that makes sense. The BufferedImageOp, LookupOp, seems
> closest to meeting the OP's needs.

What function are you using?

John B. Matthews

unread,
Mar 11, 2010, 12:39:54 AM3/11/10
to
In article <hn9cmh$s6c$1...@news.eternal-september.org>,
Jeff Higgins <oohi...@yahoo.com> wrote:

Assuming a bufferedImage and three short[256] initialized to 0..255,

r[255] = 255; g[255] = 153; b[255] = 0;
short[][] whiteOrange = new short[][]{r, g, b};
LookupOp whiteOrangeOp = new LookupOp(
new ShortLookupTable(0, whiteOrange), null));

then

whiteOrangeOp.filter(bufferedImage, null);

> How would you use a ColorConvertOp to change all white pixels in an
> image to green(orange) per the OR?

I don't know that I could. It wouldn't be a very useful ColorSpace!

> I'm going to take this opportunty to plug Dick Baldwin:
> <http://www.dickbaldwin.com/toc.htm>
> the 400 series - Processing Image Pixels
> <http://www.dickbaldwin.com/tocadv.htm>

Yep, I'm a fan.

0 new messages