当前位置: 技术问答>linux和unix
QImage如何压缩成Jpeg?在线等,急!
来源: 互联网 发布时间:2015-05-22
本文导语: 请问如何将QImage的文件压缩成Jpeg格式(无文件头,用于传输的,不是写进文件)以及如何反向解压成QImage,谢谢,非常着急,希望得到高手指点 | 给你一段我写的代码,需要#include link时候加上...
请问如何将QImage的文件压缩成Jpeg格式(无文件头,用于传输的,不是写进文件)以及如何反向解压成QImage,谢谢,非常着急,希望得到高手指点
|
给你一段我写的代码,需要#include
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);
}
int count_for_jpeg;
int add_scanline_to_rgbbuffer(unsigned char* linebuffer,int length,unsigned char* rgbbuffer)
{
// fprintf(stderr,"length = %dn",length);
memcpy((rgbbuffer+length*(count_for_jpeg++)),linebuffer,length);
return 0;
}
int DecompressJpeg(const char* input,unsigned char* rgbbuffer)//input为jpeg文件名,rgbbuffer为解码后的rgb原始数据
{
count_for_jpeg = 0;
struct jpeg_decompress_struct cinfo;
struct my_error_mgr jerr;
/* More stuff */
FILE * infile; /* source file */
JSAMPARRAY buffer; /* Output row buffer */
int row_stride; /* physical row width in output buffer */
char* filename = (char*)input;
if ((infile = fopen(filename, "rb")) == NULL)
{
fprintf(stderr, "can't open %sn", 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);
/* Step 3: read file parameters with jpeg_read_header() */
(void) jpeg_read_header(&cinfo, TRUE);
/* Step 5: Start decompressor */
(void) jpeg_start_decompress(&cinfo);
/* 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 = %dn",cinfo.output_scanline,cinfo.output_height);
while (cinfo.output_scanline
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);
}
int count_for_jpeg;
int add_scanline_to_rgbbuffer(unsigned char* linebuffer,int length,unsigned char* rgbbuffer)
{
// fprintf(stderr,"length = %dn",length);
memcpy((rgbbuffer+length*(count_for_jpeg++)),linebuffer,length);
return 0;
}
int DecompressJpeg(const char* input,unsigned char* rgbbuffer)//input为jpeg文件名,rgbbuffer为解码后的rgb原始数据
{
count_for_jpeg = 0;
struct jpeg_decompress_struct cinfo;
struct my_error_mgr jerr;
/* More stuff */
FILE * infile; /* source file */
JSAMPARRAY buffer; /* Output row buffer */
int row_stride; /* physical row width in output buffer */
char* filename = (char*)input;
if ((infile = fopen(filename, "rb")) == NULL)
{
fprintf(stderr, "can't open %sn", 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);
/* Step 3: read file parameters with jpeg_read_header() */
(void) jpeg_read_header(&cinfo, TRUE);
/* Step 5: Start decompressor */
(void) jpeg_start_decompress(&cinfo);
/* 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 = %dn",cinfo.output_scanline,cinfo.output_height);
while (cinfo.output_scanline
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
站内导航:
特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!