您的位置:首页 > 运维架构 > Linux

Linux下jpeg解码方法

2011-01-06 22:47 357 查看
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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: