home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / bitmap1.zip / BITMAP.CPP < prev    next >
Text File  |  1993-12-02  |  7KB  |  240 lines

  1. // bitmap class implementation
  2.  
  3. // Copyright (C) Joel Barnum, Descriptor Systems, 1993
  4.  
  5. #define    INCL_PM
  6. #include <os2.h>
  7. #include <stdlib.h>
  8. #include <io.h>
  9. #include <string.h>
  10. #include <stdio.h>
  11. #include "printerr.h"
  12. #include "bitmap.hpp"
  13.  
  14. //*********************************************************
  15. // create a bitmap from a resource file
  16. //*********************************************************
  17. bitmap::bitmap ( HDC hdc, HMODULE hmod, ULONG id )
  18. {
  19.   // create a memory DC compatible with the input DC
  20.       HAB        hab = WinQueryAnchorBlock ( NULLHANDLE );
  21.  
  22.     hdc = DevOpenDC ( hab, OD_MEMORY, "*", 0, NULL, hdc );
  23.     pmassert ( hdc, hab );
  24.  
  25.   // create a micro PS for the bitmap
  26.       SIZEL    sizel = {0,0};
  27.       hps = GpiCreatePS ( hab, hdc, &sizel
  28.                       , GPIT_MICRO | PU_PELS | GPIA_ASSOC );
  29.     pmassert ( hps, hab );
  30.  
  31.   // load the bitmap
  32.       hbm = GpiLoadBitmap ( hps, hmod, id, 0, 0 );
  33.     pmassert ( hbm, hab );
  34.  
  35.   // initialize the object data members
  36.       initialize ( hab );
  37. }
  38.  
  39. //*********************************************************
  40. // create a bitmap from memory
  41. //*********************************************************
  42. bitmap::bitmap ( HDC hdc, ULONG cxIn, ULONG cyIn
  43.            , ULONG ulbppIn, PRGB2 pColorTable, PBYTE ab )
  44. {
  45.   // create a memory DC compatible with the input DC
  46.       HAB        hab = WinQueryAnchorBlock ( NULLHANDLE );
  47.  
  48.     hdc = DevOpenDC ( hab, OD_MEMORY, "*", 0, NULL, hdc );
  49.     pmassert ( hdc, hab );
  50.  
  51.   // create a micro PS for the bitmap
  52.       SIZEL    sizel = {0,0};
  53.       hps = GpiCreatePS ( hab, hdc, &sizel
  54.                       , GPIT_MICRO | PU_PELS | GPIA_ASSOC );
  55.     pmassert ( hps, hab );
  56.  
  57.  
  58.   // calculate the size of the bitmap info struct
  59.   // we subtract one from the number of colors because
  60.   // the BITMAPINFO struct defines one color table entry
  61.       PBITMAPINFO2    pbmi2;
  62.       ULONG    cb;
  63.  
  64.       cColors = 1 << ulbppIn;
  65.     cb = sizeof (BITMAPINFO2) + sizeof ( RGB2 ) * (cColors-1);
  66.       pbmi2 = (PBITMAPINFO2)malloc ( cb );
  67.  
  68.   // initialize the bitmap info structure
  69.       memset ( pbmi2, 0, sizeof (BITMAPINFOHEADER2) );
  70.     pbmi2->cbFix = sizeof (BITMAPINFOHEADER2);
  71.     pbmi2->cx = cxIn;
  72.     pbmi2->cy = cyIn;
  73.     pbmi2->cPlanes = 1;
  74.     pbmi2->cBitCount = ulbppIn;
  75.  
  76.   // copy the input color table into the just-allocated struct
  77.       PRGB2    pColor = (PRGB2)( (PBYTE)pbmi2 + pbmi2->cbFix );
  78.  
  79.     memcpy ( pColor, pColorTable, cColors * sizeof ( RGB2 ) );
  80.  
  81.   // create and initialize a bitmap from the memory
  82.     hbm = GpiCreateBitmap ( hps
  83.                 , (PBITMAPINFOHEADER2)pbmi2
  84.                 , CBM_INIT
  85.                 , ab
  86.                 , (PBITMAPINFO2)pbmi2 );
  87.     pmassert ( hbm, hab );
  88.  
  89.   // initialize the object data members
  90.       initialize ( hab );
  91.  
  92.   // clean up
  93.       free ( pbmi2 );
  94. }
  95.  
  96. //*********************************************************
  97. // create a bitmap from memory, given a PM BITMAPINFO2 struct
  98. //*********************************************************
  99. bitmap::bitmap ( HDC, PBITMAPINFO2 pbmp2, PBYTE ab )
  100. {
  101.  
  102.   // create a memory DC compatible with the input DC
  103.       HAB        hab = WinQueryAnchorBlock ( NULLHANDLE );
  104.  
  105.     hdc = DevOpenDC ( hab, OD_MEMORY, "*", 0, NULL, hdc );
  106.     pmassert ( hdc, hab );
  107.  
  108.   // create a micro PS for the bitmap
  109.       SIZEL    sizel = {0,0};
  110.       hps = GpiCreatePS ( hab, hdc, &sizel
  111.                       , GPIT_MICRO | PU_PELS | GPIA_ASSOC );
  112.     pmassert ( hps, hab );
  113.  
  114.   // create and initialize a bitmap from the memory
  115.     hbm = GpiCreateBitmap ( hps
  116.                 , (PBITMAPINFOHEADER2)pbmp2
  117.                 , CBM_INIT
  118.                 , ab
  119.                 , (PBITMAPINFO2)pbmp2 );
  120.     pmassert ( hbm, hab );
  121.  
  122.   // initialize the object data members
  123.       initialize ( hab );
  124. }
  125.  
  126. //*********************************************************
  127. // Destructor
  128. //*********************************************************
  129. bitmap::~bitmap ( void )
  130. {
  131.     delete [] aRGB2Colors;
  132.     GpiSetBitmap ( hps, NULLHANDLE );
  133.     GpiDeleteBitmap ( hbm );
  134.     GpiDestroyPS ( hps );
  135. }
  136.  
  137. //*********************************************************
  138. // initialize the data members of this class
  139. //*********************************************************
  140. void bitmap::initialize ( HAB hab )
  141. {
  142.   // select the bitmap into the presentation space
  143.       BOOL    fSuccess;
  144.       fSuccess = GpiSetBitmap ( hps, hbm );
  145.     pmassert ( fSuccess != HBM_ERROR, hab );
  146.  
  147.   // query info about the bitmap and fill in member data
  148.       BITMAPINFOHEADER2    bmp2;
  149.  
  150.     bmp2.cbFix = sizeof ( BITMAPINFOHEADER2 );
  151.     fSuccess = GpiQueryBitmapInfoHeader ( hbm, &bmp2 );
  152.     cx = bmp2.cx;
  153.     cy = bmp2.cy;
  154.     ulbpp = bmp2.cBitCount;
  155.  
  156.   // query the bitmap's color table
  157.       PBITMAPINFO2    pbmi2;
  158.     ULONG            cb;
  159.  
  160.       cColors = 1 << ulbpp;
  161.     cb = sizeof (BITMAPINFO2) + sizeof ( RGB2 ) * cColors;
  162.       pbmi2 = (PBITMAPINFO2)malloc ( cb );
  163.     pbmi2->cbFix = cb;
  164.     fSuccess = GpiQueryBitmapBits ( hps, 0, 0, NULL, pbmi2 );
  165.     pmassert ( fSuccess, hab );
  166.  
  167.   // copy the color table into the data member with that name
  168.       aRGB2Colors = new RGB2 [cColors];
  169.     memcpy ( aRGB2Colors, pbmi2->argbColor, cColors * sizeof ( RGB2 ) );
  170.     free ( pbmi2 );
  171.  
  172.   // initialize other data members
  173.       lRop = ROP_SRCCOPY;        // overpaint
  174.  
  175.     rcl.xLeft = 0;            // draw entire bitmap
  176.     rcl.xRight = cx - 1;
  177.     rcl.yBottom = 0;
  178.     rcl.yTop = cy - 1;
  179.  
  180. }
  181.  
  182. //*********************************************************
  183. // draw the bitmap to a target rectangle
  184. //*********************************************************
  185. BOOL bitmap::draw ( HPS hpsTarget, RECTL & rclTarg )
  186. {
  187.  
  188.     HAB    hab = WinQueryAnchorBlock ( NULLHANDLE );
  189.  
  190.   // setup the source and target rectangles
  191.     POINTL    aptl[4];
  192.  
  193.     aptl[0].x = rclTarg.xLeft;        // target lower left
  194.     aptl[0].y = rclTarg.yBottom;
  195.     aptl[1].x = rclTarg.xRight;        // target upper right
  196.     aptl[1].y = rclTarg.yTop;
  197.  
  198.     aptl[2].x = rcl.xLeft;            // source lower left
  199.     aptl[2].y = rcl.yBottom;
  200.     aptl[3].x = rcl.xRight;            // source upper right
  201.     aptl[3].y = rcl.yTop;
  202.  
  203.   // transfer the bits
  204.     LONG    lSuccess;
  205.  
  206.     lSuccess = GpiBitBlt ( hpsTarget, hps, 4, aptl, lRop
  207.                          , BBO_IGNORE );
  208.     pmassert ( lSuccess != GPI_ERROR, hab );
  209.     return (BOOL)lSuccess;
  210. }
  211.  
  212. //*********************************************************
  213. // draw the bitmap to a target lower left point
  214. //*********************************************************
  215. BOOL bitmap::draw ( HPS hpsTarget, POINTL & ptlTarg )
  216. {
  217.  
  218.     HAB    hab = WinQueryAnchorBlock ( NULLHANDLE );
  219.  
  220.   // setup the source and target rectangles
  221.     POINTL    aptl[3];
  222.  
  223.     aptl[0].x = ptlTarg.x;            // target lower left
  224.     aptl[0].y = ptlTarg.y;
  225.     aptl[1].x = ptlTarg.x + cx;        // target upper right
  226.     aptl[1].y = ptlTarg.y + cy;
  227.  
  228.     aptl[2].x = rcl.xLeft;            // source lower left
  229.     aptl[2].y = rcl.yBottom;
  230.  
  231.   // transfer the bits
  232.     LONG    lSuccess;
  233.  
  234.     lSuccess = GpiBitBlt ( hpsTarget, hps, 3, aptl, lRop
  235.                          , BBO_IGNORE );
  236.     pmassert ( lSuccess != GPI_ERROR, hab );
  237.     return (BOOL)lSuccess;
  238. }
  239.  
  240.