首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >libJpeg乱码读取

libJpeg乱码读取
EN

Stack Overflow用户
提问于 2013-10-23 21:20:28
回答 1查看 450关注 0票数 1

我在很大程度上使用了这个例子,它确实创建了一个只是乱码的图像:

代码语言:javascript
复制
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;
}

我正在阅读的图像在现有的阅读器中加载正常。我猜我遗漏了一些解压设置,尽管我认为它应该是从头文件中得到的。

EN

回答 1

Stack Overflow用户

发布于 2013-10-23 22:27:58

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19542842

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档