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