root/OpenSceneGraph/trunk/include/osg/ImageUtils @ 13205

Revision 13205, 9.6 kB (checked in by robert, 14 minutes ago)

From Paul Martz, fixed placement of OpenGL header so that it gets generated and placed in the build directory as per the Config file

  • 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_IMAGEUTILS
15#define OSG_IMAGEUTILS 1
16
17#include <osg/Export>
18
19#include <osg/Image>
20
21namespace osg {
22
23template <typename T, class O>
24void _readRow(unsigned int num, GLenum pixelFormat, const T* data,float scale, O& operation)
25{
26    switch(pixelFormat)
27    {
28        case(GL_LUMINANCE):         { for(unsigned int i=0;i<num;++i) { float l = float(*data++)*scale; operation.luminance(l); } }  break;
29        case(GL_ALPHA):             { for(unsigned int i=0;i<num;++i) { float a = float(*data++)*scale; operation.alpha(a); } }  break;
30        case(GL_LUMINANCE_ALPHA):   { for(unsigned int i=0;i<num;++i) { float l = float(*data++)*scale; float a = float(*data++)*scale; operation.luminance_alpha(l,a); } }  break;
31        case(GL_RGB):               { for(unsigned int i=0;i<num;++i) { float r = float(*data++)*scale; float g = float(*data++)*scale; float b = float(*data++)*scale; operation.rgb(r,g,b); } }  break;
32        case(GL_RGBA):              { for(unsigned int i=0;i<num;++i) { float r = float(*data++)*scale; float g = float(*data++)*scale; float b = float(*data++)*scale; float a = float(*data++)*scale; operation.rgba(r,g,b,a); } }  break;
33        case(GL_BGR):               { for(unsigned int i=0;i<num;++i) { float b = float(*data++)*scale; float g = float(*data++)*scale; float r = float(*data++)*scale; operation.rgb(r,g,b); } }  break;
34        case(GL_BGRA):              { for(unsigned int i=0;i<num;++i) { float b = float(*data++)*scale; float g = float(*data++)*scale; float r = float(*data++)*scale; float a = float(*data++)*scale; operation.rgba(r,g,b,a); } }  break;
35    }
36}
37
38template <class O>
39void readRow(unsigned int num, GLenum pixelFormat, GLenum dataType, const unsigned char* data, O& operation)
40{
41    switch(dataType)
42    {
43        case(GL_BYTE):              _readRow(num,pixelFormat, (const char*)data,            1.0f/128.0f,        operation); break;
44        case(GL_UNSIGNED_BYTE):     _readRow(num,pixelFormat, (const unsigned char*)data,   1.0f/255.0f,        operation); break;
45        case(GL_SHORT):             _readRow(num,pixelFormat, (const short*) data,          1.0f/32768.0f,      operation); break;
46        case(GL_UNSIGNED_SHORT):    _readRow(num,pixelFormat, (const unsigned short*)data,  1.0f/65535.0f,      operation); break;
47        case(GL_INT):               _readRow(num,pixelFormat, (const int*) data,            1.0f/2147483648.0f, operation); break;
48        case(GL_UNSIGNED_INT):      _readRow(num,pixelFormat, (const unsigned int*) data,   1.0f/4294967295.0f, operation); break;
49        case(GL_FLOAT):             _readRow(num,pixelFormat, (const float*) data,          1.0f,               operation); break;
50    }
51}
52
53template <class O>
54void readImage(const osg::Image* image, O& operation)
55{
56    if (!image) return;
57
58    for(int r=0;r<image->r();++r)
59    {
60        for(int t=0;t<image->t();++t)
61        {
62            readRow(image->s(), image->getPixelFormat(), image->getDataType(), image->data(0,t,r), operation);
63        }
64    }
65}
66
67// example ModifyOperator
68// struct ModifyOperator
69// {
70//     inline void luminance(float& l) const {}
71//     inline void alpha(float& a) const {}
72//     inline void luminance_alpha(float& l,float& a) const {}
73//     inline void rgb(float& r,float& g,float& b) const {}
74//     inline void rgba(float& r,float& g,float& b,float& a) const {}
75// };
76
77
78template <typename T, class M>
79void _modifyRow(unsigned int num, GLenum pixelFormat, T* data,float scale, const M& operation)
80{
81    float inv_scale = 1.0f/scale;
82    switch(pixelFormat)
83    {
84        case(GL_LUMINANCE):         { for(unsigned int i=0;i<num;++i) { float l = float(*data)*scale; operation.luminance(l); *data++ = T(l*inv_scale); } }  break;
85        case(GL_ALPHA):             { for(unsigned int i=0;i<num;++i) { float a = float(*data)*scale; operation.alpha(a); *data++ = T(a*inv_scale); } }  break;
86        case(GL_LUMINANCE_ALPHA):   { for(unsigned int i=0;i<num;++i) { float l = float(*data)*scale; float a = float(*(data+1))*scale; operation.luminance_alpha(l,a); *data++ = T(l*inv_scale); *data++ = T(a*inv_scale); } }  break;
87        case(GL_RGB):               { for(unsigned int i=0;i<num;++i) { float r = float(*data)*scale; float g = float(*(data+1))*scale; float b = float(*(data+2))*scale; operation.rgb(r,g,b); *data++ = T(r*inv_scale); *data++ = T(g*inv_scale); *data++ = T(b*inv_scale); } }  break;
88        case(GL_RGBA):              { for(unsigned int i=0;i<num;++i) { float r = float(*data)*scale; float g = float(*(data+1))*scale; float b = float(*(data+2))*scale; float a = float(*(data+3))*scale; operation.rgba(r,g,b,a); *data++ = T(r*inv_scale); *data++ = T(g*inv_scale); *data++ = T(b*inv_scale); *data++ = T(a*inv_scale); } }  break;
89        case(GL_BGR):               { for(unsigned int i=0;i<num;++i) { float b = float(*data)*scale; float g = float(*(data+1))*scale; float r = float(*(data+2))*scale; operation.rgb(r,g,b); *data++ = T(b*inv_scale); *data++ = T(g*inv_scale); *data++ = T(r*inv_scale); } }  break;
90        case(GL_BGRA):              { for(unsigned int i=0;i<num;++i) { float b = float(*data)*scale; float g = float(*(data+1))*scale; float r = float(*(data+2))*scale; float a = float(*(data+3))*scale; operation.rgba(r,g,b,a); *data++ = T(b*inv_scale); *data++ = T(g*inv_scale); *data++ = T(r*inv_scale); *data++ = T(a*inv_scale); } }  break;
91    }
92}
93
94template <class M>
95void modifyRow(unsigned int num, GLenum pixelFormat, GLenum dataType, unsigned char* data, const M& operation)
96{
97    switch(dataType)
98    {
99        case(GL_BYTE):              _modifyRow(num,pixelFormat, (char*)data,            1.0f/128.0f,        operation); break;
100        case(GL_UNSIGNED_BYTE):     _modifyRow(num,pixelFormat, (unsigned char*)data,   1.0f/255.0f,        operation); break;
101        case(GL_SHORT):             _modifyRow(num,pixelFormat, (short*) data,          1.0f/32768.0f,      operation); break;
102        case(GL_UNSIGNED_SHORT):    _modifyRow(num,pixelFormat, (unsigned short*)data,  1.0f/65535.0f,      operation); break;
103        case(GL_INT):               _modifyRow(num,pixelFormat, (int*) data,            1.0f/2147483648.0f, operation); break;
104        case(GL_UNSIGNED_INT):      _modifyRow(num,pixelFormat, (unsigned int*) data,   1.0f/4294967295.0f, operation); break;
105        case(GL_FLOAT):             _modifyRow(num,pixelFormat, (float*) data,          1.0f,               operation); break;
106    }
107}
108
109template <class M>
110void modifyImage(osg::Image* image, const M& operation)
111{
112    if (!image) return;
113
114    for(int r=0;r<image->r();++r)
115    {
116        for(int t=0;t<image->t();++t)
117        {
118            modifyRow(image->s(), image->getPixelFormat(), image->getDataType(), image->data(0,t,r), operation);
119        }
120    }
121}
122
123/** Compute the min max colour values in the image.*/
124extern OSG_EXPORT bool computeMinMax(const osg::Image* image, osg::Vec4& min, osg::Vec4& max);
125
126/** Compute the min max colour values in the image.*/
127extern OSG_EXPORT bool offsetAndScaleImage(osg::Image* image, const osg::Vec4& offset, const osg::Vec4& scale);
128
129/** Compute source image to destination image.*/
130extern OSG_EXPORT bool copyImage(const osg::Image* srcImage, int src_s, int src_t, int src_r, int width, int height, int depth,
131                                       osg::Image* destImage, int dest_s, int dest_t, int dest_r, bool doRescale = false);
132
133/** Compute the min max colour values in the image.*/
134extern OSG_EXPORT bool clearImageToColor(osg::Image* image, const osg::Vec4& colour);
135
136typedef std::vector< osg::ref_ptr<osg::Image> > ImageList;
137
138/** Search through the list of Images and find the maximum number of components used amoung the images.*/
139extern OSG_EXPORT unsigned int maximimNumOfComponents(const ImageList& imageList);
140
141/** create a 3D osg::Image from a list of osg::Image.*/
142extern OSG_EXPORT osg::Image* createImage3D(const ImageList& imageList,
143            GLenum desiredPixelFormat,
144            int s_maximumImageSize = 1024,
145            int t_maximumImageSize = 1024,
146            int r_maximumImageSize = 1024,
147            bool resizeToPowerOfTwo = false);
148
149/** create a 3D osg::Image from a list of osg::Image.*/
150extern OSG_EXPORT osg::Image* createImage3DWithAlpha(const ImageList& imageList,
151            int s_maximumImageSize = 1024,
152            int t_maximumImageSize = 1024,
153            int r_maximumImageSize = 1024,
154            bool resizeToPowerOfTwo = false);
155
156
157
158
159/** create a 2D osg::Image that provides a point at the center of the image.
160 *  The colour across th image is computed from a balance between the center color and the background color controlled by the power of the radius from the center.*/
161extern OSG_EXPORT osg::Image* createSpotLightImage(const osg::Vec4& centerColour, const osg::Vec4& backgroudColour, unsigned int size, float power);
162
163
164enum ColorSpaceOperation
165{
166    NO_COLOR_SPACE_OPERATION,
167    MODULATE_ALPHA_BY_LUMINANCE,
168    MODULATE_ALPHA_BY_COLOR,
169    REPLACE_ALPHA_WITH_LUMINANCE,
170    REPLACE_RGB_WITH_LUMINANCE
171};
172
173/** Convert the RGBA values in a Image based on a ColorSpaceOperation defined scheme.*/
174extern osg::Image* colorSpaceConversion(ColorSpaceOperation op, osg::Image* image, const osg::Vec4& colour);
175
176
177}
178
179
180#endif
Note: See TracBrowser for help on using the browser.