【问题标题】:libJpeg garbled readlibJpeg 乱码读取
【发布时间】:2013-10-23 13:20:28
【问题描述】:

我大部分时间都在使用该示例,它确实创建了一个只是乱码的图像:

unsigned char* readJpeg(JNIEnv* env, libraw_processed_image_t *raw)
{
    // http://sourceforge.net/p/libjpeg-turbo/code/HEAD/tree/trunk/example.c#l109
    // http://stackoverflow.com/questions/5616216/need-help-in-reading-jpeg-file-using-libjpeg

    struct jpeg_decompress_struct cinfo;
    struct jpeg_error_mgr jerr;
    int row_stride;     /* physical row width in output buffer */

    cinfo.err = jpeg_std_error(&jerr);

    /* Now we can initialize the JPEG decompression object. */
    jpeg_create_decompress(&cinfo);


    /* Step 2: specify data source (eg, a file) */
    jpeg_mem_src(&cinfo, raw->data, raw->data_size);

    /* Step 3: read file parameters with jpeg_read_header() */
    (void) jpeg_read_header(&cinfo, TRUE);

    /* Step 4: set parameters for decompression */

    /* In this example, we don't need to change any of the defaults set by
    * jpeg_read_header(), so we do nothing here.
    */

    /* Step 5: Start decompressor */

    (void) jpeg_start_decompress(&cinfo);
    /* We can ignore the return value since suspension is not possible
    * with the stdio data source.
    */

    /* We may need to do some setup of our own at this point before reading
    * the data.  After jpeg_start_decompress() we have the correct scaled
    * output image dimensions available, as well as the output colormap
    * if we asked for color quantization.
    * In this example, we need to make an output work buffer of the right size.
    */
    /* JSAMPLEs per row in output buffer */
    row_stride = cinfo.output_width * cinfo.output_components;
    JSAMPROW rowData;
    unsigned char* imageData = new unsigned char[cinfo.output_height * row_stride];
    /* Step 6: while (scan lines remain to be read) */
    /*           jpeg_read_scanlines(...); */

    /* Here we use the library's state variable cinfo.output_scanline as the
    * loop counter, so that we don't have to keep track ourselves.
    */
    __android_log_write(ANDROID_LOG_INFO, "JNI", "Made it to read lines");
    int row = 0;
    while (cinfo.output_scanline < cinfo.output_height)
    {
        rowData = imageData + (row * row_stride);
        jpeg_read_scanlines(&cinfo, &rowData, 1);
        ++row;
    }

    /* Step 7: Finish decompression */

    (void) jpeg_finish_decompress(&cinfo);
    /* We can ignore the return value since suspension is not possible
    * with the stdio data source.
    */

    /* Step 8: Release JPEG decompression object */

    /* This is an important step since it will release a good deal of memory. */
    jpeg_destroy_decompress(&cinfo);

    /* At this point you may want to check to see whether any corrupt-data
    * warnings occurred (test whether jerr.pub.num_warnings is nonzero).
    */

    /* And we're done! */
    return imageData;
}

我正在阅读的图像在现有阅读器中加载良好。我猜我错过了某种解压缩设置,尽管我会从标题中获取这些设置。

【问题讨论】:

  • 在我看来,行步长计算存在一些问题。可以调试看看没问题吗?在读取和显示功能中。
  • 是的,这是有道理的,但我在整个程序中记录了输入并且它起作用了。另外只是为了检查我增加了宽度并得到了段错误,所以它是正确的尺寸。
  • 实际上这似乎是正确的,在我看过的每张图像中,cinfo.output_width 似乎都偏离了 18 个像素......
  • 更正,它因图像尺寸而异,在 18 像素范围内。高度也不对。基本上 cinfo 中的值由于某种原因是错误的......

标签: c++ libjpeg


【解决方案1】:

我遇到了类似的问题,但后来发现您必须将 RGB 数组对齐到 8 字节边界。检查http://atlc.sourceforge.net/bmp.html#_toc381201083

【讨论】:

  • 感谢您的意见,您能说得更详细一点吗?自学代码,所以我有时会忘记内存管理的复杂性。
  • [此处] (imagetools.codeplex.com/SourceControl/latest#src/ImageTools/…) 提供的代码应该会有所帮助。要注意的功能是CalculateScanlineLength。您必须通过用零填充将行长度缓冲区与 8 字节边界对齐。例如,你的图像是 10 像素宽度的 RGB,那么每一行应该分配 3*10+(8-((3*10)%8))=32 字节所以如果图像是 10x10 像素那么你需要 32 *10 缓冲区。
  • row_stride = cinfo.output_width * cinfo.output_components; row_stride += (8-(row_stride%8)); 应该修复代码。也不要忘记将imageData的内存归零
  • 建议的行确实移动了像素,但只是不同的错位。它肯定与row_stride有关。当你说 zeromemory 时,你的意思是 free 吗?
  • zeromemory 在使用前用零初始化内存。你必须使用memset 来清零内存。在上述情况下,您必须在 new 之后调用 memset ((void*)image Data, 0, cinfo.output_height * row_stride);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-10
  • 2015-12-31
  • 2011-09-13
  • 2013-11-25
  • 1970-01-01
相关资源
最近更新 更多