~ubuntu-branches/debian/sid/openscenegraph/sid

« back to all changes in this revision

Viewing changes to OpenSceneGraph/include/osg/Image

  • Committer: Package Import Robot
  • Author(s): Manuel A. Fernandez Montecelo, Alberto Luaces, Manuel A. Fernandez Montecelo
  • Date: 2013-07-30 13:07:57 UTC
  • mfrom: (1.4.3)
  • Revision ID: package-import@ubuntu.com-20130730130757-b3weq3502sennb16
Tags: 3.2.0~rc1-1
[Alberto Luaces]
* New upstream release.
* Updated standards version (3.9.4).
* Updated SVN URL.
* Removed the static version of the libraries since they impose a huge
  load on the build servers, add additional complexity on the build
  scripts, and make a great impact on the size of the packages, given
  the very small cases where they are useful.
* Acknowledge NMU.

[ Manuel A. Fernandez Montecelo ]
* Switch to @debian.org address

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield 
 
1
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2
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 
 
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
5
 * (at your option) any later version.  The full license is in LICENSE file
6
6
 * included with this distribution, and on the openscenegraph.org website.
7
 
 * 
 
7
 *
8
8
 * This library is distributed in the hope that it will be useful,
9
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
11
 * OpenSceneGraph Public License for more details.
12
12
*/
13
13
 
82
82
    #define GL_LUMINANCE8           0x8040
83
83
    #define GL_LUMINANCE8_ALPHA8    0x8045
84
84
    #define GL_RGBA8                0x8058
 
85
    #define GL_RGBA16               0x805B
85
86
    #define GL_PACK_ROW_LENGTH      0x0D02
86
87
#endif
87
88
 
92
93
    #define GL_UNPACK_IMAGE_HEIGHT  0x806E
93
94
#endif
94
95
 
 
96
#ifndef GL_OES_compressed_ETC1_RGB8_texture
 
97
    #define GL_ETC1_RGB8_OES        0x8D64
 
98
#endif
 
99
 
 
100
#ifndef GL_DEPTH_COMPONENT
 
101
#define GL_DEPTH_COMPONENT              0x1902
 
102
#endif
 
103
 
 
104
#ifndef GL_VERSION_1_4
 
105
#define GL_DEPTH_COMPONENT16              0x81A5
 
106
#define GL_DEPTH_COMPONENT24              0x81A6
 
107
#define GL_DEPTH_COMPONENT32              0x81A7
 
108
#endif
 
109
 
 
110
#ifndef GL_DEPTH_COMPONENT32F
 
111
#define GL_DEPTH_COMPONENT32F             0x8CAC
 
112
#endif
 
113
 
 
114
#ifndef GL_DEPTH_COMPONENT32F_NV
 
115
#define GL_DEPTH_COMPONENT32F_NV          0x8DAB
 
116
#endif
 
117
 
95
118
namespace osg {
96
119
 
97
120
// forward declare
104
127
    public :
105
128
 
106
129
        Image();
107
 
        
 
130
 
108
131
        /** Copy constructor using CopyOp to manage deep vs shallow copy. */
109
132
        Image(const Image& image,const CopyOp& copyop=CopyOp::SHALLOW_COPY);
110
133
 
114
137
        virtual const char* libraryName() const { return "osg"; }
115
138
        virtual const char* className() const { return "Image"; }
116
139
 
 
140
        virtual osg::Image* asImage() { return this; }
 
141
        virtual const osg::Image* asImage() const { return this; }
 
142
 
117
143
        virtual const GLvoid*   getDataPointer() const { return data(); }
118
144
        virtual unsigned int    getTotalDataSize() const { return getTotalSizeInBytesIncludingMipmaps(); }
119
145
 
122
148
 
123
149
        void setFileName(const std::string& fileName);
124
150
        inline const std::string& getFileName() const { return _fileName; }
125
 
        
 
151
 
126
152
        enum WriteHint {
127
153
            NO_PREFERENCE,
128
154
            STORE_INLINE,
129
155
            EXTERNAL_FILE
130
156
        };
131
 
        
 
157
 
132
158
        void setWriteHint(WriteHint writeHint) { _writeHint = writeHint; }
133
159
        WriteHint getWriteHint() const { return _writeHint; }
134
 
        
 
160
 
135
161
        enum AllocationMode {
136
162
            NO_DELETE,
137
163
            USE_NEW_DELETE,
138
164
            USE_MALLOC_FREE
139
165
        };
140
 
        
 
166
 
141
167
        /** Set the method used for deleting data once it goes out of scope. */
142
168
        void setAllocationMode(AllocationMode mode) { _allocationMode = mode; }
143
169
 
149
175
        virtual void allocateImage(int s,int t,int r,
150
176
                           GLenum pixelFormat,GLenum type,
151
177
                           int packing=1);
152
 
        
153
 
        
 
178
 
 
179
 
154
180
        /** Set the image dimensions, format and data. */
155
181
        virtual void setImage(int s,int t,int r,
156
182
                      GLint internalTextureformat,
157
183
                      GLenum pixelFormat,GLenum type,
158
184
                      unsigned char* data,
159
185
                      AllocationMode mode,
160
 
                      int packing=1);
161
 
            
 
186
                      int packing=1, int rowLength=0);
 
187
 
162
188
        /** Read pixels from current frame buffer at specified position and size, using glReadPixels.
163
189
          * Create memory for storage if required, reuse existing pixel coords if possible.
164
190
        */
165
191
        virtual void readPixels(int x,int y,int width,int height,
166
 
                        GLenum pixelFormat,GLenum type);
167
 
            
 
192
                        GLenum pixelFormat, GLenum type, int packing=1);
 
193
 
168
194
 
169
195
        /** Read the contents of the current bound texture, handling compressed pixelFormats if present.
170
196
          * Create memory for storage if required, reuse existing pixel coords if possible.
171
197
        */
172
 
        virtual void readImageFromCurrentTexture(unsigned int contextID, bool copyMipMapsIfAvailable, GLenum type = GL_UNSIGNED_BYTE);
 
198
        virtual void readImageFromCurrentTexture(unsigned int contextID, bool copyMipMapsIfAvailable, GLenum type = GL_UNSIGNED_BYTE, unsigned int face = 0);
173
199
 
 
200
        /** swap the data and settings between two image objects.*/
 
201
        void swap(osg::Image& rhs);
174
202
 
175
203
        /** Scale image to specified size. */
176
204
        void scaleImage(int s,int t,int r) { scaleImage(s,t,r, getDataType()); }
188
216
        virtual void copySubImage(int s_offset, int t_offset, int r_offset, const osg::Image* source);
189
217
 
190
218
 
191
 
        enum Origin 
 
219
        enum Origin
192
220
        {
193
221
            BOTTOM_LEFT,
194
222
            TOP_LEFT
195
223
        };
196
 
        
 
224
 
197
225
        /** Set the origin of the image.
198
226
          * The default value is BOTTOM_LEFT and is consistent with OpenGL.
199
227
          * TOP_LEFT is used for imagery that follows standard Imagery convention, such as movies,
200
228
          * and hasn't been flipped yet.  For such images one much flip the t axis of the tex coords.
201
229
          * to handle this origin position. */
202
230
        void setOrigin(Origin origin) { _origin = origin; }
203
 
        
 
231
 
204
232
        /** Get the origin of the image.*/
205
233
        Origin getOrigin() const { return _origin; }
206
 
        
 
234
 
207
235
 
208
236
        /** Width of image. */
209
237
        inline int s() const { return _s; }
210
238
 
211
239
        /** Height of image. */
212
240
        inline int t() const { return _t; }
213
 
        
 
241
 
214
242
        /** Depth of image. */
215
243
        inline int r() const { return _r; }
216
 
        
 
244
 
 
245
        void setRowLength(int length);
 
246
        inline int getRowLength() const { return _rowLength; }
 
247
 
217
248
        void setInternalTextureFormat(GLint internalFormat);
218
249
        inline GLint getInternalTextureFormat() const { return _internalTextureFormat; }
219
 
        
 
250
 
220
251
        void setPixelFormat(GLenum pixelFormat);
221
252
        inline GLenum getPixelFormat() const { return _pixelFormat; }
222
 
        
 
253
 
223
254
        void setDataType(GLenum dataType);
224
 
        inline GLenum getDataType() const { return _dataType; }        
225
 
        
 
255
        inline GLenum getDataType() const { return _dataType; }
 
256
 
226
257
        void setPacking(unsigned int packing) { _packing = packing; }
227
258
        inline unsigned int getPacking() const { return _packing; }
228
259
 
229
 
        /** return true of the pixel format is an OpenGL compressed pixel format.*/
 
260
        /** Return true of the pixel format is an OpenGL compressed pixel format.*/
230
261
        bool isCompressed() const;
231
262
 
232
263
        /** Set the pixel aspect ratio, defined as the pixel width divided by the pixel height.*/
234
265
 
235
266
        /** Get the pixel aspect ratio.*/
236
267
        inline float getPixelAspectRatio() const { return _pixelAspectRatio; }
237
 
        
 
268
 
238
269
        /** Return the number of bits required for each pixel. */
239
270
        inline unsigned int getPixelSizeInBits() const { return computePixelSizeInBits(_pixelFormat,_dataType); }
240
271
 
241
272
        /** Return the number of bytes each row of pixels occupies once it has been packed. */
242
273
        inline unsigned int getRowSizeInBytes() const { return computeRowWidthInBytes(_s,_pixelFormat,_dataType,_packing); }
243
274
 
 
275
        /** Return the number of bytes between each successive row.
 
276
          * Note, getRowSizeInBytes() will only equal getRowStepInBytes() when isDataContiguous() return true. */
 
277
        inline unsigned int getRowStepInBytes() const { return computeRowWidthInBytes(_rowLength==0?_s:_rowLength,_pixelFormat,_dataType,_packing); }
 
278
 
244
279
        /** Return the number of bytes each image (_s*_t) of pixels occupies. */
245
280
        inline unsigned int getImageSizeInBytes() const { return getRowSizeInBytes()*_t; }
246
 
        
 
281
 
 
282
        /** Return the number of bytes between each successive image.
 
283
          * Note, getImageSizeInBytes() will only equal getImageStepInBytes() when isDataContiguous() return true. */
 
284
        inline unsigned int getImageStepInBytes() const { return getRowStepInBytes()*_t; }
 
285
 
247
286
        /** Return the number of bytes the whole row/image/volume of pixels occupies. */
248
287
        inline unsigned int getTotalSizeInBytes() const { return getImageSizeInBytes()*_r; }
249
288
 
253
292
        /** Return true if the Image represent a valid and usable imagery.*/
254
293
        bool valid() const { return _s!=0 && _t!=0 && _r!=0 && _data!=0 && _dataType!=0; }
255
294
 
256
 
        /** Raw image data. */
 
295
        /** Raw image data.
 
296
          * Note, data in successive rows may not be contiguous, isDataContiguous() return false then you should
 
297
          * take care to access the data per row rather than treating the whole data as a single block. */
257
298
        inline unsigned char* data() { return _data; }
258
 
        
259
 
        /** Raw const image data. */
 
299
 
 
300
        /** Raw const image data.
 
301
          * Note, data in successive rows may not be contiguous, isDataContiguous() return false then you should
 
302
          * take care to access the data per row rather than treating the whole data as a single block. */
260
303
        inline const unsigned char* data() const { return _data; }
261
304
 
262
 
 
263
305
        inline unsigned char* data(int column, int row=0,int image=0)
264
306
        {
265
307
            if (!_data) return NULL;
266
 
            return _data+(column*getPixelSizeInBits())/8+row*getRowSizeInBytes()+image*getImageSizeInBytes();
 
308
            return _data+(column*getPixelSizeInBits())/8+row*getRowStepInBytes()+image*getImageSizeInBytes();
267
309
        }
268
 
        
 
310
 
269
311
        inline const unsigned char* data(int column, int row=0,int image=0) const
270
312
        {
271
313
            if (!_data) return NULL;
272
 
            return _data+(column*getPixelSizeInBits())/8+row*getRowSizeInBytes()+image*getImageSizeInBytes();
 
314
            return _data+(column*getPixelSizeInBits())/8+row*getRowStepInBytes()+image*getImageSizeInBytes();
273
315
        }
274
316
 
 
317
        /** return true if the data stored in the image is a contiguous block of data.*/
 
318
        bool isDataContiguous() const { return _rowLength==0 || _rowLength==_s; }
 
319
 
 
320
        /** Convenience class for assisting the copying of image data when the image data isn't contiguous.*/
 
321
        class OSG_EXPORT DataIterator
 
322
        {
 
323
        public:
 
324
            DataIterator(const Image* image);
 
325
            DataIterator(const DataIterator& ri);
 
326
            ~DataIterator() {}
 
327
 
 
328
            /** advance iterator to next block of data.*/
 
329
            void operator ++ ();
 
330
 
 
331
            /** is iterator valid.*/
 
332
            bool valid() const { return _currentPtr!=0; }
 
333
 
 
334
            /** data pointer of current block to copy.*/
 
335
            const unsigned char* data() const { return _currentPtr; }
 
336
 
 
337
            /** Size of current block to copy.*/
 
338
            unsigned int size() const { return _currentSize; }
 
339
 
 
340
        protected:
 
341
 
 
342
 
 
343
            void assign();
 
344
 
 
345
            const osg::Image*                   _image;
 
346
            int                                 _rowNum;
 
347
            int                                 _imageNum;
 
348
            unsigned int                        _mipmapNum;
 
349
            const unsigned char*                _currentPtr;
 
350
            unsigned int                        _currentSize;
 
351
        };
 
352
 
275
353
        /** Get the color value for specified texcoord.*/
276
354
        Vec4 getColor(unsigned int s,unsigned t=0,unsigned r=0) const;
277
355
 
282
360
        Vec4 getColor(const Vec3& texcoord) const;
283
361
 
284
362
 
285
 
        /** Flip the image horizontally. */
 
363
        /** Flip the image horizontally, around s dimension. */
286
364
        void flipHorizontal();
287
 
        
288
 
        /** Flip the image vertically. */
 
365
 
 
366
        /** Flip the image vertically, around t dimension. */
289
367
        void flipVertical();
290
368
 
 
369
        /** Flip the image around the r dimension. Only relevent for 3D textures. */
 
370
        void flipDepth();
291
371
 
292
372
        /** Ensure image dimensions are a power of two.
293
373
          * Mipmapped textures require the image dimensions to be
299
379
        static bool isPackedType(GLenum type);
300
380
        static GLenum computePixelFormat(GLenum pixelFormat);
301
381
        static GLenum computeFormatDataType(GLenum pixelFormat);
 
382
        static unsigned int computeBlockSize(GLenum pixelFormat, GLenum packing);
302
383
        static unsigned int computeNumComponents(GLenum pixelFormat);
303
384
        static unsigned int computePixelSizeInBits(GLenum pixelFormat,GLenum type);
304
385
        static unsigned int computeRowWidthInBytes(int width,GLenum pixelFormat,GLenum type,int packing);
 
386
        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);
305
387
        static int computeNearestPowerOfTwo(int s,float bias=0.5f);
306
388
        static int computeNumberOfMipmapLevels(int s,int t = 1, int r = 1);
307
389
 
317
399
 
318
400
        /** Send offsets into data. It is assumed that first mipmap offset (index 0) is 0.*/
319
401
        inline void setMipmapLevels(const MipmapDataType& mipmapDataVector) { _mipmapData = mipmapDataVector; }
320
 
        
 
402
 
321
403
        inline const MipmapDataType& getMipmapLevels() const { return _mipmapData; }
322
404
 
323
405
        inline unsigned int getMipmapOffset(unsigned int mipmapLevel) const
328
410
               return _mipmapData[mipmapLevel-1];
329
411
            return 0;
330
412
        };
331
 
        
 
413
 
332
414
        inline unsigned char* getMipmapData(unsigned int mipmapLevel)
333
415
        {
334
416
           return _data+getMipmapOffset(mipmapLevel);
339
421
           return _data+getMipmapOffset(mipmapLevel);
340
422
        }
341
423
 
342
 
        /*inline const unsigned char* getMipmapData(unsigned int row, unsigned int column, unsigned int mipmapLevel) const
343
 
        {
344
 
           if (!_data) return NULL;
345
 
           return getMipmapData(mipmapLevel) + (column*getPixelSizeInBits())/8+row*getRowSizeInBytes();
346
 
        }*/
 
424
        /** returns false for texture formats that do not support texture subloading */
 
425
        bool supportsTextureSubloading() const;
347
426
 
348
427
        /** Return true if this image is translucent - i.e. it has alpha values that are less 1.0 (when normalized). */
349
428
        virtual bool isImageTranslucent() const;
350
429
 
351
 
        /** Set the optional PixelBufferObject used to map the image memory efficiently to graphics memory. */ 
 
430
        /** Set the optional PixelBufferObject used to map the image memory efficiently to graphics memory. */
352
431
        void setPixelBufferObject(PixelBufferObject* buffer) { setBufferObject(buffer); }
353
432
 
354
433
        /** Get the PixelBufferObject.*/
355
 
        PixelBufferObject* getPixelBufferObject() { return dynamic_cast<PixelBufferObject*>(_bufferObject.get()); }
 
434
        PixelBufferObject* getPixelBufferObject() { return dynamic_cast<PixelBufferObject*>(getBufferObject()); }
356
435
 
357
436
        /** Get the const PixelBufferObject.*/
358
 
        const PixelBufferObject* getPixelBufferObject() const { return dynamic_cast<const PixelBufferObject*>(_bufferObject.get()); }
 
437
        const PixelBufferObject* getPixelBufferObject() const { return dynamic_cast<const PixelBufferObject*>(getBufferObject()); }
359
438
 
360
 
        /** return whether the update(NodeVisitor* nv) should be required on each frame to enable proper working of osg::Image.*/
 
439
        /** Return whether the update(NodeVisitor* nv) should be required on each frame to enable proper working of osg::Image.*/
361
440
        virtual bool requiresUpdateCall() const { return false; }
362
441
 
363
442
        /** update method for osg::Image subclasses that update themselves during the update traversal.*/
364
443
        virtual void update(NodeVisitor* /*nv*/) {}
365
444
 
366
 
        /** convience update callback class that can be attached to StateAttribute (such as Textures) to ensure
 
445
        /** Convenience update callback class that can be attached to a StateAttribute (such as Textures) to ensure
367
446
          * that the Image::update(NodeVisitor*) method is called during the update traversal.  This callback
368
447
          * is automatically attached when Image::requiresUpdateCall() is true (it's false by default.)
369
448
          */
372
451
            virtual void operator () (osg::StateAttribute* attr, osg::NodeVisitor* nv);
373
452
        };
374
453
 
375
 
        /** method for hinting whether to enable or disable focus to images acting as front ends to interactive surfaces such as a vnc or browser window.  Return true if handled. */
 
454
        /** Hint whether to enable or disable focus to images acting as front ends to interactive surfaces such as a vnc or browser window.  Return true if handled. */
376
455
        virtual bool sendFocusHint(bool /*focus*/) { return false; }
377
456
 
378
 
        /** method for sending pointer events to images that are acting as front ends to interactive surfaces such as a vnc or browser window.  Return true if handled. */
 
457
        /** Send pointer events to images that are acting as front ends to interactive surfaces such as a vnc or browser window.  Return true if handled. */
379
458
        virtual bool sendPointerEvent(int /*x*/, int /*y*/, int /*buttonMask*/) { return false; }
380
459
 
381
 
        /** method for sending key events to images that are acting as front ends to interactive surfaces such as a vnc or browser window.  Return true if handled.*/
 
460
        /** Send key events to images that are acting as front ends to interactive surfaces such as a vnc or browser window.  Return true if handled.*/
382
461
        virtual bool sendKeyEvent(int /*key*/, bool /*keyDown*/) { return false; }
383
462
 
384
 
        /** method for passing frame information to the custom Image classes, to be called only when objects associated with imagery are not culled.*/
 
463
        /** Pass frame information to the custom Image classes, to be called only when objects associated with imagery are not culled.*/
385
464
        virtual void setFrameLastRendered(const osg::FrameStamp* /*frameStamp*/) {}
 
465
        
 
466
        class DimensionsChangedCallback : public osg::Referenced {
 
467
        public:
 
468
            DimensionsChangedCallback() : osg::Referenced() {}
 
469
            virtual void operator()(osg::Image* image) = 0;
 
470
        };
 
471
        
 
472
        typedef std::vector< osg::ref_ptr<DimensionsChangedCallback> > DimensionsChangedCallbackVector;
 
473
        
 
474
        void addDimensionsChangedCallback(DimensionsChangedCallback* cb);
 
475
        void removeDimensionsChangedCallback(DimensionsChangedCallback* cb);
386
476
 
387
477
    protected :
388
478
 
389
479
        virtual ~Image();
390
480
 
391
481
        Image& operator = (const Image&) { return *this; }
 
482
        
 
483
        void handleDimensionsChangedCallbacks() 
 
484
        {
 
485
            for(DimensionsChangedCallbackVector::iterator i = _dimensionsChangedCallbacks.begin(); i != _dimensionsChangedCallbacks.end(); ++i)
 
486
            {
 
487
                (*i)->operator()(this);
 
488
            }
 
489
        }
392
490
 
393
491
        std::string _fileName;
394
492
        WriteHint   _writeHint;
395
493
 
396
 
 
397
494
        Origin _origin;
398
495
 
399
496
        int _s, _t, _r;
 
497
        int _rowLength;
400
498
        GLint _internalTextureFormat;
401
499
        GLenum _pixelFormat;
402
500
        GLenum _dataType;
405
503
 
406
504
        AllocationMode _allocationMode;
407
505
        unsigned char* _data;
408
 
        
 
506
 
409
507
        void deallocateData();
410
 
        
 
508
 
411
509
        void setData(unsigned char* data,AllocationMode allocationMode);
412
510
 
413
511
        MipmapDataType _mipmapData;
414
512
        
415
 
        ref_ptr<PixelBufferObject> _bufferObject;
 
513
        DimensionsChangedCallbackVector _dimensionsChangedCallbacks;
416
514
};
417
515
 
418
516
class Geode;