FreeImagePlus  - FreeImage 3.15.3
FreeImagePlus.h
00001 // ==========================================================
00002 // FreeImagePlus 3
00003 //
00004 // Design and implementation by
00005 // - Hervé Drolon (drolon@infonie.fr)
00006 //
00007 // This file is part of FreeImage 3
00008 //
00009 // COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY
00010 // OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES
00011 // THAT THE COVERED CODE IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE
00012 // OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED
00013 // CODE IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT
00014 // THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY
00015 // SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL
00016 // PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER
00017 // THIS DISCLAIMER.
00018 //
00019 // Use at your own risk!
00020 // ==========================================================
00021 
00022 #ifndef FREEIMAGEPLUS_H
00023 #define FREEIMAGEPLUS_H
00024 
00025 #ifdef _WIN32
00026 #include <windows.h>
00027 #endif // _WIN32
00028 #include "FreeImage.h"
00029 
00030 
00031 // Compiler options ---------------------------------------------------------
00032 
00033 #if defined(FREEIMAGE_LIB)
00034         #define FIP_API
00035         #define FIP_CALLCONV
00036 #else
00037         #if defined(_WIN32) || defined(__WIN32__)
00038                 #define WIN32_LEAN_AND_MEAN
00039                 #define FIP_CALLCONV __stdcall
00040                 // The following ifdef block is the standard way of creating macros which make exporting 
00041                 // from a DLL simpler. All files within this DLL are compiled with the FIP_EXPORTS
00042                 // symbol defined on the command line. this symbol should not be defined on any project
00043                 // that uses this DLL. This way any other project whose source files include this file see 
00044                 // FIP_API functions as being imported from a DLL, wheras this DLL sees symbols
00045                 // defined with this macro as being exported.
00046                 #ifdef FIP_EXPORTS
00047                         #define FIP_API __declspec(dllexport)
00048                 #else
00049                         #define FIP_API __declspec(dllimport)
00050                 #endif // FIP_EXPORTS
00051         #else
00052                 // try the gcc visibility support (see http://gcc.gnu.org/wiki/Visibility)
00053                 #if defined(__GNUC__) && ((__GNUC__ >= 4) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4))
00054                         #ifndef GCC_HASCLASSVISIBILITY
00055                                 #define GCC_HASCLASSVISIBILITY
00056                         #endif
00057                 #endif  
00058                 #define FIP_CALLCONV
00059                 #if defined(GCC_HASCLASSVISIBILITY)
00060                         #define FIP_API __attribute__ ((visibility("default")))
00061                 #else
00062                         #define FIP_API
00063                 #endif
00064         #endif // WIN32 / !WIN32
00065 #endif // FREEIMAGE_LIB
00066 
00068 
00069 // ----------------------------------------------------------
00070 
00076 class FIP_API fipObject
00077 {
00078 public:
00080         virtual ~fipObject(){};
00081         
00084 
00085         virtual BOOL isValid() const = 0;
00087 };
00088 
00089 // ----------------------------------------------------------
00090 
00091 class fipMemoryIO;
00092 class fipMultiPage;
00093 class fipTag;
00094 
00103 class FIP_API fipImage : public fipObject
00104 {
00105 protected:
00107         FIBITMAP *_dib;
00109         FREE_IMAGE_FORMAT _fif;
00111         mutable BOOL _bHasChanged;
00112 
00113 public:
00114         friend class fipMultiPage;
00115 
00116 public:
00117 
00124         fipImage(FREE_IMAGE_TYPE image_type = FIT_BITMAP, unsigned width = 0, unsigned height = 0, unsigned bpp = 0);
00126         virtual ~fipImage();
00131         BOOL setSize(FREE_IMAGE_TYPE image_type, unsigned width, unsigned height, unsigned bpp, unsigned red_mask = 0, unsigned green_mask = 0, unsigned blue_mask = 0);
00133         virtual void clear();
00135 
00142         fipImage(const fipImage& src);
00147         fipImage& operator=(const fipImage& src);
00153         fipImage& operator=(FIBITMAP *dib);
00154 
00155 
00168         BOOL copySubImage(fipImage& dst, int left, int top, int right, int bottom) const;
00169 
00183         BOOL pasteSubImage(fipImage& src, int left, int top, int alpha = 256);
00184 
00195         BOOL crop(int left, int top, int right, int bottom);
00196 
00198 
00208         static FREE_IMAGE_FORMAT identifyFIF(const char* lpszPathName);
00209 
00214         static FREE_IMAGE_FORMAT identifyFIFU(const wchar_t* lpszPathName);
00215 
00223         static FREE_IMAGE_FORMAT identifyFIFFromHandle(FreeImageIO *io, fi_handle handle);
00224 
00231         static FREE_IMAGE_FORMAT identifyFIFFromMemory(FIMEMORY *hmem);
00232 
00234 
00235 
00247         BOOL load(const char* lpszPathName, int flag = 0);
00248 
00253         BOOL loadU(const wchar_t* lpszPathName, int flag = 0);
00254 
00263         BOOL loadFromHandle(FreeImageIO *io, fi_handle handle, int flag = 0);
00264 
00272         BOOL loadFromMemory(fipMemoryIO& memIO, int flag = 0);
00273 
00281         BOOL save(const char* lpszPathName, int flag = 0) const;
00282 
00287         BOOL saveU(const wchar_t* lpszPathName, int flag = 0) const;
00288 
00298         BOOL saveToHandle(FREE_IMAGE_FORMAT fif, FreeImageIO *io, fi_handle handle, int flag = 0) const;
00299 
00308         BOOL saveToMemory(FREE_IMAGE_FORMAT fif, fipMemoryIO& memIO, int flag = 0) const;
00309 
00311 
00316 
00321         FREE_IMAGE_TYPE getImageType() const;
00322 
00327         unsigned getWidth() const;
00328         
00333         unsigned getHeight() const;
00334         
00339         unsigned getScanWidth() const;
00340 
00353         operator FIBITMAP*() { 
00354                 return _dib; 
00355         }
00356 
00358         BOOL isValid() const;
00359 
00364         BITMAPINFO* getInfo() const;
00365 
00370     BITMAPINFOHEADER* getInfoHeader() const;
00371 
00377         LONG getImageSize() const;
00378         
00384         unsigned getBitsPerPixel() const;
00385 
00391         unsigned getLine() const;
00392 
00397         double getHorizontalResolution() const;
00398         
00403         double getVerticalResolution() const;
00404 
00409         void setHorizontalResolution(double value);
00410         
00415         void setVerticalResolution(double value);
00416 
00418 
00425         RGBQUAD* getPalette() const;
00426         
00431         unsigned getPaletteSize() const;
00432 
00437         unsigned getColorsUsed() const;
00438 
00443         FREE_IMAGE_COLOR_TYPE getColorType() const;
00444 
00449         BOOL isGrayscale() const;
00451 
00454 
00460         BOOL getThumbnail(fipImage& image) const;
00461 
00467         BOOL setThumbnail(const fipImage& image);
00468 
00474         BOOL hasThumbnail() const;
00475 
00481         BOOL clearThumbnail();
00482 
00484 
00487 
00496         BYTE* accessPixels() const;
00497 
00503         BYTE* getScanLine(unsigned scanline) const;
00504 
00513         BOOL getPixelIndex(unsigned x, unsigned y, BYTE *value) const;
00514 
00523         BOOL getPixelColor(unsigned x, unsigned y, RGBQUAD *value) const;
00524 
00533         BOOL setPixelIndex(unsigned x, unsigned y, BYTE *value);
00534 
00543         BOOL setPixelColor(unsigned x, unsigned y, RGBQUAD *value);
00544 
00546 
00558         BOOL convertToType(FREE_IMAGE_TYPE image_type, BOOL scale_linear = TRUE);
00559 
00566         BOOL threshold(BYTE T);
00567         
00574         BOOL dither(FREE_IMAGE_DITHER algorithm);
00575 
00581         BOOL convertTo4Bits();
00582 
00588         BOOL convertTo8Bits();
00589 
00596         BOOL convertToGrayscale();
00597         
00605         BOOL colorQuantize(FREE_IMAGE_QUANTIZE algorithm);
00606 
00612         BOOL convertTo16Bits555();
00613         
00619         BOOL convertTo16Bits565();
00620         
00626         BOOL convertTo24Bits();
00627         
00633         BOOL convertTo32Bits();
00634 
00640         BOOL convertToFloat();
00641 
00647         BOOL convertToRGBF();
00648 
00654         BOOL convertToUINT16();
00655 
00661         BOOL convertToRGB16();
00662 
00673         BOOL toneMapping(FREE_IMAGE_TMO tmo, double first_param = 0, double second_param = 0, double third_param = 1, double fourth_param = 0);
00674 
00676 
00679 
00684         BOOL isTransparent() const;
00685 
00691         unsigned getTransparencyCount() const;
00692 
00698         BYTE* getTransparencyTable() const;
00699 
00704         void setTransparencyTable(BYTE *table, int count);
00705 
00710         BOOL hasFileBkColor() const;
00711 
00720         BOOL getFileBkColor(RGBQUAD *bkcolor) const;
00721 
00730         BOOL setFileBkColor(RGBQUAD *bkcolor);
00732 
00741         BOOL getChannel(fipImage& image, FREE_IMAGE_COLOR_CHANNEL channel) const;
00742 
00750         BOOL setChannel(fipImage& image, FREE_IMAGE_COLOR_CHANNEL channel);
00751 
00760         BOOL splitChannels(fipImage& RedChannel, fipImage& GreenChannel, fipImage& BlueChannel);
00761 
00769         BOOL combineChannels(fipImage& red, fipImage& green, fipImage& blue);
00771 
00785         BOOL rotateEx(double angle, double x_shift, double y_shift, double x_origin, double y_origin, BOOL use_mask);
00786 
00794         BOOL rotate(double angle, const void *bkcolor = NULL);
00795 
00800         BOOL flipHorizontal();
00801 
00806         BOOL flipVertical();
00808 
00816         BOOL invert();
00817         
00831         BOOL adjustCurve(BYTE *LUT, FREE_IMAGE_COLOR_CHANNEL channel);
00832 
00839         BOOL adjustGamma(double gamma);
00840 
00848         BOOL adjustBrightness(double percentage);
00849 
00857         BOOL adjustContrast(double percentage);
00858 
00869         BOOL adjustBrightnessContrastGamma(double brightness, double contrast, double gamma);
00870 
00881         BOOL getHistogram(DWORD *histo, FREE_IMAGE_COLOR_CHANNEL channel = FICC_BLACK) const;
00883 
00886 
00895         BOOL rescale(unsigned new_width, unsigned new_height, FREE_IMAGE_FILTER filter);
00896 
00904         BOOL makeThumbnail(unsigned max_size, BOOL convert = TRUE);
00906 
00916         void setModified(BOOL bStatus = TRUE) {
00917                 _bHasChanged = bStatus;
00918         }
00919 
00925         BOOL isModified() {
00926                 return _bHasChanged;
00927         }
00929 
00937         unsigned getMetadataCount(FREE_IMAGE_MDMODEL model) const;
00946         BOOL getMetadata(FREE_IMAGE_MDMODEL model, const char *key, fipTag& tag) const;
00966         BOOL setMetadata(FREE_IMAGE_MDMODEL model, const char *key, fipTag& tag);
00968 
00969 
00970   protected:
00973           BOOL replace(FIBITMAP *new_dib);
00975 
00976 };
00977 
00978 // ----------------------------------------------------------
00979 
00991 #ifdef _WIN32
00992 
00993 class FIP_API fipWinImage : public fipImage
00994 {
00995 public:
00998 
00999         fipWinImage(FREE_IMAGE_TYPE image_type = FIT_BITMAP, unsigned width = 0, unsigned height = 0, unsigned bpp = 0);
01000 
01002         virtual ~fipWinImage();
01003 
01005         virtual void clear();
01006 
01008         BOOL isValid() const;
01010 
01013 
01020         fipWinImage& operator=(const fipImage& src);
01021 
01028         fipWinImage& operator=(const fipWinImage& src);
01029 
01036         HANDLE copyToHandle() const;
01037 
01044         BOOL copyFromHandle(HANDLE hMem);
01045 
01050         BOOL copyFromBitmap(HBITMAP hbmp);
01052 
01061         BOOL copyToClipboard(HWND hWndNewOwner) const;
01062 
01067         BOOL pasteFromClipboard();
01069 
01077         BOOL captureWindow(HWND hWndApplicationWindow, HWND hWndSelectedWindow);
01079 
01080 
01083 
01092         void draw(HDC hDC, RECT& rcDest) const {
01093                 drawEx(hDC, rcDest, FALSE, NULL, NULL);
01094         }
01095 
01113         void drawEx(HDC hDC, RECT& rcDest, BOOL useFileBkg = FALSE, RGBQUAD *appBkColor = NULL, FIBITMAP *bg = NULL) const;
01114 
01125         void setToneMappingOperator(FREE_IMAGE_TMO tmo, double first_param = 0, double second_param = 0, double third_param = 1, double fourth_param = 0);
01126 
01136         void getToneMappingOperator(FREE_IMAGE_TMO *tmo, double *first_param, double *second_param, double *third_param, double *fourth_param) const;
01137 
01139 
01140 protected:
01142         mutable FIBITMAP *_display_dib;
01144         mutable BOOL _bDeleteMe;
01146         FREE_IMAGE_TMO _tmo;
01148         double _tmo_param_1;
01150         double _tmo_param_2;
01152         double _tmo_param_3;
01154         double _tmo_param_4;
01155 };
01156 
01157 #endif // _WIN32
01158 
01159 // ----------------------------------------------------------
01160 
01167 class FIP_API fipMemoryIO : public fipObject
01168 {
01169 protected:
01171         FIMEMORY *_hmem;
01172 
01173 public :
01183     fipMemoryIO(BYTE *data = NULL, DWORD size_in_bytes = 0);
01184 
01189         virtual ~fipMemoryIO();
01190 
01195         void close();
01196 
01199         BOOL isValid() const;
01200 
01204         FREE_IMAGE_FORMAT getFileType() const;
01205 
01210         operator FIMEMORY*() { 
01211                 return _hmem; 
01212         }
01213 
01223         FIBITMAP* load(FREE_IMAGE_FORMAT fif, int flags = 0) const;
01231         FIMULTIBITMAP* loadMultiPage(FREE_IMAGE_FORMAT fif, int flags = 0) const;
01240         BOOL save(FREE_IMAGE_FORMAT fif, FIBITMAP *dib, int flags = 0);
01249         BOOL saveMultiPage(FREE_IMAGE_FORMAT fif, FIMULTIBITMAP *bitmap, int flags = 0);
01258         unsigned read(void *buffer, unsigned size, unsigned count) const;
01267         unsigned write(const void *buffer, unsigned size, unsigned count);
01272         long tell() const;
01277         BOOL seek(long offset, int origin);
01284         BOOL acquire(BYTE **data, DWORD *size_in_bytes);
01286 
01287 private:
01289         fipMemoryIO(const fipMemoryIO& src);
01291         fipMemoryIO& operator=(const fipMemoryIO& src);
01292 
01293 };
01294 
01295 // ----------------------------------------------------------
01296 
01302 class FIP_API fipMultiPage : public fipObject 
01303 {
01304 protected:
01306         FIMULTIBITMAP *_mpage;
01308         BOOL _bMemoryCache;
01309 
01310 public:
01315         fipMultiPage(BOOL keep_cache_in_memory = FALSE);
01316 
01321         virtual ~fipMultiPage();
01322 
01324         BOOL isValid() const;
01325 
01330         operator FIMULTIBITMAP*() { 
01331                 return _mpage; 
01332         }
01333 
01343         BOOL open(const char* lpszPathName, BOOL create_new, BOOL read_only, int flags = 0);
01344 
01352         BOOL open(fipMemoryIO& memIO, int flags = 0);
01353 
01362         BOOL open(FreeImageIO *io, fi_handle handle, int flags = 0);
01363 
01370         BOOL close(int flags = 0);
01371 
01381         BOOL saveToHandle(FREE_IMAGE_FORMAT fif, FreeImageIO *io, fi_handle handle, int flags = 0) const;
01382 
01391         BOOL saveToMemory(FREE_IMAGE_FORMAT fif, fipMemoryIO& memIO, int flags = 0) const;
01392 
01397         int getPageCount() const;
01398 
01404         void appendPage(fipImage& image);
01405 
01412         void insertPage(int page, fipImage& image);
01413 
01419         void deletePage(int page);
01420 
01428         BOOL movePage(int target, int source);
01429 
01447         FIBITMAP* lockPage(int page);
01448 
01455         void unlockPage(fipImage& image, BOOL changed);
01456 
01465         BOOL getLockedPageNumbers(int *pages, int *count) const;
01466 };
01467 
01468 // ----------------------------------------------------------
01469 
01475 class FIP_API fipTag : public fipObject
01476 {
01477 protected:
01479         FITAG *_tag;
01480 
01481 public:
01488         fipTag();
01493         virtual ~fipTag();
01502         BOOL setKeyValue(const char *key, const char *value);
01503 
01505 
01512         fipTag(const fipTag& tag);
01517         fipTag& operator=(const fipTag& tag);
01523         fipTag& operator=(FITAG *tag);
01525 
01531         operator FITAG*() { 
01532                 return _tag; 
01533         }
01534 
01536         BOOL isValid() const;
01537 
01544         const char *getKey() const;
01549         const char *getDescription() const;
01554         WORD getID() const;
01559         FREE_IMAGE_MDTYPE getType() const;
01564         DWORD getCount() const;
01569         DWORD getLength() const;
01574         const void *getValue() const;
01580         BOOL setKey(const char *key);
01586         BOOL setDescription(const char *description);
01592         BOOL setID(WORD id);
01598         BOOL setType(FREE_IMAGE_MDTYPE type);
01604         BOOL setCount(DWORD count);
01610         BOOL setLength(DWORD length);
01616         BOOL setValue(const void *value);
01617 
01619 
01625         const char* toString(FREE_IMAGE_MDMODEL model, char *Make = NULL) const;
01626 
01627 };
01628 
01655 class FIP_API fipMetadataFind : public fipObject
01656 {
01657 protected:
01659         FIMETADATA *_mdhandle;
01660 
01661 public:
01663         BOOL isValid() const;
01664 
01666         fipMetadataFind();
01671         virtual ~fipMetadataFind();
01681         BOOL findFirstMetadata(FREE_IMAGE_MDMODEL model, fipImage& image, fipTag& tag);
01689         BOOL findNextMetadata(fipTag& tag);
01690 
01691 };
01692 
01693 #endif  // FREEIMAGEPLUS_H