c - 使用 libjpeg 将 rgb char* 保存到 jpeg 图像中

标签 c jpeg rgb decode libjpeg

我正在尝试将 rgb 字节数组保存到 jpg 文件中。

我按照此 page 中的说明进行操作这是我的代码

//rgb is the image
//rgb->imageData is char* to the rgb data
//rgb->imageSize is the image size which is 640*480*3

struct jpeg_compress_struct cinfo;
  struct jpeg_error_mgr jerr;

  /* this is a pointer to one row of image data */
  FILE *outfile = fopen( "file.jpeg", "wb" );

  cinfo.err = jpeg_std_error( &jerr );
  jpeg_create_compress(&cinfo);
  jpeg_stdio_dest(&cinfo, outfile);

  /* Setting the parameters of the output file here */
  cinfo.image_width = 640;//width;
  cinfo.image_height = 480;//height;
  cinfo.input_components = 3;
  cinfo.in_color_space = JCS_RGB;

  jpeg_set_defaults( &cinfo );
  /* Now do the compression .. */
  jpeg_start_compress( &cinfo, TRUE );

  JSAMPROW buffer;
  for(int i=0;i<rgb->imageSize;i+=640)
  {
      memcpy(buffer,
             (JSAMPROW)rgb->imageData+i,
             640);//segmentation fault here
      jpeg_write_scanlines( &cinfo, &buffer, 1 );
  }
  jpeg_finish_compress( &cinfo );
  jpeg_destroy_compress( &cinfo );
  fclose( outfile );

我遇到段错误,当尝试 buffer = rgb->image data 时,我收到 libjpeg 错误,扫描线太多并且没有任何内容写入文件,我的代码有什么问题吗??

我可以在网上找到一堆 libjpeg 示例,但找不到内存到内存(char* 到 char*)示例。

我还有一个次要问题: 我有一个非常优化的 YUV 到 rgb 函数,将 YUV(更压缩)图像转换为 RGB(未压缩)然后将 RGB 转换为 jpeg 好吗?或者只在 libjpeg 中使用 YUV 转 Jpeg ??

最佳答案

JSAMPROW 类型是如何声明的?试试这个:

memcpy(&buffer, (JSAMPROW)rgb->imageData+i, 640);

关于c - 使用 libjpeg 将 rgb char* 保存到 jpeg 图像中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16740165/

相关文章:

python - Python 中 Tkinter Canvas 上 .jpg 图像的滚动条

python - 通过 Picasa API 上传图片时如何防止 JPEG 压缩?

swift - 从 UIColor 获取色调会产生错误的结果

c - 卡尔曼滤波器 - 四元数 - 角度传感器

c - 如果一个字符串是其他字符串的旋转。所有测试用例未运行

c - 从 C 访问 Lua 子表字段

c - 如何知道文件在linux中被修改

c# - 将字节数组保存为 Pgn 或 Jpg

objective-c - 将 Obj-C RGBtoHSV() 函数转换为 Swift。 min = MIN( r, MIN(g, b )); ETC

java - 如何将RGB图像转换为CIELUV色彩空间?