home *** CD-ROM | disk | FTP | other *** search
/ Windows Graphics Programming / Feng_Yuan_Win32_GDI_DirectX.iso / Samples / include / Dib.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-12  |  6.3 KB  |  171 lines

  1. #pragma once
  2.  
  3. //-----------------------------------------------------------------------------------//
  4. //              Windows Graphics Programming: Win32 GDI and DirectDraw               //
  5. //                             ISBN  0-13-086985-6                                   //
  6. //                                                                                   //
  7. //  Written            by  Yuan, Feng                             www.fengyuan.com   //
  8. //  Copyright (c) 2000 by  Hewlett-Packard Company                www.hp.com         //
  9. //  Published          by  Prentice Hall PTR, Prentice-Hall, Inc. www.phptr.com      //
  10. //                                                                                   //
  11. //  FileName   : dib.h                                                                 //
  12. //  Description: Device independent bitmap                                           //
  13. //  Version    : 1.00.000, May 31, 2000                                              //
  14. //-----------------------------------------------------------------------------------//
  15.  
  16. typedef enum 
  17. {
  18.     DIB_1BPP,            //   2 color image, palette-based
  19.     DIB_2BPP,            //   4 color image, palttte-based
  20.     DIB_4BPP,            //  16 color image, palette-based
  21.     DIB_4BPPRLE,        //  16 color image, palette-based, RLE compressed
  22.     DIB_8BPP,            // 256 color image, palette-based 
  23.     DIB_8BPPRLE,        // 256 color image, palette-based, RLE compressed
  24.  
  25.     DIB_16RGB555,        // 15 bit RGB color image, 5-5-5
  26.     DIB_16RGB565,        // 16 bit RGB color image, 5-6-5, 1 bit unused
  27.     DIB_24RGB888,        // 24 bit RGB color image, 8-8-8
  28.     DIB_32RGB888,        // 32 bit RGB color image, 8-8-8, 8 bit unused
  29.  
  30.     DIB_32RGBA8888,        // 32 bit RGBA color image, 8-8-8-8
  31.  
  32.     DIB_16RGBbitfields,    // 16 bit RGB color image, non-standard bit masks, NT-only
  33.     DIB_32RGBbitfields,    // 32 bit RGB color image, non-standard bit masks, NT-only
  34.  
  35.     DIB_JPEG,            // embedded JPEG image
  36.     DIB_PNG                // embedded PNG image
  37. }  DIBFormat;
  38.  
  39.  
  40. const TCHAR * PixelFormatName(int id);
  41.  
  42. typedef enum 
  43. {
  44.     DIB_BMI_NEEDFREE   = 1,
  45.     DIB_BMI_READONLY   = 2,
  46.     DIB_BITS_NEEDFREE  = 4,
  47.     DIB_BITS_READONLY  = 8    
  48. };
  49.  
  50. class KDIB
  51. {
  52. //    template <class Transform>
  53. //        friend     bool TransformDIB(CDIB * dib, Transform map);
  54.  
  55. public:
  56.     DIBFormat     m_nImageFormat;    // pixel array format
  57.     int             m_Flags;             // DIB_BMI_NEEDFREE, ....
  58.     BITMAPINFO * m_pBMI;            // Bitmap Information Header + mask + color table
  59.     BYTE       * m_pBits;            // pixel array
  60.  
  61.     RGBTRIPLE  * m_pRGBTRIPLE;        // OS/2 DIB color table within m_pBMI
  62.     RGBQUAD    * m_pRGBQUAD;        // V3,4,5 DIB color table within m_pBMI
  63.     int             m_nClrUsed;        // No of color in color table
  64.     int             m_nClrImpt;        // Real color used
  65.     DWORD       * m_pBitFields;        // 16, 32-bpp masks within m_pBMI
  66.     
  67.     int             m_nWidth;            // image pixel width
  68.     int             m_nHeight;            // image pixel height, positive
  69.     int             m_nPlanes;            // plane count
  70.     int             m_nBitCount;        // bit per plane
  71.     int             m_nColorDepth;        // bit count * plane count
  72.     int             m_nImageSize;        // pixel array size
  73.  
  74.                                     // Precalculated values
  75.     int             m_nBPS;            // byte per scan line, per plane
  76.     BYTE *       m_pOrigin;            // point to logical origin
  77.     int             m_nDelta;            // delta to next scan line
  78.  
  79.     KDIB();                        // default constructor, empty image
  80.     virtual ~KDIB();            // virtual destructor
  81.  
  82.     BOOL Create(int width, int height, int bitcount);
  83.  
  84.     bool AttachDIB(BITMAPINFO * pDIB, BYTE * pBits, int flags);
  85.     bool LoadFile(const TCHAR * pFileName);
  86.     bool LoadBitmap(HMODULE hModlue, LPCTSTR pBitmapName);
  87.     void ReleaseDIB(void);            // release memory
  88.  
  89.     int  GetWidth(void)          const    { return m_nWidth;  }
  90.     int  GetHeight(void)      const    { return m_nHeight; }
  91.     int  GetDepth(void)          const { return m_nColorDepth; }
  92.     BITMAPINFO * GetBMI(void) const { return m_pBMI; }
  93.     BYTE * GetBits(void)      const { return m_pBits; }
  94.     int  GetBPS(void)          const { return m_nBPS; }
  95.  
  96.     bool IsCompressed(void) const 
  97.     {
  98.         return (m_nImageFormat == DIB_4BPPRLE) ||
  99.                (m_nImageFormat == DIB_8BPPRLE) ||
  100.                (m_nImageFormat == DIB_JPEG) ||
  101.                (m_nImageFormat == DIB_PNG);
  102.     }
  103.  
  104.     int  GetColorCount(void) const;
  105.  
  106.     int DrawDIB(HDC hDC, int dx, int dy, int dw, int dh, int sx, int sy, int sw, int sh, DWORD rop)
  107.     {
  108.         if ( m_pBMI )
  109.             return ::StretchDIBits(hDC, dx, dy, dw, dh, sx, sy, sw, sh, 
  110.                 m_pBits, m_pBMI, DIB_RGB_COLORS, rop);
  111.         else
  112.             return GDI_ERROR;
  113.     }
  114.  
  115.     int SetDIB(HDC hDC, int dx, int dy, int firstscan, int scanno, const BYTE * pBits)
  116.     {
  117.         if ( m_pBMI )
  118.             return ::SetDIBitsToDevice(hDC, 
  119.                     dx, dy, m_nWidth, m_nHeight, 0, 0, firstscan, scanno, 
  120.                     pBits, m_pBMI, DIB_RGB_COLORS);
  121.         else
  122.             return GDI_ERROR;
  123.     }
  124.  
  125.     HBITMAP ConvertToDDB(HDC hDC)
  126.     {
  127.         return CreateDIBitmap(hDC, & m_pBMI->bmiHeader, CBM_INIT, m_pBits,
  128.                     m_pBMI, DIB_RGB_COLORS);
  129.     }
  130.  
  131.     void DecodeDIBFormat(TCHAR mess[]);
  132.     BOOL SaveFile(const TCHAR * pFileName);
  133.  
  134.     // Pixel accessing
  135.     DWORD GetPixelIndex(int x, int y) const;
  136.     BOOL  SetPixelIndex(int x, int y, DWORD index);
  137.  
  138.     // transformation using GDI GetPixel/SetPixel
  139.     BOOL PlgBltGetPixel(const POINT * pPoint, 
  140.             HDC hSrc, int nXSrc, int nYSrc, int nWidth, int nHeight, HDC hDst);
  141.  
  142.     // transformation using GetPixelIndex/SetPixelIndex
  143.     BOOL  PlgBlt(const POINT * pPoint, KDIB * pSrc, int nXSrc, int nYSrc, int nWidth, int nHeight);
  144.  
  145.     // transformation using inline function + integer only operations
  146.     BOOL  PlgBlt24(const POINT * pPoint, KDIB * pSrc, int nXSrc, int nYSrc, int nWidth, int nHeight);
  147.  
  148.     typedef enum { method_gdi, method_direct, method_24bpp };
  149.     HBITMAP TransformBitmap(XFORM * xm, COLORREF crBack, int method);
  150. };
  151.  
  152. int GetDIBPixelSize(const BITMAPINFOHEADER & bmih);
  153. int GetDIBColorCount(const BITMAPINFOHEADER & bmih);
  154.  
  155. BITMAPINFO * BitmapToDIB(HPALETTE hPal,    // palette for color conversion
  156.                    HBITMAP  hBmp,                            // DDB for convert
  157.                    int        nBitCount, int nCompression);    // format wanted
  158.  
  159. int PixelFormat(HDC hdc);
  160.  
  161. void SaveWindowToDIB(HWND hWnd, int nBitCount, int nCompression);
  162. BOOL SaveDIBToFile(const TCHAR * pFileName, const BITMAPINFO * pBMI, const void * pBits);
  163.  
  164.  
  165. BOOL AlphaBlend3232(BITMAPINFO * pBMIDst, BYTE * pBitsDst, int dx, int dy, int w, int h,
  166.                     BITMAPINFO * pBMISrc, BYTE * pBitsSrc, int sx, int sy,
  167.                     BLENDFUNCTION blend);
  168.  
  169. // AlphaBlending a 32-bpp DIB to hdc
  170. BOOL AlphaBlend(HDC hdc, int x0, int y0, const BITMAPINFO *pbmpInfo, const void * pAlphaImageData);
  171.