home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
bitmap1.zip
/
BITMAP.CPP
< prev
next >
Wrap
Text File
|
1993-12-02
|
7KB
|
240 lines
// bitmap class implementation
// Copyright (C) Joel Barnum, Descriptor Systems, 1993
#define INCL_PM
#include <os2.h>
#include <stdlib.h>
#include <io.h>
#include <string.h>
#include <stdio.h>
#include "printerr.h"
#include "bitmap.hpp"
//*********************************************************
// create a bitmap from a resource file
//*********************************************************
bitmap::bitmap ( HDC hdc, HMODULE hmod, ULONG id )
{
// create a memory DC compatible with the input DC
HAB hab = WinQueryAnchorBlock ( NULLHANDLE );
hdc = DevOpenDC ( hab, OD_MEMORY, "*", 0, NULL, hdc );
pmassert ( hdc, hab );
// create a micro PS for the bitmap
SIZEL sizel = {0,0};
hps = GpiCreatePS ( hab, hdc, &sizel
, GPIT_MICRO | PU_PELS | GPIA_ASSOC );
pmassert ( hps, hab );
// load the bitmap
hbm = GpiLoadBitmap ( hps, hmod, id, 0, 0 );
pmassert ( hbm, hab );
// initialize the object data members
initialize ( hab );
}
//*********************************************************
// create a bitmap from memory
//*********************************************************
bitmap::bitmap ( HDC hdc, ULONG cxIn, ULONG cyIn
, ULONG ulbppIn, PRGB2 pColorTable, PBYTE ab )
{
// create a memory DC compatible with the input DC
HAB hab = WinQueryAnchorBlock ( NULLHANDLE );
hdc = DevOpenDC ( hab, OD_MEMORY, "*", 0, NULL, hdc );
pmassert ( hdc, hab );
// create a micro PS for the bitmap
SIZEL sizel = {0,0};
hps = GpiCreatePS ( hab, hdc, &sizel
, GPIT_MICRO | PU_PELS | GPIA_ASSOC );
pmassert ( hps, hab );
// calculate the size of the bitmap info struct
// we subtract one from the number of colors because
// the BITMAPINFO struct defines one color table entry
PBITMAPINFO2 pbmi2;
ULONG cb;
cColors = 1 << ulbppIn;
cb = sizeof (BITMAPINFO2) + sizeof ( RGB2 ) * (cColors-1);
pbmi2 = (PBITMAPINFO2)malloc ( cb );
// initialize the bitmap info structure
memset ( pbmi2, 0, sizeof (BITMAPINFOHEADER2) );
pbmi2->cbFix = sizeof (BITMAPINFOHEADER2);
pbmi2->cx = cxIn;
pbmi2->cy = cyIn;
pbmi2->cPlanes = 1;
pbmi2->cBitCount = ulbppIn;
// copy the input color table into the just-allocated struct
PRGB2 pColor = (PRGB2)( (PBYTE)pbmi2 + pbmi2->cbFix );
memcpy ( pColor, pColorTable, cColors * sizeof ( RGB2 ) );
// create and initialize a bitmap from the memory
hbm = GpiCreateBitmap ( hps
, (PBITMAPINFOHEADER2)pbmi2
, CBM_INIT
, ab
, (PBITMAPINFO2)pbmi2 );
pmassert ( hbm, hab );
// initialize the object data members
initialize ( hab );
// clean up
free ( pbmi2 );
}
//*********************************************************
// create a bitmap from memory, given a PM BITMAPINFO2 struct
//*********************************************************
bitmap::bitmap ( HDC, PBITMAPINFO2 pbmp2, PBYTE ab )
{
// create a memory DC compatible with the input DC
HAB hab = WinQueryAnchorBlock ( NULLHANDLE );
hdc = DevOpenDC ( hab, OD_MEMORY, "*", 0, NULL, hdc );
pmassert ( hdc, hab );
// create a micro PS for the bitmap
SIZEL sizel = {0,0};
hps = GpiCreatePS ( hab, hdc, &sizel
, GPIT_MICRO | PU_PELS | GPIA_ASSOC );
pmassert ( hps, hab );
// create and initialize a bitmap from the memory
hbm = GpiCreateBitmap ( hps
, (PBITMAPINFOHEADER2)pbmp2
, CBM_INIT
, ab
, (PBITMAPINFO2)pbmp2 );
pmassert ( hbm, hab );
// initialize the object data members
initialize ( hab );
}
//*********************************************************
// Destructor
//*********************************************************
bitmap::~bitmap ( void )
{
delete [] aRGB2Colors;
GpiSetBitmap ( hps, NULLHANDLE );
GpiDeleteBitmap ( hbm );
GpiDestroyPS ( hps );
}
//*********************************************************
// initialize the data members of this class
//*********************************************************
void bitmap::initialize ( HAB hab )
{
// select the bitmap into the presentation space
BOOL fSuccess;
fSuccess = GpiSetBitmap ( hps, hbm );
pmassert ( fSuccess != HBM_ERROR, hab );
// query info about the bitmap and fill in member data
BITMAPINFOHEADER2 bmp2;
bmp2.cbFix = sizeof ( BITMAPINFOHEADER2 );
fSuccess = GpiQueryBitmapInfoHeader ( hbm, &bmp2 );
cx = bmp2.cx;
cy = bmp2.cy;
ulbpp = bmp2.cBitCount;
// query the bitmap's color table
PBITMAPINFO2 pbmi2;
ULONG cb;
cColors = 1 << ulbpp;
cb = sizeof (BITMAPINFO2) + sizeof ( RGB2 ) * cColors;
pbmi2 = (PBITMAPINFO2)malloc ( cb );
pbmi2->cbFix = cb;
fSuccess = GpiQueryBitmapBits ( hps, 0, 0, NULL, pbmi2 );
pmassert ( fSuccess, hab );
// copy the color table into the data member with that name
aRGB2Colors = new RGB2 [cColors];
memcpy ( aRGB2Colors, pbmi2->argbColor, cColors * sizeof ( RGB2 ) );
free ( pbmi2 );
// initialize other data members
lRop = ROP_SRCCOPY; // overpaint
rcl.xLeft = 0; // draw entire bitmap
rcl.xRight = cx - 1;
rcl.yBottom = 0;
rcl.yTop = cy - 1;
}
//*********************************************************
// draw the bitmap to a target rectangle
//*********************************************************
BOOL bitmap::draw ( HPS hpsTarget, RECTL & rclTarg )
{
HAB hab = WinQueryAnchorBlock ( NULLHANDLE );
// setup the source and target rectangles
POINTL aptl[4];
aptl[0].x = rclTarg.xLeft; // target lower left
aptl[0].y = rclTarg.yBottom;
aptl[1].x = rclTarg.xRight; // target upper right
aptl[1].y = rclTarg.yTop;
aptl[2].x = rcl.xLeft; // source lower left
aptl[2].y = rcl.yBottom;
aptl[3].x = rcl.xRight; // source upper right
aptl[3].y = rcl.yTop;
// transfer the bits
LONG lSuccess;
lSuccess = GpiBitBlt ( hpsTarget, hps, 4, aptl, lRop
, BBO_IGNORE );
pmassert ( lSuccess != GPI_ERROR, hab );
return (BOOL)lSuccess;
}
//*********************************************************
// draw the bitmap to a target lower left point
//*********************************************************
BOOL bitmap::draw ( HPS hpsTarget, POINTL & ptlTarg )
{
HAB hab = WinQueryAnchorBlock ( NULLHANDLE );
// setup the source and target rectangles
POINTL aptl[3];
aptl[0].x = ptlTarg.x; // target lower left
aptl[0].y = ptlTarg.y;
aptl[1].x = ptlTarg.x + cx; // target upper right
aptl[1].y = ptlTarg.y + cy;
aptl[2].x = rcl.xLeft; // source lower left
aptl[2].y = rcl.yBottom;
// transfer the bits
LONG lSuccess;
lSuccess = GpiBitBlt ( hpsTarget, hps, 3, aptl, lRop
, BBO_IGNORE );
pmassert ( lSuccess != GPI_ERROR, hab );
return (BOOL)lSuccess;
}