Skip to content

Commit a96ed98

Browse files
authored
Merge pull request #76 from lordnn/master
More `new` used instead of `malloc()`. Code reorganization.
2 parents a00f5f0 + 0766178 commit a96ed98

17 files changed

Lines changed: 257 additions & 307 deletions

Source/FreeImage/SimpleTools.cpp

Lines changed: 97 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,104 @@
77
#include "SimpleTools.h"
88
#include <cstring>
99
#include <algorithm>
10+
#include "yato/types.h"
1011

12+
namespace {
13+
14+
struct YuvBrightness
15+
{
16+
template <typename Ty_>
17+
inline
18+
auto operator()(const Ty_& p, std::enable_if_t<IsPixelType<Ty_>::value, void*> = nullptr)
19+
{
20+
return p.red;
21+
}
22+
};
23+
24+
template <typename PixelTy_, typename BrightnessOp_ = Brightness>
25+
std::tuple<PixelTy_*, PixelTy_*, double, double> FindMinMax(FIBITMAP* src, BrightnessOp_ brightnessOp = BrightnessOp_{})
26+
{
27+
PixelTy_* minIt{}, * maxIt{};
28+
double minVal = 0.0, maxVal = 0.0;
29+
if (src) {
30+
const unsigned h = FreeImage_GetHeight(src);
31+
const unsigned w = FreeImage_GetWidth(src);
32+
const unsigned pitch = FreeImage_GetPitch(src);
33+
uint8_t *srcLine = FreeImage_GetBits(src);
34+
for (unsigned j = 0; j < h; ++j, srcLine += pitch) {
35+
auto pixIt = yato::pointer_cast<PixelTy_*>(srcLine);
36+
for (unsigned i = 0; i < w; ++i, ++pixIt) {
37+
if (IsNan(*pixIt)) {
38+
continue;
39+
}
40+
const auto b = static_cast<double>(brightnessOp(*pixIt));
41+
if (!minIt || !maxIt) {
42+
minIt = maxIt = pixIt;
43+
minVal = maxVal = b;
44+
}
45+
else {
46+
if (b < minVal) {
47+
minIt = pixIt;
48+
minVal = b;
49+
}
50+
if (maxVal < b) {
51+
maxIt = pixIt;
52+
maxVal = b;
53+
}
54+
}
55+
}
56+
}
57+
}
58+
return std::make_tuple(minIt, maxIt, minVal, maxVal);
59+
}
60+
61+
template <typename PixelType_>
62+
inline constexpr
63+
void PixelFill(PixelType_& p, const ToValueType<PixelType_>& v)
64+
{
65+
SetChannel<0>(p, v);
66+
SetChannel<1>(p, v);
67+
SetChannel<2>(p, v);
68+
SetChannel<3>(p, v);
69+
}
70+
71+
template <typename PixelType_>
72+
void FindMinMaxValueImpl(FIBITMAP* src, void* out_min_value, void* out_max_value)
73+
{
74+
PixelType_ minVal, maxVal;
75+
PixelFill(minVal, std::numeric_limits<ToValueType<PixelType_>>::max());
76+
PixelFill(maxVal, std::numeric_limits<ToValueType<PixelType_>>::lowest());
77+
78+
const unsigned width = FreeImage_GetWidth(src);
79+
const unsigned height = FreeImage_GetHeight(src);
80+
const unsigned src_pitch = FreeImage_GetPitch(src);
81+
82+
const uint8_t* src_bits = FreeImage_GetBits(src);
83+
for (unsigned y = 0; y < height; ++y) {
84+
auto src_pixel = yato::pointer_cast<const PixelType_*>(src_bits);
85+
for (unsigned x = 0; x < width; ++x) {
86+
const PixelType_ val = src_pixel[x];
87+
SetChannel<0>(minVal, std::min(GetChannel<0>(minVal), GetChannel<0>(val)));
88+
SetChannel<1>(minVal, std::min(GetChannel<1>(minVal), GetChannel<1>(val)));
89+
SetChannel<2>(minVal, std::min(GetChannel<2>(minVal), GetChannel<2>(val)));
90+
SetChannel<3>(minVal, std::min(GetChannel<3>(minVal), GetChannel<3>(val)));
91+
SetChannel<0>(maxVal, std::max(GetChannel<0>(maxVal), GetChannel<0>(val)));
92+
SetChannel<1>(maxVal, std::max(GetChannel<1>(maxVal), GetChannel<1>(val)));
93+
SetChannel<2>(maxVal, std::max(GetChannel<2>(maxVal), GetChannel<2>(val)));
94+
SetChannel<3>(maxVal, std::max(GetChannel<3>(maxVal), GetChannel<3>(val)));
95+
}
96+
src_bits += src_pitch;
97+
}
98+
99+
if (out_min_value) {
100+
*static_cast<PixelType_*>(out_min_value) = minVal;
101+
}
102+
if (out_max_value) {
103+
*static_cast<PixelType_*>(out_max_value) = maxVal;
104+
}
105+
}
106+
107+
} // unnamed namespace
11108

12109
FIBOOL DLL_CALLCONV
13110
FreeImage_FindMinMax(FIBITMAP* dib, double* min_brightness, double* max_brightness, void** min_ptr, void** max_ptr)
@@ -134,47 +231,6 @@ FreeImage_FindMinMax(FIBITMAP* dib, double* min_brightness, double* max_brightne
134231
return success ? TRUE : FALSE;
135232
}
136233

137-
namespace
138-
{
139-
140-
template <typename PixelType_>
141-
void FindMinMaxValueImpl(FIBITMAP* src, void* out_min_value, void* out_max_value)
142-
{
143-
PixelType_ minVal, maxVal;
144-
PixelFill(minVal, std::numeric_limits<ToValueType<PixelType_>>::max());
145-
PixelFill(maxVal, std::numeric_limits<ToValueType<PixelType_>>::lowest());
146-
147-
const unsigned width = FreeImage_GetWidth(src);
148-
const unsigned height = FreeImage_GetHeight(src);
149-
const unsigned src_pitch = FreeImage_GetPitch(src);
150-
151-
uint8_t* src_bits = FreeImage_GetBits(src);
152-
for (unsigned y = 0; y < height; ++y) {
153-
auto src_pixel = static_cast<PixelType_*>(static_cast<void*>(src_bits));
154-
for (unsigned x = 0; x < width; ++x) {
155-
const PixelType_ val = src_pixel[x];
156-
SetChannel<0>(minVal, std::min(GetChannel<0>(minVal), GetChannel<0>(val)));
157-
SetChannel<1>(minVal, std::min(GetChannel<1>(minVal), GetChannel<1>(val)));
158-
SetChannel<2>(minVal, std::min(GetChannel<2>(minVal), GetChannel<2>(val)));
159-
SetChannel<3>(minVal, std::min(GetChannel<3>(minVal), GetChannel<3>(val)));
160-
SetChannel<0>(maxVal, std::max(GetChannel<0>(maxVal), GetChannel<0>(val)));
161-
SetChannel<1>(maxVal, std::max(GetChannel<1>(maxVal), GetChannel<1>(val)));
162-
SetChannel<2>(maxVal, std::max(GetChannel<2>(maxVal), GetChannel<2>(val)));
163-
SetChannel<3>(maxVal, std::max(GetChannel<3>(maxVal), GetChannel<3>(val)));
164-
}
165-
src_bits += src_pitch;
166-
}
167-
168-
if (out_min_value) {
169-
*static_cast<PixelType_*>(out_min_value) = minVal;
170-
}
171-
if (out_max_value) {
172-
*static_cast<PixelType_*>(out_max_value) = maxVal;
173-
}
174-
}
175-
176-
} // namespace
177-
178234
FIBOOL DLL_CALLCONV
179235
FreeImage_FindMinMaxValue(FIBITMAP* dib, void* min_value, void* max_value)
180236
{

Source/FreeImage/SimpleTools.h

Lines changed: 0 additions & 168 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ void BitmapTransform(FIBITMAP* dst, FIBITMAP* src, UnaryOperation_ unary_op)
5555
}
5656
}
5757

58-
5958
template <typename Ty_>
6059
using IsIntPixelType = std::integral_constant<bool,
6160
std::is_same_v<Ty_, FIRGB8> ||
@@ -102,46 +101,6 @@ namespace details
102101
template <typename PixelType_>
103102
using ToValueType = typename details::ToValueTypeImpl<PixelType_>::type;
104103

105-
106-
namespace details
107-
{
108-
template <typename Ty_>
109-
struct ToWiderTypeImpl {};
110-
111-
template <> struct ToWiderTypeImpl<uint64_t> { using type = uint64_t; };
112-
template <> struct ToWiderTypeImpl<int64_t> { using type = int64_t; };
113-
template <> struct ToWiderTypeImpl<uint32_t> { using type = uint64_t; };
114-
template <> struct ToWiderTypeImpl<int32_t> { using type = int64_t; };
115-
template <> struct ToWiderTypeImpl<uint16_t> { using type = uint32_t; };
116-
template <> struct ToWiderTypeImpl<int16_t> { using type = int32_t; };
117-
template <> struct ToWiderTypeImpl<uint8_t> { using type = uint32_t; };
118-
template <> struct ToWiderTypeImpl<int8_t> { using type = int32_t; };
119-
}
120-
121-
template <typename Ty_>
122-
using ToWiderType = typename details::ToWiderTypeImpl<Ty_>::type;
123-
124-
125-
namespace details
126-
{
127-
template <typename Ty_>
128-
struct ToUnsignedTypeImpl {};
129-
130-
template <> struct ToUnsignedTypeImpl<uint64_t> { using type = uint64_t; };
131-
template <> struct ToUnsignedTypeImpl<int64_t> { using type = uint64_t; };
132-
template <> struct ToUnsignedTypeImpl<uint32_t> { using type = uint32_t; };
133-
template <> struct ToUnsignedTypeImpl<int32_t> { using type = uint32_t; };
134-
template <> struct ToUnsignedTypeImpl<uint16_t> { using type = uint16_t; };
135-
template <> struct ToUnsignedTypeImpl<int16_t> { using type = uint16_t; };
136-
template <> struct ToUnsignedTypeImpl<uint8_t> { using type = uint8_t; };
137-
template <> struct ToUnsignedTypeImpl<int8_t> { using type = uint8_t; };
138-
}
139-
140-
template <typename Ty_>
141-
using ToUnsignedType = typename details::ToUnsignedTypeImpl<Ty_>::type;
142-
143-
144-
145104
template <uint32_t Value_>
146105
using uint32_constant = std::integral_constant<uint32_t, Value_>;
147106

@@ -159,26 +118,6 @@ template <> struct PixelChannelsNumber<FIRGB8> : public uint32_constant<3> {};
159118
template <> struct PixelChannelsNumber<FICOMPLEX> : public uint32_constant<2> {};
160119
template <> struct PixelChannelsNumber<FICOMPLEXF> : public uint32_constant<2> {};
161120

162-
namespace details
163-
{
164-
template <typename PixelType_>
165-
struct ToNoAlphaTypeImpl
166-
{
167-
using type = PixelType_;
168-
};
169-
170-
template <> struct ToNoAlphaTypeImpl<FIRGBAF> { using type = FIRGBF; };
171-
template <> struct ToNoAlphaTypeImpl<FIRGBA32> { using type = FIRGB32; };
172-
template <> struct ToNoAlphaTypeImpl<FIRGBA16> { using type = FIRGB16; };
173-
template <> struct ToNoAlphaTypeImpl<FIRGBA8> { using type = FIRGB8; };
174-
175-
}
176-
177-
template <typename PixelType_>
178-
using ToNoAlphaType = typename details::ToNoAlphaTypeImpl<PixelType_>::type;
179-
180-
181-
182121
template <uint32_t ChannelIndex_, typename PixelType_>
183122
inline constexpr
184123
void SetChannel(PixelType_& p, ToValueType<PixelType_> v)
@@ -200,61 +139,6 @@ ToValueType<PixelType_> GetChannel(const PixelType_& p, ToValueType<PixelType_>
200139
}
201140
}
202141

203-
template <typename PixelType_>
204-
inline constexpr
205-
void PixelFill(PixelType_& p, const ToValueType<PixelType_>& v)
206-
{
207-
SetChannel<0>(p, v);
208-
SetChannel<1>(p, v);
209-
SetChannel<2>(p, v);
210-
SetChannel<3>(p, v);
211-
}
212-
213-
template <typename PixelType_, typename BinaryOperation_>
214-
inline constexpr
215-
auto PixelReduce(const PixelType_& p, ToValueType<PixelType_> init, BinaryOperation_&& op)
216-
{
217-
constexpr uint32_t channelsNumber = PixelChannelsNumber<PixelType_>::value;
218-
if constexpr (channelsNumber > 0) {
219-
init = op(std::move(init), GetChannel<0>(p));
220-
}
221-
if constexpr (channelsNumber > 1) {
222-
init = op(std::move(init), GetChannel<1>(p));
223-
}
224-
if constexpr (channelsNumber > 2) {
225-
init = op(std::move(init), GetChannel<2>(p));
226-
}
227-
if constexpr (channelsNumber > 3) {
228-
init = op(std::move(init), GetChannel<3>(p));
229-
}
230-
return init;
231-
}
232-
233-
template <typename PixelType_>
234-
inline constexpr
235-
auto PixelMin(const PixelType_& p, ToValueType<PixelType_> init = std::numeric_limits<ToValueType<PixelType_>>::max())
236-
{
237-
return PixelReduce(p, init, [](const auto& lhs, const auto& rhs) { return std::min(lhs, rhs); });
238-
}
239-
240-
template <typename PixelType_>
241-
inline constexpr
242-
auto PixelMax(const PixelType_& p, ToValueType<PixelType_> init = std::numeric_limits<ToValueType<PixelType_>>::lowest())
243-
{
244-
return PixelReduce(p, init, [](const auto& lhs, const auto& rhs) { return std::max(lhs, rhs); });
245-
}
246-
247-
template <typename PixelType_>
248-
inline constexpr
249-
auto StripAlpha(PixelType_&& p)
250-
{
251-
return ToNoAlphaType<PixelType_>(std::forward<PixelType_>(p));
252-
}
253-
254-
255-
256-
257-
258142
inline
259143
bool IsNan(float p) {
260144
return std::isnan(p);
@@ -283,8 +167,6 @@ std::enable_if_t<std::is_integral_v<Ty_> || IsIntPixelType<Ty_>::value, bool> Is
283167
return false;
284168
}
285169

286-
287-
288170
struct Brightness
289171
{
290172
template <typename Ty_>
@@ -302,54 +184,4 @@ struct Brightness
302184
}
303185
};
304186

305-
struct YuvBrightness
306-
{
307-
template <typename Ty_>
308-
inline
309-
auto operator()(const Ty_& p, std::enable_if_t<IsPixelType<Ty_>::value, void*> = nullptr)
310-
{
311-
return p.red;
312-
}
313-
};
314-
315-
316-
template <typename PixelTy_, typename BrightnessOp_ = Brightness>
317-
std::tuple<PixelTy_*, PixelTy_*, double, double> FindMinMax(FIBITMAP* src, BrightnessOp_ brightnessOp = BrightnessOp_{})
318-
{
319-
PixelTy_* minIt{}, * maxIt{};
320-
double minVal = 0.0, maxVal = 0.0;
321-
if (src) {
322-
const unsigned h = FreeImage_GetHeight(src);
323-
const unsigned w = FreeImage_GetWidth(src);
324-
const unsigned pitch = FreeImage_GetPitch(src);
325-
auto srcLine = static_cast<uint8_t*>(static_cast<void*>(FreeImage_GetBits(src)));
326-
for (unsigned j = 0; j < h; ++j, srcLine += pitch) {
327-
auto pixIt = static_cast<PixelTy_*>(static_cast<void*>(srcLine));
328-
for (unsigned i = 0; i < w; ++i, ++pixIt) {
329-
if (IsNan(*pixIt)) {
330-
continue;
331-
}
332-
const auto b = static_cast<double>(brightnessOp(*pixIt));
333-
if (!minIt || !maxIt) {
334-
minIt = maxIt = pixIt;
335-
minVal = maxVal = b;
336-
}
337-
else {
338-
if (b < minVal) {
339-
minIt = pixIt;
340-
minVal = b;
341-
}
342-
if (maxVal < b) {
343-
maxIt = pixIt;
344-
maxVal = b;
345-
}
346-
}
347-
}
348-
}
349-
}
350-
return std::make_tuple(minIt, maxIt, minVal, maxVal);
351-
}
352-
353-
354-
355187
#endif //FREEIMAGE_SIMPLE_TOOLS_H_

Source/FreeImage/tmoColorConvert.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#include "FreeImage.h"
2424
#include "Utilities.h"
2525
#include "ToneMapping.h"
26-
#include <algorithm>
2726

2827
// ----------------------------------------------------------
2928
// Convert RGB to and from Yxy, same as in Reinhard et al. SIGGRAPH 2002

Source/FreeImage/tmoReinhard05.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#include "FreeImage.h"
2424
#include "Utilities.h"
2525
#include "ToneMapping.h"
26-
#include <algorithm>
2726

2827
// ----------------------------------------------------------
2928
// Global and/or local tone mapping operator

0 commit comments

Comments
 (0)