Link to home
Start Free TrialLog in
Avatar of Simonhaner
Simonhaner

asked on

codes for convert 16/24/32 bit bitmap to 256 color

Hi ,

I want to convert 16/24/32 bit bitmap to 256 color or grayscale . If anyone can give me a piece of code or address the forum ?

Thanks & Best Regards !
 


Avatar of MichaelS
MichaelS

ASKER CERTIFIED SOLUTION
Avatar of lyonst
lyonst
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Simonhaner

ASKER

Hi ,

thanks for your help .

I've wrote a piece of code but the right of image was move to the left in most photo apps( if i open the saved image , ms painter can display it correctly while others can not)
 
i paste the codes here and hope u can give me a hand .

in the follow codes "buffer" is an array contains grayed pixel .  


BITMAPINFO bmpInfo2;
bmpInfo2.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpInfo2.bmiHeader.biWidth              = 800;
bmpInfo2.bmiHeader.biHeight             = 600;
bmpInfo2.bmiHeader.biPlanes             = 1;
bmpInfo2.bmiHeader.biBitCount           = 8;
bmpInfo2.bmiHeader.biCompression        = BI_RGB;
bmpInfo2.bmiHeader.biXPelsPerMeter      = 0;
bmpInfo2.bmiHeader.biYPelsPerMeter      = 0;
bmpInfo2.bmiHeader.biClrUsed            = 256;
bmpInfo2.bmiHeader.biClrImportant       = 0;
bmpInfo2.bmiHeader.biSizeImage          = 800*600 ;
 
   RGBQUAD pal[256] ;
   for(int a =0 ;a < 256; a++ )
   {
   pal[a].rgbBlue = pal[a].rgbGreen=pal[a].rgbRed = a ;
   pal[a].rgbReserved = 0 ;
   
   }
 
   CFile file ;
   file.Open ("c:\\bmp",CFile::modeCreate |    CFile::modeWrite,NULL);

// z is begin with "BM8" z[4] = 0x24 z[10] = 0x36;

   file.Write(z,14);
   file.Write((char*)&bmpInfo2,sizeof(BITMAPINFO)-4);
   file.Write((char*)pal,sizeof(RGBQUAD)*256);
   file.Write((char*)buffer,800*600);
   file.Close();



Thanks & b.Regards !

Hi ,

it's strange after i modify some codes the image is ok now
but anyone can tell me why ?

  char z[14];
   ZeroMemory(z,14);
   strcpy(z,"BM");
   z[3] = 0x36;
   z[4] = 0x57;
   z[10] = 0x36;
   z[11] = 0x4 ;

.........

thanks !
I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. Unless there is objection or further activity,  I will suggest to accept "lyonst" comment(s) as an answer.

If you think your question was not answered at all, you can post a request in Community support (please include this link) to refund your points.
The link to the Community Support area is: https://www.experts-exchange.com/commspt

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
Per recommendation, force-accepted by
Netminder
CS Moderator