home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / com / activexcontrol / webimage / dibcls.cpp < prev    next >
C/C++ Source or Header  |  1997-10-09  |  5KB  |  202 lines

  1. //=--------------------------------------------------------------------------=
  2. // DibCls.Cpp
  3. //=--------------------------------------------------------------------------=
  4. // Copyright 1995 - 1997 Microsoft Corporation.  All Rights Reserved.
  5. //
  6. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF 
  7. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO 
  8. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A 
  9. // PARTICULAR PURPOSE.
  10. //=--------------------------------------------------------------------------=
  11. //
  12. // contains the CDibFile and CDibSection classes.
  13. //
  14. #include "IPServer.H"
  15.  
  16. #include "DibCls.H"
  17.  
  18. #define WIDTHBYTES(bits)    (((bits) + 31) / 32 * 4)
  19.  
  20. //=--------------------------------------------------------------------------=
  21. // DIB Utitilty Classes
  22. //=--------------------------------------------------------------------------=
  23. // Not wholey generic but getting there...
  24. //
  25. // Notes:
  26. //
  27. CDibFile::CDibFile()
  28. {
  29.     m_headerSize = 0;
  30.     m_bmi.p = 0;
  31. }
  32.  
  33. CDibFile::~CDibFile()
  34. {
  35.     if( m_bmi.p )
  36.         delete m_bmi.p;
  37. }
  38.  
  39.  
  40. DWORD CDibFile::CalcImageSize()
  41. {
  42.     DWORD & dw = m_bmi.p->bmiHeader.biSizeImage;
  43.     if( dw == 0)
  44.         dw = WIDTHBYTES((DWORD)m_bmi.p->bmiHeader.biWidth *
  45.                 m_bmi.p->bmiHeader.biBitCount) * m_bmi.p->bmiHeader.biHeight;
  46.  
  47.     return(dw);
  48. }
  49.  
  50. HRESULT CDibFile::GetInfoHeader( IStream * strm )
  51. {
  52.     HRESULT hr = S_OK; m_bmi.bytes = new unsigned char[ m_headerSize ];
  53.  
  54.     if( !m_bmi.bytes )
  55.         hr = E_OUTOFMEMORY;
  56.  
  57.     if( SUCCEEDED(hr) )
  58.         hr = strm->Read(m_bmi.bytes,m_headerSize,0);
  59.  
  60.     if( SUCCEEDED(hr) )
  61.         CalcImageSize();
  62.  
  63.     return(hr);
  64. }
  65.  
  66. HRESULT CDibFile::GetFileHeader(IStream * strm)
  67. {
  68.     BITMAPFILEHEADER    bmfh;
  69.  
  70.     HRESULT    hr = strm->Read(&bmfh,sizeof(bmfh),0);
  71.     
  72.     if( SUCCEEDED(hr) && (bmfh.bfType != 0x4d42 ))
  73.         hr = E_UNEXPECTED;
  74.  
  75.     if( SUCCEEDED(hr) )
  76.         m_headerSize = bmfh.bfOffBits - sizeof(bmfh);
  77.  
  78.     return(hr);
  79. }
  80.  
  81.  
  82. CDibSection::CDibSection()
  83. {
  84.     m_bitsBase    = 0;
  85.     m_current    = 0;
  86.     m_memDC        = 0;
  87.     m_handle    =
  88.     m_oldBitmap = 0;
  89.     m_w            =
  90.     m_h            = 32;  // totally arbitrary
  91. }
  92.  
  93. CDibSection::~CDibSection()
  94. {
  95.     if( m_memDC )
  96.     {
  97.         if( m_oldBitmap )
  98.             ::SelectObject( m_memDC, m_oldBitmap );
  99.  
  100.         ::DeleteDC(m_memDC);
  101.     }
  102.  
  103.     if( m_handle )
  104.         ::DeleteObject(m_handle);
  105.  
  106. }
  107.     
  108.     
  109. HRESULT CDibSection::Create(CDibFile& dibFile)
  110. {
  111.     HRESULT                hr        = S_OK;
  112.     BITMAPINFOHEADER *    bmih    = dibFile;    // will convert itself
  113.  
  114.     m_handle = ::CreateDIBSection(
  115.                     m_memDC,                // handle to device context
  116.                     dibFile,                // pointer to structure containing bitmap size,
  117.                                             //    format, and color data
  118.                     DIB_RGB_COLORS,            // color data type indicator: RGB values or
  119.                                             //    palette indices
  120.                     (void **)&m_bitsBase,    // pointer to variable to receive a pointer
  121.                                             //    to the bitmap's bit values
  122.                     0,                        // optional handle to a file mapping object
  123.                     0                        // offset to the bitmap bit values
  124.                                             //    within the file mapping object
  125.                     );
  126.  
  127.     if( !m_handle )
  128.         hr = E_FAIL;
  129.     
  130.     if( SUCCEEDED(hr) )
  131.     {
  132.         m_oldBitmap = (HBITMAP) ::SelectObject( m_memDC, m_handle );
  133.         
  134.         if( !m_oldBitmap )
  135.             hr = E_FAIL;
  136.     }
  137.  
  138.     if( SUCCEEDED(hr) )
  139.     {
  140.         m_current = m_bitsBase;
  141.  
  142.         m_w = bmih->biWidth;
  143.         m_h = bmih->biHeight;
  144.         
  145.         if( m_h < 0 )
  146.             m_h *= -1;
  147.     }
  148.         
  149.     return(hr);
  150. }
  151.  
  152. HRESULT CDibSection::ReadFrom( IStream * strm, DWORD amount )
  153. {
  154.    DWORD    dwRead      = 0;
  155.    HRESULT  hr;
  156.  
  157.    do
  158.    {
  159.       hr = strm->Read(m_current, m_imageSize, &dwRead);
  160.  
  161.       if (SUCCEEDED(hr) || hr == E_PENDING)
  162.           m_current += dwRead;
  163.    }
  164.    while (!(hr == E_PENDING || hr == S_FALSE) && SUCCEEDED(hr));
  165.  
  166.    return (hr);
  167. }
  168.  
  169.  
  170. HRESULT CDibSection::Setup(HDC hdc)
  171. {
  172.     m_memDC = ::CreateCompatibleDC(hdc);
  173.  
  174.     return( m_memDC ? NOERROR : E_FAIL );
  175. }
  176.  
  177.  
  178. HRESULT    CDibSection::PaintTo(HDC hdc, int x, int y)
  179. {
  180.     BOOL b = BitBlt(
  181.                  hdc,        // handle to destination device context
  182.                  x,            // x-coordinate of destination rectangle's upper-left corner
  183.                  y,            // x-coordinate of destination rectangle's upper-left corner
  184.                  m_w,        // width of destination rectangle
  185.                  m_h,        // height of destination rectangle
  186.                  m_memDC,    // handle to source device context
  187.                  0,            // x-coordinate of source rectangle's upper-left corner
  188.                  0,            // y-coordinate of source rectangle's upper-left corner
  189.                  SRCCOPY    // raster operation code
  190.                 );
  191.  
  192.     return( b ? NOERROR : E_FAIL );
  193. }
  194.  
  195. HRESULT    CDibSection::GetSize(SIZEL &sz)
  196. {
  197.     sz.cx = m_w;
  198.     sz.cy = m_h;
  199.  
  200.     return(S_OK);
  201. }
  202.