home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / bmp2.zip / GETBMP.C < prev    next >
C/C++ Source or Header  |  1995-07-22  |  32KB  |  905 lines

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