source: trunk/dll/loadbmp.c@ 351

Last change on this file since 351 was 351, checked in by root, 19 years ago

Check more run time errors

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.1 KB
RevLine 
[351]1
2/***********************************************************************
3
4 $Id: loadbmp.c 351 2006-07-26 19:03:27Z root $
5
6 Load bitmaps
7
8 Copyright (c) 1993-98 M. Kimes
9 Copyright (c) 2006 Steven H.Levine
10
11 22 Jul 06 SHL Check more run time errors
12
13***********************************************************************/
14
[2]15#define INCL_DOS
16#define INCL_WIN
17#define INCL_GPI
[351]18#include <os2.h>
[2]19
20#include <stdlib.h>
21#include <stdio.h>
22#include <string.h>
23#include <share.h>
[351]24
[2]25#include "fm3dll.h"
26
[351]27static PSZ pszSrcFile = __FILE__;
28
[2]29#pragma alloc_text(LOADBITMAP,LoadBitmapFromFile,LoadBitmapFromFileNum)
30
31
[351]32HBITMAP LoadBitmapFromFileNum (USHORT id)
33{
34 char s[CCHMAXPATH];
[2]35
36 save_dir2(s);
37 sprintf(s + strlen(s),"\\%u.BMP",id);
38 return LoadBitmapFromFile(s);
39}
40
41
[351]42HBITMAP LoadBitmapFromFile (CHAR *pszFileName)
43{
[2]44 HBITMAP hBmp = (HBITMAP)0;
45 FILE *File;
46 ULONG rc;
47 USHORT usType = 0; // #@!!! compiler warnings
48 PBITMAPARRAYFILEHEADER2 pbafh2 = NULL; // (MAM) chng init values to NULL instead of ptr to 0
[351]49 PBITMAPFILEHEADER2 pbfh2 = NULL;
50 PBITMAPINFOHEADER2 pbih2 = NULL;
51 PBITMAPINFO2 pbmi2 = NULL;
52 PBITMAPARRAYFILEHEADER pbafh = NULL;
53 PBITMAPFILEHEADER pbfh = NULL;
54 PBITMAPINFOHEADER pbih = NULL;
[2]55 BOOL f2; // format 1.x or 2.x
56 ULONG ulOffset;
57 PBYTE pData = NULL;
58 ULONG ulDataSize;
59 SIZEL sizel;
60 HPS hPS = WinGetPS(HWND_DESKTOP);
61
62 //--- open the file
63 File = _fsopen(pszFileName,"r",SH_DENYWR);
64 if(!File)
65 goto ExitLoadBMP;
66
67 /* Read image type, and reset the stream...................................*/
68 /* The image type is a USHORT, so we only read that........................*/
69 rc = fread(&usType,1,sizeof(usType),File);
70 if(rc != sizeof(usType))
71 goto ExitLoadBMP;
72
73 /* Next read the bitmap info header........................................*/
74 // we allocate enough to hold a complete bitmap array file header
[351]75 pbafh2 = xmalloc(sizeof(*pbafh2) + 256 * sizeof(RGB2),pszSrcFile,__LINE__);
[2]76 if(!pbafh2)
77 goto ExitLoadBMP;
78 /* Next we assign pointers to the file header and bitmap info header...*/
79 /* Both the 1.x and 2.x structures are assigned just in case...........*/
80 pbfh2 = &pbafh2->bfh2;
81 pbih2 = &pbfh2->bmp2;
82 pbmi2 = (PBITMAPINFO2)pbih2;
83 pbafh = (PBITMAPARRAYFILEHEADER)pbafh2;
84 pbfh = &pbafh->bfh;
85 pbih = &pbfh->bmp;
86 switch (usType) {
87 case BFT_BMAP:
88 case BFT_ICON:
89 case BFT_POINTER:
90 case BFT_COLORICON:
91 case BFT_COLORPOINTER:
92 {
93 /* Now we assume the image is a 2.0 image and so we read a bitmap-file-*/
94 /* Now we reset the stream, next we'll read the bitmap info header. To do .*/
95 /* this we need to reset the stream to 0...................................*/
96 fseek(File,0,SEEK_SET);
97 /*-header-2 structure..................................................*/
98 rc = fread(pbfh2,1,sizeof(*pbfh2),File);
99 if(rc != sizeof(*pbfh2))
100 goto ExitLoadBMP;
101
102 f2 = pbih2->cbFix > sizeof(*pbih); // 1.x or 2.x bitmap
103 /* We will need to read the color table. Thus we position the stream...*/
104 /* so that the next read will read IT. This, of course, depends on the.*/
105 /* type of the bitmap (old vs new), note that in the NEW case, we can..*/
106 /* not be certain of the size of the bitmap header.....................*/
107 ulOffset = (f2) ? sizeof(*pbfh2) + pbih->cbFix - sizeof(*pbih2) :
108 sizeof(*pbfh);
109 }
110 break;
111
112 case BFT_BITMAPARRAY:
113 {
114 /* Now we are dealing with a bitmap array. This is a collection of ....*/
115 /* bitmap files and each has its own file header.......................*/
116
117 BOOL bBest = FALSE;
118 ULONG ulCurOffset, ulOffsetTemp = 0;
119 LONG lScreenWidth;
120 LONG lScreenHeight;
121 LONG lClrsDev, lClrsTemp = 0;
122 LONG lClrs;
123 ULONG ulSizeDiff, ulSizeDiffTemp = 0xffffffff;
124 HDC hdc;
125
126 // -- We will browse through the array and chose the bitmap best suited
127 // -- for the current display size and color capacities.
128 hdc = GpiQueryDevice( hPS );
129 DevQueryCaps(hdc,CAPS_COLORS,1,&lClrsDev);
130 DevQueryCaps(hdc,CAPS_WIDTH, 1,&lScreenWidth);
131 DevQueryCaps(hdc,CAPS_HEIGHT,1,&lScreenHeight);
132 pbafh2->offNext = 0;
133 do {
134 ulCurOffset = pbafh2->offNext;
135 rc = fseek(File,pbafh2->offNext,SEEK_SET);
136 if(rc)
137 goto ExitLoadBMP;
138 rc = fread(pbafh2,1,sizeof(*pbafh2),File);
139 if(rc != sizeof(*pbafh2))
140 goto ExitLoadBMP;
141 f2 = pbih2->cbFix > sizeof(*pbih);
142 if(f2)
143 lClrs = 1 << (pbafh2->bfh2.bmp2.cBitCount *
144 pbafh2->bfh2.bmp2.cPlanes);
145 else
146 lClrs = 1 << (pbafh->bfh.bmp.cBitCount *
147 pbafh->bfh.bmp.cPlanes);
148 if((pbafh2->cxDisplay == 0) && (pbafh2->cyDisplay == 0)) {
149 // This is a device independant bitmap
150 // Process it as a VGA
151 pbafh2->cxDisplay = 640;
152 pbafh2->cyDisplay = 480;
153 } // endif
154 ulSizeDiff = abs(pbafh2->cxDisplay - lScreenWidth) +
155 abs(pbafh2->cyDisplay - lScreenHeight);
156 if((lClrsDev == lClrs) &&
157 (ulSizeDiff == 0)) {
158 // We found the perfect match
159 bBest = TRUE;
160 ulOffsetTemp = ulCurOffset;
161 }
162 else {
163 if((ulOffsetTemp == 0) || // First time thru
164 (ulSizeDiff < ulSizeDiffTemp) || // Better fit than any previous
165 ((lClrs > lClrsTemp) && (lClrs < lClrsDev)) || // More colors than prev & less than device
166 ((lClrs < lClrsTemp) && (lClrs > lClrsDev))) {
167 ulOffsetTemp = ulCurOffset; // Make this our current pick
168 lClrsTemp = lClrs;
169 ulSizeDiffTemp = ulSizeDiff;
170 } // endif
171 } // endif
172 } while((pbafh2->offNext != 0) && !bBest); // enddo
173
174 // Now retrieve the best bitmap
175 rc = fseek(File,ulOffsetTemp,SEEK_SET);
176 if(rc)
177 goto ExitLoadBMP;
178 rc = fread(pbafh2,1,sizeof(*pbafh2),File);
179 if(rc != sizeof(*pbafh2))
180 goto ExitLoadBMP;
181
182 f2 = pbih2->cbFix > sizeof(*pbih);
183 /* as before, we calculate where to position the stream in order to ...*/
184 /* read the color table information....................................*/
185 ulOffset = ulOffsetTemp;
186 ulOffset += (f2) ? sizeof(*pbafh2) + pbih2->cbFix - sizeof(*pbih2):
187 sizeof(*pbafh);
188 }
189 break;
190
191 default:
192 goto ExitLoadBMP;
193 } /* endswitch */
194
195 /* We now position the stream on the color table information...............*/
196 rc = fseek(File,ulOffset,SEEK_SET);
197 if(rc)
198 goto ExitLoadBMP;
199
200 /* Read the color table....................................................*/
201 if(f2) {
202 /* For a 2.0 bitmap, all we need to do is read the color table...........*/
203 /* The bitmap info structure is just the header + color table............*/
204 // If we have 24 bits per pel, there is usually no color table, unless
205 // pbih2->cclrUsed or pbih2->cclrImportant are non zero, we should
206 // test that !
207 if(pbih2->cBitCount < 24) {
208
209 ULONG ul = (1 << pbih2->cBitCount) * sizeof(RGB2);
210
211 rc = fread(&pbmi2->argbColor[0],1,ul,File);
212 if(rc != ul)
213 goto ExitLoadBMP;
214 } // endif
215 /* remember the bitmap info we mentioned just above?.....................*/
216 pbmi2 = (PBITMAPINFO2)pbih2;
217 }
218 else {
219 /* This is an old format bitmap. Since the common format is the 2.0......*/
220 /* We have to convert all the RGB entries to RGB2........................*/
221
222 ULONG ul, cColors;
223 RGB rgb;
224
225 if(pbih->cBitCount <24)
226 cColors = 1 << pbih->cBitCount;
227 else
228 // If there are 24 bits per pel, the 24 bits are assumed to be a RGB value
229 cColors = 0;
230 /* Loop over the original table and create the new table, the extra byte.*/
231 /* has to be 0...........................................................*/
232 for(ul = 0; ul < cColors; ul++) {
233 fread(&rgb,1,sizeof(rgb),File);
234 pbmi2->argbColor[ul].bRed = rgb.bRed;
235 pbmi2->argbColor[ul].bGreen = rgb.bGreen;
236 pbmi2->argbColor[ul].bBlue = rgb.bBlue;
237 pbmi2->argbColor[ul].fcOptions = 0;
238 } /* endfor */
239
240 // we have to convert the old to the new version header
241 pbmi2->cbFix = sizeof(*pbih2);
242 pbmi2->cBitCount = pbih->cBitCount;
243 pbmi2->cPlanes = pbih->cPlanes;
244 pbmi2->cy = pbih->cy;
245 pbmi2->cx = pbih->cx;
246 // set rest to zero
247 memset((PCHAR)pbmi2 + 16,0,sizeof(*pbih2) - 16);
248 } /* endif */
249
250 /* We have the 2.0 bitmap info structure set...............................*/
251 /* move to the stream to the start of the bitmap data......................*/
252 rc = fseek(File,pbfh2->offBits,SEEK_SET);
253 if(rc)
254 goto ExitLoadBMP;
255
256 /* Read the bitmap data, the read size is derived using the magic formula..*/
257 /* The bitmap scan line is aligned on a doubleword boundary................*/
258 /* The size of the scan line is the number of pels times the bpp...........*/
259 /* After aligning it, we divide by 4 to get the number of bytes, and.......*/
260 /* multiply by the number of scan lines and the number of pel planes.......*/
261 if(pbmi2->ulCompression)
262 ulDataSize = pbmi2->cbImage;
263 else
264 ulDataSize = (((pbmi2->cBitCount * pbmi2->cx) + 31) / 32) * 4 *
265 pbmi2->cy * pbmi2->cPlanes;
[351]266 pData = xmalloc(ulDataSize,pszSrcFile,__LINE__);
[2]267 if(!pData)
268 goto ExitLoadBMP;
269 rc = fread(pData, 1, ulDataSize, File);
270 if(rc != ulDataSize)
271 goto ExitLoadBMP;
272
273 /* Now, we create the bitmap...............................................*/
274 sizel.cx = pbmi2->cx;
275 sizel.cy = pbmi2->cy;
276
277 hBmp = GpiCreateBitmap(hPS,(PBITMAPINFOHEADER2)pbmi2,CBM_INIT,
278 pData,pbmi2);
279
280ExitLoadBMP:
[351]281
282 xfree(pData);
283 xfree(pbafh2);
[2]284 fclose(File);
285 WinReleasePS(hPS);
[351]286 return hBmp;
[2]287}
288
Note: See TracBrowser for help on using the repository browser.