Camaradas, 

Estou desenvolvento um software para o meu mestrado e nele preciso saber quais 
as cores que estão sendo usadas em um bitmap. Achei várias rotinas na internet, 
mais nenhuma funcionou. Depois de descobrir quais as cores que estão sendo 
usadas (que são chamadas de importantes) eu preciso substituir a cor do bitmap 
por outra.
Atualmente eu estou varrendo o bitmap pixel a pixel e carregando um array 
TColor. Porém isto fica muuuuuuiiiiiittttooo lento por que os bitmaps são 
grandes. Os Bitmaps que eu estou usando são de 16 e 256 cores.
A rotina que eu achei que "quase" funcionou, pois dá algumas informações como 
largura, altura profundidade, tamanho,...e teóricamente a quantidade de cores 
totais e a quantidade de cores importantes é a que segue abaixo deste texto, 
porém ela está retornando as cores importantes como 0 e além de saber quantas 
são preciso saber quais são. Camaradas preciso muito disto para fazer o meu 
programa. Desde já agradeço. 


Para rodar é necessário:2 TMemo, 1 TOpenPictureDialog, 1 TImage e um TButton 
para disparar a trolha.

procedure TForm1.Button1Click(Sender: TObject);
var f: file;  idstr:string[2];  Bitmap : TBitmap; y, i, rowlength : integer;
        pb : Pointer;
begin
   if OpenPictureDialog1.Execute then
      begin
         AssignFile(f, OpenPictureDialog1.FileName);
         Reset(f, 1);  idstr := '';
         {a bitmap file starts with the id 'BM'}
         BlockRead(f, AChar, 1, amt);
         idstr := Achar;
         BlockRead(f, Achar, 1, amt);
         idstr := idstr + Achar;
         If idstr <> 'BM' then
            begin
               MessageDlg('The file is not a valid bitmap', mterror, [mbok],0);
               CloseFile(f);
               Exit;
            end
         {end if};
         {read the file header info}
         BlockRead(f, Alongint, 4, amt);
         BitmapFileHeader.bmfFileSize := Alongint;
         BlockRead(f, Alongint, 4, amt);
         BitmapFileHeader.bmfReserved := Alongint;
         BlockRead(f, Alongint, 4, amt);
         BitmapFileHeader.bmfBitMapDataOffset := Alongint;
         {read the bitmap info header}
         BlockRead(f, Alongint, 4, amt);
         BitmapInfoHeader.biSize := Alongint; {size of header itself}
         BlockRead(f, Alongint, 4, amt);
         BitmapInfoHeader.biWidth := Alongint;
         BlockRead(f, Alongint, 4, amt);
         BitmapInfoHeader.biHeight :=  Alongint;
         Blockread(f, AWord, 2, amt);
         BitmapInfoHeader.biPlanes := Aword;
         Blockread(f, AWord, 2, amt);
         BitmapInfoHeader.biBitCount := Aword;  {bits per pixel}
         BlockRead(f, Alongint, 4, amt);
         BitmapInfoHeader.biCompression := Alongint;
         BlockRead(f, Alongint, 4, amt);
         BitmapInfoHeader.biSizeImage := Alongint;
         BlockRead(f, Alongint, 4, amt);
         BitmapInfoHeader.biXPelsPerMeter := Alongint;
         BlockRead(f, Alongint, 4, amt);
         BitmapInfoHeader.biYPelsPerMeter := Alongint;
         BlockRead(f, Alongint, 4, amt);
         BitmapInfoHeader.biClrUsed := Alongint;
         BlockRead(f, Alongint, 4, amt);
         BitmapInfoHeader.biClrImportant := Alongint;
         Bitmap := TBitmap.Create;
         Bitmap.Width := BitmapInfoHeader.biWidth ;
         Bitmap.Height := BitmapInfoHeader.biHeight ;
         case BitMapInfoHeader.biBitCount of
                1  : Bitmap.PixelFormat := pf1bit;
                4  : Bitmap.PixelFormat := pf4bit;
                8  : Bitmap.PixelFormat := pf8bit;
                15 : Bitmap.PixelFormat := pf15bit;
                16 : Bitmap.PixelFormat := pf16bit;
                24 : Bitmap.PixelFormat := pf24bit;
                32 : Bitmap.PixelFormat := pf32bit;
        {   : XORBitmap.PixelFormat := pfCustom;}
        end;

        {get the color palette}
         If BitmapInfoHeader.biClrUsed <= 256 then
            begin
               palcount := BitmapInfoHeader.biClrUsed ;
               GetMem(BMPalette, sizeof(TLogPalette)
                       + sizeof(TPaletteEntry)
                       * palcount);
               BMPalette.palVersion := $300;
               BMPalette.palNumEntries := palcount;
           {reading in a RGB table red first
                results in a backwards colored bitmap
               and so this code  reads in the blue first}
               for i := 0 to palcount - 1 do
                  begin
                     BlockRead(f, BlueByte, 1, Amt);
                     BlockRead(f, GreenByte, 1, Amt);
                     BlockRead(f, RedByte, 1, Amt);
                     BlockRead(f, AByte, 1, Amt); {The flag byte}
                     BMPalette.palPalEntry[i].peBlue := BlueByte;
                     BMPalette.palPalEntry[i].peGreen := GreenByte;
                     BMPalette.palPalEntry[i].peRed := RedByte;
                     BMPalette.palPalEntry[i].peFlags := Abyte;
                  end
               {end for};
               BMhPalette := CreatePalette(BMPalette^);
               If (BMhPalette <> 0) and (BitMapInfoHeader.biBitCount <= 8) then
                  Bitmap.Palette := BMhPalette;
         end; {if color count <= 256}
         {get the pixel data of the bitmap}
         Rowlength := (BitmapInfoHeader.biWidth
                        * BitmapInfoHeader.biBitCount) div 8;
         {pixel data is stored upside down, so fill bitmap from bottom up}
         for y := BitmapInfoHeader.biHeight - 1 downto 0 do begin
                pb := BitMap.ScanLine[y];  {get ^ address of first byte on row}
                BlockRead(f, pb^, RowLength, amt);
         end;
         CloseFile(f);
         Image1.Picture.Bitmap := Bitmap;
         WriteBitmapInfoToMemo;
     end;
end;

__________________________________________________
Fale com seus amigos  de graça com o novo Yahoo! Messenger 
http://br.messenger.yahoo.com/ 

[As partes desta mensagem que não continham texto foram removidas]

Responder a