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