Board index » delphi » Replacing colors on a gif image

Replacing colors on a gif image


2006-05-10 03:48:19 AM
delphi43
Hello group,
I would like to replace colors on a gif image with black.
Here is a link to the image:
www.ssec.wisc.edu/data/sst/latest_sst.gif
The image has land mass that is in shades of brown and green, and these are the colors that I want to replace with black, basically making all land areas black.
Any suggestions on how to do this?
Best Regards,
Bryan
 
 

Re:Replacing colors on a gif image

Just change the global palette in the gif.
Like this:
finn.mobilixnet.dk/latest_sst_new.gif
--
Finn Tolderlund
"Bryan" <XXXX@XXXXX.COM>skrev i en meddelelse
Quote
I would like to replace colors on a gif image with black.

Here is a link to the image:
www.ssec.wisc.edu/data/sst/latest_sst.gif

The image has land mass that is in shades of brown and green, and these
are the colors that I want to replace with black, basically making all
land areas black.

Any suggestions on how to do this?
 

Re:Replacing colors on a gif image

Quote
I would like to replace colors on a gif image with black.

Here is a link to the image:
www.ssec.wisc.edu/data/sst/latest_sst.gif

The image has land mass that is in shades of brown and green, and
these are the colors that I want to replace with black, basically
making all land areas black.

Any suggestions on how to do this?
If you care only about this specific image, i wouldn't bother writing a
program for it. Use a standard image manipulation tool. Since the
colors are indexed all you need to do is change the pallete. All colors
you want to change are next to one another, so this is really easy here.
If you have a good image editor, use it. If you don't, the GIMP is a
good, free alternative. It takes some time to find the stuff you need
when you start with it, but eventually you will like it.
www.gimp.org/
It you will be wanting to change more pictures you will need a component to
load and store GIF.
 

Re:Replacing colors on a gif image

Bryan writes:
Quote
The image has land mass that is in shades of brown and green, and
these are the colors that I want to replace with black, basically
making all land areas black.
If Finn's solution won't work you will need to examine each pixel and
make a determination. I converted your image to bmp, loaded it into a
tImage, and wrote the following code:
procedure TForm1.Button1Click(Sender: TObject);
var row, col : integer;
p : tColor;
r, g, b : byte;
begin
for col := 0 to Image1.Picture.Width-1 do begin
for row := 0 to Image1.Picture.Height-1 do begin
p := Image1.Picture.Bitmap.Canvas.Pixels[col, row];
r := GetRValue(p);
g := GetGValue(p);
b := GetBValue(p);
if (not ((r = 0) or (g = 0) or (b = 0))) and (g < 200) then begin
Image1.Picture.Bitmap.Canvas.Pixels[col, row] := clBlack;
end;
end;
end;
end;
--
-Mike (TeamB)
 

Re:Replacing colors on a gif image

My first try was a quick hack using a hex editor.
I felt like putting more effort into it.
Here is some quick code to do it tested in Delphi 7:
uses
GifImage; // get it from finn.mobilixnet.dk/delphi/
procedure TForm1.Button1Click(Sender: TObject);
const
BrownGreen: array [1..34] of integer = (
$1D3804,$263D08,$2F430C,$384810,$53591C,$5C5E20,$656424,$6E6928,
$776F2C,$807430,$897A34,$927F38,$9B853C,$A48A40,$AD9044,$B69548,
$BF9B4C,$BD964B,$B58F47,$AD8844,$A58140,$98753A,$906D36,$886632,
$805F2E,$735328,$6B4B24,$634421,$5B3D1D,$4D3117,$452A13,$3D220F,
$351B0B,$280F05 );
var
gif: TGifImage;
i, j: Integer;
c: TColor;
begin
gif := TGifImage.Create;
try
gif.LoadFromFile('C:\gif-test\latest_sst.gif');
for i := 0 to gif.GlobalColorMap.Count-1 do
begin
c := gif.GlobalColorMap.Colors[i];
c := GetRValue(c) shl 16 + GetGValue(c) shl 8 + GetBValue(c);
for j := Low(BrownGreen) to High(BrownGreen) do
if BrownGreen[j] = c then
begin
gif.GlobalColorMap.Colors[i] := 0;
Break;
end;
end;
gif.SaveToFile('C:\gif-test\latest_sst_new2.gif');
finally
gif.Free;
end;
end;
--
Finn Tolderlund
"Finn Tolderlund" <XXXX@XXXXX.COM>skrev i en meddelelse
Quote
Just change the global palette in the gif.
Like this:
finn.mobilixnet.dk/latest_sst_new.gif
--
Finn Tolderlund


"Bryan" <XXXX@XXXXX.COM>skrev i en meddelelse
news:4460f203$XXXX@XXXXX.COM...
>I would like to replace colors on a gif image with black.
>
>Here is a link to the image:
>www.ssec.wisc.edu/data/sst/latest_sst.gif
>
>The image has land mass that is in shades of brown and green, and these
>are the colors that I want to replace with black, basically making all
>land areas black.
>
>Any suggestions on how to do this?
 

Re:Replacing colors on a gif image

Mike Williams (TeamB) writes:
Quote
Bryan writes:

>The image has land mass that is in shades of brown and green, and
>these are the colors that I want to replace with black, basically
>making all land areas black.

If Finn's solution won't work you will need to examine each pixel and
Nonsense !
The easy way is to Assign the GIF to a 8-bit bitmap, get the Colortable, change
the brown and green entries to black, save the Colortable, the Assign the bitmap
back to the GIF.
Production code will be posted to borland.public.attachments shortly.
cheers,
Mat
 

Re:Replacing colors on a gif image

To use this code with a GIF (PNG, TIF, etc), just:
ABitmap := TBitmap.Create;
ABitmap.PixelFormat := pf8bit;
ABitmap.Assign(MyGIF);
ChangeColor(ABitmap, clGreen, clBlack);
ChangeColor(ABitmap, clBrown, clBlack);
MyGIF.Assign(ABitmap);
ABitmap.Free;
{------------------------------------------------------------------------------
Procedure: ChangeColor
Description: This procedure changes all pixels in a bitmap from one colour to
another
Author: Mat Ballard
Date created: 08/20/2005
Date modified: 11/21/2005 by Mat Ballard
Purpose:
Known Issues:
------------------------------------------------------------------------------}
procedure ChangeColor(ABitmap: TBitmap; FromColor, ToColor: TColor);
var
i, j: Integer;
FromRGB, ToRGB: TRGBQuad;
ColorTable: array [0..255] of TRGBQuad;
begin
if (ABitmap.PixelFormat <= pf8bit) then
begin
FromRGB.rgbRed := FromColor and $FF;
FromRGB.rgbGreen := (FromColor shr 8) and $FF;
FromRGB.rgbBlue := (FromColor shr 16) and $FF;
ToRGB.rgbRed := ToColor and $FF;
ToRGB.rgbGreen := (ToColor shr 8) and $FF;
ToRGB.rgbBlue := (ToColor shr 16) and $FF;
GetDIBColorTable(ABitmap.Canvas.Handle, 0, 256, ColorTable);
for i := 0 to 255 do
begin
if ((ColorTable[i].rgbRed = FromRGB.rgbRed) and
(ColorTable[i].rgbGreen = FromRGB.rgbGreen) and
(ColorTable[i].rgbBlue = FromRGB.rgbBlue)) then
begin
ColorTable[i].rgbRed := ToRGB.rgbRed;
ColorTable[i].rgbGreen := ToRGB.rgbGreen;
ColorTable[i].rgbBlue := ToRGB.rgbBlue;
end;
end;
SetDIBColorTable(ABitmap.Canvas.Handle, 0, 256, ColorTable);
end
else if (ABitmap.PixelFormat = pf24bit) then begin
for i := 0 to ABitmap.Height-1 do
begin
for j := 0 to ABitmap.Width-1 do
begin
if (ABitmap.Canvas.Pixels[i, j] = FromColor) then
ABitmap.Canvas.Pixels[i, j] := ToColor;
end;
end;
end
else
raise EGraphicError.Create('ChangeColor: Bitmaps must be 1, 8 or 24 bit !');
end;
 

Re:Replacing colors on a gif image

In the case of a paletted image like GIF, wouldn't it just be faster to
change the color table that preceeds the compressed data?
Incidently, if you are going to modify a 24 bit color image directly,
it is significantly faster to access the raw rgb array from Scanline
than it is to use the Pixels property (which is very, very slow in
comparison)
Incidently, what GIF library are you using?
 

Re:Replacing colors on a gif image

Mat Ballard writes:
Quote
The easy way is to Assign the GIF to a 8-bit bitmap, get the
Colortable, change the brown and green entries to black, save the
Colortable, the Assign the bitmap back to the GIF.
Interesting. How would he determine which colors need to be changed
and which don't? Note that there are many different shades of browns
and greens in the image and that some of the green pixels should not be
changed.
--
-Mike (TeamB)
 

Re:Replacing colors on a gif image

Mike Williams (TeamB) writes:
Quote
Mat Ballard writes:

>The easy way is to Assign the GIF to a 8-bit bitmap, get the
>Colortable, change the brown and green entries to black, save the
>Colortable, the Assign the bitmap back to the GIF.

Interesting. How would he determine which colors need to be changed
and which don't? Note that there are many different shades of browns
and greens in the image and that some of the green pixels should not be
changed.
I have used a right-click activated popup menu with "Change Color" as a menu item.
Identify the colour to be changed at the click position.
cheers,
Mat
 

Re:Replacing colors on a gif image

Clinton R. Johnson writes:
Quote
In the case of a paletted image like GIF, wouldn't it just be faster to
change the color table that preceeds the compressed data?
this routine was designed to handle any TGraphic (PNG, TIF, etc), so writing it
explicitly around each format would have been much more tedious tedious.
Quote
Incidently, if you are going to modify a 24 bit color image directly,
it is significantly faster to access the raw rgb array from Scanline
than it is to use the Pixels property (which is very, very slow in
comparison)
agreed. that section is scheduled to be replaced by Scanline.

Incidently, what GIF library are you using?
I'm using TGifImage.
cheers,
Mat
 

Re:Replacing colors on a gif image

"Mat Ballard" <XXXX@XXXXX.COM>skrev i en meddelelse
Quote
Nonsense !

The easy way is to Assign the GIF to a 8-bit bitmap, get the Colortable,
change
the brown and green entries to black, save the Colortable, the Assign the
bitmap
back to the GIF.
Nonsense!
:-)
There is need to copy data back and forth between gif and bitmap.
Just change the colour palette directly in the gif file.
See the code I posted earlier in this thread on how to do this.
--
Finn Tolderlund
 

Re:Replacing colors on a gif image

Note: This code assumes that the gif has a global colour palette and no
local colour palette.
To handle a local colour palette it would only require a couple of more
lines.
--
Finn Tolderlund
"Finn Tolderlund" <XXXX@XXXXX.COM>skrev i en meddelelse
Quote
My first try was a quick hack using a hex editor.
I felt like putting more effort into it.
Here is some quick code to do it tested in Delphi 7:

uses
GifImage; // get it from finn.mobilixnet.dk/delphi/

procedure TForm1.Button1Click(Sender: TObject);
const
BrownGreen: array [1..34] of integer = (
$1D3804,$263D08,$2F430C,$384810,$53591C,$5C5E20,$656424,$6E6928,
$776F2C,$807430,$897A34,$927F38,$9B853C,$A48A40,$AD9044,$B69548,
$BF9B4C,$BD964B,$B58F47,$AD8844,$A58140,$98753A,$906D36,$886632,
$805F2E,$735328,$6B4B24,$634421,$5B3D1D,$4D3117,$452A13,$3D220F,
$351B0B,$280F05 );
var
gif: TGifImage;
i, j: Integer;
c: TColor;
begin
gif := TGifImage.Create;
try
gif.LoadFromFile('C:\gif-test\latest_sst.gif');
for i := 0 to gif.GlobalColorMap.Count-1 do
begin
c := gif.GlobalColorMap.Colors[i];
c := GetRValue(c) shl 16 + GetGValue(c) shl 8 + GetBValue(c);
for j := Low(BrownGreen) to High(BrownGreen) do
if BrownGreen[j] = c then
begin
gif.GlobalColorMap.Colors[i] := 0;
Break;
end;
end;
gif.SaveToFile('C:\gif-test\latest_sst_new2.gif');
finally
gif.Free;
end;
end;
--
Finn Tolderlund
 

Re:Replacing colors on a gif image

Quote
>The easy way is to Assign the GIF to a 8-bit bitmap, get the
>Colortable, change the brown and green entries to black, save the
>Colortable, the Assign the bitmap back to the GIF.

Interesting. How would he determine which colors need to be changed
and which don't? Note that there are many different shades of browns
and greens in the image and that some of the green pixels should not
be changed.
The colors that need changing are mext to each other. You can easily
see which ones if you open the palette in a image editor.
 

Re:Replacing colors on a gif image

Finn,
Thank you for your response and sample code...
I will test it today. :)
Thanks again!
Bryan
"Finn Tolderlund" <XXXX@XXXXX.COM>writes:
Quote
Note: This code assumes that the gif has a global colour palette and no
local colour palette.
To handle a local colour palette it would only require a couple of more
lines.