当前位置:  开发笔记 > 编程语言 > 正文

使用jpeglib进行JPEG压缩字节流

如何解决《使用jpeglib进行JPEG压缩字节流》经验,为你挑选了1个好方法。

我将JPEG压缩字节流存储在一个名为"Image"的变量中,我想将此字节流转换为RGB.

例如:unsigned char*Image;

我的问题是:有没有办法将"Image"传递给jpeg_stdio_src()来获取RGB颜色值?

任何人都可以告诉我如何使用jpeglib库从字节流"图像"中获取RGB?

谢谢



1> Mr Fooz..:

尝试这样的事情.这使用对象currImage来存储结果(此处未声明).

typedef struct {
  struct jpeg_source_mgr pub;   // public fields

  JOCTET * buffer;              // start of buffer
  boolean start_of_file;        // have we gotten any data yet?
} my_source_mgr;

typedef my_source_mgr * my_src_ptr;

static void jpg_memInitSource(j_decompress_ptr cinfo)
{
  my_src_ptr src = (my_src_ptr) cinfo->src;
  src->start_of_file = true;
}

static boolean jpg_memFillInputBuffer(j_decompress_ptr cinfo)
{
  my_src_ptr src = (my_src_ptr) cinfo->src;
  src->start_of_file = FALSE;
  return TRUE;
}

static void jpg_memSkipInputData(j_decompress_ptr cinfo, long num_bytes)
{
  my_src_ptr src = (my_src_ptr) cinfo->src;
  if (num_bytes > 0) {
    src->pub.next_input_byte += (size_t) num_bytes;
    src->pub.bytes_in_buffer -= (size_t) num_bytes;
  }
}

static void jpg_memTermSource(j_decompress_ptr cinfo)
{
  // no work necessary here
}

void decompressJpeg(uint8 const *compressed, size_t len)
{
  struct jpeg_decompress_struct cinfo;
  struct jpeg_error_mgr         jerr;
  my_src_ptr src;

  // ToDo: change error handling to work with our return codes
  cinfo.err = jpeg_std_error(&jerr);
  jpeg_create_decompress(&cinfo);

  cinfo.src = (struct jpeg_source_mgr *)
    (*cinfo.mem->alloc_small) ((j_common_ptr) &cinfo, JPOOL_PERMANENT,
    sizeof(my_source_mgr));
  src = (my_src_ptr) cinfo.src;
  src->buffer = (JOCTET *)compressed;

  src->pub.init_source = jpg_memInitSource;
  src->pub.fill_input_buffer = jpg_memFillInputBuffer;
  src->pub.skip_input_data = jpg_memSkipInputData;
  src->pub.resync_to_restart = jpeg_resync_to_restart;
  src->pub.term_source = jpg_memTermSource;
  src->pub.bytes_in_buffer = len;
  src->pub.next_input_byte = compressed;

  jpeg_read_header(&cinfo, true);
  jpeg_start_decompress(&cinfo);

  // check: cinfo.out_color_space == JCS_RGB

  currImage.setSize(
    (int)cinfo.output_width, (int)cinfo.output_height));

  size_t w = currImage.getWidth() * 3;
  uint8 *p = (uint8*)currImage.getPixels();
  while (cinfo.output_scanline < cinfo.output_height) {
    jpeg_read_scanlines(&cinfo, &p, 1);
    p += w;
  }

  jpeg_finish_decompress(&cinfo); // Do this even under error conditions
  jpeg_destroy_decompress(&cinfo); // Do this even under error conditions
}

编辑:添加了一些缺少的支持代码.谢谢,Edu Filipe!

推荐阅读
kikokikolove
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有