正如标题所述,我正在尝试使用 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);
}`
任何帮助深表感谢!