2006-07-18 23:21:48 +08:00
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2003-01-22 00:45:36 +08:00
*
* This library is open source and may be redistributed and / or modified under
* the terms of the OpenSceneGraph Public License ( OSGPL ) version 0.0 or
* ( at your option ) any later version . The full license is in LICENSE file
* included with this distribution , and on the openscenegraph . org website .
*
* This library is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* OpenSceneGraph Public License for more details .
*/
2001-10-04 05:44:07 +08:00
# include <osg/GL>
# include <osg/GLU>
# include <osg/Image>
# include <osg/Notify>
2001-01-11 00:32:10 +08:00
# include <osg/Geode>
2002-06-26 04:27:51 +08:00
# include <osg/Geometry>
2001-09-20 05:08:56 +08:00
# include <osg/StateSet>
2002-08-25 03:39:39 +08:00
# include <osg/Texture2D>
2004-09-29 18:01:46 +08:00
# include <osg/Texture3D>
2007-09-07 19:21:02 +08:00
# include <osg/Texture2DArray>
2001-01-11 00:32:10 +08:00
2004-08-17 04:57:24 +08:00
# include "dxtctool.h"
2001-09-20 05:08:56 +08:00
2001-01-11 00:32:10 +08:00
using namespace osg ;
2002-02-23 01:12:10 +08:00
using namespace std ;
2001-01-11 00:32:10 +08:00
Image : : Image ( )
2007-08-22 18:34:11 +08:00
: Object ( true )
2001-01-11 00:32:10 +08:00
{
2004-07-27 18:11:45 +08:00
setDataVariance ( STATIC ) ;
2002-04-12 01:15:07 +08:00
_fileName = " " ;
2007-06-13 00:55:44 +08:00
_origin = BOTTOM_LEFT ;
2002-04-12 01:15:07 +08:00
_s = _t = _r = 0 ;
_internalTextureFormat = 0 ;
_pixelFormat = ( unsigned int ) 0 ;
_dataType = ( unsigned int ) 0 ;
_packing = 4 ;
2001-01-11 00:32:10 +08:00
2003-02-25 19:56:18 +08:00
_allocationMode = USE_NEW_DELETE ;
_data = ( unsigned char * ) 0L ;
2001-11-19 05:31:16 +08:00
2005-02-09 18:39:45 +08:00
_modifiedCount = 0 ;
2001-01-11 00:32:10 +08:00
}
2002-01-29 22:04:06 +08:00
Image : : Image ( const Image & image , const CopyOp & copyop ) :
Object ( image , copyop ) ,
Added support for shallow and deep copy of nodes, drawables and state, via a
copy constructor which takes an optional Cloner object, and the old
osg::Object::clone() has changed so that it now requires a Cloner as paramter.
This is passed on to the copy constructor to help control the shallow vs
deep copying. The old functionality of clone() which was clone of type has
been renamed to cloneType().
Updated all of the OSG to work with these new conventions, implemention all
the required copy constructors etc. A couple of areas will do shallow
copies by design, a couple of other still need to be updated to do either
shallow or deep.
Neither of the shallow or deep copy operations have been tested yet, only
the old functionality of the OSG has been checked so far, such running the
viewer on various demo datasets.
Also fixed a problem in osg::Optimize::RemoveRendundentNodesVisitor which
was not checking that Group didn't have have any attached StateSet's, Callbacks
or UserData. These checks have now been added, which fixes a bug which was
revealled by the new osgscribe demo, this related to removal of group acting
as state decorator.
method
2002-01-29 05:17:01 +08:00
_fileName ( image . _fileName ) ,
2007-06-13 00:55:44 +08:00
_origin ( image . _origin ) ,
Added support for shallow and deep copy of nodes, drawables and state, via a
copy constructor which takes an optional Cloner object, and the old
osg::Object::clone() has changed so that it now requires a Cloner as paramter.
This is passed on to the copy constructor to help control the shallow vs
deep copying. The old functionality of clone() which was clone of type has
been renamed to cloneType().
Updated all of the OSG to work with these new conventions, implemention all
the required copy constructors etc. A couple of areas will do shallow
copies by design, a couple of other still need to be updated to do either
shallow or deep.
Neither of the shallow or deep copy operations have been tested yet, only
the old functionality of the OSG has been checked so far, such running the
viewer on various demo datasets.
Also fixed a problem in osg::Optimize::RemoveRendundentNodesVisitor which
was not checking that Group didn't have have any attached StateSet's, Callbacks
or UserData. These checks have now been added, which fixes a bug which was
revealled by the new osgscribe demo, this related to removal of group acting
as state decorator.
method
2002-01-29 05:17:01 +08:00
_s ( image . _s ) , _t ( image . _t ) , _r ( image . _r ) ,
2002-04-12 01:15:07 +08:00
_internalTextureFormat ( image . _internalTextureFormat ) ,
Added support for shallow and deep copy of nodes, drawables and state, via a
copy constructor which takes an optional Cloner object, and the old
osg::Object::clone() has changed so that it now requires a Cloner as paramter.
This is passed on to the copy constructor to help control the shallow vs
deep copying. The old functionality of clone() which was clone of type has
been renamed to cloneType().
Updated all of the OSG to work with these new conventions, implemention all
the required copy constructors etc. A couple of areas will do shallow
copies by design, a couple of other still need to be updated to do either
shallow or deep.
Neither of the shallow or deep copy operations have been tested yet, only
the old functionality of the OSG has been checked so far, such running the
viewer on various demo datasets.
Also fixed a problem in osg::Optimize::RemoveRendundentNodesVisitor which
was not checking that Group didn't have have any attached StateSet's, Callbacks
or UserData. These checks have now been added, which fixes a bug which was
revealled by the new osgscribe demo, this related to removal of group acting
as state decorator.
method
2002-01-29 05:17:01 +08:00
_pixelFormat ( image . _pixelFormat ) ,
_dataType ( image . _dataType ) ,
_packing ( image . _packing ) ,
_data ( 0L ) ,
2005-02-09 18:39:45 +08:00
_modifiedCount ( image . _modifiedCount ) ,
2002-04-23 22:58:33 +08:00
_mipmapData ( image . _mipmapData )
Added support for shallow and deep copy of nodes, drawables and state, via a
copy constructor which takes an optional Cloner object, and the old
osg::Object::clone() has changed so that it now requires a Cloner as paramter.
This is passed on to the copy constructor to help control the shallow vs
deep copying. The old functionality of clone() which was clone of type has
been renamed to cloneType().
Updated all of the OSG to work with these new conventions, implemention all
the required copy constructors etc. A couple of areas will do shallow
copies by design, a couple of other still need to be updated to do either
shallow or deep.
Neither of the shallow or deep copy operations have been tested yet, only
the old functionality of the OSG has been checked so far, such running the
viewer on various demo datasets.
Also fixed a problem in osg::Optimize::RemoveRendundentNodesVisitor which
was not checking that Group didn't have have any attached StateSet's, Callbacks
or UserData. These checks have now been added, which fixes a bug which was
revealled by the new osgscribe demo, this related to removal of group acting
as state decorator.
method
2002-01-29 05:17:01 +08:00
{
if ( image . _data )
{
2004-08-04 02:06:36 +08:00
int size = image . getTotalSizeInBytesIncludingMipmaps ( ) ;
2003-02-25 19:56:18 +08:00
setData ( new unsigned char [ size ] , USE_NEW_DELETE ) ;
Added support for shallow and deep copy of nodes, drawables and state, via a
copy constructor which takes an optional Cloner object, and the old
osg::Object::clone() has changed so that it now requires a Cloner as paramter.
This is passed on to the copy constructor to help control the shallow vs
deep copying. The old functionality of clone() which was clone of type has
been renamed to cloneType().
Updated all of the OSG to work with these new conventions, implemention all
the required copy constructors etc. A couple of areas will do shallow
copies by design, a couple of other still need to be updated to do either
shallow or deep.
Neither of the shallow or deep copy operations have been tested yet, only
the old functionality of the OSG has been checked so far, such running the
viewer on various demo datasets.
Also fixed a problem in osg::Optimize::RemoveRendundentNodesVisitor which
was not checking that Group didn't have have any attached StateSet's, Callbacks
or UserData. These checks have now been added, which fixes a bug which was
revealled by the new osgscribe demo, this related to removal of group acting
as state decorator.
method
2002-01-29 05:17:01 +08:00
memcpy ( _data , image . _data , size ) ;
}
2002-04-23 05:13:33 +08:00
Added support for shallow and deep copy of nodes, drawables and state, via a
copy constructor which takes an optional Cloner object, and the old
osg::Object::clone() has changed so that it now requires a Cloner as paramter.
This is passed on to the copy constructor to help control the shallow vs
deep copying. The old functionality of clone() which was clone of type has
been renamed to cloneType().
Updated all of the OSG to work with these new conventions, implemention all
the required copy constructors etc. A couple of areas will do shallow
copies by design, a couple of other still need to be updated to do either
shallow or deep.
Neither of the shallow or deep copy operations have been tested yet, only
the old functionality of the OSG has been checked so far, such running the
viewer on various demo datasets.
Also fixed a problem in osg::Optimize::RemoveRendundentNodesVisitor which
was not checking that Group didn't have have any attached StateSet's, Callbacks
or UserData. These checks have now been added, which fixes a bug which was
revealled by the new osgscribe demo, this related to removal of group acting
as state decorator.
method
2002-01-29 05:17:01 +08:00
}
2001-01-11 00:32:10 +08:00
Image : : ~ Image ( )
{
2003-02-25 19:56:18 +08:00
deallocateData ( ) ;
2001-01-11 00:32:10 +08:00
}
2003-02-25 19:56:18 +08:00
void Image : : deallocateData ( )
{
if ( _data ) {
if ( _allocationMode = = USE_NEW_DELETE ) delete [ ] _data ;
else if ( _allocationMode = = USE_MALLOC_FREE ) : : free ( _data ) ;
_data = 0 ;
}
}
2001-01-11 00:32:10 +08:00
2003-03-11 21:30:03 +08:00
int Image : : compare ( const Image & rhs ) const
{
2005-03-07 22:30:55 +08:00
// if at least one filename is empty, then need to test buffer
// pointers because images could have been created on the fly
// and therefore we can't rely on file names to get an accurate
// comparison
if ( getFileName ( ) . empty ( ) | | rhs . getFileName ( ) . empty ( ) )
{
if ( _data < rhs . _data ) return - 1 ;
if ( _data > rhs . _data ) return 1 ;
}
2003-03-11 21:30:03 +08:00
// need to test against image contents here...
COMPARE_StateAttribute_Parameter ( _s )
COMPARE_StateAttribute_Parameter ( _t )
COMPARE_StateAttribute_Parameter ( _internalTextureFormat )
COMPARE_StateAttribute_Parameter ( _pixelFormat )
COMPARE_StateAttribute_Parameter ( _dataType )
COMPARE_StateAttribute_Parameter ( _packing )
2005-03-07 22:30:55 +08:00
COMPARE_StateAttribute_Parameter ( _mipmapData )
2005-02-09 18:39:45 +08:00
COMPARE_StateAttribute_Parameter ( _modifiedCount )
2003-03-11 21:30:03 +08:00
2005-03-07 22:30:55 +08:00
// same buffer + same parameters = same image
2005-11-17 17:20:58 +08:00
if ( ( _data | | rhs . _data ) & & ( _data = = rhs . _data ) ) return 0 ;
2005-03-07 22:30:55 +08:00
// slowest comparison at the bottom!
COMPARE_StateAttribute_Parameter ( getFileName ( ) )
2003-03-11 21:30:03 +08:00
return 0 ;
}
2001-09-20 05:08:56 +08:00
void Image : : setFileName ( const std : : string & fileName )
2001-01-11 00:32:10 +08:00
{
2001-09-20 05:08:56 +08:00
_fileName = fileName ;
2001-01-11 00:32:10 +08:00
}
2003-02-25 19:56:18 +08:00
void Image : : setData ( unsigned char * data , AllocationMode mode )
{
deallocateData ( ) ;
_data = data ;
_allocationMode = mode ;
}
2002-04-11 06:10:07 +08:00
2002-09-02 20:31:35 +08:00
bool Image : : isPackedType ( GLenum type )
2002-04-12 01:15:07 +08:00
{
2002-05-14 17:34:11 +08:00
switch ( type )
2002-04-12 01:15:07 +08:00
{
case ( GL_UNSIGNED_BYTE_3_3_2 ) :
case ( GL_UNSIGNED_BYTE_2_3_3_REV ) :
case ( GL_UNSIGNED_SHORT_5_6_5 ) :
case ( GL_UNSIGNED_SHORT_5_6_5_REV ) :
case ( GL_UNSIGNED_SHORT_4_4_4_4 ) :
case ( GL_UNSIGNED_SHORT_4_4_4_4_REV ) :
case ( GL_UNSIGNED_SHORT_5_5_5_1 ) :
case ( GL_UNSIGNED_SHORT_1_5_5_5_REV ) :
case ( GL_UNSIGNED_INT_8_8_8_8 ) :
case ( GL_UNSIGNED_INT_8_8_8_8_REV ) :
case ( GL_UNSIGNED_INT_10_10_10_2 ) :
case ( GL_UNSIGNED_INT_2_10_10_10_REV ) : return true ;
default : return false ;
}
}
2006-10-15 05:50:29 +08:00
GLenum Image : : computePixelFormat ( GLenum format )
{
switch ( format )
{
case ( GL_ALPHA16F_ARB ) :
case ( GL_ALPHA32F_ARB ) :
return GL_ALPHA ;
case ( GL_LUMINANCE16F_ARB ) :
case ( GL_LUMINANCE32F_ARB ) :
return GL_LUMINANCE ;
case ( GL_INTENSITY16F_ARB ) :
case ( GL_INTENSITY32F_ARB ) :
return GL_INTENSITY ;
case ( GL_LUMINANCE_ALPHA16F_ARB ) :
case ( GL_LUMINANCE_ALPHA32F_ARB ) :
return GL_LUMINANCE_ALPHA ;
case ( GL_RGB32F_ARB ) :
case ( GL_RGB16F_ARB ) :
return GL_RGB ;
case ( GL_RGBA32F_ARB ) :
case ( GL_RGBA16F_ARB ) :
return GL_RGBA ;
2007-09-11 20:04:58 +08:00
case ( GL_ALPHA8I_EXT ) :
case ( GL_ALPHA16I_EXT ) :
case ( GL_ALPHA32I_EXT ) :
case ( GL_ALPHA8UI_EXT ) :
case ( GL_ALPHA16UI_EXT ) :
case ( GL_ALPHA32UI_EXT ) :
return GL_ALPHA_INTEGER_EXT ;
case ( GL_LUMINANCE8I_EXT ) :
case ( GL_LUMINANCE16I_EXT ) :
case ( GL_LUMINANCE32I_EXT ) :
case ( GL_LUMINANCE8UI_EXT ) :
case ( GL_LUMINANCE16UI_EXT ) :
case ( GL_LUMINANCE32UI_EXT ) :
return GL_LUMINANCE_INTEGER_EXT ;
case ( GL_INTENSITY8I_EXT ) :
case ( GL_INTENSITY16I_EXT ) :
case ( GL_INTENSITY32I_EXT ) :
case ( GL_INTENSITY8UI_EXT ) :
case ( GL_INTENSITY16UI_EXT ) :
case ( GL_INTENSITY32UI_EXT ) :
notify ( WARN ) < < " Image::computePixelFormat( " < < std : : hex < < format < < std : : dec < < " ) intensity pixel format is not correctly specified, so assume GL_LUMINANCE_INTEGER. " < < std : : endl ;
return GL_LUMINANCE_INTEGER_EXT ;
case ( GL_LUMINANCE_ALPHA8I_EXT ) :
case ( GL_LUMINANCE_ALPHA16I_EXT ) :
case ( GL_LUMINANCE_ALPHA32I_EXT ) :
case ( GL_LUMINANCE_ALPHA8UI_EXT ) :
case ( GL_LUMINANCE_ALPHA16UI_EXT ) :
case ( GL_LUMINANCE_ALPHA32UI_EXT ) :
return GL_LUMINANCE_ALPHA_INTEGER_EXT ; ;
case ( GL_RGB32I_EXT ) :
case ( GL_RGB16I_EXT ) :
case ( GL_RGB8I_EXT ) :
case ( GL_RGB32UI_EXT ) :
case ( GL_RGB16UI_EXT ) :
case ( GL_RGB8UI_EXT ) :
return GL_RGB_INTEGER_EXT ;
case ( GL_RGBA32I_EXT ) :
case ( GL_RGBA16I_EXT ) :
case ( GL_RGBA8I_EXT ) :
case ( GL_RGBA32UI_EXT ) :
case ( GL_RGBA16UI_EXT ) :
case ( GL_RGBA8UI_EXT ) :
return GL_RGBA_INTEGER_EXT ; ;
2006-10-15 05:50:29 +08:00
default :
return format ;
}
}
2003-09-22 17:13:22 +08:00
unsigned int Image : : computeNumComponents ( GLenum pixelFormat )
2002-04-12 01:15:07 +08:00
{
2003-09-22 17:13:22 +08:00
switch ( pixelFormat )
2002-04-12 01:15:07 +08:00
{
2003-04-03 02:26:34 +08:00
case ( GL_COMPRESSED_RGB_S3TC_DXT1_EXT ) : return 3 ;
2003-06-24 23:40:09 +08:00
case ( GL_COMPRESSED_RGBA_S3TC_DXT1_EXT ) : return 4 ;
2003-04-03 02:26:34 +08:00
case ( GL_COMPRESSED_RGBA_S3TC_DXT3_EXT ) : return 4 ;
case ( GL_COMPRESSED_RGBA_S3TC_DXT5_EXT ) : return 4 ;
2002-04-12 01:15:07 +08:00
case ( GL_COLOR_INDEX ) : return 1 ;
case ( GL_STENCIL_INDEX ) : return 1 ;
case ( GL_DEPTH_COMPONENT ) : return 1 ;
case ( GL_RED ) : return 1 ;
case ( GL_GREEN ) : return 1 ;
case ( GL_BLUE ) : return 1 ;
case ( GL_ALPHA ) : return 1 ;
2007-09-11 20:04:58 +08:00
case ( GL_ALPHA8I_EXT ) : return 1 ;
case ( GL_ALPHA8UI_EXT ) : return 1 ;
case ( GL_ALPHA16I_EXT ) : return 1 ;
case ( GL_ALPHA16UI_EXT ) : return 1 ;
case ( GL_ALPHA32I_EXT ) : return 1 ;
case ( GL_ALPHA32UI_EXT ) : return 1 ;
2006-10-15 05:50:29 +08:00
case ( GL_ALPHA16F_ARB ) : return 1 ;
case ( GL_ALPHA32F_ARB ) : return 1 ;
2002-04-12 01:15:07 +08:00
case ( GL_RGB ) : return 3 ;
case ( GL_BGR ) : return 3 ;
2007-09-11 20:04:58 +08:00
case ( GL_RGB8I_EXT ) : return 3 ;
case ( GL_RGB8UI_EXT ) : return 3 ;
case ( GL_RGB16I_EXT ) : return 3 ;
case ( GL_RGB16UI_EXT ) : return 3 ;
case ( GL_RGB32I_EXT ) : return 3 ;
case ( GL_RGB32UI_EXT ) : return 3 ;
2006-10-15 05:50:29 +08:00
case ( GL_RGB16F_ARB ) : return 3 ;
case ( GL_RGB32F_ARB ) : return 3 ;
2007-09-24 17:26:21 +08:00
case ( GL_RGBA16F_ARB ) : return 4 ;
case ( GL_RGBA32F_ARB ) : return 4 ;
2002-04-12 01:15:07 +08:00
case ( GL_RGBA ) : return 4 ;
case ( GL_BGRA ) : return 4 ;
case ( GL_LUMINANCE ) : return 1 ;
2007-03-06 19:20:39 +08:00
case ( GL_LUMINANCE4 ) : return 1 ;
case ( GL_LUMINANCE8 ) : return 1 ;
case ( GL_LUMINANCE12 ) : return 1 ;
case ( GL_LUMINANCE16 ) : return 1 ;
2007-09-11 20:04:58 +08:00
case ( GL_LUMINANCE8I_EXT ) : return 1 ;
case ( GL_LUMINANCE8UI_EXT ) : return 1 ;
case ( GL_LUMINANCE16I_EXT ) : return 1 ;
case ( GL_LUMINANCE16UI_EXT ) : return 1 ;
case ( GL_LUMINANCE32I_EXT ) : return 1 ;
case ( GL_LUMINANCE32UI_EXT ) : return 1 ;
2006-10-15 05:50:29 +08:00
case ( GL_LUMINANCE16F_ARB ) : return 1 ;
case ( GL_LUMINANCE32F_ARB ) : return 1 ;
2007-03-06 19:20:39 +08:00
case ( GL_LUMINANCE4_ALPHA4 ) : return 2 ;
case ( GL_LUMINANCE6_ALPHA2 ) : return 2 ;
case ( GL_LUMINANCE8_ALPHA8 ) : return 2 ;
case ( GL_LUMINANCE12_ALPHA4 ) : return 2 ;
case ( GL_LUMINANCE12_ALPHA12 ) : return 2 ;
case ( GL_LUMINANCE16_ALPHA16 ) : return 2 ;
2004-06-17 23:07:50 +08:00
case ( GL_INTENSITY ) : return 1 ;
2007-03-06 19:20:39 +08:00
case ( GL_INTENSITY4 ) : return 1 ;
case ( GL_INTENSITY8 ) : return 1 ;
case ( GL_INTENSITY12 ) : return 1 ;
case ( GL_INTENSITY16 ) : return 1 ;
2007-09-11 20:04:58 +08:00
case ( GL_INTENSITY8UI_EXT ) : return 1 ;
case ( GL_INTENSITY8I_EXT ) : return 1 ;
case ( GL_INTENSITY16I_EXT ) : return 1 ;
case ( GL_INTENSITY16UI_EXT ) : return 1 ;
case ( GL_INTENSITY32I_EXT ) : return 1 ;
case ( GL_INTENSITY32UI_EXT ) : return 1 ;
2006-10-15 05:50:29 +08:00
case ( GL_INTENSITY16F_ARB ) : return 1 ;
case ( GL_INTENSITY32F_ARB ) : return 1 ;
2002-04-16 22:09:46 +08:00
case ( GL_LUMINANCE_ALPHA ) : return 2 ;
2007-09-11 20:04:58 +08:00
case ( GL_LUMINANCE_ALPHA8I_EXT ) : return 2 ;
case ( GL_LUMINANCE_ALPHA8UI_EXT ) : return 2 ;
case ( GL_LUMINANCE_ALPHA16I_EXT ) : return 2 ;
case ( GL_LUMINANCE_ALPHA16UI_EXT ) : return 2 ;
case ( GL_LUMINANCE_ALPHA32I_EXT ) : return 2 ;
case ( GL_LUMINANCE_ALPHA32UI_EXT ) : return 2 ;
2006-10-15 05:50:29 +08:00
case ( GL_LUMINANCE_ALPHA16F_ARB ) : return 2 ;
case ( GL_LUMINANCE_ALPHA32F_ARB ) : return 2 ;
2004-03-14 23:08:12 +08:00
case ( GL_HILO_NV ) : return 2 ;
case ( GL_DSDT_NV ) : return 2 ;
case ( GL_DSDT_MAG_NV ) : return 3 ;
case ( GL_DSDT_MAG_VIB_NV ) : return 4 ;
2003-06-24 23:40:09 +08:00
default :
{
2003-09-22 17:13:22 +08:00
notify ( WARN ) < < " error pixelFormat = " < < std : : hex < < pixelFormat < < std : : endl ;
2003-06-24 23:40:09 +08:00
return 0 ;
}
2002-04-12 01:15:07 +08:00
}
2002-04-11 05:51:34 +08:00
}
2002-04-12 01:15:07 +08:00
2002-09-02 20:31:35 +08:00
unsigned int Image : : computePixelSizeInBits ( GLenum format , GLenum type )
2002-04-12 01:15:07 +08:00
{
2003-06-24 23:40:09 +08:00
2005-01-28 04:18:49 +08:00
switch ( format )
{
case ( GL_COMPRESSED_RGB_S3TC_DXT1_EXT ) : return 4 ;
case ( GL_COMPRESSED_RGBA_S3TC_DXT1_EXT ) : return 4 ;
case ( GL_COMPRESSED_RGBA_S3TC_DXT3_EXT ) : return 8 ;
case ( GL_COMPRESSED_RGBA_S3TC_DXT5_EXT ) : return 8 ;
default : break ;
}
2004-05-26 00:10:28 +08:00
2005-01-28 04:18:49 +08:00
// note, haven't yet added proper handling of the ARB GL_COMPRESSRED_* pathways
// yet, no clear size for these since its probably implementation dependent
// which raises the question of how to actually querry for these sizes...
// will need to revisit this issue, for now just report an error.
// this is possible a bit of mute point though as since the ARB compressed formats
// arn't yet used for storing images to disk, so its likely that users wont have
// osg::Image's for pixel formats set the ARB compressed formats, just using these
// compressed formats as internal texture modes. This is very much speculation though
// if get the below error then its time to revist this issue :-)
// Robert Osfield, Jan 2005.
switch ( format )
{
case ( GL_COMPRESSED_ALPHA ) :
case ( GL_COMPRESSED_LUMINANCE ) :
case ( GL_COMPRESSED_LUMINANCE_ALPHA ) :
case ( GL_COMPRESSED_INTENSITY ) :
case ( GL_COMPRESSED_RGB ) :
case ( GL_COMPRESSED_RGBA ) :
notify ( WARN ) < < " Image::computePixelSizeInBits(format,type) : cannot compute correct size of compressed format ( " < < format < < " ) returning 0. " < < std : : endl ;
return 0 ;
default : break ;
2004-05-26 00:10:28 +08:00
}
2007-03-06 19:20:39 +08:00
switch ( format )
{
case ( GL_LUMINANCE4 ) : return 4 ;
case ( GL_LUMINANCE8 ) : return 8 ;
case ( GL_LUMINANCE12 ) : return 12 ;
case ( GL_LUMINANCE16 ) : return 16 ;
case ( GL_LUMINANCE4_ALPHA4 ) : return 8 ;
case ( GL_LUMINANCE6_ALPHA2 ) : return 8 ;
case ( GL_LUMINANCE8_ALPHA8 ) : return 16 ;
case ( GL_LUMINANCE12_ALPHA4 ) : return 16 ;
case ( GL_LUMINANCE12_ALPHA12 ) : return 24 ;
case ( GL_LUMINANCE16_ALPHA16 ) : return 32 ;
case ( GL_INTENSITY4 ) : return 4 ;
case ( GL_INTENSITY8 ) : return 8 ;
case ( GL_INTENSITY12 ) : return 12 ;
case ( GL_INTENSITY16 ) : return 16 ;
default : break ;
}
2004-05-26 00:10:28 +08:00
switch ( type )
{
2002-04-14 21:41:13 +08:00
case ( GL_BITMAP ) : return computeNumComponents ( format ) ;
2002-04-12 01:15:07 +08:00
case ( GL_BYTE ) :
2002-04-14 21:41:13 +08:00
case ( GL_UNSIGNED_BYTE ) : return 8 * computeNumComponents ( format ) ;
2002-04-12 01:15:07 +08:00
2004-09-01 16:49:48 +08:00
case ( GL_HALF_FLOAT_NV ) :
2002-04-12 01:15:07 +08:00
case ( GL_SHORT ) :
2002-04-14 21:41:13 +08:00
case ( GL_UNSIGNED_SHORT ) : return 16 * computeNumComponents ( format ) ;
2002-04-12 01:15:07 +08:00
case ( GL_INT ) :
case ( GL_UNSIGNED_INT ) :
2002-04-14 21:41:13 +08:00
case ( GL_FLOAT ) : return 32 * computeNumComponents ( format ) ;
2002-04-12 01:15:07 +08:00
case ( GL_UNSIGNED_BYTE_3_3_2 ) :
case ( GL_UNSIGNED_BYTE_2_3_3_REV ) : return 8 ;
case ( GL_UNSIGNED_SHORT_5_6_5 ) :
case ( GL_UNSIGNED_SHORT_5_6_5_REV ) :
case ( GL_UNSIGNED_SHORT_4_4_4_4 ) :
case ( GL_UNSIGNED_SHORT_4_4_4_4_REV ) :
case ( GL_UNSIGNED_SHORT_5_5_5_1 ) :
case ( GL_UNSIGNED_SHORT_1_5_5_5_REV ) : return 16 ;
case ( GL_UNSIGNED_INT_8_8_8_8 ) :
case ( GL_UNSIGNED_INT_8_8_8_8_REV ) :
case ( GL_UNSIGNED_INT_10_10_10_2 ) :
case ( GL_UNSIGNED_INT_2_10_10_10_REV ) : return 32 ;
2003-06-24 23:40:09 +08:00
default :
{
2003-06-25 05:57:13 +08:00
notify ( WARN ) < < " error type = " < < type < < std : : endl ;
2003-06-24 23:40:09 +08:00
return 0 ;
}
2002-04-12 01:15:07 +08:00
}
}
2003-09-22 17:13:22 +08:00
unsigned int Image : : computeRowWidthInBytes ( int width , GLenum pixelFormat , GLenum type , int packing )
2002-04-12 01:15:07 +08:00
{
2003-09-22 17:13:22 +08:00
unsigned int pixelSize = computePixelSizeInBits ( pixelFormat , type ) ;
2002-04-12 01:15:07 +08:00
int widthInBits = width * pixelSize ;
int packingInBits = packing * 8 ;
2003-06-25 05:57:13 +08:00
//notify(INFO) << "width="<<width<<" pixelSize="<<pixelSize<<" width in bit="<<widthInBits<<" packingInBits="<<packingInBits<<" widthInBits%packingInBits="<<widthInBits%packingInBits<<std::endl;
2002-04-14 21:41:13 +08:00
return ( widthInBits / packingInBits + ( ( widthInBits % packingInBits ) ? 1 : 0 ) ) * packing ;
2002-04-12 01:15:07 +08:00
}
2003-04-01 19:49:09 +08:00
int Image : : computeNearestPowerOfTwo ( int s , float bias )
2002-08-16 21:33:32 +08:00
{
if ( ( s & ( s - 1 ) ) ! = 0 )
{
// it isn't so lets find the closest power of two.
// yes, logf and powf are slow, but this code should
// only be called during scene graph initilization,
// if at all, so not critical in the greater scheme.
float p2 = logf ( ( float ) s ) / logf ( 2.0f ) ;
float rounded_p2 = floorf ( p2 + bias ) ;
s = ( int ) ( powf ( 2.0f , rounded_p2 ) ) ;
}
return s ;
}
2003-06-24 23:40:09 +08:00
unsigned int Image : : getTotalSizeInBytesIncludingMipmaps ( ) const
{
if ( _mipmapData . empty ( ) )
{
// no mips so just return size of main image
return getTotalSizeInBytes ( ) ;
}
int s = _s ;
int t = _t ;
int r = _r ;
unsigned int maxValue = 0 ;
for ( unsigned int i = 0 ; i < _mipmapData . size ( ) & & _mipmapData [ i ] ; + + i )
{
s > > = 1 ;
t > > = 1 ;
r > > = 1 ;
maxValue = maximum ( maxValue , _mipmapData [ i ] ) ;
}
if ( s = = 0 ) s = 1 ;
if ( t = = 0 ) t = 1 ;
if ( r = = 0 ) r = 1 ;
2005-01-28 04:18:49 +08:00
unsigned int sizeOfLastMipMap = computeRowWidthInBytes ( s , _pixelFormat , _dataType , _packing ) * r * t ;
switch ( _pixelFormat )
2004-05-26 00:10:28 +08:00
{
2005-01-28 04:18:49 +08:00
case ( GL_COMPRESSED_RGB_S3TC_DXT1_EXT ) :
case ( GL_COMPRESSED_RGBA_S3TC_DXT1_EXT ) :
sizeOfLastMipMap = maximum ( sizeOfLastMipMap , 8u ) ; // block size of 8
2005-11-17 21:35:53 +08:00
break ;
2005-01-28 04:18:49 +08:00
case ( GL_COMPRESSED_RGBA_S3TC_DXT3_EXT ) :
case ( GL_COMPRESSED_RGBA_S3TC_DXT5_EXT ) :
sizeOfLastMipMap = maximum ( sizeOfLastMipMap , 16u ) ; // block size of 16
2005-11-17 21:35:53 +08:00
break ;
2005-01-28 04:18:49 +08:00
default : break ;
2004-05-26 00:10:28 +08:00
}
2003-06-24 23:40:09 +08:00
2003-06-25 05:57:13 +08:00
// notify(INFO)<<"sizeOfLastMipMap="<<sizeOfLastMipMap<<"\ts="<<s<<"\tt="<<t<<"\tr"<<r<<std::endl;
2004-05-26 00:10:28 +08:00
2003-06-24 23:40:09 +08:00
return maxValue + sizeOfLastMipMap ;
2004-05-26 00:10:28 +08:00
2003-06-24 23:40:09 +08:00
}
2002-04-16 22:57:39 +08:00
void Image : : setInternalTextureFormat ( GLint internalFormat )
{
// won't do any sanity checking right now, leave it to
// OpenGL to make the call.
_internalTextureFormat = internalFormat ;
}
2003-09-22 17:13:22 +08:00
void Image : : setPixelFormat ( GLenum pixelFormat )
2002-04-16 22:57:39 +08:00
{
2003-09-22 17:13:22 +08:00
if ( _pixelFormat = = pixelFormat ) return ; // do nothing if the same.
2002-04-16 22:57:39 +08:00
2006-08-25 23:49:29 +08:00
if ( _pixelFormat = = 0 | | computeNumComponents ( _pixelFormat ) = = computeNumComponents ( pixelFormat ) )
2002-04-16 22:57:39 +08:00
{
// if the two formats have the same number of componets then
// we can do a straight swap.
2003-09-22 17:13:22 +08:00
_pixelFormat = pixelFormat ;
2002-04-16 22:57:39 +08:00
}
else
{
notify ( WARN ) < < " Image::setPixelFormat(..) - warning, attempt to reset the pixel format with a different number of components. " < < std : : endl ;
}
}
2006-08-25 23:49:29 +08:00
void Image : : setDataType ( GLenum dataType )
{
if ( _dataType = = dataType ) return ; // do nothing if the same.
if ( _dataType = = 0 )
{
// setting the datatype for the first time
_dataType = dataType ;
}
else
{
notify ( WARN ) < < " Image::setDataType(..) - warning, attempt to reset the data type not permitted. " < < std : : endl ;
}
}
2002-08-27 18:06:57 +08:00
void Image : : allocateImage ( int s , int t , int r ,
2002-04-12 01:15:07 +08:00
GLenum format , GLenum type ,
int packing )
{
2002-04-23 05:13:33 +08:00
_mipmapData . clear ( ) ;
2003-07-23 16:18:19 +08:00
unsigned int previousTotalSize = 0 ;
if ( _data ) previousTotalSize = computeRowWidthInBytes ( _s , _pixelFormat , _dataType , _packing ) * _t * _r ;
2002-04-12 01:15:07 +08:00
unsigned int newTotalSize = computeRowWidthInBytes ( s , format , type , packing ) * t * r ;
if ( newTotalSize ! = previousTotalSize )
{
2002-04-14 21:41:13 +08:00
if ( newTotalSize )
2003-02-25 19:56:18 +08:00
setData ( new unsigned char [ newTotalSize ] , USE_NEW_DELETE ) ;
2002-04-14 21:41:13 +08:00
else
2003-02-25 19:56:18 +08:00
deallocateData ( ) ; // and sets it to NULL.
2002-04-12 01:15:07 +08:00
}
if ( _data )
{
_s = s ;
_t = t ;
_r = r ;
_pixelFormat = format ;
_dataType = type ;
_packing = packing ;
2004-03-18 04:03:56 +08:00
_internalTextureFormat = format ;
2002-04-12 01:15:07 +08:00
}
else
{
2003-01-22 00:45:36 +08:00
2002-04-12 01:15:07 +08:00
// throw exception?? not for now, will simply set values to 0.
_s = 0 ;
_t = 0 ;
_r = 0 ;
_pixelFormat = 0 ;
_dataType = 0 ;
_packing = 0 ;
2004-03-18 04:03:56 +08:00
_internalTextureFormat = 0 ;
2002-04-12 01:15:07 +08:00
}
2007-08-17 17:37:49 +08:00
dirty ( ) ;
2002-04-12 01:15:07 +08:00
}
void Image : : setImage ( int s , int t , int r ,
GLint internalTextureFormat ,
GLenum format , GLenum type ,
2001-01-11 00:32:10 +08:00
unsigned char * data ,
2003-02-25 19:56:18 +08:00
AllocationMode mode ,
2002-04-12 01:15:07 +08:00
int packing )
2001-01-11 00:32:10 +08:00
{
2002-04-23 05:13:33 +08:00
_mipmapData . clear ( ) ;
2001-01-11 00:32:10 +08:00
_s = s ;
_t = t ;
_r = r ;
2002-04-12 01:15:07 +08:00
_internalTextureFormat = internalTextureFormat ;
_pixelFormat = format ;
_dataType = type ;
2001-01-11 00:32:10 +08:00
2003-02-25 19:56:18 +08:00
setData ( data , mode ) ;
2002-04-12 01:15:07 +08:00
_packing = packing ;
2002-04-11 05:51:34 +08:00
2007-08-17 17:37:49 +08:00
dirty ( ) ;
2001-01-11 00:32:10 +08:00
}
2002-04-11 05:51:34 +08:00
void Image : : readPixels ( int x , int y , int width , int height ,
2002-04-12 01:15:07 +08:00
GLenum format , GLenum type )
2002-04-11 05:51:34 +08:00
{
2002-08-27 18:06:57 +08:00
allocateImage ( width , height , 1 , format , type ) ;
2002-04-11 05:51:34 +08:00
glPixelStorei ( GL_PACK_ALIGNMENT , _packing ) ;
2002-04-12 01:15:07 +08:00
glReadPixels ( x , y , width , height , format , type , _data ) ;
2002-04-11 05:51:34 +08:00
}
2006-07-06 22:02:14 +08:00
void Image : : readImageFromCurrentTexture ( unsigned int contextID , bool copyMipMapsIfAvailable , GLenum type )
2003-04-03 02:26:34 +08:00
{
const osg : : Texture : : Extensions * extensions = osg : : Texture : : getExtensions ( contextID , true ) ;
2004-09-29 18:01:46 +08:00
const osg : : Texture3D : : Extensions * extensions3D = osg : : Texture3D : : getExtensions ( contextID , true ) ;
2007-09-07 19:21:02 +08:00
const osg : : Texture2DArray : : Extensions * extensions2DArray = osg : : Texture2DArray : : getExtensions ( contextID , true ) ;
2004-09-29 18:01:46 +08:00
2007-09-07 19:21:02 +08:00
GLboolean binding1D , binding2D , binding3D , binding2DArray ;
2004-09-29 18:01:46 +08:00
glGetBooleanv ( GL_TEXTURE_BINDING_1D , & binding1D ) ;
glGetBooleanv ( GL_TEXTURE_BINDING_2D , & binding2D ) ;
glGetBooleanv ( GL_TEXTURE_BINDING_3D , & binding3D ) ;
2007-09-07 19:21:02 +08:00
if ( extensions2DArray - > isTexture2DArraySupported ( ) )
{
glGetBooleanv ( GL_TEXTURE_BINDING_2D_ARRAY_EXT , & binding2DArray ) ;
}
else
{
2007-09-08 15:04:27 +08:00
binding2DArray = GL_FALSE ;
2007-09-07 19:21:02 +08:00
}
2004-09-29 18:01:46 +08:00
2007-09-07 19:21:02 +08:00
GLenum textureMode = binding1D ? GL_TEXTURE_1D : binding2D ? GL_TEXTURE_2D : binding3D ? GL_TEXTURE_3D : binding2DArray ? GL_TEXTURE_2D_ARRAY_EXT : 0 ;
2004-09-29 18:01:46 +08:00
if ( textureMode = = 0 ) return ;
2003-04-03 02:26:34 +08:00
GLint internalformat ;
GLint width ;
GLint height ;
2004-07-06 05:09:30 +08:00
GLint depth ;
2006-07-06 22:02:14 +08:00
GLint packing ;
2004-09-29 18:01:46 +08:00
2004-07-06 05:09:30 +08:00
GLint numMipMaps = 0 ;
if ( copyMipMapsIfAvailable )
{
for ( ; numMipMaps < 20 ; + + numMipMaps )
{
2004-09-29 18:01:46 +08:00
glGetTexLevelParameteriv ( textureMode , numMipMaps , GL_TEXTURE_WIDTH , & width ) ;
glGetTexLevelParameteriv ( textureMode , numMipMaps , GL_TEXTURE_HEIGHT , & height ) ;
glGetTexLevelParameteriv ( textureMode , numMipMaps , GL_TEXTURE_DEPTH , & depth ) ;
2004-07-06 05:09:30 +08:00
if ( width = = 0 | | height = = 0 | | depth = = 0 ) break ;
}
}
else
{
numMipMaps = 1 ;
}
GLint compressed = 0 ;
2003-04-03 02:26:34 +08:00
2004-09-29 18:01:46 +08:00
if ( textureMode = = GL_TEXTURE_2D )
{
if ( extensions - > isCompressedTexImage2DSupported ( ) )
{
2007-09-07 19:21:02 +08:00
glGetTexLevelParameteriv ( textureMode , 0 , GL_TEXTURE_COMPRESSED_ARB , & compressed ) ;
2004-09-29 18:01:46 +08:00
}
}
2004-09-30 17:10:29 +08:00
else if ( textureMode = = GL_TEXTURE_3D )
2003-04-03 02:26:34 +08:00
{
2004-09-29 18:01:46 +08:00
if ( extensions3D - > isCompressedTexImage3DSupported ( ) )
{
2007-09-07 19:21:02 +08:00
glGetTexLevelParameteriv ( textureMode , 0 , GL_TEXTURE_COMPRESSED_ARB , & compressed ) ;
}
}
else if ( textureMode = = GL_TEXTURE_2D_ARRAY_EXT )
{
if ( extensions2DArray - > isCompressedTexImage3DSupported ( ) )
{
glGetTexLevelParameteriv ( textureMode , 0 , GL_TEXTURE_COMPRESSED_ARB , & compressed ) ;
2004-09-29 18:01:46 +08:00
}
2004-07-06 05:09:30 +08:00
}
2004-09-29 18:01:46 +08:00
2004-07-06 05:09:30 +08:00
/* if the compression has been successful */
if ( compressed = = GL_TRUE )
{
2003-04-03 02:26:34 +08:00
2004-07-06 05:09:30 +08:00
MipmapDataType mipMapData ;
unsigned int total_size = 0 ;
GLint i ;
for ( i = 0 ; i < numMipMaps ; + + i )
2003-04-03 02:26:34 +08:00
{
2004-07-06 05:09:30 +08:00
if ( i > 0 ) mipMapData . push_back ( total_size ) ;
2003-04-03 02:26:34 +08:00
2004-07-06 05:09:30 +08:00
GLint compressed_size ;
2004-09-29 18:01:46 +08:00
glGetTexLevelParameteriv ( textureMode , i , GL_TEXTURE_COMPRESSED_IMAGE_SIZE_ARB , & compressed_size ) ;
2003-04-03 02:26:34 +08:00
2004-07-06 05:09:30 +08:00
total_size + = compressed_size ;
2003-04-03 02:26:34 +08:00
}
2004-07-06 05:09:30 +08:00
unsigned char * data = new unsigned char [ total_size ] ;
if ( ! data )
{
osg : : notify ( osg : : WARN ) < < " Warning: Image::readImageFromCurrentTexture(..) out of memory, now image read. " < < std : : endl ;
return ;
}
2004-07-06 05:46:02 +08:00
deallocateData ( ) ; // and sets it to NULL.
2004-09-29 18:01:46 +08:00
glGetTexLevelParameteriv ( textureMode , 0 , GL_TEXTURE_INTERNAL_FORMAT , & internalformat ) ;
glGetTexLevelParameteriv ( textureMode , 0 , GL_TEXTURE_WIDTH , & width ) ;
glGetTexLevelParameteriv ( textureMode , 0 , GL_TEXTURE_HEIGHT , & height ) ;
glGetTexLevelParameteriv ( textureMode , 0 , GL_TEXTURE_DEPTH , & depth ) ;
2006-07-06 22:02:14 +08:00
glGetIntegerv ( GL_UNPACK_ALIGNMENT , & packing ) ;
glPixelStorei ( GL_PACK_ALIGNMENT , packing ) ;
2004-07-06 05:09:30 +08:00
_data = data ;
_s = width ;
_t = height ;
_r = depth ;
_pixelFormat = internalformat ;
2006-07-06 22:02:14 +08:00
_dataType = type ;
2004-07-06 05:09:30 +08:00
_internalTextureFormat = internalformat ;
_mipmapData = mipMapData ;
2004-07-06 05:46:02 +08:00
_allocationMode = USE_NEW_DELETE ;
2006-07-06 22:02:14 +08:00
_packing = packing ;
2004-07-06 05:09:30 +08:00
for ( i = 0 ; i < numMipMaps ; + + i )
{
2004-09-29 18:01:46 +08:00
extensions - > glGetCompressedTexImage ( textureMode , i , getMipmapData ( i ) ) ;
2004-07-06 05:09:30 +08:00
}
2007-08-17 17:37:49 +08:00
dirty ( ) ;
2004-07-06 05:46:02 +08:00
2003-04-03 02:26:34 +08:00
}
2004-07-06 05:09:30 +08:00
else
{
MipmapDataType mipMapData ;
2003-04-03 02:26:34 +08:00
2006-07-06 22:02:14 +08:00
// Get the internal texture format and packing value from OpenGL,
// instead of using possibly outdated values from the class.
glGetTexLevelParameteriv ( textureMode , 0 , GL_TEXTURE_INTERNAL_FORMAT , & internalformat ) ;
glGetIntegerv ( GL_UNPACK_ALIGNMENT , & packing ) ;
glPixelStorei ( GL_PACK_ALIGNMENT , packing ) ;
2004-07-06 05:09:30 +08:00
unsigned int total_size = 0 ;
GLint i ;
for ( i = 0 ; i < numMipMaps ; + + i )
{
if ( i > 0 ) mipMapData . push_back ( total_size ) ;
2004-09-29 18:01:46 +08:00
glGetTexLevelParameteriv ( textureMode , i , GL_TEXTURE_WIDTH , & width ) ;
glGetTexLevelParameteriv ( textureMode , i , GL_TEXTURE_HEIGHT , & height ) ;
glGetTexLevelParameteriv ( textureMode , i , GL_TEXTURE_DEPTH , & depth ) ;
2004-07-06 05:09:30 +08:00
2006-07-06 22:02:14 +08:00
unsigned int level_size = computeRowWidthInBytes ( width , internalformat , type , packing ) * height * depth ;
2003-04-03 02:26:34 +08:00
2004-07-06 05:09:30 +08:00
total_size + = level_size ;
}
unsigned char * data = new unsigned char [ total_size ] ;
if ( ! data )
{
osg : : notify ( osg : : WARN ) < < " Warning: Image::readImageFromCurrentTexture(..) out of memory, now image read. " < < std : : endl ;
return ;
}
2003-04-03 02:26:34 +08:00
2004-07-06 05:46:02 +08:00
deallocateData ( ) ; // and sets it to NULL.
2004-09-29 18:01:46 +08:00
glGetTexLevelParameteriv ( textureMode , 0 , GL_TEXTURE_WIDTH , & width ) ;
glGetTexLevelParameteriv ( textureMode , 0 , GL_TEXTURE_HEIGHT , & height ) ;
glGetTexLevelParameteriv ( textureMode , 0 , GL_TEXTURE_DEPTH , & depth ) ;
2004-07-06 05:09:30 +08:00
_data = data ;
_s = width ;
_t = height ;
_r = depth ;
2006-10-15 05:50:29 +08:00
_pixelFormat = computePixelFormat ( internalformat ) ;
2006-07-06 22:02:14 +08:00
_dataType = type ;
2004-07-06 05:09:30 +08:00
_internalTextureFormat = internalformat ;
_mipmapData = mipMapData ;
2004-07-06 05:46:02 +08:00
_allocationMode = USE_NEW_DELETE ;
2006-07-06 22:02:14 +08:00
_packing = packing ;
2004-07-06 05:09:30 +08:00
for ( i = 0 ; i < numMipMaps ; + + i )
{
2004-09-29 18:01:46 +08:00
glGetTexImage ( textureMode , i , _pixelFormat , _dataType , getMipmapData ( i ) ) ;
2004-07-06 05:09:30 +08:00
}
2007-08-17 17:37:49 +08:00
dirty ( ) ;
2004-07-06 05:09:30 +08:00
}
2003-04-03 02:26:34 +08:00
}
2003-10-30 07:10:11 +08:00
void Image : : scaleImage ( int s , int t , int r , GLenum newDataType )
2001-01-11 00:32:10 +08:00
{
2002-08-27 18:06:57 +08:00
if ( _s = = s & & _t = = t & & _r = = r ) return ;
2002-04-12 01:15:07 +08:00
if ( _data = = NULL )
{
notify ( WARN ) < < " Error Image::scaleImage() do not succeed : cannot scale NULL image. " < < std : : endl ;
return ;
}
if ( _r ! = 1 | | r ! = 1 )
{
notify ( WARN ) < < " Error Image::scaleImage() do not succeed : scaling of volumes not implemented. " < < std : : endl ;
return ;
}
2002-08-27 18:06:57 +08:00
2002-04-12 01:15:07 +08:00
2003-10-30 07:10:11 +08:00
unsigned int newTotalSize = computeRowWidthInBytes ( s , _pixelFormat , newDataType , _packing ) * t ;
2001-01-11 00:32:10 +08:00
2002-04-11 05:51:34 +08:00
// need to sort out what size to really use...
2003-02-25 19:56:18 +08:00
unsigned char * newData = new unsigned char [ newTotalSize ] ;
2002-04-14 21:41:13 +08:00
if ( ! newData )
2002-04-12 01:15:07 +08:00
{
// should we throw an exception??? Just return for time being.
2002-05-28 19:40:37 +08:00
notify ( FATAL ) < < " Error Image::scaleImage() do not succeed : out of memory. " < < newTotalSize < < std : : endl ;
2002-04-12 01:15:07 +08:00
return ;
}
2001-01-11 00:32:10 +08:00
glPixelStorei ( GL_PACK_ALIGNMENT , _packing ) ;
glPixelStorei ( GL_UNPACK_ALIGNMENT , _packing ) ;
2002-04-12 01:15:07 +08:00
GLint status = gluScaleImage ( _pixelFormat ,
2001-09-20 05:08:56 +08:00
_s ,
_t ,
2002-04-12 01:15:07 +08:00
_dataType ,
2001-09-20 05:08:56 +08:00
_data ,
s ,
t ,
2003-10-30 07:10:11 +08:00
newDataType ,
2001-09-20 05:08:56 +08:00
newData ) ;
if ( status = = 0 )
{
2001-01-11 00:32:10 +08:00
// free old image.
_s = s ;
_t = t ;
2003-10-30 07:10:11 +08:00
_dataType = newDataType ;
2003-02-25 19:56:18 +08:00
setData ( newData , USE_NEW_DELETE ) ;
2001-01-11 00:32:10 +08:00
}
else
{
2003-02-25 19:56:18 +08:00
delete [ ] newData ;
2001-01-11 00:32:10 +08:00
2003-12-24 08:14:45 +08:00
notify ( WARN ) < < " Error Image::scaleImage() did not succeed : errorString = " < < gluErrorString ( ( GLenum ) status ) < < std : : endl ;
2001-01-11 00:32:10 +08:00
}
2001-11-19 05:31:16 +08:00
2007-08-17 17:37:49 +08:00
dirty ( ) ;
2001-01-11 00:32:10 +08:00
}
2002-08-27 18:06:57 +08:00
void Image : : copySubImage ( int s_offset , int t_offset , int r_offset , osg : : Image * source )
{
if ( ! source ) return ;
if ( s_offset < 0 | | t_offset < 0 | | r_offset < 0 )
{
notify ( WARN ) < < " Warning: negative offsets passed to Image::copySubImage(..) not supported, operation ignored. " < < std : : endl ;
return ;
}
if ( ! _data )
{
2003-06-25 05:57:13 +08:00
notify ( INFO ) < < " allocating image " < < endl ;
2004-10-18 22:46:57 +08:00
allocateImage ( s_offset + source - > s ( ) , t_offset + source - > t ( ) , r_offset + source - > r ( ) ,
2002-08-27 18:06:57 +08:00
source - > getPixelFormat ( ) , source - > getDataType ( ) ,
source - > getPacking ( ) ) ;
}
if ( s_offset > = _s | | t_offset > = _t | | r_offset > = _r )
{
notify ( WARN ) < < " Warning: offsets passed to Image::copySubImage(..) outside destination image, operation ignored. " < < std : : endl ;
return ;
}
if ( _pixelFormat ! = source - > getPixelFormat ( ) )
{
notify ( WARN ) < < " Warning: image with an incompatible pixel formats passed to Image::copySubImage(..), operation ignored. " < < std : : endl ;
return ;
}
void * data_destination = data ( s_offset , t_offset , r_offset ) ;
glPixelStorei ( GL_PACK_ALIGNMENT , source - > getPacking ( ) ) ;
glPixelStorei ( GL_PACK_ROW_LENGTH , _s ) ;
glPixelStorei ( GL_UNPACK_ALIGNMENT , _packing ) ;
GLint status = gluScaleImage ( _pixelFormat ,
source - > s ( ) ,
source - > t ( ) ,
source - > getDataType ( ) ,
source - > data ( ) ,
source - > s ( ) ,
source - > t ( ) ,
_dataType ,
data_destination ) ;
glPixelStorei ( GL_PACK_ROW_LENGTH , 0 ) ;
if ( status ! = 0 )
{
notify ( WARN ) < < " Error Image::scaleImage() do not succeed : errorString = " < < gluErrorString ( ( GLenum ) status ) < < std : : endl ;
}
}
2004-08-17 04:57:24 +08:00
void Image : : flipHorizontal ( )
2002-05-14 17:34:11 +08:00
{
if ( _data = = NULL )
{
2006-08-25 00:33:23 +08:00
notify ( WARN ) < < " Error Image::flipHorizontal() did not succeed : cannot flip NULL image. " < < std : : endl ;
2002-05-14 17:34:11 +08:00
return ;
}
unsigned int elemSize = getPixelSizeInBits ( ) / 8 ;
2006-08-25 00:33:23 +08:00
if ( _mipmapData . empty ( ) )
2002-05-14 17:34:11 +08:00
{
2004-08-17 04:57:24 +08:00
2006-08-25 00:33:23 +08:00
for ( int r = 0 ; r < _r ; + + r )
{
for ( int t = 0 ; t < _t ; + + t )
2004-08-17 04:57:24 +08:00
{
2006-08-25 00:33:23 +08:00
unsigned char * rowData = _data + t * getRowSizeInBytes ( ) + r * getImageSizeInBytes ( ) ;
unsigned char * left = rowData ;
unsigned char * right = rowData + ( ( _s - 1 ) * getPixelSizeInBits ( ) ) / 8 ;
while ( left < right )
{
char tmp [ 32 ] ; // max elem size is four floats
memcpy ( tmp , left , elemSize ) ;
memcpy ( left , right , elemSize ) ;
memcpy ( right , tmp , elemSize ) ;
left + = elemSize ;
right - = elemSize ;
}
2004-08-17 04:57:24 +08:00
}
2002-05-14 17:34:11 +08:00
}
}
2006-08-25 00:33:23 +08:00
else
{
notify ( WARN ) < < " Error Image::flipHorizontal() did not succeed : cannot flip mipmapped image. " < < std : : endl ;
return ;
}
2007-08-17 17:37:49 +08:00
dirty ( ) ;
2002-05-14 17:34:11 +08:00
}
2004-08-17 04:57:24 +08:00
void flipImageVertical ( unsigned char * top , unsigned char * bottom , unsigned int rowSize )
{
while ( top < bottom )
{
for ( unsigned int i = 0 ; i < rowSize ; + + i , + + top , + + bottom )
{
unsigned char temp = * top ;
* top = * bottom ;
* bottom = temp ;
}
bottom - = 2 * rowSize ;
}
}
2002-05-14 17:34:11 +08:00
2004-08-17 04:57:24 +08:00
void Image : : flipVertical ( )
2002-05-14 17:34:11 +08:00
{
if ( _data = = NULL )
{
notify ( WARN ) < < " Error Image::flipVertical() do not succeed : cannot flip NULL image. " < < std : : endl ;
return ;
}
2004-08-17 04:57:24 +08:00
if ( ! _mipmapData . empty ( ) & & _r > 1 )
{
notify ( WARN ) < < " Error Image::flipVertical() do not succeed : flipping of mipmap 3d textures not yet supported. " < < std : : endl ;
return ;
}
2002-05-14 17:34:11 +08:00
2004-08-17 04:57:24 +08:00
if ( _mipmapData . empty ( ) )
2002-05-14 17:34:11 +08:00
{
2004-08-17 04:57:24 +08:00
// no mipmaps,
// so we can safely handle 3d textures
for ( int r = 0 ; r < _r ; + + r )
{
if ( ! dxtc_tool : : VerticalFlip ( _s , _t , _pixelFormat , data ( 0 , 0 , r ) ) )
{
// its not a compressed image, so implement flip oursleves.
unsigned int rowSize = computeRowWidthInBytes ( _s , _pixelFormat , _dataType , _packing ) ;
unsigned char * top = data ( 0 , 0 , r ) ;
unsigned char * bottom = top + ( _t - 1 ) * rowSize ;
flipImageVertical ( top , bottom , rowSize ) ;
}
}
2002-05-14 17:34:11 +08:00
}
2004-08-17 04:57:24 +08:00
else if ( _r = = 1 )
{
if ( ! dxtc_tool : : VerticalFlip ( _s , _t , _pixelFormat , _data ) )
{
// its not a compressed image, so implement flip oursleves.
unsigned int rowSize = computeRowWidthInBytes ( _s , _pixelFormat , _dataType , _packing ) ;
unsigned char * top = data ( 0 , 0 , 0 ) ;
unsigned char * bottom = top + ( _t - 1 ) * rowSize ;
2002-05-14 17:34:11 +08:00
2004-08-17 04:57:24 +08:00
flipImageVertical ( top , bottom , rowSize ) ;
}
int s = _s ;
int t = _t ;
//int r = _r;
for ( unsigned int i = 0 ; i < _mipmapData . size ( ) & & _mipmapData [ i ] ; + + i )
{
s > > = 1 ;
t > > = 1 ;
if ( s = = 0 ) s = 1 ;
if ( t = = 0 ) t = 1 ;
if ( ! dxtc_tool : : VerticalFlip ( s , t , _pixelFormat , _data + _mipmapData [ i ] ) )
{
// its not a compressed image, so implement flip oursleves.
unsigned int rowSize = computeRowWidthInBytes ( s , _pixelFormat , _dataType , _packing ) ;
unsigned char * top = _data + _mipmapData [ i ] ;
unsigned char * bottom = top + ( t - 1 ) * rowSize ;
flipImageVertical ( top , bottom , rowSize ) ;
}
}
}
2002-05-14 17:34:11 +08:00
2007-08-17 17:37:49 +08:00
dirty ( ) ;
2002-05-14 17:34:11 +08:00
}
2001-09-20 05:08:56 +08:00
2002-09-17 04:58:05 +08:00
void Image : : ensureValidSizeForTexturing ( GLint maxTextureSize )
2001-01-11 00:32:10 +08:00
{
2002-08-16 21:33:32 +08:00
int new_s = computeNearestPowerOfTwo ( _s ) ;
int new_t = computeNearestPowerOfTwo ( _t ) ;
2001-10-05 18:38:16 +08:00
2002-09-17 04:58:05 +08:00
if ( new_s > maxTextureSize ) new_s = maxTextureSize ;
if ( new_t > maxTextureSize ) new_t = maxTextureSize ;
2001-10-05 18:38:16 +08:00
if ( new_s ! = _s | | new_t ! = _t )
2001-01-11 00:32:10 +08:00
{
2001-12-15 07:18:28 +08:00
if ( ! _fileName . empty ( ) ) notify ( NOTICE ) < < " Scaling image ' " < < _fileName < < " ' from ( " < < _s < < " , " < < _t < < " ) to ( " < < new_s < < " , " < < new_t < < " ) " < < std : : endl ;
else notify ( NOTICE ) < < " Scaling image from ( " < < _s < < " , " < < _t < < " ) to ( " < < new_s < < " , " < < new_t < < " ) " < < std : : endl ;
2001-09-20 05:08:56 +08:00
2001-01-11 00:32:10 +08:00
scaleImage ( new_s , new_t , _r ) ;
}
}
2005-02-10 23:52:18 +08:00
template < typename T >
bool _findLowerAlphaValueInRow ( unsigned int num , T * data , T value , unsigned int delta )
{
for ( unsigned int i = 0 ; i < num ; + + i )
{
if ( * data < value ) return true ;
data + = delta ;
}
return false ;
}
2007-06-11 03:22:13 +08:00
template < typename T >
bool _maskedFindLowerAlphaValueInRow ( unsigned int num , T * data , T value , T mask , unsigned int delta )
{
for ( unsigned int i = 0 ; i < num ; + + i )
{
if ( ( * data & mask ) < value ) return true ;
data + = delta ;
}
return false ;
}
2002-11-26 00:39:05 +08:00
bool Image : : isImageTranslucent ( ) const
{
2005-02-10 23:52:18 +08:00
unsigned int offset = 0 ;
unsigned int delta = 1 ;
switch ( _pixelFormat )
{
case ( GL_ALPHA ) :
offset = 0 ;
delta = 1 ;
break ;
case ( GL_LUMINANCE_ALPHA ) :
offset = 1 ;
delta = 2 ;
break ;
case ( GL_RGBA ) :
offset = 3 ;
delta = 4 ;
break ;
case ( GL_BGRA ) :
offset = 3 ;
delta = 4 ;
break ;
default :
return false ;
}
2002-11-26 00:39:05 +08:00
2005-02-10 23:52:18 +08:00
for ( int ir = 0 ; ir < r ( ) ; + + ir )
2002-11-26 00:39:05 +08:00
{
2005-02-10 23:52:18 +08:00
for ( int it = 0 ; it < t ( ) ; + + it )
{
2005-03-24 05:00:28 +08:00
const unsigned char * d = data ( 0 , it , ir ) ;
2005-02-10 23:52:18 +08:00
switch ( _dataType )
{
2005-07-27 00:05:42 +08:00
case ( GL_BYTE ) :
if ( _findLowerAlphaValueInRow ( s ( ) , ( char * ) d + offset , ( char ) 127 , delta ) )
return true ;
break ;
case ( GL_UNSIGNED_BYTE ) :
if ( _findLowerAlphaValueInRow ( s ( ) , ( unsigned char * ) d + offset , ( unsigned char ) 255 , delta ) )
return true ;
break ;
case ( GL_SHORT ) :
if ( _findLowerAlphaValueInRow ( s ( ) , ( short * ) d + offset , ( short ) 32767 , delta ) )
return true ;
break ;
case ( GL_UNSIGNED_SHORT ) :
if ( _findLowerAlphaValueInRow ( s ( ) , ( unsigned short * ) d + offset , ( unsigned short ) 65535 , delta ) )
return true ;
break ;
case ( GL_INT ) :
if ( _findLowerAlphaValueInRow ( s ( ) , ( int * ) d + offset , ( int ) 2147483647 , delta ) )
return true ;
break ;
case ( GL_UNSIGNED_INT ) :
if ( _findLowerAlphaValueInRow ( s ( ) , ( unsigned int * ) d + offset , 4294967295u , delta ) )
return true ;
break ;
case ( GL_FLOAT ) :
if ( _findLowerAlphaValueInRow ( s ( ) , ( float * ) d + offset , 1.0f , delta ) )
return true ;
break ;
2007-06-11 03:22:13 +08:00
case ( GL_UNSIGNED_SHORT_5_5_5_1 ) :
if ( _maskedFindLowerAlphaValueInRow ( s ( ) , ( unsigned short * ) d ,
( unsigned short ) 0x0001 ,
( unsigned short ) 0x0001 , 1 ) )
return true ;
break ;
case ( GL_UNSIGNED_SHORT_1_5_5_5_REV ) :
if ( _maskedFindLowerAlphaValueInRow ( s ( ) , ( unsigned short * ) d ,
( unsigned short ) 0x8000 ,
( unsigned short ) 0x8000 , 1 ) )
return true ;
break ;
case ( GL_UNSIGNED_SHORT_4_4_4_4 ) :
if ( _maskedFindLowerAlphaValueInRow ( s ( ) , ( unsigned short * ) d ,
( unsigned short ) 0x000f ,
( unsigned short ) 0x000f , 1 ) )
return true ;
break ;
case ( GL_UNSIGNED_SHORT_4_4_4_4_REV ) :
if ( _maskedFindLowerAlphaValueInRow ( s ( ) , ( unsigned short * ) d ,
( unsigned short ) 0xf000 ,
( unsigned short ) 0xf000 , 1 ) )
return true ;
break ;
case ( GL_UNSIGNED_INT_10_10_10_2 ) :
if ( _maskedFindLowerAlphaValueInRow ( s ( ) , ( unsigned int * ) d ,
0x00000003u ,
0x00000003u , 1 ) )
return true ;
break ;
case ( GL_UNSIGNED_INT_2_10_10_10_REV ) :
if ( _maskedFindLowerAlphaValueInRow ( s ( ) , ( unsigned int * ) d ,
0xc0000000u ,
0xc0000000u , 1 ) )
return true ;
break ;
case ( GL_HALF_FLOAT_NV ) :
if ( _findLowerAlphaValueInRow ( s ( ) , ( unsigned short * ) d + offset ,
( unsigned short ) 0x3c00 , delta ) )
return true ;
break ;
2005-02-10 23:52:18 +08:00
}
}
2002-11-26 00:39:05 +08:00
}
return false ;
}
2002-08-16 21:33:32 +08:00
///////////////////////////////////////////////////////////////////////////////
2001-09-20 05:08:56 +08:00
2001-01-11 00:32:10 +08:00
Geode * osg : : createGeodeForImage ( osg : : Image * image )
{
return createGeodeForImage ( image , image - > s ( ) , image - > t ( ) ) ;
}
2001-09-20 05:08:56 +08:00
2002-09-02 20:31:35 +08:00
Geode * osg : : createGeodeForImage ( osg : : Image * image , float s , float t )
2001-01-11 00:32:10 +08:00
{
if ( image )
{
if ( s > 0 & & t > 0 )
{
float y = 1.0 ;
float x = y * ( s / t ) ;
2001-09-20 05:08:56 +08:00
// set up the texture.
2002-12-16 21:40:58 +08:00
osg : : Texture2D * texture = new osg : : Texture2D ;
2001-01-11 00:32:10 +08:00
texture - > setImage ( image ) ;
2001-09-20 05:08:56 +08:00
// set up the drawstate.
2002-12-16 21:40:58 +08:00
osg : : StateSet * dstate = new osg : : StateSet ;
2001-09-20 05:08:56 +08:00
dstate - > setMode ( GL_CULL_FACE , osg : : StateAttribute : : OFF ) ;
dstate - > setMode ( GL_LIGHTING , osg : : StateAttribute : : OFF ) ;
2002-07-10 19:22:24 +08:00
dstate - > setTextureAttributeAndModes ( 0 , texture , osg : : StateAttribute : : ON ) ;
2001-01-11 00:32:10 +08:00
// set up the geoset.
2002-12-16 21:40:58 +08:00
Geometry * geom = new Geometry ;
2002-06-26 04:27:51 +08:00
geom - > setStateSet ( dstate ) ;
2002-12-16 21:40:58 +08:00
Vec3Array * coords = new Vec3Array ( 4 ) ;
2002-06-26 04:27:51 +08:00
( * coords ) [ 0 ] . set ( - x , 0.0f , y ) ;
( * coords ) [ 1 ] . set ( - x , 0.0f , - y ) ;
( * coords ) [ 2 ] . set ( x , 0.0f , - y ) ;
( * coords ) [ 3 ] . set ( x , 0.0f , y ) ;
geom - > setVertexArray ( coords ) ;
2002-12-16 21:40:58 +08:00
Vec2Array * tcoords = new Vec2Array ( 4 ) ;
2002-06-26 04:27:51 +08:00
( * tcoords ) [ 0 ] . set ( 0.0f , 1.0f ) ;
( * tcoords ) [ 1 ] . set ( 0.0f , 0.0f ) ;
( * tcoords ) [ 2 ] . set ( 1.0f , 0.0f ) ;
( * tcoords ) [ 3 ] . set ( 1.0f , 1.0f ) ;
geom - > setTexCoordArray ( 0 , tcoords ) ;
2002-12-16 21:40:58 +08:00
osg : : Vec4Array * colours = new osg : : Vec4Array ( 1 ) ;
2002-06-26 04:27:51 +08:00
( * colours ) [ 0 ] . set ( 1.0f , 1.0f , 1.0 , 1.0f ) ;
geom - > setColorArray ( colours ) ;
geom - > setColorBinding ( Geometry : : BIND_OVERALL ) ;
2002-12-16 21:40:58 +08:00
geom - > addPrimitiveSet ( new DrawArrays ( PrimitiveSet : : QUADS , 0 , 4 ) ) ;
2001-01-11 00:32:10 +08:00
// set up the geode.
2002-12-16 21:40:58 +08:00
osg : : Geode * geode = new osg : : Geode ;
2002-06-26 04:27:51 +08:00
geode - > addDrawable ( geom ) ;
2001-01-11 00:32:10 +08:00
return geode ;
}
else
{
return NULL ;
}
}
else
{
return NULL ;
}
}
2002-08-16 21:33:32 +08:00