Skip to content

Commit 7c4680e

Browse files
committed
FIX: WIC WINCODEC_ERR_BADHEADER only for JPEG images
https://stackoverflow.com/a/12928336/494472
1 parent 80597e4 commit 7c4680e

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/os/win32/sys-codecs.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "sys-codecs.h"
3434

3535
IWICImagingFactory *pIWICFactory = NULL;
36+
static LARGE_INTEGER zero = { 0 };
3637

3738
CODECS_API int codecs_init()
3839
{
@@ -101,6 +102,10 @@ CODECS_API int DecodeImageFromFile(PCWSTR *uri, UINT frame, REBCDI *codi)
101102
ULONG written;
102103
hr = pStream->Write(codi->data, codi->len, &written);
103104
ASSERT_HR("pStream->Write");
105+
106+
// WIC JPEG decoder needs the stream to seek to head manually!
107+
// https://stackoverflow.com/a/12928336/494472
108+
pStream->Seek(zero, STREAM_SEEK_SET, NULL);
104109

105110
hr = pIWICFactory->CreateDecoderFromStream(
106111
pStream
@@ -306,7 +311,6 @@ CODECS_API int EncodeImageToFile(PCWSTR *uri, REBCDI *codi)
306311
}
307312

308313
ULONG read;
309-
LARGE_INTEGER zero = { 0 };
310314
outStream->Seek(zero, STREAM_SEEK_SET, NULL);
311315
hr = outStream->Read(codi->data, stat.cbSize.LowPart, &read);
312316
ASSERT_HR("outStream->Read");

0 commit comments

Comments
 (0)