1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
| Picture* Picture::readJpegFile(const char* pFile) {
cout<<endl<<"****read jpeg****"<<endl;
unsigned int x;
unsigned int y;
unsigned int d;
unsigned int width;
unsigned int height;
unsigned int depth;
struct jpeg_decompress_struct cinfo;
//struct my_error_mgr jerr;
jpeg_error_mgr jerr;
FILE* infile;
JSAMPARRAY buffer;
Picture* result;
//cout<<endl<<"**Trying to read "<< pFile<<endl;
if ((infile = fopen(pFile, "rb")) == NULL) {
cout<<"Can't open image :"<< pFile<<endl ;
}
cinfo.err = jpeg_std_error(&jerr);
jerr.error_exit = my_error_exit;
// Not trivial : system specifications (cf http://www.cs.utk.edu/~plank/plank/classes/cs360/360/notes/Setjmp/lecture.html or http://www.cs.utk.edu/~plank/plank/classes/cs360/360/notes/Setjmp/lecture.html)
// the following line is executed after a longjmp call !!
if (setjmp(setjmp_buffer) == 1) {
jpeg_destroy_decompress(&cinfo);
fclose(infile);
width = 0;
height = 0;
depth = 0;
cout<<endl<<"Picture "<< pFile<< " doesn't seem to be a valid jpeg file";
}
jpeg_create_decompress(&cinfo);
jpeg_stdio_src(&cinfo, infile);
jpeg_read_header(&cinfo, TRUE);
jpeg_start_decompress(&cinfo);
width = cinfo.output_width;
height = cinfo.output_height;
depth = cinfo.output_components;
result = new Picture(width, height);
result->setFile(pFile);
buffer = (*cinfo.mem->alloc_sarray) ((j_common_ptr) &cinfo, JPOOL_IMAGE, width*depth, 1);
y = 0;
if (depth == 1) {
while (cinfo.output_scanline < cinfo.output_height) {
jpeg_read_scanlines(&cinfo, buffer, 1);
for (x=0; x<width; x++) {
PixelValue v = ((PixelValue) (buffer[0][x]))/PixelValue(MAXJSAMPLE+1);
result->setRGBPixelValue(x, y, RED_CHANNEL, v);
result->setRGBPixelValue(x, y, GREEN_CHANNEL, v);
result->setRGBPixelValue(x, y, BLUE_CHANNEL, v);
}
y++;
}
} else if (depth != 3) {
zap(result);
cout<<endl<<"Picture "<< pFile<<" has color depth = "<<depth;
} else {
while (cinfo.output_scanline < cinfo.output_height) {
jpeg_read_scanlines(&cinfo, buffer, 1);
for (d=0; d<depth; d++) {
for (x=0; x<width; x++) {
cout<<endl<<"buffer = "<<((PixelValue) (buffer[0][x*depth+d]))/PixelValue(MAXJSAMPLE+1)<<endl ;
result->setRGBPixelValue(x, y, d, ((PixelValue) (buffer[0][x*depth+d]))/PixelValue(MAXJSAMPLE+1));
}
}
y++;
}
}
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
cout<<endl<<"din decompress"<<endl ;
fclose(infile);
return result;
}
} |
Partager