libjpeg-turbo 压缩照片 函数使用

libjpeg-turbo 是 libjpeg 的一个分支,它使用 SIMD 指令加速 JPEG 压缩和解压缩过程。libjpeg-turbo 的 API 与 libjpeg 的 API 兼容,所以如果你熟悉 libjpeg,使用 libjpeg-turbo 应该会很直接。

以下是使用 libjpeg-turbo API 压缩 JPEG 图像的一个基本示例:

#include <stdio.h>
#include <jpeglib.h>
#include <setjmp.h>

GLOBAL(void)
write_JPEG_file (char * filename, int quality, JSAMPLE * image_buffer, int image_width, int image_height)
{
  struct jpeg_compress_struct cinfo;
  struct jpeg_error_mgr jerr;
  
  FILE * outfile;               /* target file */
  JSAMPROW row_pointer[1];      /* pointer to JSAMPLE row[s] */
  int row_stride;               /* physical row width in image buffer */

  /* Step 1: allocate and initialize JPEG compression object */
  cinfo.err = jpeg_std_error(&jerr);
  jpeg_create_compress(&cinfo);

  /* Step 2: specify data destination (eg, a file) */
  if ((outfile = fopen(filename, "wb")) == NULL) {
    fprintf(stderr, "Can't open %s\n", filename);
    exit(1);
  }
  jpeg_stdio_dest(&cinfo, outfile);

  /* Step 3: set parameters for compression */
  cinfo.image_width = image_width;      /* image width and height, in pixels */
  cinfo.image_height = image_height;
  cinfo.input_components = 3;           /* # of color components per pixel */
  cinfo.in_color_space = JCS_RGB;       /* colorspace of input image */

  jpeg_set_defaults(&cinfo);
  jpeg_set_quality(&cinfo, quality, TRUE /* limit to baseline-JPEG values */);

  /* Step 4: Start compressor */
  jpeg_start_compress(&cinfo, TRUE);

  /* Step 5: while (scan lines remain to be written) */
  /*           jpeg_write_scanlines(...); */
  row_stride = image_width * 3; /* JSAMPLEs per row in image_buffer */

  while (cinfo.next_scanline < cinfo.image_height) {
    row_pointer[0] = &image_buffer[cinfo.next_scanline * row_stride];
    (void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
  }

  /* Step 6: Finish compression */
  jpeg_finish_compress(&cinfo);

  /* Step 7: release JPEG compression object */
  fclose(outfile);
  jpeg_destroy_compress(&cinfo);
}

/* Call the function */
int main() {
  /* You will need to fill in the image_buffer with your image data */
  JSAMPLE * image_buffer; // Pointer to large array of R,G,B-order data
  int image_height; // Number of rows in image
  int image_width;  // Number of columns in image
  
  // ... (initialize image_buffer, image_height, and image_width) ...

  write_JPEG_file("output.jpg", 75, image_buffer, image_width, image_height);

  // ... (free resources) ...

  return 0;
}

请注意,这段代码仅作为一个示例,它没有进行错误处理,并且假设图像数据是以 RGB 格式存储在 image_buffer 中。在实际应用中,你需要确保正确地处理所有可能的错误情况,并正确地管理内存和文件资源。

在使用这段代码之前,你需要确保已经安装了 libjpeg-turbo 库,并且在编译时链接了适当的库文件。例如,使用 gcc 你可能需要添加 -ljpeg 到编译命令中,以确保链接了 JPEG 库。

  • 8
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值