wicdec: add support for reading from stdin

This adds stdin support to cwebp when built with WIC support

Change-Id: I3f8b0321322e6fb08af60ac5297cbd448e6547db
This commit is contained in:
James Zern 2015-12-08 19:04:22 -08:00
parent 015f173ff8
commit 79fcf29af8

View File

@ -17,6 +17,7 @@
#include <assert.h> #include <assert.h>
#include <stdio.h> #include <stdio.h>
#include <string.h>
#ifdef HAVE_WINCODEC_H #ifdef HAVE_WINCODEC_H
#ifdef __MINGW32__ #ifdef __MINGW32__
@ -26,11 +27,13 @@
#define COBJMACROS #define COBJMACROS
#define _WIN32_IE 0x500 // Workaround bug in shlwapi.h when compiling C++ #define _WIN32_IE 0x500 // Workaround bug in shlwapi.h when compiling C++
// code with COBJMACROS. // code with COBJMACROS.
#include <ole2.h> // CreateStreamOnHGlobal()
#include <shlwapi.h> #include <shlwapi.h>
#include <windows.h> #include <windows.h>
#include <wincodec.h> #include <wincodec.h>
#include "webp/encode.h" #include "webp/encode.h"
#include "./example_util.h"
#include "./metadata.h" #include "./metadata.h"
#define IFS(fn) \ #define IFS(fn) \
@ -82,7 +85,32 @@ WEBP_DEFINE_GUID(GUID_WICPixelFormat64bppRGBA_,
static HRESULT OpenInputStream(const char* filename, IStream** stream) { static HRESULT OpenInputStream(const char* filename, IStream** stream) {
HRESULT hr = S_OK; 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)) { if (FAILED(hr)) {
fprintf(stderr, "Error opening input file %s (%08lx)\n", filename, hr); fprintf(stderr, "Error opening input file %s (%08lx)\n", filename, hr);
} }