home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 July / CMCD0704.ISO / Software / Freeware / Utilitare / VisualBoyAdvance-1.7.2 / win32 / include / cximage / ximage.h < prev    next >
C/C++ Source or Header  |  2004-02-29  |  21KB  |  548 lines

  1. /*
  2.  * File:    ximage.h
  3.  * Purpose:    General Purpose Image Class 
  4.  */
  5. /* === C R E D I T S  &  D I S C L A I M E R S ==============
  6.  * Permission is given by the author to freely redistribute and include
  7.  * this code in any program as long as this credit is given where due.
  8.  *
  9.  * CxImage (c)  07/Aug/2001 Davide Pizzolato - www.xdp.it
  10.  * CxImage version 5.99a 08/Feb/2004
  11.  * See the file history.htm for the complete bugfix and news report.
  12.  *
  13.  * original CImage and CImageIterator implementation are:
  14.  * Copyright: (c) 1995, Alejandro Aguilar Sierra <asierra(at)servidor(dot)unam(dot)mx>
  15.  *
  16.  * COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY
  17.  * OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES
  18.  * THAT THE COVERED CODE IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE
  19.  * OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED
  20.  * CODE IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT
  21.  * THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY
  22.  * SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL
  23.  * PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER
  24.  * THIS DISCLAIMER.
  25.  *
  26.  * Use at your own risk!
  27.  * ==========================================================
  28.  */
  29. #if !defined(__CXIMAGE_H)
  30. #define __CXIMAGE_H
  31.  
  32. #if _MSC_VER > 1000
  33. #pragma once
  34. #endif 
  35.  
  36. /////////////////////////////////////////////////////////////////////////////
  37. // CxImage supported features
  38. #define CXIMAGE_SUPPORT_ALPHA          1
  39. #define CXIMAGE_SUPPORT_SELECTION      0
  40. #define CXIMAGE_SUPPORT_TRANSFORMATION 0
  41. #define CXIMAGE_SUPPORT_DSP            0
  42. #define CXIMAGE_SUPPORT_LAYERS           0
  43.  
  44. #define CXIMAGE_SUPPORT_DECODE    1
  45. #define CXIMAGE_SUPPORT_ENCODE    0        //<vho><T.Peck>
  46. #define    CXIMAGE_SUPPORT_WINDOWS 1
  47. #define    CXIMAGE_SUPPORT_WINCE   0        //<T.Peck>
  48.  
  49. /////////////////////////////////////////////////////////////////////////////
  50. // CxImage supported formats
  51. #define CXIMAGE_SUPPORT_BMP 1
  52. #define CXIMAGE_SUPPORT_GIF 0
  53. #define CXIMAGE_SUPPORT_JPG 1
  54. #define CXIMAGE_SUPPORT_PNG 1
  55. #define CXIMAGE_SUPPORT_MNG 0
  56. #define CXIMAGE_SUPPORT_ICO 0
  57. #define CXIMAGE_SUPPORT_TIF 0
  58. #define CXIMAGE_SUPPORT_TGA 0
  59. #define CXIMAGE_SUPPORT_PCX 0
  60. #define CXIMAGE_SUPPORT_WBMP 0
  61. #define CXIMAGE_SUPPORT_WMF 0
  62. #define CXIMAGE_SUPPORT_J2K 0        // Beta, use JP2
  63. #define CXIMAGE_SUPPORT_JBG 0        // GPL'd see ../jbig/copying.txt & ../jbig/patents.htm
  64.  
  65. #define CXIMAGE_SUPPORT_JP2 0
  66. #define CXIMAGE_SUPPORT_JPC 0
  67. #define CXIMAGE_SUPPORT_PGX 0
  68. #define CXIMAGE_SUPPORT_PNM 0
  69. #define CXIMAGE_SUPPORT_RAS 0
  70.  
  71. /////////////////////////////////////////////////////////////////////////////
  72.  
  73. #include <TCHAR.h>    // For UNICODE support
  74. #include "xfile.h"
  75. #include "xiofile.h"
  76. #include "xmemfile.h"
  77.  
  78. #include "ximadefs.h"    //<vho> adjust some #define
  79.  
  80. /////////////////////////////////////////////////////////////////////////////
  81. // CxImage formats enumerator
  82. enum ENUM_CXIMAGE_FORMATS{
  83. CXIMAGE_FORMAT_UNKNOWN,
  84. #if CXIMAGE_SUPPORT_BMP
  85. CXIMAGE_FORMAT_BMP,
  86. #endif
  87. #if CXIMAGE_SUPPORT_GIF
  88. CXIMAGE_FORMAT_GIF,
  89. #endif
  90. #if CXIMAGE_SUPPORT_JPG
  91. CXIMAGE_FORMAT_JPG,
  92. #endif
  93. #if CXIMAGE_SUPPORT_PNG
  94. CXIMAGE_FORMAT_PNG,
  95. #endif
  96. #if CXIMAGE_SUPPORT_MNG
  97. CXIMAGE_FORMAT_MNG,
  98. #endif
  99. #if CXIMAGE_SUPPORT_ICO
  100. CXIMAGE_FORMAT_ICO,
  101. #endif
  102. #if CXIMAGE_SUPPORT_TIF
  103. CXIMAGE_FORMAT_TIF,
  104. #endif
  105. #if CXIMAGE_SUPPORT_TGA
  106. CXIMAGE_FORMAT_TGA,
  107. #endif
  108. #if CXIMAGE_SUPPORT_PCX
  109. CXIMAGE_FORMAT_PCX,
  110. #endif
  111. #if CXIMAGE_SUPPORT_WBMP
  112. CXIMAGE_FORMAT_WBMP,
  113. #endif
  114. #if CXIMAGE_SUPPORT_WMF
  115. CXIMAGE_FORMAT_WMF,
  116. #endif
  117. #if CXIMAGE_SUPPORT_J2K
  118. CXIMAGE_FORMAT_J2K,
  119. #endif
  120. #if CXIMAGE_SUPPORT_JBG
  121. CXIMAGE_FORMAT_JBG,
  122. #endif
  123. #if CXIMAGE_SUPPORT_JP2
  124. CXIMAGE_FORMAT_JP2,
  125. #endif
  126. #if CXIMAGE_SUPPORT_JPC
  127. CXIMAGE_FORMAT_JPC,
  128. #endif
  129. #if CXIMAGE_SUPPORT_PGX
  130. CXIMAGE_FORMAT_PGX,
  131. #endif
  132. #if CXIMAGE_SUPPORT_PNM
  133. CXIMAGE_FORMAT_PNM,
  134. #endif
  135. #if CXIMAGE_SUPPORT_RAS
  136. CXIMAGE_FORMAT_RAS,
  137. #endif
  138. CMAX_IMAGE_FORMATS
  139. };
  140.  
  141. //color to grey mapping <H. Muelner> <jurgene>
  142. //#define RGB2GRAY(r,g,b) (((b)*114 + (g)*587 + (r)*299)/1000)
  143. #define RGB2GRAY(r,g,b) (((b)*117 + (g)*601 + (r)*306) >> 10)
  144.  
  145. struct rgb_color { BYTE r,g,b; };
  146.  
  147.  
  148. // <VATI> text placement data
  149. // members must be initialized with the InitTextInfo(&this) function.
  150. typedef struct DLL_EXP tagCxTextInfo
  151. {
  152.     char     text[4096];      // text
  153.     LOGFONT  lfont;           // font and codepage data
  154.     COLORREF fcolor;          // foreground color
  155.     long     align;           // DT_CENTER, DT_RIGHT, DT_LEFT aligment for multiline text
  156.     BYTE     opaque;          // text has background or hasn't. Default is true.
  157.              // data for background (ignored if .opaque==FALSE) 
  158.     COLORREF bcolor;          // background color
  159.     float    b_opacity;       // opacity value for background between 0.0-1.0 Default is 0. (opaque)
  160.     BYTE     b_outline;       // outline width for background (zero: no outline)
  161.     BYTE     b_round;         // rounding radius for background rectangle. % of the height, between 0-50. Default is 10.
  162.                               // (backgr. always has a frame: width = 3 pixel + 10% of height by default.)
  163. }  CXTEXTINFO;
  164.  
  165. /////////////////////////////////////////////////////////////////////////////
  166. // CxImage class
  167. /////////////////////////////////////////////////////////////////////////////
  168. class DLL_EXP CxImage
  169. {
  170. //extensible information collector
  171. typedef struct tagCxImageInfo {
  172.     DWORD    dwEffWidth;            //DWORD aligned scan line width
  173.     BYTE*    pImage;                //THE IMAGE BITS
  174.     CxImage* pGhost;            //if this is a ghost, pGhost points to the body
  175.     CxImage* pParent;            //if this is a layer, pParent points to the body
  176.     DWORD    dwType;                //original image format
  177.     char    szLastError[256];    //debugging
  178.     long    nProgress;            //monitor
  179.     long    nEscape;            //escape
  180.     long    nBkgndIndex;        //used for GIF, PNG, MNG
  181.     RGBQUAD nBkgndColor;        //used for RGB transparency
  182.     BYTE    nQuality;            //used for JPEG
  183.     BYTE    nScale;                //used for JPEG <ignacio>
  184.     long    nFrame;                //used for TIF, GIF, MNG : actual frame
  185.     long    nNumFrames;            //used for TIF, GIF, MNG : total number of frames
  186.     DWORD    dwFrameDelay;        //used for GIF, MNG
  187.     long    xDPI;                //horizontal resolution
  188.     long    yDPI;                //vertical resolution
  189.     RECT    rSelectionBox;        //bounding rectangle
  190.     BYTE    nAlphaMax;            //max opacity (fade)
  191.     bool    bAlphaPaletteEnabled; //true if alpha values in the palette are enabled.
  192.     bool    bEnabled;            //enables the painting functions
  193.     long    xOffset;
  194.     long    yOffset;
  195.     DWORD    dwCodecOption;        //for GIF, TIF : 0=def.1=unc,2=fax3,3=fax4,4=pack,5=jpg
  196.     RGBQUAD last_c;                //for GetNearestIndex optimization
  197.     BYTE    last_c_index;
  198.     bool    last_c_isvalid;
  199.     long    nNumLayers;
  200.     DWORD    dwFlags;            // 0x??00000 = reserved, 0x00??0000 = blend mode, 0x0000???? = layer id - user flags
  201.  
  202. } CXIMAGEINFO;
  203.  
  204. public:
  205.     //constructors
  206.     CxImage(DWORD imagetype = 0);
  207.     CxImage(DWORD dwWidth, DWORD dwHeight, DWORD wBpp, DWORD imagetype = 0);
  208.     CxImage(const CxImage &src, bool copypixels = true, bool copyselection = true, bool copyalpha = true);
  209.     CxImage(const TCHAR * filename, DWORD imagetype);    // For UNICODE support: char -> TCHAR
  210. //    CxImage(const char * filename, DWORD imagetype);
  211.     CxImage(FILE * stream, DWORD imagetype);
  212.     CxImage(CxFile * stream, DWORD imagetype);
  213.     CxImage(BYTE * buffer, DWORD size, DWORD imagetype);
  214.     virtual ~CxImage() { Destroy(); };
  215.     CxImage& operator = (const CxImage&);
  216.  
  217.     //initializzation
  218.     void*    Create(DWORD dwWidth, DWORD dwHeight, DWORD wBpp, DWORD imagetype = 0);
  219.     bool    Destroy();
  220.     void    Clear(BYTE bval=0);
  221.     void    Copy(const CxImage &src, bool copypixels = true, bool copyselection = true, bool copyalpha = true);
  222.     bool    Transfer(CxImage &from);
  223.     bool    CreateFromArray(BYTE* pArray,DWORD dwWidth,DWORD dwHeight,DWORD dwBitsperpixel, DWORD dwBytesperline, bool bFlipImage);
  224.     bool    CreateFromMatrix(BYTE** ppMatrix,DWORD dwWidth,DWORD dwHeight,DWORD dwBitsperpixel, DWORD dwBytesperline, bool bFlipImage);
  225.  
  226.     //Attributes
  227.     long    GetSize();
  228.     BYTE*    GetBits(DWORD row = 0);
  229.     BYTE    GetColorType();
  230.     void*    GetDIB()        const {return pDib;}
  231.     DWORD    GetHeight()        const {return head.biHeight;}
  232.     DWORD    GetWidth()        const {return head.biWidth;}
  233.     DWORD    GetEffWidth()    const {return info.dwEffWidth;}
  234.     DWORD    GetNumColors()    const {return head.biClrUsed;}
  235.     WORD    GetBpp()        const {return head.biBitCount;}
  236.     DWORD    GetType()        const {return info.dwType;}
  237.     char*    GetLastError()    {return info.szLastError;}
  238.     const TCHAR* GetVersion();
  239.  
  240.     DWORD    GetFrameDelay() const {return info.dwFrameDelay;}
  241.     void    SetFrameDelay(DWORD d) {info.dwFrameDelay=d;}
  242.  
  243.     void    GetOffset(long *x,long *y) {*x=info.xOffset; *y=info.yOffset;}
  244.     void    SetOffset(long x,long y) {info.xOffset=x; info.yOffset=y;}
  245.  
  246.     BYTE    GetJpegQuality() const {return info.nQuality;}
  247.     void    SetJpegQuality(BYTE q) {info.nQuality = q;}
  248.  
  249.     //<ignacio> used for scaling down during JPEG decoding valid numbers are 1, 2, 4, 8
  250.     BYTE    GetJpegScale() const {return info.nScale;}
  251.     void    SetJpegScale(BYTE q) {info.nScale = q;}
  252.  
  253.     long    GetXDPI()        const {return info.xDPI;}
  254.     long    GetYDPI()        const {return info.yDPI;}
  255.     void    SetXDPI(long dpi);
  256.     void    SetYDPI(long dpi);
  257.  
  258.     DWORD    GetClrImportant() const {return head.biClrImportant;}
  259.     void    SetClrImportant(DWORD ncolors = 0);
  260.  
  261.     long    GetProgress()    const {return info.nProgress;}
  262.     long    GetEscape()     const {return info.nEscape;}
  263.     void    SetProgress(long p) {info.nProgress = p;}
  264.     void    SetEscape(long i)   {info.nEscape = i;}
  265.  
  266.     long    GetTransIndex()    const {return info.nBkgndIndex;}
  267.     RGBQUAD    GetTransColor();
  268.     void    SetTransIndex(long idx) {info.nBkgndIndex = idx;}
  269.     void    SetTransColor(RGBQUAD rgb) {rgb.rgbReserved=0; info.nBkgndColor = rgb;}
  270.     bool    IsTransparent() const {return info.nBkgndIndex>=0;} // <vho>
  271.  
  272.     DWORD    GetCodecOption() const {return info.dwCodecOption;}
  273.     void    SetCodecOption(DWORD opt) {info.dwCodecOption = opt;}
  274.  
  275.     DWORD    GetFlags() const {return info.dwFlags;}
  276.     void    SetFlags(DWORD flags, bool bLockReservedFlags = true);
  277.  
  278.     //void*    GetUserData() const {return info.pUserData;}
  279.     //void    SetUserData(void* pUserData) {info.pUserData = pUserData;}
  280.  
  281.     //palette operations
  282.     bool    IsGrayScale();
  283.     bool    IsIndexed() {return head.biClrUsed!=0;}
  284.     DWORD    GetPaletteSize();
  285.     RGBQUAD* GetPalette() const;
  286.     RGBQUAD GetPaletteColor(BYTE idx);
  287.     bool    GetPaletteColor(BYTE i, BYTE* r, BYTE* g, BYTE* b);
  288.     BYTE    GetNearestIndex(RGBQUAD c);
  289.     void    BlendPalette(COLORREF cr,long perc);
  290.     void    SetGrayPalette();
  291.     void    SetPalette(DWORD n, BYTE *r, BYTE *g, BYTE *b);
  292.     void    SetPalette(RGBQUAD* pPal,DWORD nColors=256);
  293.     void    SetPalette(rgb_color *rgb,DWORD nColors=256);
  294.     void    SetPaletteColor(BYTE idx, BYTE r, BYTE g, BYTE b, BYTE alpha=0);
  295.     void    SetPaletteColor(BYTE idx, RGBQUAD c);
  296.     void    SetPaletteColor(BYTE idx, COLORREF cr);
  297.     void    SwapIndex(BYTE idx1, BYTE idx2);
  298.     void    SetStdPalette();
  299.  
  300.     //pixel operations
  301.     bool    IsInside(long x, long y);
  302.     bool    IsTransparent(long x,long y);
  303.     RGBQUAD GetPixelColor(long x,long y, bool bGetAlpha = true);
  304.     BYTE    GetPixelIndex(long x,long y);
  305.     BYTE    GetPixelGray(long x, long y);
  306.     void    SetPixelColor(long x,long y,RGBQUAD c, bool bSetAlpha = false);
  307.     void    SetPixelColor(long x,long y,COLORREF cr);
  308.     void    SetPixelIndex(long x,long y,BYTE i);
  309.     void    DrawLine(int StartX, int EndX, int StartY, int EndY, RGBQUAD color, bool bSetAlpha=false);
  310.     void    DrawLine(int StartX, int EndX, int StartY, int EndY, COLORREF cr);
  311.  
  312.  
  313.     //painting operations
  314. #if CXIMAGE_SUPPORT_WINCE
  315.     long    Blt(HDC pDC, long x=0, long y=0);
  316. #endif
  317. #if CXIMAGE_SUPPORT_WINDOWS
  318.     HBITMAP MakeBitmap(HDC hdc = NULL);
  319.     HANDLE    CopyToHandle();
  320.     bool    CreateFromHANDLE(HANDLE hMem);        //Windows objects (clipboard)
  321.     bool    CreateFromHBITMAP(HBITMAP hbmp, HPALETTE hpal=0);    //Windows resource
  322.     bool    CreateFromHICON(HICON hico);
  323.     long    Draw(HDC hdc, long x=0, long y=0, long cx = -1, long cy = -1, RECT* pClipRect = 0);
  324.     long    Draw(HDC hdc, const RECT& rect, RECT* pClipRect=NULL) { return Draw(hdc, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, pClipRect); }
  325.     long    Stretch(HDC hdc, long xoffset, long yoffset, long xsize, long ysize, DWORD dwRop = SRCCOPY);
  326.     long    Stretch(HDC hdc, const RECT& rect, DWORD dwRop = SRCCOPY) { return Stretch(hdc, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, dwRop); }
  327.     long    Tile(HDC hdc, RECT *rc);
  328.     long    Draw2(HDC hdc, long x=0, long y=0, long cx = -1, long cy = -1);
  329.     long    Draw2(HDC hdc, const RECT& rect) { return Draw2(hdc, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top); }
  330.     //long    DrawString(HDC hdc, long x, long y, const char* text, RGBQUAD color, const char* font, long lSize=0, long lWeight=400, BYTE bItalic=0, BYTE bUnderline=0, bool bSetAlpha=false);
  331.     long    DrawString(HDC hdc, long x, long y, const TCHAR* text, RGBQUAD color, const TCHAR* font, long lSize=0, long lWeight=400, BYTE bItalic=0, BYTE bUnderline=0, bool bSetAlpha=false);
  332.     // <VATI> extensions
  333.     long    DrawStringEx(HDC hdc, long x, long y, CXTEXTINFO *pTextType, bool bSetAlpha=false );
  334.     void    InitTextInfo( CXTEXTINFO *txt );
  335. #endif //CXIMAGE_SUPPORT_WINDOWS
  336.  
  337.     // file operations
  338. #if CXIMAGE_SUPPORT_DECODE
  339. #ifdef WIN32
  340.     //bool Load(LPCWSTR filename, DWORD imagetype=0);
  341.     bool LoadResource(HRSRC hRes, DWORD imagetype, HMODULE hModule=NULL);
  342. #endif
  343.     // For UNICODE support: char -> TCHAR
  344.     bool Load(const TCHAR* filename, DWORD imagetype=0);
  345.     //bool Load(const char * filename, DWORD imagetype=0);
  346.     bool Decode(FILE * hFile, DWORD imagetype);
  347.     bool Decode(CxFile * hFile, DWORD imagetype);
  348.     bool Decode(BYTE * buffer, DWORD size, DWORD imagetype);
  349. #endif //CXIMAGE_SUPPORT_DECODE
  350.  
  351. #if CXIMAGE_SUPPORT_ENCODE
  352. protected:
  353.     bool EncodeSafeCheck(CxFile *hFile);
  354. public:
  355. #ifdef WIN32
  356.     //bool Save(LPCWSTR filename, DWORD imagetype=0);
  357. #endif
  358.     // For UNICODE support: char -> TCHAR
  359.     bool Save(const TCHAR* filename, DWORD imagetype=0);
  360.     //bool Save(const char * filename, DWORD imagetype=0);
  361.     bool Encode(FILE * hFile, DWORD imagetype);
  362.     bool Encode(CxFile * hFile, DWORD imagetype);
  363.     bool Encode(CxFile * hFile, CxImage ** pImages, int pagecount, DWORD imagetype);
  364.     bool Encode(FILE *hFile, CxImage ** pImages, int pagecount, DWORD imagetype);
  365.     bool Encode(BYTE * &buffer, long &size, DWORD imagetype);
  366. #endif //CXIMAGE_SUPPORT_ENCODE
  367.  
  368.     //misc.
  369.     bool IsValid() const {return pDib!=0;}
  370.     bool IsEnabled() const {return info.bEnabled;}
  371.     void Enable(bool enable=true){info.bEnabled=enable;}
  372.  
  373.     // frame operations
  374.     long GetNumFrames() const {return info.nNumFrames;}
  375.     long GetFrame() const {return info.nFrame;}
  376.     void SetFrame(long nFrame) {info.nFrame=nFrame;}
  377.  
  378. #if CXIMAGE_SUPPORT_BASICTRANSFORMATIONS
  379.     bool GrayScale();
  380.     bool Flip();
  381.     bool Mirror();
  382.     bool Negative();
  383.     bool RotateLeft(CxImage* iDst = NULL);
  384.     bool RotateRight(CxImage* iDst = NULL);
  385. #endif //CXIMAGE_SUPPORT_BASICTRANSFORMATIONS
  386.  
  387. #if CXIMAGE_SUPPORT_TRANSFORMATION
  388.     // image operations
  389.     bool Rotate(float angle, CxImage* iDst = NULL);
  390.     bool Rotate180(CxImage* iDst = NULL);
  391.     bool Resample(long newx, long newy, int mode = 1, CxImage* iDst = NULL);
  392.     bool DecreaseBpp(DWORD nbit, bool errordiffusion, RGBQUAD* ppal = 0, DWORD clrimportant = 0);
  393.     bool IncreaseBpp(DWORD nbit);
  394.     bool Dither(long method = 0);
  395.     bool Crop(long left, long top, long right, long bottom, CxImage* iDst = NULL);
  396.     bool Crop(const RECT& rect, CxImage* iDst = NULL) { return Crop(rect.left, rect.top, rect.right, rect.bottom, iDst); }
  397.     bool CropRotatedRectangle( long topx, long topy, long width, long height, float angle, CxImage* iDst = NULL);
  398.     bool Skew(float xgain, float ygain, long xpivot=0, long ypivot=0);
  399.     bool Expand(long left, long top, long right, long bottom, RGBQUAD canvascolor, CxImage* iDst = 0);
  400.     bool Expand(long newx, long newy, RGBQUAD canvascolor, CxImage* iDst = 0);
  401.     bool Thumbnail(long newx, long newy, RGBQUAD canvascolor, CxImage* iDst = 0);
  402.     bool CircleTransform(int type,long rmax=0,float Koeff=1.0f);
  403.  
  404. protected:
  405.     float b3spline(float x);
  406. public:
  407. #endif //CXIMAGE_SUPPORT_TRANSFORMATION
  408.  
  409. #if CXIMAGE_SUPPORT_DSP
  410.     bool Contour();
  411.     bool HistogramStretch(long method = 0);
  412.     bool HistogramEqualize();
  413.     bool HistogramNormalize();
  414.     bool HistogramRoot();
  415.     bool HistogramLog();
  416.     long Histogram(long* red, long* green = 0, long* blue = 0, long* gray = 0, long colorspace = 0);
  417.     bool Jitter(long radius=2);
  418.     bool Repair(float radius = 0.25f, long niterations = 1, long colorspace = 0);
  419.     bool Combine(CxImage* r,CxImage* g,CxImage* b,CxImage* a, long colorspace = 0);
  420.     bool FFT2(CxImage* srcReal, CxImage* srcImag, CxImage* dstReal, CxImage* dstImag, long direction = 1, bool bForceFFT = true, bool bMagnitude = true);
  421.     bool Noise(long level);
  422.     bool Median(long Ksize=3);
  423.     bool Gamma(float gamma);
  424.     bool ShiftRGB(long r, long g, long b);
  425.     bool Threshold(BYTE level);
  426.     bool Colorize(BYTE hue, BYTE sat);
  427.     bool Light(long brightness, long contrast = 0);
  428.     float Mean();
  429.     bool Filter(long* kernel, long Ksize, long Kfactor, long Koffset);
  430.     bool Erode(long Ksize=2);
  431.     bool Dilate(long Ksize=2);
  432.     void HuePalette(float correction=1);
  433.     enum ImageOpType { OpAdd, OpAnd, OpXor, OpOr, OpMask, OpSrcCopy, OpDstCopy, OpSub, OpSrcBlend };
  434.     void Mix(CxImage & imgsrc2, ImageOpType op, long lXOffset = 0, long lYOffset = 0);
  435.     void MixFrom(CxImage & imagesrc2, long lXOffset, long lYOffset);
  436.     bool UnsharpMask(float radius = 5.0, float amount = 0.5, int threshold = 0);
  437.     bool Lut(BYTE* pLut);
  438.     bool Lut(BYTE* pLutR, BYTE* pLutG, BYTE* pLutB, BYTE* pLutA = 0);
  439.  
  440. protected:
  441.     bool IsPowerof2(long x);
  442.     bool FFT(int dir,int m,double *x,double *y);
  443.     bool DFT(int dir,long m,double *x1,double *y1,double *x2,double *y2);
  444.     bool RepairChannel(CxImage *ch, float radius);
  445.     // <nipper>
  446.     int gen_convolve_matrix (float radius, float **cmatrix_p);
  447.     float* gen_lookup_table (float *cmatrix, int cmatrix_length);
  448.     void blur_line (float *ctable, float *cmatrix, int cmatrix_length, BYTE* cur_col, BYTE* dest_col, int y, long bytes);
  449. public:
  450.     //color conversion utilities
  451.     bool SplitRGB(CxImage* r,CxImage* g,CxImage* b);
  452.     bool SplitYUV(CxImage* y,CxImage* u,CxImage* v);
  453.     bool SplitHSL(CxImage* h,CxImage* s,CxImage* l);
  454.     bool SplitYIQ(CxImage* y,CxImage* i,CxImage* q);
  455.     bool SplitXYZ(CxImage* x,CxImage* y,CxImage* z);
  456.     bool SplitCMYK(CxImage* c,CxImage* m,CxImage* y,CxImage* k);
  457.     RGBQUAD HSLtoRGB(COLORREF cHSLColor);
  458.     RGBQUAD RGBtoHSL(RGBQUAD lRGBColor);
  459.     RGBQUAD HSLtoRGB(RGBQUAD lHSLColor);
  460.     RGBQUAD YUVtoRGB(RGBQUAD lYUVColor);
  461.     RGBQUAD RGBtoYUV(RGBQUAD lRGBColor);
  462.     RGBQUAD YIQtoRGB(RGBQUAD lYIQColor);
  463.     RGBQUAD RGBtoYIQ(RGBQUAD lRGBColor);
  464.     RGBQUAD XYZtoRGB(RGBQUAD lXYZColor);
  465.     RGBQUAD RGBtoXYZ(RGBQUAD lRGBColor);
  466. #endif //CXIMAGE_SUPPORT_DSP
  467.     RGBQUAD RGBtoRGBQUAD(COLORREF cr);
  468.     COLORREF RGBQUADtoRGB (RGBQUAD c);
  469.  
  470. #if CXIMAGE_SUPPORT_SELECTION
  471.     //selection
  472.     bool SelectionClear();
  473.     bool SelectionCreate();
  474.     bool SelectionDelete();
  475.     bool SelectionInvert();
  476.     bool SelectionAddRect(RECT r);
  477.     bool SelectionAddEllipse(RECT r);
  478.     bool SelectionAddPolygon(POINT *points, long npoints);
  479.     bool SelectionAddColor(RGBQUAD c);
  480.     bool SelectionAddPixel(int x, int y);
  481.     bool SelectionCopy(CxImage &from);
  482.     bool SelectionIsInside(long x, long y);
  483.     bool SelectionIsValid(){return pSelection!=0;}
  484.     void SelectionGetBox(RECT& r){memcpy(&r,&info.rSelectionBox,sizeof(RECT));}
  485.     bool SelectionToHRGN(HRGN& region);
  486. #endif //CXIMAGE_SUPPORT_SELECTION
  487.  
  488. #if CXIMAGE_SUPPORT_ALPHA
  489.     //Alpha
  490.     void AlphaClear();
  491.     void AlphaCreate();
  492.     void AlphaDelete();
  493.     void AlphaInvert();
  494.     bool AlphaMirror();
  495.     bool AlphaFlip();
  496.     bool AlphaCopy(CxImage &from);
  497.     bool AlphaSplit(CxImage *dest);
  498.     void AlphaStrip();
  499.     void AlphaSet(BYTE level);
  500.     bool AlphaSet(CxImage &from);
  501.     void AlphaSet(long x,long y,BYTE level);
  502.     BYTE AlphaGet(long x,long y);
  503.     BYTE AlphaGetMax() const {return info.nAlphaMax;}
  504.     void AlphaSetMax(BYTE nAlphaMax) {info.nAlphaMax=nAlphaMax;}
  505.     bool AlphaIsValid(){return pAlpha!=0;}
  506.     BYTE* AlphaGetBits() const {return pAlpha;}
  507.  
  508.     void AlphaPaletteClear();
  509.     void AlphaPaletteEnable(bool enable=true){info.bAlphaPaletteEnabled=enable;}
  510.     bool AlphaPaletteIsEnabled(){return info.bAlphaPaletteEnabled;}
  511.     bool AlphaPaletteIsValid();
  512.     bool AlphaPaletteSplit(CxImage *dest);
  513. #endif //CXIMAGE_SUPPORT_ALPHA
  514.  
  515. #if CXIMAGE_SUPPORT_LAYERS
  516.     bool LayerCreate(long position = -1);
  517.     bool LayerDelete(long position = -1);
  518.     void LayerDeleteAll();
  519.     CxImage* GetLayer(long position);
  520.     CxImage* GetParent() const {return info.pParent;}
  521.     long GetNumLayers() const {return info.nNumLayers;}
  522. #endif //CXIMAGE_SUPPORT_LAYERS
  523.  
  524. protected:
  525.     void Startup(DWORD imagetype = 0);
  526.     void CopyInfo(const CxImage &src);
  527.     void Ghost(CxImage *src);
  528.     void RGBtoBGR(BYTE *buffer, int length);
  529.     float HueToRGB(float n1,float n2, float hue);
  530.     void Bitfield2RGB(BYTE *src, WORD redmask, WORD greenmask, WORD bluemask, BYTE bpp);
  531.     static int CompareColors(const void *elem1, const void *elem2);
  532.  
  533.     void*                pDib; //contains the header, the palette, the pixels
  534.     BITMAPINFOHEADER    head; //standard header
  535.     CXIMAGEINFO            info; //extended information
  536.     BYTE*                pSelection;    //selected region
  537.     BYTE*                pAlpha; //alpha channel
  538.     CxImage**            pLayers; //generic layers
  539. };
  540. ////////////////////////////////////////////////////////////////////////////
  541.  
  542. #define    CXIMAGE_MAX_MEMORY 256000000
  543.  
  544. #define CXIMAGE_ERR_NOFILE "null file handler"
  545. #define CXIMAGE_ERR_NOIMAGE "null image!!!"
  546. ////////////////////////////////////////////////////////////////////////////
  547. #endif // !defined(__CXIMAGE_H)
  548.