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

  1. #pragma    title("View Bitmap  --  Version 1  --  (GetBmp.C)")
  2. #pragma    subtitle("   Bitmap Routines - 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-11-06                        */
  23. /*  Revised:   1995-11-06                        */
  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 VOID GetSingleBitmap(PBITMAPFILEHEADER2 pbfh2);
  81. static VOID GetSingleBitmap1(PBITMAPFILEHEADER pbfh);
  82.  
  83. static PBITMAPFILEHEADER2 pbfh2GetWindowsBmp(PBYTE pbData, ULONG cbData);
  84.  
  85. #pragma    subtitle("   Bitmap Retrieve - Select Bitmap File Header Function")
  86. #pragma    page( )
  87.  
  88. /* --- BuildBitmapStack    -------------------------------    [ Private ] ---    */
  89. /*                                    */
  90. /*     This function is    used to    build the bitmap images    for a bitmap    */
  91. /*     array and place the bitmaps within the bitmap stack.        */
  92. /*                                    */
  93. /*     Upon Entry:                            */
  94. /*                                    */
  95. /*     PBITMAPARRAYFILEHEADER2 pbafh2; = Bitmap    Array Header Pointer    */
  96. /*                                    */
  97. /*     Upon Exit:                            */
  98. /*                                    */
  99. /*     Nothing                                */
  100. /*                                    */
  101. /* --------------------------------------------------------------------    */
  102.  
  103. static VOID BuildBitmapStack(PBITMAPARRAYFILEHEADER2 pbafh2)
  104.  
  105. {
  106. PBITMAPARRAYFILEHEADER2    pbafhOrg;  /* Bitmap Array File    Header Origin    */
  107. PBITMAPFILEHEADER2    pbfh2;       /* Bitmap Array File    Header Pointer    */
  108. HPS            hpsMem;       /* Memory Presentation Space    Handle    */
  109.  
  110. hpsMem = WinGetPS(HWND_DESKTOP);
  111. abm[cBitmaps].pb = (PBYTE)(pbafhOrg = pbafh2);
  112.  
  113.                /* Point    to the independent bitmap file header    */
  114.                /* since    it is the default            */
  115. pbfh2 =    &pbafh2->bfh2;
  116. abm[cBitmaps].cColours    = (ULONG)(1 << pbfh2->bmp2.cBitCount);
  117. abm[cBitmaps].cBitCount    = pbfh2->bmp2.cBitCount;
  118. abm[cBitmaps].cx    = pbfh2->bmp2.cx;
  119. abm[cBitmaps].cy    = pbfh2->bmp2.cy;
  120. abm[cBitmaps++].hbm = GpiCreateBitmap(hpsMem, &pbfh2->bmp2, CBM_INIT, abm[cBitmaps].pbImage = (PBYTE)((PBYTE)pbafh2 + pbafh2->bfh2.offBits),
  121.                       (PBITMAPINFO2)(PVOID)&pbfh2->bmp2);
  122.  
  123.                /* Scan the bitmap array    for the    desired    icon    */
  124.                /* type based on    the current screen display    */
  125. while (    pbafh2->offNext    )
  126.    {
  127.    abm[cBitmaps].pb = (PBYTE)(pbafh2 = (PBITMAPARRAYFILEHEADER2)((PBYTE)pbafhOrg + pbafh2->offNext));
  128.  
  129.                /* Point    to the next array header        */
  130.  
  131.    abm[cBitmaps].cColours  = (ULONG)(1 << pbafh2->bfh2.bmp2.cBitCount);
  132.    abm[cBitmaps].cBitCount = pbafh2->bfh2.bmp2.cBitCount;
  133.    abm[cBitmaps].cx       = pbafh2->bfh2.bmp2.cx;
  134.    abm[cBitmaps].cy       = pbafh2->bfh2.bmp2.cy;
  135.  
  136.                /* Create a memory presentation space using the    */
  137.                /* desktop window handle    and create the colour    */
  138.                /* bitmap                    */
  139.  
  140.    abm[cBitmaps].hbm = GpiCreateBitmap(hpsMem, &pbafh2->bfh2.bmp2, CBM_INIT,
  141.                        abm[cBitmaps].pbImage = (PBYTE)((PBYTE)pbafhOrg + pbafh2->bfh2.offBits),
  142.                        (PBITMAPINFO2)(PVOID)&pbafh2->bfh2.bmp2);
  143.    if (    ++cBitmaps == 32L )
  144.        break;
  145.    }
  146.                /* Release the memory presentation space        */
  147. WinReleasePS(hpsMem);
  148.                /* Return the icon pointer handle        */
  149. }
  150. #pragma    subtitle("   Bitmap Retrieve - Select Bitmap File Header Function")
  151. #pragma    page( )
  152.  
  153. /* --- BuildBitmapStack1 ------------------------------    [ Private ] ---    */
  154. /*                                    */
  155. /*     This function is    used to    build the bitmap images    for a bitmap    */
  156. /*     array and place the bitmaps within the bitmap stack.        */
  157. /*                                    */
  158. /*     Upon Entry:                            */
  159. /*                                    */
  160. /*     PBITMAPARRAYFILEHEADER2 pbafh2; = Bitmap    Array Header Pointer    */
  161. /*                                    */
  162. /*     Upon Exit:                            */
  163. /*                                    */
  164. /*     Nothing                                */
  165. /*                                    */
  166. /* --------------------------------------------------------------------    */
  167.  
  168. static VOID BuildBitmapStack1(PBITMAPARRAYFILEHEADER pbafh)
  169.  
  170. {
  171. PBITMAPARRAYFILEHEADER pbafhOrg;   /* Bitmap Array File    Header Origin    */
  172. PBITMAPFILEHEADER      pbfh;       /* Bitmap Array File    Header Pointer    */
  173. HPS               hpsMem;       /* Memory Presentation Space    Handle    */
  174.  
  175. hpsMem = WinGetPS(HWND_DESKTOP);
  176.  
  177.                /* Save the start of the    bitmap file header    */
  178.                /* since    all offsets are    from the beginning of    */
  179.                /* the header                    */
  180.  
  181. abm[cBitmaps].pb    = (PBYTE)(pbafhOrg = pbafh);
  182.  
  183.                /* Point    to the independent bitmap file header    */
  184.                /* since    it is the default            */
  185. pbfh = &pbafh->bfh;
  186. abm[cBitmaps].cColours    = (ULONG)(1 << pbfh->bmp.cBitCount);
  187. abm[cBitmaps].cBitCount    = pbfh->bmp.cBitCount;
  188. abm[cBitmaps].cx    = pbfh->bmp.cx;
  189. abm[cBitmaps].cy    = pbfh->bmp.cy;
  190. abm[cBitmaps++].hbm = GpiCreateBitmap(hpsMem, (PBITMAPINFOHEADER2)&pbfh->bmp, CBM_INIT,
  191.                       abm[cBitmaps].pbImage = (PBYTE)((PBYTE)pbafh + pbafh->bfh.offBits),
  192.                       (PBITMAPINFO2)(PVOID)&pbfh->bmp);
  193.  
  194.                /* Scan the bitmap array    for the    desired    icon    */
  195.                /* type based on    the current screen display    */
  196. while (    pbafh->offNext )
  197.    {
  198.    abm[cBitmaps].pb = (PBYTE)(pbafh = (PBITMAPARRAYFILEHEADER)((PBYTE)pbafhOrg + pbafh->offNext));
  199.  
  200.                /* Point    to the next array header        */
  201.  
  202.    abm[cBitmaps].cBitCount = pbafh->bfh.bmp.cBitCount;
  203.    abm[cBitmaps].cColours  = (ULONG)(1 << pbafh->bfh.bmp.cBitCount);
  204.    abm[cBitmaps].cx       = pbafh->bfh.bmp.cx;
  205.    abm[cBitmaps].cy       = pbafh->bfh.bmp.cy;
  206.  
  207.                /* Create a memory presentation space using the    */
  208.                /* desktop window handle    and create the colour    */
  209.                /* bitmap                    */
  210.  
  211.    abm[cBitmaps].hbm = GpiCreateBitmap(hpsMem, (PBITMAPINFOHEADER2)&pbafh->bfh.bmp, CBM_INIT,
  212.                        abm[cBitmaps].pbImage = (PBYTE)((PBYTE)pbafhOrg + pbafh->bfh.offBits),
  213.                        (PBITMAPINFO2)(PVOID)&pbafh->bfh.bmp);
  214.    if (    ++cBitmaps == 32L )
  215.        break;
  216.    }
  217.                /* Release the memory presentation space        */
  218. WinReleasePS(hpsMem);
  219.                /* Return the icon pointer handle        */
  220. }
  221. #pragma    subtitle("   Bitmap Retrieve - Retrieve Single Bitmap Function")
  222. #pragma    page( )
  223.  
  224. /* --- GetSingleBitmap --------------------------------    [ Private ] ---    */
  225. /*                                    */
  226. /*     This function is    used to    load a single bitmap from a bitmap    */
  227. /*     file.                                */
  228. /*                                    */
  229. /*     Upon Entry:                            */
  230. /*                                    */
  231. /*     PBITMAPFILEHEADER pbafh;    = Bitmap File Header Pointer        */
  232. /*                                    */
  233. /*     Upon Exit:                            */
  234. /*                                    */
  235. /*     Nothing                                */
  236. /*                                    */
  237. /* --------------------------------------------------------------------    */
  238.  
  239. static VOID GetSingleBitmap(PBITMAPFILEHEADER2 pbfh2)
  240.  
  241. {
  242. HPS    hPS;               /* Presentation Space        */
  243.  
  244.                /* Check    to see that the    size of    the bitmap    */
  245.                /* info header is correct otherwise the bitmap    */
  246.                /* cannot be created                */
  247.  
  248. if ( pbfh2->bmp2.cbFix != sizeof(BITMAPINFOHEADER2) )
  249.  
  250.                /* Invalid header size, return NULL bitmap    */
  251.                /* handle                    */
  252.    return;
  253.                /* Save the start of the    bitmap file header    */
  254.                /* since    all offsets are    from the beginning of    */
  255.                /* the header                    */
  256. cBitmaps = 1L;
  257. abm[0].pb = (PBYTE)pbfh2;
  258. abm[0].cBitCount = pbfh2->bmp2.cBitCount;
  259. abm[0].cColours    = (ULONG)(1 << pbfh2->bmp2.cBitCount);
  260. abm[0].cx = pbfh2->bmp2.cx;
  261. abm[0].cy = pbfh2->bmp2.cy;
  262.  
  263.                /* Point    to the independent bitmap file header    */
  264.                /* since    it is the default            */
  265.  
  266. abm[0].hbm = GpiCreateBitmap(hPS = WinGetPS(HWND_DESKTOP), &pbfh2->bmp2, CBM_INIT, abm[0].pbImage = (PBYTE)pbfh2 + pbfh2->offBits,
  267.                  (PBITMAPINFO2)(PVOID)&pbfh2->bmp2);
  268.  
  269.                /* Release the desktop presentation space    */
  270. WinReleasePS(hPS);
  271.  
  272. }
  273. #pragma    subtitle("   Bitmap Retrieve - Retrieve Single Bitmap Function")
  274. #pragma    page( )
  275.  
  276. /* --- GetSingleBitmap1    -------------------------------    [ Private ] ---    */
  277. /*                                    */
  278. /*     This function is    used to    load a single bitmap from a bitmap    */
  279. /*     file.                                */
  280. /*                                    */
  281. /*     Upon Entry:                            */
  282. /*                                    */
  283. /*     PBITMAPFILEHEADER pbafh;    = Bitmap File Header Pointer        */
  284. /*                                    */
  285. /*     Upon Exit:                            */
  286. /*                                    */
  287. /*     Nothing                                */
  288. /*                                    */
  289. /* --------------------------------------------------------------------    */
  290.  
  291. static VOID GetSingleBitmap1(PBITMAPFILEHEADER pbfh)
  292.  
  293. {
  294. HPS    hPS;               /* Presentation Space        */
  295.  
  296.                /* Check    to see that the    size of    the bitmap    */
  297.                /* info header is correct otherwise the bitmap    */
  298.                /* cannot be created                */
  299.  
  300. if ( pbfh->bmp.cbFix !=    sizeof(BITMAPINFOHEADER) )
  301.  
  302.                /* Invalid header size, return NULL bitmap    */
  303.                /* handle                    */
  304.    return;
  305.                /* Save the start of the    bitmap file header    */
  306.                /* since    all offsets are    from the beginning of    */
  307.                /* the header                    */
  308. cBitmaps = 1L;
  309. abm[0].pb = (PBYTE)pbfh;
  310. abm[0].cBitCount = pbfh->bmp.cBitCount;
  311. abm[0].cColours    = (ULONG)(1 << pbfh->bmp.cBitCount);
  312. abm[0].cx = pbfh->bmp.cx;
  313. abm[0].cy = pbfh->bmp.cy;
  314.  
  315.                /* Get the desktop presentation space to    create    */
  316.                /* the bitmap                    */
  317.  
  318. abm[0].hbm = GpiCreateBitmap(hPS = WinGetPS(HWND_DESKTOP), (PBITMAPINFOHEADER2)(PVOID)&pbfh->bmp,
  319.                  CBM_INIT, abm[0].pbImage =    (BYTE *)pbfh + pbfh->offBits, (PBITMAPINFO2)(PVOID)&pbfh->bmp);
  320.  
  321.                /* Release the desktop presentation space    */
  322. WinReleasePS(hPS);
  323. }
  324. #pragma    subtitle("   Bitmap Retrieve - Bitmap Retrieve Function")
  325. #pragma    page( )
  326.  
  327. /* --- GetBitmap --------------------------------------- [ Public ] ---    */
  328. /*                                    */
  329. /*     This function is    used to    load the requested bitmap file and to    */
  330. /*     create a    bitmap that can    be displayed through Prominare        */
  331. /*     Designer.                            */
  332. /*                                    */
  333. /*     Upon Entry:                            */
  334. /*                                    */
  335. /*     PSZ pszFileName;    = Bitmap Filename                */
  336. /*                                    */
  337. /*     Upon Exit:                            */
  338. /*                                    */
  339. /*     hbmGetBitmap = Bitmap Handle                    */
  340. /*                                    */
  341. /* --------------------------------------------------------------------    */
  342.  
  343. VOID GetBitmap(PSZ pszFileName)
  344.  
  345. {
  346. FILESTATUS3           fs;       /* File Status Information        */
  347. HFILE               hFile;       /* File Handle            */
  348. PBITMAPARRAYFILEHEADER pbafh = 0;  /* Bitmap File Header Pointer    */
  349. ULONG               cbRead;       /* Bytes Read            */
  350. ULONG               ulResult;   /* File Open    Fail Result        */
  351. register INT i;               /* Loop Counter            */
  352.  
  353.                /* Open the requested bitmap file        */
  354.  
  355. if ( DosOpen(pszFileName, &hFile, &ulResult, 0UL, FILE_NORMAL, OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_FAIL_IF_NEW,
  356.          OPEN_ACCESS_READONLY | OPEN_SHARE_DENYNONE, NULL) )
  357.  
  358.                /* Could    not open the file, return NULL bitmap    */
  359.                /* pointer handle                */
  360.    return;
  361.                /* Get the file information to allow the    proper    */
  362.                /* amount of memory to be allocated for the    */
  363.                /* bitmap information                */
  364.  
  365. DosQueryFileInfo(hFile,    FIL_STANDARD, (PVOID)&fs, sizeof(FILESTATUS3));
  366.  
  367.                /* Allocate memory for the bitmap information    */
  368.  
  369. if ( (pbafh = (PBITMAPARRAYFILEHEADER)malloc(fs.cbFile)) == NULL )
  370.  
  371.                /* Memory allocation failed, return NULL    bitmap    */
  372.                /* pointer handle                */
  373.    return;
  374.                /* Read into memory the bitmap file        */
  375.  
  376. if ( DosRead(hFile, (PVOID)pbafh, fs.cbFile, (PULONG)&cbRead) || (cbRead != fs.cbFile) )
  377.    {
  378.                /* Release the memory allocated for the bitmap    */
  379.                /* information                    */
  380.    free(pbafh);
  381.                /* Close    the bitmap file                */
  382.    DosClose(hFile);
  383.                /* Error    occurred in reading the    bitmap file,    */
  384.                /* return NULL bitmap handle            */
  385.    return;
  386.    }
  387. else
  388.                /* Close    the bitmap file                */
  389.    DosClose(hFile);
  390.                /* Delete any previously    loaded bitmaps        */
  391.  
  392. for ( i    = 0; i < cBitmaps; i++ )
  393.    GpiDeleteBitmap(abm[i].hbm);
  394.  
  395. cBitmaps = 0L;
  396.  
  397. iBitmap    = 0L;
  398.  
  399. if ( (pbafh->cbSize == sizeof(BITMAPARRAYFILEHEADER)) || (pbafh->cbSize    == sizeof(BITMAPFILEHEADER)) )
  400.    {
  401.    fWindowsBitmap = f20Bitmap =    fBitmapArray = FALSE;
  402.  
  403.                /* Check    to see if the bitmap is    monochrome    */
  404.  
  405.    if (    pbafh->usType == BFT_BMAP )
  406.  
  407.                /* Monochrome bitmap, convert into a memory    */
  408.                /* bitmap and a valid bitmap handle        */
  409.  
  410.        GetSingleBitmap1((PBITMAPFILEHEADER)(PVOID)pbafh);
  411.    else
  412.                /* Check    to see if the icon is a    bitmap array    */
  413.                /* thereby indicating a coloured    bitmap        */
  414.  
  415.        if ( pbafh->usType == BFT_BITMAPARRAY )
  416.        {
  417.                /* Coloured bitmap, convert into    a memory bitmap    */
  418.                /* bitmap and a valid bitmap handle        */
  419.  
  420.        BuildBitmapStack1((PBITMAPARRAYFILEHEADER)pbafh);
  421.        fBitmapArray    = TRUE;
  422.        }
  423.    }
  424. else
  425.    if (    (pbafh->cbSize == sizeof(BITMAPARRAYFILEHEADER2)) || (pbafh->cbSize == sizeof(BITMAPFILEHEADER2)) )
  426.        {
  427.        f20Bitmap = TRUE;
  428.        fWindowsBitmap =    fBitmapArray = FALSE;
  429.  
  430.                /* Check    to see if the bitmap is    monochrome    */
  431.  
  432.        if ( pbafh->usType == BFT_BMAP )
  433.  
  434.                /* Monochrome bitmap, convert into a memory    */
  435.                /* bitmap and a valid bitmap handle        */
  436.  
  437.        GetSingleBitmap((PBITMAPFILEHEADER2)(PVOID)pbafh);
  438.        else
  439.                /* Check    to see if the icon is a    bitmap array    */
  440.                /* thereby indicating a coloured    bitmap        */
  441.  
  442.        if (    pbafh->usType == BFT_BITMAPARRAY )
  443.            {
  444.                /* Coloured bitmap, convert into    a memory bitmap    */
  445.                /* bitmap and a valid bitmap handle        */
  446.  
  447.            BuildBitmapStack((PBITMAPARRAYFILEHEADER2)(PVOID)pbafh);
  448.            fBitmapArray = TRUE;
  449.            }
  450.        }
  451.    else
  452.                /* Appears to be    a Windows bitmap, try to    */
  453.                /* convert it to    an OS/2    2.x format bitmap    */
  454.        {
  455.        fBitmapArray = FALSE;
  456.        GetSingleBitmap((PBITMAPFILEHEADER2)(pbafh = (PBITMAPARRAYFILEHEADER)pbfh2GetWindowsBmp((PBYTE)pbafh, fs.cbFile)));
  457.        }
  458.  
  459. if ( pb    )
  460.    free(pb);
  461. pb = (PBYTE)pbafh;
  462.  
  463. }
  464. #pragma    subtitle("   Bitmap Retrieve - Bitmap Image Retrieve Function")
  465. #pragma    page( )
  466.  
  467. /* --- pbfh2GetWindowsBmp ------------------------------ [ Public ] ---    */
  468. /*                                    */
  469. /*     This function is    used to    convert    a Windows bitmap to an OS/2    */
  470. /*     2.x type    of bitmap.                        */
  471. /*                                    */
  472. /*     Upon Entry:                            */
  473. /*                                    */
  474. /*     PBYTE pbBitmap; = Pointer to Windows Bitmap Data            */
  475. /*     ULONG cbData;   = Windows Bitmap    Data Size            */
  476. /*                                    */
  477. /*     Upon Exit:                            */
  478. /*                                    */
  479. /*     pbfh2GetWindowsBmp = Convert Bitmap Data    Pointer            */
  480. /*                                    */
  481. /* --------------------------------------------------------------------    */
  482.  
  483. static PBITMAPFILEHEADER2 pbfh2GetWindowsBmp(PBYTE pbBitmap, ULONG cbData)
  484.  
  485. {
  486. PBITMAPFILEHEADER2   pbfh2;       /* Bitmap File Header Pointer    */
  487. PULONG             pul;       /* Value Pointer            */
  488. PwinRGBQUAD         pargbq;       /* RGB Quad Array Pointer        */
  489. PwinRGBTRIPLE         pargbt;       /* RGB Triple Array Pointer        */
  490. ULONG             cClrs;       /* Colours Table Count        */
  491. ULONG             cb;       /* Image Size            */
  492. winBITMAPCOREHEADER  bmpc;       /* Bitmap Information Header        */
  493. winBITMAPINFOHEADER  bmpi;       /* Bitmap Information Header        */
  494. PwinBITMAPFILEHEADER pwbfh;       /* Bitmap File Header        */
  495. PBYTE             pbImage;       /* Image Pointer            */
  496. PBYTE             pbClrTable;   /* Colour Table Pointer        */
  497. register INT i;               /* Loop Counter            */
  498.  
  499. pwbfh =    (PwinBITMAPFILEHEADER)pbBitmap;
  500.  
  501. if ( (pwbfh->bfType == BFT_BMAP) || (pwbfh->bfSize == cbData) )
  502.    {
  503.                /* Check    to see if the bitmap is    a variation of    */
  504.                /* the bitmap layouts in    which case, transfer    */
  505.                /* the info and point to    the RGB    colour table    */
  506.  
  507.    if (    pwbfh->bmpi.biSize == sizeof(winBITMAPCOREHEADER) )
  508.        {
  509.        memset(&bmpi, 0,    sizeof(winBITMAPINFOHEADER));
  510.        memcpy(&bmpc, &pwbfh->bmpi, sizeof(winBITMAPCOREHEADER));
  511.        bmpi.biWidth    = bmpc.bcWidth;
  512.        bmpi.biHeight   = bmpc.bcHeight;
  513.        bmpi.biPlanes   = bmpc.bcPlanes;
  514.        bmpi.biBitCount = bmpc.bcBitCount;
  515.        pbClrTable = pbBitmap + (sizeof(winBITMAPFILEHEADER) - sizeof(winBITMAPINFOHEADER)) + sizeof(winBITMAPCOREHEADER);
  516.        }
  517.    else
  518.                /* Standard bitmap format, transfer the info and    */
  519.                /* point    to the colour table            */
  520.        {
  521.        memcpy(&bmpi, &pwbfh->bmpi, sizeof(winBITMAPINFOHEADER));
  522.        pbClrTable = pbBitmap + sizeof(winBITMAPFILEHEADER);
  523.        }
  524.                /* Point    to the image data of the bitmap        */
  525.  
  526.    pbImage = pbBitmap +    pwbfh->bfOffBits;
  527.  
  528.                /* Get the colours used count and when zero    */
  529.                /* the colours are defined to be    the maximum    */
  530.                /* values for the bit count            */
  531.  
  532.    if (    (cClrs = bmpi.biClrUsed) == 0 )
  533.        switch (    bmpi.biBitCount    )
  534.        {
  535.                /* 2 colour bitmap                */
  536.        case    1 :
  537.            cClrs = 2UL;
  538.            break;
  539.                /* 16 colour bitmap                */
  540.        case    4 :
  541.            cClrs = 16UL;
  542.            break;
  543.                /* 256 colour bitmap                */
  544.        case    8 :
  545.            cClrs = 256UL;
  546.            break;
  547.                /* 16 MB    colour bitmap, no colour table defined    */
  548.        default :
  549.            cClrs = 0UL;
  550.            break;
  551.        }
  552.                /* When colour table defined for    bitmap,        */
  553.                /* allocated enough memory for it and then read    */
  554.                /* it in    from disk                */
  555.    if (    cClrs )
  556.                /* Check    to see if the bitmap is    a variant in    */
  557.                /* which    case the RGB colours are like the OS/2    */
  558.                /* 1.x colours, 3 bytes in size,    therefore need    */
  559.                /* to be    transferred individually        */
  560.  
  561.        if ( pwbfh->bmpi.biSize == sizeof(winBITMAPCOREHEADER) )
  562.        {
  563.        memset(pargbq = (winRGBQUAD *)malloc(sizeof(winRGBQUAD) * cClrs),
  564.           0, sizeof(winRGBQUAD)    * cClrs);
  565.        pargbt = (PwinRGBTRIPLE)pbClrTable;
  566.        for ( i = 0;    i < cClrs; i++ )
  567.            {
  568.            pargbq[i].rgbBlue  = pargbt[i].rgbBlue;
  569.            pargbq[i].rgbGreen = pargbt[i].rgbGreen;
  570.            pargbq[i].rgbRed      = pargbt[i].rgbRed;
  571.            }
  572.        }
  573.        else
  574.                /* Standard colour table, transfer the info    */
  575.  
  576.        memcpy(pargbq = (winRGBQUAD *)malloc(sizeof(winRGBQUAD) * cClrs),
  577.           pbClrTable, sizeof(winRGBQUAD) * cClrs);
  578.    else
  579.                /* No colour table                */
  580.  
  581.        pargbq =    (PwinRGBQUAD)NULL;
  582.  
  583.    if (    bmpi.biSizeImage )
  584.        cb = bmpi.biSizeImage;
  585.    else
  586.        if ( pwbfh->bmpi.biSize == sizeof(winBITMAPCOREHEADER) )
  587.        cb =    (ULONG)(cbData - (sizeof(winBITMAPFILEHEADER) -    sizeof(winBITMAPINFOHEADER)) - (sizeof(winRGBTRIPLE) * cClrs));
  588.        else
  589.        cb =    (ULONG)(cbData - sizeof(winBITMAPFILEHEADER) - (sizeof(winRGBQUAD) * cClrs));
  590.    }
  591. else
  592.    {
  593.  
  594.    pul = (PULONG)pbBitmap;
  595.  
  596.    if (    *pul ==    sizeof(winBITMAPCOREHEADER) )
  597.        {
  598.                /* Read the bitmap file info header from    the    */
  599.                /* bitmap                    */
  600.  
  601.        memcpy(&bmpc, pbBitmap, sizeof(winBITMAPCOREHEADER));
  602.        pbImage = pbBitmap + sizeof(winBITMAPCOREHEADER);
  603.        memset(&bmpi, 0,    sizeof(winBITMAPINFOHEADER));
  604.        bmpi.biWidth    = bmpc.bcWidth;
  605.        bmpi.biHeight   = bmpc.bcHeight;
  606.        bmpi.biPlanes   = bmpc.bcPlanes;
  607.        bmpi.biBitCount = bmpc.bcBitCount;
  608.        }
  609.    else
  610.                /* Read the bitmap file info header from    the    */
  611.                /* bitmap                    */
  612.        {
  613.        memcpy(&bmpi, pbBitmap, sizeof(winBITMAPINFOHEADER));
  614.        pbImage = pbBitmap + sizeof(winBITMAPINFOHEADER);
  615.        }
  616.                /* Get the colours used count and when zero    */
  617.                /* the colours are defined to be    the maximum    */
  618.                /* values for the bit count            */
  619.  
  620.    if (    (cClrs = bmpi.biClrUsed) == 0 )
  621.        switch (    bmpi.biBitCount    )
  622.        {
  623.                /* 2 colour bitmap                */
  624.        case    1 :
  625.            cClrs = 2UL;
  626.            break;
  627.                /* 16 colour bitmap                */
  628.        case    4 :
  629.            cClrs = 16UL;
  630.            break;
  631.                /* 256 colour bitmap                */
  632.        case    8 :
  633.            cClrs = 256UL;
  634.            break;
  635.                /* 16 MB    colour bitmap, no colour table defined    */
  636.        default :
  637.            cClrs = 0UL;
  638.            break;
  639.        }
  640.                /* When colour table defined for    bitmap,        */
  641.                /* allocated enough memory for it and then read    */
  642.                /* it in    from disk                */
  643.    if (    cClrs )
  644.        {
  645.        memcpy(pargbq = (winRGBQUAD *)malloc(sizeof(winRGBQUAD) * cClrs),
  646.           pbBitmap,    sizeof(winRGBQUAD) * cClrs);
  647.        pbImage = pbImage + (sizeof(winRGBQUAD) * cClrs);
  648.        }
  649.    else
  650.        pargbq =    (PwinRGBQUAD)NULL;
  651.  
  652.    cb =    (ULONG)(cbData - sizeof(winBITMAPINFOHEADER) - (sizeof(winRGBQUAD) * cClrs));
  653.    }
  654.                /* Initialize the bitmap    array header        */
  655.  
  656. memset(pbfh2 = (PBITMAPFILEHEADER2)malloc(sizeof(BITMAPFILEHEADER2) + cClrs * sizeof(RGB2) + cb),
  657.        0, sizeof(BITMAPFILEHEADER2));
  658.  
  659.                /* Initialize the first bitmap header for the    */
  660.                /* bitmap                    */
  661.  
  662. pbfh2->usType    = (USHORT)BFT_BMAP;
  663. pbfh2->cbSize    = sizeof(BITMAPFILEHEADER2);
  664. pbfh2->xHotspot    = (SHORT)(bmpi.biWidth    / 2);
  665. pbfh2->yHotspot    = (SHORT)(bmpi.biHeight    / 2);
  666. pbfh2->offBits    = sizeof(BITMAPFILEHEADER2) + sizeof(RGB2) * cClrs;
  667.  
  668.                /* Initialize the bitmap    information for    the    */
  669.                /* bitmap based on the values from the Windows    */
  670.                /* 3.1 bitmap                    */
  671.  
  672. pbfh2->bmp2.cbFix      = sizeof(BITMAPINFOHEADER2);
  673. pbfh2->bmp2.cx          = (ULONG)bmpi.biWidth;
  674. pbfh2->bmp2.cy          = (ULONG)bmpi.biHeight;
  675. pbfh2->bmp2.cPlanes      =       bmpi.biPlanes;
  676. pbfh2->bmp2.cBitCount      =       bmpi.biBitCount;
  677. pbfh2->bmp2.cbImage      =       cb;
  678. pbfh2->bmp2.cclrUsed      = cClrs;
  679. pbfh2->bmp2.cclrImportant = cClrs;
  680.  
  681.                /* When a colour    table present, write out the    */
  682.                /* colour table                    */
  683. if ( cClrs )
  684.    {
  685.    memcpy((PBYTE)pbfh2 + sizeof(BITMAPFILEHEADER2), pargbq, sizeof(winRGBQUAD) * cClrs);
  686.    memcpy((PBYTE)pbfh2 + sizeof(BITMAPFILEHEADER2) + sizeof(RGB2) * cClrs, pbImage, cb);
  687.    }
  688. else
  689.    memcpy((PBYTE)pbfh2 + sizeof(BITMAPFILEHEADER2), pbImage, cb);
  690.  
  691.                /* If a colour table was    present, release the    */
  692.                /* memory allocated for it            */
  693. if ( cClrs )
  694.    free(pargbq);
  695.  
  696. fWindowsBitmap = f20Bitmap = TRUE;
  697.  
  698. free(pbBitmap);
  699.  
  700. return(pbfh2);
  701. }
  702.