osgEarth 2.1.1

/home/cube/sources/osgearth/src/osgEarth/ImageUtils

Go to the documentation of this file.
00001 /* -*-c++-*- */
00002 /* osgEarth - Dynamic map generation toolkit for OpenSceneGraph
00003  * Copyright 2008-2010 Pelican Mapping
00004  * http://osgearth.org
00005  *
00006  * osgEarth is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU Lesser General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public License
00017  * along with this program.  If not, see <http://www.gnu.org/licenses/>
00018  */
00019 
00020 #ifndef OSGEARTH_IMAGEUTILS_H
00021 #define OSGEARTH_IMAGEUTILS_H
00022 
00023 #include <osgEarth/Common>
00024 #include <osg/Image>
00025 #include <osg/GL>
00026 
00027 //These formats were not added to OSG until after 2.8.3 so we need to define them to use them.
00028 #ifndef GL_EXT_texture_compression_rgtc
00029   #define GL_COMPRESSED_RED_RGTC1_EXT                0x8DBB
00030   #define GL_COMPRESSED_SIGNED_RED_RGTC1_EXT         0x8DBC
00031   #define GL_COMPRESSED_RED_GREEN_RGTC2_EXT          0x8DBD
00032   #define GL_COMPRESSED_SIGNED_RED_GREEN_RGTC2_EXT   0x8DBE
00033 #endif
00034 
00035 #ifndef GL_IMG_texture_compression_pvrtc
00036     #define GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG      0x8C00
00037     #define GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG      0x8C01
00038     #define GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG     0x8C02
00039     #define GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG     0x8C03
00040 #endif
00041 
00042 namespace osgEarth
00043 {
00044     class OSGEARTH_EXPORT ImageUtils
00045     {
00046     public:
00054         static osg::Image* cloneImage( const osg::Image* image );
00055 
00063         static void normalizeImage( osg::Image* image );
00064 
00068         static bool copyAsSubImage(
00069             const osg::Image* src, 
00070             osg::Image* dst, 
00071             int dst_start_col, int dst_start_row, int dst_start_img=0 );
00072 
00086         static bool resizeImage(
00087             const osg::Image* input, 
00088             unsigned int new_s, unsigned int new_t,
00089             osg::ref_ptr<osg::Image>& output,
00090             unsigned int mipmapLevel =0 );
00091 
00115         static osg::Image* cropImage(
00116             const osg::Image* image,
00117             double src_minx, double src_miny, double src_maxx, double src_maxy,
00118             double &dst_minx, double &dst_miny, double &dst_maxx, double &dst_maxy);
00119 
00127         static osg::Image* createMipmapBlendedImage(
00128             const osg::Image* primary,
00129             const osg::Image* secondary );
00130 
00135         static bool mix( osg::Image* dest, const osg::Image* src, float a );
00136 
00141         static osg::Image* sharpenImage( const osg::Image* image );
00142 
00146         static bool isPowerOfTwo(const osg::Image* image);
00147 
00151         static osg::Image* createEmptyImage();
00152 
00157         static bool canConvert(const osg::Image* image, GLenum pixelFormat, GLenum dataType);
00158 
00162         static osg::Image* convert(const osg::Image* image, GLenum pixelFormat, GLenum dataType);
00163 
00167                 static osg::Image* convertToRGB8(const osg::Image* image);
00168 
00172                 static osg::Image* convertToRGBA8(const osg::Image* image);
00173 
00177                 static bool areEquivalent(const osg::Image *lhs, const osg::Image *rhs);
00178 
00182         static bool areRGBEquivalent( const osg::Vec4& lhs, const osg::Vec4& rhs, float epsilon =0.01f ) {
00183             return
00184                 fabs(lhs.r() - rhs.r()) < epsilon &&
00185                 fabs(lhs.g() - rhs.g()) < epsilon &&
00186                 fabs(lhs.b() - rhs.b()) < epsilon;
00187         }
00188 
00192         static bool hasAlphaChannel( const osg::Image* image );
00193 
00197         static bool isCompressed( const osg::Image* image );
00198 
00202         class OSGEARTH_EXPORT PixelReader
00203         {
00204         public:
00205             PixelReader(const osg::Image* image);
00206 
00208             static bool supports( GLenum pixelFormat, GLenum dataType );
00209 
00211             static bool supports( const osg::Image* image ) {
00212                 return image && supports(image->getPixelFormat(), image->getDataType() );
00213             }
00214 
00216             osg::Vec4 operator()(int s, int t, int r=0, int m=0) const {
00217                 return (*_reader)(this, s, t, r, m);
00218             }
00219 
00220             // internals:
00221             const unsigned char* data(int s=0, int t=0, int r=0, int m=0) const {
00222                 return m == 0 ?
00223                     _image->data() + s*_colMult + t*_rowMult + r*_imageSize :
00224                     _image->getMipmapData(m) + s*_colMult + t*(_rowMult >> m) + r*(_imageSize>>m) ;
00225             }
00226 
00227             typedef osg::Vec4 (*ReaderFunc)(const PixelReader* ia, int s, int t, int r, int m);
00228             ReaderFunc _reader;
00229             const osg::Image* _image;
00230             unsigned _colMult;
00231             unsigned _rowMult;
00232             unsigned _imageSize;
00233         };
00234         
00238         class OSGEARTH_EXPORT PixelWriter
00239         {
00240         public:
00241             PixelWriter(osg::Image* image);
00242 
00244             static bool supports( GLenum pixelFormat, GLenum dataType );
00245 
00247             static bool supports( const osg::Image* image ) {
00248                 return image && supports(image->getPixelFormat(), image->getDataType() );
00249             }
00250 
00252             void operator()(const osg::Vec4& c, int s, int t, int r=0, int m=0) {
00253                 (*_writer)(this, c, s, t, r, m );
00254             }
00255 
00256             // internals:
00257             osg::Image* _image;
00258             unsigned _colMult;
00259             unsigned _rowMult;
00260             unsigned _imageSize;
00261 
00262             unsigned char* data(int s=0, int t=0, int r=0, int m=0) const {
00263                 return m == 0 ?
00264                     _image->data() + s*_colMult + t*_rowMult + r*_imageSize :
00265                     _image->getMipmapData(m) + s*_colMult + t*(_rowMult >> m) + r*(_imageSize>>m);
00266             }
00267 
00268             typedef void (*WriterFunc)(const PixelWriter* iw, const osg::Vec4& c, int s, int t, int r, int m);
00269             WriterFunc _writer;
00270         };
00271 
00275         template<typename T>
00276         struct PixelVisitor : public T
00277         {
00285             void accept( osg::Image* image ) {
00286                 PixelReader _reader( image );
00287                 PixelWriter _writer( image );
00288                 for( int r=0; r<image->r(); ++r ) {
00289                     for( int t=0; t<image->t(); ++t ) {
00290                         for( int s=0; s<image->s(); ++s ) {
00291                             osg::Vec4f pixel = _reader(s,t,r);
00292                             if ( (*this)(pixel) )
00293                                 _writer(pixel,s,t,r);
00294                         }
00295                     }
00296                 }
00297             }          
00298 
00307             void accept( const osg::Image* src, osg::Image* dest ) {
00308                 PixelReader _readerSrc( src );
00309                 PixelReader _readerDest( dest );
00310                 PixelWriter _writerDest( dest );
00311                 for( int r=0; r<src->r(); ++r ) {
00312                     for( int t=0; t<src->t(); ++t ) {
00313                         for( int s=0; s<src->s(); ++s ) {
00314                             const osg::Vec4f pixelSrc = _readerSrc(s,t,r);
00315                             osg::Vec4f pixelDest = _readerDest(s,t,r);
00316                             if ( (*this)(pixelSrc, pixelDest) )
00317                                 _writerDest(pixelDest,s,t,r);
00318                         }
00319                     }
00320                 }
00321             }
00322         };
00323 
00330         struct CopyImage {
00331             bool operator()( const osg::Vec4f& src, osg::Vec4f& dest ) {
00332                 dest = src;
00333                 return true;
00334             }
00335         };
00336     };
00337 }
00338 
00339 #endif //OSGEARTH_IMAGEUTILS_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines