1
+ /* **************************************************************************/
2
+ /* F r e e C o d e */
3
+ /* */
4
+ /* THIS CODE IS FREEWARE AND AS SUCH YOU ARE PERMITTED TO DO WHAT YOU WISH */
5
+ /* WITH IT. THESE PROGRAMS ARE PROVIDED AS IS WITHOUT ANY WARRANTY, */
6
+ /* EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO FITNESS FOR ANY */
7
+ /* PARTICULAR PURPOSE. */
8
+ /* */
9
+ /* However, we would appriciate if you shared any enhancements to us */
10
+ /* Please send them to www.jmast.se/free/ and we will include them in */
11
+ /* future distributions. */
12
+ /* */
13
+ /* **************************************************************************/
14
+ /* ***************************************************************************
15
+ BITMAP.C
16
+ ==========================================================================
17
+ 961211 Started with this.
18
+ ==========================================================================
19
+ ****************************************************************************/
20
+ #define INCL_WIN
21
+ #define INCL_GPI
22
+
23
+ #include < os2.h>
24
+ #include < string.h>
25
+ #include " stlibpmc.h"
26
+
27
+
28
+ /* -----------------------------------------------------------------------------------
29
+ LoadBitmapFromFile: Copys bitmap from file to hbm.
30
+ -----------------------------------------------------------------------------------*/
31
+ HBITMAP LoadBitmapFromFile (HWND hwnd, char *szFileName)
32
+ {
33
+ APIRET rc; /* API return code */
34
+ BOOL fRet = FALSE ; /* Function return code. */
35
+ HFILE hfile;
36
+ ULONG ulAction;
37
+ FILESTATUS fsts;
38
+ PBITMAPFILEHEADER2 pbfh2; /* can address any file types */
39
+ PBITMAPINFOHEADER2 pbmp2; /* address any info headers */
40
+ PBYTE pFileBegin = NULL ; /* beginning of bitmap file data */
41
+ ULONG cbRead; /* Number of bytes read by DosRead. */
42
+ ULONG cScans, cScansRet; /* number of scan lines in bitmap (cy) */
43
+
44
+ HAB hab;
45
+ HDC hdc;
46
+ HPS hps;
47
+ SIZEL sizel;
48
+ CHAR szToken[]=" *" ;
49
+
50
+ HBITMAP hbm;
51
+ BOOL bIgnore = FALSE ;
52
+
53
+
54
+ hab = WinQueryAnchorBlock (hwnd);
55
+
56
+ /* *********************************************************************
57
+ Use Loop to avoid duplicate cleanup code. If any errors, a break
58
+ statement will jump directly to error cleanup code.
59
+ **********************************************************************/
60
+ do
61
+ {
62
+ /* Read the bitmap from file
63
+ ****************************/
64
+ rc = DosOpen (szFileName, &hfile, &ulAction, 0 , FILE_NORMAL, FILE_OPEN, OPEN_ACCESS_READONLY | OPEN_SHARE_DENYREADWRITE, NULL );
65
+ if (rc)
66
+ break ;
67
+
68
+ rc = DosQueryFileInfo (hfile, 1 , &fsts, sizeof (fsts));
69
+ if (rc)
70
+ break ;
71
+
72
+ rc = DosAllocMem ((PPVOID)&pFileBegin, (ULONG)fsts.cbFile , (ULONG) PAG_READ | PAG_WRITE | PAG_COMMIT);
73
+ if (rc)
74
+ break ;
75
+
76
+ if (DosRead ( hfile, (PVOID)pFileBegin, fsts.cbFile , &cbRead))
77
+ break ;
78
+
79
+ /* *********************************************************************
80
+ If it's a bitmap-array, point to common file header. Otherwise,
81
+ point to beginning of file.
82
+ ***********************************************************************/
83
+ pbfh2 = (PBITMAPFILEHEADER2) pFileBegin;
84
+ pbmp2 = NULL ;
85
+
86
+ switch (pbfh2->usType )
87
+ {
88
+ case BFT_BITMAPARRAY:
89
+ /* ***********************************************************
90
+ If this is a Bitmap-Array, adjust pointer to the normal
91
+ file header. We'll just use the first bitmap in the
92
+ array and ignore other device forms.
93
+ *************************************************************/
94
+ pbfh2 = &(((PBITMAPARRAYFILEHEADER2) pFileBegin)->bfh2 );
95
+ pbmp2 = &pbfh2->bmp2 ; /* pointer to info header (readability) */
96
+ break ;
97
+
98
+ case BFT_BMAP:
99
+ pbmp2 = &pbfh2->bmp2 ; /* pointer to info header (readability) */
100
+ break ;
101
+
102
+ default : /* these formats aren't supported; don't set any ptrs */
103
+ case BFT_ICON:
104
+ case BFT_POINTER:
105
+ case BFT_COLORICON:
106
+ case BFT_COLORPOINTER:
107
+ break ;
108
+ }
109
+
110
+ if (pbmp2 == NULL )
111
+ break ; /* File format NOT SUPPORTED: break out to error code */
112
+
113
+ /* ***********************************************************************
114
+ Check to see if BMP file has an old structure, a new structure, or
115
+ Windows structure. Capture the common data and treat all bitmaps
116
+ generically with pointer to new format. API's will determine format
117
+ using cbFixed field.
118
+
119
+ Windows bitmaps have the new format, but with less data fields
120
+ than PM. The old strucuture has some different size fields,
121
+ though the field names are the same.
122
+
123
+
124
+ NOTE: bitmap data is located by offsetting the beginning of the file
125
+ by the offset contained in pbfh2->offBits. This value is in
126
+ the same relatie location for different format bitmap files.
127
+ ***********************************************************************/
128
+
129
+ if (pbmp2->cbFix == sizeof (BITMAPINFOHEADER)) /* old format? */
130
+ cScans = (ULONG) ((PBITMAPINFOHEADER)pbmp2)->cy ;
131
+ else /* new PM format, Windows, or other */
132
+ cScans = pbmp2->cy ;
133
+
134
+ /* Create memory device context and make a hps.
135
+ ***********************************************/
136
+ sizel.cx = 0 ;
137
+ sizel.cy = 0 ;
138
+ hdc = DevOpenDC (hab, OD_MEMORY, szToken, 0L , 0L , 0L );
139
+ hps = GpiCreatePS (hab, hdc, &sizel, PU_PELS | GPIF_DEFAULT | GPIT_MICRO | GPIA_ASSOC);
140
+
141
+ hbm = GpiCreateBitmap (hps, pbmp2, 0L , NULL , NULL );
142
+
143
+ if (!hbm)
144
+ break ;
145
+
146
+ if (GpiSetBitmap (hps, hbm) == (HBITMAP)BMB_ERROR)
147
+ break ;
148
+
149
+ /* ********************************************************************
150
+ Tell GPI to put the bits into the thread's PS. The function returns
151
+ the number of scan lines of the bitmap that were copied. We want
152
+ all of them at once.
153
+ *********************************************************************/
154
+ cScansRet = GpiSetBitmapBits (hps, 0L , cScans, pFileBegin + pbfh2->offBits , (PBITMAPINFO2) pbmp2);
155
+
156
+ if (cScansRet != cScans)
157
+ break ;
158
+
159
+ GpiDestroyPS (hps);
160
+ DevCloseDC (hdc);
161
+ DosFreeMem (pFileBegin);
162
+ DosClose (hfile);
163
+ return (hbm);
164
+ }
165
+ while (bIgnore == FALSE );
166
+
167
+ /* Error return)
168
+ *********************/
169
+ if (pFileBegin != NULL )
170
+ DosFreeMem (pFileBegin);
171
+ if (hps != (HPS) NULL )
172
+ GpiDestroyPS (hps);
173
+ if (hdc != (HDC) NULL )
174
+ DevCloseDC (hdc);
175
+ DosClose ( hfile);
176
+ return ((HBITMAP) NULL );
177
+ }
178
+
179
+
180
+
0 commit comments