testMag.cxx to HTML

magnify -|- index

Generated: On Tue Jan 8 16:41:01 2008,
From: c:\GTools\tools\testap3\testMag.cxx, dated Tue Jan 8 15:10:43 2008, with size 8051 bytes.

// testMag.cxx
// Trying to group together the Magnify service ...
#include "testap3.h"

#ifndef MALLOC
#define  MALLOC(a)      new BYTE[a]
#define  MFREE(a)       delete a
#endif // MALLOC/MFREE

// FOR DIAGNOSTIC ONLY
//#define HDC2BMPFILE(a,b) { \
//   HBITMAP _hbm; \
//   _hbm = (HBITMAP)GetCurrentObject(a, OBJ_BITMAP); \
//   if(_hbm) { \
//   HANDLE _hdib = BMPToDIB(_hbm, NULL ); \
//   if( _hdib ) { \
//   WriteBMPFile( b, _hdib ); \
//   MFREE(_hdib); \
//   }}}
//#define  DBG_HDC2BMPFILE   HDC2BMPFILE
#define  DBG_HDC2BMPFILE   // do nothing

// no real reason for this structure, other than grouping together
// all the variables used, and ensuring they start as zero.
typedef struct tagMAGSTR {
   HRGN rgn1, rgn2, rgn3;
   HBRUSH hbrclrTransparency;
   HDC dcIn, dcImg, dcAnd, dcXor, dcTemp;
   HBITMAP bitmap, bitmap2, bitmapAnd, bitmapXor, bitmapTemp;
   HBITMAP old, old2, old3, old4, old5;
   HBRUSH hbr;
} MAGSTR, * PMAGSTR;

// some macros to address this structure
#define rgn1   pms->rgn1
#define rgn2   pms->rgn2
#define rgn3   pms->rgn3
#define hbrclrTransparency pms->hbr

#define dcIn   pms->dcIn
#define bitmap pms->bitmap
#define old    pms->old

#define dcImg  pms->dcImg
#define bitmap2 pms->bitmap2
#define old2    pms->old2

#define dcAnd  pms->dcAnd
#define bitmapAnd pms->bitmapAnd
#define old3    pms->old3

#define dcXor  pms->dcXor
#define bitmapXor pms->bitmapXor
#define old4    pms->old4

#define dcTemp  pms->dcTemp
#define bitmapTemp pms->bitmapTemp
#define old5    pms->old5

/* ========================================================
   int TEST_Magnify( HDC hdcDst, int centrx, int centry, 
             SIZE sz, SIZE size, COLORREF clrTransparency )
   PURPOSE: Given a screen context, magnify the area around a
   given center, of inner size, and magnify it to an outer size,
   and blit back the results, as an eliipse.
   PARAMETERS:
   HDC hdcDst - Both source and destination device context.
   int centrx, centry - window coordinates of CENTER of block.
   SIZE sz - x,y size of INNER block to magnify.
   SIZE size - x,y size of OUTER block - gives magnification factor.
   COLORREF clrTransparency - Use for masking. I used RGB(255,0,254)
   ======================================================== */
int TEST_Magnify( HDC hdcDst, int centrx, int centry, 
             SIZE sz, SIZE size, COLORREF clrTransparency )
{
#ifdef _testStamp_H_
   TmStamp tm;
#endif // #ifdef _testStamp_H_
   int   iret = 0;
   int   stlx, stly;
   int   mtlx, mtly;
   PMAGSTR pms = (PMAGSTR)MALLOC( sizeof(MAGSTR) );
   if(!pms)
      return iret;

   SecureZeroMemory(pms, sizeof(MAGSTR));
   stlx = centrx - (sz.cx / 2);   // set TOP/LEFT of INNER
   stly = centry - (sz.cy / 2);
   mtlx = centrx - (size.cx / 2); // set TOP/LEFT of OUTER
   mtly = centry - (size.cy / 2);

   // create REGION
   rgn1 = CreateEllipticRgn(0,0,size.cx,size.cy);
   rgn2 = CreateRectRgn(0,0,size.cx,size.cy);
   rgn3 = CreateRectRgn(0,0,size.cx,size.cy);
   
   if( !rgn1 || !rgn2 || !rgn3 ) goto Cleanup;
   // get the OUTER region
   CombineRgn( rgn3, rgn2, rgn1, RGN_DIFF ); // subtract center ellipse

   hbrclrTransparency = CreateSolidBrush( clrTransparency );
   if( !hbrclrTransparency ) goto Cleanup;

   // original area to be magnified
   dcIn = CreateCompatibleDC( hdcDst );
   if( !dcIn ) goto Cleanup;
   bitmap = CreateCompatibleBitmap( hdcDst, sz.cx, sz.cy );
   if( !bitmap ) goto Cleanup;

   // magnified area
   dcImg = CreateCompatibleDC( hdcDst );
   if( !dcImg ) goto Cleanup;
   bitmap2 = CreateCompatibleBitmap( hdcDst, size.cx, size.cy );
   if( !bitmap2 ) goto Cleanup;

   // AND block, to hold a 1 BPP bitmap
   dcAnd = CreateCompatibleDC (hdcDst);
   if( !dcAnd ) goto Cleanup;
   // NOTE: This is 1-BPP bitmap only
   bitmapAnd = CreateBitmap(size.cx, size.cy, 1, 1, NULL);
   if( !bitmapAnd ) goto Cleanup;

   // XOR block
   dcXor = CreateCompatibleDC (hdcDst);
   if( !dcXor ) goto Cleanup;
   bitmapXor = CreateCompatibleBitmap( hdcDst, size.cx, size.cy );
   if( !bitmapXor ) goto Cleanup;

   // final building block
   dcTemp = CreateCompatibleDC (hdcDst);
   if( !dcTemp ) goto Cleanup;
   bitmapTemp = CreateCompatibleBitmap( hdcDst, size.cx, size.cy );
   if( !bitmapTemp ) goto Cleanup;

   // select the BITMAPS into the appropriate contexts
   old  = (HBITMAP)SelectObject(dcIn,   bitmap    );
   old2 = (HBITMAP)SelectObject(dcImg,  bitmap2   );
   old3 = (HBITMAP)SelectObject(dcAnd,  bitmapAnd );
   old4 = (HBITMAP)SelectObject(dcXor,  bitmapXor );
   old5 = (HBITMAP)SelectObject(dcTemp, bitmapTemp);

   // get the ORIGINAL inner block
   BitBlt( dcIn, 0, 0, sz.cx, sz.cy, hdcDst, stlx, stly, SRCCOPY );
   DBG_HDC2BMPFILE( dcIn, L"temp\\temp01dcIn.bmp" );

   // magnify (stretch) it to the outer block
   StretchBlt(dcImg,0,0,size.cx,size.cy,dcIn,0, 0,sz.cx,sz.cy, SRCCOPY);
   DBG_HDC2BMPFILE( dcImg, L"temp\\temp02dcImg1.bmp" );
   // fill the outer region with chosen 'transparent' color
   FillRgn( dcImg, rgn3, hbrclrTransparency );
   DBG_HDC2BMPFILE( dcImg, L"temp\\temp03dcImg2.bmp" );

   // set the background to the transparent color
   SetBkColor(dcImg, clrTransparency);
   // and get an AND block - Black and White only
   BitBlt (dcAnd, 0, 0, size.cx, size.cy, dcImg, 0, 0,   SRCCOPY);
   DBG_HDC2BMPFILE( dcAnd, L"temp\\temp04dcAnd.bmp" );

   // copy to the XOR block
   BitBlt (dcXor, 0, 0, size.cx, size.cy, dcImg, 0, 0,   SRCCOPY);
   // and NOTSRCAND from the AND block, to complete XOR block
   BitBlt (dcXor, 0, 0, size.cx, size.cy, dcAnd, 0, 0,   0x220326);
   DBG_HDC2BMPFILE( dcXor, L"temp\\temp05dcXor.bmp" );

   // get the current whole block
   BitBlt (dcTemp, 0, 0, size.cx, size.cy, hdcDst, mtlx, mtly, SRCCOPY);
   DBG_HDC2BMPFILE( dcTemp, L"temp\\temp06dcTemp1.bmp" );

   // AND in the And block
   BitBlt (dcTemp, 0, 0, size.cx, size.cy, dcAnd, 0, 0,  SRCAND);
   DBG_HDC2BMPFILE( dcTemp, L"temp\\temp07dcTemp2.bmp" );

   // INVERT per the Xor block
   BitBlt (dcTemp, 0, 0, size.cx, size.cy, dcXor, 0, 0,  SRCINVERT);
   DBG_HDC2BMPFILE( dcTemp, L"temp\\temp08dcTemp3.bmp" );

   // FINAL copy it into place
   BitBlt (hdcDst, mtlx, mtly, size.cx, size.cy, dcTemp, 0, 0, SRCCOPY);

   // unselect the bitmaps from the contexts
   bitmap     = (HBITMAP)SelectObject(dcIn,   old );
   bitmap2    = (HBITMAP)SelectObject(dcImg,  old2);
   bitmapAnd  = (HBITMAP)SelectObject(dcAnd,  old3);
   bitmapXor  = (HBITMAP)SelectObject(dcXor,  old4);
   bitmapTemp = (HBITMAP)SelectObject(dcTemp, old5);

   // eye candy only - 
   // outline the magnified elliptical (circular) image in RED
   {
      RECT rc;
      rc.left   = mtlx;
      rc.top    = mtly;
      rc.right  = rc.left + size.cx;
      rc.bottom = rc.top  + size.cy;
      // this is a special ellipse maker, which select in a HOLLOW
      // brush first to avoid the FILL with current brush.
      DrawColoredEllipse( hdcDst, &rc, RGB(255,0,0) );
   }

   iret = 1;   // we have success

Cleanup:

   // delete all the work objects
   if( rgn1 )
      DeleteObject( rgn1 );
   if( rgn2 )
      DeleteObject( rgn2 );
   if( rgn3 )
      DeleteObject( rgn3 );
   if( hbrclrTransparency )
      DeleteObject( hbrclrTransparency );
   if( dcIn )
      DeleteDC( dcIn );
   if( bitmap )
      DeleteObject(bitmap);
   if( dcImg )
      DeleteDC( dcImg );
   if( bitmap2 )
      DeleteObject(bitmap2);
   if( dcAnd )
      DeleteDC( dcAnd );
   if( bitmapAnd )
      DeleteObject( bitmapAnd );
   if( dcTemp )
      DeleteDC( dcTemp );
   if( bitmapTemp )
      DeleteObject( bitmapTemp );

   MFREE(pms); // toss the memory

#ifdef _testStamp_H_
   long db = tm.elapsed();
   sprtf( "TEST_Magnify took %d microseconds (%0.6f secs)\n", db,
      (double)db / 1000000.0 );
#endif // #ifdef _testStamp_H_

   return iret;
}

// eof - testMag.cxx

top

checked by tidy  Valid HTML 4.01 Transitional