Re: Pixel format conversion



> Hi,
>
> I want to convert one pixel format to another pixel format. For example
> 8bit raw pixel data to 16 bit raw...16 to 24...32 to 16.....etc....
>
> -What's the best way and fast to do this conversion?
> -I referred few references and find out something like this...
>
> from 32bit RGB to 8bit RGB is ( (((pixel) & 0xE00000) >> 16) | \
> (((pixel) & 0x00E000) >> 11) | \
> (((pixel) & 0x0000C0) >> 6) )
>
> and from 8bit RGB to 16 RGB is
> ( (((pixel) & 0xF80000) >> 8) | \
> (((pixel) & 0x00FC00) >> 5) | \
> (((pixel) & 0x0000F8) >> 3) )
>
> But I am not sure how its derived!!! thanks for any explation?
>
> -Is there any generic way to derive such calculation/formula? Where can
> I find find more info about it?
>
> Thanks...
>
> GG
>

It works like this...
Say your source format is 24bpp and it's in the format R8 G8 B8
(rrrrrrrrggggggggbbbbbbbb).
and your destination format is 16bpp and it's in the format R5 G6 B5
(rrrrrggggggbbbbb).

First, do the red:
Extract the 5 most significant red bits by ANDing with binary
111110000000000000000000 (&F80000)
then shift it 8 places to the right so the bits are in the right position
for the 16bpp format.
Eg. RedBits = (Col24 & F80000) >> 8

Do the same for Blue and Green and then OR (or just add) the values together
to get your 16bpp colour value.

It's pretty much the same going the other way round. You extract the bits by
ORing, and then shift them so they cover the most significant (i.e. left
most) bits for that colour in your destination format.

The conversion is very fast as bitwise ANDs and shifts are very fast
operations.

Hope that makes sense. One thing to note is that 8bpp formats are sometimes
palettised, so the values are indexes into an RGB colour table. To convert
to/from a palettised 8bpp format, you'd need to do colour matching.




.



Relevant Pages

  • Re: Representing image data
    ... any application that uses raster image data. ... row and store it in a format appropriate for the application. ... With ImageMagick the only way to change the pixel format is to ... interface to return a pointer to an array of pixels; ...
    (comp.lang.ada)
  • Re: Problem with DirectX6 -> DDSURFACEDESC2 structure and lpSurface
    ... > image file format into the lpSurface. ... Writing raw 24-bit RGB data to the surface pointer only ... surface with "RGB565" pixel format; note that other formats are also ...
    (microsoft.public.win32.programmer.directx.graphics)
  • Re: colors are slightly off importing gifs and bmps
    ... Since the problem is clearly in pixel format not matching what you have on ... >> If you tried to bit shift each pixel in your image, on demand, then this ... >> Tim Wilson ...
    (microsoft.public.dotnet.framework.compactframework)
  • RE: How to copy YUV planar formats data(YV12,I420,YVU9 ) to Direct
    ... Sir, It's true, to transform Planer into Packed format when DirectDraw ... Planer YUV pixel format DirectDraw surface. ...
    (microsoft.public.win32.programmer.directx.video)
  • Re: Failed to connect Video Renderer Filter in WinCE 4.2
    ... I have appended an RGB format and then it works under ... > The video renderer only handles GDI-supported formats at type-negotiation ... > What you need to do is expose an RGB format in addition to the YUV ... >> I have implemented an customized video decoder filter for WindowsCE.Net ...
    (microsoft.public.windowsce.embedded)