正如標題所述,我試圖使用libjpeg-turbo讀取JPEG文件。我在家裏的Mac上試過這個代碼,它工作,但現在我在Windows上,它給我jpeg_read_header
Empty input file
錯誤。我已經通過執行fseek/ftell來驗證該文件不是空的,並且我得到的大小對應於我期望的大小。使用libjpegturbo解壓縮jpeg返回「空輸入文件」
我最初的想法是,我可能沒有以二進制模式打開文件,所以我嘗試使用_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);
}`
任何幫助非常感謝!
你真的應該使用你的調試器來看看發生了什麼,而不是猜測可能是什麼問題。 – PaulMcKenzie
你說得對。我目前沒有libjpeg的源代碼/符號,因此我無法在此處進行挖掘。我希望有人已經遇到過這個問題,因爲我想要做的事情是非常基本的。最壞的情況下,我必須自己下載/構建libjpeg – Sid
如何在Windows上構建應用程序? – user58697