From 79fcf29af83283d01c3002da78796cb7777ea6ad Mon Sep 17 00:00:00 2001 From: James Zern Date: Tue, 8 Dec 2015 19:04:22 -0800 Subject: [PATCH] wicdec: add support for reading from stdin This adds stdin support to cwebp when built with WIC support Change-Id: I3f8b0321322e6fb08af60ac5297cbd448e6547db --- examples/wicdec.c | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) 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); }