非常感谢热心的高手,我也看过jpeglib.h,里面好象必须设置destination,而且参数必须是FILE*,我现在想法是能够对一个buffer(raw image, RGB or BGR)直接进行JPEG压缩,并且能够反向解码,而且最好高效的,不是对文件操作,我不需要读jpeg文件,也不用写进文件,只是buffer到buffer的操作,谢谢
给你一段我写的代码,需要#include <jpeglib.h>
link时候加上-ljpeg即可
typedef struct my_error_mgr * my_error_ptr;
/*
* Here's the routine that will replace the standard error_exit method:
*/
METHODDEF(void)
my_error_exit (j_common_ptr cinfo)
{
/* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
my_error_ptr myerr = (my_error_ptr) cinfo->err;
/* Always display the message. */
/* We could postpone this until after returning, if we chose. */
(*cinfo->err->output_message) (cinfo);
/* Return control to the setjmp point */
longjmp(myerr->setjmp_buffer, 1);
}
char* filename = (char*)input;
if ((infile = fopen(filename, "rb")) == NULL)
{
fprintf(stderr, "can't open %s\n", filename);
return -1;
}
/* Step 1: allocate and initialize JPEG decompression object */
/* We set up the normal JPEG error routines, then override error_exit. */
cinfo.err = jpeg_std_error(&jerr.pub);
jerr.pub.error_exit = my_error_exit;
/* Establish the setjmp return context for my_error_exit to use. */
if (setjmp(jerr.setjmp_buffer))
{
/* If we get here, the JPEG code has signaled an error.
* We need to clean up the JPEG object, close the input file, and return.
*/
fprintf(stderr,"Error setjmp()\n");
jpeg_destroy_decompress(&cinfo);
return -1;
}
/* Now we can initialize the JPEG decompression object. */
jpeg_create_decompress(&cinfo);
/* Step 2: specify data source (eg, a file) */
jpeg_stdio_src(&cinfo, infile);
/* JSAMPLEs per row in output buffer */
row_stride = cinfo.output_width * cinfo.output_components;
/* Make a one-row-high sample array that will go away when done with image */
buffer = (*cinfo.mem->alloc_sarray)
((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
/* Step 6: while (scan lines remain to be read) */
//fprintf(stderr,"cinfo.output_scanline = %d,cinfo.output_height = %d\n",cinfo.output_scanline,cinfo.output_height);
while (cinfo.output_scanline < cinfo.output_height)
{
//fprintf(stderr,"cinfo.output_scanline = %d,cinfo.output_height = %d\n",cinfo.output_scanline,cinfo.output_height);
(void) jpeg_read_scanlines(&cinfo, buffer, 1);
/* Assume put_scanline_someplace wants a pointer and sample count. */
add_scanline_to_rgbbuffer(buffer[0], row_stride,rgbbuffer);
}