Visualization Library

A lightweight C++ OpenGL middleware for 2D/3D graphics
[Home] [Tutorials] [All Classes] [Grouped Classes]

X:/dropbox/visualizationlibrary/src/vlCore/Image.hpp

Go to the documentation of this file.
00001 /**************************************************************************************/
00002 /*                                                                                    */
00003 /*  Visualization Library                                                             */
00004 /*  http://www.visualizationlibrary.org                                               */
00005 /*                                                                                    */
00006 /*  Copyright (c) 2005-2010, Michele Bosi                                             */
00007 /*  All rights reserved.                                                              */
00008 /*                                                                                    */
00009 /*  Redistribution and use in source and binary forms, with or without modification,  */
00010 /*  are permitted provided that the following conditions are met:                     */
00011 /*                                                                                    */
00012 /*  - Redistributions of source code must retain the above copyright notice, this     */
00013 /*  list of conditions and the following disclaimer.                                  */
00014 /*                                                                                    */
00015 /*  - Redistributions in binary form must reproduce the above copyright notice, this  */
00016 /*  list of conditions and the following disclaimer in the documentation and/or       */
00017 /*  other materials provided with the distribution.                                   */
00018 /*                                                                                    */
00019 /*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND   */
00020 /*  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED     */
00021 /*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE            */
00022 /*  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR  */
00023 /*  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES    */
00024 /*  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;      */
00025 /*  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON    */
00026 /*  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT           */
00027 /*  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS     */
00028 /*  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                      */
00029 /*                                                                                    */
00030 /**************************************************************************************/
00031 
00032 #ifndef Image_INCUDE_ONCE
00033 #define Image_INCUDE_ONCE
00034 
00035 #include <vlCore/String.hpp>
00036 #include <vlCore/Buffer.hpp>
00037 #include <vlCore/Vector4.hpp>
00038 #include <vlCore/vlnamespace.hpp>
00039 #include <vlCore/Rect.hpp>
00040 #include <vlCore/KeyValues.hpp>
00041 #include <vector>
00042 
00043 namespace vl
00044 {
00045   class VirtualFile;
00046 
00047   //------------------------------------------------------------------------------
00048   // Image
00049   //------------------------------------------------------------------------------
00054   class VLCORE_EXPORT Image: public Object
00055   {
00056     VL_INSTRUMENT_CLASS(vl::Image, Object)
00057 
00058   public:
00059     virtual ~Image();
00060 
00061     Image();
00062 
00063     Image(const String& path);
00064 
00065     Image(int x, int y, int z, int bytealign, EImageFormat format, EImageType type);
00066 
00067     Image(const Image& other);
00068 
00069     Image& operator=(const Image& other);
00070 
00071     bool isCubemap() const { return mIsCubemap; }
00072 
00073     bool isValid() const;
00074 
00075     EImageDimension dimension() const;
00076 
00077     void allocate();
00078 
00079     void allocate1D(int x, EImageFormat format, EImageType type);
00080 
00081     void allocate2D(int x, int y, int bytealign, EImageFormat format, EImageType type);
00082 
00083     void allocate3D(int x, int y, int z, int bytealign, EImageFormat format, EImageType type);
00084 
00085     void allocateCubemap(int x, int y, int bytealign, EImageFormat format, EImageType type);
00086 
00088     void reset(int x, int y, int z, int bytealign, EImageFormat format, EImageType type, bool is_cubemap);
00089 
00091     void reset();
00092 
00093     int byteAlignment() const;
00094 
00095     void setByteAlignment(int bytealign);
00096 
00097     static int bitsPerPixel(EImageType type, EImageFormat format);
00098 
00099     int bitsPerPixel() const { return bitsPerPixel(type(),format()); }
00100 
00101     int requiredMemory() const;
00102 
00103     static int requiredMemory(int x, int y, int z, int bytealign, EImageFormat format, EImageType type, bool is_cubemap);
00104 
00105     static int requiredMemory1D(int x, EImageFormat format, EImageType type) { return requiredMemory(x, 0, 0, 1, format, type, false); } 
00106 
00107     static int requiredMemory2D(int x, int y, int bytealign, EImageFormat format, EImageType type) { return requiredMemory(x, y, 0, bytealign, format, type, false); } 
00108 
00109     static int requiredMemory3D(int x, int y, int z, int bytealign, EImageFormat format, EImageType type) { return requiredMemory(x, y, z, bytealign, format, type, false); } 
00110 
00111     static int requiredMemoryCubemap(int x, int y, int bytealign, EImageFormat format, EImageType type) { return requiredMemory(x, y, 0, bytealign, format, type, true); } 
00112 
00113     String print() const;
00114 
00115     String printType() const;
00116 
00117     String printFormat() const;
00118     
00119     void setWidth(int x) { mWidth = x; updatePitch(); }
00120     
00121     void setHeight(int y) { mHeight = y; }
00122     
00123     void setDepth(int z) { mDepth = z; }
00124     
00125     void setFormat(EImageFormat format) { mFormat = format; updatePitch(); }
00126     
00127     void setType(EImageType type) { mType=type; updatePitch(); }
00128 
00130     bool hasAlpha() const { return mHasAlpha; }
00131     
00133     void setHasAlpha(bool has_alpha) { mHasAlpha = has_alpha; }
00134 
00136     int alphaBits() const;
00137 
00139     bool isNormalMap() const { return mIsNormalMap; }
00140 
00142     void setIsNormalMap(bool is_normalmap) { mIsNormalMap = is_normalmap; }
00143 
00146     const KeyValues* tags() const { return mTags.get(); }
00147 
00150     KeyValues* tags() { return mTags.get(); }
00151 
00153     void setTags(KeyValues* tags) { mTags = tags; }
00154 
00156     void setImageBuffer(Buffer* buffer) { mPixels = buffer; }
00157 
00159     Buffer* imageBuffer() { return mPixels.get(); }
00160 
00162     const Buffer* imageBuffer() const { return mPixels.get(); }
00163 
00164     const unsigned char* pixels() const { if (mPixels->bytesUsed()) return mPixels->ptr(); else return NULL; }
00165     
00166     unsigned char* pixels() { if (mPixels->bytesUsed()) return mPixels->ptr(); else return NULL; }
00167     
00168     bool empty() { return pixels() == NULL; }
00169     
00170     unsigned char* pixelsZSlice(int slice);
00171     
00172     unsigned char* pixelsXP();
00173     unsigned char* pixelsXN();
00174     unsigned char* pixelsYP();
00175     unsigned char* pixelsYN();
00176     unsigned char* pixelsZP();
00177     unsigned char* pixelsZN();
00178     
00179     const unsigned char* pixelsXP() const;
00180     const unsigned char* pixelsXN() const;
00181     const unsigned char* pixelsYP() const;
00182     const unsigned char* pixelsYN() const;
00183     const unsigned char* pixelsZP() const;
00184     const unsigned char* pixelsZN() const;
00185 
00186     void setMipmaps(const std::vector< ref<Image> >& mipmaps) { mMipmaps = mipmaps; };
00187     
00188     const std::vector< ref<Image> >& mipmaps() const { return mMipmaps; };
00189     
00190     std::vector< ref<Image> >& mipmaps() { return mMipmaps; };
00191     
00192     int width() const { return mWidth; }
00193     
00194     int height() const { return mHeight; }
00195     
00196     int depth() const { return mDepth; }
00197     
00198     int pitch() const { return mPitch; }
00199     
00200     EImageFormat format() const { return mFormat; }
00201     
00202     EImageType type() const { return mType; }
00203     
00204     int isCompressedFormat(EImageFormat fmt);
00205 
00206     void flipVertically();
00207 
00233     ref<Image> convertType(EImageType new_type) const;
00234 
00259     ref<Image> convertFormat(EImageFormat new_format) const;
00260 
00262     bool equalize();
00263 
00265     bool contrast(float black, float white);
00266 
00268     bool contrastHounsfieldAuto();
00269 
00271     bool contrastHounsfield(float center, float width, float intercept, float range);
00272 
00274     fvec4 sampleLinear(double x) const;
00275 
00277     fvec4 sampleLinear(double x, double y) const;
00278 
00280     fvec4 sampleLinear(double x, double y, double z) const;
00281 
00310     fvec4 sample(int x, int y=0, int z=0) const;
00311 
00319     ref<Image> subImage(int xstart, int ystart, int width, int height);
00320 
00325     void copySubImage(Image* img_src, RectI src, ivec2 dst);
00326 
00339     void substituteColorRGB_RGBA(unsigned int before, unsigned int after);
00340 
00351     void substituteColorRGB_RGB(unsigned int before, unsigned int after);
00352 
00353     void substituteColorGreenKey(unsigned int col0, unsigned int col1);
00354 
00356     const String& filePath() const { return mFilePath; }
00357 
00359     void setFilePath(const String& path) { mFilePath = path; }
00360 
00361   protected:
00362     void updatePitch();
00363 
00364   protected:
00365     ref<Buffer> mPixels;
00366     ref<KeyValues> mTags;
00367     String mFilePath;
00368     std::vector< ref<Image> > mMipmaps;
00369     int mWidth;
00370     int mHeight;
00371     int mDepth;
00372     int mPitch;
00373     int mByteAlign;
00374     EImageFormat mFormat;
00375     EImageType mType;
00376     bool mIsCubemap;
00377     bool mIsNormalMap;
00378     bool mHasAlpha;
00379   };
00380 
00382   ref<Image> createCubemap(const Image* xp, const Image* xn, const Image* yp, const Image* yn, const Image* zp, const Image* zn);
00383 
00385   VLCORE_EXPORT ref<Image> loadCubemap(const String& xp_file, const String& xn_file, const String& yp_file, const String& yn_file, const String& zp_file, const String& zn_file);
00386 
00391   VLCORE_EXPORT ref<Image> loadRAW(VirtualFile* file, long long file_offset, int width, int height, int depth, int bytealign, EImageFormat format, EImageType type);
00392 
00394   VLCORE_EXPORT ref<Image> loadImage(VirtualFile* file);
00395 
00397   VLCORE_EXPORT ref<Image> loadImage(const String& path);
00398 
00400   VLCORE_EXPORT bool loadImagesFromDir(const String& dir_path, const String& ext, std::vector< ref<Image> >& images);
00401 
00403   VLCORE_EXPORT ref<Image> assemble3DImage(const std::vector< ref<Image> >& images);
00404 
00406   VLCORE_EXPORT bool saveImage(Image* img, VirtualFile* file);
00407 
00409   VLCORE_EXPORT bool saveImage(Image* img, const String& path);
00410 
00412   VLCORE_EXPORT ref<Image> makeNonUniformColorSpectrum(size_t width, size_t col_count, const fvec4* colors, const float* col_pos);
00413 
00415   VLCORE_EXPORT ref<Image> makeNonUniformColorSpectrum(int width, const std::vector<fvec4>& colors, const std::vector<float>& col_pos);
00416 
00418   VLCORE_EXPORT ref<Image> makeColorSpectrum(size_t width, const std::vector<fvec4>& colors);
00419 
00421   VLCORE_EXPORT ref<Image> makeColorSpectrum(size_t width, const fvec4& c0, const fvec4& c1);
00422 
00424   VLCORE_EXPORT ref<Image> makeColorSpectrum(size_t width, const fvec4& c0, const fvec4& c1, const fvec4& c2);
00425 
00427   VLCORE_EXPORT ref<Image> makeColorSpectrum(size_t width, const fvec4& c0, const fvec4& c1, const fvec4& c2, const fvec4& c3);
00428 
00430   VLCORE_EXPORT ref<Image> makeColorSpectrum(size_t width, const fvec4& c0, const fvec4& c1, const fvec4& c2, const fvec4& c3, const fvec4& c4);
00431 }
00432 
00433 #endif

Visualization Library 2011.09.1160 Reference Documentation
Copyright 2005-2011 Michele Bosi. All rights reserved.
Updated on Thu May 2 2013 13:40:29.
Permission is granted to use this page to write and publish articles regarding Visualization Library.