-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageLoader.cpp
More file actions
184 lines (161 loc) · 7.42 KB
/
ImageLoader.cpp
File metadata and controls
184 lines (161 loc) · 7.42 KB
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
179
180
181
182
183
184
#include "ImageLoader.hpp"
#include <wincodec.h>
#include <gdiplus.h>
#pragma comment(lib, "windowscodecs.lib")
#pragma comment(lib, "gdiplus.lib")
namespace imageLoading {
namespace {
imageData LoadRgba8WithGdiPlus(const std::filesystem::path& filepath) {
Gdiplus::GdiplusStartupInput startupInput;
ULONG_PTR token = 0;
const Gdiplus::Status startupStatus = Gdiplus::GdiplusStartup(&token, &startupInput, nullptr);
if (startupStatus != Gdiplus::Ok) {
std::cout << std::format(
"[ ImageLoader ] ERROR\nGDI+ startup failed for image: {}\nStatus: {}\n",
filepath.string(),
static_cast<int>(startupStatus));
abort();
}
auto shutdownGdi = [&]() {
if (token)
Gdiplus::GdiplusShutdown(token);
};
imageData image;
{
Gdiplus::Bitmap bitmap(filepath.c_str());
if (bitmap.GetLastStatus() != Gdiplus::Ok) {
std::cout << std::format(
"[ ImageLoader ] ERROR\nGDI+ failed to open image: {}\nStatus: {}\n",
filepath.string(),
static_cast<int>(bitmap.GetLastStatus()));
shutdownGdi();
abort();
}
const UINT width = bitmap.GetWidth();
const UINT height = bitmap.GetHeight();
if (width == 0 || height == 0) {
std::cout << std::format("[ ImageLoader ] ERROR\nGDI+ reported invalid image size: {}\n", filepath.string());
shutdownGdi();
abort();
}
Gdiplus::Rect rect(0, 0, static_cast<INT>(width), static_cast<INT>(height));
Gdiplus::BitmapData bitmapData{};
const Gdiplus::Status lockStatus = bitmap.LockBits(
&rect,
Gdiplus::ImageLockModeRead,
PixelFormat32bppARGB,
&bitmapData);
if (lockStatus != Gdiplus::Ok) {
std::cout << std::format(
"[ ImageLoader ] ERROR\nGDI+ failed to lock image bits: {}\nStatus: {}\n",
filepath.string(),
static_cast<int>(lockStatus));
shutdownGdi();
abort();
}
image.width = width;
image.height = height;
image.pixels.resize(static_cast<size_t>(width) * height * 4);
for (UINT y = 0; y < height; ++y) {
const auto* srcRow = static_cast<const uint8_t*>(bitmapData.Scan0) + static_cast<ptrdiff_t>(y) * bitmapData.Stride;
auto* dstRow = image.pixels.data() + static_cast<size_t>(y) * width * 4;
for (UINT x = 0; x < width; ++x) {
const uint8_t b = srcRow[x * 4 + 0];
const uint8_t g = srcRow[x * 4 + 1];
const uint8_t r = srcRow[x * 4 + 2];
const uint8_t a = srcRow[x * 4 + 3];
dstRow[x * 4 + 0] = r;
dstRow[x * 4 + 1] = g;
dstRow[x * 4 + 2] = b;
dstRow[x * 4 + 3] = a;
}
}
bitmap.UnlockBits(&bitmapData);
}
shutdownGdi();
return image;
}
}
imageData LoadRgba8(const std::filesystem::path& filepath) {
HRESULT hr = CoInitializeEx(nullptr, COINIT_MULTITHREADED);
const bool shouldUninitialize = SUCCEEDED(hr);
if (FAILED(hr) && hr != RPC_E_CHANGED_MODE) {
std::cout << std::format("[ ImageLoader ] ERROR\nFailed to initialize COM. HRESULT: 0x{:08X}\n", static_cast<uint32_t>(hr));
abort();
}
IWICImagingFactory* factory = nullptr;
IWICBitmapDecoder* decoder = nullptr;
IWICBitmapFrameDecode* frame = nullptr;
IWICFormatConverter* converter = nullptr;
auto releaseAll = [&]() {
if (converter) converter->Release();
if (frame) frame->Release();
if (decoder) decoder->Release();
if (factory) factory->Release();
if (shouldUninitialize) CoUninitialize();
};
auto fallbackToGdiPlus = [&]() {
releaseAll();
return LoadRgba8WithGdiPlus(filepath);
};
hr = CoCreateInstance(
CLSID_WICImagingFactory,
nullptr,
CLSCTX_INPROC_SERVER,
IID_PPV_ARGS(&factory));
if (FAILED(hr)) {
std::cout << std::format("[ ImageLoader ] WARNING\nWIC factory creation failed, falling back to GDI+: HRESULT 0x{:08X}\n", static_cast<uint32_t>(hr));
return fallbackToGdiPlus();
}
hr = factory->CreateDecoderFromFilename(
filepath.c_str(),
nullptr,
GENERIC_READ,
WICDecodeMetadataCacheOnLoad,
&decoder);
if (FAILED(hr)) {
std::cout << std::format("[ ImageLoader ] WARNING\nWIC failed to open image, falling back to GDI+: {}\nHRESULT: 0x{:08X}\n", filepath.string(), static_cast<uint32_t>(hr));
return fallbackToGdiPlus();
}
hr = decoder->GetFrame(0, &frame);
if (FAILED(hr)) {
std::cout << std::format("[ ImageLoader ] WARNING\nWIC failed to get image frame, falling back to GDI+: {}\nHRESULT: 0x{:08X}\n", filepath.string(), static_cast<uint32_t>(hr));
return fallbackToGdiPlus();
}
hr = factory->CreateFormatConverter(&converter);
if (FAILED(hr)) {
std::cout << std::format("[ ImageLoader ] WARNING\nWIC failed to create format converter, falling back to GDI+: HRESULT 0x{:08X}\n", static_cast<uint32_t>(hr));
return fallbackToGdiPlus();
}
hr = converter->Initialize(
frame,
GUID_WICPixelFormat32bppRGBA,
WICBitmapDitherTypeNone,
nullptr,
0.0,
WICBitmapPaletteTypeCustom);
if (FAILED(hr)) {
std::cout << std::format("[ ImageLoader ] WARNING\nWIC failed to convert image to RGBA8, falling back to GDI+: {}\nHRESULT: 0x{:08X}\n", filepath.string(), static_cast<uint32_t>(hr));
return fallbackToGdiPlus();
}
imageData image;
hr = converter->GetSize(&image.width, &image.height);
if (FAILED(hr) || image.width == 0 || image.height == 0) {
std::cout << std::format("[ ImageLoader ] WARNING\nWIC reported invalid image size, falling back to GDI+: {}\nHRESULT: 0x{:08X}\n", filepath.string(), static_cast<uint32_t>(hr));
return fallbackToGdiPlus();
}
const uint32_t rowPitch = image.width * 4;
image.pixels.resize(static_cast<size_t>(rowPitch) * image.height);
hr = converter->CopyPixels(
nullptr,
rowPitch,
static_cast<uint32_t>(image.pixels.size()),
image.pixels.data());
if (FAILED(hr)) {
std::cout << std::format("[ ImageLoader ] WARNING\nWIC failed to copy image pixels, falling back to GDI+: {}\nHRESULT: 0x{:08X}\n", filepath.string(), static_cast<uint32_t>(hr));
return fallbackToGdiPlus();
}
releaseAll();
return image;
}
}