利用jpeglib库进行图像压缩和解压缩
来源:互联网 发布:mac注销appstore 编辑:程序博客网 时间:2024/06/09 19:22
int Compress::compress_rgb_to_jpeg(unsigned char *jpeg_buffer, void *buffer, int width, int height, unsigned long* size, int quality) { struct jpeg_compress_struct cinfo; struct jpeg_error_mgr jerr; JSAMPROW row_pointer; FILE * outfile; static int written; unsigned char* out_buffer = NULL; unsigned long out_size; cinfo.err = jpeg_std_error (&jerr); jpeg_create_compress (&cinfo); /*if ((outfile = fopen("C:\\dest.jpg", "wb")) == NULL) { return 0; } jpeg_stdio_dest (&cinfo, outfile); */ //dest_buffer(&cinfo, jpeg_buffer, size, &written); jpeg_mem_dest(&cinfo, &out_buffer, size); cinfo.image_width = width; cinfo.image_height = height; cinfo.input_components = 3; cinfo.in_color_space = JCS_RGB; jpeg_set_defaults(&cinfo); /* cinfo.num_components = 1; cinfo.jpeg_color_space = JCS_GRAYSCALE;*/ cinfo.num_components = 3; cinfo.jpeg_color_space = JCS_YCbCr; cinfo.data_precision = 8; jpeg_set_quality (&cinfo, quality, TRUE); jpeg_start_compress (&cinfo, TRUE); char* path = (char*)buffer; char temp = 0; /*for (int i=0; i < width*height*3; i+=3) { temp = *(path+i); *(path+i) = *(path+i+2); *(path+i+2) = temp; }*/ /*for (int i=0, j=0; j < width*height*3; i+=3, j+=4) { *(path+i)=*(path+j+2); *(path+i+1)=*(path+j+1); *(path+i+2)=*(path+j); } */ while (cinfo.next_scanline < height) { row_pointer = (JSAMPROW)buffer + cinfo.next_scanline*width*3; jpeg_write_scanlines (&cinfo, &row_pointer, 1); } jpeg_finish_compress (&cinfo); if (jpeg_buffer != NULL) { for(int i = 0; i < *size; i++) { jpeg_buffer[i] = out_buffer[i]; } } jpeg_destroy_compress (&cinfo); if(NULL != out_buffer) { free(out_buffer); out_buffer = NULL; } return 1 ;}int Decompress::Decompress_jpeg_to_rgb(unsigned char *rgb_buffer, void *jpeg_buffer, int width, int height, unsigned long jpeg_size){struct jpeg_decompress_struct cinfo;struct jpeg_error_mgr jerr;FILE * infile;JSAMPARRAY buffer;int row_stride; //绑定标准错误处理结构cinfo.err = jpeg_std_error(&jerr); //初始化JPEG对象jpeg_create_decompress(&cinfo);//指定图像文件/*if ((infile = fopen("C:\\sample.jpg", "rb")) == NULL){return;}jpeg_stdio_src(&cinfo, infile);*/jpeg_mem_src( &cinfo, (unsigned char *)jpeg_buffer, jpeg_size );//读取图像信息(void) jpeg_read_header(&cinfo, TRUE);//设定解压缩参数,此处我们将图像长宽缩小为原图的1/2//cinfo.scale_num=1;//cinfo.scale_denom=2; cinfo.num_components = 3;cinfo.jpeg_color_space = JCS_YCbCr;cinfo.data_precision = 8;/*cinfo.out_color_space = JCS_RGB;cinfo.image_width = width;cinfo.image_height = height;*/ //开始解压缩图像(void) jpeg_start_decompress(&cinfo);//分配缓冲区空间row_stride = cinfo.output_width * cinfo.output_components;//printf("cinfo.output_width = %d\n",cinfo.output_width);//printf("cinfo.output_components = %d\n",cinfo.output_components);buffer = (*cinfo.mem->alloc_sarray)((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);//读取数据while (cinfo.output_scanline < cinfo.output_height){(void) jpeg_read_scanlines(&cinfo, buffer, 1);//output_scanline是从1开始,所以需要减1int line=cinfo.output_scanline-1;memcpy(rgb_buffer+line*row_stride,buffer[0],row_stride);}//结束解压缩操作(void) jpeg_finish_decompress(&cinfo);//释放资源jpeg_destroy_decompress(&cinfo);//fclose(infile);return 0;}
0 0
- 利用jpeglib库进行图像压缩和解压缩
- 利用jpeglib压缩图像为jpg格式
- 利用jpeglib压缩图像为jpg格式
- 利用jpeglib压缩图像为jpg格式
- 利用SharpZipLib进行压缩和解压缩
- 利用jpeglib压缩和解压图像为jpg格式
- 利用SharpZipLib进行文件的压缩和解压缩
- 利用SharpZipLib进行文件的压缩和解压缩
- 利用SharpZipLib进行文件的压缩和解压缩
- C#利用SharpZipLib进行文件的压缩和解压缩
- jpeglib库实现MJPEG图像的压缩与解压缩
- JPEG图像压缩和解压缩操作
- 利用7z.exe压缩和解压缩
- .net 利用 GZipStream 压缩和解压缩
- C#利用WinRAR实现压缩和解压缩
- C#调用winrar进行压缩和解压缩
- 使用Java原生进行压缩和解压缩
- 在C#中利用SharpZipLib进行文件的压缩和解压缩
- 详细介绍Word转换为PDF的方法步骤
- 欢迎使用CSDN-markdown编辑器
- JVM基础 之Java HotSpot虚拟机中的内存管理
- QT事件(4)
- Python 创建空列表的字典
- 利用jpeglib库进行图像压缩和解压缩
- 微信协议分析] 文本消息
- JQuery Ajax Post实例
- 开源项目整理
- Linux文件权限管理
- Missing Number
- JVM基础 之The Java HotSpot Performance Engine Architecture
- Python 替换列表元素
- android:layout_gravity 居中布局