www.pudn.com > XRayImg.rar > dibimage.h


/*
Module : DIBIMAGE.H
Purpose: Defines the interface to an MFC class that encapsulates DIBs
and supports a variety of image manipulation functions on it
Created: PJN / 23-07-1997

Copyright (c) 1997 - 2000 by PJ Naughter.
All rights reserved.

*/


////////////////////////////////// Macros ///////////////////////////

#ifndef __DIBIMAGE_H__
#define __DIBIMAGE_H__

//不需要jpeg支持
#ifdef DIBIMAGE_NO_JPEG
#undef DIBIMAGE_NO_JPEG
#endif

////////////////////////////////// Includes /////////////////////////
#include "dibapi.h" // The "C/SDK" style functions provided by MS to
// handle DIBs which are used internally by CDibImage





/////////////////////////// Classes /////////////////////////////////


//forward declaration
class CDibImage;




//Implements a generic selection object (pure virtual)
class CWorkingArea
{
public:
virtual BOOL PointInSelection(const CPoint&amt; point) = 0;
virtual CRect BoundingRectangle() = 0;
virtual CWorkingArea* Clone() = 0;
};


//A rectangular selection
class CRectWorkingArea : public CWorkingArea
{
public:
CRectWorkingArea(CRect rect) { m_Rect = rect; };

virtual BOOL PointInSelection(const CPoint&amt; point) { return m_Rect.PtInRect(point); };
virtual CRect BoundingRectangle() { return m_Rect; };
virtual CWorkingArea* Clone() { return new CRectWorkingArea(m_Rect); };

protected:
CRect m_Rect;
};


//A filter which can be used in call to CDibImage::UserDefinedFilter(..)
class CUserDefinedFilter
{
public:
virtual COLORREF Filter(CDibImage&amt; dibImage, LPSTR lpDibBits, int x, int y)=0;
};


//a concrete implementation of a user defined filter
class C3By3Filter : public CUserDefinedFilter
{
public:
C3By3Filter();
int m_nValues[3][3];
int m_nDivision;
int m_nBias;

virtual COLORREF Filter(CDibImage&amt; dibImage, LPSTR lpDibBits, int x, int y);
};


//a concrete implementation of a user defined filter
class C5By5Filter : public CUserDefinedFilter
{
public:
C5By5Filter();
int m_nValues[5][5];
int m_nDivision;
int m_nBias;

virtual COLORREF Filter(CDibImage&amt; dibImage, LPSTR lpDibBits, int x, int y);
};


//a concrete implementation of a user defined filter
class C3By3MedianFilter : public CUserDefinedFilter
{
public:
C3By3MedianFilter();
virtual COLORREF Filter(CDibImage&amt; dibImage, LPSTR lpDibBits, int x, int y);

protected:
static int CompareFunc(const void *elem1, const void *elem2);

COLORREF m_Ordered[9];
};


//a concrete implementation of a user defined filter
class C3By3MeanFilter : public CUserDefinedFilter
{
public:
C3By3MeanFilter();
virtual COLORREF Filter(CDibImage&amt; dibImage, LPSTR lpDibBits, int x, int y);
};


//a concrete implementation of a user defined filter
class C7By7Filter : public CUserDefinedFilter
{
public:
C7By7Filter();
int m_nValues[7][7];
int m_nDivision;
int m_nBias;

virtual COLORREF Filter(CDibImage&amt; dibImage, LPSTR lpDibBits, int x, int y);
};


//Class to hold an undo state
class CUndoNode
{
public:
CUndoNode(CDibImage* pImage, const CString&amt; sDescription);
~CUndoNode();

CString GetDescription() const { return m_sDescription; };
CDibImage* GetImage() const { return m_pImage; };

void SetDescription(const CString&amt; sDescription) { m_sDescription = sDescription; };

protected:
CDibImage* m_pImage;
CString m_sDescription;
};


//The DIB class itself
class CDibImage
{
public:
//删除所有保存的备份
void RemoveAllRedoItems();
void RemoveAllUndoItems();
//gray adjust scheme
enum
{
GRAY_MAX_VALUE=0, GRAY_AVERAGE_VALUE,GRAY_SCALE_AVERAGE_VALUE
};

//设置新的dib尺寸
BOOL ResizeImageSize(int xNew,int yNew,BOOL bNeedBack=TRUE);

BOOL Rotate90Unclock(BOOL bNeedBack=TRUE);
BOOL Rotate90Clock(BOOL bNeedBack=TRUE);

//Creation &amt; Destruction
CDibImage();
~CDibImage();
BOOL Attach(HDIB hGlobal);
HDIB Detach(void);
void Destroy();
BOOL Create(CSize size, WORD nBitCount);
void DestroyWorkingArea();
void DestroyHDIB();
void DestroyHPALLETTE();

//Static functions
static WORD GetVersion();
static void RGBtoHSL(COLORREF rgb, double* H, double* S, double* L);
static COLORREF HLStoRGB(const double&amt; H, const double&amt; L, const double&amt; S);

//Loading &amt; Saving (File &amt; Resource)
BOOL Load(LPCTSTR lpszPathName);
BOOL Load(HINSTANCE hInst, LPCTSTR lpResourceName);
BOOL Load(HINSTANCE hInst, UINT uID) { return Load(hInst, MAKEINTRESOURCE(uID)); };
BOOL Save(LPCTSTR lpszPathName);

//Clipboard / Screen Capture support
BOOL CopyToClipboard();
BOOL PasteFromClipboard();
static BOOL PasteAvailable();
BOOL CopyFromBitmap(HBITMAP hBitmap, HPALETTE hPal);
HBITMAP CopyToBitmap(void);
BOOL CopyFromMemBmpFile(HMEMBMPFILE hMemBmpFile);
HMEMBMPFILE CopyToMemBmpFile(void);
BOOL CopyFromWindow(CWnd *pWnd, CRect* pScreenRect = NULL);

//Copying
CDibImage(const CDibImage&amt; ds);
CDibImage&amt; operator=(const CDibImage&amt; ds);
BOOL CopySelection(CDibImage&amt; dib);

//Multi level Undo / Redo support
void SetUndoSize(int nUndoSize);
int UndoSize() const { return m_nUndoSize; };
BOOL SaveState(const CString&amt; sDescription);
BOOL SaveState(UINT nID);
BOOL UndoAvailable();
BOOL Undo();
BOOL Redo();
BOOL RedoAvailable();
CString UndoDescription() const;
CString RedoDescription() const;

//Selection / Working Area support
CWorkingArea* GetWorkingArea();
void SetWorkingArea(CWorkingArea* pWorkingArea);

//Misc functions
int ColorsUsed() const;
LPSTR GetDIBBits();
BOOL DataFormatSupported(void) const;
BOOL FilteringFormatSupported(void) const;
BOOL ConvertTo24Bits(void);
BOOL IsRunLengthEncoded() const;

//Area Image processing support
BOOL SetColor(COLORREF color);
BOOL Flip(BOOL bNeedBack=TRUE);
BOOL Mirror(BOOL bNeedBack=TRUE);

//Color Image Processing support
BOOL AdjustBrightness(int nValue,BOOL bNeedBack=TRUE);
BOOL AdjustContrast(int nValue,BOOL bNeedBack=TRUE);
BOOL AdjustGammaCorrection(float Value,BOOL bNeedBack=TRUE);

BOOL AdjustHighLight(int Percentage,BOOL bNeedBack=TRUE);
BOOL AdjustMidtone(int Percentage,BOOL bNeedBack=TRUE);
BOOL AdjustShadow(int Percentage,BOOL bNeedBack=TRUE);
BOOL AdjustHue(int Percentage,BOOL bNeedBack=TRUE);
BOOL AdjustSaturation(int Percentage,BOOL bNeedBack=TRUE);
BOOL AdjustHSL(int PercentHue, int PercentSaturation, int PercentLuminosity,BOOL bNeedBack=TRUE);
BOOL AdjustRed(int Percentage,BOOL bNeedBack=TRUE);
BOOL AdjustGreen(int Percentage,BOOL bNeedBack=TRUE);
BOOL AdjustBlue(int Percentage,BOOL bNeedBack=TRUE);
BOOL AdjustGrayscale(int iGrayType=GRAY_SCALE_AVERAGE_VALUE,BOOL bNeedBack=TRUE);
BOOL Negate(BOOL bNeedBack=TRUE);

//Filter Image processing support
BOOL FindEdgesFilter(BOOL bNeedBack=TRUE);
BOOL FindVerticalEdgesFilter(BOOL bNeedBack=TRUE);
BOOL FindHorizontalEdgesFilter(BOOL bNeedBack=TRUE);
BOOL BlurFilter(BOOL bNeedBack=TRUE);
BOOL AddNoiseFilter(int Percentage,BOOL bNeedBack=TRUE);
BOOL MedianFilter(BOOL bNeedBack=TRUE);
BOOL UserDefinedFilter(CUserDefinedFilter&amt; Filter,BOOL bNeedBack=TRUE);
BOOL MeanFilter(BOOL bNeedBack=TRUE);

//Channel processing support
BOOL SplitChannels(CDibImage&amt; red, CDibImage&amt; green, CDibImage&amt; blue);
BOOL CombineChannels(const CDibImage&amt; red, const CDibImage&amt; green, const CDibImage&amt; blue);
BOOL GetRedChannel(CDibImage&amt; red);
BOOL GetGreenChannel(CDibImage&amt; green);
BOOL GetBlueChannel(CDibImage&amt; blue);

//Drawing support
BOOL Draw(CDC&amt; dc, const CRect* rcDst=NULL, const CRect* rcSrc=NULL,
CPalette* pPal=NULL,CRect *rcClient=NULL);

//Data accessors
CSize Size() const { return CSize(m_nWidth, m_nHeight); };
CRect Rect() const { return CRect(CPoint(0, 0), Size()); };
int Width() const { return m_nWidth; };
int Height() const { return m_nHeight; };
int ScanWidth() const { return m_nScanWidth; };
int BitsPerPixel() const { return m_nBitsPerPixel; };

//Direct Pixel access
inline BOOL GetPixel(int x, int y, COLORREF&amt; value, LPSTR lpDibBits = NULL) const;
inline BOOL SetPixel(int x, int y, const COLORREF&amt; value, LPSTR lpDibBits = NULL);
BOOL GetPixelData(int x, int y, ULONG&amt; value, LPSTR lpDibBits) const;
BOOL SetPixelData(int x, int y, const ULONG&amt; value, LPSTR lpDibBits);
LPBYTE GetPixelAddress(int x, int y, LPBYTE lpDibBits) const;

//Channel access support
BOOL GetRedHistogram(int* RedChannel, int nSize);
BOOL GetGreenHistogram(int* GreenChannel, int nSize);
BOOL GetBlueHistogram(int* BlueChannel, int nSize);
BOOL GetHistogram(int* RedChannel, int nRedSize, int* GreenChannel, int nGreenSize, int* BlueChannel, int nBlueSize);

//If you really must muck around with the internals
//of the class
HDIB GetHDIB() const { return m_hDib; };
HPALETTE GetHPALLETTE() const { return m_Pal; };

protected:
//Internal functions
int GetBitsPerPixel() const;
int ComputePaletteSize(DWORD nBitCount);
static double HuetoRGB(double m1, double m2, double h);

//member variables
HDIB m_hDib;
HPALETTE m_Pal;
int m_nWidth;
int m_nHeight;
int m_nScanWidth;
int m_nBitsPerPixel;
CWorkingArea* m_pWorkingArea;
DWORD m_dwChannel;
int m_nUndoSize;

//Undo / redo support member variables
CArray<CUndoNode*, CUndoNode*&amt;> m_UndoStack;
CArray<CUndoNode*, CUndoNode*&amt;> m_RedoStack;
CString m_sCurrentDescription;

public:
//读取原始数据文件
BOOL ReadDATFile(CFile&amt; f);
private:
//DEAL ".dat"原始文件数据
UCHAR* DealDatFileData(UCHAR* pRevBuff,WORD *pRevBuff1, WORD*pRevBuff2);

};


#endif //__DIBIMAGE_H__