libjpeg-turbo是一个效率很高的图片处理的C库,所以严格来说放在Go这里不太妥当,但是之后我们会把它封装成Go可以调用的类库。据说libjpeg-turbo在处理jpeg格式图片时比ImageMagick库效率高了三倍,参照示例代码写了一个缩略图的功能:
1 1 // 2 2 // main.cpp 3 3 // libjpegturbo 4 4 // 5 5 // Created by 张皓然 on 2018/4/17. 6 6 // Copyright © 2018年 张皓然. All rights reserved. 7 7 // 8 8 9 9 #include <iostream> 1010 #include "/opt/libjpeg-turbo/include/jpeglib.h" 1111 1212 using namespace std; 1313 1414 int compress_JPEG_file(string strImageName, string strDstImageName, int quality, int scale_num, int scale_denom) { 1515 //创建必要变量,后缀in是源图片信息,out是目标图片信息,buffer是缓冲区 1616 struct jpeg_decompress_struct cinfo_in; 1717 struct jpeg_compress_struct cinfo_out; 1818 struct jpeg_error_mgr jerr_in, jerr_out; 1919 FILE *infile, *outfile; 2020 JSAMPARRAY buffer; 2121 //表示每行的宽度,为width*components 2222 int row_stride; 2323 2424 //初始化对象 2525 jpeg_create_decompress(&cinfo_in); 2626 jpeg_create_compress(&cinfo_out); 2727 //绑定错误结构 2828 cinfo_in.err = jpeg_std_error(&jerr_in); 2929 cinfo_out.err = jpeg_std_error(&jerr_out); 3030 //C库方式打开图片文件,并填入cinfo对象 3131 if((infile = fopen(strImageName.c_str(), "rb")) == NULL) { 3232 fprintf(stderr, "can't open %s\n", strImageName.c_str()); 3333 return -1; 3434 } 3535 if((outfile = fopen(strDstImageName.c_str(), "wb")) == NULL) { 3636 fprintf(stderr, "can't open %s\n", strDstImageName.c_str()); 3737 return -1; 3838 } 3939 jpeg_stdio_src(&cinfo_in, infile); 4040 jpeg_stdio_dest(&cinfo_out, outfile); 4141 //读取文件信息 4242 jpeg_read_header(&cinfo_in, TRUE); 4343 4444 //设定文件压缩比例,M/N 4545 cinfo_in.scale_num = scale_num; 4646 cinfo_in.scale_denom = scale_denom; 4747 4848 //开始解压缩 4949 jpeg_start_decompress(&cinfo_in); 5050 5151 //将压缩后的信息存入cinfo_out中作为新图片的参数 5252 cinfo_out.image_height = cinfo_in.output_height; 5353 cinfo_out.image_width = cinfo_in.output_width; 5454 cinfo_out.input_components = 3; 5555 cinfo_out.in_color_space = JCS_RGB; 5656 jpeg_set_defaults(&cinfo_out); 5757 //设置图片质量 5858 jpeg_set_quality(&cinfo_out, quality, TRUE); 5959 //开始压缩 6060 jpeg_start_compress(&cinfo_out, TRUE); 6161 6262 //设定每行的宽度准备写入,并分配缓冲区空间 6363 row_stride = cinfo_in.output_width * cinfo_in.output_components; 6464 buffer = (*cinfo_in.mem -> alloc_sarray)((j_common_ptr)&cinfo_in, JPOOL_IMAGE, row_stride, 1); 6565 6666 while(cinfo_in.output_scanline < cinfo_in.output_height) { 6767 //从in中读取数据并写入out中 6868 jpeg_read_scanlines(&cinfo_in, buffer, 1); 6969 jpeg_write_scanlines(&cinfo_out, buffer, 1); 7070 } 7171 7272 //结束,释放变量 7373 jpeg_finish_decompress(&cinfo_in); 7474 jpeg_destroy_decompress(&cinfo_in); 7575 7676 jpeg_finish_compress(&cinfo_out); 7777 jpeg_destroy_compress(&cinfo_out); 7878 7979 fclose(infile); 8080 fclose(outfile); 8181 8282 return 0; 8383 } 8484 8585 int main() { 8686 clock_t start, finish; 8787 start = clock(); 8888 for(int i = 0; i < 100; i ++) compress_JPEG_file("/Users/zhanghaoran/Desktop/abc.jpg", "/Users/zhanghaoran/Desktop/abc_compress.jpg", 80, 1, 2); 8989 finish = clock(); 9090 cout << "Total : " << (double)(finish - start) / CLOCKS_PER_SEC << endl; 9191 }