bitmap.h
Upload User: xuczgm
Upload Date: 2022-04-25
Package Size: 8601k
Code Size: 4k
Category:

Special Effects

Development Platform:

Visual C++

  1. #include <stdlib.h>
  2. #include <windows.h>
  3. #include <stdio.h>
  4. #define BITMAP_ID   0x4D42    // the universal bitmap ID
  5. /*****************************************************************************
  6.  LoadBitmapFile()
  7.  Returns a pointer to the bitmap image of the bitmap specified by filename.
  8.  Also returns the bitmap header information. No support for 8-bit bitmaps.
  9. *****************************************************************************/
  10. unsigned char *LoadBitmapFile(char *filename, BITMAPINFOHEADER *bitmapInfoHeader)
  11. {
  12.   FILE *filePtr;                        // the file pointer
  13.   BITMAPFILEHEADER  bitmapFileHeader;   // bitmap file header
  14.   unsigned char    *bitmapImage;        // bitmap image data
  15.   unsigned int      imageIdx = 0;       // image index counter
  16.   unsigned char     tempRGB;            // swap variable
  17.   // open filename in "read binary" mode
  18.   filePtr = fopen(filename, "rb");
  19.   if (filePtr == NULL)
  20.     return NULL;
  21.   // read the bitmap file header
  22.   fread(&bitmapFileHeader, sizeof(BITMAPFILEHEADER), 1, filePtr);
  23.   // verify that this is a bitmap by checking for the universal bitmap id
  24.   if (bitmapFileHeader.bfType != BITMAP_ID)
  25.   {
  26.     fclose(filePtr);
  27.     return NULL;
  28.   }
  29.   // read the bitmap information header
  30.   fread(bitmapInfoHeader, sizeof(BITMAPINFOHEADER), 1, filePtr);
  31.   // move file pointer to beginning of bitmap data
  32.   fseek(filePtr, bitmapFileHeader.bfOffBits, SEEK_SET);
  33.   // allocate enough memory for the bitmap image data
  34.   bitmapImage = (unsigned char*)malloc(bitmapInfoHeader->biSizeImage);
  35.   // verify memory allocation
  36.   if (!bitmapImage)
  37.   {
  38.     free(bitmapImage);
  39.     fclose(filePtr);
  40.     return NULL;
  41.   }
  42.   // read in the bitmap image data
  43.   fread(bitmapImage, 1, bitmapInfoHeader->biSizeImage, filePtr);
  44.   // make sure bitmap image data was read
  45.   if (bitmapImage == NULL)
  46.   {
  47.     fclose(filePtr);
  48.     return NULL;
  49.   }
  50.   // swap the R and B values to get RGB since the bitmap color format is in BGR
  51.   for (imageIdx = 0; imageIdx < bitmapInfoHeader->biSizeImage; imageIdx+=3)
  52.   {
  53.     tempRGB = bitmapImage[imageIdx];
  54.     bitmapImage[imageIdx] = bitmapImage[imageIdx + 2];
  55.     bitmapImage[imageIdx + 2] = tempRGB;
  56.   }
  57.   // close the file and return the bitmap image data
  58.   fclose(filePtr);
  59.   return bitmapImage;
  60. } // end LoadBitmapFile()
  61. /*****************************************************************************
  62.  LoadBitmapFileWithAlpha
  63.  Loads a bitmap file normally, and then adds an alpha component to use for
  64.  blending
  65. *****************************************************************************/
  66. unsigned char *LoadBitmapFileWithAlpha(char *filename, BITMAPINFOHEADER *bitmapInfoHeader)
  67. {
  68.   unsigned char *bitmapImage = LoadBitmapFile(filename, bitmapInfoHeader);
  69.   unsigned char *bitmapWithAlpha = (unsigned char *)malloc(bitmapInfoHeader->biSizeImage * 4 / 3);
  70.   if (bitmapImage == NULL || bitmapWithAlpha == NULL)
  71.     return NULL;
  72.   // loop through the bitmap data
  73.   for (unsigned int src = 0, dst = 0; src < bitmapInfoHeader->biSizeImage; src +=3, dst +=4)
  74.   {
  75.     // if the pixel is black, set the alpha to 0. Otherwise, set it to 255.
  76.     if (bitmapImage[src] == 0 && bitmapImage[src+1] == 0 && bitmapImage[src+2] == 0)
  77.       bitmapWithAlpha[dst+3] = 0;
  78.     else
  79.       bitmapWithAlpha[dst+3] = 0xFF;
  80.     // copy pixel data over
  81.     bitmapWithAlpha[dst] = bitmapImage[src];
  82.     bitmapWithAlpha[dst+1] = bitmapImage[src+1];
  83.     bitmapWithAlpha[dst+2] = bitmapImage[src+2];
  84.   }
  85. // free(bitmapImage);
  86.   return bitmapWithAlpha;
  87. } // end LoadBitmapFileWithAlpha()