OSG  3.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Image.h
Go to the documentation of this file.
1 /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2  *
3  * This library is open source and may be redistributed and/or modified under
4  * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5  * (at your option) any later version. The full license is in LICENSE file
6  * included with this distribution, and on the openscenegraph.org website.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * OpenSceneGraph Public License for more details.
12 */
13 
14 #ifndef OSG_IMAGE
15 #define OSG_IMAGE 1
16 
17 #include <osg/BufferObject>
18 #include <osg/Vec2>
19 #include <osg/Vec3>
20 #include <osg/Vec4>
21 #include <osg/FrameStamp>
22 #include <osg/StateAttribute>
23 
24 #include <string>
25 #include <vector>
26 
27 #ifndef GL_VERSION_1_2
28  // 1.2 definitions...
29  #define GL_BGR 0x80E0
30  #define GL_BGRA 0x80E1
31  #define GL_UNSIGNED_BYTE_3_3_2 0x8032
32  #define GL_UNSIGNED_BYTE_2_3_3_REV 0x8362
33  #define GL_UNSIGNED_SHORT_5_6_5 0x8363
34  #define GL_UNSIGNED_SHORT_5_6_5_REV 0x8364
35  #define GL_UNSIGNED_SHORT_4_4_4_4 0x8033
36  #define GL_UNSIGNED_SHORT_4_4_4_4_REV 0x8365
37  #define GL_UNSIGNED_SHORT_5_5_5_1 0x8034
38  #define GL_UNSIGNED_SHORT_1_5_5_5_REV 0x8366
39  #define GL_UNSIGNED_INT_8_8_8_8 0x8035
40  #define GL_UNSIGNED_INT_8_8_8_8_REV 0x8367
41  #define GL_UNSIGNED_INT_10_10_10_2 0x8036
42  #define GL_UNSIGNED_INT_2_10_10_10_REV 0x8368
43 #endif
44 
45 #ifndef GL_COMPRESSED_ALPHA
46  #define GL_COMPRESSED_ALPHA 0x84E9
47  #define GL_COMPRESSED_LUMINANCE 0x84EA
48  #define GL_COMPRESSED_LUMINANCE_ALPHA 0x84EB
49  #define GL_COMPRESSED_INTENSITY 0x84EC
50  #define GL_COMPRESSED_RGB 0x84ED
51  #define GL_COMPRESSED_RGBA 0x84EE
52 #endif
53 
54 
55 #ifndef GL_ABGR_EXT
56 #define GL_ABGR_EXT 0x8000
57 #endif
58 
59 #if defined(OSG_GLES1_AVAILABLE) || defined(OSG_GLES2_AVAILABLE)
60  #define GL_RED 0x1903
61  #define GL_GREEN 0x1904
62  #define GL_BLUE 0x1905
63  #define GL_DEPTH_COMPONENT 0x1902
64  #define GL_STENCIL_INDEX 0x1901
65 #endif
66 
67 #if defined(OSG_GLES1_AVAILABLE) || defined(OSG_GLES2_AVAILABLE) || defined(OSG_GL3_AVAILABLE)
68  #define GL_BITMAP 0x1A00
69  #define GL_COLOR_INDEX 0x1900
70  #define GL_INTENSITY12 0x804C
71  #define GL_INTENSITY16 0x804D
72  #define GL_INTENSITY 0x8049
73  #define GL_INTENSITY4 0x804A
74  #define GL_INTENSITY8 0x804B
75  #define GL_LUMINANCE12 0x8041
76  #define GL_LUMINANCE12_ALPHA4 0x8046
77  #define GL_LUMINANCE12_ALPHA12 0x8047
78  #define GL_LUMINANCE16 0x8042
79  #define GL_LUMINANCE16_ALPHA16 0x8048
80  #define GL_LUMINANCE4 0x803F
81  #define GL_LUMINANCE4_ALPHA4 0x8043
82  #define GL_LUMINANCE6_ALPHA2 0x8044
83  #define GL_LUMINANCE8 0x8040
84  #define GL_LUMINANCE8_ALPHA8 0x8045
85  #define GL_RGBA8 0x8058
86  #define GL_RGBA16 0x805B
87  #define GL_PACK_ROW_LENGTH 0x0D02
88 #endif
89 
90 #ifndef GL_PACK_SKIP_IMAGES
91  #define GL_PACK_SKIP_IMAGES 0x806B
92  #define GL_PACK_IMAGE_HEIGHT 0x806C
93  #define GL_UNPACK_SKIP_IMAGES 0x806D
94  #define GL_UNPACK_IMAGE_HEIGHT 0x806E
95 #endif
96 
97 #ifndef GL_OES_compressed_ETC1_RGB8_texture
98  #define GL_ETC1_RGB8_OES 0x8D64
99 #endif
100 
101 #ifndef GL_ARB_ES3_compatibility
102  #define GL_COMPRESSED_RGB8_ETC2 0x9274
103  #define GL_COMPRESSED_SRGB8_ETC2 0x9275
104  #define GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2 0x9276
105  #define GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2 0x9277
106  #define GL_COMPRESSED_RGBA8_ETC2_EAC 0x9278
107  #define GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC 0x9279
108  #define GL_COMPRESSED_R11_EAC 0x9270
109  #define GL_COMPRESSED_SIGNED_R11_EAC 0x9271
110  #define GL_COMPRESSED_RG11_EAC 0x9272
111  #define GL_COMPRESSED_SIGNED_RG11_EAC 0x9273
112 #endif
113 
114 #ifndef GL_DEPTH_COMPONENT
115 #define GL_DEPTH_COMPONENT 0x1902
116 #endif
117 
118 #ifndef GL_VERSION_1_4
119 #define GL_DEPTH_COMPONENT16 0x81A5
120 #define GL_DEPTH_COMPONENT24 0x81A6
121 #define GL_DEPTH_COMPONENT32 0x81A7
122 #endif
123 
124 #ifndef GL_DEPTH_COMPONENT32F
125 #define GL_DEPTH_COMPONENT32F 0x8CAC
126 #endif
127 
128 #ifndef GL_DEPTH_COMPONENT32F_NV
129 #define GL_DEPTH_COMPONENT32F_NV 0x8DAB
130 #endif
131 
132 namespace osg {
133 
134 // forward declare
135 class NodeVisitor;
136 
139 {
140 
141  public :
142 
143  Image();
144 
146  Image(const Image& image,const CopyOp& copyop=CopyOp::SHALLOW_COPY);
147 
148  virtual Object* cloneType() const { return new Image(); }
149  virtual Object* clone(const CopyOp& copyop) const { return new Image(*this,copyop); }
150  virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const Image*>(obj)!=0; }
151  virtual const char* libraryName() const { return "osg"; }
152  virtual const char* className() const { return "Image"; }
153 
154  virtual osg::Image* asImage() { return this; }
155  virtual const osg::Image* asImage() const { return this; }
156 
157  virtual const GLvoid* getDataPointer() const { return data(); }
158  virtual unsigned int getTotalDataSize() const { return getTotalSizeInBytesIncludingMipmaps(); }
159 
161  virtual int compare(const Image& rhs) const;
162 
163  void setFileName(const std::string& fileName);
164  inline const std::string& getFileName() const { return _fileName; }
165 
166  enum WriteHint {
169  EXTERNAL_FILE
170  };
171 
172  void setWriteHint(WriteHint writeHint) { _writeHint = writeHint; }
173  WriteHint getWriteHint() const { return _writeHint; }
174 
178  USE_MALLOC_FREE
179  };
180 
182  void setAllocationMode(AllocationMode mode) { _allocationMode = mode; }
183 
185  AllocationMode getAllocationMode() const { return _allocationMode; }
186 
187 
189  virtual void allocateImage(int s,int t,int r,
190  GLenum pixelFormat,GLenum type,
191  int packing=1);
192 
193 
195  virtual void setImage(int s,int t,int r,
196  GLint internalTextureformat,
197  GLenum pixelFormat,GLenum type,
198  unsigned char* data,
199  AllocationMode mode,
200  int packing=1, int rowLength=0);
201 
205  virtual void readPixels(int x,int y,int width,int height,
206  GLenum pixelFormat, GLenum type, int packing=1);
207 
208 
212  virtual void readImageFromCurrentTexture(unsigned int contextID, bool copyMipMapsIfAvailable, GLenum type = GL_UNSIGNED_BYTE, unsigned int face = 0);
213 
215  void swap(osg::Image& rhs);
216 
218  void scaleImage(int s,int t,int r) { scaleImage(s,t,r, getDataType()); }
219 
221  virtual void scaleImage(int s,int t,int r, GLenum newDataType);
222 
230  virtual void copySubImage(int s_offset, int t_offset, int r_offset, const osg::Image* source);
231 
232 
233  enum Origin
234  {
236  TOP_LEFT
237  };
238 
244  void setOrigin(Origin origin) { _origin = origin; }
245 
247  Origin getOrigin() const { return _origin; }
248 
249 
251  inline int s() const { return _s; }
252 
254  inline int t() const { return _t; }
255 
257  inline int r() const { return _r; }
258 
259  void setRowLength(int length);
260  inline int getRowLength() const { return _rowLength; }
261 
262  void setInternalTextureFormat(GLint internalFormat);
263  inline GLint getInternalTextureFormat() const { return _internalTextureFormat; }
264 
265  void setPixelFormat(GLenum pixelFormat);
266  inline GLenum getPixelFormat() const { return _pixelFormat; }
267 
268  void setDataType(GLenum dataType);
269  inline GLenum getDataType() const { return _dataType; }
270 
271  void setPacking(unsigned int packing) { _packing = packing; }
272  inline unsigned int getPacking() const { return _packing; }
273 
275  bool isCompressed() const;
276 
278  inline void setPixelAspectRatio(float pixelAspectRatio) { _pixelAspectRatio = pixelAspectRatio; }
279 
281  inline float getPixelAspectRatio() const { return _pixelAspectRatio; }
282 
284  inline unsigned int getPixelSizeInBits() const { return computePixelSizeInBits(_pixelFormat,_dataType); }
285 
287  inline unsigned int getRowSizeInBytes() const { return computeRowWidthInBytes(_s,_pixelFormat,_dataType,_packing); }
288 
291  inline unsigned int getRowStepInBytes() const { return computeRowWidthInBytes(_rowLength==0?_s:_rowLength,_pixelFormat,_dataType,_packing); }
292 
294  inline unsigned int getImageSizeInBytes() const { return getRowSizeInBytes()*_t; }
295 
298  inline unsigned int getImageStepInBytes() const { return getRowStepInBytes()*_t; }
299 
301  inline unsigned int getTotalSizeInBytes() const { return getImageSizeInBytes()*_r; }
302 
304  unsigned int getTotalSizeInBytesIncludingMipmaps() const;
305 
307  bool valid() const { return _s!=0 && _t!=0 && _r!=0 && _data!=0 && _dataType!=0; }
308 
312  inline unsigned char* data() { return _data; }
313 
317  inline const unsigned char* data() const { return _data; }
318 
319  inline unsigned char* data(unsigned int column, unsigned int row = 0, unsigned int image = 0)
320  {
321  if (!_data) return NULL;
322  return _data+(column*getPixelSizeInBits())/8+row*getRowStepInBytes()+image*getImageSizeInBytes();
323  }
324 
325  inline const unsigned char* data(unsigned int column, unsigned int row = 0, unsigned int image = 0) const
326  {
327  if (!_data) return NULL;
328  return _data+(column*getPixelSizeInBits())/8+row*getRowStepInBytes()+image*getImageSizeInBytes();
329  }
330 
332  bool isDataContiguous() const { return _rowLength==0 || _rowLength==_s; }
333 
336  {
337  public:
338  DataIterator(const Image* image);
339  DataIterator(const DataIterator& ri);
341 
343  void operator ++ ();
344 
346  bool valid() const { return _currentPtr!=0; }
347 
349  const unsigned char* data() const { return _currentPtr; }
350 
352  unsigned int size() const { return _currentSize; }
353 
354  protected:
355 
356 
357  void assign();
358 
360  int _rowNum;
362  unsigned int _mipmapNum;
363  const unsigned char* _currentPtr;
364  unsigned int _currentSize;
365  };
366 
368  Vec4 getColor(unsigned int s,unsigned t=0,unsigned r=0) const;
369 
371  Vec4 getColor(const Vec2& texcoord) const { return getColor(Vec3(texcoord.x(),texcoord.y(),0.0f)); }
372 
374  Vec4 getColor(const Vec3& texcoord) const;
375 
377  void setColor(const osg::Vec4& color, unsigned int s, unsigned int t=0, unsigned int r=0);
378 
380  void setColor(const osg::Vec4& color, const osg::Vec2& texcoord ) { setColor(color, osg::Vec3(texcoord, 0.0f)); }
381 
383  void setColor(const osg::Vec4& color, const osg::Vec3& texcoord );
384 
386  void flipHorizontal();
387 
389  void flipVertical();
390 
392  void flipDepth();
393 
399  void ensureValidSizeForTexturing(GLint maxTextureSize);
400 
401  static bool isPackedType(GLenum type);
402  static GLenum computePixelFormat(GLenum pixelFormat);
403  static GLenum computeFormatDataType(GLenum pixelFormat);
404  static unsigned int computeBlockSize(GLenum pixelFormat, GLenum packing);
405  static unsigned int computeNumComponents(GLenum pixelFormat);
406  static unsigned int computePixelSizeInBits(GLenum pixelFormat,GLenum type);
407  static unsigned int computeRowWidthInBytes(int width,GLenum pixelFormat,GLenum type,int packing);
408  static unsigned int computeImageSizeInBytes(int width,int height, int depth, GLenum pixelFormat, GLenum type, int packing = 1, int slice_packing = 1, int image_packing = 1);
409  static int computeNearestPowerOfTwo(int s,float bias=0.5f);
410  static int computeNumberOfMipmapLevels(int s,int t = 1, int r = 1);
411 
413  typedef std::vector< unsigned int > MipmapDataType;
414 
415  inline bool isMipmap() const {return !_mipmapData.empty();};
416 
417  unsigned int getNumMipmapLevels() const
418  {
419  return static_cast<unsigned int>(_mipmapData.size())+1;
420  };
421 
423  inline void setMipmapLevels(const MipmapDataType& mipmapDataVector) { _mipmapData = mipmapDataVector; }
424 
425  inline const MipmapDataType& getMipmapLevels() const { return _mipmapData; }
426 
427  inline unsigned int getMipmapOffset(unsigned int mipmapLevel) const
428  {
429  if(mipmapLevel == 0)
430  return 0;
431  else if (mipmapLevel < getNumMipmapLevels())
432  return _mipmapData[mipmapLevel-1];
433  return 0;
434  };
435 
436  inline unsigned char* getMipmapData(unsigned int mipmapLevel)
437  {
438  return _data+getMipmapOffset(mipmapLevel);
439  }
440 
441  inline const unsigned char* getMipmapData(unsigned int mipmapLevel) const
442  {
443  return _data+getMipmapOffset(mipmapLevel);
444  }
445 
447  bool supportsTextureSubloading() const;
448 
450  virtual bool isImageTranslucent() const;
451 
453  void setPixelBufferObject(PixelBufferObject* buffer) { setBufferObject(buffer); }
454 
456  PixelBufferObject* getPixelBufferObject() { return dynamic_cast<PixelBufferObject*>(getBufferObject()); }
457 
459  const PixelBufferObject* getPixelBufferObject() const { return dynamic_cast<const PixelBufferObject*>(getBufferObject()); }
460 
462  virtual bool requiresUpdateCall() const { return false; }
463 
465  virtual void update(NodeVisitor* /*nv*/) {}
466 
472  {
473  virtual void operator () (osg::StateAttribute* attr, osg::NodeVisitor* nv);
474  };
475 
477  virtual bool sendFocusHint(bool /*focus*/) { return false; }
478 
480  virtual bool sendPointerEvent(int /*x*/, int /*y*/, int /*buttonMask*/) { return false; }
481 
483  virtual bool sendKeyEvent(int /*key*/, bool /*keyDown*/) { return false; }
484 
486  virtual void setFrameLastRendered(const osg::FrameStamp* /*frameStamp*/) {}
487 
489  public:
491  virtual void operator()(osg::Image* image) = 0;
492  };
493 
494  typedef std::vector< osg::ref_ptr<DimensionsChangedCallback> > DimensionsChangedCallbackVector;
495 
496  void addDimensionsChangedCallback(DimensionsChangedCallback* cb);
497  void removeDimensionsChangedCallback(DimensionsChangedCallback* cb);
498 
499  protected :
500 
501  virtual ~Image();
502 
503  Image& operator = (const Image&) { return *this; }
504 
506  {
507  for(DimensionsChangedCallbackVector::iterator i = _dimensionsChangedCallbacks.begin(); i != _dimensionsChangedCallbacks.end(); ++i)
508  {
509  (*i)->operator()(this);
510  }
511  }
512 
513  std::string _fileName;
515 
517 
518  int _s, _t, _r;
521  GLenum _pixelFormat;
522  GLenum _dataType;
523  unsigned int _packing;
525 
527  unsigned char* _data;
528 
529  void deallocateData();
530 
531  void setData(unsigned char* data,AllocationMode allocationMode);
532 
533  MipmapDataType _mipmapData;
534 
535  DimensionsChangedCallbackVector _dimensionsChangedCallbacks;
536 };
537 
538 class Geode;
539 
544 extern OSG_EXPORT Geode* createGeodeForImage(Image* image);
549 extern OSG_EXPORT Geode* createGeodeForImage(Image* image,float s,float t);
550 
551 }
552 
553 #endif // __SG_IMAGE_H
void setMipmapLevels(const MipmapDataType &mipmapDataVector)
Definition: Image.h:423
#define OSG_EXPORT
Definition: Export.h:43
virtual const char * libraryName() const
Definition: Image.h:151
GLenum _dataType
Definition: Image.h:522
int s() const
Definition: Image.h:251
virtual bool isSameKindAs(const Object *obj) const
Definition: Image.h:150
#define NULL
Definition: Export.h:59
virtual bool sendPointerEvent(int, int, int)
Definition: Image.h:480
Origin _origin
Definition: Image.h:516
void handleDimensionsChangedCallbacks()
Definition: Image.h:505
GLint GLenum internalFormat
Definition: GLU.h:71
unsigned int getPixelSizeInBits() const
Definition: Image.h:284
int _t
Definition: Image.h:518
unsigned int getMipmapOffset(unsigned int mipmapLevel) const
Definition: Image.h:427
value_type & y()
Definition: Vec2f.h:68
virtual const GLvoid * getDataPointer() const
Definition: Image.h:157
unsigned int getImageStepInBytes() const
Definition: Image.h:298
unsigned int _mipmapNum
Definition: Image.h:362
virtual void setFrameLastRendered(const osg::FrameStamp *)
Definition: Image.h:486
int _rowLength
Definition: Image.h:519
void setPixelBufferObject(PixelBufferObject *buffer)
Definition: Image.h:453
GLint GLenum GLsizei GLsizei GLsizei depth
Definition: GLU.h:71
unsigned int getImageSizeInBytes() const
Definition: Image.h:294
GLenum getPixelFormat() const
Definition: Image.h:266
const unsigned char * data() const
Definition: Image.h:317
const MipmapDataType & getMipmapLevels() const
Definition: Image.h:425
unsigned int getPacking() const
Definition: Image.h:272
const osg::Image * _image
Definition: Image.h:359
void scaleImage(int s, int t, int r)
Definition: Image.h:218
std::string _fileName
Definition: Image.h:513
const unsigned char * getMipmapData(unsigned int mipmapLevel) const
Definition: Image.h:441
int t() const
Definition: Image.h:254
void setWriteHint(WriteHint writeHint)
Definition: Image.h:172
AllocationMode getAllocationMode() const
Definition: Image.h:185
value_type & x()
Definition: Vec2f.h:67
void swap(MixinVector< ValueT > &left, MixinVector< ValueT > &right)
Definition: MixinVector.h:174
unsigned int size() const
Definition: Image.h:352
std::vector< unsigned int > MipmapDataType
Definition: Image.h:413
virtual unsigned int getTotalDataSize() const
Definition: Image.h:158
const unsigned char * data() const
Definition: Image.h:349
virtual const char * className() const
Definition: Image.h:152
int r() const
Definition: Image.h:257
Vec4 getColor(const Vec2 &texcoord) const
Definition: Image.h:371
PixelBufferObject * getPixelBufferObject()
Definition: Image.h:456
bool valid() const
Definition: Image.h:346
virtual void update(NodeVisitor *)
Definition: Image.h:465
GLint GLenum GLsizei width
Definition: GLU.h:71
std::vector< osg::ref_ptr< DimensionsChangedCallback > > DimensionsChangedCallbackVector
Definition: Image.h:494
unsigned char * _data
Definition: Image.h:527
Vec3f Vec3
Definition: Vec3.h:21
AllocationMode
Definition: Image.h:175
void setColor(const osg::Vec4 &color, const osg::Vec2 &texcoord)
Definition: Image.h:380
void setPixelAspectRatio(float pixelAspectRatio)
Definition: Image.h:278
unsigned int getRowStepInBytes() const
Definition: Image.h:291
virtual bool sendKeyEvent(int, bool)
Definition: Image.h:483
unsigned int getTotalSizeInBytes() const
Definition: Image.h:301
GLenum _pixelFormat
Definition: Image.h:521
DimensionsChangedCallbackVector _dimensionsChangedCallbacks
Definition: Image.h:535
void setAllocationMode(AllocationMode mode)
Definition: Image.h:182
bool isDataContiguous() const
Definition: Image.h:332
bool valid() const
Definition: Image.h:307
GLenum getDataType() const
Definition: Image.h:269
GLint GLenum GLsizei GLsizei GLsizei GLint GLenum GLenum type
Definition: GLU.h:71
bool isMipmap() const
Definition: Image.h:415
const unsigned char * data(unsigned int column, unsigned int row=0, unsigned int image=0) const
Definition: Image.h:325
GLint GLenum GLsizei GLsizei height
Definition: GLU.h:71
virtual bool requiresUpdateCall() const
Definition: Image.h:462
GLint getInternalTextureFormat() const
Definition: Image.h:263
void setOrigin(Origin origin)
Definition: Image.h:244
int getRowLength() const
Definition: Image.h:260
unsigned int getNumMipmapLevels() const
Definition: Image.h:417
unsigned char * getMipmapData(unsigned int mipmapLevel)
Definition: Image.h:436
void setPacking(unsigned int packing)
Definition: Image.h:271
float _pixelAspectRatio
Definition: Image.h:524
unsigned char * data()
Definition: Image.h:312
virtual Object * clone(const CopyOp &copyop) const
Definition: Image.h:149
const std::string & getFileName() const
Definition: Image.h:164
virtual bool sendFocusHint(bool)
Definition: Image.h:477
Definition: AlphaFunc.h:19
WriteHint _writeHint
Definition: Image.h:514
virtual Object * cloneType() const
Definition: Image.h:148
AllocationMode _allocationMode
Definition: Image.h:526
const PixelBufferObject * getPixelBufferObject() const
Definition: Image.h:459
GLint _internalTextureFormat
Definition: Image.h:520
float getPixelAspectRatio() const
Definition: Image.h:281
Origin getOrigin() const
Definition: Image.h:247
virtual osg::Image * asImage()
Definition: Image.h:154
unsigned char * data(unsigned int column, unsigned int row=0, unsigned int image=0)
Definition: Image.h:319
MipmapDataType _mipmapData
Definition: Image.h:533
unsigned int _packing
Definition: Image.h:523
WriteHint getWriteHint() const
Definition: Image.h:173
const unsigned char * _currentPtr
Definition: Image.h:363
unsigned int getRowSizeInBytes() const
Definition: Image.h:287
OSG_EXPORT Geode * createGeodeForImage(Image *image)
virtual const osg::Image * asImage() const
Definition: Image.h:155
unsigned int _currentSize
Definition: Image.h:364