home *** CD-ROM | disk | FTP | other *** search
- //*****************************************************************
- //
- // bitmap.cpp Hello POET bitmap implementation
- //
- // Implementation of the following classes:
- // Bitmap
- //
- // Remarks
- // This class contains a Bitmap that can be displayed
- // inside a window.
- //
- // Parts of the load and display implementation has been
- // taken from some Borland example files and has been
- // adopted
- //
- // Author: POET Software, August 1993
- //
- //*****************************************************************
-
- #include <stdafx.h> // header to MFC
- #include <bitmap.h> // header to this file
- #include <string.h> // header to string functions
-
-
- void Bitmap::Setup (CWnd * pWnd, PtBlob *Blob)
- {
- CDC *hdc = pWnd->GetDC(); // device context of dialog window
- BITMAP bm; // BITMAP struct
- CPoint pnt; // MFC CPoint compatible to POINT struct
-
- if ( ! LoadBitmap (Blob) ) // load bitmap into a CBitmap object
- return;
-
- m_pBitmap->GetObject( sizeof (BITMAP), (LPSTR)&bm ); // fill bitmapstruct
- pnt.x = bm.bmWidth; // pixel width
- pnt.y = bm.bmHeight; // pixel height
- hdc->DPtoLP(&pnt, 1); // converts from device points to logical points
- xBMWidth = pnt.x; // logical width
- yBMHeight = pnt.y; // logical height
- pWnd->ReleaseDC(hdc);
- }
-
-
-
- void Bitmap::Draw (CPaintDC *hdc, short xPos, short yPos)
- {
- CDC hdcMem; // memory device context
- CPoint ptOrg (0, 0); // initialize CPoint object
-
- hdcMem.CreateCompatibleDC(hdc); // creates a memory device context that is
- // compatible with DC of dialog window
- hdcMem.SelectObject (m_pBitmap); // selects bitmap into memory device context
- hdcMem.SetMapMode (hdc->GetMapMode()); // set same MapMode
- hdcMem.DPtoLP(&ptOrg, 1); // converts device points into logical points
- hdc->BitBlt(xPos, yPos, xBMWidth, yBMHeight, // copies the bitmap from the memory device
- &hdcMem, ptOrg.x, ptOrg.y, SRCCOPY); // context to DC of dialog window
- hdcMem.DeleteDC();
- }
-
- void Bitmap::GetBitmapData (char *data, HANDLE BitsHandle,long BitsByteSize)
- {
- // copy bitmapdata from Blob into memory
- long Bits = (long)GlobalLock(BitsHandle);
- hmemcpy ( (LPSTR)Bits, (const char PtHUGE *)data, BitsByteSize );
- GlobalUnlock(BitsHandle);
- }
-
-
- BOOL Bitmap::OpenDIB(char *data)
- {
- WORD bitCount; // 1 = monochrome, 4 = 16 Colors, 8 = 256 Colors
- WORD size; // size of BITMAPINFO struct
- long longWidth;
- LPSTR BitsPtr; // pointer to bitmapdata
- BITMAPINFO *BitmapInfo; // pointer to BITMAPINFO struct
- CDC DCHandle; // display device context
- HANDLE BitsHandle; // HANDLE to bitmap
- DWORD NewPixelWidth , NewPixelHeight;
- BOOL retval = TRUE;
- CBitmap *pTempBitmap = new CBitmap (); // pointer to CBitmap for temporary use
-
- bitCount = *(WORD *)(data+28);
-
- if ( bitCount <= 8 ) // maximal 256 Colors supported
- {
- size = sizeof(BITMAPINFOHEADER) + ((1 << bitCount) * sizeof(RGBQUAD));
- BitmapInfo = (BITMAPINFO *)new char[size];
- data += sizeof(BITMAPFILEHEADER);
- hmemcpy((LPSTR)BitmapInfo, (const char PtHUGE *)data, (long)size);
- data += size; // set pointer to first pixeldata
- NewPixelWidth = BitmapInfo->bmiHeader.biWidth;
- NewPixelHeight = BitmapInfo->bmiHeader.biHeight;
- longWidth = (((NewPixelWidth * bitCount) + 31)/32) * 4;
- BitmapInfo->bmiHeader.biSizeImage = longWidth * NewPixelHeight;
-
- GlobalCompact(BitmapInfo->bmiHeader.biSizeImage); // reserve enough memory on global heap
- BitsHandle = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, // allocate memory
- BitmapInfo->bmiHeader.biSizeImage);
- GetBitmapData(data, BitsHandle, BitmapInfo->bmiHeader.biSizeImage); // copy bitmapdata into global heap
- DCHandle.CreateDC ("Display", NULL, NULL, NULL); // create display context
- BitsPtr = (LPSTR) GlobalLock(BitsHandle); // lock memory while creating bitmap
- // Now creating bitmap with API instead of MFC, cause it is much easier to generate it from BITMAPINFO
- BitmapHandle =
- CreateDIBitmap(DCHandle.m_hDC, &(BitmapInfo->bmiHeader), CBM_INIT, BitsPtr,
- BitmapInfo, 0);
- DCHandle.DeleteDC ();
- GlobalUnlock(BitsHandle); // unlock memory
- GlobalFree(BitsHandle); // deallocate memory
- delete BitmapInfo;
- if ( BitmapHandle )
- {
- // create CBitmap object from HBITMAP handle
- m_pBitmap = pTempBitmap->FromHandle ((HBITMAP) BitmapHandle);
- PixelWidth = (WORD)NewPixelWidth;
- PixelHeight = (WORD)NewPixelHeight;
- }
- else
- retval = FALSE;
- delete pTempBitmap; // no longer at use, bitmap in m_pBitmap
- }
- else
- retval = FALSE;
- return retval;
- }
-
-
- BOOL Bitmap::LoadBitmap (PtBlob *blob)
- {
- long TestWin30Bitmap;
- char ErrorMsg[50];
- BOOL retval = FALSE;
- char * data = blob->Seek ( 0 ); // set pointer to first Blobdata
-
- memcpy(&TestWin30Bitmap, data+14, sizeof(TestWin30Bitmap));
- if ( TestWin30Bitmap == 40 )
- {
- if ( ! OpenDIB(data) )
- strcpy(ErrorMsg,
- "Unable to create Windows 3.0 bitmap from PtBlob");
- else
- retval = TRUE;
- }
- else
- strcpy(ErrorMsg, "Not a Windows 3.0 bitmap");
-
- if ( ! retval )
- MessageBox ( NULL, "Load Bitmap", ErrorMsg, MB_OK);
-
- return retval;
- }
-
-