root/OpenSceneGraph/trunk/include/osg/Image @ 10600

Revision 10600, 15.0 kB (checked in by robert, 5 years ago)

Introduced new BufferObject? design + implementation in preperation of implementing a pool system for buffer objects

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
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
23#include <string>
24#include <vector>
25
26#ifndef GL_VERSION_1_2
27    // 1.2 definitions...
28    #define GL_BGR                          0x80E0
29    #define GL_BGRA                         0x80E1
30    #define GL_UNSIGNED_BYTE_3_3_2          0x8032
31    #define GL_UNSIGNED_BYTE_2_3_3_REV      0x8362
32    #define GL_UNSIGNED_SHORT_5_6_5         0x8363
33    #define GL_UNSIGNED_SHORT_5_6_5_REV     0x8364
34    #define GL_UNSIGNED_SHORT_4_4_4_4       0x8033
35    #define GL_UNSIGNED_SHORT_4_4_4_4_REV   0x8365
36    #define GL_UNSIGNED_SHORT_5_5_5_1       0x8034
37    #define GL_UNSIGNED_SHORT_1_5_5_5_REV   0x8366
38    #define GL_UNSIGNED_INT_8_8_8_8         0x8035
39    #define GL_UNSIGNED_INT_8_8_8_8_REV     0x8367
40    #define GL_UNSIGNED_INT_10_10_10_2      0x8036
41    #define GL_UNSIGNED_INT_2_10_10_10_REV  0x8368
42#endif
43
44#ifndef GL_COMPRESSED_ALPHA
45    #define GL_COMPRESSED_ALPHA             0x84E9
46    #define GL_COMPRESSED_LUMINANCE         0x84EA
47    #define GL_COMPRESSED_LUMINANCE_ALPHA   0x84EB
48    #define GL_COMPRESSED_INTENSITY         0x84EC
49    #define GL_COMPRESSED_RGB               0x84ED
50    #define GL_COMPRESSED_RGBA              0x84EE
51#endif
52
53
54#ifndef GL_ABGR_EXT
55#define GL_ABGR_EXT                         0x8000
56#endif
57
58namespace osg {
59
60// forward declare
61class NodeVisitor;
62
63/** Image class for encapsulating the storage texture image data. */
64class OSG_EXPORT Image : public BufferData
65{
66
67    public :
68
69        Image();
70       
71        /** Copy constructor using CopyOp to manage deep vs shallow copy. */
72        Image(const Image& image,const CopyOp& copyop=CopyOp::SHALLOW_COPY);
73
74        virtual Object* cloneType() const { return new Image(); }
75        virtual Object* clone(const CopyOp& copyop) const { return new Image(*this,copyop); }
76        virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const Image*>(obj)!=0; }
77        virtual const char* libraryName() const { return "osg"; }
78        virtual const char* className() const { return "Image"; }
79
80        virtual const GLvoid*   getDataPointer() const { return data(); }
81        virtual unsigned int    getTotalDataSize() const { return getTotalSizeInBytesIncludingMipmaps(); }
82
83        /** Return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs. */
84        virtual int compare(const Image& rhs) const;
85
86        void setFileName(const std::string& fileName);
87        inline const std::string& getFileName() const { return _fileName; }
88       
89        enum WriteHint {
90            NO_PREFERENCE,
91            STORE_INLINE,
92            EXTERNAL_FILE
93        };
94       
95        void setWriteHint(WriteHint writeHint) { _writeHint = writeHint; }
96        WriteHint getWriteHint() const { return _writeHint; }
97       
98        enum AllocationMode {
99            NO_DELETE,
100            USE_NEW_DELETE,
101            USE_MALLOC_FREE
102        };
103       
104        /** Set the method used for deleting data once it goes out of scope. */
105        void setAllocationMode(AllocationMode mode) { _allocationMode = mode; }
106
107        /** Get the method used for deleting data once it goes out of scope. */
108        AllocationMode getAllocationMode() const { return _allocationMode; }
109
110
111        /** Allocate a pixel block of specified size and type. */
112        void allocateImage(int s,int t,int r,
113                           GLenum pixelFormat,GLenum type,
114                           int packing=1);
115       
116       
117        /** Set the image dimensions, format and data. */
118        void setImage(int s,int t,int r,
119                      GLint internalTextureformat,
120                      GLenum pixelFormat,GLenum type,
121                      unsigned char* data,
122                      AllocationMode mode,
123                      int packing=1);
124           
125        /** Read pixels from current frame buffer at specified position and size, using glReadPixels.
126          * Create memory for storage if required, reuse existing pixel coords if possible.
127        */
128        void readPixels(int x,int y,int width,int height,
129                        GLenum pixelFormat,GLenum type);
130           
131
132        /** Read the contents of the current bound texture, handling compressed pixelFormats if present.
133          * Create memory for storage if required, reuse existing pixel coords if possible.
134        */
135        void readImageFromCurrentTexture(unsigned int contextID, bool copyMipMapsIfAvailable, GLenum type = GL_UNSIGNED_BYTE);
136
137
138        /** Scale image to specified size. */
139        void scaleImage(int s,int t,int r) { scaleImage(s,t,r, getDataType()); }
140
141        /** Scale image to specified size and with specified data type. */
142        void scaleImage(int s,int t,int r, GLenum newDataType);
143
144        /** Copy a source Image into a subpart of this Image at specified position.
145          * Typically used to copy to an already allocated image, such as creating
146          * a 3D image from a stack 2D images.
147          * If this Image is empty then image data is created to
148          * accomodate the source image in its offset position.
149          * If source is NULL then no operation happens, this Image is left unchanged.
150        */
151        void copySubImage(int s_offset, int t_offset, int r_offset, const osg::Image* source);
152
153
154        enum Origin
155        {
156            BOTTOM_LEFT,
157            TOP_LEFT
158        };
159       
160        /** Set the origin of the image.
161          * The default value is BOTTOM_LEFT and is consistent with OpenGL.
162          * TOP_LEFT is used for imagery that follows standard Imagery convention, such as movies,
163          * and hasn't been flipped yet.  For such images one much flip the t axis of the tex coords.
164          * to handle this origin position. */
165        void setOrigin(Origin origin) { _origin = origin; }
166       
167        /** Get the origin of the image.*/
168        Origin getOrigin() const { return _origin; }
169       
170
171        /** Width of image. */
172        inline int s() const { return _s; }
173
174        /** Height of image. */
175        inline int t() const { return _t; }
176       
177        /** Depth of image. */
178        inline int r() const { return _r; }
179       
180        void setInternalTextureFormat(GLint internalFormat);
181        inline GLint getInternalTextureFormat() const { return _internalTextureFormat; }
182       
183        void setPixelFormat(GLenum pixelFormat);
184        inline GLenum getPixelFormat() const { return _pixelFormat; }
185       
186        void setDataType(GLenum dataType);
187        inline GLenum getDataType() const { return _dataType; }       
188       
189        void setPacking(unsigned int packing) { _packing = packing; }
190        inline unsigned int getPacking() const { return _packing; }
191
192        /** Set the pixel aspect ratio, defined as the pixel width divided by the pixel height.*/
193        inline void setPixelAspectRatio(float pixelAspectRatio) { _pixelAspectRatio = pixelAspectRatio; }
194
195        /** Get the pixel aspect ratio.*/
196        inline float getPixelAspectRatio() const { return _pixelAspectRatio; }
197       
198        /** Return the number of bits required for each pixel. */
199        inline unsigned int getPixelSizeInBits() const { return computePixelSizeInBits(_pixelFormat,_dataType); }
200
201        /** Return the number of bytes each row of pixels occupies once it has been packed. */
202        inline unsigned int getRowSizeInBytes() const { return computeRowWidthInBytes(_s,_pixelFormat,_dataType,_packing); }
203
204        /** Return the number of bytes each image (_s*_t) of pixels occupies. */
205        inline unsigned int getImageSizeInBytes() const { return getRowSizeInBytes()*_t; }
206       
207        /** Return the number of bytes the whole row/image/volume of pixels occupies. */
208        inline unsigned int getTotalSizeInBytes() const { return getImageSizeInBytes()*_r; }
209
210        /** Return the number of bytes the whole row/image/volume of pixels occupies, including all mip maps if included. */
211        unsigned int getTotalSizeInBytesIncludingMipmaps() const;
212
213        /** Return true if the Image represent a valid and usable imagery.*/
214        bool valid() const { return _s!=0 && _t!=0 && _r!=0 && _data!=0 && _dataType!=0; }
215
216        /** Raw image data. */
217        inline unsigned char* data() { return _data; }
218       
219        /** Raw const image data. */
220        inline const unsigned char* data() const { return _data; }
221
222
223        inline unsigned char* data(int column, int row=0,int image=0)
224        {
225            if (!_data) return NULL;
226            return _data+(column*getPixelSizeInBits())/8+row*getRowSizeInBytes()+image*getImageSizeInBytes();
227        }
228       
229        inline const unsigned char* data(int column, int row=0,int image=0) const
230        {
231            if (!_data) return NULL;
232            return _data+(column*getPixelSizeInBits())/8+row*getRowSizeInBytes()+image*getImageSizeInBytes();
233        }
234
235        /** Get the color value for specified texcoord.*/
236        Vec4 getColor(unsigned int s,unsigned t=0,unsigned r=0) const;
237
238        /** Get the color value for specified texcoord.*/
239        Vec4 getColor(const Vec2& texcoord) const { return getColor(Vec3(texcoord.x(),texcoord.y(),0.0f)); }
240
241        /** Get the color value for specified texcoord.*/
242        Vec4 getColor(const Vec3& texcoord) const;
243
244
245        /** Flip the image horizontally. */
246        void flipHorizontal();
247       
248        /** Flip the image vertically. */
249        void flipVertical();
250
251
252        /** Ensure image dimensions are a power of two.
253          * Mipmapped textures require the image dimensions to be
254          * power of two and are within the maxiumum texture size for
255          * the host machine.
256        */
257        void ensureValidSizeForTexturing(GLint maxTextureSize);
258
259        static bool isPackedType(GLenum type);
260        static GLenum computePixelFormat(GLenum pixelFormat);
261        static GLenum computeFormatDataType(GLenum pixelFormat);
262        static unsigned int computeNumComponents(GLenum pixelFormat);
263        static unsigned int computePixelSizeInBits(GLenum pixelFormat,GLenum type);
264        static unsigned int computeRowWidthInBytes(int width,GLenum pixelFormat,GLenum type,int packing);
265        static int computeNearestPowerOfTwo(int s,float bias=0.5f);
266        static int computeNumberOfMipmapLevels(int s,int t = 1, int r = 1);
267               
268        /** Precomputed mipmaps stuff. */
269        typedef std::vector< unsigned int > MipmapDataType;
270
271        inline bool isMipmap() const {return !_mipmapData.empty();};
272
273        unsigned int getNumMipmapLevels() const
274        {
275            return static_cast<unsigned int>(_mipmapData.size())+1;
276        };
277
278        /** Send offsets into data. It is assumed that first mipmap offset (index 0) is 0.*/
279        inline void setMipmapLevels(const MipmapDataType& mipmapDataVector) { _mipmapData = mipmapDataVector; }
280       
281        inline const MipmapDataType& getMipmapLevels() const { return _mipmapData; }
282
283        inline unsigned int getMipmapOffset(unsigned int mipmapLevel) const
284        {
285            if(mipmapLevel == 0)
286                return 0;
287            else if (mipmapLevel < getNumMipmapLevels())
288               return _mipmapData[mipmapLevel-1];
289            return 0;
290        };
291       
292        inline unsigned char* getMipmapData(unsigned int mipmapLevel)
293        {
294           return _data+getMipmapOffset(mipmapLevel);
295        }
296
297        inline const unsigned char* getMipmapData(unsigned int mipmapLevel) const
298        {
299           return _data+getMipmapOffset(mipmapLevel);
300        }
301
302        /*inline const unsigned char* getMipmapData(unsigned int row, unsigned int column, unsigned int mipmapLevel) const
303        {
304           if (!_data) return NULL;
305           return getMipmapData(mipmapLevel) + (column*getPixelSizeInBits())/8+row*getRowSizeInBytes();
306        }*/
307
308        /** Return true if this image is translucent - i.e. it has alpha values that are less 1.0 (when normalized). */
309        virtual bool isImageTranslucent() const;
310
311        /** Set the optional PixelBufferObject used to map the image memory efficiently to graphics memory. */
312        void setPixelBufferObject(PixelBufferObject* buffer) { setBufferObject(buffer); }
313
314        /** Get the PixelBufferObject.*/
315        PixelBufferObject* getPixelBufferObject() { return dynamic_cast<PixelBufferObject*>(_bufferObject.get()); }
316
317        /** Get the const PixelBufferObject.*/
318        const PixelBufferObject* getPixelBufferObject() const { return dynamic_cast<const PixelBufferObject*>(_bufferObject.get()); }
319       
320        virtual void update(NodeVisitor* /*nv*/) {}
321
322        /** 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. */
323        virtual bool sendPointerEvent(int /*x*/, int /*y*/, int /*buttonMask*/) { return false; }
324
325        /** 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.*/
326        virtual bool sendKeyEvent(int /*key*/, bool /*keyDown*/) { return false; }
327
328        /** method for passing frame information to the custom Image classes, to be called only when objects associated with imagery are not culled.*/
329        virtual void setFrameLastRendered(const osg::FrameStamp* /*frameStamp*/) {}
330
331    protected :
332
333        virtual ~Image();
334
335        Image& operator = (const Image&) { return *this; }
336
337        std::string _fileName;
338        WriteHint   _writeHint;
339
340
341        Origin _origin;
342
343        int _s, _t, _r;
344        GLint _internalTextureFormat;
345        GLenum _pixelFormat;
346        GLenum _dataType;
347        unsigned int _packing;
348        float _pixelAspectRatio;
349
350        AllocationMode _allocationMode;
351        unsigned char* _data;
352       
353        void deallocateData();
354       
355        void setData(unsigned char* data,AllocationMode allocationMode);
356
357        MipmapDataType _mipmapData;
358       
359        ref_ptr<PixelBufferObject> _bufferObject;
360};
361
362class Geode;
363
364/** Convenience function to be used by image loaders to generate a valid geode
365  * to return for readNode().
366  * Use the image's s and t values to scale the dimensions of the image.
367*/
368extern OSG_EXPORT Geode* createGeodeForImage(Image* image);
369/** Convenience function to be used by image loaders to generate a valid geode
370  * to return for readNode().
371  * Use the specified s and t values to scale the dimensions of the image.
372*/
373extern OSG_EXPORT Geode* createGeodeForImage(Image* image,float s,float t);
374
375}
376
377#endif                                            // __SG_IMAGE_H
Note: See TracBrowser for help on using the browser.