dibimage.h
Upload User: ty808080
Upload Date: 2022-02-22
Package Size: 101k
Code Size: 8k
Category:

GDI-Bitmap

Development Platform:

Visual C++

  1. /*
  2. Module : DIBIMAGE.H
  3. Purpose: Defines the interface to an MFC class that encapsulates DIBs
  4.          and supports a variety of image manipulation functions on it
  5. Created: PJN / DIBIMAGE/1 / 23-07-1997
  6. History: None
  7. Copyright (c) 1997 by PJ Naughter.  
  8. All rights reserved.
  9. */
  10. ////////////////////////////////// Macros ///////////////////////////
  11. #ifndef __DIBIMAGE_H__
  12. #define __DIBIMAGE_H__
  13. ////////////////////////////////// Includes /////////////////////////
  14. #include "dibapi.h"   // The "C/SDK" style functions provided by MS to 
  15.                       // handle DIBs which are used internally by CDibImage
  16. /////////////////////////// Classes /////////////////////////////////
  17. //forward declaration
  18. class CDibImage;
  19. //Implements a generic selection object (pure virtual)
  20. class CWorkingArea
  21. {
  22. public:
  23.   virtual BOOL PointInSelection(const CPoint& point) = 0;
  24.   virtual CRect BoundingRectangle() = 0;
  25.   virtual CWorkingArea* Clone() = 0;
  26. };
  27. //A rectangular selection
  28. class CRectWorkingArea : public CWorkingArea
  29. {
  30. public:
  31.   CRectWorkingArea(CRect rect) { m_Rect = rect; };
  32.   virtual BOOL PointInSelection(const CPoint& point) { return m_Rect.PtInRect(point); };
  33.   virtual CRect BoundingRectangle() { return m_Rect; };
  34.   virtual CWorkingArea* Clone() { return new CRectWorkingArea(m_Rect); };
  35. protected:
  36.   CRect m_Rect;
  37. };
  38. //A filter which can be used in call to CDibImage::UserDefinedFilter(..)
  39. class CUserDefinedFilter
  40. {
  41. public:
  42.   virtual COLORREF Filter(CDibImage& dibImage, LPSTR lpDibBits, int x, int y)=0;
  43. };
  44. //a concrete implementation of a user defined filter
  45. class C3By3Filter : public CUserDefinedFilter
  46. {
  47. public:
  48.   C3By3Filter();
  49.   int m_nValues[3][3];
  50.   int m_nDivision;
  51.   int m_nBias;
  52.   virtual COLORREF Filter(CDibImage& dibImage, LPSTR lpDibBits, int x, int y);
  53. };
  54. //a concrete implementation of a user defined filter
  55. class C5By5Filter : public CUserDefinedFilter
  56. {
  57. public:
  58.   C5By5Filter();
  59.   int m_nValues[5][5];
  60.   int m_nDivision;
  61.   int m_nBias;
  62.   virtual COLORREF Filter(CDibImage& dibImage, LPSTR lpDibBits, int x, int y);
  63. };
  64. //a concrete implementation of a user defined filter
  65. class C3By3MedianFilter : public CUserDefinedFilter
  66. {
  67. public:
  68.   C3By3MedianFilter();
  69.   virtual COLORREF Filter(CDibImage& dibImage, LPSTR lpDibBits, int x, int y);
  70. protected:
  71.   static int CompareFunc(const void *elem1, const void *elem2);
  72.   COLORREF m_Ordered[9];
  73. };
  74. //a concrete implementation of a user defined filter
  75. class C7By7Filter : public CUserDefinedFilter
  76. {
  77. public:
  78.   C7By7Filter();
  79.   int m_nValues[7][7];
  80.   int m_nDivision;
  81.   int m_nBias;
  82.   virtual COLORREF Filter(CDibImage& dibImage, LPSTR lpDibBits, int x, int y);
  83. };
  84. //Class to hold an undo state
  85. class CUndoNode                                            
  86. {
  87. public:
  88.   CUndoNode(CDibImage* pImage, const CString& sDescription);
  89.   ~CUndoNode();
  90.   CString GetDescription() const { return m_sDescription; };
  91.   CDibImage* GetImage() const { return m_pImage; };
  92.   void SetDescription(const CString& sDescription) { m_sDescription = sDescription; };
  93. protected:
  94.   CDibImage* m_pImage;
  95.   CString m_sDescription;
  96. };
  97. //The DIB class itself
  98. class CDibImage
  99. {
  100. public:
  101. //Creation & Destruction
  102.   CDibImage();
  103.   ~CDibImage();
  104.   BOOL Attach(HGLOBAL hGlobal);
  105. void Destroy();
  106.   BOOL Create(CSize size, WORD nBitCount);
  107. //Static functions
  108.   static WORD     GetVersion();
  109.   static void     RGBtoHSL(COLORREF rgb, double* H, double* S, double* L);
  110.   static COLORREF HLStoRGB(const double& H, const double& L, const double& S);
  111. //Loading & Saving (File & Resource)
  112. BOOL Load(LPCTSTR lpszPathName);
  113. BOOL Load(HINSTANCE hInst, LPCTSTR lpResourceName);
  114. BOOL Load(HINSTANCE hInst, UINT uID) { return Load(hInst, MAKEINTRESOURCE(uID)); };
  115.   BOOL Save(LPCTSTR lpszPathName);
  116. //Clipboard support
  117.   BOOL CopyToClipboard();
  118.   BOOL PasteFromClipboard();
  119.   static BOOL PasteAvailable();
  120. //Copying
  121.   CDibImage(const CDibImage& ds);
  122.   CDibImage& operator=(const CDibImage& ds);
  123.   BOOL CopySelection(CDibImage& dib);
  124. //Multi level Undo / Redo support
  125.   void SetUndoSize(int nUndoSize);
  126.   int  UndoSize() const { return m_nUndoSize; };
  127.   BOOL SaveState(const CString& sDescription);
  128.   BOOL SaveState(UINT nID);
  129.   BOOL UndoAvailable();
  130.   BOOL Undo();
  131.   BOOL Redo();
  132.   BOOL RedoAvailable();
  133.   CString UndoDescription() const;
  134.   CString RedoDescription() const;
  135. //Selection / Working Area support
  136.   CWorkingArea* GetWorkingArea();
  137.   void  SetWorkingArea(CWorkingArea* pWorkingArea);
  138. //Misc functions
  139.   int ColorsUsed() const;
  140.   LPSTR GetDIBBits();
  141. //Area Image processing support
  142.   BOOL SetColor(COLORREF color);
  143.   BOOL Flip();
  144.   BOOL Mirror();
  145. //Color Image Processing support
  146.   BOOL AdjustBrightness(int Percentage);
  147.   BOOL AdjustContrast(int Percentage);
  148.   BOOL AdjustGammaCorrection(float Value);
  149.   BOOL AdjustHighLight(int Percentage);
  150.   BOOL AdjustMidtone(int Percentage);
  151.   BOOL AdjustShadow(int Percentage);
  152.   BOOL AdjustHue(int Percentage);
  153.   BOOL AdjustSaturation(int Percentage);
  154.   BOOL AdjustHSL(int PercentHue, int PercentSaturation, int PercentLuminosity);
  155.   BOOL AdjustRed(int Percentage);
  156.   BOOL AdjustGreen(int Percentage);
  157.   BOOL AdjustBlue(int Percentage);
  158.   BOOL Greyscale();
  159.   BOOL Negate();
  160. //Filter Image processing support
  161.   BOOL FindEdgesFilter();
  162.   BOOL FindVerticalEdgesFilter();
  163.   BOOL FindHorizontalEdgesFilter();
  164.   BOOL BlurFilter();
  165.   BOOL AddNoiseFilter(int Percentage);
  166.   BOOL MedianFilter();
  167.   BOOL UserDefinedFilter(CUserDefinedFilter& Filter);
  168. //Channel processing support
  169.   BOOL SplitChannels(CDibImage& red, CDibImage& green, CDibImage& blue);
  170.   BOOL CombineChannels(const CDibImage& red, const CDibImage& green, const CDibImage& blue);
  171.   BOOL GetRedChannel(CDibImage& red);
  172.   BOOL GetGreenChannel(CDibImage& green);
  173.   BOOL GetBlueChannel(CDibImage& blue);
  174. //Drawing support
  175.   BOOL Draw(CDC& dc, const CRect* rcDst=NULL, const CRect* rcSrc=NULL, CPalette* pPal=NULL);
  176. //Data accessors
  177.   CSize Size() const          { return CSize(m_nWidth, m_nHeight); };
  178.   CRect Rect() const          { return CRect(CPoint(0, 0), Size()); };
  179.   int   Width() const         { return m_nWidth; };
  180.   int   Height() const        { return m_nHeight; };
  181.   int   ScanWidth() const     { return m_nScanWidth; };
  182.   int   BitsPerPixel() const  { return m_nBitsPerPixel; };
  183. //Direct Pixel access
  184.   inline BOOL GetPixel(int x, int y, COLORREF& value, LPSTR lpDibBits = NULL) const;
  185.   inline BOOL SetPixel(int x, int y, const COLORREF& value, LPSTR lpDibBits = NULL);
  186. //Channel access support
  187.   BOOL GetRedHistogram(int* RedChannel, int nSize);
  188.   BOOL GetGreenHistogram(int* GreenChannel, int nSize);
  189.   BOOL GetBlueHistogram(int* BlueChannel, int nSize);
  190.   BOOL GetHistogram(int* RedChannel, int nRedSize, int* GreenChannel, int nGreenSize, int* BlueChannel, int nBlueSize);
  191. //If you really must muck around with the internals 
  192. //of the class
  193.   HDIB GetHDIB() const { return m_hDib; };
  194.   HPALETTE GetHPALLETTE() const { return m_Pal; };
  195. protected:
  196. //Internal functions
  197.   int GetBitsPerPixel() const;
  198.   int ComputePaletteSize(DWORD nBitCount);
  199.   static double HuetoRGB(double m1, double m2, double h);
  200. //member variables
  201.   HDIB          m_hDib;
  202.   HPALETTE      m_Pal;
  203.   int           m_nWidth;
  204.   int           m_nHeight;
  205.   int           m_nScanWidth;
  206.   int           m_nBitsPerPixel;
  207.   CWorkingArea* m_pWorkingArea;
  208.   DWORD         m_dwChannel;
  209.   int           m_nUndoSize;
  210. //Undo / redo support member variables
  211.   CArray<CUndoNode*, CUndoNode*&> m_UndoStack;
  212.   CArray<CUndoNode*, CUndoNode*&> m_RedoStack;
  213.   CString m_sCurrentDescription;
  214. };
  215. #endif //__DIBIMAGE_H__