00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "mxjpeg.h"
00017 #include<iostream>
00018
00019
00020 namespace mx
00021 {
00022
00023 bool mxJpeg::jpgOpen(FILE *fptr) {
00024
00025
00026 jpeg_decompress_struct deInfo;
00027 jpeg_error_mgr deErr;
00028
00029 deInfo.err = jpeg_std_error(&deErr);
00030 jpeg_create_decompress(&deInfo);
00031 jpeg_stdio_src(&deInfo, fptr);
00032 jpeg_read_header(&deInfo, TRUE);
00033 jpeg_start_decompress(&deInfo);
00034
00035 int width = deInfo.output_width, height = deInfo.output_height, depth = deInfo.num_components;
00036 int output_lines = deInfo.output_height;
00037
00038 if(data != 0) delete [] data;
00039 data = 0;
00040
00041 data = new unsigned char [ ( width * height ) * depth ];
00042 JSAMPROW row_ptr[1];
00043
00044 row_ptr[0] = (unsigned char*) malloc(deInfo.output_width*deInfo.num_components);
00045
00046 unsigned int i = 0;
00047 unsigned long location = 0;
00048
00049 while (deInfo.output_scanline < deInfo.image_height)
00050 {
00051 jpeg_read_scanlines(&deInfo, row_ptr, 1);
00052 for(i = 0; i <deInfo.image_width*deInfo.num_components; i++)
00053 data[location++] = row_ptr[0][i];
00054 }
00055
00056 jpeg_finish_decompress(&deInfo);
00057 jpeg_destroy_decompress(&deInfo);
00058 w = width, h = height, bytes = depth;
00059 fclose(fptr);
00060 free(row_ptr[0]);
00061
00062 return true;
00063 }
00064
00065 bool mxJpeg::jpgOpen(string fileName) {
00066
00067
00068 FILE *fptr = fopen(fileName.c_str(), "rb");
00069
00070 if(fptr == 0) return false;
00071
00072 return jpgOpen(fptr);
00073
00074 }
00075
00076
00077 SDL_Surface *mxJpeg::LoadJPG() {
00078
00079 SDL_Surface *surf = CreateBuffer(w,h);
00080
00081
00082 if(SDL_MUSTLOCK(surf)) SDL_LockSurface(surf);
00083
00084
00085 unsigned int *pixels = (unsigned int*) surf->pixels;
00086 unsigned int *buf = (unsigned int *) data;
00087 unsigned int pos = 0;
00088 unsigned char *dat = (unsigned char*) data;
00089
00090 while ( pos < (w*h) ) {
00091 unsigned char *v = dat;
00092 pixels[pos++] = SDL_MapRGB(surf->format, v[0], v[1], v[2]);
00093 dat += bytes;
00094 }
00095
00096 if(SDL_MUSTLOCK(surf)) SDL_UnlockSurface(surf);
00097
00098 return surf;
00099 }
00100
00101 void mxJpeg::jpgClose() {
00102
00103 if(data) delete [] data;
00104 data = 0;
00105 }
00106
00107
00108 }
00109
00110