Visualization Library 2.1.0

A lightweight C++ OpenGL middleware for 2D/3D graphics

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
Image.hpp
Go to the documentation of this file.
1 /**************************************************************************************/
2 /* */
3 /* Visualization Library */
4 /* http://visualizationlibrary.org */
5 /* */
6 /* Copyright (c) 2005-2020, Michele Bosi */
7 /* All rights reserved. */
8 /* */
9 /* Redistribution and use in source and binary forms, with or without modification, */
10 /* are permitted provided that the following conditions are met: */
11 /* */
12 /* - Redistributions of source code must retain the above copyright notice, this */
13 /* list of conditions and the following disclaimer. */
14 /* */
15 /* - Redistributions in binary form must reproduce the above copyright notice, this */
16 /* list of conditions and the following disclaimer in the documentation and/or */
17 /* other materials provided with the distribution. */
18 /* */
19 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */
20 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
21 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
22 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */
23 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
24 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
25 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
26 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
27 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
28 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
29 /* */
30 /**************************************************************************************/
31 
32 #ifndef Image_INCUDE_ONCE
33 #define Image_INCUDE_ONCE
34 
35 #include <vlCore/String.hpp>
36 #include <vlCore/Buffer.hpp>
37 #include <vlCore/Vector4.hpp>
38 #include <vlCore/vlnamespace.hpp>
39 #include <vlCore/Rect.hpp>
40 #include <vlCore/KeyValues.hpp>
41 #include <vector>
42 
43 namespace vl
44 {
45  class VirtualFile;
46 
47  //------------------------------------------------------------------------------
48  // Image
49  //------------------------------------------------------------------------------
54  class VLCORE_EXPORT Image: public Object
55  {
57 
58  public:
59  virtual ~Image();
60 
62  Image();
63 
66  Image(void* buffer_ptr, int buffer_bytes);
67 
69  Image(const String& file_path);
70 
72  Image(int x, int y, int z, int bytealign, EImageFormat format, EImageType type);
73 
74  Image(const Image& other);
75 
76  Image& operator=(const Image& other);
77 
78  bool isCubemap() const { return mIsCubemap; }
79 
80  bool isValid() const;
81 
82  EImageDimension dimension() const;
83 
84  void allocate1D(int x, EImageFormat format, EImageType type);
85 
86  void allocate2D(int x, int y, int bytealign, EImageFormat format, EImageType type);
87 
88  void allocate3D(int x, int y, int z, int bytealign, EImageFormat format, EImageType type);
89 
90  void allocateCubemap(int x, int y, int bytealign, EImageFormat format, EImageType type);
91 
93  void reset(int x, int y, int z, int bytealign, EImageFormat format, EImageType type, bool is_cubemap);
94 
96  void reset();
97 
98  int byteAlignment() const;
99 
100  void setByteAlignment(int bytealign);
101 
102  static int bitsPerPixel(EImageType type, EImageFormat format);
103 
104  int bitsPerPixel() const { return bitsPerPixel(type(),format()); }
105 
106  int requiredMemory() const;
107 
108  static int requiredMemory(int x, int y, int z, int bytealign, EImageFormat format, EImageType type, bool is_cubemap);
109 
110  static int requiredMemory1D(int x, EImageFormat format, EImageType type) { return requiredMemory(x, 0, 0, 1, format, type, false); }
111 
112  static int requiredMemory2D(int x, int y, int bytealign, EImageFormat format, EImageType type) { return requiredMemory(x, y, 0, bytealign, format, type, false); }
113 
114  static int requiredMemory3D(int x, int y, int z, int bytealign, EImageFormat format, EImageType type) { return requiredMemory(x, y, z, bytealign, format, type, false); }
115 
116  static int requiredMemoryCubemap(int x, int y, int bytealign, EImageFormat format, EImageType type) { return requiredMemory(x, y, 0, bytealign, format, type, true); }
117 
118  String print() const;
119 
120  String printType() const;
121 
122  String printFormat() const;
123 
124  void setWidth(int x) { mWidth = x; updatePitch(); }
125 
126  void setHeight(int y) { mHeight = y; }
127 
128  void setDepth(int z) { mDepth = z; }
129 
130  void setFormat(EImageFormat format) { mFormat = format; updatePitch(); }
131 
132  void setType(EImageType type) { mType=type; updatePitch(); }
133 
135  bool hasAlpha() const { return mHasAlpha; }
136 
138  void setHasAlpha(bool has_alpha) { mHasAlpha = has_alpha; }
139 
141  int alphaBits() const;
142 
144  bool isNormalMap() const { return mIsNormalMap; }
145 
147  void setIsNormalMap(bool is_normalmap) { mIsNormalMap = is_normalmap; }
148 
151  const KeyValues* tags() const { return mTags.get(); }
152 
155  KeyValues* tags() { return mTags.get(); }
156 
158  void setTags(KeyValues* tags) { mTags = tags; }
159 
161  void setImageBuffer(Buffer* buffer) { mPixels = buffer; }
162 
164  Buffer* imageBuffer() { return mPixels.get(); }
165 
167  const Buffer* imageBuffer() const { return mPixels.get(); }
168 
170  const unsigned char* pixels() const { if (mPixels->bytesUsed()) return mPixels->ptr(); else return NULL; }
171 
173  unsigned char* pixels() { if (mPixels->bytesUsed()) return mPixels->ptr(); else return NULL; }
174 
176  bool empty() { return pixels() == NULL; }
177 
179  unsigned char* pixelsZSlice(int zth_slice);
180 
182  unsigned char* pixelsXP();
184  unsigned char* pixelsXN();
186  unsigned char* pixelsYP();
188  unsigned char* pixelsYN();
190  unsigned char* pixelsZP();
192  unsigned char* pixelsZN();
193 
194  const unsigned char* pixelsXP() const;
195  const unsigned char* pixelsXN() const;
196  const unsigned char* pixelsYP() const;
197  const unsigned char* pixelsYN() const;
198  const unsigned char* pixelsZP() const;
199  const unsigned char* pixelsZN() const;
200 
201  void setMipmaps(const std::vector< ref<Image> >& mipmaps) { mMipmaps = mipmaps; };
202 
203  const std::vector< ref<Image> >& mipmaps() const { return mMipmaps; };
204 
205  std::vector< ref<Image> >& mipmaps() { return mMipmaps; };
206 
207  int width() const { return mWidth; }
208 
209  int height() const { return mHeight; }
210 
211  int depth() const { return mDepth; }
212 
213  int pitch() const { return mPitch; }
214 
215  EImageFormat format() const { return mFormat; }
216 
217  EImageType type() const { return mType; }
218 
219  int isCompressedFormat(EImageFormat fmt);
220 
221  void flipVertically();
222 
248  ref<Image> convertType(EImageType new_type) const;
249 
274  ref<Image> convertFormat(EImageFormat new_format) const;
275 
277  bool equalize();
278 
280  bool contrast(float black, float white);
281 
283  bool contrastHounsfieldAuto();
284 
286  bool contrastHounsfield(float center, float width, float intercept, float range);
287 
289  fvec4 sampleLinear(double x) const;
290 
292  fvec4 sampleLinear(double x, double y) const;
293 
295  fvec4 sampleLinear(double x, double y, double z) const;
296 
325  fvec4 sample(int x, int y=0, int z=0) const;
326 
334  ref<Image> subImage(int xstart, int ystart, int width, int height);
335 
340  void copySubImage(Image* img_src, RectI src, ivec2 dst);
341 
354  void substituteColorRGB_RGBA(unsigned int before, unsigned int after);
355 
366  void substituteColorRGB_RGB(unsigned int before, unsigned int after);
367 
368  void substituteColorGreenKey(unsigned int col0, unsigned int col1);
369 
371  const String& filePath() const { return mFilePath; }
372 
374  void setFilePath(const String& path) { mFilePath = path; }
375 
376  protected:
377  void updatePitch();
378  void allocate();
379 
380  protected:
384  std::vector< ref<Image> > mMipmaps;
385  int mWidth;
386  int mHeight;
387  int mDepth;
388  int mPitch;
394  bool mHasAlpha;
395  };
396 
398  ref<Image> createCubemap(const Image* xp, const Image* xn, const Image* yp, const Image* yn, const Image* zp, const Image* zn);
399 
401  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);
402 
407  VLCORE_EXPORT ref<Image> loadRAW(VirtualFile* file, long long file_offset, int width, int height, int depth, int bytealign, EImageFormat format, EImageType type);
408 
411 
414 
416  VLCORE_EXPORT bool loadImagesFromDir(const String& dir_path, const String& ext, std::vector< ref<Image> >& images);
417 
419  VLCORE_EXPORT ref<Image> assemble3DImage(const std::vector< ref<Image> >& images);
420 
422  VLCORE_EXPORT bool saveImage(Image* img, VirtualFile* file);
423 
425  VLCORE_EXPORT bool saveImage(Image* img, const String& path);
426 
428  VLCORE_EXPORT ref<Image> makeNonUniformColorSpectrum(size_t width, size_t col_count, const fvec4* colors, const float* col_pos);
429 
431  VLCORE_EXPORT ref<Image> makeNonUniformColorSpectrum(int width, const std::vector<fvec4>& colors, const std::vector<float>& col_pos);
432 
434  VLCORE_EXPORT ref<Image> makeColorSpectrum(size_t width, const std::vector<fvec4>& colors);
435 
437  VLCORE_EXPORT ref<Image> makeColorSpectrum(size_t width, const fvec4& c0, const fvec4& c1);
438 
440  VLCORE_EXPORT ref<Image> makeColorSpectrum(size_t width, const fvec4& c0, const fvec4& c1, const fvec4& c2);
441 
443  VLCORE_EXPORT ref<Image> makeColorSpectrum(size_t width, const fvec4& c0, const fvec4& c1, const fvec4& c2, const fvec4& c3);
444 
446  VLCORE_EXPORT ref<Image> makeColorSpectrum(size_t width, const fvec4& c0, const fvec4& c1, const fvec4& c2, const fvec4& c3, const fvec4& c4);
447 
449  VLCORE_EXPORT ref<Image> makeColorSpectrum(size_t width, const fvec4& c0, const fvec4& c1, const fvec4& c2, const fvec4& c3, const fvec4& c4, const fvec4& c5);
450 }
451 
452 #endif
static int requiredMemoryCubemap(int x, int y, int bytealign, EImageFormat format, EImageType type)
Definition: Image.hpp:116
int depth() const
Definition: Image.hpp:211
const Buffer * imageBuffer() const
The buffer used to store the image pixels.
Definition: Image.hpp:167
void setImageBuffer(Buffer *buffer)
The buffer used to store the image pixels.
Definition: Image.hpp:161
static int requiredMemory1D(int x, EImageFormat format, EImageType type)
Definition: Image.hpp:110
An abstract class representing a file.
Definition: VirtualFile.hpp:60
KeyValues * tags()
A set of key/value couples that can be used to attach extra information to an image like DICOM inform...
Definition: Image.hpp:155
The RectI class represents a 2D rectangular area using int precision.
Definition: Rect.hpp:163
const unsigned char * pixels() const
Raw pointer to pixels.
Definition: Image.hpp:170
void setHeight(int y)
Definition: Image.hpp:126
The String class implements an advanced UTF16 (Unicode BMP) string manipulation engine.
Definition: String.hpp:62
VLCORE_EXPORT ref< Image > loadRAW(VirtualFile *file, long long file_offset, int width, int height, int depth, int bytealign, EImageFormat format, EImageType type)
Loads a raw image file.
Definition: Image.cpp:1280
ref< KeyValues > mTags
Definition: Image.hpp:382
const String & filePath() const
The file from which the image was loaded.
Definition: Image.hpp:371
ref< Buffer > mPixels
Definition: Image.hpp:381
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)
Loads six images and assembles them into a cubemap image.
Definition: Image.cpp:1260
EImageFormat mFormat
Definition: Image.hpp:390
std::vector< ref< Image > > & mipmaps()
Definition: Image.hpp:205
std::vector< ref< Image > > mMipmaps
Definition: Image.hpp:384
Implements a buffer whose storage is in local memory.
Definition: Buffer.hpp:46
VLCORE_EXPORT bool loadImagesFromDir(const String &dir_path, const String &ext, std::vector< ref< Image > > &images)
Loads all the images with the specified extension from the given directory.
Definition: Image.cpp:1179
void setType(EImageType type)
Definition: Image.hpp:132
VLCORE_EXPORT ref< Image > makeColorSpectrum(size_t width, const std::vector< fvec4 > &colors)
Creates a 1D Image whose color is interpolated from left to right from the specified spectrum...
Definition: Image.cpp:1134
int mDepth
Definition: Image.hpp:387
int bitsPerPixel() const
Definition: Image.hpp:104
VLCORE_EXPORT ref< Image > loadImage(VirtualFile *file)
Loads an image from the specified file.
Definition: Image.cpp:1214
#define VL_INSTRUMENT_CLASS(ClassName, BaseClass)
Definition: TypeInfo.hpp:122
void setDepth(int z)
Definition: Image.hpp:128
bool hasAlpha() const
Whether an image contains relevant alpha information.
Definition: Image.hpp:135
VLCORE_EXPORT ref< Image > assemble3DImage(const std::vector< ref< Image > > &images)
Assembles the given 2D images in a single 2D image, all the images must be 2D images and have the sam...
Definition: Image.cpp:1151
void setFilePath(const String &path)
The file from which the image was loaded.
Definition: Image.hpp:374
Visualization Library main namespace.
ref< Image > createCubemap(const Image *xp, const Image *xn, const Image *yp, const Image *yn, const Image *zp, const Image *zn)
Assembles a cubemap image.
Definition: Image.cpp:858
bool isNormalMap() const
Whether an image represents a normal map.
Definition: Image.hpp:144
void setMipmaps(const std::vector< ref< Image > > &mipmaps)
Definition: Image.hpp:201
bool empty()
True if image is empty.
Definition: Image.hpp:176
int height() const
Definition: Image.hpp:209
const KeyValues * tags() const
A set of key/value couples that can be used to attach extra information to an image like DICOM inform...
Definition: Image.hpp:151
VLCORE_EXPORT bool saveImage(Image *img, VirtualFile *file)
Writes an image on the specified file.
Definition: Image.cpp:1250
The base class for all the reference counted objects.
Definition: Object.hpp:158
int mPitch
Definition: Image.hpp:388
Buffer * imageBuffer()
The buffer used to store the image pixels.
Definition: Image.hpp:164
EImageDimension
int width() const
Definition: Image.hpp:207
VLCORE_EXPORT ref< Image > makeNonUniformColorSpectrum(size_t width, size_t col_count, const fvec4 *colors, const float *col_pos)
Creates a 1D Image whose color is interpolated from left to right from the specified spectrum...
Definition: Image.cpp:1089
const std::vector< ref< Image > > & mipmaps() const
Definition: Image.hpp:203
int pitch() const
Definition: Image.hpp:213
EImageFormat
void setFormat(EImageFormat format)
Definition: Image.hpp:130
#define NULL
Definition: OpenGLDefs.hpp:81
int mHeight
Definition: Image.hpp:386
bool mIsCubemap
Definition: Image.hpp:392
EImageType mType
Definition: Image.hpp:391
EImageType
bool mIsNormalMap
Definition: Image.hpp:393
static int requiredMemory3D(int x, int y, int z, int bytealign, EImageFormat format, EImageType type)
Definition: Image.hpp:114
void setTags(KeyValues *tags)
A set of key/value couples that can be used to attach extra information to an image like DICOM inform...
Definition: Image.hpp:158
bool mHasAlpha
Definition: Image.hpp:394
int mWidth
Definition: Image.hpp:385
EImageType type() const
Definition: Image.hpp:217
void setWidth(int x)
Definition: Image.hpp:124
Implements a generic 1d, 2d, 3d and cubemap image that can have mipmaps.
Definition: Image.hpp:54
The ref<> class is used to reference-count an Object.
Definition: Object.hpp:55
String mFilePath
Definition: Image.hpp:383
int mByteAlign
Definition: Image.hpp:389
void setHasAlpha(bool has_alpha)
Whether an image contains relevant alpha information.
Definition: Image.hpp:138
unsigned char * pixels()
Raw pointer to pixels.
Definition: Image.hpp:173
Visualization Library&#39;s enums in the &#39;vl&#39; namespace.
static int requiredMemory2D(int x, int y, int bytealign, EImageFormat format, EImageType type)
Definition: Image.hpp:112
A set of key/value pairs usually used to associate generic information, tags, attributes etc...
Definition: KeyValues.hpp:42
void setIsNormalMap(bool is_normalmap)
Whether an image represents a normal map.
Definition: Image.hpp:147
EImageFormat format() const
Definition: Image.hpp:215
bool isCubemap() const
Definition: Image.hpp:78