2013-01-23 03:28:52 +01:00
|
|
|
// Copyright 2013 Google Inc. All Rights Reserved.
|
|
|
|
//
|
2013-06-07 08:05:58 +02:00
|
|
|
// Use of this source code is governed by a BSD-style license
|
|
|
|
// that can be found in the COPYING file in the root of the source
|
|
|
|
// tree. An additional intellectual property rights grant can be found
|
|
|
|
// in the file PATENTS. All contributing project authors may
|
|
|
|
// be found in the AUTHORS file in the root of the source tree.
|
2013-01-23 03:28:52 +01:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Windows Imaging Component (WIC) decode.
|
|
|
|
|
|
|
|
#include "./wicdec.h"
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
2014-06-11 02:53:44 +02:00
|
|
|
#include "webp/config.h"
|
2013-01-23 03:28:52 +01:00
|
|
|
#endif
|
|
|
|
|
2015-01-17 02:43:36 +01:00
|
|
|
#include <assert.h>
|
2013-01-23 03:28:52 +01:00
|
|
|
#include <stdio.h>
|
2015-12-09 04:04:22 +01:00
|
|
|
#include <string.h>
|
2013-01-23 03:28:52 +01:00
|
|
|
|
|
|
|
#ifdef HAVE_WINCODEC_H
|
|
|
|
#ifdef __MINGW32__
|
|
|
|
#define INITGUID // Without this GUIDs are declared extern and fail to link
|
|
|
|
#endif
|
|
|
|
#define CINTERFACE
|
|
|
|
#define COBJMACROS
|
|
|
|
#define _WIN32_IE 0x500 // Workaround bug in shlwapi.h when compiling C++
|
|
|
|
// code with COBJMACROS.
|
2015-12-09 04:04:22 +01:00
|
|
|
#include <ole2.h> // CreateStreamOnHGlobal()
|
2013-01-23 03:28:52 +01:00
|
|
|
#include <shlwapi.h>
|
|
|
|
#include <windows.h>
|
|
|
|
#include <wincodec.h>
|
|
|
|
|
|
|
|
#include "webp/encode.h"
|
2015-12-09 04:04:22 +01:00
|
|
|
#include "./example_util.h"
|
2013-01-25 00:37:49 +01:00
|
|
|
#include "./metadata.h"
|
2013-01-23 03:28:52 +01:00
|
|
|
|
|
|
|
#define IFS(fn) \
|
|
|
|
do { \
|
2013-02-05 01:13:59 +01:00
|
|
|
if (SUCCEEDED(hr)) { \
|
|
|
|
hr = (fn); \
|
|
|
|
if (FAILED(hr)) fprintf(stderr, #fn " failed %08lx\n", hr); \
|
|
|
|
} \
|
2013-01-23 03:28:52 +01:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
// modified version of DEFINE_GUID from guiddef.h.
|
|
|
|
#define WEBP_DEFINE_GUID(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \
|
2013-02-06 01:25:47 +01:00
|
|
|
static const GUID name = \
|
|
|
|
{ l, w1, w2, { b1, b2, b3, b4, b5, b6, b7, b8 } }
|
2013-01-23 03:28:52 +01:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
#define MAKE_REFGUID(x) (x)
|
|
|
|
#else
|
|
|
|
#define MAKE_REFGUID(x) &(x)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
typedef struct WICFormatImporter {
|
|
|
|
const GUID* pixel_format;
|
|
|
|
int bytes_per_pixel;
|
|
|
|
int (*import)(WebPPicture* const, const uint8_t* const, int);
|
|
|
|
} WICFormatImporter;
|
|
|
|
|
2013-02-06 01:25:47 +01:00
|
|
|
// From Microsoft SDK 7.0a -- wincodec.h
|
|
|
|
// Create local copies for compatibility when building against earlier
|
|
|
|
// versions of the SDK.
|
|
|
|
WEBP_DEFINE_GUID(GUID_WICPixelFormat24bppBGR_,
|
|
|
|
0x6fddc324, 0x4e03, 0x4bfe,
|
|
|
|
0xb1, 0x85, 0x3d, 0x77, 0x76, 0x8d, 0xc9, 0x0c);
|
|
|
|
WEBP_DEFINE_GUID(GUID_WICPixelFormat24bppRGB_,
|
|
|
|
0x6fddc324, 0x4e03, 0x4bfe,
|
|
|
|
0xb1, 0x85, 0x3d, 0x77, 0x76, 0x8d, 0xc9, 0x0d);
|
|
|
|
WEBP_DEFINE_GUID(GUID_WICPixelFormat32bppBGRA_,
|
|
|
|
0x6fddc324, 0x4e03, 0x4bfe,
|
|
|
|
0xb1, 0x85, 0x3d, 0x77, 0x76, 0x8d, 0xc9, 0x0f);
|
|
|
|
WEBP_DEFINE_GUID(GUID_WICPixelFormat32bppRGBA_,
|
|
|
|
0xf5c7ad2d, 0x6a8d, 0x43dd,
|
|
|
|
0xa7, 0xa8, 0xa2, 0x99, 0x35, 0x26, 0x1a, 0xe9);
|
2015-10-09 05:33:33 +02:00
|
|
|
WEBP_DEFINE_GUID(GUID_WICPixelFormat64bppBGRA_,
|
|
|
|
0x1562ff7c, 0xd352, 0x46f9,
|
|
|
|
0x97, 0x9e, 0x42, 0x97, 0x6b, 0x79, 0x22, 0x46);
|
|
|
|
WEBP_DEFINE_GUID(GUID_WICPixelFormat64bppRGBA_,
|
|
|
|
0x6fddc324, 0x4e03, 0x4bfe,
|
|
|
|
0xb1, 0x85, 0x3d, 0x77, 0x76, 0x8d, 0xc9, 0x16);
|
2013-02-06 01:25:47 +01:00
|
|
|
|
2013-02-16 05:20:57 +01:00
|
|
|
static HRESULT OpenInputStream(const char* filename, IStream** stream) {
|
2013-01-23 03:28:52 +01:00
|
|
|
HRESULT hr = S_OK;
|
2015-12-09 04:04:22 +01:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2013-02-05 01:13:59 +01:00
|
|
|
if (FAILED(hr)) {
|
|
|
|
fprintf(stderr, "Error opening input file %s (%08lx)\n", filename, hr);
|
|
|
|
}
|
2013-01-23 03:28:52 +01:00
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
2013-02-05 01:13:59 +01:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// Metadata processing
|
|
|
|
|
2013-02-16 05:20:57 +01:00
|
|
|
// Stores the first non-zero sized color profile from 'frame' to 'iccp'.
|
2013-01-25 00:37:49 +01:00
|
|
|
// Returns an HRESULT to indicate success or failure. The caller is responsible
|
|
|
|
// for freeing 'iccp->bytes' in either case.
|
2013-02-16 05:20:57 +01:00
|
|
|
static HRESULT ExtractICCP(IWICImagingFactory* const factory,
|
|
|
|
IWICBitmapFrameDecode* const frame,
|
2013-01-25 00:37:49 +01:00
|
|
|
MetadataPayload* const iccp) {
|
|
|
|
HRESULT hr = S_OK;
|
|
|
|
UINT i, count;
|
2013-02-16 05:20:57 +01:00
|
|
|
IWICColorContext** color_contexts;
|
2013-01-25 00:37:49 +01:00
|
|
|
|
2013-02-16 05:20:57 +01:00
|
|
|
IFS(IWICBitmapFrameDecode_GetColorContexts(frame, 0, NULL, &count));
|
2013-01-25 00:37:49 +01:00
|
|
|
if (FAILED(hr) || count == 0) return hr;
|
|
|
|
|
2013-02-16 05:20:57 +01:00
|
|
|
color_contexts = (IWICColorContext**)calloc(count, sizeof(*color_contexts));
|
|
|
|
if (color_contexts == NULL) return E_OUTOFMEMORY;
|
2013-01-25 00:37:49 +01:00
|
|
|
for (i = 0; SUCCEEDED(hr) && i < count; ++i) {
|
2013-02-16 05:20:57 +01:00
|
|
|
IFS(IWICImagingFactory_CreateColorContext(factory, &color_contexts[i]));
|
2013-01-25 00:37:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (SUCCEEDED(hr)) {
|
|
|
|
UINT num_color_contexts;
|
2013-02-16 05:20:57 +01:00
|
|
|
IFS(IWICBitmapFrameDecode_GetColorContexts(frame,
|
|
|
|
count, color_contexts,
|
2013-01-25 00:37:49 +01:00
|
|
|
&num_color_contexts));
|
2015-01-17 02:43:36 +01:00
|
|
|
assert(FAILED(hr) || num_color_contexts <= count);
|
2013-01-25 00:37:49 +01:00
|
|
|
for (i = 0; SUCCEEDED(hr) && i < num_color_contexts; ++i) {
|
|
|
|
WICColorContextType type;
|
2013-02-16 05:20:57 +01:00
|
|
|
IFS(IWICColorContext_GetType(color_contexts[i], &type));
|
2013-01-25 00:37:49 +01:00
|
|
|
if (SUCCEEDED(hr) && type == WICColorContextProfile) {
|
|
|
|
UINT size;
|
2013-02-16 05:20:57 +01:00
|
|
|
IFS(IWICColorContext_GetProfileBytes(color_contexts[i],
|
2013-01-25 00:37:49 +01:00
|
|
|
0, NULL, &size));
|
2015-01-17 02:43:36 +01:00
|
|
|
if (SUCCEEDED(hr) && size > 0) {
|
2013-01-25 00:37:49 +01:00
|
|
|
iccp->bytes = (uint8_t*)malloc(size);
|
|
|
|
if (iccp->bytes == NULL) {
|
|
|
|
hr = E_OUTOFMEMORY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
iccp->size = size;
|
2013-02-16 05:20:57 +01:00
|
|
|
IFS(IWICColorContext_GetProfileBytes(color_contexts[i],
|
2013-01-25 00:37:49 +01:00
|
|
|
(UINT)iccp->size, iccp->bytes,
|
|
|
|
&size));
|
|
|
|
if (SUCCEEDED(hr) && size != iccp->size) {
|
|
|
|
fprintf(stderr, "Warning! ICC profile size (%u) != expected (%u)\n",
|
2013-06-15 03:44:59 +02:00
|
|
|
size, (uint32_t)iccp->size);
|
2013-01-25 00:37:49 +01:00
|
|
|
iccp->size = size;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (i = 0; i < count; ++i) {
|
2013-02-16 05:20:57 +01:00
|
|
|
if (color_contexts[i] != NULL) IUnknown_Release(color_contexts[i]);
|
2013-01-25 00:37:49 +01:00
|
|
|
}
|
2013-02-16 05:20:57 +01:00
|
|
|
free(color_contexts);
|
2013-01-25 00:37:49 +01:00
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
2013-02-16 05:20:57 +01:00
|
|
|
static HRESULT ExtractMetadata(IWICImagingFactory* const factory,
|
|
|
|
IWICBitmapFrameDecode* const frame,
|
2013-01-25 00:37:49 +01:00
|
|
|
Metadata* const metadata) {
|
|
|
|
// TODO(jzern): add XMP/EXIF extraction.
|
2013-02-16 05:20:57 +01:00
|
|
|
const HRESULT hr = ExtractICCP(factory, frame, &metadata->iccp);
|
2013-01-25 00:37:49 +01:00
|
|
|
if (FAILED(hr)) MetadataFree(metadata);
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
2013-02-05 01:13:59 +01:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
2013-02-06 01:25:47 +01:00
|
|
|
static int HasPalette(GUID pixel_format) {
|
|
|
|
return (IsEqualGUID(MAKE_REFGUID(pixel_format),
|
|
|
|
MAKE_REFGUID(GUID_WICPixelFormat1bppIndexed)) ||
|
|
|
|
IsEqualGUID(MAKE_REFGUID(pixel_format),
|
|
|
|
MAKE_REFGUID(GUID_WICPixelFormat2bppIndexed)) ||
|
|
|
|
IsEqualGUID(MAKE_REFGUID(pixel_format),
|
|
|
|
MAKE_REFGUID(GUID_WICPixelFormat4bppIndexed)) ||
|
|
|
|
IsEqualGUID(MAKE_REFGUID(pixel_format),
|
|
|
|
MAKE_REFGUID(GUID_WICPixelFormat8bppIndexed)));
|
|
|
|
}
|
|
|
|
|
2013-02-16 05:20:57 +01:00
|
|
|
static int HasAlpha(IWICImagingFactory* const factory,
|
|
|
|
IWICBitmapDecoder* const decoder,
|
|
|
|
IWICBitmapFrameDecode* const frame,
|
|
|
|
GUID pixel_format) {
|
2013-02-06 01:25:47 +01:00
|
|
|
int has_alpha;
|
2013-02-16 05:20:57 +01:00
|
|
|
if (HasPalette(pixel_format)) {
|
|
|
|
IWICPalette* frame_palette = NULL;
|
|
|
|
IWICPalette* global_palette = NULL;
|
2013-02-06 01:25:47 +01:00
|
|
|
BOOL frame_palette_has_alpha = FALSE;
|
|
|
|
BOOL global_palette_has_alpha = FALSE;
|
|
|
|
|
|
|
|
// A palette may exist at the frame or container level,
|
|
|
|
// check IWICPalette::HasAlpha() for both if present.
|
2013-02-16 05:20:57 +01:00
|
|
|
if (SUCCEEDED(IWICImagingFactory_CreatePalette(factory, &frame_palette)) &&
|
|
|
|
SUCCEEDED(IWICBitmapFrameDecode_CopyPalette(frame, frame_palette))) {
|
|
|
|
IWICPalette_HasAlpha(frame_palette, &frame_palette_has_alpha);
|
2013-02-06 01:25:47 +01:00
|
|
|
}
|
2013-02-16 05:20:57 +01:00
|
|
|
if (SUCCEEDED(IWICImagingFactory_CreatePalette(factory, &global_palette)) &&
|
|
|
|
SUCCEEDED(IWICBitmapDecoder_CopyPalette(decoder, global_palette))) {
|
|
|
|
IWICPalette_HasAlpha(global_palette, &global_palette_has_alpha);
|
2013-02-06 01:25:47 +01:00
|
|
|
}
|
|
|
|
has_alpha = frame_palette_has_alpha || global_palette_has_alpha;
|
|
|
|
|
2013-02-16 05:20:57 +01:00
|
|
|
if (frame_palette != NULL) IUnknown_Release(frame_palette);
|
|
|
|
if (global_palette != NULL) IUnknown_Release(global_palette);
|
2013-02-06 01:25:47 +01:00
|
|
|
} else {
|
2013-02-16 05:20:57 +01:00
|
|
|
has_alpha = IsEqualGUID(MAKE_REFGUID(pixel_format),
|
2013-02-06 01:25:47 +01:00
|
|
|
MAKE_REFGUID(GUID_WICPixelFormat32bppRGBA_)) ||
|
2013-02-16 05:20:57 +01:00
|
|
|
IsEqualGUID(MAKE_REFGUID(pixel_format),
|
2015-10-09 05:33:33 +02:00
|
|
|
MAKE_REFGUID(GUID_WICPixelFormat32bppBGRA_)) ||
|
|
|
|
IsEqualGUID(MAKE_REFGUID(pixel_format),
|
|
|
|
MAKE_REFGUID(GUID_WICPixelFormat64bppRGBA_)) ||
|
|
|
|
IsEqualGUID(MAKE_REFGUID(pixel_format),
|
|
|
|
MAKE_REFGUID(GUID_WICPixelFormat64bppBGRA_));
|
2013-02-06 01:25:47 +01:00
|
|
|
}
|
|
|
|
return has_alpha;
|
|
|
|
}
|
|
|
|
|
2013-01-23 03:28:52 +01:00
|
|
|
int ReadPictureWithWIC(const char* const filename,
|
2013-01-25 00:37:49 +01:00
|
|
|
WebPPicture* const pic, int keep_alpha,
|
|
|
|
Metadata* const metadata) {
|
2013-02-05 01:13:59 +01:00
|
|
|
// From Microsoft SDK 6.0a -- ks.h
|
|
|
|
// Define a local copy to avoid link errors under mingw.
|
|
|
|
WEBP_DEFINE_GUID(GUID_NULL_, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
|
2013-02-16 05:20:57 +01:00
|
|
|
static const WICFormatImporter kAlphaFormatImporters[] = {
|
2013-01-23 03:28:52 +01:00
|
|
|
{ &GUID_WICPixelFormat32bppBGRA_, 4, WebPPictureImportBGRA },
|
|
|
|
{ &GUID_WICPixelFormat32bppRGBA_, 4, WebPPictureImportRGBA },
|
|
|
|
{ NULL, 0, NULL },
|
|
|
|
};
|
2013-02-16 05:20:57 +01:00
|
|
|
static const WICFormatImporter kNonAlphaFormatImporters[] = {
|
2013-01-23 03:28:52 +01:00
|
|
|
{ &GUID_WICPixelFormat24bppBGR_, 3, WebPPictureImportBGR },
|
|
|
|
{ &GUID_WICPixelFormat24bppRGB_, 3, WebPPictureImportRGB },
|
|
|
|
{ NULL, 0, NULL },
|
|
|
|
};
|
|
|
|
HRESULT hr = S_OK;
|
2013-02-16 05:20:57 +01:00
|
|
|
IWICBitmapFrameDecode* frame = NULL;
|
|
|
|
IWICFormatConverter* converter = NULL;
|
|
|
|
IWICImagingFactory* factory = NULL;
|
|
|
|
IWICBitmapDecoder* decoder = NULL;
|
|
|
|
IStream* stream = NULL;
|
|
|
|
UINT frame_count = 0;
|
2013-01-23 03:28:52 +01:00
|
|
|
UINT width = 0, height = 0;
|
|
|
|
BYTE* rgb = NULL;
|
2013-02-16 05:20:57 +01:00
|
|
|
WICPixelFormatGUID src_pixel_format = GUID_WICPixelFormatUndefined;
|
2013-01-23 03:28:52 +01:00
|
|
|
const WICFormatImporter* importer = NULL;
|
2013-02-16 05:20:57 +01:00
|
|
|
GUID src_container_format = GUID_NULL_;
|
|
|
|
static const GUID* kAlphaContainers[] = {
|
2013-01-23 03:28:52 +01:00
|
|
|
&GUID_ContainerFormatBmp,
|
|
|
|
&GUID_ContainerFormatPng,
|
2013-02-05 01:13:59 +01:00
|
|
|
&GUID_ContainerFormatTiff,
|
|
|
|
NULL
|
2013-01-23 03:28:52 +01:00
|
|
|
};
|
|
|
|
int has_alpha = 0;
|
2013-02-05 01:13:59 +01:00
|
|
|
int stride;
|
2013-01-23 03:28:52 +01:00
|
|
|
|
|
|
|
IFS(CoInitialize(NULL));
|
|
|
|
IFS(CoCreateInstance(MAKE_REFGUID(CLSID_WICImagingFactory), NULL,
|
2013-02-16 05:20:57 +01:00
|
|
|
CLSCTX_INPROC_SERVER,
|
|
|
|
MAKE_REFGUID(IID_IWICImagingFactory),
|
|
|
|
(LPVOID*)&factory));
|
2013-01-23 03:28:52 +01:00
|
|
|
if (hr == REGDB_E_CLASSNOTREG) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Couldn't access Windows Imaging Component (are you running "
|
|
|
|
"Windows XP SP3 or newer?). Most formats not available. "
|
|
|
|
"Use -s for the available YUV input.\n");
|
|
|
|
}
|
|
|
|
// Prepare for image decoding.
|
2013-02-16 05:20:57 +01:00
|
|
|
IFS(OpenInputStream(filename, &stream));
|
|
|
|
IFS(IWICImagingFactory_CreateDecoderFromStream(
|
|
|
|
factory, stream, NULL,
|
|
|
|
WICDecodeMetadataCacheOnDemand, &decoder));
|
|
|
|
IFS(IWICBitmapDecoder_GetFrameCount(decoder, &frame_count));
|
|
|
|
if (SUCCEEDED(hr) && frame_count == 0) {
|
2013-01-23 03:28:52 +01:00
|
|
|
fprintf(stderr, "No frame found in input file.\n");
|
|
|
|
hr = E_FAIL;
|
|
|
|
}
|
2013-02-16 05:20:57 +01:00
|
|
|
IFS(IWICBitmapDecoder_GetFrame(decoder, 0, &frame));
|
|
|
|
IFS(IWICBitmapFrameDecode_GetPixelFormat(frame, &src_pixel_format));
|
|
|
|
IFS(IWICBitmapDecoder_GetContainerFormat(decoder, &src_container_format));
|
2013-01-23 03:28:52 +01:00
|
|
|
|
2015-01-17 02:43:36 +01:00
|
|
|
if (SUCCEEDED(hr) && keep_alpha) {
|
2013-02-05 01:13:59 +01:00
|
|
|
const GUID** guid;
|
2013-02-16 05:20:57 +01:00
|
|
|
for (guid = kAlphaContainers; *guid != NULL; ++guid) {
|
|
|
|
if (IsEqualGUID(MAKE_REFGUID(src_container_format),
|
|
|
|
MAKE_REFGUID(**guid))) {
|
|
|
|
has_alpha = HasAlpha(factory, decoder, frame, src_pixel_format);
|
2013-01-23 03:28:52 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare for pixel format conversion (if necessary).
|
2013-02-16 05:20:57 +01:00
|
|
|
IFS(IWICImagingFactory_CreateFormatConverter(factory, &converter));
|
2013-01-23 03:28:52 +01:00
|
|
|
|
2013-02-16 05:20:57 +01:00
|
|
|
for (importer = has_alpha ? kAlphaFormatImporters : kNonAlphaFormatImporters;
|
2013-01-23 03:28:52 +01:00
|
|
|
hr == S_OK && importer->import != NULL; ++importer) {
|
2013-02-16 05:20:57 +01:00
|
|
|
BOOL can_convert;
|
2013-01-23 03:28:52 +01:00
|
|
|
const HRESULT cchr = IWICFormatConverter_CanConvert(
|
2013-02-16 05:20:57 +01:00
|
|
|
converter,
|
|
|
|
MAKE_REFGUID(src_pixel_format),
|
2013-01-23 03:28:52 +01:00
|
|
|
MAKE_REFGUID(*importer->pixel_format),
|
2013-02-16 05:20:57 +01:00
|
|
|
&can_convert);
|
|
|
|
if (SUCCEEDED(cchr) && can_convert) break;
|
2013-01-23 03:28:52 +01:00
|
|
|
}
|
|
|
|
if (importer->import == NULL) hr = E_FAIL;
|
|
|
|
|
2013-02-16 05:20:57 +01:00
|
|
|
IFS(IWICFormatConverter_Initialize(converter, (IWICBitmapSource*)frame,
|
|
|
|
importer->pixel_format,
|
|
|
|
WICBitmapDitherTypeNone,
|
|
|
|
NULL, 0.0, WICBitmapPaletteTypeCustom));
|
2013-01-23 03:28:52 +01:00
|
|
|
|
|
|
|
// Decode.
|
2013-02-16 05:20:57 +01:00
|
|
|
IFS(IWICFormatConverter_GetSize(converter, &width, &height));
|
2013-01-23 03:28:52 +01:00
|
|
|
stride = importer->bytes_per_pixel * width * sizeof(*rgb);
|
|
|
|
if (SUCCEEDED(hr)) {
|
|
|
|
rgb = (BYTE*)malloc(stride * height);
|
|
|
|
if (rgb == NULL)
|
|
|
|
hr = E_OUTOFMEMORY;
|
|
|
|
}
|
2013-02-16 05:20:57 +01:00
|
|
|
IFS(IWICFormatConverter_CopyPixels(converter, NULL,
|
|
|
|
stride, stride * height, rgb));
|
2013-01-23 03:28:52 +01:00
|
|
|
|
|
|
|
// WebP conversion.
|
|
|
|
if (SUCCEEDED(hr)) {
|
|
|
|
int ok;
|
|
|
|
pic->width = width;
|
|
|
|
pic->height = height;
|
2015-10-27 22:54:11 +01:00
|
|
|
pic->use_argb = 1; // For WIC, we always force to argb
|
2013-01-23 03:28:52 +01:00
|
|
|
ok = importer->import(pic, rgb, stride);
|
2013-02-16 05:20:57 +01:00
|
|
|
if (!ok) hr = E_FAIL;
|
2013-01-23 03:28:52 +01:00
|
|
|
}
|
|
|
|
if (SUCCEEDED(hr)) {
|
2013-01-25 00:37:49 +01:00
|
|
|
if (metadata != NULL) {
|
2013-02-16 05:20:57 +01:00
|
|
|
hr = ExtractMetadata(factory, frame, metadata);
|
2013-01-25 00:37:49 +01:00
|
|
|
if (FAILED(hr)) {
|
|
|
|
fprintf(stderr, "Error extracting image metadata using WIC!\n");
|
|
|
|
}
|
|
|
|
}
|
2013-01-23 03:28:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanup.
|
2013-02-16 05:20:57 +01:00
|
|
|
if (converter != NULL) IUnknown_Release(converter);
|
|
|
|
if (frame != NULL) IUnknown_Release(frame);
|
|
|
|
if (decoder != NULL) IUnknown_Release(decoder);
|
|
|
|
if (factory != NULL) IUnknown_Release(factory);
|
|
|
|
if (stream != NULL) IUnknown_Release(stream);
|
2013-01-23 03:28:52 +01:00
|
|
|
free(rgb);
|
|
|
|
return SUCCEEDED(hr);
|
|
|
|
}
|
|
|
|
#else // !HAVE_WINCODEC_H
|
|
|
|
int ReadPictureWithWIC(const char* const filename,
|
2013-01-25 00:37:49 +01:00
|
|
|
struct WebPPicture* const pic, int keep_alpha,
|
|
|
|
struct Metadata* const metadata) {
|
2013-01-23 03:28:52 +01:00
|
|
|
(void)filename;
|
|
|
|
(void)pic;
|
|
|
|
(void)keep_alpha;
|
2013-01-25 00:37:49 +01:00
|
|
|
(void)metadata;
|
2013-01-23 03:28:52 +01:00
|
|
|
fprintf(stderr, "Windows Imaging Component (WIC) support not compiled. "
|
|
|
|
"Visual Studio and mingw-w64 builds support WIC. Make sure "
|
|
|
|
"wincodec.h detection is working correctly if using autoconf "
|
|
|
|
"and HAVE_WINCODEC_H is defined before building.\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif // HAVE_WINCODEC_H
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|