Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

OgreILImageCodec.cpp

Go to the documentation of this file.
00001 /*
00002 -----------------------------------------------------------------------------
00003 This source file is part of OGRE
00004 (Object-oriented Graphics Rendering Engine)
00005 For the latest info, see http://www.ogre3d.org/
00006 
00007 Copyright © 2000-2002 The OGRE Team
00008 Also see acknowledgements in Readme.html
00009 
00010 This program is free software; you can redistribute it and/or modify it under
00011 the terms of the GNU Lesser General Public License as published by the Free Software
00012 Foundation; either version 2 of the License, or (at your option) any later
00013 version.
00014 
00015 This program is distributed in the hope that it will be useful, but WITHOUT
00016 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00017 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00018 
00019 You should have received a copy of the GNU Lesser General Public License along with
00020 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00021 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00022 http://www.gnu.org/copyleft/lesser.txt.
00023 -----------------------------------------------------------------------------
00024 */
00025 
00026 #include "OgreStableHeaders.h"
00027 
00028 #include "OgreRoot.h"
00029 #include "OgreRenderSystem.h"
00030 #include "OgreILImageCodec.h"
00031 #include "OgreImage.h"
00032 #include "OgreException.h"
00033 
00034 #include <IL/il.h>
00035 #include <IL/ilu.h>
00036 
00037 namespace Ogre {
00038 
00039     bool ILImageCodec::_is_initialised = false;    
00040 
00041     //---------------------------------------------------------------------
00042     void ILImageCodec::code( const DataChunk& input, DataChunk* output, ... ) const
00043     {        
00044         OgreGuard( "ILCodec::code" );
00045 
00046         Except(Exception::UNIMPLEMENTED_FEATURE, "code to memory not implemented",
00047             "ILCodec::code");
00048 
00049         OgreUnguard();
00050 
00051     }
00052 
00053 
00054     //---------------------------------------------------------------------
00055     void ILImageCodec::codeToFile( const DataChunk& input, 
00056         const String& outFileName, Codec::CodecData* pData) const
00057     {
00058         OgreGuard( "ILImageCodec::codeToFile" );
00059 
00060         ILuint ImageName;
00061 
00062         ilGenImages( 1, &ImageName );
00063         ilBindImage( ImageName );
00064 
00065         ImageData* pImgData = static_cast< ImageData * >( pData );
00066         std::pair< int, int > fmt_bpp = OgreFormat2ilFormat( pImgData->format );
00067         ilTexImage( 
00068             pImgData->width, pImgData->height, 1, fmt_bpp.second, fmt_bpp.first, IL_UNSIGNED_BYTE, 
00069             static_cast< void * >( const_cast< uchar * >( ( input.getPtr() ) ) ) );
00070         iluFlipImage();
00071 
00072         // Implicitly pick DevIL codec
00073         ilSaveImage(const_cast< char * >( outFileName.c_str() ) );
00074 
00075         ilDeleteImages(1, &ImageName);
00076 
00077         OgreUnguard();
00078     }
00079     //---------------------------------------------------------------------
00080     Codec::CodecData * ILImageCodec::decode( const DataChunk& input, DataChunk* output, ... ) const
00081     {
00082         OgreGuard( "ILImageCodec::decode" );
00083 
00084         // DevIL variables
00085         ILuint ImageName;
00086         ILint ImageFormat, BytesPerPixel;
00087         ImageData * ret_data = new ImageData;
00088 
00089         // Load the image
00090         ilGenImages( 1, &ImageName );
00091         ilBindImage( ImageName );
00092 
00093         // Put it right side up
00094         ilEnable(IL_ORIGIN_SET);
00095         ilSetInteger(IL_ORIGIN_MODE, IL_ORIGIN_UPPER_LEFT);
00096 
00097         // Keep DXTC(compressed) data if present
00098         ilSetInteger(IL_KEEP_DXTC_DATA, IL_TRUE);
00099 
00100         // Load image from disk
00101         ilLoadL( 
00102             getILType(), 
00103             ( void * )const_cast< uchar * >( input.getPtr() ), 
00104             static_cast< ILuint >( input.getSize() ) );
00105 
00106         // Check if everything was ok
00107         ILenum PossibleError = ilGetError() ;
00108         if( PossibleError != IL_NO_ERROR ) {
00109             Except( Exception::UNIMPLEMENTED_FEATURE,
00110                 "IL Error",
00111                 iluErrorString(PossibleError) ) ;
00112         }
00113 
00114         // Format conversion to RGB or RGBA
00115         ImageFormat = ilGetInteger( IL_IMAGE_FORMAT );
00116         if(ImageFormat==IL_BGR || ImageFormat==IL_BGRA) {
00117             // New image format
00118             if(ImageFormat==IL_BGR)
00119                 ImageFormat = IL_RGB;
00120             else
00121                 ImageFormat = IL_RGBA;
00122 
00123             // Doing this with IL_FORMAT_SET/IL_FORMAT_MODE would have
00124             // to be done before loading, and would always
00125             // produce images with alpha channel.
00126             iluSwapColours();
00127         }
00128 
00129         // Now sets some variables
00130         BytesPerPixel = ilGetInteger( IL_IMAGE_BYTES_PER_PIXEL ); 
00131 
00132         ret_data->format = ilFormat2OgreFormat( ImageFormat, BytesPerPixel );
00133         ret_data->width = ilGetInteger( IL_IMAGE_WIDTH );
00134         ret_data->height = ilGetInteger( IL_IMAGE_HEIGHT );
00135         ret_data->depth = ilGetInteger( IL_IMAGE_DEPTH );
00136         ret_data->num_mipmaps = ilGetInteger ( IL_NUM_MIPMAPS );
00137         ret_data->flags = 0;
00138 
00139         // Check for cubemap
00140         ILuint cubeflags = ilGetInteger ( IL_IMAGE_CUBEFLAGS );
00141         if(cubeflags)
00142             ret_data->flags |= IF_CUBEMAP;
00143 
00144         // Keep DXT data (if present at all)
00145         ILuint dxtFormat = ilGetInteger( IL_DXTC_DATA_FORMAT );
00146         if(dxtFormat != IL_DXT_NO_COMP && Root::getSingleton().getRenderSystem()->getCapabilities()->hasCapability( RSC_TEXTURE_COMPRESSION_DXT ))
00147         {
00148             ILuint dxtSize = ilGetDXTCData(NULL, 0, dxtFormat);
00149             output->allocate( dxtSize );
00150             ilGetDXTCData(output->getPtr(), dxtSize, dxtFormat);
00151 
00152             ret_data->size = dxtSize;
00153             ret_data->format = ilFormat2OgreFormat( dxtFormat, BytesPerPixel );
00154             ret_data->flags |= IF_COMPRESSED;
00155         }
00156         else
00157         {
00158             uint numImagePasses = cubeflags ? 6 : 1;
00159             uint imageSize = ilGetInteger(IL_IMAGE_SIZE_OF_DATA);
00160             output->allocate( imageSize * numImagePasses );
00161 
00162             unsigned int i = 0, offset = 0;
00163             for(i = 0; i < numImagePasses; i++)
00164             {
00165                 if(cubeflags)
00166                 {
00167                     ilBindImage(ImageName);
00168                     ilActiveImage(i);
00169                 }
00170 
00171                 // Move the image data to the output buffer
00172                 memcpy( output->getPtr() + offset, ilGetData(), imageSize );
00173                 offset += imageSize;
00174             }
00175 
00176             ret_data->size = imageSize * numImagePasses;
00177             ret_data->format = ilFormat2OgreFormat( ImageFormat, BytesPerPixel );
00178         }
00179 
00180         // Restore IL state
00181         ilDisable(IL_ORIGIN_SET);
00182         ilDisable(IL_FORMAT_SET);
00183 
00184         ilDeleteImages( 1, &ImageName );
00185 
00186         OgreUnguardRet( ret_data );
00187     }
00188     //---------------------------------------------------------------------
00189     void ILImageCodec::initialiseIL(void)
00190     {
00191         if( !_is_initialised )
00192         {
00193             ilInit();
00194             ilEnable( IL_FILE_OVERWRITE );
00195             _is_initialised = true;
00196         }
00197     }
00198 }

Copyright © 2002-2003 by The OGRE Team
Last modified Sun Nov 28 19:48:29 2004