#include <stdio.h>
#include <setjmp.h>
#include "jpeglib.h"
#include "jerror.h"
struct my_error_mgr
{
struct jpeg_error_mgr pub; /* "public" fields */
jmp_buf setjmp_buffer; /* for return to caller */
};
typedef struct my_error_mgr * my_error_ptr;
void my_error_exit (j_common_ptr cinfo)
{
my_error_ptr myerr = (my_error_ptr) cinfo->err;
(*cinfo->err->output_message) (cinfo);
longjmp(myerr->setjmp_buffer, 1);
}
int read_JPEG_file (char * filename, char *outfilename)
{
struct jpeg_decompress_struct cinfo;
struct my_error_mgr jerr;
FILE * infile; /* source file */
FILE * outfile;
JSAMPARRAY buffer; /* Output row buffer */
int row_stride; /* physical row width in output buffer */
if ((infile = fopen(filename, "rb")) == NULL)
{
fprintf(stderr, "can't open %s/n", filename);
return 0;
}
if ((outfile = fopen(outfilename, "wb")) == NULL)
{
fprintf(stderr, "can't open %s/n", outfilename);
return 0;
}
cinfo.err = jpeg_std_error(&jerr.pub);
jerr.pub.error_exit = my_error_exit;
if (setjmp(jerr.setjmp_buffer))
{
printf("Eror/n");
jpeg_destroy_decompress(&cinfo);
fclose(infile);
return 0;
}
jpeg_create_decompress(&cinfo);
jpeg_stdio_src(&cinfo, infile);
jpeg_read_header(&cinfo, TRUE);
printf("image width is %d/n", cinfo.image_width); /* nominal image width (from SOF marker) */
printf("image height is %d/n", cinfo.image_height); /* nominal image width (from SOF marker) */
printf("numcom is %d/n", cinfo.num_components); /* nominal image width (from SOF marker) */
jpeg_start_decompress(&cinfo);
printf("image width is %d/n", cinfo.output_width); /* nominal image width (from SOF marker) */
printf("image height is %d/n", cinfo.output_height); /* nominal image width (from SOF marker) */
printf("numcom is %d/n", cinfo.output_components); /* nominal image width (from SOF marker) */
row_stride = cinfo.output_width * cinfo.output_components;
buffer = (*cinfo.mem->alloc_sarray) ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
while (cinfo.output_scanline < cinfo.output_height)
{
jpeg_read_scanlines(&cinfo, buffer, 1);
//put_scanline_someplace(buffer[0], row_stride);
fwrite(buffer[0], 1, row_stride, outfile);
}
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
fclose(infile);
fclose(outfile);
return 1;
}
int main(int argc, char **argv)
{
if(argc<3)
{
printf("Input Error/n");
return 0;
}
read_JPEG_file(argv[1], argv[2]);
return 0;
}
decoder jpeg to rgb 24, using libjpeg
最新推荐文章于 2022-09-13 10:17:48 发布