Board index » delphi » Changing a single color on a bitmap palette

Changing a single color on a bitmap palette

I need to change a single color (clLime, shipped by us) on a bitmap
palette to a different color (various, user selected depending on
variables). When I have done this with a loop, it worked fine on my 64k
color machine, but the colors were all wrong on a 256 color machine. So
I figure that the palette is the problem. Is there any way of making
sure that the colors I need are avaiable on the bitmap canvas?

And, I was thinking that I should be able to just change the one color
in the palette that I know needs to be changed, not have to loop through
the whole canvas. Am I right? How would I do this?

Thanks
Ed Dressel

 

Re:Changing a single color on a bitmap palette


Quote
"Ed Dressel" <E...@brandentech.com> wrote in message

news:387A52D0.D9B50A6A@brandentech.com...

Quote
> I need to change a single color (clLime, shipped by us) on a bitmap
> palette to a different color (various, user selected depending on
> variables). When I have done this with a loop, it worked fine on my 64k
> color machine, but the colors were all wrong on a 256 color machine. So
> I figure that the palette is the problem. Is there any way of making
> sure that the colors I need are avaiable on the bitmap canvas?

What is the PixelFormat of your image?  pf8bit or pf24bit?

How are you changing the color?  Are you using the canvas.Pixels
property (which will be fairly slow), or are you changing the palette
and filling in the Scanlines?

Quote
> And, I was thinking that I should be able to just change the one color
> in the palette that I know needs to be changed, not have to loop through
> the whole canvas. Am I right? How would I do this?

While working on pf8bit scanline examples, described at
http://www.efg2.com/Lab/ImageProcessing/Scanline.htm, I observed
the only way I obtained reliable results was to set the palette for a new
bitmap and then fill in the scanlines.   This is fairly fast using the
Scanline property.

I'm only guessing, but when you try to change a palette on a bitmap
and leave the scanlines untouched, Windows tries to find the "nearest"
color for you and can modify the scanline data as part of what it does.
I found I had to fill in the scanline data once the palette was set to
get reliable results.

For links about using palettes in Delphi, try Section A of
http://www.efg2.com/Lab/Library/Delphi/Graphics/Algorithms.htm

--
efg

Earl F. Glynn     E-Mail:  EarlGl...@att.net
Overland Park, KS  USA

efg's Computer Lab:  http://www.efg2.com/Lab

Re:Changing a single color on a bitmap palette


Hi Earl,

"Earl F. Glynn" schrieb:

Quote
> While working on pf8bit scanline examples, described at
> http://www.efg2.com/Lab/ImageProcessing/Scanline.htm, I observed
> the only way I obtained reliable results was to set the palette for a new
> bitmap and then fill in the scanlines.   This is fairly fast using the
> Scanline property.

> I'm only guessing, but when you try to change a palette on a bitmap
> and leave the scanlines untouched, Windows tries to find the "nearest"
> color for you and can modify the scanline data as part of what it does.
> I found I had to fill in the scanline data once the palette was set to
> get reliable results.

I believe that the right approach here is to use the SetDIBColorTable
API call to replace the respective entries in the DIB's color table.
You then don't need to modify the scanline data.

Greetings, Robert
--
Robert Ro?mair
http://home.t-online.de/home/Robert.Rossmair/
Programming environment: Delphi 5 Professional, WinNT 4.0 SP6

Re:Changing a single color on a bitmap palette


Hi,

Ed Dressel schrieb:

Quote
> I need to change a single color (clLime, shipped by us) on a bitmap
> palette to a different color (various, user selected depending on
> variables). When I have done this with a loop, it worked fine on my 64k
> color machine, but the colors were all wrong on a 256 color machine. So
> I figure that the palette is the problem. Is there any way of making
> sure that the colors I need are avaiable on the bitmap canvas?

> And, I was thinking that I should be able to just change the one color
> in the palette that I know needs to be changed, not have to loop through
> the whole canvas. Am I right? How would I do this?

There is a function named "MapBitmapColors" in my rrColors unit
(freeware + source, see http://home.t-online.de/home/Robert.Rossmair/).
Since it works on DIBs, you need Delphi 3 or above to use it.

Greetings, Robert
--
Robert Ro?mair
Programming environment: Delphi 5 Professional, WinNT 4.0 SP6

Re:Changing a single color on a bitmap palette


Ed,

I had a similar problem a while back, and the following worked for me.  Hope
it helps you...

MyBitmap.Handle := CreateMappedBmp(MyBitmap.Handle, [clLime], [NewColor]);

CreateMappedBmp is in the graphics.pas unit.

-Eric Harmon

Other Threads