diff -Naur Irrlicht.old/CImageLoaderTGA.h Irrlicht/CImageLoaderTGA.h --- Irrlicht.old/CImageLoaderTGA.h 2005-03-01 14:43:45.142555619 +0100 +++ Irrlicht/CImageLoaderTGA.h 2005-03-08 18:18:38.000000000 +0100 @@ -7,6 +7,15 @@ #include "IImageLoader.h" +#include +#include "IrrCompileConfig.h" +#ifdef _IRR_COMPILE_WITH_LIBJPEG_ +extern "C" { +#include +#include +#include +} +#endif namespace irr { diff -Naur Irrlicht.old/CImageLoaderTGA.new Irrlicht/CImageLoaderTGA.new --- Irrlicht.old/CImageLoaderTGA.new 1970-01-01 01:00:00.000000000 +0100 +++ Irrlicht/CImageLoaderTGA.new 2005-03-09 14:15:37.000000000 +0100 @@ -0,0 +1,147 @@ +#include "CImageLoaderTGA.h" +#include +#include "CColorConverter.h" +#include "CImage.h" + +namespace irr +{ +namespace video +{ + +//! constructor +CImageLoaderTGA::CImageLoaderTGA() +{ + #ifdef _DEBUG + setDebugName("CImageLoaderTGA"); + #endif +} + + + +//! destructor +CImageLoaderTGA::~CImageLoaderTGA() +{ +} + + + +//! returns true if the file maybe is able to be loaded by this class +//! based on the file extension (e.g. ".tga") +bool CImageLoaderTGA::isALoadableFileExtension(const c8* fileName) +{ + return strstr(fileName, ".tga") != 0; +} + + +//! returns true if the file maybe is able to be loaded by this class +bool CImageLoaderTGA::isALoadableFileFormat(irr::io::IReadFile* file) +{ + #ifndef _IRR_COMPILE_WITH_LIBJPEG_ + + return false; + + #else + + if (!file) + return false; + + return true; + + #endif +} + + + +//! creates a surface from the file +IImage* CImageLoaderTGA::loadImage(irr::io::IReadFile* file) +{ + // allocate and initialize JPEG compression object + struct jpeg_compress_struct cinfo; + + //We have to set up the error handler first, in case the initialization + //step fails. (Unlikely, but it could happen if you are out of memory.) + //This routine fills in the contents of struct jerr, and returns jerr's + //address which we place into the link field in cinfo. + struct jpeg_error_mgr jerr; + cinfo.err = jpeg_std_error(&jerr); + + // Now we can initialize the JPEG compression object. + jpeg_create_compress(&cinfo); + + cinfo.in_color_space = JCS_RGB; + jpeg_set_defaults(&cinfo); + + // specify data source + cjpeg_source_ptr src_mgr; + + // Set up data pointer + src_mgr = jinit_read_targa(&cinfo); + src_mgr->input_file = fopen(file->getFileName(),"rb"); + (*src_mgr->start_input) (&cinfo, src_mgr); + + // Set the correct colorspace based on image information + jpeg_default_colorspace(&cinfo); + + // Get image data + u16 rowspan = cinfo.image_width * cinfo.num_components; + unsigned width = cinfo.image_width; + unsigned height = cinfo.image_height; + + bool greyscale; + + if (cinfo.jpeg_color_space == JCS_GRAYSCALE) + greyscale = true; + else + greyscale = false; + + // Allocate memory for buffer + u8 *output = new u8[rowspan * height]; + + unsigned numScanlines, rowsRead = 0; + + // We have to initialize even these things to allocate temp space + jpeg_stdio_dest(&cinfo, NULL); + jpeg_start_compress(&cinfo, TRUE); + + while( rowsRead < height ) + { + numScanlines = (*src_mgr->get_pixel_rows) (&cinfo, src_mgr); + // instead of compressing the buffer we copy it + memcpy(output+rowsRead*rowspan,*src_mgr->buffer,numScanlines*rowspan); + rowsRead += numScanlines; + } + + // Finish compression does not work so leave it untouched +// (void) jpeg_finish_compress(&cinfo); + + // convert image using the allocated output area (so do not delete it!) + ECOLOR_FORMAT format=ECF_R8G8B8; + switch (cinfo.num_components) + { + case 2: format=ECF_A1R5G5B5; + break; + case 4: format=ECF_A8R8G8B8; + break; + } + + // Release JPEG decompression object + // This is an important step since it releases a good deal of memory. + jpeg_destroy_compress(&cinfo); + + IImage* image = new CImage(format, + core::dimension2d(width, height), output); + + return image; +} + + + +//! creates a loader which is able to load tga images +IImageLoader* createImageLoaderTGA() +{ + return new CImageLoaderTGA(); +} + +} // end namespace video +} // end namespace irr +