home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / os / mswindo / programm / tools / 942 < prev    next >
Encoding:
Internet Message Format  |  1992-09-12  |  3.3 KB

  1. Path: sparky!uunet!decwrl!pa.dec.com!nntpd2.cxo.dec.com!nntpd.lkg.dec.com!amber!dialup.athena.lkg.dec.com!mills
  2. From: mills@athena.lkg.dec.com (George Mills)
  3. Newsgroups: comp.os.ms-windows.programmer.tools
  4. Subject: Re: Trying to read in 256 color .BMP file
  5. Message-ID: <mills.716345326@dialup.athena.lkg.dec.com>
  6. Date: 13 Sep 92 00:48:46 GMT
  7. References: <mills.715656976@dialup.athena.lkg.dec.com> <1992Sep8.160645.1@ntuvax.ntu.ac.sg>
  8. Distribution: usa
  9. Lines: 101
  10.  
  11.  
  12. If some sharp windows programmer could tell me what I'm doing wrong here
  13. it would be greatly appreciated.
  14.  
  15. I'm using Turbo C++ for windows and this is actually a derivative of
  16. their example. If someone could email some of the SDK examples of reading
  17. in a .BMP that would be very helpful. The function reads in the bits of
  18. a .BMP and trys to "set" them into an existing DIB.
  19.  
  20. Should I create the secondary DIB and do a GETDIBITS and then set them
  21. in the backing store DIB (this seemed redundant since I already have
  22. the "bits" directly from the .BMP.
  23.  
  24. BOOL TMyWindow::OpenDIB(int TheFile)
  25.    {
  26.    WORD bitCount;
  27.    WORD size;
  28.    long longWidth;
  29.    LPSTR BitsPtr;
  30.    BITMAPINFO *BitmapInfo;
  31.    HBITMAP BitsHandle;
  32.    DWORD NewPixelWidth , NewPixelHeight;
  33.    BITMAPFILEHEADER BitmapFileHeader;
  34.    int i;
  35.    
  36.    /* read header stuff of .bmp */
  37.  
  38.    _llseek(TheFile, 0, 0);
  39.    _lread(TheFile, (LPSTR)&BitmapFileHeader, sizeof(BitmapFileHeader));
  40.    
  41.    _llseek(TheFile, 28, 0);
  42.    _lread(TheFile, (LPSTR)&bitCount, sizeof(bitCount));
  43.    
  44.    if (!(bitCount <= 8)) return FALSE;
  45.    
  46.    size = sizeof(BITMAPINFOHEADER) + ((1 << bitCount) * sizeof(RGBQUAD));
  47.    BitmapInfo = (BITMAPINFO *)new char[size];
  48.    
  49.    _llseek(TheFile, sizeof(BITMAPFILEHEADER), 0);
  50.    _lread(TheFile, (LPSTR)BitmapInfo, size);
  51.    
  52.    /* load color add the .BMP colors to the palette */
  53.  
  54.    for (i=0;i<(1<<bitCount);i++) LoadColor(
  55.    BitmapInfo->bmiColors[i].rgbRed,
  56.    BitmapInfo->bmiColors[i].rgbGreen,
  57.    BitmapInfo->bmiColors[i].rgbBlue);
  58.  
  59.    NewPixelWidth = BitmapInfo->bmiHeader.biWidth;
  60.    NewPixelHeight = BitmapInfo->bmiHeader.biHeight;
  61.    
  62.    longWidth = (((NewPixelWidth * bitCount) + 31)/32) * 4;
  63.    
  64.    BitmapInfo->bmiHeader.biSizeImage = longWidth * NewPixelHeight;
  65.    
  66.    GlobalCompact(-1);
  67.    
  68.    BitsHandle = (HBITMAP)GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, BitmapInfo->bmiHeader.biSizeImage);
  69.    if (!BitsHandle)
  70.       {
  71.       delete BitmapInfo;
  72.       return FALSE;
  73.       }
  74.    
  75.    /* read bits from .BMP into Bitshandle */
  76.  
  77.    GetBitmapData(TheFile, BitsHandle, BitmapInfo->bmiHeader.biSizeImage);
  78.    
  79.    BitsPtr = (LPSTR)GlobalLock((HGLOBAL)BitsHandle);
  80.    
  81.    ScreenDC = GetDC(0);
  82.    MemDC = CreateCompatibleDC(ScreenDC);
  83.    OldObject = SelectObject(MemDC, MemoryBitMap);
  84.    OldPalette = SelectPalette(MemDC, ThePalette, FALSE);
  85.    RealizePalette(MemDC);
  86.    
  87.    /*
  88.    This gets nothing, If I do this to ScreenDC it works fine (I get 256 image),
  89.    but I want it in my backing store MemDC also a 256 DIB which works fine.
  90.    */
  91.  
  92.    SetDIBitsToDevice(MemDC,
  93.       0,
  94.       0,
  95.       NewPixelWidth,
  96.       NewPixelHeight, 0, 0, 0, NewPixelHeight, BitsPtr, BitmapInfo,
  97.       DIB_RGB_COLORS);
  98.  
  99.    ReleaseDC(0, ScreenDC);
  100.    
  101.    GlobalUnlock(BitsHandle);
  102.    GlobalFree(BitsHandle);
  103.  
  104.    delete BitmapInfo;
  105.  
  106.    SelectPalette(MemDC, OldPalette, FALSE);
  107.    SelectObject(MemDC, OldObject);
  108.    DeleteDC(MemDC);
  109.    
  110.    return TRUE;
  111.    }
  112.