home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / c / library / mslang / poetmf / src / bitmap.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-02  |  5.1 KB  |  152 lines

  1. //*****************************************************************
  2. //
  3. //    bitmap.cpp        Hello POET bitmap implementation
  4. //
  5. //    Implementation of the following classes:
  6. //        Bitmap
  7. //
  8. //    Remarks
  9. //        This class contains a Bitmap that can be displayed
  10. //        inside a window. 
  11. //
  12. //        Parts of the load and display implementation has been
  13. //        taken from some Borland example files and has been
  14. //        adopted
  15. //
  16. //    Author:    POET Software, August 1993
  17. //
  18. //*****************************************************************
  19.  
  20. #include <stdafx.h>        //    header to MFC
  21. #include <bitmap.h>        //    header to this file
  22. #include <string.h>        //    header to string functions
  23.  
  24.  
  25. void Bitmap::Setup (CWnd * pWnd, PtBlob *Blob)
  26. {
  27.     CDC     *hdc = pWnd->GetDC();    //    device context of dialog window
  28.     BITMAP     bm;                        //    BITMAP struct 
  29.     CPoint     pnt;                    //    MFC CPoint compatible to POINT struct 
  30.  
  31.     if ( ! LoadBitmap (Blob) )        //    load bitmap into a CBitmap object
  32.         return;
  33.  
  34.     m_pBitmap->GetObject( sizeof (BITMAP), (LPSTR)&bm ); //    fill bitmapstruct 
  35.     pnt.x     = bm.bmWidth;        //    pixel width    
  36.     pnt.y     = bm.bmHeight;    //    pixel height
  37.     hdc->DPtoLP(&pnt, 1);    // converts from device points to logical points
  38.     xBMWidth  = pnt.x;    //    logical width
  39.     yBMHeight = pnt.y;  //    logical height
  40.     pWnd->ReleaseDC(hdc);
  41. }
  42.  
  43.  
  44.  
  45. void Bitmap::Draw (CPaintDC *hdc, short xPos, short yPos)
  46. {
  47.     CDC        hdcMem;        //    memory device context
  48.     CPoint    ptOrg (0, 0);    //    initialize CPoint object
  49.  
  50.     hdcMem.CreateCompatibleDC(hdc);    //    creates a memory device context that is
  51.                                     //    compatible with DC of dialog window
  52.     hdcMem.SelectObject (m_pBitmap);    //    selects bitmap into memory device context
  53.     hdcMem.SetMapMode (hdc->GetMapMode());    // set same MapMode
  54.     hdcMem.DPtoLP(&ptOrg, 1);    //    converts device points into logical points
  55.     hdc->BitBlt(xPos, yPos, xBMWidth, yBMHeight,    //    copies the bitmap from the memory device                                                    
  56.            &hdcMem, ptOrg.x, ptOrg.y, SRCCOPY);        //    context to DC of dialog window
  57.     hdcMem.DeleteDC();
  58. }
  59.  
  60. void Bitmap::GetBitmapData (char *data, HANDLE BitsHandle,long BitsByteSize)
  61. {
  62.     //    copy bitmapdata from Blob into memory
  63.     long Bits = (long)GlobalLock(BitsHandle);
  64.     hmemcpy ( (LPSTR)Bits, (const char PtHUGE *)data, BitsByteSize );
  65.     GlobalUnlock(BitsHandle);
  66. }
  67.  
  68.  
  69. BOOL Bitmap::OpenDIB(char *data)
  70. {
  71.     WORD         bitCount;    //    1 = monochrome, 4 = 16 Colors, 8 = 256 Colors
  72.     WORD         size;       //    size of BITMAPINFO struct
  73.     long         longWidth;
  74.     LPSTR         BitsPtr;      //    pointer to bitmapdata
  75.     BITMAPINFO *BitmapInfo; //    pointer to BITMAPINFO struct
  76.     CDC            DCHandle;   //    display device context
  77.     HANDLE         BitsHandle; //    HANDLE to bitmap
  78.     DWORD         NewPixelWidth , NewPixelHeight;
  79.     BOOL         retval = TRUE;
  80.     CBitmap        *pTempBitmap = new CBitmap ();    //    pointer to CBitmap for temporary use
  81.  
  82.     bitCount = *(WORD *)(data+28);
  83.  
  84.     if ( bitCount <= 8 )    //    maximal 256 Colors supported
  85.     {
  86.         size = sizeof(BITMAPINFOHEADER) + ((1 << bitCount) * sizeof(RGBQUAD));
  87.         BitmapInfo = (BITMAPINFO *)new char[size];
  88.         data += sizeof(BITMAPFILEHEADER);
  89.         hmemcpy((LPSTR)BitmapInfo, (const char PtHUGE *)data, (long)size);
  90.         data += size;    //    set pointer to first pixeldata
  91.         NewPixelWidth = BitmapInfo->bmiHeader.biWidth;
  92.         NewPixelHeight = BitmapInfo->bmiHeader.biHeight;
  93.         longWidth = (((NewPixelWidth * bitCount) + 31)/32) * 4;
  94.         BitmapInfo->bmiHeader.biSizeImage = longWidth * NewPixelHeight;
  95.  
  96.         GlobalCompact(BitmapInfo->bmiHeader.biSizeImage);    //    reserve enough memory on global heap
  97.         BitsHandle = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT,    //    allocate memory
  98.             BitmapInfo->bmiHeader.biSizeImage);
  99.         GetBitmapData(data, BitsHandle, BitmapInfo->bmiHeader.biSizeImage);    //    copy bitmapdata into global heap
  100.         DCHandle.CreateDC ("Display", NULL, NULL, NULL);    //    create display context
  101.         BitsPtr = (LPSTR) GlobalLock(BitsHandle);    //    lock memory while creating bitmap
  102. //    Now creating bitmap with API instead of MFC, cause it is much easier to generate it from BITMAPINFO
  103.         BitmapHandle =
  104.             CreateDIBitmap(DCHandle.m_hDC, &(BitmapInfo->bmiHeader), CBM_INIT, BitsPtr,
  105.         BitmapInfo, 0);
  106.         DCHandle.DeleteDC ();
  107.         GlobalUnlock(BitsHandle);    //    unlock memory
  108.         GlobalFree(BitsHandle);        //    deallocate memory
  109.         delete BitmapInfo;
  110.         if ( BitmapHandle )
  111.         {                             
  112.             //    create CBitmap object from HBITMAP handle
  113.             m_pBitmap = pTempBitmap->FromHandle ((HBITMAP) BitmapHandle);
  114.               PixelWidth = (WORD)NewPixelWidth;
  115.               PixelHeight = (WORD)NewPixelHeight;
  116.         }
  117.         else
  118.               retval = FALSE;
  119.           delete pTempBitmap;    //    no longer at use, bitmap in m_pBitmap
  120.       }
  121.       else
  122.         retval = FALSE;
  123. return retval;
  124. }
  125.  
  126.  
  127. BOOL Bitmap::LoadBitmap (PtBlob *blob)
  128. {
  129.     long     TestWin30Bitmap;
  130.     char     ErrorMsg[50];
  131.     BOOL     retval = FALSE;
  132.     char     * data = blob->Seek ( 0 );    //    set pointer to first Blobdata
  133.  
  134.     memcpy(&TestWin30Bitmap, data+14, sizeof(TestWin30Bitmap));
  135.     if ( TestWin30Bitmap == 40 )    
  136.     {
  137.       if ( ! OpenDIB(data) )
  138.           strcpy(ErrorMsg,
  139.                 "Unable to create Windows 3.0 bitmap from PtBlob");
  140.         else
  141.             retval = TRUE;
  142.     }
  143.     else
  144.         strcpy(ErrorMsg, "Not a Windows 3.0 bitmap");
  145.  
  146.   if ( ! retval )
  147.     MessageBox ( NULL, "Load Bitmap", ErrorMsg, MB_OK);
  148.  
  149.   return retval;
  150. }
  151.  
  152.