home *** CD-ROM | disk | FTP | other *** search
/ Windows Graphics Programming / Feng_Yuan_Win32_GDI_DirectX.iso / Samples / Chapt_11 / AdvBitmap / RopChart.cpp < prev   
Encoding:
C/C++ Source or Header  |  2000-05-24  |  9.5 KB  |  296 lines

  1. //-----------------------------------------------------------------------------------//
  2. //              Windows Graphics Programming: Win32 GDI and DirectDraw               //
  3. //                             ISBN  0-13-086985-6                                   //
  4. //                                                                                   //
  5. //  Written            by  Yuan, Feng                             www.fengyuan.com   //
  6. //  Copyright (c) 2000 by  Hewlett-Packard Company                www.hp.com         //
  7. //  Published          by  Prentice Hall PTR, Prentice-Hall, Inc. www.phptr.com      //
  8. //                                                                                   //
  9. //  FileName   : ropchart.cpp                                                         //
  10. //  Description: Raster operation chart                                              //
  11. //  Version    : 1.00.000, May 31, 2000                                              //
  12. //-----------------------------------------------------------------------------------//
  13.  
  14. #define STRICT
  15. #define _WIN32_WINNT 0x0500
  16. #define NOCRYPT
  17.  
  18. #include <windows.h>
  19. #include <assert.h>
  20. #include <tchar.h>
  21. #include <math.h>
  22.  
  23. #include "..\..\include\Rop.h"
  24.  
  25. #define TextH  20
  26. #define xx(j)  (j)*(8*scale+10*scale)+32                      
  27. #define yy(i)  (i+2)*(8*scale+(20*3+23) & 0xFF8)+32
  28.  
  29.  
  30. void DispText(HDC hdc, int x, int y, LPCSTR mess)
  31. {   
  32.     if (mess!=NULL)
  33.         TextOut(hdc, x, y, mess, _tcslen(mess));
  34. }
  35.  
  36.  
  37. void DispRectangle(HDC hdc, RECT *rect, short offset, COLORREF color)
  38. {
  39.     HPEN pold;
  40.     
  41.     pold=(HPEN) SelectObject(hdc, CreatePen(PS_SOLID, 1, color));
  42.     
  43.     HGDIOBJ hOldBrush = SelectObject(hdc, GetStockObject(NULL_BRUSH));
  44.     Rectangle(hdc, rect->left-offset, rect->top-offset, rect->right+offset, rect->bottom+offset);
  45.     SelectObject(hdc, hOldBrush);
  46.  
  47.     DeleteObject(SelectObject(hdc, pold));            
  48. }
  49.  
  50.  
  51. void DispBmp(HDC hdc, HDC memdc, int x, int y, short scale, HBITMAP bmp, DWORD rop)
  52. {
  53.     BITMAP  bmpinfo;
  54.     HGDIOBJ oldbmp;
  55.     
  56.     GetObject(bmp, sizeof(BITMAP), &bmpinfo);
  57.     oldbmp = SelectObject(memdc, bmp);
  58.     
  59.     StretchBlt(hdc, 
  60.                x, y, bmpinfo.bmWidth*scale, bmpinfo.bmHeight*scale, memdc, 
  61.                0, 0, bmpinfo.bmWidth, bmpinfo.bmHeight, 
  62.                rop);
  63.                
  64.     SelectObject(memdc, oldbmp);
  65. }
  66.  
  67. const WORD Bit_Pattern    [] = { 0xF0, 0xF0, 0xF0, 0xF0, 0x0F, 0x0F, 0x0F, 0x0F };
  68. const WORD Bit_Source     [] = { 0xCC, 0xCC, 0x33, 0x33, 0xCC, 0xCC, 0x33, 0x33 };
  69. const WORD Bit_Destination[] = { 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55 };
  70.  
  71.  
  72. void TestRop3(HINSTANCE hInstance, HDC hDC)
  73. {   
  74.     HBITMAP Pbmp = CreateBitmap(8, 8, 1, 1, Bit_Pattern);
  75.     HBITMAP Sbmp = CreateBitmap(8, 8, 1, 1, Bit_Source);
  76.     HBITMAP Dbmp = CreateBitmap(8, 8, 1, 1, Bit_Destination);
  77.     HBITMAP Rbmp = CreateBitmap(8, 8, 1, 1, NULL);
  78.     
  79.     HBRUSH  Pat  = CreatePatternBrush(Pbmp);    // pattern brush
  80.     HDC        Src     = CreateCompatibleDC(hDC);        // memdc for source
  81.     HDC        Dst     = CreateCompatibleDC(hDC);        // memdc for destination
  82.     HDC     Rst  = CreateCompatibleDC(hDC);        // memdc for result
  83.  
  84.     SelectObject(Src, Sbmp);
  85.     SelectObject(Dst, Dbmp);
  86.     SelectObject(Rst, Pbmp);
  87.  
  88.     StretchBlt(hDC, 20,  20, 80, 80, Rst, 0, 0, 8, 8, SRCCOPY);
  89.     StretchBlt(hDC, 20, 220, 80, 80, Src, 0, 0, 8, 8, SRCCOPY);
  90.     StretchBlt(hDC, 20, 420, 80, 80, Dst, 0, 0, 8, 8, SRCCOPY);
  91.  
  92.     SetBkMode(hDC, TRANSPARENT);
  93.     TextOut(hDC, 20, 105, "Pattern", 7);
  94.     TextOut(hDC, 20, 305, "Source",  6);
  95.     TextOut(hDC, 20, 505, "Destination", 11);
  96.  
  97.     SelectObject(Rst, Rbmp);
  98.     SelectObject(Rst, Pat);
  99.        
  100.     char mess[3];
  101.  
  102.     for (int i=0; i<16; i++)
  103.     {
  104.         wsprintf(mess, "%02X", i);
  105.         TextOut(hDC, 140 + i*38, 10, mess, 2);
  106.  
  107.         wsprintf(mess, "%02X", i*16);
  108.         TextOut(hDC, 115, 30+i*38, mess, 2);
  109.     }
  110.  
  111.     for (int rop=0; rop<256; rop++)
  112.     { 
  113.         BitBlt(Rst, 0, 0, 8, 8, Dst, 0, 0, SRCCOPY);
  114.         BitBlt(Rst, 0, 0, 8, 8, Src, 0, 0, GetRopCode(rop));
  115.  
  116.         StretchBlt(hDC, 140 + (rop%16)*38, 30 + (rop/16)*38, 32, 32, Rst, 0, 0, 8, 8, SRCCOPY);
  117.     }
  118.  
  119.     DeleteObject(Src);
  120.     DeleteObject(Dst);
  121.     DeleteObject(Rst);
  122.     DeleteObject(Pat);
  123.     DeleteObject(Pbmp);
  124.     DeleteObject(Sbmp);
  125.     DeleteObject(Dbmp);
  126.     DeleteObject(Rbmp);
  127. }
  128.  
  129.  
  130. void TestRop4(HINSTANCE hInstance, HDC hDC)
  131. {   
  132.     int      oldbk;
  133.     COLORREF oldcl;
  134.     HDC      memdc;
  135.     BITMAP   bmpinfo;
  136. //  char     mess[20];
  137.     int      scale;
  138.     int x,y;
  139.     HBITMAP Pbmp, Sbmp, Dbmp;
  140.     HBRUSH  hbr, hbrOld;          
  141.     RECT    rect;
  142.     
  143.     memdc= CreateCompatibleDC(hDC);
  144. //  Pbmp = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_PATTERN));
  145. //  Dbmp = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_DESTINATION));
  146. //  Sbmp = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_SOURCE));
  147.  
  148.     Pbmp = CreateBitmap(8, 8, 1, 1, Bit_Pattern);
  149.     Sbmp = CreateBitmap(8, 8, 1, 1, Bit_Source);
  150.     Dbmp = CreateBitmap(8, 8, 1, 1, Bit_Destination);
  151.     
  152.     hbr  = CreatePatternBrush(Pbmp);
  153.     
  154.     hbrOld = (HBRUSH)SelectObject(hDC, hbr);
  155.     
  156.     oldbk=SetBkMode(hDC, OPAQUE);
  157.     oldcl=SetBkColor(hDC, RGB(255, 255, 255));
  158.     
  159.     GetObject(Sbmp, sizeof(BITMAP), &bmpinfo);
  160. //  if (IsPrinting(hDC))
  161. //      scale=2;
  162. //  else
  163.         scale=4;
  164.     
  165.     y=yy(-2);
  166.     x=10;
  167.  
  168.     DispText(hDC, x, y+20,        "SelectObject(CreatePatternBrush(Pattern))");
  169.     DispText(hDC, x, y+20*2,      "StretchBlt(Destination, SRCCOPY)");
  170.     DispText(hDC, x, y+20*3,      "StretchBlt(Source, Rop3)");
  171.  
  172.     DispText(hDC, x=xx(7), y-20, "Pattern");
  173.     DispBmp(hDC, memdc, x, y, scale, Pbmp, SRCCOPY);
  174.  
  175.     DispText(hDC, x=xx(10), y-20, "Source");    
  176.     DispBmp(hDC, memdc, x, y, scale, Sbmp, SRCCOPY);
  177.  
  178.     DispText(hDC, x=xx(13), y-20, "Destination");
  179.     DispBmp(hDC, memdc, x, y, scale, Dbmp, SRCCOPY);
  180.        
  181.     
  182.     DispText(hDC, x=xx(4), y-TextH,      "P: 1 1 1 1  0 0 0 0");
  183.     DispText(hDC, x,       y,            "S: 1 1 0 0  1 1 0 0");
  184.     DispText(hDC, x,       y+TextH,      "D: 1 0 1 0  1 0 1 0");
  185.     DispText(hDC, x,       y+TextH*2+10, "R: 1 1 1 0  0 0 1 0");
  186.  
  187.     y=yy(-1);
  188.     DispText(hDC, x=xx(13),  y-TextH*2,  "RopNeedsNoDestination");
  189.     DispText(hDC, x,        y-TextH,    "((Rop & 0xAA) >> 1) == (Rop & 0x55)");
  190.     rect.left  = x;
  191.     rect.top   = y;
  192.     rect.right = rect.left + 32*scale;
  193.     rect.bottom= rect.top  + 32*scale;
  194.     DispRectangle(hDC, &rect, 0, RGB(0xFF,0x00, 0x00)); 
  195.         
  196.     DispText(hDC, x=xx(10),  y-TextH*2,  "RopNeedsNoSource");
  197.     DispText(hDC, x,        y-TextH,    "((Rop & 0xCC) >> 2) == (Rop & 0x33)");
  198.     rect.left  = x;
  199.     rect.top   = y;
  200.     rect.right = rect.left + 32*scale;
  201.     rect.bottom= rect.top  + 32*scale;
  202.     DispRectangle(hDC, &rect, 0, RGB(0x00, 0xFF, 0x00));
  203.     
  204.     DispText(hDC, x=xx(7), y-TextH*2,  "RopNeedsNoPattern");
  205.     DispText(hDC, x,        y-TextH,    "((Rop & 0xF0) >> 4) == (Rop & 0x0F)");
  206.     rect.left  = x;
  207.     rect.top   = y;
  208.     rect.right = rect.left + 32*scale;
  209.     rect.bottom= rect.top  + 32*scale;
  210.     DispRectangle(hDC, &rect, 0, RGB(0x00, 0x00, 0xFF));
  211. /*    
  212.     for (int i=0; i<16; i++)
  213.     for (int j=0; j<16; j++)
  214.     { 
  215.         x   = xx(j); 
  216.         y   = yy(i);
  217.         unsigned char rop = i*16 + j;
  218.     
  219.         DispText(hDC, x, y-TextH*3-2, RopDict[rop].name);
  220.         DispText(hDC, x, y-TextH*2-3, RopDict[rop].formula);
  221.         
  222.         wsprintf(mess,"%06lx",   RopDict[rop].ropcode);
  223.         DispText(hDC, x, y-TextH-4, mess);
  224.     
  225.         POINT p = { x, y };
  226.         LPtoDP(hDC, &p, 1);
  227.         SetBrushOrgEx(hDC, p.x, p.y, NULL);
  228.  
  229.         DispBmp(hDC, memdc, x, y, scale, dbmp, SRCCOPY);
  230.         DispBmp(hDC, memdc, x, y, scale, sbmp, RopDict[rop].ropcode);
  231.         
  232.         rect.left   = x;
  233.         rect.top    = y;
  234.         rect.right  = rect.left + bmpinfo.bmWidth*scale  ;
  235.         rect.bottom = rect.top  + bmpinfo.bmHeight*scale ;
  236.         
  237.         if ( strchr(RopDict[rop].formula, 'D')==NULL )
  238.             DispRectangle(hDC, &rect, 3, RGB(0xFF,0x00, 0x00)); 
  239.         
  240.         if ( strchr(RopDict[rop].formula,'S')==NULL )
  241.             DispRectangle(hDC, &rect, 4, RGB(0x00, 0xFF, 0x00));
  242.           
  243.         if ( strchr(RopDict[rop].formula,'P')==NULL )
  244.             DispRectangle(hDC, &rect, 5, RGB(0x00, 0x00, 0xFF));
  245.     }
  246. */
  247.  
  248.     HBITMAP hBmp = CreateBitmap(8, 8, 1, 1, NULL);
  249.     SelectObject(memdc, hBmp);
  250.     SelectObject(memdc, hbr);
  251.  
  252.     HDC S = CreateCompatibleDC(hDC); SelectObject(S, Sbmp);
  253.     HDC D = CreateCompatibleDC(hDC); SelectObject(D, Dbmp);
  254.     
  255.     for (int i=0; i<16; i++)
  256.     for (int j=0; j<16; j++)
  257.     { 
  258.         x   = xx(0) + i*42; 
  259.         y   = yy(0) + j*42;
  260.         int rop = i*16 + j;
  261.  
  262.         BitBlt(memdc, 0, 0, 8, 8, D, 0, 0, SRCCOPY);
  263.         BitBlt(memdc, 0, 0, 8, 8, S, 0, 0, GetRopCode(rop));
  264.  
  265.         StretchBlt(hDC, x, y, 32, 32, memdc, 0, 0, 8, 8, SRCCOPY);
  266.         
  267.         rect.left   = x;
  268.         rect.top    = y;
  269.         rect.right  = rect.left + 32;
  270.         rect.bottom = rect.top  + 32;
  271.         
  272.         if ( RopNeedsNoDestination(rop) )
  273.             DispRectangle(hDC, &rect, 3, RGB(0xFF,0x00, 0x00)); 
  274.         
  275.         if ( RopNeedsNoSource(rop) )
  276.             DispRectangle(hDC, &rect, 4, RGB(0x00, 0xFF, 0x00));
  277.           
  278.         if ( RopNeedsNoPattern(rop) )
  279.             DispRectangle(hDC, &rect, 5, RGB(0x00, 0x00, 0xFF));
  280.     }
  281.       
  282.  
  283.     SetBkMode(hDC, oldbk);
  284.     SetBkColor(hDC, oldcl);
  285.       
  286.     DeleteObject(SelectObject(hDC, hbrOld));  
  287.     DeleteObject(memdc);
  288.     DeleteObject(Sbmp);
  289.     DeleteObject(Dbmp);
  290.     DeleteObject(Pbmp);
  291.  
  292. //  if ( IsPrinting(hDC) )
  293. //    SetScrollSizes(MM_TEXT, CSize(xx(16), yy(16)), CSize(40, 40), CSize(8,8));
  294. }
  295.  
  296.