Simple Image Loading LibrarY 0.1.0
|
00001 /*********************************************************************** 00002 filename: SILLYImage.cpp 00003 created: 10 Jun 2006 00004 author: Olivier Delannoy 00005 00006 purpose: Implementation of the Image class 00007 *************************************************************************/ 00008 /*************************************************************************** 00009 * Copyright (C) 2004 - 2006 Paul D Turner & The CEGUI Development Team 00010 * 00011 * Permission is hereby granted, free of charge, to any person obtaining 00012 * a copy of this software and associated documentation files (the 00013 * "Software"), to deal in the Software without restriction, including 00014 * without limitation the rights to use, copy, modify, merge, publish, 00015 * distribute, sublicense, and/or sell copies of the Software, and to 00016 * permit persons to whom the Software is furnished to do so, subject to 00017 * the following conditions: 00018 * 00019 * The above copyright notice and this permission notice shall be 00020 * included in all copies or substantial portions of the Software. 00021 * 00022 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00023 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00024 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 00025 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 00026 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 00027 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 00028 * OTHER DEALINGS IN THE SOFTWARE. 00029 ***************************************************************************/ 00030 #ifdef HAVE_CONFIG_H 00031 #include <config.h> 00032 #endif 00033 00034 #include "SILLYImage.h" 00035 00036 #ifndef SILLY_OPT_INLINE 00037 #define inline 00038 #include "SILLYImage.icpp" 00039 #undef inline 00040 #endif 00041 #include "SILLYImageLoaderManager.h" 00042 00043 00044 // Start section of namespace SILLY 00045 namespace SILLY 00046 { 00047 00048 Image::Image(DataSource& source) 00049 : d_bpp(0), d_pixels(0), d_data(&source), d_imageContext(0), d_imageLoader(0) 00050 { 00051 } 00052 00053 00054 bool Image::loadImageHeader() 00055 { 00056 ImageLoaderList::iterator iter = ImageLoaderManager::getSingleton().begin(); 00057 for (; ! d_imageLoader && iter != ImageLoaderManager::getSingleton().end() ; ++iter) 00058 { 00059 d_imageContext = (*iter)->loadHeader(d_pfSource, d_data); 00060 if (d_imageContext) 00061 d_imageLoader = (*iter); 00062 } 00063 assert((! d_imageLoader || d_imageContext) && "ASSERT: Internal state of image invalid"); 00064 return d_imageLoader != 0; 00065 00066 } 00067 00068 bool Image::loadImageData(PixelFormat resultFormat, PixelOrigin order) 00069 { 00070 switch (resultFormat) 00071 { 00072 case PF_A1B5G5R5: 00073 d_bpp = 2; 00074 break; 00075 case PF_RGB: 00076 d_bpp = 3; 00077 break; 00078 00079 case PF_RGBA: 00080 d_bpp = 4; 00081 break; 00082 //default: 00083 // Unsupported format 00084 }; 00085 00086 if (! allocate()) 00087 { 00088 return false; 00089 } 00090 d_imageContext->setDestination(d_pixels, getWidth() * getHeight() * d_bpp, resultFormat); 00091 00092 if (! d_imageLoader->loadImageData(order, d_data, d_imageContext)) 00093 { 00094 delete [] d_pixels; 00095 return false; 00096 } 00097 return true; 00098 } 00099 00100 bool Image::allocate() 00101 { 00102 delete [] d_pixels; 00103 d_pixels = 0; 00104 d_pixels = new byte[d_bpp * getWidth() * getHeight()]; 00105 return d_pixels != 0; 00106 } 00107 00108 } // End section of namespace SILLY