going from 24bit color to 16bit color

Started by
4 comments, last by Baine 22 years, 9 months ago
Howdy.. I''m working through a book that i picked up recently.. "Windows Game Programming for Dummies" By Andre LaMothe.. anywho.. in Chapter 10 it''s explain how to load fullscreen bitmaps, and the source code on the cd is supposed to demonstrate that. Now.. when you compile and run the program, (Prog10_5.cpp for anybody that has read this book) when you give it a 24bit bitmap to display, it corrupts the color scheme horribly. Almost completely green. Now when i checked the source i noticed that if the bitmap of the given file is 24bit, the program automatically converts it to 16bit. I don''t know if the bitmap was supposed to be displayed in green, but somehow i don''t think so. If you just load up the source .bmp in paint or whatever it looks fine.. so I guess what i''m asking is, can anybody help me out with the following algorithm? I think the problem is that it''s not addressing the red components of the bitmap properly but i could be way off. I understand the indexing of the loop, but the >> shift right operator has me a little confused. More than likely due to my less-than-adequate knowledge of how these colors, (8/16/32bit), are built up. any advice would be helpful. ///////////////////////////////////////////////////////////////// // now convert each 24 bit RGB value into a 16 bit value for (index=0; index < bitmap->bitmapinfoheader.biWidth* bitmap->bitmapinfoheader.biHeight; index++) { // extract RGB components (in BGR order), note the scaling UCHAR blue = (temp_buffer[index*3 + 0] >> 3), green = (temp_buffer[index*3 + 1] >> 3), red = (temp_buffer[index*3 + 2] >> 3); // build up 16 bit color word USHORT color = _RGB16BIT(red,green,blue); // write color to buffer ((USHORT *)bitmap->buffer)[index] = color; } // end for index //////////////////////////////////////////////////////////////// temp_buffer is a temporary placeholder for the 24bit bitmap, memory allocated accordingly bitmap->buffer is the actual working bitmap structure.. I''m guessing that the r,g,b components of temp_buffer must be copied here. _RGB16BIT is a macro designed to build up a 16 bit color hmm.. if you could point me toward a site that could help me understand how these colors are built up, and how to index through them properly.. that would probably be the best solution, but thanks for any advice groovy ern.
groovy,ern
Advertisement
If i remember correctly (i read that book a very long time ago) the a. lamothe gives instructions in the extras / something like that folder on the cd. I remember that his code was only ready to load 8 - bit bitmaps. On his site or somewhere on the cd, he gives an article on loading 24/16 bit bitmaps. good luck
masterghttp:/masterg.andyc.org
Yeah only problem is that in LaMothe''s book it only works for 565 video cards not 555. I should have some code to do it correctly. I''ll post it when I get home.



...A CRPG in development...

Need help? Well, go FAQ yourself.
Need help? Well, go FAQ yourself. "Just don't look at the hole." -- Unspoken_Magi
LaMothe has 16 bit versions of all the functions on the CD but this should replace LaMothe's Load_Bitmap_File16() because his only works on 565 video cards, not 555. All his other functions should work though...


    int Load_Bitmap_File(BITMAP_FILE_PTR bitmap, char *filename){// this function opens a bitmap file and loads the data into bitmapint file_handle,  // the file handle    index;        // looping indexUCHAR   *temp_buffer = NULL; // used to convert 24 bit images to 16 bitOFSTRUCT file_data;          // the file data information// open the file if it existsif ((file_handle = OpenFile(filename,&file_data,OF_READ))==-1)   return(0);// now load the bitmap file header_lread(file_handle, &bitmap->bitmapfileheader,sizeof(BITMAPFILEHEADER));// test if this is a bitmap fileif (bitmap->bitmapfileheader.bfType!=BITMAP_ID)   {   // close the file   _lclose(file_handle);   // return error   return(0);   } // end if// now we know this is a bitmap, so read in all the sections// first the bitmap infoheader// now load the bitmap file header_lread(file_handle, &bitmap->bitmapinfoheader,sizeof(BITMAPINFOHEADER));// now load the color palette if there is oneif (bitmap->bitmapinfoheader.biBitCount == 8)   {   _lread(file_handle, &bitmap->palette,MAX_COLORS_PALETTE*sizeof(PALETTEENTRY));   // now set all the flags in the palette correctly and fix the reversed    // BGR RGBQUAD data format   for (index=0; index < MAX_COLORS_PALETTE; index++)       {       // reverse the red and green fields       int temp_color                = bitmap->palette[index].peRed;       bitmap->palette[index].peRed  = bitmap->palette[index].peBlue;       bitmap->palette[index].peBlue = temp_color;              // always set the flags word to this       bitmap->palette[index].peFlags = PC_NOCOLLAPSE;       } // end for index    } // end if// finally the image data itself_lseek(file_handle,-(int)(bitmap->bitmapinfoheader.biSizeImage),SEEK_END);// now read in the imageif (bitmap->bitmapinfoheader.biBitCount==8 || bitmap->bitmapinfoheader.biBitCount==16)    {   // delete the last image if there was one   if (bitmap->buffer)       free(bitmap->buffer);   // allocate the memory for the image   if (!(bitmap->buffer = (UCHAR *)malloc(bitmap->bitmapinfoheader.biSizeImage)))      {      // close the file      _lclose(file_handle);      // return error      return(0);      } // end if   // now read it in   _lread(file_handle,bitmap->buffer,bitmap->bitmapinfoheader.biSizeImage);   } // end ifelseif (bitmap->bitmapinfoheader.biBitCount==24)   {   // allocate temporary buffer to load 24 bit image   if (!(temp_buffer = (UCHAR *)malloc(bitmap->bitmapinfoheader.biSizeImage)))      {      // close the file      _lclose(file_handle);      // return error      return(0);      } // end if      // allocate final 16 bit storage buffer   if (!(bitmap->buffer=(UCHAR *)malloc(2*bitmap->bitmapinfoheader.biWidth*bitmap->bitmapinfoheader.biHeight)))      {      // close the file      _lclose(file_handle);      // release working buffer      free(temp_buffer);      // return error      return(0);      } // end if   // now read the file in   _lread(file_handle,temp_buffer,bitmap->bitmapinfoheader.biSizeImage);   // now convert each 24 bit RGB value into a 16 bit value   for (index=0; index < bitmap->bitmapinfoheader.biWidth*bitmap->bitmapinfoheader.biHeight; index++)       {       // build up 16 bit color word       USHORT color;              // build pixel based on format of directdraw surface       if (dd_pixel_format==DD_PIXEL_FORMAT555)           {		   pixform=555;           // extract RGB components (in BGR order), note the scaling           USHORT blue  = (USHORT)(temp_buffer[index*3 + 0]),                  green = (USHORT)(temp_buffer[index*3 + 1]),                  red   = (USHORT)(temp_buffer[index*3 + 2]);           		  blue = blue & 248;           	      blue = blue >> 3;           	      green = green & 248;           	      green = green << 2;           	      red = red & 248;           	      red = red << 7;           	      color = red | green | blue;           } // end if 555       else       if (dd_pixel_format==DD_PIXEL_FORMAT565)           {		   pixform=565;          // extract RGB components (in BGR order), note the scaling           UCHAR blue  = (temp_buffer[index*3 + 0] >> 3),                 green = (temp_buffer[index*3 + 1] >> 3),                 red   = (temp_buffer[index*3 + 2] >> 3);           color=_RGB16BIT565(red,green,blue);          } // end if 565       // write color to buffer       ((USHORT *)bitmap->buffer)[index] = color;       } // end for index   // finally write out the correct number of bits   bitmap->bitmapinfoheader.biBitCount=16;   // release working buffer   free(temp_buffer);   } // end if 24 bitelse   {   // serious problem   return(0);   } // end else#if 0// write the file info out printf("\nfilename:%s \nsize=%d \nwidth=%d \nheight=%d \nbitsperpixel=%d \ncolors=%d \nimpcolors=%d",        filename,        bitmap->bitmapinfoheader.biSizeImage,        bitmap->bitmapinfoheader.biWidth,        bitmap->bitmapinfoheader.biHeight,		bitmap->bitmapinfoheader.biBitCount,        bitmap->bitmapinfoheader.biClrUsed,        bitmap->bitmapinfoheader.biClrImportant);#endif// close the file_lclose(file_handle);// flip the bitmapFlip_Bitmap(bitmap->buffer,             bitmap->bitmapinfoheader.biWidth*(bitmap->bitmapinfoheader.biBitCount/8),             bitmap->bitmapinfoheader.biHeight);// return successreturn(1);} // end Load_Bitmap_File    




...A CRPG in development...

Need help? Well, go FAQ yourself.


Edited by - Nazrix on July 12, 2001 1:54:01 AM
Need help? Well, go FAQ yourself. "Just don't look at the hole." -- Unspoken_Magi
hey thanks a lot for that.

i''ll give it a try

groovy,
ern.
groovy,ern
NP...and if you have any problems just say so and someone smarter than I will answer



...A CRPG in development...

Need help? Well, go FAQ yourself.
Need help? Well, go FAQ yourself. "Just don't look at the hole." -- Unspoken_Magi

This topic is closed to new replies.

Advertisement