2009-08-09 01:49:33 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2009 Vincent Povirk for CodeWeavers
|
2017-09-15 18:12:08 +02:00
|
|
|
* Copyright 2016 Dmitry Timoshkov
|
2009-08-09 01:49:33 +02:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
2012-09-24 08:03:10 +02:00
|
|
|
#define NONAMELESSUNION
|
2009-08-09 01:49:33 +02:00
|
|
|
#define COBJMACROS
|
|
|
|
|
|
|
|
#include "windef.h"
|
|
|
|
#include "winbase.h"
|
|
|
|
#include "objbase.h"
|
|
|
|
|
|
|
|
#include "wincodecs_private.h"
|
|
|
|
|
|
|
|
#include "wine/debug.h"
|
|
|
|
|
2015-04-01 21:57:45 +02:00
|
|
|
static inline ULONG read_ulong_be(BYTE* data)
|
|
|
|
{
|
|
|
|
return data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3];
|
|
|
|
}
|
|
|
|
|
2012-09-11 00:01:15 +02:00
|
|
|
static HRESULT LoadTextMetadata(IStream *stream, const GUID *preferred_vendor,
|
|
|
|
DWORD persist_options, MetadataItem **items, DWORD *item_count)
|
|
|
|
{
|
|
|
|
HRESULT hr;
|
|
|
|
BYTE type[4];
|
|
|
|
BYTE *data;
|
|
|
|
ULONG data_size;
|
|
|
|
ULONG name_len, value_len;
|
|
|
|
BYTE *name_end_ptr;
|
|
|
|
LPSTR name, value;
|
|
|
|
MetadataItem *result;
|
|
|
|
|
|
|
|
hr = read_png_chunk(stream, type, &data, &data_size);
|
|
|
|
if (FAILED(hr)) return hr;
|
|
|
|
|
|
|
|
name_end_ptr = memchr(data, 0, data_size);
|
|
|
|
|
|
|
|
name_len = name_end_ptr - data;
|
|
|
|
|
|
|
|
if (!name_end_ptr || name_len > 79)
|
|
|
|
{
|
|
|
|
HeapFree(GetProcessHeap(), 0, data);
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
value_len = data_size - name_len - 1;
|
|
|
|
|
2015-03-05 12:12:50 +01:00
|
|
|
result = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(MetadataItem));
|
2012-09-11 00:01:15 +02:00
|
|
|
name = HeapAlloc(GetProcessHeap(), 0, name_len + 1);
|
|
|
|
value = HeapAlloc(GetProcessHeap(), 0, value_len + 1);
|
|
|
|
if (!result || !name || !value)
|
|
|
|
{
|
|
|
|
HeapFree(GetProcessHeap(), 0, data);
|
|
|
|
HeapFree(GetProcessHeap(), 0, result);
|
|
|
|
HeapFree(GetProcessHeap(), 0, name);
|
|
|
|
HeapFree(GetProcessHeap(), 0, value);
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
PropVariantInit(&result[0].schema);
|
|
|
|
PropVariantInit(&result[0].id);
|
|
|
|
PropVariantInit(&result[0].value);
|
|
|
|
|
|
|
|
memcpy(name, data, name_len + 1);
|
|
|
|
memcpy(value, name_end_ptr + 1, value_len);
|
|
|
|
value[value_len] = 0;
|
|
|
|
|
|
|
|
result[0].id.vt = VT_LPSTR;
|
2021-03-24 18:37:50 +01:00
|
|
|
result[0].id.pszVal = name;
|
2012-09-11 00:01:15 +02:00
|
|
|
result[0].value.vt = VT_LPSTR;
|
2021-03-24 18:37:50 +01:00
|
|
|
result[0].value.pszVal = value;
|
2012-09-11 00:01:15 +02:00
|
|
|
|
|
|
|
*items = result;
|
|
|
|
*item_count = 1;
|
|
|
|
|
|
|
|
HeapFree(GetProcessHeap(), 0, data);
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const MetadataHandlerVtbl TextReader_Vtbl = {
|
|
|
|
0,
|
|
|
|
&CLSID_WICPngTextMetadataReader,
|
|
|
|
LoadTextMetadata
|
|
|
|
};
|
|
|
|
|
2014-01-14 12:18:11 +01:00
|
|
|
HRESULT PngTextReader_CreateInstance(REFIID iid, void** ppv)
|
2012-09-11 00:01:15 +02:00
|
|
|
{
|
2014-01-14 12:18:11 +01:00
|
|
|
return MetadataReader_Create(&TextReader_Vtbl, iid, ppv);
|
2012-09-11 00:01:15 +02:00
|
|
|
}
|
|
|
|
|
2015-04-01 21:57:45 +02:00
|
|
|
static HRESULT LoadGamaMetadata(IStream *stream, const GUID *preferred_vendor,
|
|
|
|
DWORD persist_options, MetadataItem **items, DWORD *item_count)
|
|
|
|
{
|
|
|
|
HRESULT hr;
|
|
|
|
BYTE type[4];
|
|
|
|
BYTE *data;
|
|
|
|
ULONG data_size;
|
|
|
|
ULONG gamma;
|
|
|
|
LPWSTR name;
|
|
|
|
MetadataItem *result;
|
|
|
|
|
|
|
|
hr = read_png_chunk(stream, type, &data, &data_size);
|
|
|
|
if (FAILED(hr)) return hr;
|
|
|
|
|
|
|
|
if (data_size < 4)
|
|
|
|
{
|
|
|
|
HeapFree(GetProcessHeap(), 0, data);
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
gamma = read_ulong_be(data);
|
|
|
|
|
|
|
|
HeapFree(GetProcessHeap(), 0, data);
|
|
|
|
|
|
|
|
result = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(MetadataItem));
|
2020-12-01 00:58:23 +01:00
|
|
|
name = HeapAlloc(GetProcessHeap(), 0, sizeof(L"ImageGamma"));
|
2015-04-01 21:57:45 +02:00
|
|
|
if (!result || !name)
|
|
|
|
{
|
|
|
|
HeapFree(GetProcessHeap(), 0, result);
|
|
|
|
HeapFree(GetProcessHeap(), 0, name);
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
PropVariantInit(&result[0].schema);
|
|
|
|
PropVariantInit(&result[0].id);
|
|
|
|
PropVariantInit(&result[0].value);
|
|
|
|
|
2020-12-01 00:58:23 +01:00
|
|
|
memcpy(name, L"ImageGamma", sizeof(L"ImageGamma"));
|
2015-04-01 21:57:45 +02:00
|
|
|
|
|
|
|
result[0].id.vt = VT_LPWSTR;
|
2021-03-24 18:37:50 +01:00
|
|
|
result[0].id.pwszVal = name;
|
2015-04-01 21:57:45 +02:00
|
|
|
result[0].value.vt = VT_UI4;
|
2021-03-24 18:37:50 +01:00
|
|
|
result[0].value.ulVal = gamma;
|
2015-04-01 21:57:45 +02:00
|
|
|
|
|
|
|
*items = result;
|
|
|
|
*item_count = 1;
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const MetadataHandlerVtbl GamaReader_Vtbl = {
|
|
|
|
0,
|
|
|
|
&CLSID_WICPngGamaMetadataReader,
|
|
|
|
LoadGamaMetadata
|
|
|
|
};
|
|
|
|
|
|
|
|
HRESULT PngGamaReader_CreateInstance(REFIID iid, void** ppv)
|
|
|
|
{
|
|
|
|
return MetadataReader_Create(&GamaReader_Vtbl, iid, ppv);
|
|
|
|
}
|
|
|
|
|
2016-06-15 19:43:40 +02:00
|
|
|
static HRESULT LoadChrmMetadata(IStream *stream, const GUID *preferred_vendor,
|
|
|
|
DWORD persist_options, MetadataItem **items, DWORD *item_count)
|
|
|
|
{
|
|
|
|
HRESULT hr;
|
|
|
|
BYTE type[4];
|
|
|
|
BYTE *data;
|
|
|
|
ULONG data_size;
|
|
|
|
static const WCHAR names[8][12] = {
|
2020-12-01 00:58:23 +01:00
|
|
|
L"WhitePointX",
|
|
|
|
L"WhitePointY",
|
|
|
|
L"RedX",
|
|
|
|
L"RedY",
|
|
|
|
L"GreenX",
|
|
|
|
L"GreenY",
|
|
|
|
L"BlueX",
|
|
|
|
L"BlueY",
|
2016-06-15 19:43:40 +02:00
|
|
|
};
|
|
|
|
LPWSTR dyn_names[8] = {0};
|
|
|
|
MetadataItem *result;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
hr = read_png_chunk(stream, type, &data, &data_size);
|
|
|
|
if (FAILED(hr)) return hr;
|
|
|
|
|
|
|
|
if (data_size < 32)
|
|
|
|
{
|
|
|
|
HeapFree(GetProcessHeap(), 0, data);
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(MetadataItem)*8);
|
|
|
|
for (i=0; i<8; i++)
|
|
|
|
{
|
|
|
|
dyn_names[i] = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR)*(lstrlenW(names[i])+1));
|
|
|
|
if (!dyn_names[i]) break;
|
|
|
|
}
|
|
|
|
if (!result || i < 8)
|
|
|
|
{
|
|
|
|
HeapFree(GetProcessHeap(), 0, result);
|
|
|
|
for (i=0; i<8; i++)
|
|
|
|
HeapFree(GetProcessHeap(), 0, dyn_names[i]);
|
|
|
|
HeapFree(GetProcessHeap(), 0, data);
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i=0; i<8; i++)
|
|
|
|
{
|
|
|
|
PropVariantInit(&result[i].schema);
|
|
|
|
|
|
|
|
PropVariantInit(&result[i].id);
|
|
|
|
result[i].id.vt = VT_LPWSTR;
|
2021-03-24 18:37:50 +01:00
|
|
|
result[i].id.pwszVal = dyn_names[i];
|
2016-06-15 19:43:40 +02:00
|
|
|
lstrcpyW(dyn_names[i], names[i]);
|
|
|
|
|
|
|
|
PropVariantInit(&result[i].value);
|
|
|
|
result[i].value.vt = VT_UI4;
|
2021-03-24 18:37:50 +01:00
|
|
|
result[i].value.ulVal = read_ulong_be(&data[i*4]);
|
2016-06-15 19:43:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
*items = result;
|
|
|
|
*item_count = 8;
|
|
|
|
|
|
|
|
HeapFree(GetProcessHeap(), 0, data);
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const MetadataHandlerVtbl ChrmReader_Vtbl = {
|
|
|
|
0,
|
|
|
|
&CLSID_WICPngChrmMetadataReader,
|
|
|
|
LoadChrmMetadata
|
|
|
|
};
|
|
|
|
|
|
|
|
HRESULT PngChrmReader_CreateInstance(REFIID iid, void** ppv)
|
|
|
|
{
|
|
|
|
return MetadataReader_Create(&ChrmReader_Vtbl, iid, ppv);
|
|
|
|
}
|
|
|
|
|
2020-11-15 21:12:25 +01:00
|
|
|
HRESULT PngDecoder_CreateInstance(REFIID iid, void** ppv)
|
2009-09-23 21:49:17 +02:00
|
|
|
{
|
2009-09-23 23:53:15 +02:00
|
|
|
HRESULT hr;
|
2020-11-15 21:12:25 +01:00
|
|
|
struct decoder *decoder;
|
|
|
|
struct decoder_info decoder_info;
|
2009-09-23 23:53:15 +02:00
|
|
|
|
2021-10-18 14:36:33 +02:00
|
|
|
hr = png_decoder_create(&decoder_info, &decoder);
|
2009-09-23 23:53:15 +02:00
|
|
|
|
|
|
|
if (SUCCEEDED(hr))
|
2020-11-15 21:12:25 +01:00
|
|
|
hr = CommonDecoder_CreateInstance(decoder, &decoder_info, iid, ppv);
|
2017-09-05 20:57:32 +02:00
|
|
|
|
|
|
|
return hr;
|
2009-09-23 20:54:39 +02:00
|
|
|
}
|
|
|
|
|
2014-01-14 12:18:11 +01:00
|
|
|
HRESULT PngEncoder_CreateInstance(REFIID iid, void** ppv)
|
2020-10-09 21:56:23 +02:00
|
|
|
{
|
|
|
|
HRESULT hr;
|
2020-11-15 21:12:25 +01:00
|
|
|
struct encoder *encoder;
|
|
|
|
struct encoder_info encoder_info;
|
2020-10-09 21:56:23 +02:00
|
|
|
|
2021-10-18 14:36:33 +02:00
|
|
|
hr = png_encoder_create(&encoder_info, &encoder);
|
2020-10-09 21:56:23 +02:00
|
|
|
|
|
|
|
if (SUCCEEDED(hr))
|
2020-11-15 21:12:25 +01:00
|
|
|
hr = CommonEncoder_CreateInstance(encoder, &encoder_info, iid, ppv);
|
2020-10-09 21:56:23 +02:00
|
|
|
|
|
|
|
return hr;
|
|
|
|
}
|