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:
00081 
00082         virtual BOOL isValid() const = 0;
00084 };
00085 
00086 // ----------------------------------------------------------
00087 
00088 class fipMemoryIO;
00089 class fipMultiPage;
00090 class fipTag;
00091 
00100 class FIP_API fipImage : public fipObject
00101 {
00102 protected:
00104         FIBITMAP *_dib;
00106         mutable BOOL _bHasChanged;
00107 
00108 public:
00109         friend class fipMultiPage;
00110 
00111 public:
00112 
00119         fipImage(FREE_IMAGE_TYPE image_type = FIT_BITMAP, WORD width = 0, WORD height = 0, WORD bpp = 0);
00121         ~fipImage();
00126         BOOL setSize(FREE_IMAGE_TYPE image_type, WORD width, WORD height, WORD bpp, unsigned red_mask = 0, unsigned green_mask = 0, unsigned blue_mask = 0);
00128         virtual void clear();
00130 
00137         fipImage(const fipImage& src);
00142         fipImage& operator=(const fipImage& src);
00148         fipImage& operator=(FIBITMAP *dib);
00149 
00150 
00163         BOOL copySubImage(fipImage& dst, int left, int top, int right, int bottom) const;
00164 
00178         BOOL pasteSubImage(fipImage& src, int left, int top, int alpha = 256);
00179 
00190         BOOL crop(int left, int top, int right, int bottom);
00191 
00192 
00194 
00206         BOOL load(const char* lpszPathName, int flag = 0);
00207 
00212         BOOL loadU(const wchar_t* lpszPathName, int flag = 0);
00213 
00222         BOOL loadFromHandle(FreeImageIO *io, fi_handle handle, int flag = 0);
00223 
00231         BOOL loadFromMemory(fipMemoryIO& memIO, int flag = 0);
00232 
00240         BOOL save(const char* lpszPathName, int flag = 0) const;
00241 
00246         BOOL saveU(const wchar_t* lpszPathName, int flag = 0) const;
00247 
00257         BOOL saveToHandle(FREE_IMAGE_FORMAT fif, FreeImageIO *io, fi_handle handle, int flag = 0) const;
00258 
00267         BOOL saveToMemory(FREE_IMAGE_FORMAT fif, fipMemoryIO& memIO, int flag = 0) const;
00268 
00270 
00275 
00280         FREE_IMAGE_TYPE getImageType() const;
00281 
00286         WORD getWidth() const;
00287         
00292         WORD getHeight() const;
00293         
00298         WORD getScanWidth() const;
00299 
00312         operator FIBITMAP*() { 
00313                 return _dib; 
00314         }
00315 
00317         BOOL isValid() const;
00318 
00323         BITMAPINFO* getInfo() const;
00324 
00329     BITMAPINFOHEADER* getInfoHeader() const;
00330 
00336         LONG getImageSize() const;
00337         
00343         WORD getBitsPerPixel() const;
00344 
00350         WORD getLine() const;
00351 
00356         double getHorizontalResolution() const;
00357         
00362         double getVerticalResolution() const;
00363 
00368         void setHorizontalResolution(double value);
00369         
00374         void setVerticalResolution(double value);
00375 
00377 
00384         RGBQUAD* getPalette() const;
00385         
00390         WORD getPaletteSize() const;
00391 
00396         WORD getColorsUsed() const;
00397 
00402         FREE_IMAGE_COLOR_TYPE getColorType() const;
00403 
00408         BOOL isGrayscale() const;
00410 
00413 
00422         BYTE* accessPixels() const;
00423 
00429         BYTE* getScanLine(WORD scanline) const;
00430 
00439         BOOL getPixelIndex(unsigned x, unsigned y, BYTE *value) const;
00440 
00449         BOOL getPixelColor(unsigned x, unsigned y, RGBQUAD *value) const;
00450 
00459         BOOL setPixelIndex(unsigned x, unsigned y, BYTE *value);
00460 
00469         BOOL setPixelColor(unsigned x, unsigned y, RGBQUAD *value);
00470 
00472 
00484         BOOL convertToType(FREE_IMAGE_TYPE image_type, BOOL scale_linear = TRUE);
00485 
00492         BOOL threshold(BYTE T);
00493         
00500         BOOL dither(FREE_IMAGE_DITHER algorithm);
00501 
00507         BOOL convertTo4Bits();
00508 
00514         BOOL convertTo8Bits();
00515 
00522         BOOL convertToGrayscale();
00523         
00531         BOOL colorQuantize(FREE_IMAGE_QUANTIZE algorithm);
00532 
00538         BOOL convertTo16Bits555();
00539         
00545         BOOL convertTo16Bits565();
00546         
00552         BOOL convertTo24Bits();
00553         
00559         BOOL convertTo32Bits();
00560 
00566         BOOL convertToRGBF();
00567 
00576         BOOL toneMapping(FREE_IMAGE_TMO tmo, double first_param = 0, double second_param = 0);
00577 
00579 
00582 
00587         BOOL isTransparent() const;
00588 
00594         unsigned getTransparencyCount() const;
00595 
00601         BYTE* getTransparencyTable() const;
00602 
00607         void setTransparencyTable(BYTE *table, int count);
00608 
00613         BOOL hasFileBkColor() const;
00614 
00623         BOOL getFileBkColor(RGBQUAD *bkcolor) const;
00624 
00633         BOOL setFileBkColor(RGBQUAD *bkcolor);
00635 
00644         BOOL getChannel(fipImage& image, FREE_IMAGE_COLOR_CHANNEL channel) const;
00645 
00653         BOOL setChannel(fipImage& image, FREE_IMAGE_COLOR_CHANNEL channel);
00654 
00663         BOOL splitChannels(fipImage& RedChannel, fipImage& GreenChannel, fipImage& BlueChannel);
00664 
00672         BOOL combineChannels(fipImage& red, fipImage& green, fipImage& blue);
00674 
00688         BOOL rotateEx(double angle, double x_shift, double y_shift, double x_origin, double y_origin, BOOL use_mask);
00689 
00696         BOOL rotate(double angle);
00697 
00702         BOOL flipHorizontal();
00703 
00708         BOOL flipVertical();
00710 
00718         BOOL invert();
00719         
00733         BOOL adjustCurve(BYTE *LUT, FREE_IMAGE_COLOR_CHANNEL channel);
00734 
00741         BOOL adjustGamma(double gamma);
00742 
00750         BOOL adjustBrightness(double percentage);
00751 
00759         BOOL adjustContrast(double percentage);
00760 
00771         BOOL getHistogram(DWORD *histo, FREE_IMAGE_COLOR_CHANNEL channel = FICC_BLACK) const;
00773 
00776 
00785         BOOL rescale(WORD new_width, WORD new_height, FREE_IMAGE_FILTER filter);
00786 
00794         BOOL makeThumbnail(WORD max_size, BOOL convert = TRUE);
00796 
00806         void setModified(BOOL bStatus = TRUE) {
00807                 _bHasChanged = bStatus;
00808         }
00809 
00815         BOOL isModified() {
00816                 return _bHasChanged;
00817         }
00819 
00827         unsigned getMetadataCount(FREE_IMAGE_MDMODEL model) const;
00836         BOOL getMetadata(FREE_IMAGE_MDMODEL model, const char *key, fipTag& tag) const;
00856         BOOL setMetadata(FREE_IMAGE_MDMODEL model, const char *key, fipTag& tag);
00858 
00859 
00860   protected:
00863           BOOL replace(FIBITMAP *new_dib);
00865 
00866 };
00867 
00868 // ----------------------------------------------------------
00869 
00881 #ifdef _WIN32
00882 
00883 class FIP_API fipWinImage : public fipImage
00884 {
00885 public:
00888 
00889         fipWinImage(FREE_IMAGE_TYPE image_type = FIT_BITMAP, WORD width = 0, WORD height = 0, WORD bpp = 0);
00890 
00892         ~fipWinImage();
00893 
00895         virtual void clear();
00896 
00898         BOOL isValid() const;
00900 
00903 
00910         fipWinImage& operator=(const fipImage& src);
00911 
00918         fipWinImage& operator=(const fipWinImage& src);
00919 
00926         HANDLE copyToHandle() const;
00927 
00934         BOOL copyFromHandle(HANDLE hMem);
00935 
00940         BOOL copyFromBitmap(HBITMAP hbmp);
00942 
00951         BOOL copyToClipboard(HWND hWndNewOwner) const;
00952 
00957         BOOL pasteFromClipboard();
00959 
00967         BOOL captureWindow(HWND hWndApplicationWindow, HWND hWndSelectedWindow);
00969 
00970 
00973 
00982         void draw(HDC hDC, RECT& rcDest) const {
00983                 drawEx(hDC, rcDest, FALSE, NULL, NULL);
00984         }
00985 
01003         void drawEx(HDC hDC, RECT& rcDest, BOOL useFileBkg = FALSE, RGBQUAD *appBkColor = NULL, FIBITMAP *bg = NULL) const;
01004 
01013         void setToneMappingOperator(FREE_IMAGE_TMO tmo, double first_param = 0, double second_param = 0);
01014 
01022         void getToneMappingOperator(FREE_IMAGE_TMO *tmo, double *first_param, double *second_param) const;
01023 
01025 
01026 protected:
01028         mutable FIBITMAP *_display_dib;
01030         mutable BOOL _bDeleteMe;
01032         FREE_IMAGE_TMO _tmo;
01034         double _tmo_param_1;
01036         double _tmo_param_2;
01037 };
01038 
01039 #endif // _WIN32
01040 
01041 // ----------------------------------------------------------
01042 
01049 class FIP_API fipMemoryIO : public fipObject
01050 {
01051 protected:
01053         FIMEMORY *_hmem;
01054 
01055 public :
01065     fipMemoryIO(BYTE *data = NULL, DWORD size_in_bytes = 0);
01066 
01071         ~fipMemoryIO();
01072 
01075         BOOL isValid() const;
01076 
01080         FREE_IMAGE_FORMAT getFileType() const;
01081 
01086         operator FIMEMORY*() { 
01087                 return _hmem; 
01088         }
01089 
01099         FIBITMAP* load(FREE_IMAGE_FORMAT fif, int flags = 0) const;
01108         BOOL save(FREE_IMAGE_FORMAT fif, FIBITMAP *dib, int flags = 0);
01117         unsigned read(void *buffer, unsigned size, unsigned count) const;
01126         unsigned write(const void *buffer, unsigned size, unsigned count);
01131         long tell() const;
01136         BOOL seek(long offset, int origin);
01143         BOOL acquire(BYTE **data, DWORD *size_in_bytes);
01145 
01146 private:
01148         fipMemoryIO(const fipMemoryIO& src);
01150         fipMemoryIO& operator=(const fipMemoryIO& src);
01151 
01152 };
01153 
01154 // ----------------------------------------------------------
01155 
01161 class FIP_API fipMultiPage : public fipObject 
01162 {
01163 protected:
01165         FIMULTIBITMAP *_mpage;
01167         BOOL _bMemoryCache;
01168 
01169 public:
01174         fipMultiPage(BOOL keep_cache_in_memory = FALSE);
01175 
01180         ~fipMultiPage();
01181 
01183         BOOL isValid() const;
01184 
01194         BOOL open(const char* lpszPathName, BOOL create_new, BOOL read_only, int flags = 0);
01195 
01203         BOOL open(fipMemoryIO& memIO, int flags = 0);
01204 
01211         BOOL close(int flags = 0);
01212 
01217         int getPageCount() const;
01218 
01224         void appendPage(fipImage& image);
01225 
01232         void insertPage(int page, fipImage& image);
01233 
01239         void deletePage(int page);
01240 
01248         BOOL movePage(int target, int source);
01249 
01267         FIBITMAP* lockPage(int page);
01268 
01275         void unlockPage(fipImage& image, BOOL changed);
01276 
01285         BOOL getLockedPageNumbers(int *pages, int *count) const;
01286 };
01287 
01288 // ----------------------------------------------------------
01289 
01295 class FIP_API fipTag : public fipObject
01296 {
01297 protected:
01299         FITAG *_tag;
01300 
01301 public:
01308         fipTag();
01313         ~fipTag();
01322         BOOL setKeyValue(const char *key, const char *value);
01323 
01325 
01332         fipTag(const fipTag& tag);
01337         fipTag& operator=(const fipTag& tag);
01343         fipTag& operator=(FITAG *tag);
01345 
01351         operator FITAG*() { 
01352                 return _tag; 
01353         }
01354 
01356         BOOL isValid() const;
01357 
01364         const char *getKey() const;
01369         const char *getDescription() const;
01374         WORD getID() const;
01379         FREE_IMAGE_MDTYPE getType() const;
01384         DWORD getCount() const;
01389         DWORD getLength() const;
01394         const void *getValue() const;
01400         BOOL setKey(const char *key);
01406         BOOL setDescription(const char *description);
01412         BOOL setID(WORD id);
01418         BOOL setType(FREE_IMAGE_MDTYPE type);
01424         BOOL setCount(DWORD count);
01430         BOOL setLength(DWORD length);
01436         BOOL setValue(const void *value);
01437 
01439 
01445         const char* toString(FREE_IMAGE_MDMODEL model, char *Make = NULL) const;
01446 
01447 };
01448 
01475 class FIP_API fipMetadataFind : public fipObject
01476 {
01477 protected:
01479         FIMETADATA *_mdhandle;
01480 
01481 public:
01483         BOOL isValid() const;
01484 
01486         fipMetadataFind();
01491         ~fipMetadataFind();
01501         BOOL findFirstMetadata(FREE_IMAGE_MDMODEL model, fipImage& image, fipTag& tag);
01509         BOOL findNextMetadata(fipTag& tag);
01510 
01511 };
01512 
01513 #endif  // FREEIMAGEPLUS_H