home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / WINDOWS / LENGINE.ZIP / HDIB.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-17  |  2.1 KB  |  94 lines

  1. #include "header.h"
  2.  
  3. int HDIB::Load(char *file)
  4. {
  5.     FILE    *fp;
  6.     int        NumColors;
  7.     BYTE    *TempData;
  8.     char    *d;
  9.     int        i, j;
  10.     BYTE    Temp;
  11.     int        Rem;
  12.         
  13.     fp = fopen(file, "rb");
  14.     if(!fp)
  15.     {
  16.         MessageBeep(0);
  17.         fclose(fp);
  18.         return 0;
  19.     }
  20.  
  21. // This will be the bitmap's info
  22.     fseek(fp, sizeof(BITMAPFILEHEADER), SEEK_SET);
  23.     fread(&Info, sizeof(BITMAPINFOHEADER), 1, fp);
  24.     Rem = 4 - Info.biWidth & 3;
  25.     Info.biSizeImage = ((DWORD)(UINT)Info.biWidth * (DWORD)(UINT)Info.biHeight);
  26.     Data = (BYTE *)GlobalAlloc(GPTR, Info.biSizeImage);
  27.     TempData = new BYTE[Info.biSizeImage];
  28.     d = (char *)Data;
  29.  
  30. // This should be the palette for the bitmap
  31.     NumColors = (Info.biClrUsed == 0 ? 256 : (int)Info.biClrUsed);
  32.     fread(Palette, sizeof(RGBQUAD), NumColors, fp);
  33.     for(i = 0; i < 256; i++)
  34.     {
  35.         Temp = Palette[i].peRed;
  36.         Palette[i].peRed = Palette[i].peBlue;
  37.         Palette[i].peBlue = Temp;
  38.     }
  39.     Info.biClrUsed = NumColors;
  40.  
  41. // And this will be the rest of the bitmap data
  42.     for(i = 0; i < Info.biHeight; i++)
  43.     {
  44.         fread((BYTE *)&TempData[i * Info.biWidth], sizeof(char), (int)Info.biWidth, fp);
  45.         for(j = 0; j < Rem; j++)
  46.             fgetc(fp);
  47.     }
  48.  
  49.     for(long s = Info.biSizeImage - 1; s >= 0; s--)
  50.         *(d + s) = TempData[Info.biSizeImage - 1 - s];
  51.     delete TempData;
  52.  
  53.     TempData = new BYTE[Info.biWidth];
  54.     for(long h = 0; h < Info.biHeight; h++)
  55.     {
  56.         for(long w = 0; w < Info.biWidth; w++)
  57.             TempData[w] = *(d + h * Info.biWidth + Info.biWidth - w - 1);
  58.         for(w = 0; w < Info.biWidth; w++)
  59.             *(d + h * Info.biWidth + w) = TempData[w];
  60.     }
  61.     delete TempData;
  62.  
  63.     fclose(fp);
  64.     return 0;
  65. };
  66.  
  67. int HDIB::Free()
  68. {
  69.     GlobalFree((BYTE *)Data);
  70.     return 0;
  71. };
  72.  
  73. HPALETTE HDIB::ReturnPalette()
  74. {
  75.     LOGPALETTE    *LogPal;
  76.     HPALETTE    Pal;
  77.     int            i;
  78.     
  79.     LogPal = (LOGPALETTE *)LocalAlloc(LPTR, sizeof(LOGPALETTE) + 236 * sizeof(PALETTEENTRY));
  80.     LogPal->palNumEntries = 236;
  81.     LogPal->palVersion = 0x300;
  82.     
  83.     for(i = 0; i < 236; i++)
  84.     {
  85.         LogPal->palPalEntry[i] = Palette[i + 10];
  86.         LogPal->palPalEntry[i].peFlags = PC_RESERVED;
  87.     }
  88.     
  89.     Pal = CreatePalette((LOGPALETTE *)LogPal);
  90.     LocalFree((LOGPALETTE *)LogPal);
  91.     return(Pal);
  92. };
  93.  
  94.