forked from agruzdev/FreeImageRe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleTools.cpp
More file actions
348 lines (324 loc) · 8.88 KB
/
Copy pathSimpleTools.cpp
File metadata and controls
348 lines (324 loc) · 8.88 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
//===========================================================
// FreeImage Re(surrected)
// Modified fork from the original FreeImage 3.18
// with updated dependencies and extended features.
//===========================================================
#include "SimpleTools.h"
#include <cstring>
#include <algorithm>
#include "yato/types.h"
namespace {
struct YuvBrightness
{
template <typename Ty_>
inline
auto operator()(const Ty_& p, std::enable_if_t<IsPixelType<Ty_>::value, void*> = nullptr)
{
return p.red;
}
};
template <typename PixelTy_, typename BrightnessOp_ = Brightness>
std::tuple<PixelTy_*, PixelTy_*, double, double> FindMinMax(FIBITMAP* src, BrightnessOp_ brightnessOp = BrightnessOp_{})
{
PixelTy_* minIt{}, * maxIt{};
double minVal = 0.0, maxVal = 0.0;
if (src) {
const unsigned h = FreeImage_GetHeight(src);
const unsigned w = FreeImage_GetWidth(src);
const unsigned pitch = FreeImage_GetPitch(src);
uint8_t *srcLine = FreeImage_GetBits(src);
for (unsigned j = 0; j < h; ++j, srcLine += pitch) {
auto pixIt = yato::pointer_cast<PixelTy_*>(srcLine);
for (unsigned i = 0; i < w; ++i, ++pixIt) {
if (IsNan(*pixIt)) {
continue;
}
const auto b = static_cast<double>(brightnessOp(*pixIt));
if (!minIt || !maxIt) {
minIt = maxIt = pixIt;
minVal = maxVal = b;
}
else {
if (b < minVal) {
minIt = pixIt;
minVal = b;
}
if (maxVal < b) {
maxIt = pixIt;
maxVal = b;
}
}
}
}
}
return std::make_tuple(minIt, maxIt, minVal, maxVal);
}
template <typename PixelType_>
inline constexpr
void PixelFill(PixelType_& p, const ToValueType<PixelType_>& v)
{
SetChannel<0>(p, v);
SetChannel<1>(p, v);
SetChannel<2>(p, v);
SetChannel<3>(p, v);
}
template <typename PixelType_>
void FindMinMaxValueImpl(FIBITMAP* src, void* out_min_value, void* out_max_value)
{
PixelType_ minVal, maxVal;
PixelFill(minVal, std::numeric_limits<ToValueType<PixelType_>>::max());
PixelFill(maxVal, std::numeric_limits<ToValueType<PixelType_>>::lowest());
const unsigned width = FreeImage_GetWidth(src);
const unsigned height = FreeImage_GetHeight(src);
const unsigned src_pitch = FreeImage_GetPitch(src);
const uint8_t* src_bits = FreeImage_GetBits(src);
for (unsigned y = 0; y < height; ++y) {
auto src_pixel = yato::pointer_cast<const PixelType_*>(src_bits);
for (unsigned x = 0; x < width; ++x) {
const PixelType_ val = src_pixel[x];
SetChannel<0>(minVal, std::min(GetChannel<0>(minVal), GetChannel<0>(val)));
SetChannel<1>(minVal, std::min(GetChannel<1>(minVal), GetChannel<1>(val)));
SetChannel<2>(minVal, std::min(GetChannel<2>(minVal), GetChannel<2>(val)));
SetChannel<3>(minVal, std::min(GetChannel<3>(minVal), GetChannel<3>(val)));
SetChannel<0>(maxVal, std::max(GetChannel<0>(maxVal), GetChannel<0>(val)));
SetChannel<1>(maxVal, std::max(GetChannel<1>(maxVal), GetChannel<1>(val)));
SetChannel<2>(maxVal, std::max(GetChannel<2>(maxVal), GetChannel<2>(val)));
SetChannel<3>(maxVal, std::max(GetChannel<3>(maxVal), GetChannel<3>(val)));
}
src_bits += src_pitch;
}
if (out_min_value) {
*static_cast<PixelType_*>(out_min_value) = minVal;
}
if (out_max_value) {
*static_cast<PixelType_*>(out_max_value) = maxVal;
}
}
} // unnamed namespace
FIBOOL DLL_CALLCONV
FreeImage_FindMinMax(FIBITMAP* dib, double* min_brightness, double* max_brightness, void** min_ptr, void** max_ptr)
{
if (!FreeImage_HasPixels(dib)) {
return FALSE;
}
std::tuple<void*, void*, double, double> res{};
bool success = false;
const auto colorType = FreeImage_GetColorType2(dib);
switch (FreeImage_GetImageType(dib))
{
case FIT_BITMAP: {
const auto bpp = FreeImage_GetBPP(dib);
if (bpp == 32) {
if (colorType == FIC_RGBALPHA) {
res = FindMinMax<FIRGBA8>(dib);
success = true;
}
else if (colorType == FIC_YUV) {
res = FindMinMax<FIRGBA8, YuvBrightness>(dib);
success = true;
}
}
else if (bpp == 24) {
if (colorType == FIC_RGB) {
res = FindMinMax<FIRGB8>(dib);
success = true;
}
else if (colorType == FIC_YUV) {
res = FindMinMax<FIRGB8, YuvBrightness>(dib);
success = true;
}
}
else if (bpp == 8) {
if (colorType == FIC_MINISBLACK) {
res = FindMinMax<uint8_t>(dib);
success = true;
}
}
}
break;
case FIT_RGBAF:
if (colorType == FIC_RGBALPHA) {
res = FindMinMax<FIRGBAF>(dib);
success = true;
}
else if (colorType == FIC_YUV) {
res = FindMinMax<FIRGBAF, YuvBrightness>(dib);
success = true;
}
break;
case FIT_RGBF:
if (colorType == FIC_RGB) {
res = FindMinMax<FIRGBF>(dib);
success = true;
}
else if (colorType == FIC_YUV) {
res = FindMinMax<FIRGBF, YuvBrightness>(dib);
success = true;
}
break;
case FIT_RGBA32:
res = FindMinMax<FIRGBA32>(dib);
success = true;
break;
case FIT_RGB32:
res = FindMinMax<FIRGB32>(dib);
success = true;
break;
case FIT_RGBA16:
res = FindMinMax<FIRGBA16>(dib);
success = true;
break;
case FIT_RGB16:
res = FindMinMax<FIRGB16>(dib);
success = true;
break;
case FIT_DOUBLE:
res = FindMinMax<double>(dib);
success = true;
break;
case FIT_FLOAT:
res = FindMinMax<float>(dib);
success = true;
break;
case FIT_UINT32:
res = FindMinMax<uint32_t>(dib);
success = true;
break;
case FIT_INT32:
res = FindMinMax<int32_t>(dib);
success = true;
break;
case FIT_UINT16:
res = FindMinMax<uint16_t>(dib);
success = true;
break;
case FIT_INT16:
res = FindMinMax<int32_t>(dib);
success = true;
break;
default:
break;
}
if (success) {
if (min_brightness) {
*min_brightness = std::get<2>(res);
}
if (max_brightness) {
*max_brightness = std::get<3>(res);
}
if (min_ptr) {
*min_ptr = std::get<0>(res);
}
if (max_ptr) {
*max_ptr = std::get<1>(res);
}
}
return success ? TRUE : FALSE;
}
FIBOOL DLL_CALLCONV
FreeImage_FindMinMaxValue(FIBITMAP* dib, void* min_value, void* max_value)
{
if (!FreeImage_HasPixels(dib)) {
return FALSE;
}
switch (FreeImage_GetImageType(dib)) {
case FIT_BITMAP: {
const auto bpp = FreeImage_GetBPP(dib);
const auto colorType = FreeImage_GetColorType2(dib);
if (bpp == 32) {
if (colorType == FIC_RGBALPHA || colorType == FIC_YUV) {
FindMinMaxValueImpl<FIRGBA8>(dib, min_value, max_value);
}
else {
return FALSE;
}
}
else if (bpp == 24) {
if (colorType == FIC_RGB || colorType == FIC_YUV) {
FindMinMaxValueImpl<FIRGB8>(dib, min_value, max_value);
}
else {
return FALSE;
}
}
else if (bpp == 8) {
if (colorType == FIC_MINISBLACK) {
FindMinMaxValueImpl<uint8_t>(dib, min_value, max_value);
}
else {
return FALSE;
}
}
else {
return FALSE;
}
}
break;
case FIT_RGBAF:
FindMinMaxValueImpl<FIRGBAF>(dib, min_value, max_value);
break;
case FIT_RGBF:
FindMinMaxValueImpl<FIRGBF>(dib, min_value, max_value);
break;
case FIT_RGBA32:
FindMinMaxValueImpl<FIRGBA32>(dib, min_value, max_value);
break;
case FIT_RGB32:
FindMinMaxValueImpl<FIRGB32>(dib, min_value, max_value);
break;
case FIT_RGBA16:
FindMinMaxValueImpl<FIRGBA16>(dib, min_value, max_value);
break;
case FIT_RGB16:
FindMinMaxValueImpl<FIRGB16>(dib, min_value, max_value);
break;
case FIT_DOUBLE:
FindMinMaxValueImpl<double>(dib, min_value, max_value);
break;
case FIT_FLOAT:
FindMinMaxValueImpl<float>(dib, min_value, max_value);
break;
case FIT_UINT32:
FindMinMaxValueImpl<uint32_t>(dib, min_value, max_value);
break;
case FIT_INT32:
FindMinMaxValueImpl<int32_t>(dib, min_value, max_value);
break;
case FIT_UINT16:
FindMinMaxValueImpl<uint16_t>(dib, min_value, max_value);
break;
case FIT_INT16:
FindMinMaxValueImpl<uint16_t>(dib, min_value, max_value);
break;
case FIT_COMPLEX:
FindMinMaxValueImpl<FICOMPLEX>(dib, min_value, max_value);
break;
case FIT_COMPLEXF:
FindMinMaxValueImpl<FICOMPLEXF>(dib, min_value, max_value);
break;
default:
return FALSE;
}
return TRUE;
}
FIBOOL DLL_CALLCONV
FreeImage_Fill(FIBITMAP* dib, const void* value_ptr, size_t value_size)
{
if (!FreeImage_HasPixels(dib)) {
return FALSE;
}
if (FreeImage_GetBPP(dib) != 8 * value_size) {
return FALSE;
}
const unsigned width = FreeImage_GetWidth(dib);
const unsigned height = FreeImage_GetHeight(dib);
const unsigned pitch = FreeImage_GetPitch(dib);
uint8_t* dst_line = FreeImage_GetBits(dib);
for (unsigned y = 0; y < height; ++y, dst_line += pitch) {
uint8_t* dst_pixel = dst_line;
for (unsigned x = 0; x < width; ++x, dst_pixel += value_size) {
std::memcpy(dst_pixel, value_ptr, value_size);
}
}
return TRUE;
}