diff --git a/examples/wicdec.c b/examples/wicdec.c index f2319524..4b722670 100644 --- a/examples/wicdec.c +++ b/examples/wicdec.c @@ -17,6 +17,7 @@ #include #include +#include #ifdef HAVE_WINCODEC_H #ifdef __MINGW32__ @@ -26,11 +27,13 @@ #define COBJMACROS #define _WIN32_IE 0x500 // Workaround bug in shlwapi.h when compiling C++ // code with COBJMACROS. +#include // CreateStreamOnHGlobal() #include #include #include #include "webp/encode.h" +#include "./example_util.h" #include "./metadata.h" #define IFS(fn) \ @@ -82,7 +85,32 @@ WEBP_DEFINE_GUID(GUID_WICPixelFormat64bppRGBA_, static HRESULT OpenInputStream(const char* filename, IStream** stream) { HRESULT hr = S_OK; - IFS(SHCreateStreamOnFileA(filename, STGM_READ, stream)); + if (!strcmp(filename, "-")) { + const uint8_t* data = NULL; + size_t data_size = 0; + const int ok = ExUtilReadFile(filename, &data, &data_size); + if (ok) { + HGLOBAL image = GlobalAlloc(GMEM_MOVEABLE, data_size); + if (image != NULL) { + void* const image_mem = GlobalLock(image); + if (image_mem != NULL) { + memcpy(image_mem, data, data_size); + GlobalUnlock(image); + IFS(CreateStreamOnHGlobal(image, TRUE, stream)); + } else { + hr = E_FAIL; + } + } else { + hr = E_OUTOFMEMORY; + } + free((void*)data); + } else { + hr = E_FAIL; + } + } else { + IFS(SHCreateStreamOnFileA(filename, STGM_READ, stream)); + } + if (FAILED(hr)) { fprintf(stderr, "Error opening input file %s (%08lx)\n", filename, hr); }