2008. december 31., szerda

Convert 8 or 16 bit color images to 32 bit


Problem/Question/Abstract:

How do I convert a 8 bit color image or a 16 bit color image to 32 bits of color? In my case, I have files with 8bit or 16bit images which I read using scanline. So I have one or two bytes determining the pixel color. What I want to do is still read one or two bytes for each pixel, for either the 8bit or the 16bit, but convert it to 32 bits on display.

Answer:

I think you mean 8 or 16 bit per pixel, not 8 or 16 per sample. You must divide the 8 bit and the 16 bit. 8 bit is a indexed image (palette image). The pixel value is an index in a color table (palette). You need a pointer to this color table and get the color from this table. 16 bit is an RGB image, the pixel directly contains the color data. Unfortunately, you must determine the pixel mask. On Win9x the data can be 5 bit blue, 6 bit green, 5 bit red or 5 bit blue, 5 bit green, 5 bit red (bit 15 is unused). On WinNT or later the data can be stored in other combinations, but this is unlikely.

From a own project to convert such pixel data:

function DIB555ToBGR(Value: DWord): DWord; assembler;
asm
  {blue}
  mov   ecx, eax
  shl   eax, 3
  mov   edx, eax
  and   eax, $0000F8
  shr   ecx, 2
  and   ecx, $000007
  or    eax, ecx
  {green}
  shl   edx, 3
  mov   ecx, edx
  and   edx, $00F800
  or    eax, edx
  shr   edx, 5
  and   edx, $000700
  or    eax, edx
  {red}
  shl   ecx, 3
  and   ecx, $F80000
  or    eax, ecx
  shr   ecx, 5
  and   ecx, $070000
  or    eax, ecx
end;

function DIB565ToBGR(Value: DWord): DWord; assembler;
asm
  {blue}
  mov   ecx, eax
  shl   eax, 3
  mov   edx, eax
  and   eax, $0000F8
  shr   ecx, 2
  and   ecx, $000007
  or    eax, ecx
  {green}
  shl   edx, 2
  mov   ecx, edx
  and   edx, $00FC00
  or    eax, edx
  shr   edx, 6
  and   edx, $000300
  or    eax, edx
  {red}
  shl   ecx, 3
  and   ecx, $F80000
  or    eax, ecx
  shr   ecx, 5
  and   ecx, $070000
  or    eax, ecx
end;

However, I think you should simple use TBitmap.PixelFormat:

PixelFormat := pf32Bit;

This sets the bitmap to 32 Bit.

On the other side, you can directly display the 8 or 16 bit bitmap, the WinAPI convert the bitmap to the correct format.

Nincsenek megjegyzések:

Megjegyzés küldése