home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / bmp1.zip / GetBmp.C < prev    next >
C/C++ Source or Header  |  1995-10-25  |  32KB  |  901 lines

  1. #pragma    title("View Bitmap  --  Version 1  --  (GetBmp.C)")
  2. #pragma    subtitle("   Main Client Window - Interface Definitions")
  3.  
  4. #define    INCL_BITMAPFILEFORMAT       /* Include OS/2 Bitmap Information    */
  5. #define    INCL_DOS           /* Include OS/2 DOS Kernal        */
  6. #define    INCL_GPI           /* Include OS/2 GPI Interface    */
  7. #define    INCL_WIN           /* Include OS/2 PM Windows Interface    */
  8.  
  9. #include <malloc.h>
  10. #include <os2.h>
  11. #include <string.h>
  12.  
  13. #include "appdefs.h"
  14. #include "winbmp.h"
  15.  
  16.  
  17. /* This    module contains    the functions to load a    requested .BMP file.    */
  18.  
  19. /* Filename:   GetBmp.C                            */
  20.  
  21. /*  Version:   1                            */
  22. /*  Created:   1995-07-05                        */
  23. /*  Revised:   1995-07-22                        */
  24.  
  25. /* Routines:   static VOID BuildBitmapStack(                */
  26. /*                   PBITMAPARRAYFILEHEADER2 pbafh2);    */
  27. /*           static VOID BuildBitmapStack1(                */
  28. /*                   PBITMAPARRAYFILEHEADER pbafh);    */
  29. /*           static PBITMAPFILEHEADER2 pbfhSelectBfh(            */
  30. /*                       PBITMAPARRAYFILEHEADER2 pbafh2,    */
  31. /*                       ULONG cxDefault,            */
  32. /*                       ULONG cyDefault);        */
  33. /*           static PBITMAPFILEHEADER    pbfhSelectBfh1(            */
  34. /*                       PBITMAPARRAYFILEHEADER pbafh,    */
  35. /*                       ULONG cxDefault,            */
  36. /*                       ULONG cyDefault);        */
  37. /*           static HBITMAP hbmGetSingleBitmap(            */
  38. /*                         PBITMAPFILEHEADER2    pbfh2);    */
  39. /*           static HBITMAP hbmGetSingleBitmap1(            */
  40. /*                           PBITMAPFILEHEADER pbfh);    */
  41. /*           HBITMAP hbmGetBitmap(PSZ    pszFileName);            */
  42. /*           static PBITMAPFILEHEADER2 pbfh2GetWindowsBmp(        */
  43. /*                   PBYTE pbData, ULONG cbData);        */
  44.  
  45.  
  46. /************************************************************************/
  47. /************************************************************************/
  48. /************************************************************************/
  49. /* DISCLAIMER OF WARRANTIES:                        */
  50. /* -------------------------                        */
  51. /* The following [enclosed] code is sample code    created    by IBM        */
  52. /* Corporation and Prominare Inc.  This    sample code is not part    of any    */
  53. /* standard IBM    product    and is provided    to you solely for the purpose    */
  54. /* of assisting    you in the development of your applications.  The code    */
  55. /* is provided "AS IS",    without    warranty of any    kind.  Neither IBM nor    */
  56. /* Prominare shall be liable for any damages arising out of your    */
  57. /* use of the sample code, even    if they    have been advised of the    */
  58. /* possibility of such damages.                        */
  59. /************************************************************************/
  60. /************************************************************************/
  61. /************************************************************************/
  62. /*               D I S C L A I M E R                */
  63. /* This    code is    provided on an as is basis with    no implied support.    */
  64. /* It should be    considered freeware that cannot    be rebundled as        */
  65. /* part    of a larger "*ware" offering without our consent.        */
  66. /************************************************************************/
  67. /************************************************************************/
  68. /************************************************************************/
  69.  
  70. /* Copyright ╕ International Business Machines Corp., 1995.        */
  71. /* Copyright ╕ 1995  Prominare Inc.  All Rights    Reserved.        */
  72.  
  73. /* --------------------------------------------------------------------    */
  74.  
  75. /* --- Module Prototype    Definitions -----------------------------------    */
  76.  
  77. static VOID BuildBitmapStack(PBITMAPARRAYFILEHEADER2 pbafh2);
  78. static VOID BuildBitmapStack1(PBITMAPARRAYFILEHEADER pbafh);
  79.  
  80. static LONG lSelectBfh(PBITMAPARRAYFILEHEADER2 pbafh2);
  81. static HBITMAP          hbmGetSingleBitmap(PBITMAPFILEHEADER2    pbfh2);
  82.  
  83. static LONG lSelectBfh1(PBITMAPARRAYFILEHEADER pbafh);
  84. static HBITMAP          hbmGetSingleBitmap1(PBITMAPFILEHEADER    pbfh);
  85.  
  86. static PBITMAPFILEHEADER2 pbfh2GetWindowsBmp(PBYTE pbData, ULONG cbData);
  87.  
  88. #pragma    subtitle("   Bitmap Retrieve - Select Bitmap File Header Function")
  89. #pragma    page( )
  90.  
  91. /* --- BuildBitmapStack    -------------------------------    [ Private ] ---    */
  92. /*                                    */
  93. /*     This function is    used to    build the bitmap images    for a bitmap    */
  94. /*     array and place the bitmaps within the bitmap stack.        */
  95. /*                                    */
  96. /*     Upon Entry:                            */
  97. /*                                    */
  98. /*     PBITMAPARRAYFILEHEADER2 pbafh2; = Bitmap    Array Header Pointer    */
  99. /*                                    */
  100. /*     Upon Exit:                            */
  101. /*                                    */
  102. /*     Nothing                                */
  103. /*                                    */
  104. /* --------------------------------------------------------------------    */
  105.  
  106. static VOID BuildBitmapStack(PBITMAPARRAYFILEHEADER2 pbafh2)
  107.  
  108. {
  109. PBITMAPARRAYFILEHEADER2    pbafhOrg;  /* Bitmap Array File    Header Origin    */
  110. PBITMAPFILEHEADER2    pbfh2;       /* Bitmap Array File    Header Pointer    */
  111. HPS           hpsMem;       /* Memory Presentation Space    Handle    */
  112. register INT i;               /* Loop Counter            */
  113.  
  114.                /* Delete any previously    loaded bitmaps        */
  115.  
  116. for ( i    = 0; i < cBitmaps; i++ )
  117.    GpiDeleteBitmap(abm[i].hbm);
  118.  
  119. cBitmaps = 0L;
  120. hpsMem = WinGetPS(HWND_DESKTOP);
  121.  
  122.                /* Save the start of the    bitmap file header    */
  123.                /* since    all offsets are    from the beginning of    */
  124.                /* the header                    */
  125.  
  126. abm[cBitmaps].pb = (PBYTE)(pbafhOrg = pbafh2);
  127.  
  128.                /* Point    to the independent bitmap file header    */
  129.                /* since    it is the default            */
  130. pbfh2 =    &pbafh2->bfh2;
  131. abm[cBitmaps].cColours = (ULONG)(1 << pbfh2->bmp2.cBitCount);
  132. abm[cBitmaps++].hbm = GpiCreateBitmap(hpsMem, &pbfh2->bmp2, CBM_INIT,
  133.                       (PBYTE)((PBYTE)pbafh2 + pbafh2->bfh2.offBits),
  134.                       (PBITMAPINFO2)(PVOID)&pbfh2->bmp2);
  135.  
  136.                /* Scan the bitmap array    for the    desired    icon    */
  137.                /* type based on    the current screen display    */
  138. while (    pbafh2->offNext    )
  139.    {
  140.                /* Point    to the next array header        */
  141.  
  142.    abm[cBitmaps].pb = (PBYTE)(pbafh2 = (PBITMAPARRAYFILEHEADER2)((PBYTE)pbafhOrg + pbafh2->offNext));
  143.    abm[cBitmaps].cColours = (ULONG)(1 << pbafh2->bfh2.bmp2.cBitCount);
  144.  
  145.                /* Create a memory presentation space using the    */
  146.                /* desktop window handle    and create the colour    */
  147.                /* bitmap                    */
  148.  
  149.    abm[cBitmaps].hbm = GpiCreateBitmap(hpsMem, &pbafh2->bfh2.bmp2, CBM_INIT,
  150.                        (PBYTE)((PBYTE)pbafhOrg + pbafh2->bfh2.offBits),
  151.                        (PBITMAPINFO2)(PVOID)&pbafh2->bfh2.bmp2);
  152.    if (    ++cBitmaps == 32L )
  153.        break;
  154.    }
  155.                /* Release the memory presentation space        */
  156. WinReleasePS(hpsMem);
  157. }
  158. #pragma    subtitle("   Bitmap Retrieve - Select Bitmap File Header Function")
  159. #pragma    page( )
  160.  
  161. /* --- BuildBitmapStack1 ------------------------------    [ Private ] ---    */
  162. /*                                    */
  163. /*     This function is    used to    build the bitmap images    for a bitmap    */
  164. /*     array and place the bitmaps within the bitmap stack.        */
  165. /*                                    */
  166. /*     Upon Entry:                            */
  167. /*                                    */
  168. /*     PBITMAPARRAYFILEHEADER2 pbafh2; = Bitmap    Array Header Pointer    */
  169. /*                                    */
  170. /*     Upon Exit:                            */
  171. /*                                    */
  172. /*     Nothing                                */
  173. /*                                    */
  174. /* --------------------------------------------------------------------    */
  175.  
  176. static VOID BuildBitmapStack1(PBITMAPARRAYFILEHEADER pbafh)
  177.  
  178. {
  179. PBITMAPARRAYFILEHEADER pbafhOrg;   /* Bitmap Array File    Header Origin    */
  180. PBITMAPFILEHEADER      pbfh;       /* Bitmap Array File    Header Pointer    */
  181. HPS               hpsMem;       /* Memory Presentation Space    Handle    */
  182. register INT i;               /* Loop Counter            */
  183.  
  184.                /* Delete any previously    loaded bitmaps        */
  185.  
  186. for ( i    = 0; i < cBitmaps; i++ )
  187.    GpiDeleteBitmap(abm[i].hbm);
  188.  
  189. cBitmaps = 0L;
  190. hpsMem = WinGetPS(HWND_DESKTOP);
  191.  
  192.                /* Save the start of the    bitmap file header    */
  193.                /* since    all offsets are    from the beginning of    */
  194.                /* the header                    */
  195.  
  196. abm[cBitmaps].pb    = (PBYTE)(pbafhOrg = pbafh);
  197.  
  198.                /* Point    to the independent bitmap file header    */
  199.                /* since    it is the default            */
  200. pbfh = &pbafh->bfh;
  201. abm[cBitmaps].cColours = (ULONG)(1 << pbfh->bmp.cBitCount);
  202. abm[cBitmaps++].hbm = GpiCreateBitmap(hpsMem, (PBITMAPINFOHEADER2)&pbfh->bmp, CBM_INIT,
  203.                       (PBYTE)((PBYTE)pbafh + pbafh->bfh.offBits),
  204.                       (PBITMAPINFO2)(PVOID)&pbfh->bmp);
  205.  
  206.                /* Scan the bitmap array    for the    desired    icon    */
  207.                /* type based on    the current screen display    */
  208. while (    pbafh->offNext )
  209.    {
  210.                /* Point    to the next array header        */
  211.  
  212.    abm[cBitmaps].pb = (PBYTE)(pbafh = (PBITMAPARRAYFILEHEADER)((PBYTE)pbafhOrg + pbafh->offNext));
  213.    abm[cBitmaps].cColours = (ULONG)(1 << pbafh->bfh.bmp.cBitCount);
  214.  
  215.                /* Create a memory presentation space using the    */
  216.                /* desktop window handle    and create the colour    */
  217.                /* bitmap                    */
  218.  
  219.    abm[cBitmaps].hbm = GpiCreateBitmap(hpsMem, (PBITMAPINFOHEADER2)&pbafh->bfh.bmp, CBM_INIT,
  220.                        (PBYTE)((PBYTE)pbafhOrg + pbafh->bfh.offBits),
  221.                        (PBITMAPINFO2)(PVOID)&pbafh->bfh.bmp);
  222.    if (    ++cBitmaps == 32L )
  223.        break;
  224.    }
  225.                /* Release the memory presentation space        */
  226. WinReleasePS(hpsMem);
  227. }
  228. #pragma    subtitle("   Bitmap Retrieve - Select Bitmap File Header Function")
  229. #pragma    page( )
  230.  
  231. /* --- lSelectBfh -------------------------------------    [ Private ] ---    */
  232. /*                                    */
  233. /*     This function is    used to    move through the bitmap    array file    */
  234. /*     header passed to    it to locate the appropriate bitmap file    */
  235. /*     header.    Two methods can    be used    to locate the bitmap:  use    */
  236. /*     the cxDisplay and cyDisplay to match the    screen size; or    to    */
  237. /*     use the independent form    which located first in the file.    */
  238. /*                                    */
  239. /*     Upon Entry:                            */
  240. /*                                    */
  241. /*     PBITMAPARRAYFILEHEADER2 pbafh2; = Bitmap    Array Header Pointer    */
  242. /*                                    */
  243. /*     Upon Exit:                            */
  244. /*                                    */
  245. /*     lSelectBfh = Selected Bitmap Index                */
  246. /*                                    */
  247. /* --------------------------------------------------------------------    */
  248.  
  249. static LONG lSelectBfh(PBITMAPARRAYFILEHEADER2 pbafh2)
  250.  
  251. {
  252. PBITMAPARRAYFILEHEADER2    pbafhOrg;  /* Bitmap Array File    Header Origin    */
  253. USHORT            cxDisplay; /* Bitmap Screen Design Width    */
  254. USHORT            cyDisplay; /* Bitmap Screen Design Height    */
  255. USHORT            cxTarget;  /* Bitmap Screen Design Width    */
  256. USHORT            cyTarget;  /* Bitmap Screen Design Height    */
  257.  
  258.                /* Check    to make    sure that a bitmap array has    */
  259.                /* been read in                    */
  260.  
  261. if ( pbafh2->usType != BFT_BITMAPARRAY )
  262.  
  263.                /* Not a    bitmap array, return the beginning of    */
  264.                /* header to ensure proper decoding        */
  265.    return(iBitmap);
  266. else
  267.    {
  268.    cxDisplay = cyDisplay = (USHORT)0;
  269.    if (    (cxScreen >= 1024L) && (cxScreen >= 768L) )
  270.        {
  271.        cxTarget    = (USHORT)1024;
  272.        cyTarget    = (USHORT)768;
  273.        }
  274.    else
  275.        {
  276.        cxTarget    = (USHORT)640;
  277.        cyTarget    = (USHORT)480;
  278.        }
  279.    }
  280.                /* Save the start of the    bitmap file header    */
  281.                /* since    all offsets are    from the beginning of    */
  282.                /* the header                    */
  283. pbafhOrg = pbafh2;
  284.                /* Check    to see if the current entry's size      */
  285.                /* matches that of the current screen        */
  286.  
  287. if ( (pbafh2->cxDisplay    == cxTarget) &&    (pbafh2->cyDisplay == cyTarget)    )
  288.    return(iBitmap);
  289. else
  290.    if (    (pbafh2->cxDisplay > cxDisplay)    && (pbafh2->cyDisplay >    cyDisplay) )
  291.        {
  292.        cxDisplay = pbafh2->cxDisplay;
  293.        cyDisplay = pbafh2->cyDisplay;
  294.        }
  295.                /* Scan the bitmap array    for the    desired    icon    */
  296.                /* type based on    the current screen display    */
  297. while (    pbafh2->offNext    )
  298.    {
  299.                /* Point    to the next array header        */
  300.  
  301.    pbafh2 = (PBITMAPARRAYFILEHEADER2)((PBYTE)pbafhOrg +    pbafh2->offNext);
  302.    ++iBitmap;
  303.                /* Check    to see if the current entry's size      */
  304.                /* matches that of the current screen        */
  305.  
  306.    if (    (pbafh2->cxDisplay == cxTarget)    && (pbafh2->cyDisplay == cyTarget) )
  307.        return(iBitmap);
  308.    else
  309.        if ( (pbafh2->cxDisplay > cxDisplay) && (pbafh2->cyDisplay > cyDisplay) )
  310.        {
  311.        cxDisplay = pbafh2->cxDisplay;
  312.        cyDisplay = pbafh2->cyDisplay;
  313.        }
  314.    }
  315.                /* Return the address to    the desired bitmap    */
  316.                /* array    file header                */
  317. return(iBitmap);
  318. }
  319. #pragma    subtitle("   Bitmap Retrieve - Select Bitmap File Header Function")
  320. #pragma    page( )
  321.  
  322. /* --- lSelectBfh1 ------------------------------------    [ Private ] ---    */
  323. /*                                    */
  324. /*     This function is    used to    move through the bitmap    array file    */
  325. /*     header passed to    it to locate the appropriate bitmap file    */
  326. /*     header.    Two methods can    be used    to locate the bitmap:  use    */
  327. /*     the cxDisplay and cyDisplay to match the    screen size; or    to    */
  328. /*     use the independent form    which located first in the file.    */
  329. /*                                    */
  330. /*     Upon Entry:                            */
  331. /*                                    */
  332. /*     PBITMAPARRAYFILEHEADER pbafh; = Bitmap Array File Pointer    */
  333. /*                                    */
  334. /*     Upon Exit:                            */
  335. /*                                    */
  336. /*     lSelectBfh1 = Selected Bitmap Index                */
  337. /*                                    */
  338. /* --------------------------------------------------------------------    */
  339.  
  340. static LONG lSelectBfh1(PBITMAPARRAYFILEHEADER pbafh)
  341.  
  342. {
  343. PBITMAPARRAYFILEHEADER pbafhOrg;   /* Bitmap Array File    Header Origin    */
  344. USHORT               cxDisplay;  /* Bitmap Screen Design Width    */
  345. USHORT               cyDisplay;  /* Bitmap Screen Design Height    */
  346. USHORT               cxTarget;   /* Bitmap Screen Design Width    */
  347. USHORT               cyTarget;   /* Bitmap Screen Design Height    */
  348.  
  349.                /* Check    to make    sure that a bitmap array has    */
  350.                /* been read in                    */
  351.  
  352. if ( pbafh->usType != BFT_BITMAPARRAY )
  353.  
  354.                /* Not a    bitmap array, return the beginning of    */
  355.                /* header to ensure proper decoding        */
  356.    return(iBitmap);
  357. else
  358.    {
  359.    cxDisplay = cyDisplay = (USHORT)0;
  360.    if (    (cxScreen >= 1024L) && (cxScreen >= 768L) )
  361.        {
  362.        cxTarget    = (USHORT)1024;
  363.        cyTarget    = (USHORT)768;
  364.        }
  365.    else
  366.        {
  367.        cxTarget    = (USHORT)640;
  368.        cyTarget    = (USHORT)480;
  369.        }
  370.    }
  371.                /* Save the start of the    bitmap file header    */
  372.                /* since    all offsets are    from the beginning of    */
  373.                /* the header                    */
  374. pbafhOrg = pbafh;
  375.                /* Check    to see if the current entry's size      */
  376.                /* matches that of the current screen        */
  377.  
  378. if ( (pbafh->cxDisplay == cxTarget) && (pbafh->cyDisplay == cyTarget) )
  379.    return(iBitmap);
  380. else
  381.    if (    (pbafh->cxDisplay > cxDisplay) && (pbafh->cyDisplay > cyDisplay) )
  382.        {
  383.        cxDisplay = pbafh->cxDisplay;
  384.        cyDisplay = pbafh->cyDisplay;
  385.        }
  386.                /* Scan the bitmap array    for the    desired    icon    */
  387.                /* type based on    the current screen display    */
  388. while (    pbafh->offNext )
  389.    {
  390.                /* Point    to the next array header        */
  391.  
  392.    pbafh = (PBITMAPARRAYFILEHEADER)((PBYTE)pbafhOrg + pbafh->offNext);
  393.    ++iBitmap;
  394.                /* Check    to see if the current entry's size      */
  395.                /* matches that of the current screen        */
  396.  
  397.    if (    (pbafh->cxDisplay == cxTarget) && (pbafh->cyDisplay == cyTarget) )
  398.        return(iBitmap);
  399.    else
  400.        if ( (pbafh->cxDisplay >    cxDisplay) && (pbafh->cyDisplay    > cyDisplay) )
  401.        {
  402.        cxDisplay = pbafh->cxDisplay;
  403.        cyDisplay = pbafh->cyDisplay;
  404.        }
  405.    }
  406.                /* Return the address to    the desired bitmap    */
  407.                /* array    file header                */
  408. return(iBitmap);
  409. }
  410. #pragma    subtitle("   Bitmap Retrieve - Retrieve Single Bitmap Function")
  411. #pragma    page( )
  412.  
  413. /* --- hbmGetSingleBitmap -----------------------------    [ Private ] ---    */
  414. /*                                    */
  415. /*     This function is    used to    load a single bitmap from a bitmap    */
  416. /*     file.                                */
  417. /*                                    */
  418. /*     Upon Entry:                            */
  419. /*                                    */
  420. /*     PBITMAPFILEHEADER pbafh;    = Bitmap File Header Pointer        */
  421. /*                                    */
  422. /*     Upon Exit:                            */
  423. /*                                    */
  424. /*     hbmGetSingleBitmap = Bitmap Handle                */
  425. /*                                    */
  426. /* --------------------------------------------------------------------    */
  427.  
  428. static HBITMAP hbmGetSingleBitmap(PBITMAPFILEHEADER2 pbfh2)
  429.  
  430. {
  431. HPS    hPS;               /* Presentation Space        */
  432. register INT i;               /* Loop Counter            */
  433.  
  434.                /* Check    to see that the    size of    the bitmap    */
  435.                /* info header is correct otherwise the bitmap    */
  436.                /* cannot be created                */
  437.  
  438. if ( pbfh2->bmp2.cbFix != sizeof(BITMAPINFOHEADER2) )
  439.  
  440.                /* Invalid header size, return NULL bitmap    */
  441.                /* handle                    */
  442.    return((HBITMAP)NULL);
  443.                /* Delete any previously    loaded bitmaps        */
  444.  
  445. for ( i    = 0; i < cBitmaps; i++ )
  446.    GpiDeleteBitmap(abm[i].hbm);
  447.  
  448.                /* Save the start of the    bitmap file header    */
  449.                /* since    all offsets are    from the beginning of    */
  450.                /* the header                    */
  451. cBitmaps = 1L;
  452. abm[0].pb = (PBYTE)pbfh2;
  453. abm[0].cColours    = (ULONG)(1 << pbfh2->bmp2.cBitCount);
  454.  
  455.                /* Point    to the independent bitmap file header    */
  456.                /* since    it is the default            */
  457.  
  458. abm[0].hbm = GpiCreateBitmap(hPS = WinGetPS(HWND_DESKTOP),
  459.                  &(pbfh2->bmp2), CBM_INIT, (PBYTE)pbfh2 + pbfh2->offBits,
  460.                  (PBITMAPINFO2)(PVOID)&pbfh2->bmp2);
  461.  
  462.                /* Release the desktop presentation space    */
  463. WinReleasePS(hPS);
  464.                /* Return the bitmap handle            */
  465. return(abm[0].hbm);
  466.  
  467. }
  468. #pragma    subtitle("   Bitmap Retrieve - Retrieve Single Bitmap Function")
  469. #pragma    page( )
  470.  
  471. /* --- hbmGetSingleBitmap1 ----------------------------    [ Private ] ---    */
  472. /*                                    */
  473. /*     This function is    used to    load a single bitmap from a bitmap    */
  474. /*     file.                                */
  475. /*                                    */
  476. /*     Upon Entry:                            */
  477. /*                                    */
  478. /*     PBITMAPFILEHEADER pbafh;    = Bitmap File Header Pointer        */
  479. /*                                    */
  480. /*     Upon Exit:                            */
  481. /*                                    */
  482. /*     hbmGetSingleBitmap1 = Bitmap Handle                */
  483. /*                                    */
  484. /* --------------------------------------------------------------------    */
  485.  
  486. static HBITMAP hbmGetSingleBitmap1(PBITMAPFILEHEADER pbfh)
  487.  
  488. {
  489. HPS    hPS;               /* Presentation Space        */
  490. register INT i;               /* Loop Counter            */
  491.  
  492.                /* Check    to see that the    size of    the bitmap    */
  493.                /* info header is correct otherwise the bitmap    */
  494.                /* cannot be created                */
  495.  
  496. if ( pbfh->bmp.cbFix !=    sizeof(BITMAPINFOHEADER) )
  497.  
  498.                /* Invalid header size, return NULL bitmap    */
  499.                /* handle                    */
  500.    return((HBITMAP)NULL);
  501.                /* Delete any previously    loaded bitmaps        */
  502.  
  503. for ( i    = 0; i < cBitmaps; i++ )
  504.    GpiDeleteBitmap(abm[i].hbm);
  505.  
  506.                /* Save the start of the    bitmap file header    */
  507.                /* since    all offsets are    from the beginning of    */
  508.                /* the header                    */
  509. cBitmaps = 1L;
  510. abm[0].pb = (PBYTE)pbfh;
  511. abm[0].cColours    = (ULONG)(1 << pbfh->bmp.cBitCount);
  512.  
  513.                /* Get the desktop presentation space to    create    */
  514.                /* the bitmap                    */
  515.  
  516. abm[0].hbm = GpiCreateBitmap(hPS = WinGetPS(HWND_DESKTOP),
  517.                  (PBITMAPINFOHEADER2)(PVOID)&pbfh->bmp,
  518.                  CBM_INIT, (BYTE *)pbfh + pbfh->offBits, (PBITMAPINFO2)(PVOID)&pbfh->bmp);
  519.  
  520.                /* Release the desktop presentation space    */
  521. WinReleasePS(hPS);
  522.                /* Return the bitmap handle            */
  523. return(abm[0].hbm);
  524. }
  525. #pragma    subtitle("   Bitmap Retrieve - Bitmap Retrieve Function")
  526. #pragma    page( )
  527.  
  528. /* --- hbmGetBitmap ------------------------------------ [ Public ] ---    */
  529. /*                                    */
  530. /*     This function is    used to    load the requested bitmap file and to    */
  531. /*     create a    bitmap that can    be displayed through Prominare        */
  532. /*     Designer.                            */
  533. /*                                    */
  534. /*     Upon Entry:                            */
  535. /*                                    */
  536. /*     PSZ pszFileName;    = Bitmap Filename                */
  537. /*                                    */
  538. /*     Upon Exit:                            */
  539. /*                                    */
  540. /*     hbmGetBitmap = Bitmap Handle                    */
  541. /*                                    */
  542. /* --------------------------------------------------------------------    */
  543.  
  544. HBITMAP    hbmGetBitmap(PSZ pszFileName)
  545.  
  546. {
  547. FILESTATUS3           fs;       /* File Status Information        */
  548. HFILE               hFile;       /* File Handle            */
  549. HBITMAP               hbm   = 0;  /* Bitmap Handle            */
  550. PBITMAPARRAYFILEHEADER pbafh = 0;  /* Bitmap File Header Pointer    */
  551. ULONG               cbRead;       /* Bytes Read            */
  552. ULONG               ulResult;   /* File Open    Fail Result        */
  553.  
  554.                /* Open the requested bitmap file        */
  555.  
  556. if ( DosOpen(pszFileName, &hFile, &ulResult, 0UL, FILE_NORMAL, OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_FAIL_IF_NEW,
  557.          OPEN_ACCESS_READONLY | OPEN_SHARE_DENYNONE, NULL) )
  558.  
  559.                /* Could    not open the file, return NULL bitmap    */
  560.                /* pointer handle                */
  561.    return((HBITMAP)NULL);
  562.                /* Get the file information to allow the    proper    */
  563.                /* amount of memory to be allocated for the    */
  564.                /* bitmap information                */
  565.  
  566. DosQueryFileInfo(hFile,    FIL_STANDARD, (PVOID)&fs, sizeof(FILESTATUS3));
  567.  
  568.                /* Allocate memory for the bitmap information    */
  569.  
  570. if ( (pbafh = (PBITMAPARRAYFILEHEADER)malloc(fs.cbFile)) == NULL )
  571.  
  572.                /* Memory allocation failed, return NULL    bitmap    */
  573.                /* pointer handle                */
  574.    return((HBITMAP)NULL);
  575.                /* Read into memory the bitmap file        */
  576.  
  577. if ( DosRead(hFile, (PVOID)pbafh, fs.cbFile, (PULONG)&cbRead) || (cbRead != fs.cbFile) )
  578.    {
  579.                /* Release the memory allocated for the bitmap    */
  580.                /* information                    */
  581.    free(pbafh);
  582.                /* Close    the bitmap file                */
  583.    DosClose(hFile);
  584.                /* Error    occurred in reading the    bitmap file,    */
  585.                /* return NULL bitmap handle            */
  586.    return((HBITMAP)NULL);
  587.    }
  588. else
  589.                /* Close    the bitmap file                */
  590.    DosClose(hFile);
  591.  
  592. iBitmap    = 0L;
  593.  
  594. if ( (pbafh->cbSize == sizeof(BITMAPARRAYFILEHEADER)) || (pbafh->cbSize    == sizeof(BITMAPFILEHEADER)) )
  595.    {
  596.    fWindowsBitmap = f20Bitmap =    fBitmapArray = FALSE;
  597.  
  598.                /* Check    to see if the bitmap is    monochrome    */
  599.  
  600.    if (    pbafh->usType == BFT_BMAP )
  601.  
  602.                /* Monochrome bitmap, convert into a memory    */
  603.                /* bitmap and a valid bitmap handle        */
  604.  
  605.        hbm = hbmGetSingleBitmap1((PBITMAPFILEHEADER)(PVOID)pbafh);
  606.    else
  607.                /* Check    to see if the icon is a    bitmap array    */
  608.                /* thereby indicating a coloured    bitmap        */
  609.  
  610.        if ( pbafh->usType == BFT_BITMAPARRAY )
  611.        {
  612.                /* Coloured bitmap, convert into    a memory bitmap    */
  613.                /* bitmap and a valid bitmap handle        */
  614.  
  615.        BuildBitmapStack1((PBITMAPARRAYFILEHEADER)pbafh);
  616.        hbm = abm[lSelectBfh1((PBITMAPARRAYFILEHEADER)pbafh)].hbm;
  617.        fBitmapArray    = TRUE;
  618.        }
  619.    }
  620. else
  621.    if (    (pbafh->cbSize == sizeof(BITMAPARRAYFILEHEADER2)) || (pbafh->cbSize == sizeof(BITMAPFILEHEADER2)) )
  622.        {
  623.        f20Bitmap = TRUE;
  624.        fWindowsBitmap =    fBitmapArray = FALSE;
  625.  
  626.                /* Check    to see if the bitmap is    monochrome    */
  627.  
  628.        if ( pbafh->usType == BFT_BMAP )
  629.  
  630.                /* Monochrome bitmap, convert into a memory    */
  631.                /* bitmap and a valid bitmap handle        */
  632.  
  633.        hbm = hbmGetSingleBitmap((PBITMAPFILEHEADER2)(PVOID)pbafh);
  634.        else
  635.                /* Check    to see if the icon is a    bitmap array    */
  636.                /* thereby indicating a coloured    bitmap        */
  637.  
  638.        if (    pbafh->usType == BFT_BITMAPARRAY )
  639.            {
  640.                /* Coloured bitmap, convert into    a memory bitmap    */
  641.                /* bitmap and a valid bitmap handle        */
  642.  
  643.            BuildBitmapStack((PBITMAPARRAYFILEHEADER2)(PVOID)pbafh);
  644.            hbm = abm[lSelectBfh((PBITMAPARRAYFILEHEADER2)(PVOID)pbafh)].hbm;
  645.            fBitmapArray = TRUE;
  646.            }
  647.        }
  648.    else
  649.                /* Appears to be    a Windows bitmap, try to    */
  650.                /* convert it to    an OS/2    2.x format bitmap    */
  651.        {
  652.        fBitmapArray = FALSE;
  653.        hbm = hbmGetSingleBitmap((PBITMAPFILEHEADER2)(pbafh = (PBITMAPARRAYFILEHEADER)pbfh2GetWindowsBmp((PBYTE)pbafh, fs.cbFile)));
  654.        }
  655.  
  656. if ( pb    )
  657.    free(pb);
  658. pb = (PBYTE)pbafh;
  659.                /* Return the bitmap handle            */
  660. return(hbm);
  661.  
  662. }
  663. #pragma    subtitle("   Bitmap Retrieve - Windows Bitmap Image Retrieve Function")
  664. #pragma    page( )
  665.  
  666. /* --- pbfh2GetWindowsBmp ------------------------------ [ Public ] ---    */
  667. /*                                    */
  668. /*     This function is    used to    convert    a Windows bitmap to an OS/2    */
  669. /*     2.x type    of bitmap.                        */
  670. /*                                    */
  671. /*     Upon Entry:                            */
  672. /*                                    */
  673. /*     PBYTE pbBitmap; = Pointer to Windows Bitmap Data            */
  674. /*     ULONG cbData;   = Windows Bitmap    Data Size            */
  675. /*                                    */
  676. /*     Upon Exit:                            */
  677. /*                                    */
  678. /*     pbfh2GetWindowsBmp = Convert Bitmap Data    Pointer            */
  679. /*                                    */
  680. /* --------------------------------------------------------------------    */
  681.  
  682. static PBITMAPFILEHEADER2 pbfh2GetWindowsBmp(PBYTE pbBitmap, ULONG cbData)
  683.  
  684. {
  685. PBITMAPFILEHEADER2   pbfh2;       /* Bitmap File Header Pointer    */
  686. PULONG             pul;       /* Value Pointer            */
  687. PwinRGBQUAD         pargbq;       /* RGB Quad Array Pointer        */
  688. PwinRGBTRIPLE         pargbt;       /* RGB Triple Array Pointer        */
  689. ULONG             cClrs;       /* Colours Table Count        */
  690. ULONG             cb;       /* Image Size            */
  691. winBITMAPCOREHEADER  bmpc;       /* Bitmap Information Header        */
  692. winBITMAPINFOHEADER  bmpi;       /* Bitmap Information Header        */
  693. PwinBITMAPFILEHEADER pwbfh;       /* Bitmap File Header        */
  694. PBYTE             pbImage;       /* Image Pointer            */
  695. PBYTE             pbClrTable;   /* Colour Table Pointer        */
  696. register INT i;               /* Loop Counter            */
  697.  
  698. pwbfh =    (PwinBITMAPFILEHEADER)pbBitmap;
  699.  
  700. if ( (pwbfh->bfType == BFT_BMAP) || (pwbfh->bfSize == cbData) )
  701.    {
  702.                /* Check    to see if the bitmap is    a variation of    */
  703.                /* the bitmap layouts in    which case, transfer    */
  704.                /* the info and point to    the RGB    colour table    */
  705.  
  706.    if (    pwbfh->bmpi.biSize == sizeof(winBITMAPCOREHEADER) )
  707.        {
  708.        memset(&bmpi, 0,    sizeof(winBITMAPINFOHEADER));
  709.        memcpy(&bmpc, &pwbfh->bmpi, sizeof(winBITMAPCOREHEADER));
  710.        bmpi.biWidth    = bmpc.bcWidth;
  711.        bmpi.biHeight   = bmpc.bcHeight;
  712.        bmpi.biPlanes   = bmpc.bcPlanes;
  713.        bmpi.biBitCount = bmpc.bcBitCount;
  714.        pbClrTable = pbBitmap + (sizeof(winBITMAPFILEHEADER) - sizeof(winBITMAPINFOHEADER)) + sizeof(winBITMAPCOREHEADER);
  715.        }
  716.    else
  717.                /* Standard bitmap format, transfer the info and    */
  718.                /* point    to the colour table            */
  719.        {
  720.        memcpy(&bmpi, &pwbfh->bmpi, sizeof(winBITMAPINFOHEADER));
  721.        pbClrTable = pbBitmap + sizeof(winBITMAPFILEHEADER);
  722.        }
  723.                /* Point    to the image data of the bitmap        */
  724.  
  725.    pbImage = pbBitmap +    pwbfh->bfOffBits;
  726.  
  727.                /* Get the colours used count and when zero    */
  728.                /* the colours are defined to be    the maximum    */
  729.                /* values for the bit count            */
  730.  
  731.    if (    (cClrs = bmpi.biClrUsed) == 0 )
  732.        switch (    bmpi.biBitCount    )
  733.        {
  734.                /* 2 colour bitmap                */
  735.        case    1 :
  736.            cClrs = 2UL;
  737.            break;
  738.                /* 16 colour bitmap                */
  739.        case    4 :
  740.            cClrs = 16UL;
  741.            break;
  742.                /* 256 colour bitmap                */
  743.        case    8 :
  744.            cClrs = 256UL;
  745.            break;
  746.                /* 16 MB    colour bitmap, no colour table defined    */
  747.        default :
  748.            cClrs = 0UL;
  749.            break;
  750.        }
  751.                /* When colour table defined for    bitmap,        */
  752.                /* allocated enough memory for it and then read    */
  753.                /* it in    from disk                */
  754.    if (    cClrs )
  755.                /* Check    to see if the bitmap is    a variant in    */
  756.                /* which    case the RGB colours are like the OS/2    */
  757.                /* 1.x colours, 3 bytes in size,    therefore need    */
  758.                /* to be    transferred individually        */
  759.  
  760.        if ( pwbfh->bmpi.biSize == sizeof(winBITMAPCOREHEADER) )
  761.        {
  762.        memset(pargbq = (winRGBQUAD *)malloc(sizeof(winRGBQUAD) * cClrs),
  763.           0, sizeof(winRGBQUAD)    * cClrs);
  764.        pargbt = (PwinRGBTRIPLE)pbClrTable;
  765.        for ( i = 0;    i < cClrs; i++ )
  766.            {
  767.            pargbq[i].rgbBlue  = pargbt[i].rgbBlue;
  768.            pargbq[i].rgbGreen = pargbt[i].rgbGreen;
  769.            pargbq[i].rgbRed      = pargbt[i].rgbRed;
  770.            }
  771.        }
  772.        else
  773.                /* Standard colour table, transfer the info    */
  774.  
  775.        memcpy(pargbq = (winRGBQUAD *)malloc(sizeof(winRGBQUAD) * cClrs),
  776.           pbClrTable, sizeof(winRGBQUAD) * cClrs);
  777.    else
  778.                /* No colour table                */
  779.  
  780.        pargbq =    (PwinRGBQUAD)NULL;
  781.  
  782.    if (    bmpi.biSizeImage )
  783.        cb = bmpi.biSizeImage;
  784.    else
  785.        if ( pwbfh->bmpi.biSize == sizeof(winBITMAPCOREHEADER) )
  786.        cb =    (ULONG)(cbData - (sizeof(winBITMAPFILEHEADER) -    sizeof(winBITMAPINFOHEADER)) - (sizeof(winRGBTRIPLE) * cClrs));
  787.        else
  788.        cb =    (ULONG)(cbData - sizeof(winBITMAPFILEHEADER) - (sizeof(winRGBQUAD) * cClrs));
  789.    }
  790. else
  791.    {
  792.  
  793.    pul = (PULONG)pbBitmap;
  794.  
  795.    if (    *pul ==    sizeof(winBITMAPCOREHEADER) )
  796.        {
  797.                /* Read the bitmap file info header from    the    */
  798.                /* bitmap                    */
  799.  
  800.        memcpy(&bmpc, pbBitmap, sizeof(winBITMAPCOREHEADER));
  801.        pbImage = pbBitmap + sizeof(winBITMAPCOREHEADER);
  802.        memset(&bmpi, 0,    sizeof(winBITMAPINFOHEADER));
  803.        bmpi.biWidth    = bmpc.bcWidth;
  804.        bmpi.biHeight   = bmpc.bcHeight;
  805.        bmpi.biPlanes   = bmpc.bcPlanes;
  806.        bmpi.biBitCount = bmpc.bcBitCount;
  807.        }
  808.    else
  809.                /* Read the bitmap file info header from    the    */
  810.                /* bitmap                    */
  811.        {
  812.        memcpy(&bmpi, pbBitmap, sizeof(winBITMAPINFOHEADER));
  813.        pbImage = pbBitmap + sizeof(winBITMAPINFOHEADER);
  814.        }
  815.                /* Get the colours used count and when zero    */
  816.                /* the colours are defined to be    the maximum    */
  817.                /* values for the bit count            */
  818.  
  819.    if (    (cClrs = bmpi.biClrUsed) == 0 )
  820.        switch (    bmpi.biBitCount    )
  821.        {
  822.                /* 2 colour bitmap                */
  823.        case    1 :
  824.            cClrs = 2UL;
  825.            break;
  826.                /* 16 colour bitmap                */
  827.        case    4 :
  828.            cClrs = 16UL;
  829.            break;
  830.                /* 256 colour bitmap                */
  831.        case    8 :
  832.            cClrs = 256UL;
  833.            break;
  834.                /* 16 MB    colour bitmap, no colour table defined    */
  835.        default :
  836.            cClrs = 0UL;
  837.            break;
  838.        }
  839.                /* When colour table defined for    bitmap,        */
  840.                /* allocated enough memory for it and then read    */
  841.                /* it in    from disk                */
  842.    if (    cClrs )
  843.        {
  844.        memcpy(pargbq = (winRGBQUAD *)malloc(sizeof(winRGBQUAD) * cClrs),
  845.           pbBitmap,    sizeof(winRGBQUAD) * cClrs);
  846.        pbImage = pbImage + (sizeof(winRGBQUAD) * cClrs);
  847.        }
  848.    else
  849.        pargbq =    (PwinRGBQUAD)NULL;
  850.  
  851.    cb =    (ULONG)(cbData - sizeof(winBITMAPINFOHEADER) - (sizeof(winRGBQUAD) * cClrs));
  852.    }
  853.                /* Initialize the bitmap    array header        */
  854.  
  855. memset(pbfh2 = (PBITMAPFILEHEADER2)malloc(sizeof(BITMAPFILEHEADER2) + cClrs * sizeof(RGB2) + cb),
  856.        0, sizeof(BITMAPFILEHEADER2));
  857.  
  858.                /* Initialize the first bitmap header for the    */
  859.                /* bitmap                    */
  860.  
  861. pbfh2->usType    = (USHORT)BFT_BMAP;
  862. pbfh2->cbSize    = sizeof(BITMAPFILEHEADER2);
  863. pbfh2->xHotspot    = (SHORT)(bmpi.biWidth    / 2);
  864. pbfh2->yHotspot    = (SHORT)(bmpi.biHeight    / 2);
  865. pbfh2->offBits    = sizeof(BITMAPFILEHEADER2) + sizeof(RGB2) * cClrs;
  866.  
  867.                /* Initialize the bitmap    information for    the    */
  868.                /* bitmap based on the values from the Windows    */
  869.                /* 3.1 bitmap                    */
  870.  
  871. pbfh2->bmp2.cbFix      = sizeof(BITMAPINFOHEADER2);
  872. pbfh2->bmp2.cx          = (ULONG)bmpi.biWidth;
  873. pbfh2->bmp2.cy          = (ULONG)bmpi.biHeight;
  874. pbfh2->bmp2.cPlanes      =       bmpi.biPlanes;
  875. pbfh2->bmp2.cBitCount      =       bmpi.biBitCount;
  876. pbfh2->bmp2.cbImage      =       cb;
  877. pbfh2->bmp2.cclrUsed      = cClrs;
  878. pbfh2->bmp2.cclrImportant = cClrs;
  879.  
  880.                /* When a colour    table present, write out the    */
  881.                /* colour table                    */
  882. if ( cClrs )
  883.    {
  884.    memcpy((PBYTE)pbfh2 + sizeof(BITMAPFILEHEADER2), pargbq, sizeof(winRGBQUAD) * cClrs);
  885.    memcpy((PBYTE)pbfh2 + sizeof(BITMAPFILEHEADER2) + sizeof(RGB2) * cClrs, pbImage, cb);
  886.    }
  887. else
  888.    memcpy((PBYTE)pbfh2 + sizeof(BITMAPFILEHEADER2), pbImage, cb);
  889.  
  890.                /* If a colour table was    present, release the    */
  891.                /* memory allocated for it            */
  892. if ( cClrs )
  893.    free(pargbq);
  894.  
  895. fWindowsBitmap = f20Bitmap = TRUE;
  896.  
  897. free(pbBitmap);
  898.  
  899. return(pbfh2);
  900. }
  901.