jpeg数据压缩/解压算法代码c
时间: 2023-10-04 22:08:33 浏览: 39
以下是一个简单的基于libjpeg的压缩/解压示例代码:
压缩:
```c
#include <stdio.h>
#include <jpeglib.h>
void compress(char *infilename, char *outfilename, int quality) {
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
FILE *infile, *outfile;
JSAMPROW row_pointer[1];
int row_stride;
JSAMPLE *buffer;
int image_width, image_height;
if ((infile = fopen(infilename, "rb")) == NULL) {
fprintf(stderr, "can't open %s\n", infilename);
return;
}
if ((outfile = fopen(outfilename, "wb")) == NULL) {
fprintf(stderr, "can't open %s\n", outfilename);
fclose(infile);
return;
}
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
jpeg_stdio_dest(&cinfo, outfile);
jpeg_read_header(&cinfo, TRUE);
cinfo.optimize_coding = TRUE;
cinfo.comp_quality = quality;
jpeg_start_compress(&cinfo, TRUE);
image_width = cinfo.image_width;
image_height = cinfo.image_height;
row_stride = image_width * cinfo.input_components;
buffer = (JSAMPLE *)malloc(row_stride);
while (cinfo.next_scanline < cinfo.image_height) {
fread(buffer, row_stride, 1, infile);
row_pointer[0] = buffer;
jpeg_write_scanlines(&cinfo, row_pointer, 1);
}
free(buffer);
jpeg_finish_compress(&cinfo);
fclose(infile);
fclose(outfile);
jpeg_destroy_compress(&cinfo);
}
```
解压:
```c
#include <stdio.h>
#include <jpeglib.h>
void decompress(char *infilename, char *outfilename) {
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
FILE *infile, *outfile;
JSAMPLE *buffer;
int row_stride;
if ((infile = fopen(infilename, "rb")) == NULL) {
fprintf(stderr, "can't open %s\n", infilename);
return;
}
if ((outfile = fopen(outfilename, "wb")) == NULL) {
fprintf(stderr, "can't open %s\n", outfilename);
fclose(infile);
return;
}
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo);
jpeg_stdio_src(&cinfo, infile);
jpeg_read_header(&cinfo, TRUE);
jpeg_start_decompress(&cinfo);
row_stride = cinfo.output_width * cinfo.output_components;
buffer = (JSAMPLE *)malloc(row_stride);
while (cinfo.output_scanline < cinfo.output_height) {
jpeg_read_scanlines(&cinfo, &buffer, 1);
fwrite(buffer, row_stride, 1, outfile);
}
free(buffer);
jpeg_finish_decompress(&cinfo);
fclose(infile);
fclose(outfile);
jpeg_destroy_decompress(&cinfo);
}
```
其中,`compress`函数将指定的输入文件压缩为输出文件,`quality`参数指定了压缩质量(0-100)。`decompress`函数将指定的输入文件解压为输出文件。
使用这些函数,你可以轻松地将JPEG图像压缩或解压。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)