Board index » delphi » Converting Bitmap to 16/256 colors or grayscale

Converting Bitmap to 16/256 colors or grayscale

Does anybody know how to convert pallette of TBitmap loaded from file
to :
 - 16 or 256 colors
 -  grayscale levels ?
  I'm using LoadFromFile method.
   I want to be able to load all formats of bmp file,  but I need to do some
conversion, cause I operate on images with those numbers of colors only.
Oh and one more thing. I'm writing in C++ (Builder), but I suppose in Delphi
definition of class Graphics::TBitmap is similiar, so maybe someone would
know how to do it.

                    Thanks
                                  juppie

************************************
"Things that you can't have make
you happier than things you own.
Don't lose your dreams"

 

Re:Converting Bitmap to 16/256 colors or grayscale


See http://www.efg2.com/Lab/Library/Delphi/Graphics/Color.htm on Color
Reduction.

"juppie" <jup...@wp.pl> schreef in bericht
news:91a99q$j26$1@sunflower.man.poznan.pl...

Quote
> Does anybody know how to convert pallette of TBitmap loaded from file
> to :
>  - 16 or 256 colors
>  -  grayscale levels ?
>   I'm using LoadFromFile method.
>    I want to be able to load all formats of bmp file,  but I need to do
some
> conversion, cause I operate on images with those numbers of colors only.
> Oh and one more thing. I'm writing in C++ (Builder), but I suppose in
Delphi
> definition of class Graphics::TBitmap is similiar, so maybe someone would
> know how to do it.

>                     Thanks
>                                   juppie

> ************************************
> "Things that you can't have make
> you happier than things you own.
> Don't lose your dreams"

Re:Converting Bitmap to 16/256 colors or grayscale


Quote
> Does anybody know how to convert pallette of TBitmap loaded from file
> to :
>  - 16 or 256 colors
>  -  grayscale levels ?

procedure TForm1.Button1Click(Sender: TObject);
var
  lo:tbitmap;
  i,j:integer;
  kl:longint;
  rr,gg,bb:byte;
  res:byte;
begin
  lo:=tbitmap.create;
  lo.Width:=image1.Width;
  lo.height:=image1.height;
  ProgressBar1.Max:=image1.Width+1;
  for i:=0 to image1.Width+1 do
  begin
    for j:=0 to image1.height+1 do
    begin
      kl:=ColorToRGB(image1.Canvas.Pixels[i,j]);
      rr:=byte(kl);
      gg:=byte(kl shr 8);
      bb:=byte(kl shr 8);
      res:=(rr+gg+bb) div 3;
      lo.Canvas.Pixels[i,j]:=rgb(res,res,res);
    end;
    ProgressBar1.Position:=i;
  end;//for do
  image1.Canvas.Draw(0,0,lo);
  lo.free;
end;

Other Threads