00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
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
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