记一次 libjpg 压缩后出现条纹图像的事故代码

这是一个很有趣的现象,严格来说不是错误,经过 libjpeg 压缩后的图像如果下述的这些图。

![](https://img2020.cnblogs.com/blog/1641852/202101/1641852-20210113195153682-1780073691.png

原图实际上是这样的。

这就表示你压缩的时候图像行数给少了。


static PyObject *PyJpegCompress(char *inData, int width, int height, int channels, int color_space, int quality)
{
  struct jpeg_compress_struct cinfo;
  struct jpeg_error_mgr jerr;
  long unsigned int outSize = 0;
  uint8_t *outbuffer = NULL;
  JSAMPROW row_pointer[1];

  cinfo.err = jpeg_std_error(&jerr);
  jpeg_create_compress(&cinfo);
  jpeg_mem_dest(&cinfo, &outbuffer, &outSize);
  cinfo.image_width = width;
  cinfo.image_height = height;
  cinfo.input_components = channels;  // 3 / 1
  cinfo.in_color_space = color_space; // JCS_RGB / JCS_GRAYSCALE
  jpeg_set_defaults(&cinfo);
  jpeg_set_quality(&cinfo, quality, TRUE); // default 75
  jpeg_start_compress(&cinfo, TRUE);
  int row_stride = width * 3; // 看这里,这里是 3 ,而不是 0 1 2 之类的。
  while (cinfo.next_scanline < cinfo.image_height)
  {
    row_pointer[0] = (uint8_t *)&inData[cinfo.next_scanline * row_stride];
    (void)jpeg_write_scanlines(&cinfo, row_pointer, 1);
  }
  jpeg_finish_compress(&cinfo);

  PyObject *bytes = PyBytes_FromStringAndSize((const char *)outbuffer, outSize);

  if (NULL != outbuffer)
    free(outbuffer), outbuffer = NULL;

  jpeg_destroy_compress(&cinfo);
  return bytes;
}


posted @ 2021-01-13 19:55  Juwan  阅读(48)  评论(1编辑  收藏