if ((infile = fopen(jpg_filename, "rb")) == NULL)
{
fprintf(stderr, "open %s failed/n", jpg_filename);
return(-1);
}
/*
init jpeg decompress object error handler
*/
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo);
/*
* bind jpeg decompress object to infile
*/
jpeg_stdio_src(&cinfo, infile);
/*
* read jpeg header
*/
jpeg_read_header(&cinfo, TRUE);
/*
* decompress process.
* note: after jpeg_start_decompress() is called
* the dimension infomation will be known,
* so allocate memory buffer for scanline immediately
*/
jpeg_start_decompress(&cinfo);
if ((cinfo.output_width > 320) ||
(cinfo.output_height > 240)) {
printf("too large JPEG file,cannot display/n");
return (-1);
}
buffer = (unsigned char *) malloc(cinfo.output_height * cinfo.output_width * cinfo.output_components);
buffer_tmp = buffer;
y = 0;
while (cinfo.output_scanline < cinfo.output_height) {
jpeg_read_scanlines(&cinfo, &buffer_tmp, 1);
buffer_tmp += cinfo.output_width *cinfo.output_components;
// next scanlin
//printf("read %d/n",y++);
}
/*
* finish decompress, destroy decompress object
*/
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
/*
* release memory buffer
*/
//free(buffer);
/*
* close jpeg inputing file
*/
fclose(infile);
return buffer;