🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.os.ms-windows.programmer.tools
Article #290

Re: Trying to read in 256 color .BMP file

#290
From: George Mills
Date: Sun, 13 Sep 1992 00:48
101 lines
3489 bytes
If some sharp windows programmer could tell me what I'm doing wrong here
it would be greatly appreciated.

I'm using Turbo C++ for windows and this is actually a derivative of
their example. If someone could email some of the SDK examples of reading
in a .BMP that would be very helpful. The function reads in the bits of
a .BMP and trys to "set" them into an existing DIB.

Should I create the secondary DIB and do a GETDIBITS and then set them
in the backing store DIB (this seemed redundant since I already have
the "bits" directly from the .BMP.

BOOL TMyWindow::OpenDIB(int TheFile)
   {
   WORD bitCount;
   WORD size;
   long longWidth;
   LPSTR BitsPtr;
   BITMAPINFO *BitmapInfo;
   HBITMAP BitsHandle;
   DWORD NewPixelWidth , NewPixelHeight;
   BITMAPFILEHEADER BitmapFileHeader;
   int i;
   
   /* read header stuff of .bmp */

   _llseek(TheFile, 0, 0);
   _lread(TheFile, (LPSTR)&BitmapFileHeader, sizeof(BitmapFileHeader));
   
   _llseek(TheFile, 28, 0);
   _lread(TheFile, (LPSTR)&bitCount, sizeof(bitCount));
   
   if (!(bitCount <= 8)) return FALSE;
   
   size = sizeof(BITMAPINFOHEADER) + ((1 << bitCount) * sizeof(RGBQUAD));
   BitmapInfo = (BITMAPINFO *)new char[size];
   
   _llseek(TheFile, sizeof(BITMAPFILEHEADER), 0);
   _lread(TheFile, (LPSTR)BitmapInfo, size);
   
   /* load color add the .BMP colors to the palette */

   for (i=0;i<(1<<bitCount);i++) LoadColor(
   BitmapInfo->bmiColors[i].rgbRed,
   BitmapInfo->bmiColors[i].rgbGreen,
   BitmapInfo->bmiColors[i].rgbBlue);

   NewPixelWidth = BitmapInfo->bmiHeader.biWidth;
   NewPixelHeight = BitmapInfo->bmiHeader.biHeight;
   
   longWidth = (((NewPixelWidth * bitCount) + 31)/32) * 4;
   
   BitmapInfo->bmiHeader.biSizeImage = longWidth * NewPixelHeight;
   
   GlobalCompact(-1);
   
   BitsHandle = (HBITMAP)GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, BitmapInfo->bmiHeader.biSizeImage);
   if (!BitsHandle)
      {
      delete BitmapInfo;
      return FALSE;
      }
   
   /* read bits from .BMP into Bitshandle */

   GetBitmapData(TheFile, BitsHandle, BitmapInfo->bmiHeader.biSizeImage);
   
   BitsPtr = (LPSTR)GlobalLock((HGLOBAL)BitsHandle);
   
   ScreenDC = GetDC(0);
   MemDC = CreateCompatibleDC(ScreenDC);
   OldObject = SelectObject(MemDC, MemoryBitMap);
   OldPalette = SelectPalette(MemDC, ThePalette, FALSE);
   RealizePalette(MemDC);
   
   /*
   This gets nothing, If I do this to ScreenDC it works fine (I get 256 image),
   but I want it in my backing store MemDC also a 256 DIB which works fine.
   */

   SetDIBitsToDevice(MemDC,
      0,
      0,
      NewPixelWidth,
      NewPixelHeight, 0, 0, 0, NewPixelHeight, BitsPtr, BitmapInfo,
      DIB_RGB_COLORS);

   ReleaseDC(0, ScreenDC);
   
   GlobalUnlock(BitsHandle);
   GlobalFree(BitsHandle);

   delete BitmapInfo;

   SelectPalette(MemDC, OldPalette, FALSE);
   SelectObject(MemDC, OldObject);
   DeleteDC(MemDC);
   
   return TRUE;
   }
Technical Details
Message-ID: <mills.716345326@dialup.athena.lkg.dec.com>
Path: arc1-ca.pugleaf.net!sparky!uunet!decwrl!pa.dec.com!nntpd2.cxo.dec.com!nntpd.lkg.dec.com!amber!dialup.athena.lkg.dec.com!mills
References: <mills.715656976@dialup.athena.lkg.dec.com> <1992Sep8.160645.1@ntuvax.ntu.ac.sg>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!decwrl!pa.dec.com!nntpd2.cxo.dec.com!nntpd.lkg.dec.com!amber!dialup.athena.lkg.dec.com!mills
From: mills@athena.lkg.dec.com (George Mills)
Newsgroups: comp.os.ms-windows.programmer.tools
Subject: Re: Trying to read in 256 color .BMP file
Message-ID: <mills.716345326@dialup.athena.lkg.dec.com>
Date: 13 Sep 92 00:48:46 GMT
References: <mills.715656976@dialup.athena.lkg.dec.com> <1992Sep8.160645.1@ntuvax.ntu.ac.sg>
Distribution: usa
Lines: 101