home *** CD-ROM | disk | FTP | other *** search
/ Computer Buyer 1998 October / dpcb1098.iso / Business / Maxim / MAX5 / data.z / Transblt.cpp < prev    next >
C/C++ Source or Header  |  1998-05-15  |  10KB  |  264 lines

  1. //////////////////////////////////////////////////////////////////////////////
  2. // NAME.......: Transblt.CPP                                               
  3. // PURPOSE....: copy bmp with one transparent color (usually background)
  4. //              Same as BitBlt except with one transpartent color
  5. // WRITTEN....: 96/09/27 by Darko Juvan
  6. //
  7. // This code and information is provided "as is" without warranty of any
  8. // kind, either expressed or implied, including but not limited to the
  9. // implied warranties of merchantability and/or fitness for a particular
  10. // purpose..
  11. //
  12. // Copyright (c) 1998  Multiactive Software Inc.  All Rights Reserved.
  13. //
  14. //////////////////////////////////////////////////////////////////////////////
  15. #include "stdafx.h"
  16. #include "transblt.h"
  17.  
  18. //////////////////////////////////////////////////////////////////////////////
  19. //                        
  20. // FUNCTION...: TransparentBlt ()
  21. //                                                  
  22. // DESCRIPTION: MFC version - using MFC GDI objects
  23. //
  24. //////////////////////////////////////////////////////////////////////////////
  25. void TransparentBlt (CDC* pdc, CBitmap* pBitmap, int xStart,
  26.     int yStart, COLORREF cTransparentColor)
  27. {
  28.     HDC hdc = pdc->GetSafeHdc(); 
  29.     HBITMAP hBitmap = (HBITMAP)pBitmap->GetSafeHandle();
  30.     HDC     hdcMemX;
  31.     BITMAP     bm;
  32.     POINT      ptSize;
  33.  
  34.     GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&bm);
  35.     ptSize.x = bm.bmWidth;            // Get width of bitmap
  36.     ptSize.y = bm.bmHeight;           // Get height of bitmap
  37.                                      // to logical points
  38.     
  39.     //draw no transparent
  40.     if(cTransparentColor == -2)
  41.     {
  42.         hdcMemX = CreateCompatibleDC(hdc);
  43.         DPtoLP(hdcMemX, &ptSize, 1);      // Convert from device
  44.         SelectObject(hdcMemX, hBitmap);   // Select the bitmap
  45.         BitBlt(hdc, xStart, yStart, ptSize.x, ptSize.y, hdcMemX, 0, 0, SRCCOPY);
  46.         DeleteDC(hdcMemX);
  47.         return;
  48.     }
  49.  
  50.     COLORREF   cColor;
  51.     HBITMAP    bmAndBack, bmAndObject, bmAndMem, bmSave;
  52.     HBITMAP    bmBackOld, bmObjectOld, bmMemOld, bmSaveOld;
  53.     HDC        hdcMem, hdcBack, hdcObject, hdcTemp, hdcSave;
  54.     
  55.     hdcTemp = CreateCompatibleDC(hdc);
  56.     SelectObject(hdcTemp, hBitmap);   // Select the bitmap
  57.     DPtoLP(hdcTemp, &ptSize, 1);      // Convert from device
  58.     
  59.     //take color from (1,1) position
  60.     if(cTransparentColor == -1)
  61.         cTransparentColor = GetPixel(hdcTemp, 1, 1);
  62.     
  63.     
  64.     // Create some DCs to hold temporary data.
  65.     hdcBack   = CreateCompatibleDC(hdc);
  66.     hdcObject = CreateCompatibleDC(hdc);
  67.     hdcMem    = CreateCompatibleDC(hdc);
  68.     hdcSave   = CreateCompatibleDC(hdc);
  69.     // Create a bitmap for each DC.
  70.     
  71.     // Monochrome DC
  72.     bmAndBack   = CreateBitmap(ptSize.x, ptSize.y, 1, 1, NULL);
  73.     
  74.     // Monochrome DC
  75.     bmAndObject = CreateBitmap(ptSize.x, ptSize.y, 1, 1, NULL);
  76.     bmAndMem    = CreateCompatibleBitmap(hdc, ptSize.x, ptSize.y);
  77.     bmSave      = CreateCompatibleBitmap(hdc, ptSize.x, ptSize.y);
  78.     
  79.     // Each DC must select a bitmap object to store pixel data.
  80.     bmBackOld   = (HBITMAP) SelectObject(hdcBack, bmAndBack);
  81.     bmObjectOld = (HBITMAP) SelectObject(hdcObject, bmAndObject);
  82.     bmMemOld    = (HBITMAP) SelectObject(hdcMem, bmAndMem);
  83.     bmSaveOld   = (HBITMAP) SelectObject(hdcSave, bmSave);
  84.     
  85.     // Set proper mapping mode.
  86.     SetMapMode(hdcTemp, GetMapMode(hdc));
  87.     
  88.     // Save the bitmap sent here, because it will be overwritten.
  89.     BitBlt(hdcSave, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0, SRCCOPY);
  90.     
  91.     // Set the background color of the source DC to the color.
  92.     // contained in the parts of the bitmap that should be transparent
  93.     cColor = SetBkColor(hdcTemp, cTransparentColor);
  94.     
  95.     // Create the object mask for the bitmap by performing a BitBlt
  96.     // from the source bitmap to a monochrome bitmap.
  97.     BitBlt(hdcObject, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0,
  98.           SRCCOPY);
  99.     
  100.     // Set the background color of the source DC back to the original
  101.     // color.
  102.     SetBkColor(hdcTemp, cColor);
  103.     
  104.     // Create the inverse of the object mask.
  105.     BitBlt(hdcBack, 0, 0, ptSize.x, ptSize.y, hdcObject, 0, 0,
  106.           NOTSRCCOPY);
  107.     
  108.     // Copy the background of the main DC to the destination.
  109.     BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdc, xStart, yStart,
  110.           SRCCOPY);
  111.     
  112.     // Mask out the places where the bitmap will be placed.
  113.     BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdcObject, 0, 0, SRCAND);
  114.     
  115.     // Mask out the transparent colored pixels on the bitmap.
  116.     BitBlt(hdcTemp, 0, 0, ptSize.x, ptSize.y, hdcBack, 0, 0, SRCAND);
  117.     
  118.     // XOR the bitmap with the background on the destination DC.
  119.     BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0, SRCPAINT);
  120.     
  121.     // Copy the destination to the screen.
  122.     BitBlt(hdc, xStart, yStart, ptSize.x, ptSize.y, hdcMem, 0, 0,
  123.           SRCCOPY);
  124.     
  125.     // Place the original bitmap back into the bitmap sent here.
  126.     BitBlt(hdcTemp, 0, 0, ptSize.x, ptSize.y, hdcSave, 0, 0, SRCCOPY);
  127.     
  128.     // Delete the memory bitmaps.
  129.     DeleteObject(SelectObject(hdcBack, bmBackOld));
  130.     DeleteObject(SelectObject(hdcObject, bmObjectOld));
  131.     DeleteObject(SelectObject(hdcMem, bmMemOld));
  132.     DeleteObject(SelectObject(hdcSave, bmSaveOld));
  133.     
  134.     // Delete the memory DCs.
  135.     DeleteDC(hdcMem);
  136.     DeleteDC(hdcBack);
  137.     DeleteDC(hdcObject);
  138.     DeleteDC(hdcSave);
  139.     DeleteDC(hdcTemp);
  140. }
  141.  
  142. //////////////////////////////////////////////////////////////////////////////
  143. //                        
  144. // FUNCTION...: TransparentBlt ()
  145. //                                                  
  146. // DESCRIPTION: windows API version - using GDI handlers
  147. //
  148. //////////////////////////////////////////////////////////////////////////////
  149. void TransparentBlt (HDC hdc, HBITMAP hBitmap, int xStart,
  150.     int yStart, COLORREF cTransparentColor)
  151. {
  152.     HDC     hdcMemX;
  153.     BITMAP     bm;
  154.     POINT      ptSize;
  155.  
  156.     GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&bm);
  157.     ptSize.x = bm.bmWidth;            // Get width of bitmap
  158.     ptSize.y = bm.bmHeight;           // Get height of bitmap
  159.     
  160.  
  161.     //draw no transparent
  162.     if(cTransparentColor == -2)
  163.     {
  164.         hdcMemX = CreateCompatibleDC(hdc);
  165.         SelectObject(hdcMemX, hBitmap);   // Select the bitmap
  166.         DPtoLP(hdcMemX, &ptSize, 1);      // Convert from device
  167.         BitBlt(hdc, xStart, yStart, ptSize.x, ptSize.y, hdcMemX, 0, 0, SRCCOPY);
  168.         DeleteDC(hdcMemX);
  169.         return;
  170.     }
  171.  
  172.     COLORREF   cColor;
  173.     HBITMAP    bmAndBack, bmAndObject, bmAndMem, bmSave;
  174.     HBITMAP    bmBackOld, bmObjectOld, bmMemOld, bmSaveOld;
  175.     HDC        hdcMem, hdcBack, hdcObject, hdcTemp, hdcSave;
  176.     
  177.     hdcTemp = CreateCompatibleDC(hdc);
  178.     SelectObject(hdcTemp, hBitmap);   // Select the bitmap
  179.     DPtoLP(hdcTemp, &ptSize, 1);      // Convert from device
  180.                                      // to logical points
  181.     
  182.     //take color from (1,1) position
  183.     if(cTransparentColor == -1)
  184.         cTransparentColor = GetPixel(hdcTemp, 1, 1);
  185.     
  186.     
  187.     // Create some DCs to hold temporary data.
  188.     hdcBack   = CreateCompatibleDC(hdc);
  189.     hdcObject = CreateCompatibleDC(hdc);
  190.     hdcMem    = CreateCompatibleDC(hdc);
  191.     hdcSave   = CreateCompatibleDC(hdc);
  192.     // Create a bitmap for each DC.
  193.     
  194.     // Monochrome DC
  195.     bmAndBack   = CreateBitmap(ptSize.x, ptSize.y, 1, 1, NULL);
  196.     
  197.     // Monochrome DC
  198.     bmAndObject = CreateBitmap(ptSize.x, ptSize.y, 1, 1, NULL);
  199.     bmAndMem    = CreateCompatibleBitmap(hdc, ptSize.x, ptSize.y);
  200.     bmSave      = CreateCompatibleBitmap(hdc, ptSize.x, ptSize.y);
  201.     
  202.     // Each DC must select a bitmap object to store pixel data.
  203.     bmBackOld   = (HBITMAP) SelectObject(hdcBack, bmAndBack);
  204.     bmObjectOld = (HBITMAP) SelectObject(hdcObject, bmAndObject);
  205.     bmMemOld    = (HBITMAP) SelectObject(hdcMem, bmAndMem);
  206.     bmSaveOld   = (HBITMAP) SelectObject(hdcSave, bmSave);
  207.     
  208.     // Set proper mapping mode.
  209.     SetMapMode(hdcTemp, GetMapMode(hdc));
  210.     
  211.     // Save the bitmap sent here, because it will be overwritten.
  212.     BitBlt(hdcSave, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0, SRCCOPY);
  213.     
  214.     // Set the background color of the source DC to the color.
  215.     // contained in the parts of the bitmap that should be transparent
  216.     cColor = SetBkColor(hdcTemp, cTransparentColor);
  217.     
  218.     // Create the object mask for the bitmap by performing a BitBlt
  219.     // from the source bitmap to a monochrome bitmap.
  220.     BitBlt(hdcObject, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0,
  221.           SRCCOPY);
  222.     
  223.     // Set the background color of the source DC back to the original
  224.     // color.
  225.     SetBkColor(hdcTemp, cColor);
  226.     
  227.     // Create the inverse of the object mask.
  228.     BitBlt(hdcBack, 0, 0, ptSize.x, ptSize.y, hdcObject, 0, 0,
  229.           NOTSRCCOPY);
  230.     
  231.     // Copy the background of the main DC to the destination.
  232.     BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdc, xStart, yStart,
  233.           SRCCOPY);
  234.     
  235.     // Mask out the places where the bitmap will be placed.
  236.     BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdcObject, 0, 0, SRCAND);
  237.     
  238.     // Mask out the transparent colored pixels on the bitmap.
  239.     BitBlt(hdcTemp, 0, 0, ptSize.x, ptSize.y, hdcBack, 0, 0, SRCAND);
  240.     
  241.     // XOR the bitmap with the background on the destination DC.
  242.     BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0, SRCPAINT);
  243.     
  244.     // Copy the destination to the screen.
  245.     BitBlt(hdc, xStart, yStart, ptSize.x, ptSize.y, hdcMem, 0, 0,
  246.           SRCCOPY);
  247.     
  248.     // Place the original bitmap back into the bitmap sent here.
  249.     BitBlt(hdcTemp, 0, 0, ptSize.x, ptSize.y, hdcSave, 0, 0, SRCCOPY);
  250.     
  251.     // Delete the memory bitmaps.
  252.     DeleteObject(SelectObject(hdcBack, bmBackOld));
  253.     DeleteObject(SelectObject(hdcObject, bmObjectOld));
  254.     DeleteObject(SelectObject(hdcMem, bmMemOld));
  255.     DeleteObject(SelectObject(hdcSave, bmSaveOld));
  256.     
  257.     // Delete the memory DCs.
  258.     DeleteDC(hdcMem);
  259.     DeleteDC(hdcBack);
  260.     DeleteDC(hdcObject);
  261.     DeleteDC(hdcSave);
  262.     DeleteDC(hdcTemp);
  263. }
  264.