source: branches/v2.9/common_functions/img_funcs.c

Last change on this file was 2, checked in by stevenhl, 8 years ago

Import sources from cwmm-full.zip dated 2005-03-21

File size: 13.3 KB
Line 
1/*
2 This file conatins functions for loading image files.
3 */
4#define INCL_PM
5#define INCL_MMIOOS2
6
7#include <os2.h>
8#include "os2me.h"
9#include <string.h> /* for memset() */
10#include <stdlib.h> /* for malloc() */
11#include "sys_funcs.h"
12
13
14/*!**************************************************/
15/* */
16/* @@DESC */
17/* */
18/* This function loads a supported image file. */
19/* Every format for which MMOS2 has an IO procedure */
20/* may be used here. */
21/* */
22/* @@RETURNS */
23/* */
24/* HBITMAP hBitmap */
25/* */
26/* HAndle to the bitmap of NULL. */
27/* */
28/*!!*************************************************/
29HBITMAP ImgLoadImageFile ( PSZ pszFileName )
30{
31 HBITMAP hbm;
32 HBITMAP hbmTarget;
33 MMIOINFO mmioinfo;
34 MMFORMATINFO mmFormatInfo;
35 HMMIO hmmio;
36 ULONG ulImageHeaderLength;
37 MMIMAGEHEADER mmImgHdr;
38 ULONG ulBytesRead;
39 ULONG dwNumRowBytes;
40 PBYTE pRowBuffer;
41 ULONG dwRowCount;
42 SIZEL ImageSize;
43 ULONG dwHeight, dwWidth;
44 SHORT wBitCount;
45 FOURCC fccStorageSystem;
46 ULONG dwPadBytes;
47 ULONG dwRowBits;
48 ULONG ulReturnCode;
49 ULONG dwReturnCode;
50 HBITMAP hbReturnCode;
51 LONG lReturnCode;
52 FOURCC fccIOProc;
53 HDC hdc;
54 HPS hps;
55 HAB hab;
56
57 hab=WinQueryAnchorBlock(HWND_DESKTOP);
58
59 ulReturnCode = mmioIdentifyFile ( pszFileName,
60 0L,
61 &mmFormatInfo,
62 &fccStorageSystem,
63 0L,
64 0L);
65 /*
66 * If this file was NOT identified, then this function won't
67 * work, so return an error by indicating an empty bitmap.
68 */
69 if ( ulReturnCode == MMIO_ERROR )
70 {
71 return (0L);
72 }
73 /*
74 * If mmioIdentifyFile did not find a custom-written IO proc which
75 * can understand the image file, then it will return the DOS IO Proc
76 * info because the image file IS a DOS file.
77 */
78 if( mmFormatInfo.fccIOProc == FOURCC_DOS )
79 {
80 return ( 0L );
81 }
82 /*
83 * Ensure this is an IMAGE IOproc, and that it can read
84 * translated data
85 */
86 if ( (mmFormatInfo.ulMediaType != MMIO_MEDIATYPE_IMAGE) ||
87 ((mmFormatInfo.ulFlags & MMIO_CANREADTRANSLATED) == 0) )
88 {
89 return (0L);
90 }
91 else
92 {
93 fccIOProc = mmFormatInfo.fccIOProc;
94 }
95
96 /* Clear out and initialize mminfo structure */
97 memset ( &mmioinfo, 0L, sizeof ( MMIOINFO ) );
98 mmioinfo.fccIOProc = fccIOProc;
99 mmioinfo.ulTranslate = MMIO_TRANSLATEHEADER | MMIO_TRANSLATEDATA;
100 hmmio = mmioOpen ( (PSZ) pszFileName,
101 &mmioinfo,
102 MMIO_READ | MMIO_DENYWRITE | MMIO_NOIDENTIFY );
103 if ( ! hmmio )
104 {
105 return (0L);
106 }
107
108
109 dwReturnCode = mmioQueryHeaderLength ( hmmio,
110 (PLONG)&ulImageHeaderLength,
111 0L,
112 0L);
113 if ( ulImageHeaderLength != sizeof ( MMIMAGEHEADER ) )
114 {
115 /* We have a problem.....possibly incompatible versions */
116 ulReturnCode = mmioClose (hmmio, 0L);
117 return (0L);
118 }
119
120 ulReturnCode = mmioGetHeader ( hmmio,
121 &mmImgHdr,
122 (LONG) sizeof ( MMIMAGEHEADER ),
123 (PLONG)&ulBytesRead,
124 0L,
125 0L);
126
127 if ( ulReturnCode != MMIO_SUCCESS )
128 {
129 /* Header unavailable */
130 ulReturnCode = mmioClose (hmmio, 0L);
131 return (0L);
132 }
133 /*
134 memcpy(pBMPInfoHeader2, &mmImgHdr.mmXDIBHeader.BMPInfoHeader2,
135 sizeof(BITMAPINFOHEADER2)+256*sizeof(RGB2) );
136 */
137 /*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
138
139 /*
140 * Determine the number of bytes required, per row.
141 * PLANES MUST ALWAYS BE = 1
142 */
143 dwHeight = mmImgHdr.mmXDIBHeader.BMPInfoHeader2.cy;
144 dwWidth = mmImgHdr.mmXDIBHeader.BMPInfoHeader2.cx;
145 wBitCount = mmImgHdr.mmXDIBHeader.BMPInfoHeader2.cBitCount;
146 dwRowBits = dwWidth * mmImgHdr.mmXDIBHeader.BMPInfoHeader2.cBitCount;
147 dwNumRowBytes = dwRowBits >> 3;
148
149 /*
150 * Account for odd bits used in 1bpp or 4bpp images that are
151 * NOT on byte boundaries.
152 */
153 if ( dwRowBits % 8 )
154 {
155 dwNumRowBytes++;
156 }
157 /*
158 * Ensure the row length in bytes accounts for byte padding.
159 * All bitmap data rows must are aligned on LONG/4-BYTE boundaries.
160 * The data FROM an IOProc should always appear in this form.
161 */
162 dwPadBytes = ( dwNumRowBytes % 4 );
163 if ( dwPadBytes )
164 {
165 dwNumRowBytes += 4 - dwPadBytes;
166 }
167
168 /* Allocate space for ONE row of pels */
169 if ( DosAllocMem( (PPVOID)&pRowBuffer,
170 (ULONG)dwNumRowBytes,
171 fALLOC))
172 {
173 ulReturnCode = mmioClose (hmmio, 0L);
174 return(0L);
175 }
176
177 /* Create a device context */
178 hdc=DevOpenDC(hab, OD_MEMORY,"*",0L, NULL, NULLHANDLE);
179 if(hdc==NULLHANDLE)
180 {
181 DosFreeMem(pRowBuffer);
182 mmioClose (hmmio, 0L);
183 return(0L);
184 }
185
186 /*
187 // ***************************************************
188 // Create a memory presentation space that includes
189 // the memory device context obtained above.
190 // ***************************************************
191 */
192 ImageSize.cx = dwWidth;
193 ImageSize.cy = dwHeight;
194
195 hps = GpiCreatePS ( hab,
196 hdc,
197 &ImageSize,
198 PU_PELS | GPIT_NORMAL | GPIA_ASSOC );
199 if ( !hps )
200 {
201#ifdef DEBUG
202 WinMessageBox( HWND_DESKTOP, HWND_DESKTOP,
203 "No HPS...",
204 "Open Image File",
205 (HMODULE) NULL,
206 (ULONG) MB_OK | MB_MOVEABLE |
207 MB_ERROR );
208#endif
209 DevCloseDC(hdc);
210 DosFreeMem(pRowBuffer);
211 mmioClose (hmmio, 0L);
212 return(0L);
213 }
214 /*
215 // GpiSelectPalette(hps, NULLHANDLE);
216 // ***************************************************
217 // Create an uninitialized bitmap. This is where we
218 // will put all of the bits once we read them in.
219 // ***************************************************
220 */
221
222 hbm = GpiCreateBitmap ( hps,
223 &mmImgHdr.mmXDIBHeader.BMPInfoHeader2,
224 0L,
225 NULL,
226 NULL);
227
228#if 0
229 hbm = GpiCreateBitmap ( hps,
230 pBMPInfoHeader2,
231 0L,
232 NULL,
233 NULL);
234#endif
235
236 if ( !hbm )
237 {
238#ifdef DEBUG
239 WinMessageBox( HWND_DESKTOP, HWND_DESKTOP,
240 "No HBITMAP...",
241 "Open Image File",
242 (HMODULE) NULL,
243 (ULONG) MB_OK | MB_MOVEABLE |
244 MB_ERROR );
245#endif
246 GpiDestroyPS(hps);
247 DevCloseDC(hdc);
248 DosFreeMem(pRowBuffer);
249 ulReturnCode = mmioClose (hmmio, 0L);
250 return(0L);
251 }
252 /*
253 // ***************************************************
254 // Select the bitmap into the memory device context.
255 // ***************************************************/
256 hbReturnCode = GpiSetBitmap ( hps,
257 hbm );
258 /*
259 //***************************************************************
260 // LOAD THE BITMAP DATA FROM THE FILE
261 // One line at a time, starting from the BOTTOM
262 //*************************************************************** */
263
264 for ( dwRowCount = 0; dwRowCount < dwHeight; dwRowCount++ )
265 {
266 ulBytesRead = (ULONG) mmioRead ( hmmio,
267 pRowBuffer,
268 dwNumRowBytes );
269 if ( !ulBytesRead )
270 {
271 break;
272 }
273 /*
274 * Allow context switching while previewing.. Couldn't get
275 * it to work. Perhaps will get to it when time is available...
276 */
277 lReturnCode = GpiSetBitmapBits ( hps,
278 (LONG) dwRowCount,
279 (LONG) 1,
280 (PBYTE) pRowBuffer,
281 (PBITMAPINFO2) &mmImgHdr.mmXDIBHeader.BMPInfoHeader2);
282 }
283
284 /* Clean up */
285 hbReturnCode = GpiSetBitmap ( hps,
286 NULLHANDLE );
287 ulReturnCode = mmioClose (hmmio, 0L);
288 DosFreeMem(pRowBuffer);
289 GpiDestroyPS(hps);
290 DevCloseDC(hdc);
291
292 return(hbm);
293}
294
295
296BOOL ImgGetBmpInfoHeader(PBITMAPINFOHEADER2 bmpih2, PSZ pszFileName /*, char* procName, ULONG ulLength*/)
297{
298 MMIOINFO mmioinfo;
299 MMFORMATINFO mmFormatInfo;
300 HMMIO hmmio;
301 ULONG ulImageHeaderLength;
302 MMIMAGEHEADER mmImgHdr;
303 FOURCC fccStorageSystem;
304 FOURCC fccIOProc;
305 ULONG ulReturnCode;
306 ULONG ulBytesRead;
307 char *pName;
308
309 // if(procName)
310 // procName[0]=0;
311
312 /* Check file size */
313 if(SysQueryFileSize(pszFileName)==0)
314 return FALSE; /* File is empty at the moment, so return without reading. */
315
316 ulReturnCode = mmioIdentifyFile ( pszFileName,
317 0L,
318 &mmFormatInfo,
319 &fccStorageSystem,
320 0L,
321 0L);
322 /*
323 * If this file was NOT identified, then this function won't
324 * work, so return an error by indicating an empty bitmap.
325 */
326 if ( ulReturnCode == MMIO_ERROR )
327 {
328 /* Disabled, because when copying via WPS we end here before the image is available */
329 return (0L);
330 }
331
332 /*
333 * If mmioIdentifyFile did not find a custom-written IO proc which
334 * can understand the image file, then it will return the DOS IO Proc
335 * info because the image file IS a DOS file.
336 */
337 if( mmFormatInfo.fccIOProc == FOURCC_DOS )
338 {
339 return ( 0L );
340 }
341
342 /*
343 * Ensure this is an IMAGE IOproc, and that it can read
344 * translated data
345 */
346 if ( (mmFormatInfo.ulMediaType != MMIO_MEDIATYPE_IMAGE) ||
347 ((mmFormatInfo.ulFlags & MMIO_CANREADTRANSLATED) == 0) )
348 {
349 /* Disabled, because it fails for templates */
350 return (0L);
351 }
352 else
353 {
354 fccIOProc = mmFormatInfo.fccIOProc;
355 }
356#if 0
357 if((pName=malloc(mmFormatInfo.lNameLength+1))!=NULLHANDLE)
358 {
359 LONG lBytesRead;
360 mmioGetFormatName(&mmFormatInfo, pName, &lBytesRead, 0,0);
361 if(procName && ulLength) {
362 strncpy(procName, pName, ulLength-1);
363 procName[ulLength-1]=0;
364 }
365 free(pName);
366 }
367#endif
368
369 /* Clear out and initialize mminfo structure */
370 memset ( &mmioinfo, 0L, sizeof ( MMIOINFO ) );
371 mmioinfo.fccIOProc = fccIOProc;
372 mmioinfo.ulTranslate = MMIO_TRANSLATEHEADER | MMIO_TRANSLATEDATA;
373 /*!!!!!!!!!!!!!!!!!!!!!!*/
374
375
376 hmmio = mmioOpen ( (PSZ) pszFileName,
377 &mmioinfo,
378 MMIO_READ | MMIO_DENYWRITE | MMIO_NOIDENTIFY );
379
380 if ( ! hmmio )
381 {
382 // If file could not be opened, return with error
383 return (0L);
384 }
385
386 ulReturnCode = mmioQueryHeaderLength ( hmmio,
387 (PLONG)&ulImageHeaderLength,
388 0L,
389 0L);
390 if ( ulImageHeaderLength != sizeof ( MMIMAGEHEADER ) )
391 {
392 /* We have a problem.....possibly incompatible versions */
393 ulReturnCode = mmioClose (hmmio, 0L);
394 return (0L);
395 }
396
397 ulReturnCode = mmioGetHeader ( hmmio,
398 &mmImgHdr,
399 (LONG) sizeof ( MMIMAGEHEADER ),
400 (PLONG)&ulBytesRead,
401 0L,
402 0L);
403
404 if ( ulReturnCode != MMIO_SUCCESS )
405 {
406 /* Header unavailable */
407 ulReturnCode = mmioClose (hmmio, 0L);
408 return (0L);
409 }
410
411 memcpy(bmpih2, &mmImgHdr.mmXDIBHeader.BMPInfoHeader2,
412 sizeof(BITMAPINFOHEADER2)+256*sizeof(RGB2) );
413 ulReturnCode = mmioClose (hmmio, 0L);
414 return TRUE;
415}
416
417
418
419
Note: See TracBrowser for help on using the repository browser.