-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeifCrash1.cpp
178 lines (138 loc) · 4 KB
/
HeifCrash1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#define WINVER 0x0A00
#define _WIN32_WINNT 0x0A00
#include <Windows.h>
#include <wincodec.h>
// ATL/COM
#include <comcat.h>
#define _ATL_FREE_THREADED
#include <atlbase.h>
#include <atlcoll.h>
#include <atlcom.h>
#include <atlcomcli.h>
#include <atlenc.h>
CComModule _Module;
// WIC
#include <wincodec.h>
#pragma comment(lib, "windowscodecs.lib")
#include <iostream>
using namespace std;
#define IFS(hr, expr) \
cout << #expr; \
if (SUCCEEDED((hr))) \
{ \
(hr) = (expr); \
} \
cout << " -> " << (hr) << endl; \
// Creates an instance of T and returns it to the caller with
// a ref count of 1. It will still need to be initialized.
template<typename T>
static HRESULT CreateComObject(T** ppResult)
{
if (ppResult == NULL)
{
return E_POINTER;
}
*ppResult = NULL;
HRESULT hr = S_OK;
CComObject<T>* pObject = NULL;
if (SUCCEEDED(hr))
{
hr = CComObject<T>::CreateInstance(&pObject);
}
if (SUCCEEDED(hr))
{
pObject->AddRef();
*ppResult = pObject;
}
return hr;
}
int wmain(int argc, wchar_t** argv)
{
if (argc != 3)
{
cout << "arg1 is the input filename (any type), and arg2 is the output filename (using HEIF/HEIC)" << endl;
return 1;
}
const wchar_t* szInputFileName = argv[1];
cout << "Input file: " << szInputFileName << endl;
const wchar_t* szOutputFileName = argv[2];
cout << "Output file: " << szOutputFileName << endl;
HRESULT hr = S_OK;
IFS(hr, CoInitializeEx(NULL, COINIT_MULTITHREADED));
CComPtr<IWICImagingFactory2> spFactory;
IFS(hr, CoCreateInstance(
CLSID_WICImagingFactory2,
NULL,
CLSCTX_INPROC_SERVER,
__uuidof(IWICImagingFactory),
reinterpret_cast<void**>(&spFactory)));
CComPtr<IWICStream> spInputStream;
IFS(hr, spFactory->CreateStream(&spInputStream));
IFS(hr, spInputStream->InitializeFromFilename(
szInputFileName,
GENERIC_READ));
CComPtr<IWICBitmapDecoder> spDecoder;
IFS(hr, spFactory->CreateDecoderFromStream(
spInputStream,
NULL,
WICDecodeMetadataCacheOnDemand,
&spDecoder));
CComPtr<IWICBitmapFrameDecode> spFrame0Decode;
IFS(hr, spDecoder->GetFrame(
0,
&spFrame0Decode));
if (SUCCEEDED(hr))
{
DeleteFileW(szOutputFileName);
// don't care if it fails -- could mean the file doesn't exist yet, which is fine
}
CComPtr<IWICStream> spOutputStream;
IFS(hr, spFactory->CreateStream(&spOutputStream));
IFS(hr, spOutputStream->InitializeFromFilename(
szOutputFileName,
GENERIC_READ | GENERIC_WRITE));
CComPtr<IWICBitmapEncoder> spEncoder;
IFS(hr, spFactory->CreateEncoder(
GUID_ContainerFormatHeif,
&GUID_VendorMicrosoft,
&spEncoder));
IFS(hr, spEncoder->Initialize(
spOutputStream,
WICBitmapEncoderNoCache));
CComPtr<IWICBitmapFrameEncode> spFrame0Encode;
CComPtr<IPropertyBag2> spFrameOptions;
IFS(hr, spEncoder->CreateNewFrame(
&spFrame0Encode,
&spFrameOptions));
CComBSTR bstrOptionName = SysAllocString(L"ImageQuality");
if (SUCCEEDED(hr))
{
PROPBAG2 option = { 0 };
option.pstrName = bstrOptionName;
VARIANT varValue;
VariantInit(&varValue);
varValue.vt = VT_R4;
// Using a quality of 0.95 causes a crash. Using 0.94 or below (I tested as far down as 0.50) works fine.
varValue.fltVal = 0.95f;
IFS(hr, spFrameOptions->Write(
1,
&option,
&varValue));
}
IFS(hr, spFrame0Encode->Initialize(spFrameOptions));
IFS(hr, spFrame0Encode->WriteSource(
spFrame0Decode,
NULL));
IFS(hr, spFrame0Encode->Commit());
if (SUCCEEDED(hr))
{
spFrame0Encode.Release();
}
IFS(hr, spEncoder->Commit());
if (SUCCEEDED(hr))
{
spEncoder.Release();
}
cout << "Return value, HRESULT = " << hr << endl;
return hr;
}