Click here to Skip to main content
15,509,322 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Is any algorithm available for converting palette image buffer(including bitmap information) to RGB buffer. c++
Updated 26-Sep-12 20:03pm
Sergey Alexandrovich Kryukov 27-Sep-12 1:31am    
Yes. This is trivial, so I doubt about finding the description of the algorithm, etc. I would better write it from scratch if your library does not do it out-of-the-box (but I have no idea what do you used, what is data format, etc). Besides, you are asking about algorithm...

There is no such thing because the task itself is too trivial. I don't say it has to be easy though; one issue is performance. It all depends on library and data format you use.

Once you know the format and have access to input and output buffer, you just 1) read pixels one by one, 2) get palette value, 3) for each pixel, lookup for the color in palette table (whatever format it is), 4) calculate RGB value for an output pixel, 5) write to output.

Call it "algorithm" if you want :-).

How you create output buffer and access input buffer, how you compress data (is so required) — it all depends on the imaging library you use, support of bitmap types, etc. Most libraries have something like "GetPixel"/"SetPixel" methods, but, chances are, they are prohibitively slow. You need to access the buffers and use pointers.

Share this answer
Ajesh Kumar.T.T 27-Sep-12 2:53am    
This is one time consuming process. Actualy I have a buffer which contains BitmapInfo + pixel data. this pixel data is in the palette format. Bitmapinfo structure contains color table. This can be solved by using LockBits method but I don't know how to use LockBits method for converting buffer to buffer. I found a lot of such conversions, but they are mainly processed one input file.
Sergey Alexandrovich Kryukov 27-Sep-12 12:12pm    
To talk about LockBits, you should have told us that you are using System.Drawing, but you did not. Now I see. Yes, you should use LockBits, nothing else, I would say. And the algorithm itself is still trivial. Any questions about that?
Ajesh Kumar.T.T 28-Sep-12 0:19am    
I am using GDI plus for conversion, and I tried the following code. But this does not work.

INT nNumColors = 256;
UINT uHeaderSize = nNumColors * sizeof( RGBQUAD ) + stBitmapInfo.bmiHeader.biSize;
INT nPaletteSize = nNumColors * sizeof( RGBQUAD );

// Bitmap Info Header
BYTE* pbyBMInfo = 0;
ITF_NEW_ARRAY( pbyBMInfo, BYTE, uHeaderSize );
ZeroMemory( pbyBMInfo, uHeaderSize );

// Fill Header
LPBITMAPINFO pstBMInfo = reinterpret_cast<lpbitmapinfo>( pbyBMInfo );
memcpy( pstBMInfo, &stBitmapInfo, sizeof( BITMAPINFOHEADER ));

RGBQUAD* pRGB = new RGBQUAD[nNumColors];
// Color table filled in pRGB.
BYTE* pbyPixelData = new BYTE[width * height];
// Pixel data filled in pbyPixelData.
Gdiplus::BitmapData RGBData;
memcpy( pstBMInfo + sizeof( BITMAPINFOHEADER ), pRGB, nPaletteSize );
pBitmapImg = new Gdiplus::Bitmap( pstBMInfo, pbyPixelData );
Gdiplus::Rect ImageRect( 0, 0, width, height );
Gdiplus::Status eRetStatus = pBitmapImg->LockBits( &ImageRect, Gdiplus::ImageLockModeRead,
PixelFormat24bppRGB, &RGBData );
if( Gdiplus::Ok != eRetStatus )
pBitmapImg->UnlockBits( &RGBData );
// Error case. Return
BYTE* pbyRgbBuffer = new BYTE[RGBData.Height * RGBData.Width * 3];
BYTE* pbyConvertedBuffer = reinterpret_cast<byte*>( RGBData.Scan0 );
for( int nRowIndex = 0; nRowIndex < RGBData.Height; ++nRowIndex )
int nNewBufferRowPos = nRowIndex * RGBData.Width * 3;
int nRGBRowPos = nRowIndex * RGBData.Stride;
memcpy( pbyRgbBuffer + nNewBufferRowPos, pbyConvertedBuffer + nRGBRowPos, RGBData.Width * 3 );
ULONG ulPixelDataSize = RGBData.Height * RGBData.Width * 3;
BYTE byNewSamplesPerPixel = 3;
pBitmapImg->UnlockBits( &RGBData );
I solved this problem as shown below:

ULONG ulPixelDataSize = Height * Width * 3;
RGBQUAD* pRGB = new RGBQUAD[nNumColors];// Color table filled in pRGB.
BYTE* pbyPixelData = new BYTE[width * height];// Pixel data filled in pbyPixelData.

BYTE byNewSamplesPerPixel = 3;
BYTE* pbyRgbBuffer = new BYTE[ulPixelDataSize];
for( int nBufferIndex = 0; nBufferIndex < Height * Width; ++nBufferIndex )
    int nColouIndex = pbyPixelData[nBufferIndex];
    memcpy( pbyRgbBuffer + ( nBufferIndex * byNewSamplesPerPixel ), reinterpret_cast<BYTE*>( &( pRGB[nColouIndex] )), byNewSamplesPerPixel );

Thank you for the support and valuable comments.
It will be better if this problem can be resolved with higher performance than this one.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900