From: <sv...@ww...> - 2007-04-10 08:02:15
|
Author: mkrose Date: 2007-04-10 01:02:06 -0700 (Tue, 10 Apr 2007) New Revision: 2077 Modified: trunk/csp/SConstruct trunk/csp/modules/demeter/Terrain.cpp Log: Remove dependence on SDL_image. This library was only used by demeter to load textures and terrain tiles. Replaced with osg/Image. Browse at: https://www.zerobar.net/viewcvs/viewcvs.cgi?view=rev&rev=2077 Modified: trunk/csp/SConstruct =================================================================== --- trunk/csp/SConstruct 2007-04-10 06:23:14 UTC (rev 2076) +++ trunk/csp/SConstruct 2007-04-10 08:02:06 UTC (rev 2077) @@ -95,8 +95,6 @@ name = 'sdl', config = [ build.CommandConfig(package='sdl', version_command='sdl-config --version', flags_command='sdl-config --cflags --libs', version='1.2.5', label='sdl'), - build.UnixLibConfig(lib='SDL_image', symbol='IMG_Load', label='sdl image'), - build.DevpackConfig(dlls=['SDL', 'SDL_image'], headers=[('SDL', 'SDL_mouse.h'), ('SDL', 'SDL_image.h')]), ]) build.ExternalLibrary( Modified: trunk/csp/modules/demeter/Terrain.cpp =================================================================== --- trunk/csp/modules/demeter/Terrain.cpp 2007-04-10 06:23:14 UTC (rev 2076) +++ trunk/csp/modules/demeter/Terrain.cpp 2007-04-10 08:02:06 UTC (rev 2077) @@ -41,8 +41,6 @@ #include <fcntl.h> #include <sys/stat.h> -#include <SDL/SDL_image.h> - #ifndef _WIN32 #define GL_GLEXT_PROTOTYPES #include <SDL/SDL_opengl.h> @@ -53,6 +51,10 @@ #define _read(a,b,c) read((a),(b),(c)) #endif +#include <osg/Image> +#include <osgDB/ReadFile> +#include <osgDB/Registry> + //#ifdef DEMETER_MEMORYMANAGER //#include "mmgr.h" //#endif @@ -2509,65 +2511,62 @@ } #endif -void LoadImage(const char* szShortFilename,int& width,int &height,Uint8** ppBuffer,bool bAlpha) +void LoadImage(const char* szShortFilename, int& width, int &height, Uint8** ppBuffer, bool bAlpha) { - SDL_Init(SDL_INIT_VIDEO); + width = 0; + height = 0; + *ppBuffer = NULL; + char szFullFilename[MAX_FILENAME_LENGTH]; - if (strstr(szShortFilename,"\\") || strstr(szShortFilename,"/")) - sprintf(szFullFilename,szShortFilename); + if (strstr(szShortFilename, "\\") || strstr(szShortFilename, "/")) + sprintf(szFullFilename, szShortFilename); else - Settings::GetInstance()->PrependMediaPath(szShortFilename,szFullFilename); + Settings::GetInstance()->PrependMediaPath(szShortFilename, szFullFilename); m_Logfile << "TERRAIN: Loading texture " << szFullFilename << endl; - SDL_Surface* pImage = IMG_Load(szFullFilename); - if (pImage != NULL) - { - width = pImage->w; - height = pImage->h; - Uint8* pBufferTemp; - if (bAlpha) - pBufferTemp = new Uint8[width * height * 4]; - else - pBufferTemp = new Uint8[width * height * 3]; - int i,j; - Uint8* pImagePixels = (Uint8*)pImage->pixels; - for (i = 0,j = 0; i < pImage->h * pImage->pitch; i += pImage->pitch) - { - Uint8* pImageRow = pImagePixels + i; - for (Uint8* pImagePixel = pImageRow; pImagePixel < pImageRow + pImage->w * pImage->format->BytesPerPixel; pImagePixel += pImage->format->BytesPerPixel) - { - Uint8 red,green,blue,alpha; - // Read the pixel into a 32-bit dword for use by SDL_GetRGBA - Uint8 pPixel[4]; - for (int i = 0; i < pImage->format->BytesPerPixel; i++) - pPixel[i] = pImagePixel[i]; - Uint32* pCurrentPixel = (Uint32*)pPixel; - SDL_GetRGBA(*pCurrentPixel,pImage->format,&red,&green,&blue,&alpha); - if(SDL_BYTEORDER == SDL_BIG_ENDIAN) - { - Uint8* p = (Uint8*)pCurrentPixel; - Uint32 currentPixelConverted = p[0] << 16 | p[1] << 8 | p[2]; - SDL_GetRGBA(currentPixelConverted, pImage->format, &red, &green, &blue, &alpha); - } - - pBufferTemp[j++] = red; - pBufferTemp[j++] = green; - pBufferTemp[j++] = blue; - if (bAlpha) - { - pBufferTemp[j++] = alpha; - } - } - } - *ppBuffer = pBufferTemp; - SDL_FreeSurface(pImage); + osg::ref_ptr<osg::Image> image = osgDB::readImageFile(szFullFilename); + + if (!image.valid()) { + m_Logfile << "TERRAIN: Error reading image" << endl; + return; } - else - { - width = 0; - height = 0; - *ppBuffer = NULL; + + // Only accept RGB/RGBA for simplicity. This is sufficient for the current terrain and + // texture data used in CSP. + const GLenum pixel_format = image->getPixelFormat(); + if (pixel_format != GL_RGB && pixel_format != GL_RGBA) { + m_Logfile << "TERRAIN: Can't handle pixel format " << image->getPixelFormat() << endl; + return; } + + if (image->getDataType() != GL_UNSIGNED_BYTE) { + m_Logfile << "TERRAIN: Can't handle data type " << image->getDataType() << endl; + return; + } + + width = image->s(); + height = image->t(); + Uint8* image_data = (Uint8*)image->data(); + Uint8* pBufferTemp = new Uint8[width * height * (bAlpha ? 4 : 3)]; + + const int row_size = image->getRowSizeInBytes(); + const int bits_per_pixel = image->getPixelSizeInBits(); + + assert(bits_per_pixel == 24 || bits_per_pixel == 32); + const int bytes_per_pixel = image->getPixelSizeInBits() / 8; + + for (int i = (height - 1) * row_size, j = 0; i >= 0; i -= row_size) { + Uint8* row = image_data + i; + Uint8* end = row + row_size; + for (Uint8* pixel = row; pixel < end; pixel += bytes_per_pixel) { + pBufferTemp[j++] = pixel[0]; + pBufferTemp[j++] = pixel[1]; + pBufferTemp[j++] = pixel[2]; + if (bAlpha) pBufferTemp[j++] = pixel[3]; + } + } + + *ppBuffer = pBufferTemp; } |