1

正如标题所述,我正在尝试使用 libjpeg-turbo 读取 JPEG 文件。我在家里的 Mac 上尝试了这段代码,它可以工作,但现在我在 Windows 上,它Empty input file在调用时给了我一个错误jpeg_read_header。我已经通过执行 fseek/ftell 验证了文件不是空的,并且我得到的大小与我期望的大小相对应。

我最初的想法是我可能没有以二进制模式打开文件,所以我也尝试使用 _setmode,但这似乎没有帮助。这是我的代码供参考。

int decodeJpegFile(char* filename)
{
    FILE *file = fopen(filename, "rb");

    if (file == NULL)
    {
        return NULL;
    }

    _setmode(_fileno(file), _O_BINARY);

    fseek(file, 0L, SEEK_END);
    int sz = ftell(file);
    fseek(file, 0L, SEEK_SET);


    struct jpeg_decompress_struct info; //for our jpeg info
    struct jpeg_error_mgr err; //the error handler

    info.err = jpeg_std_error(&err);
    jpeg_create_decompress(&info); //fills info structure
    jpeg_stdio_src(&info, file);
    jpeg_read_header(&info, true); // ****This is where it fails*****
    jpeg_start_decompress(&info);


    int w = info.output_width;
    int h = info.output_height;
    int numChannels = info.num_components; // 3 = RGB, 4 = RGBA
    unsigned long dataSize = w * h * numChannels;

    unsigned char *data = (unsigned char *)malloc(dataSize);
    unsigned char* rowptr;
    while (info.output_scanline < h)
    {
        rowptr = data + info.output_scanline * w * numChannels;
        jpeg_read_scanlines(&info, &rowptr, 1);
    }

    jpeg_finish_decompress(&info);
    fclose(file);

    FILE* outfile = fopen("outFile.raw", "wb");
    size_t data_out = fwrite(data, dataSize, sizeof(unsigned char), outfile);

}`

任何帮助深表感谢!

4

2 回答 2

2

问题的核心是 dll 不匹配。libjpeg是再次构建的msvcrt.dll,而应用程序是针对 MSVS2015 提供的任何运行时构建的。它们是不兼容的,并且在一个运行时打开的文件指针对另一个运行时没有意义。

根据这个讨论,解决方案是避免jpeg_stdio_src使用 API。

于 2016-08-25T01:13:44.830 回答
0

您将 C++true值传递给jpeg_read_header-- 这也可能是失败的原因。您应该改为传递 TRUE 常量。

于 2016-08-25T01:19:36.130 回答