source: trunk/classes/mm-progs/mmfind/image.c@ 2

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

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

File size: 11.4 KB
Line 
1
2#define INCL_OS2MM
3#define INCL_MMIOOS2
4#define INCL_GPI
5
6#include <os2.h>
7#include "os2me.h"
8#include "common.h"
9#include "mmres.h"
10
11extern HAB globalHab;
12extern BITMAPINFOHEADER2 bmpInfoHeader2;
13extern HBITMAP hBitmap;
14
15void showImageControls(HWND hwnd, BOOL bShow)
16{
17 /* Preview area */
18 WinShowWindow(WinWindowFromID(hwnd, IDSR_IMGPREVIEW), bShow);
19 /* Group box */
20 WinShowWindow(WinWindowFromID(hwnd, IDGB_PREVIEW), bShow);
21}
22
23VOID DrawBitmap ( HWND hwnd )
24{
25 SWP swp;
26 POINTL aptl[4];
27 HPS hps;
28 BOOL bReturnCode;
29 ULONG ulHeight;
30 ULONG ulWidth;
31 RECTL rectl;
32
33 hps = WinBeginPaint ( hwnd,
34 0,
35 NULL);
36 /*
37 * Get position of image frame
38 */
39 bReturnCode = WinQueryWindowPos ( hwnd, &swp);
40
41 WinQueryWindowRect(hwnd, &rectl);
42 WinFillRect(hps, &rectl, CLR_WHITE);
43 /* Center image */
44 ulHeight = bmpInfoHeader2.cy;
45 ulWidth = bmpInfoHeader2.cx;
46 if(ulWidth <=swp.cx && ulHeight <= swp.cy)
47 {
48 aptl[0].x=(swp.cx-ulWidth)/2;
49 aptl[1].x=aptl[0].x+ulWidth;
50
51 aptl[0].y=(swp.cy-ulHeight)/2;
52 aptl[1].y=aptl[0].y+ulHeight;
53 }
54 else {
55 float fWidth, fHeight, fRes;
56
57 fWidth=(float)swp.cx/(float)ulWidth;
58 fHeight=(float)swp.cy/(float)ulHeight;
59 fRes=( fWidth>fHeight ? fHeight : fWidth);
60
61
62 aptl[0].x=(swp.cx-ulWidth*fRes)/2;
63 aptl[1].x=aptl[0].x+ulWidth*fRes;
64
65 aptl[0].y=(swp.cy-ulHeight*fRes)/2;
66 aptl[1].y=aptl[0].y+ulHeight*fRes;
67 }
68
69 aptl[2].x = 0; // source lower left
70 aptl[2].y = 0;
71
72 aptl[3].x = ulWidth; // source upper right
73 aptl[3].y = ulHeight;
74
75 /*
76 * Call GpiBitBlt and supply 4 aptl structures. This tells
77 * it to stretch or compress the bitmap depending on what is
78 * in the aptl structures. See above lines for their current
79 * settings.
80 */
81
82 WinDrawBitmap(hps, hBitmap, NULLHANDLE, (PPOINTL)aptl, 0, 0, DBM_NORMAL|DBM_STRETCH);
83
84#if 0
85 aptl[0].x=0;
86 aptl[0].y=0;
87 //aptl[1].x=100;
88 //aptl[1].y=100;
89 // WinDrawBitmap(hps, hBitmap, NULLHANDLE, (PPOINTL)aptl, 0, 0, DBM_NORMAL/*|DBM_STRETCH*/);
90#endif
91 bReturnCode = WinEndPaint (hps);
92}
93
94
95
96
97MRESULT EXPENTRY bmpPreviewProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
98{
99
100 switch (msg)
101 {
102 case WM_PAINT:
103 {
104 if (!WinIsWindowVisible(hwnd))
105 {
106 return((MRESULT)NULL);
107 }
108 DrawBitmap(hwnd);
109 return MRTRUE;
110 }
111 default:
112 break;
113 }
114 return WinDefWindowProc( hwnd, msg, mp1, mp2);
115}
116
117
118/*
119 Load an image file using IO-Procs
120 */
121HBITMAP loadBitmap ( PSZ pszFileName, PBITMAPINFOHEADER2 pBMPInfoHeader2)
122{
123 HBITMAP hbm;
124 MMIOINFO mmioinfo;
125 MMFORMATINFO mmFormatInfo;
126 HMMIO hmmio;
127 ULONG ulImageHeaderLength;
128 MMIMAGEHEADER mmImgHdr;
129 ULONG ulBytesRead;
130 ULONG dwNumRowBytes;
131 PBYTE pRowBuffer;
132 ULONG dwRowCount;
133 SIZEL ImageSize;
134 ULONG dwHeight, dwWidth;
135 SHORT wBitCount;
136 FOURCC fccStorageSystem;
137 ULONG dwPadBytes;
138 ULONG dwRowBits;
139 ULONG ulReturnCode;
140 ULONG dwReturnCode;
141 HBITMAP hbReturnCode;
142 LONG lReturnCode;
143 FOURCC fccIOProc;
144 HDC hdc;
145 HPS hps;
146
147
148 ulReturnCode = mmioIdentifyFile ( pszFileName,
149 0L,
150 &mmFormatInfo,
151 &fccStorageSystem,
152 0L,
153 0L);
154 /*
155 * If this file was NOT identified, then this function won't
156 * work, so return an error by indicating an empty bitmap.
157 */
158 if ( ulReturnCode == MMIO_ERROR )
159 {
160 // showMsgBox(IDSTR_CWIMAGETITLE, IDSTR_NOIMGIOPROCERROR, queryModuleHandle());
161 return (0L);
162 }
163
164 /*
165 * If mmioIdentifyFile did not find a custom-written IO proc which
166 * can understand the image file, then it will return the DOS IO Proc
167 * info because the image file IS a DOS file.
168 */
169 if( mmFormatInfo.fccIOProc == FOURCC_DOS )
170 {
171 // showMsgBox(IDSTR_CWIMAGETITLE, IDSTR_NOIMGIOPROCERROR, queryModuleHandle());
172 return ( 0L );
173 }
174 /*
175 * Ensure this is an IMAGE IOproc, and that it can read
176 * translated data
177 */
178 if ( (mmFormatInfo.ulMediaType != MMIO_MEDIATYPE_IMAGE) ||
179 ((mmFormatInfo.ulFlags & MMIO_CANREADTRANSLATED) == 0) )
180 {
181 // showMsgBox(IDSTR_CWIMAGETITLE, IDSTR_NOIMGIOPROCERROR, queryModuleHandle());
182 return (0L);
183 }
184 else
185 {
186 fccIOProc = mmFormatInfo.fccIOProc;
187 }
188
189 /* Clear out and initialize mminfo structure */
190 memset ( &mmioinfo, 0L, sizeof ( MMIOINFO ) );
191 mmioinfo.fccIOProc = fccIOProc;
192 mmioinfo.ulTranslate = MMIO_TRANSLATEHEADER | MMIO_TRANSLATEDATA;
193 hmmio = mmioOpen ( (PSZ) pszFileName,
194 &mmioinfo,
195 MMIO_READ | MMIO_DENYWRITE | MMIO_NOIDENTIFY );
196 if ( ! hmmio )
197 {
198 // If file could not be opened, return with error
199 // showMsgBox(IDSTR_CWIMAGETITLE, IDSTR_OPENFILEERROR, queryModuleHandle());
200 return (0L);
201 }
202
203
204 dwReturnCode = mmioQueryHeaderLength ( hmmio,
205 (PLONG)&ulImageHeaderLength,
206 0L,
207 0L);
208 if ( ulImageHeaderLength != sizeof ( MMIMAGEHEADER ) )
209 {
210 /* We have a problem.....possibly incompatible versions */
211 ulReturnCode = mmioClose (hmmio, 0L);
212 // showMsgBox(IDSTR_CWIMAGETITLE, IDSTR_NOIMGIOPROCERROR);
213 return (0L);
214 }
215
216 ulReturnCode = mmioGetHeader ( hmmio,
217 &mmImgHdr,
218 (LONG) sizeof ( MMIMAGEHEADER ),
219 (PLONG)&ulBytesRead,
220 0L,
221 0L);
222
223 if ( ulReturnCode != MMIO_SUCCESS )
224 {
225 /* Header unavailable */
226 ulReturnCode = mmioClose (hmmio, 0L);
227 return (0L);
228 }
229
230 /*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
231 *pBMPInfoHeader2=mmImgHdr.mmXDIBHeader.BMPInfoHeader2;
232
233 /*
234 * Determine the number of bytes required, per row.
235 * PLANES MUST ALWAYS BE = 1
236 */
237 dwHeight = mmImgHdr.mmXDIBHeader.BMPInfoHeader2.cy;
238 dwWidth = mmImgHdr.mmXDIBHeader.BMPInfoHeader2.cx;
239 wBitCount = mmImgHdr.mmXDIBHeader.BMPInfoHeader2.cBitCount;
240 dwRowBits = dwWidth * mmImgHdr.mmXDIBHeader.BMPInfoHeader2.cBitCount;
241 dwNumRowBytes = dwRowBits >> 3;
242
243 /*
244 * Account for odd bits used in 1bpp or 4bpp images that are
245 * NOT on byte boundaries.
246 */
247 if ( dwRowBits % 8 )
248 {
249 dwNumRowBytes++;
250 }
251 /*
252 * Ensure the row length in bytes accounts for byte padding.
253 * All bitmap data rows must are aligned on LONG/4-BYTE boundaries.
254 * The data FROM an IOProc should always appear in this form.
255 */
256 dwPadBytes = ( dwNumRowBytes % 4 );
257 if ( dwPadBytes )
258 {
259 dwNumRowBytes += 4 - dwPadBytes;
260 }
261
262 /* Allocate space for ONE row of pels */
263 if ( DosAllocMem( (PPVOID)&pRowBuffer,
264 (ULONG)dwNumRowBytes,
265 fALLOC))
266 {
267 ulReturnCode = mmioClose (hmmio, 0L);
268 // showMsgBox(IDSTR_CWIMAGETITLE, IDSTR_NOMEMERROR, queryModuleHandle());
269 return(0L);
270 }
271
272 /* Create a device context */
273 hdc=DevOpenDC(globalHab, OD_MEMORY,"*",0L, NULL, NULLHANDLE);
274 if(hdc==NULLHANDLE)
275 {
276 DosFreeMem(pRowBuffer);
277 mmioClose (hmmio, 0L);
278 return(0L);
279 }
280
281
282 // ***************************************************
283 // Create a memory presentation space that includes
284 // the memory device context obtained above.
285 // ***************************************************
286
287 ImageSize.cx = dwWidth;
288 ImageSize.cy = dwHeight;
289
290 hps = GpiCreatePS ( globalHab,
291 hdc,
292 &ImageSize,
293 PU_PELS | GPIT_NORMAL | GPIA_ASSOC );
294 // PU_PELS | GPIF_DEFAULT | GPIT_MICRO | GPIA_ASSOC );
295 if ( !hps )
296 {
297#ifdef DEBUG
298 WinMessageBox( HWND_DESKTOP, HWND_DESKTOP,
299 "No HPS...",
300 "Open Image File",
301 (HMODULE) NULL,
302 (ULONG) MB_OK | MB_MOVEABLE |
303 MB_ERROR );
304#endif
305 DevCloseDC(hdc);
306 DosFreeMem(pRowBuffer);
307 mmioClose (hmmio, 0L);
308 return(0L);
309 }
310
311 // GpiSelectPalette(hps, NULLHANDLE);
312 // ***************************************************
313 // Create an uninitialized bitmap. This is where we
314 // will put all of the bits once we read them in.
315 // ***************************************************
316 hbm = GpiCreateBitmap ( hps,
317 pBMPInfoHeader2,
318 0L,
319 NULL,
320 NULL);
321
322 if ( !hbm )
323 {
324#ifdef DEBUG
325 WinMessageBox( HWND_DESKTOP, HWND_DESKTOP,
326 "No HBITMAP...",
327 "Open Image File",
328 (HMODULE) NULL,
329 (ULONG) MB_OK | MB_MOVEABLE |
330 MB_ERROR );
331#endif
332 GpiDestroyPS(hps);
333 DevCloseDC(hdc);
334 DosFreeMem(pRowBuffer);
335 ulReturnCode = mmioClose (hmmio, 0L);
336 return(0L);
337 }
338
339 // ***************************************************
340 // Select the bitmap into the memory device context.
341 // ***************************************************
342 hbReturnCode = GpiSetBitmap ( hps,
343 hbm );
344
345 //***************************************************************
346 // LOAD THE BITMAP DATA FROM THE FILE
347 // One line at a time, starting from the BOTTOM
348 //*************************************************************** */
349
350 for ( dwRowCount = 0; dwRowCount < dwHeight; dwRowCount++ )
351 {
352 ulBytesRead = (ULONG) mmioRead ( hmmio,
353 pRowBuffer,
354 dwNumRowBytes );
355 if ( !ulBytesRead )
356 {
357 break;
358 }
359 /*
360 * Allow context switching while previewing.. Couldn't get
361 * it to work. Perhaps will get to it when time is available...
362 */
363 lReturnCode = GpiSetBitmapBits ( hps,
364 (LONG) dwRowCount,
365 (LONG) 1,
366 (PBYTE) pRowBuffer,
367 (PBITMAPINFO2) pBMPInfoHeader2);
368 }
369
370 /* Clean up */
371 hbReturnCode = GpiSetBitmap ( hps,
372 NULLHANDLE );
373 ulReturnCode = mmioClose (hmmio, 0L);
374 DosFreeMem(pRowBuffer);
375 GpiDestroyPS(hps);
376 DevCloseDC(hdc);
377
378 return(hbm);
379}
Note: See TracBrowser for help on using the repository browser.