#include
#include
#include "jpeglib.h"
class Image {
public:
int sx,sy;
int bad;
unsigned char *data; // RGB
Image( const char *fn );
Image( int w, int h );
~Image();
void write( const char *fn );
unsigned char &at( int x, int y, int c ) {
return data[ (y*sx + x)*3+c];
}
};
Image::Image( const char *fn )
{
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
bad = 0;
FILE * input_file;
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo);
if ((input_file = fopen( fn, "r" )) == NULL) {
fprintf( stderr, "Can't open %s\n", fn );
bad = 1;
sx = sy = 0;
data = NULL;
return;
}
jpeg_stdio_src(&cinfo, input_file);
jpeg_read_header(&cinfo, TRUE);
jpeg_start_decompress(&cinfo);
sx = cinfo.output_width;
sy = cinfo.output_height;
data = new unsigned char[ sx * sy * 3 ];
JSAMPLE **samp = new JSAMPLE*[sy];
for( int i=0; i=0; x-- )
samp[i][x] = samp[i][x/3];
}
delete []samp;
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
fclose(input_file);
if( jerr.num_warnings )
bad = 1;
}
Image::Image( int w, int h )
{
sx = w;
sy = h;
bad = 0;
data = new unsigned char[sx*sy*3];
}
Image::~Image()
{
delete[] data;
}
void Image::write( const char *fn )
{
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
FILE * output_file;
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
cinfo.in_color_space = JCS_RGB;
jpeg_set_defaults(&cinfo);
cinfo.image_width = sx;
cinfo.image_height = sy;
cinfo.input_components = 3;
output_file = fopen( fn, "w" );
jpeg_stdio_dest(&cinfo, output_file);
jpeg_start_compress(&cinfo, TRUE);
JSAMPLE **samp = new JSAMPLE*[sy];
for( int i=0; iwrite( fn );
}
extern "C" void jpeg_free( Image *im )
{
delete im;
}