mirror of
https://github.com/webmproject/libwebp.git
synced 2024-12-28 06:38:20 +01:00
wicdec + dwebp cosmetics: normalize formatting
- drop mixed use of Hungarian notation - fix some line continuations Change-Id: I9e6ec5cd6c746eb78048cf3532586fd93cef4ddb
This commit is contained in:
parent
92668da6f2
commit
3151669b15
@ -82,10 +82,9 @@ typedef enum {
|
|||||||
#define MAKE_REFGUID(x) &(x)
|
#define MAKE_REFGUID(x) &(x)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static HRESULT CreateOutputStream(const char* out_file_name,
|
static HRESULT CreateOutputStream(const char* out_file_name, IStream** stream) {
|
||||||
IStream** ppStream) {
|
|
||||||
HRESULT hr = S_OK;
|
HRESULT hr = S_OK;
|
||||||
IFS(SHCreateStreamOnFileA(out_file_name, STGM_WRITE | STGM_CREATE, ppStream));
|
IFS(SHCreateStreamOnFileA(out_file_name, STGM_WRITE | STGM_CREATE, stream));
|
||||||
if (FAILED(hr)) {
|
if (FAILED(hr)) {
|
||||||
fprintf(stderr, "Error opening output file %s (%08lx)\n",
|
fprintf(stderr, "Error opening output file %s (%08lx)\n",
|
||||||
out_file_name, hr);
|
out_file_name, hr);
|
||||||
@ -97,41 +96,42 @@ static HRESULT WriteUsingWIC(const char* out_file_name, REFGUID container_guid,
|
|||||||
unsigned char* rgb, int stride,
|
unsigned char* rgb, int stride,
|
||||||
uint32_t width, uint32_t height, int has_alpha) {
|
uint32_t width, uint32_t height, int has_alpha) {
|
||||||
HRESULT hr = S_OK;
|
HRESULT hr = S_OK;
|
||||||
IWICImagingFactory* pFactory = NULL;
|
IWICImagingFactory* factory = NULL;
|
||||||
IWICBitmapFrameEncode* pFrame = NULL;
|
IWICBitmapFrameEncode* frame = NULL;
|
||||||
IWICBitmapEncoder* pEncoder = NULL;
|
IWICBitmapEncoder* encoder = NULL;
|
||||||
IStream* pStream = NULL;
|
IStream* stream = NULL;
|
||||||
WICPixelFormatGUID pixel_format = has_alpha ? GUID_WICPixelFormat32bppBGRA
|
WICPixelFormatGUID pixel_format = has_alpha ? GUID_WICPixelFormat32bppBGRA
|
||||||
: GUID_WICPixelFormat24bppBGR;
|
: GUID_WICPixelFormat24bppBGR;
|
||||||
|
|
||||||
IFS(CoInitialize(NULL));
|
IFS(CoInitialize(NULL));
|
||||||
IFS(CoCreateInstance(MAKE_REFGUID(CLSID_WICImagingFactory), NULL,
|
IFS(CoCreateInstance(MAKE_REFGUID(CLSID_WICImagingFactory), NULL,
|
||||||
CLSCTX_INPROC_SERVER, MAKE_REFGUID(IID_IWICImagingFactory),
|
CLSCTX_INPROC_SERVER,
|
||||||
(LPVOID*)&pFactory));
|
MAKE_REFGUID(IID_IWICImagingFactory),
|
||||||
|
(LPVOID*)&factory));
|
||||||
if (hr == REGDB_E_CLASSNOTREG) {
|
if (hr == REGDB_E_CLASSNOTREG) {
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
"Couldn't access Windows Imaging Component (are you running "
|
"Couldn't access Windows Imaging Component (are you running "
|
||||||
"Windows XP SP3 or newer?). PNG support not available. "
|
"Windows XP SP3 or newer?). PNG support not available. "
|
||||||
"Use -ppm or -pgm for available PPM and PGM formats.\n");
|
"Use -ppm or -pgm for available PPM and PGM formats.\n");
|
||||||
}
|
}
|
||||||
IFS(CreateOutputStream(out_file_name, &pStream));
|
IFS(CreateOutputStream(out_file_name, &stream));
|
||||||
IFS(IWICImagingFactory_CreateEncoder(pFactory, container_guid, NULL,
|
IFS(IWICImagingFactory_CreateEncoder(factory, container_guid, NULL,
|
||||||
&pEncoder));
|
&encoder));
|
||||||
IFS(IWICBitmapEncoder_Initialize(pEncoder, pStream,
|
IFS(IWICBitmapEncoder_Initialize(encoder, stream,
|
||||||
WICBitmapEncoderNoCache));
|
WICBitmapEncoderNoCache));
|
||||||
IFS(IWICBitmapEncoder_CreateNewFrame(pEncoder, &pFrame, NULL));
|
IFS(IWICBitmapEncoder_CreateNewFrame(encoder, &frame, NULL));
|
||||||
IFS(IWICBitmapFrameEncode_Initialize(pFrame, NULL));
|
IFS(IWICBitmapFrameEncode_Initialize(frame, NULL));
|
||||||
IFS(IWICBitmapFrameEncode_SetSize(pFrame, width, height));
|
IFS(IWICBitmapFrameEncode_SetSize(frame, width, height));
|
||||||
IFS(IWICBitmapFrameEncode_SetPixelFormat(pFrame, &pixel_format));
|
IFS(IWICBitmapFrameEncode_SetPixelFormat(frame, &pixel_format));
|
||||||
IFS(IWICBitmapFrameEncode_WritePixels(pFrame, height, stride,
|
IFS(IWICBitmapFrameEncode_WritePixels(frame, height, stride,
|
||||||
height * stride, rgb));
|
height * stride, rgb));
|
||||||
IFS(IWICBitmapFrameEncode_Commit(pFrame));
|
IFS(IWICBitmapFrameEncode_Commit(frame));
|
||||||
IFS(IWICBitmapEncoder_Commit(pEncoder));
|
IFS(IWICBitmapEncoder_Commit(encoder));
|
||||||
|
|
||||||
if (pFrame != NULL) IUnknown_Release(pFrame);
|
if (frame != NULL) IUnknown_Release(frame);
|
||||||
if (pEncoder != NULL) IUnknown_Release(pEncoder);
|
if (encoder != NULL) IUnknown_Release(encoder);
|
||||||
if (pFactory != NULL) IUnknown_Release(pFactory);
|
if (factory != NULL) IUnknown_Release(factory);
|
||||||
if (pStream != NULL) IUnknown_Release(pStream);
|
if (stream != NULL) IUnknown_Release(stream);
|
||||||
return hr;
|
return hr;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -144,8 +144,8 @@ static int WritePNG(const char* out_file_name,
|
|||||||
const int has_alpha = (buffer->colorspace == MODE_BGRA);
|
const int has_alpha = (buffer->colorspace == MODE_BGRA);
|
||||||
|
|
||||||
return SUCCEEDED(WriteUsingWIC(out_file_name,
|
return SUCCEEDED(WriteUsingWIC(out_file_name,
|
||||||
MAKE_REFGUID(GUID_ContainerFormatPng), rgb, stride, width,
|
MAKE_REFGUID(GUID_ContainerFormatPng),
|
||||||
height, has_alpha));
|
rgb, stride, width, height, has_alpha));
|
||||||
}
|
}
|
||||||
|
|
||||||
#elif defined(WEBP_HAVE_PNG) // !HAVE_WINCODEC_H
|
#elif defined(WEBP_HAVE_PNG) // !HAVE_WINCODEC_H
|
||||||
|
@ -71,9 +71,9 @@ WEBP_DEFINE_GUID(GUID_WICPixelFormat32bppRGBA_,
|
|||||||
0xf5c7ad2d, 0x6a8d, 0x43dd,
|
0xf5c7ad2d, 0x6a8d, 0x43dd,
|
||||||
0xa7, 0xa8, 0xa2, 0x99, 0x35, 0x26, 0x1a, 0xe9);
|
0xa7, 0xa8, 0xa2, 0x99, 0x35, 0x26, 0x1a, 0xe9);
|
||||||
|
|
||||||
static HRESULT OpenInputStream(const char* filename, IStream** ppStream) {
|
static HRESULT OpenInputStream(const char* filename, IStream** stream) {
|
||||||
HRESULT hr = S_OK;
|
HRESULT hr = S_OK;
|
||||||
IFS(SHCreateStreamOnFileA(filename, STGM_READ, ppStream));
|
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);
|
||||||
}
|
}
|
||||||
@ -83,36 +83,36 @@ static HRESULT OpenInputStream(const char* filename, IStream** ppStream) {
|
|||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
// Metadata processing
|
// Metadata processing
|
||||||
|
|
||||||
// Stores the first non-zero sized color profile from 'pFrame' to 'iccp'.
|
// Stores the first non-zero sized color profile from 'frame' to 'iccp'.
|
||||||
// Returns an HRESULT to indicate success or failure. The caller is responsible
|
// Returns an HRESULT to indicate success or failure. The caller is responsible
|
||||||
// for freeing 'iccp->bytes' in either case.
|
// for freeing 'iccp->bytes' in either case.
|
||||||
static HRESULT ExtractICCP(IWICImagingFactory* const pFactory,
|
static HRESULT ExtractICCP(IWICImagingFactory* const factory,
|
||||||
IWICBitmapFrameDecode* const pFrame,
|
IWICBitmapFrameDecode* const frame,
|
||||||
MetadataPayload* const iccp) {
|
MetadataPayload* const iccp) {
|
||||||
HRESULT hr = S_OK;
|
HRESULT hr = S_OK;
|
||||||
UINT i, count;
|
UINT i, count;
|
||||||
IWICColorContext** ppColorContext;
|
IWICColorContext** color_contexts;
|
||||||
|
|
||||||
IFS(IWICBitmapFrameDecode_GetColorContexts(pFrame, 0, NULL, &count));
|
IFS(IWICBitmapFrameDecode_GetColorContexts(frame, 0, NULL, &count));
|
||||||
if (FAILED(hr) || count == 0) return hr;
|
if (FAILED(hr) || count == 0) return hr;
|
||||||
|
|
||||||
ppColorContext = (IWICColorContext**)calloc(count, sizeof(*ppColorContext));
|
color_contexts = (IWICColorContext**)calloc(count, sizeof(*color_contexts));
|
||||||
if (ppColorContext == NULL) return E_OUTOFMEMORY;
|
if (color_contexts == NULL) return E_OUTOFMEMORY;
|
||||||
for (i = 0; SUCCEEDED(hr) && i < count; ++i) {
|
for (i = 0; SUCCEEDED(hr) && i < count; ++i) {
|
||||||
IFS(IWICImagingFactory_CreateColorContext(pFactory, &ppColorContext[i]));
|
IFS(IWICImagingFactory_CreateColorContext(factory, &color_contexts[i]));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (SUCCEEDED(hr)) {
|
if (SUCCEEDED(hr)) {
|
||||||
UINT num_color_contexts;
|
UINT num_color_contexts;
|
||||||
IFS(IWICBitmapFrameDecode_GetColorContexts(pFrame,
|
IFS(IWICBitmapFrameDecode_GetColorContexts(frame,
|
||||||
count, ppColorContext,
|
count, color_contexts,
|
||||||
&num_color_contexts));
|
&num_color_contexts));
|
||||||
for (i = 0; SUCCEEDED(hr) && i < num_color_contexts; ++i) {
|
for (i = 0; SUCCEEDED(hr) && i < num_color_contexts; ++i) {
|
||||||
WICColorContextType type;
|
WICColorContextType type;
|
||||||
IFS(IWICColorContext_GetType(ppColorContext[i], &type));
|
IFS(IWICColorContext_GetType(color_contexts[i], &type));
|
||||||
if (SUCCEEDED(hr) && type == WICColorContextProfile) {
|
if (SUCCEEDED(hr) && type == WICColorContextProfile) {
|
||||||
UINT size;
|
UINT size;
|
||||||
IFS(IWICColorContext_GetProfileBytes(ppColorContext[i],
|
IFS(IWICColorContext_GetProfileBytes(color_contexts[i],
|
||||||
0, NULL, &size));
|
0, NULL, &size));
|
||||||
if (size > 0) {
|
if (size > 0) {
|
||||||
iccp->bytes = (uint8_t*)malloc(size);
|
iccp->bytes = (uint8_t*)malloc(size);
|
||||||
@ -121,7 +121,7 @@ static HRESULT ExtractICCP(IWICImagingFactory* const pFactory,
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
iccp->size = size;
|
iccp->size = size;
|
||||||
IFS(IWICColorContext_GetProfileBytes(ppColorContext[i],
|
IFS(IWICColorContext_GetProfileBytes(color_contexts[i],
|
||||||
(UINT)iccp->size, iccp->bytes,
|
(UINT)iccp->size, iccp->bytes,
|
||||||
&size));
|
&size));
|
||||||
if (SUCCEEDED(hr) && size != iccp->size) {
|
if (SUCCEEDED(hr) && size != iccp->size) {
|
||||||
@ -135,17 +135,17 @@ static HRESULT ExtractICCP(IWICImagingFactory* const pFactory,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (i = 0; i < count; ++i) {
|
for (i = 0; i < count; ++i) {
|
||||||
if (ppColorContext[i] != NULL) IUnknown_Release(ppColorContext[i]);
|
if (color_contexts[i] != NULL) IUnknown_Release(color_contexts[i]);
|
||||||
}
|
}
|
||||||
free(ppColorContext);
|
free(color_contexts);
|
||||||
return hr;
|
return hr;
|
||||||
}
|
}
|
||||||
|
|
||||||
static HRESULT ExtractMetadata(IWICImagingFactory* const pFactory,
|
static HRESULT ExtractMetadata(IWICImagingFactory* const factory,
|
||||||
IWICBitmapFrameDecode* const pFrame,
|
IWICBitmapFrameDecode* const frame,
|
||||||
Metadata* const metadata) {
|
Metadata* const metadata) {
|
||||||
// TODO(jzern): add XMP/EXIF extraction.
|
// TODO(jzern): add XMP/EXIF extraction.
|
||||||
const HRESULT hr = ExtractICCP(pFactory, pFrame, &metadata->iccp);
|
const HRESULT hr = ExtractICCP(factory, frame, &metadata->iccp);
|
||||||
if (FAILED(hr)) MetadataFree(metadata);
|
if (FAILED(hr)) MetadataFree(metadata);
|
||||||
return hr;
|
return hr;
|
||||||
}
|
}
|
||||||
@ -163,36 +163,35 @@ static int HasPalette(GUID pixel_format) {
|
|||||||
MAKE_REFGUID(GUID_WICPixelFormat8bppIndexed)));
|
MAKE_REFGUID(GUID_WICPixelFormat8bppIndexed)));
|
||||||
}
|
}
|
||||||
|
|
||||||
static int HasAlpha(IWICImagingFactory* const pFactory,
|
static int HasAlpha(IWICImagingFactory* const factory,
|
||||||
IWICBitmapDecoder* const pDecoder,
|
IWICBitmapDecoder* const decoder,
|
||||||
IWICBitmapFrameDecode* const pFrame,
|
IWICBitmapFrameDecode* const frame,
|
||||||
GUID srcPixelFormat) {
|
GUID pixel_format) {
|
||||||
int has_alpha;
|
int has_alpha;
|
||||||
if (HasPalette(srcPixelFormat)) {
|
if (HasPalette(pixel_format)) {
|
||||||
IWICPalette* pFramePalette = NULL;
|
IWICPalette* frame_palette = NULL;
|
||||||
IWICPalette* pGlobalPalette = NULL;
|
IWICPalette* global_palette = NULL;
|
||||||
BOOL frame_palette_has_alpha = FALSE;
|
BOOL frame_palette_has_alpha = FALSE;
|
||||||
BOOL global_palette_has_alpha = FALSE;
|
BOOL global_palette_has_alpha = FALSE;
|
||||||
|
|
||||||
// A palette may exist at the frame or container level,
|
// A palette may exist at the frame or container level,
|
||||||
// check IWICPalette::HasAlpha() for both if present.
|
// check IWICPalette::HasAlpha() for both if present.
|
||||||
if (SUCCEEDED(IWICImagingFactory_CreatePalette(pFactory, &pFramePalette)) &&
|
if (SUCCEEDED(IWICImagingFactory_CreatePalette(factory, &frame_palette)) &&
|
||||||
SUCCEEDED(IWICBitmapFrameDecode_CopyPalette(pFrame, pFramePalette))) {
|
SUCCEEDED(IWICBitmapFrameDecode_CopyPalette(frame, frame_palette))) {
|
||||||
IWICPalette_HasAlpha(pFramePalette, &frame_palette_has_alpha);
|
IWICPalette_HasAlpha(frame_palette, &frame_palette_has_alpha);
|
||||||
}
|
}
|
||||||
if (SUCCEEDED(IWICImagingFactory_CreatePalette(pFactory,
|
if (SUCCEEDED(IWICImagingFactory_CreatePalette(factory, &global_palette)) &&
|
||||||
&pGlobalPalette)) &&
|
SUCCEEDED(IWICBitmapDecoder_CopyPalette(decoder, global_palette))) {
|
||||||
SUCCEEDED(IWICBitmapDecoder_CopyPalette(pDecoder, pGlobalPalette))) {
|
IWICPalette_HasAlpha(global_palette, &global_palette_has_alpha);
|
||||||
IWICPalette_HasAlpha(pGlobalPalette, &global_palette_has_alpha);
|
|
||||||
}
|
}
|
||||||
has_alpha = frame_palette_has_alpha || global_palette_has_alpha;
|
has_alpha = frame_palette_has_alpha || global_palette_has_alpha;
|
||||||
|
|
||||||
if (pFramePalette != NULL) IUnknown_Release(pFramePalette);
|
if (frame_palette != NULL) IUnknown_Release(frame_palette);
|
||||||
if (pGlobalPalette != NULL) IUnknown_Release(pGlobalPalette);
|
if (global_palette != NULL) IUnknown_Release(global_palette);
|
||||||
} else {
|
} else {
|
||||||
has_alpha = IsEqualGUID(MAKE_REFGUID(srcPixelFormat),
|
has_alpha = IsEqualGUID(MAKE_REFGUID(pixel_format),
|
||||||
MAKE_REFGUID(GUID_WICPixelFormat32bppRGBA_)) ||
|
MAKE_REFGUID(GUID_WICPixelFormat32bppRGBA_)) ||
|
||||||
IsEqualGUID(MAKE_REFGUID(srcPixelFormat),
|
IsEqualGUID(MAKE_REFGUID(pixel_format),
|
||||||
MAKE_REFGUID(GUID_WICPixelFormat32bppBGRA_));
|
MAKE_REFGUID(GUID_WICPixelFormat32bppBGRA_));
|
||||||
}
|
}
|
||||||
return has_alpha;
|
return has_alpha;
|
||||||
@ -204,29 +203,29 @@ int ReadPictureWithWIC(const char* const filename,
|
|||||||
// From Microsoft SDK 6.0a -- ks.h
|
// From Microsoft SDK 6.0a -- ks.h
|
||||||
// Define a local copy to avoid link errors under mingw.
|
// 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);
|
WEBP_DEFINE_GUID(GUID_NULL_, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
|
||||||
const WICFormatImporter alphaFormatImporters[] = {
|
static const WICFormatImporter kAlphaFormatImporters[] = {
|
||||||
{ &GUID_WICPixelFormat32bppBGRA_, 4, WebPPictureImportBGRA },
|
{ &GUID_WICPixelFormat32bppBGRA_, 4, WebPPictureImportBGRA },
|
||||||
{ &GUID_WICPixelFormat32bppRGBA_, 4, WebPPictureImportRGBA },
|
{ &GUID_WICPixelFormat32bppRGBA_, 4, WebPPictureImportRGBA },
|
||||||
{ NULL, 0, NULL },
|
{ NULL, 0, NULL },
|
||||||
};
|
};
|
||||||
const WICFormatImporter nonAlphaFormatImporters[] = {
|
static const WICFormatImporter kNonAlphaFormatImporters[] = {
|
||||||
{ &GUID_WICPixelFormat24bppBGR_, 3, WebPPictureImportBGR },
|
{ &GUID_WICPixelFormat24bppBGR_, 3, WebPPictureImportBGR },
|
||||||
{ &GUID_WICPixelFormat24bppRGB_, 3, WebPPictureImportRGB },
|
{ &GUID_WICPixelFormat24bppRGB_, 3, WebPPictureImportRGB },
|
||||||
{ NULL, 0, NULL },
|
{ NULL, 0, NULL },
|
||||||
};
|
};
|
||||||
HRESULT hr = S_OK;
|
HRESULT hr = S_OK;
|
||||||
IWICBitmapFrameDecode* pFrame = NULL;
|
IWICBitmapFrameDecode* frame = NULL;
|
||||||
IWICFormatConverter* pConverter = NULL;
|
IWICFormatConverter* converter = NULL;
|
||||||
IWICImagingFactory* pFactory = NULL;
|
IWICImagingFactory* factory = NULL;
|
||||||
IWICBitmapDecoder* pDecoder = NULL;
|
IWICBitmapDecoder* decoder = NULL;
|
||||||
IStream* pStream = NULL;
|
IStream* stream = NULL;
|
||||||
UINT frameCount = 0;
|
UINT frame_count = 0;
|
||||||
UINT width = 0, height = 0;
|
UINT width = 0, height = 0;
|
||||||
BYTE* rgb = NULL;
|
BYTE* rgb = NULL;
|
||||||
WICPixelFormatGUID srcPixelFormat = GUID_WICPixelFormatUndefined;
|
WICPixelFormatGUID src_pixel_format = GUID_WICPixelFormatUndefined;
|
||||||
const WICFormatImporter* importer = NULL;
|
const WICFormatImporter* importer = NULL;
|
||||||
GUID srcContainerFormat = GUID_NULL_;
|
GUID src_container_format = GUID_NULL_;
|
||||||
const GUID* alphaContainers[] = {
|
static const GUID* kAlphaContainers[] = {
|
||||||
&GUID_ContainerFormatBmp,
|
&GUID_ContainerFormatBmp,
|
||||||
&GUID_ContainerFormatPng,
|
&GUID_ContainerFormatPng,
|
||||||
&GUID_ContainerFormatTiff,
|
&GUID_ContainerFormatTiff,
|
||||||
@ -237,8 +236,9 @@ int ReadPictureWithWIC(const char* const filename,
|
|||||||
|
|
||||||
IFS(CoInitialize(NULL));
|
IFS(CoInitialize(NULL));
|
||||||
IFS(CoCreateInstance(MAKE_REFGUID(CLSID_WICImagingFactory), NULL,
|
IFS(CoCreateInstance(MAKE_REFGUID(CLSID_WICImagingFactory), NULL,
|
||||||
CLSCTX_INPROC_SERVER, MAKE_REFGUID(IID_IWICImagingFactory),
|
CLSCTX_INPROC_SERVER,
|
||||||
(LPVOID*)&pFactory));
|
MAKE_REFGUID(IID_IWICImagingFactory),
|
||||||
|
(LPVOID*)&factory));
|
||||||
if (hr == REGDB_E_CLASSNOTREG) {
|
if (hr == REGDB_E_CLASSNOTREG) {
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
"Couldn't access Windows Imaging Component (are you running "
|
"Couldn't access Windows Imaging Component (are you running "
|
||||||
@ -246,58 +246,60 @@ int ReadPictureWithWIC(const char* const filename,
|
|||||||
"Use -s for the available YUV input.\n");
|
"Use -s for the available YUV input.\n");
|
||||||
}
|
}
|
||||||
// Prepare for image decoding.
|
// Prepare for image decoding.
|
||||||
IFS(OpenInputStream(filename, &pStream));
|
IFS(OpenInputStream(filename, &stream));
|
||||||
IFS(IWICImagingFactory_CreateDecoderFromStream(pFactory, pStream, NULL,
|
IFS(IWICImagingFactory_CreateDecoderFromStream(
|
||||||
WICDecodeMetadataCacheOnDemand, &pDecoder));
|
factory, stream, NULL,
|
||||||
IFS(IWICBitmapDecoder_GetFrameCount(pDecoder, &frameCount));
|
WICDecodeMetadataCacheOnDemand, &decoder));
|
||||||
if (SUCCEEDED(hr) && frameCount == 0) {
|
IFS(IWICBitmapDecoder_GetFrameCount(decoder, &frame_count));
|
||||||
|
if (SUCCEEDED(hr) && frame_count == 0) {
|
||||||
fprintf(stderr, "No frame found in input file.\n");
|
fprintf(stderr, "No frame found in input file.\n");
|
||||||
hr = E_FAIL;
|
hr = E_FAIL;
|
||||||
}
|
}
|
||||||
IFS(IWICBitmapDecoder_GetFrame(pDecoder, 0, &pFrame));
|
IFS(IWICBitmapDecoder_GetFrame(decoder, 0, &frame));
|
||||||
IFS(IWICBitmapFrameDecode_GetPixelFormat(pFrame, &srcPixelFormat));
|
IFS(IWICBitmapFrameDecode_GetPixelFormat(frame, &src_pixel_format));
|
||||||
IFS(IWICBitmapDecoder_GetContainerFormat(pDecoder, &srcContainerFormat));
|
IFS(IWICBitmapDecoder_GetContainerFormat(decoder, &src_container_format));
|
||||||
|
|
||||||
if (keep_alpha) {
|
if (keep_alpha) {
|
||||||
const GUID** guid;
|
const GUID** guid;
|
||||||
for (guid = alphaContainers; *guid != NULL; ++guid) {
|
for (guid = kAlphaContainers; *guid != NULL; ++guid) {
|
||||||
if (IsEqualGUID(MAKE_REFGUID(srcContainerFormat), MAKE_REFGUID(**guid))) {
|
if (IsEqualGUID(MAKE_REFGUID(src_container_format),
|
||||||
has_alpha = HasAlpha(pFactory, pDecoder, pFrame, srcPixelFormat);
|
MAKE_REFGUID(**guid))) {
|
||||||
|
has_alpha = HasAlpha(factory, decoder, frame, src_pixel_format);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prepare for pixel format conversion (if necessary).
|
// Prepare for pixel format conversion (if necessary).
|
||||||
IFS(IWICImagingFactory_CreateFormatConverter(pFactory, &pConverter));
|
IFS(IWICImagingFactory_CreateFormatConverter(factory, &converter));
|
||||||
|
|
||||||
for (importer = has_alpha ? alphaFormatImporters : nonAlphaFormatImporters;
|
for (importer = has_alpha ? kAlphaFormatImporters : kNonAlphaFormatImporters;
|
||||||
hr == S_OK && importer->import != NULL; ++importer) {
|
hr == S_OK && importer->import != NULL; ++importer) {
|
||||||
BOOL canConvert;
|
BOOL can_convert;
|
||||||
const HRESULT cchr = IWICFormatConverter_CanConvert(
|
const HRESULT cchr = IWICFormatConverter_CanConvert(
|
||||||
pConverter,
|
converter,
|
||||||
MAKE_REFGUID(srcPixelFormat),
|
MAKE_REFGUID(src_pixel_format),
|
||||||
MAKE_REFGUID(*importer->pixel_format),
|
MAKE_REFGUID(*importer->pixel_format),
|
||||||
&canConvert);
|
&can_convert);
|
||||||
if (SUCCEEDED(cchr) && canConvert) break;
|
if (SUCCEEDED(cchr) && can_convert) break;
|
||||||
}
|
}
|
||||||
if (importer->import == NULL) hr = E_FAIL;
|
if (importer->import == NULL) hr = E_FAIL;
|
||||||
|
|
||||||
IFS(IWICFormatConverter_Initialize(pConverter, (IWICBitmapSource*)pFrame,
|
IFS(IWICFormatConverter_Initialize(converter, (IWICBitmapSource*)frame,
|
||||||
importer->pixel_format,
|
importer->pixel_format,
|
||||||
WICBitmapDitherTypeNone,
|
WICBitmapDitherTypeNone,
|
||||||
NULL, 0.0, WICBitmapPaletteTypeCustom));
|
NULL, 0.0, WICBitmapPaletteTypeCustom));
|
||||||
|
|
||||||
// Decode.
|
// Decode.
|
||||||
IFS(IWICFormatConverter_GetSize(pConverter, &width, &height));
|
IFS(IWICFormatConverter_GetSize(converter, &width, &height));
|
||||||
stride = importer->bytes_per_pixel * width * sizeof(*rgb);
|
stride = importer->bytes_per_pixel * width * sizeof(*rgb);
|
||||||
if (SUCCEEDED(hr)) {
|
if (SUCCEEDED(hr)) {
|
||||||
rgb = (BYTE*)malloc(stride * height);
|
rgb = (BYTE*)malloc(stride * height);
|
||||||
if (rgb == NULL)
|
if (rgb == NULL)
|
||||||
hr = E_OUTOFMEMORY;
|
hr = E_OUTOFMEMORY;
|
||||||
}
|
}
|
||||||
IFS(IWICFormatConverter_CopyPixels(pConverter, NULL, stride,
|
IFS(IWICFormatConverter_CopyPixels(converter, NULL,
|
||||||
stride * height, rgb));
|
stride, stride * height, rgb));
|
||||||
|
|
||||||
// WebP conversion.
|
// WebP conversion.
|
||||||
if (SUCCEEDED(hr)) {
|
if (SUCCEEDED(hr)) {
|
||||||
@ -305,12 +307,11 @@ int ReadPictureWithWIC(const char* const filename,
|
|||||||
pic->width = width;
|
pic->width = width;
|
||||||
pic->height = height;
|
pic->height = height;
|
||||||
ok = importer->import(pic, rgb, stride);
|
ok = importer->import(pic, rgb, stride);
|
||||||
if (!ok)
|
if (!ok) hr = E_FAIL;
|
||||||
hr = E_FAIL;
|
|
||||||
}
|
}
|
||||||
if (SUCCEEDED(hr)) {
|
if (SUCCEEDED(hr)) {
|
||||||
if (metadata != NULL) {
|
if (metadata != NULL) {
|
||||||
hr = ExtractMetadata(pFactory, pFrame, metadata);
|
hr = ExtractMetadata(factory, frame, metadata);
|
||||||
if (FAILED(hr)) {
|
if (FAILED(hr)) {
|
||||||
fprintf(stderr, "Error extracting image metadata using WIC!\n");
|
fprintf(stderr, "Error extracting image metadata using WIC!\n");
|
||||||
}
|
}
|
||||||
@ -318,11 +319,11 @@ int ReadPictureWithWIC(const char* const filename,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Cleanup.
|
// Cleanup.
|
||||||
if (pConverter != NULL) IUnknown_Release(pConverter);
|
if (converter != NULL) IUnknown_Release(converter);
|
||||||
if (pFrame != NULL) IUnknown_Release(pFrame);
|
if (frame != NULL) IUnknown_Release(frame);
|
||||||
if (pDecoder != NULL) IUnknown_Release(pDecoder);
|
if (decoder != NULL) IUnknown_Release(decoder);
|
||||||
if (pFactory != NULL) IUnknown_Release(pFactory);
|
if (factory != NULL) IUnknown_Release(factory);
|
||||||
if (pStream != NULL) IUnknown_Release(pStream);
|
if (stream != NULL) IUnknown_Release(stream);
|
||||||
free(rgb);
|
free(rgb);
|
||||||
return SUCCEEDED(hr);
|
return SUCCEEDED(hr);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user