mx::mxImage Class Reference

#include <mximage.h>

List of all members.

Public Member Functions

 mxImage ()
SDL_Surface * loadIMG (string filename)
bool loadIMG (mx::mxSurface &surface, string filename)
void saveJPG (SDL_Surface *surf, string filename)


Detailed Description

image class for jpeg / png graphics

Definition at line 28 of file mximage.h.


Constructor & Destructor Documentation

mx::mxImage::mxImage (  )  [inline]

mxImage default constructor

Definition at line 31 of file mximage.h.

00031 { }


Member Function Documentation

bool mx::mxImage::loadIMG ( mx::mxSurface surface,
string  filename 
)

loadIMG load a image to a mxSurface

Parameters:
mxSurface surface to copy loaded image to
filename file of image to load
Returns:
boolean value true for success false for failure

Definition at line 54 of file mximage.cpp.

References loadIMG().

00054                                                                 {
00055 
00056                 SDL_Surface *surface = loadIMG(filename);
00057                 if(surface == 0) return false;
00058                 surf = surface;
00059                 return true;
00060         }

SDL_Surface * mx::mxImage::loadIMG ( string  filename  ) 

loadIMG loads a image proper loading function selected by filename extension

Parameters:
filename name of file to load
Returns:
loaded SDL_Surface *

Definition at line 21 of file mximage.cpp.

References mx::mxJpeg::jpgClose(), mx::mxJpeg::jpgOpen(), mx::mxJpeg::LoadJPG(), mx::mxPng::LoadPNG(), mx::mxPng::pngClose(), and mx::mxPng::pngOpen().

Referenced by loadIMG().

00021                                                      {
00022 
00023                 std::string prefix = filename.substr(filename.rfind(".")+1, filename.length() - filename.rfind(".")-1 );
00024 
00025                 unsigned int i;
00026                 for(i = 0; i < prefix.length(); i++) prefix[i] = tolower(prefix[i]);
00027 
00028                 if(prefix == "bmp")
00029                         return SDL_LoadBMP(filename.c_str());
00030 
00031                 else if(prefix == "jpg" || prefix == "jpeg") {
00032 
00033                         mx::mxJpeg j;
00034                         if( j.jpgOpen(filename) == false ) return 0;
00035                         SDL_Surface *surf = j.LoadJPG();
00036                         j.jpgClose();
00037                         return surf;
00038                 }
00039 
00040                 else if(prefix == "png") {
00041                         mx::mxPng p;
00042 
00043                         if( p.pngOpen(filename) == false ) return 0;
00044                         SDL_Surface *surf = p.LoadPNG();
00045                         p.pngClose();
00046                         return surf;
00047                 }
00048 
00049                 return 0;
00050 
00051         }

void mx::mxImage::saveJPG ( SDL_Surface *  surf,
string  filename 
)

saveJPG save a SDL_Surface as a Jpeg graphic file

Parameters:
surf SDL_Surface * to save
filename filename to save as

Definition at line 62 of file mximage.cpp.

00062                                                                 {
00063 
00064                 jpeg_compress_struct comp_info;
00065                 jpeg_error_mgr jerr;
00066 
00067                 JSAMPROW row_ptr[1];
00068 
00069                 FILE *fptr = fopen(filename.c_str(), "w");
00070                 if(!fptr) return;
00071 
00072                 comp_info.err = jpeg_std_error(&jerr);
00073                 jpeg_create_compress(&comp_info);
00074                 jpeg_stdio_dest(&comp_info, fptr);
00075 
00076                 comp_info.image_width = surf->w;
00077                 comp_info.image_height = surf->h;
00078                 comp_info.input_components = 3;
00079                 comp_info.in_color_space = JCS_RGB;
00080 
00081                 jpeg_set_defaults(&comp_info);
00082                 jpeg_start_compress(&comp_info, TRUE);
00083 
00084 
00085 
00086                 if(SDL_MUSTLOCK(surf)) SDL_LockSurface(surf);
00087 
00088 
00089                 unsigned char *buffer = (unsigned char*) calloc (1,  surf->w * surf->h * 3 );
00090                 unsigned int counter = 0;
00091 
00092                 unsigned char *ptr = buffer;
00093                 unsigned char *pix = (unsigned char*)surf->pixels;
00094 
00095                 while(counter < (surf->w*surf->h)) {
00096 
00097 
00098                         unsigned int *ptr_val = (unsigned int *) pix;
00099                         SDL_GetRGB(*ptr_val, surf->format, &ptr[0], &ptr[1], &ptr[2]);
00100                         pix += surf->format->BytesPerPixel;
00101                         ptr += 3;
00102                         counter++;
00103                 }
00104 
00105                 while( comp_info.next_scanline < comp_info.image_height ) {
00106                         row_ptr[0] = &buffer[ comp_info.next_scanline * comp_info.image_width * comp_info.input_components];
00107                         jpeg_write_scanlines (&comp_info, row_ptr, 1);
00108                 }
00109 
00110 
00111                 free(buffer);
00112 
00113 
00114                 if(SDL_MUSTLOCK(surf)) SDL_UnlockSurface(surf);
00115 
00116                 jpeg_finish_compress(&comp_info);
00117                 jpeg_destroy_compress(&comp_info);
00118                 fclose(fptr);
00119 
00120         }


The documentation for this class was generated from the following files:

Generated on Wed Jun 10 14:52:02 2009 for libmx by  doxygen 1.5.8