Skip to content

Commit fd2891a

Browse files
authored
Merge pull request #74 from lordnn/master
More C++20. Cleanup.
2 parents cd43b5f + 51f48a6 commit fd2891a

36 files changed

Lines changed: 312 additions & 325 deletions

Source/FreeImage/BitmapAccess.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1023,7 +1023,7 @@ FreeImage_GetTransparencyCount(FIBITMAP *dib) {
10231023
void DLL_CALLCONV
10241024
FreeImage_SetTransparencyTable(FIBITMAP *dib, uint8_t *table, int count) {
10251025
if (dib) {
1026-
count = MAX(0, MIN(count, 256));
1026+
count = std::clamp(count, 0, 256);
10271027
if (FreeImage_GetBPP(dib) <= 8) {
10281028
((FREEIMAGEHEADER *)dib->data)->transparent = (count > 0) ? TRUE : FALSE;
10291029
((FREEIMAGEHEADER *)dib->data)->transparency_count = count;

Source/FreeImage/Conversion.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ SwapRedBlue32(FIBITMAP* dib) {
8080
uint8_t* line = FreeImage_GetBits(dib);
8181
for (unsigned y = 0; y < height; ++y, line += pitch) {
8282
for (uint8_t* pixel = line; pixel < line + lineSize ; pixel += bytesperpixel) {
83-
INPLACESWAP(pixel[0], pixel[2]);
83+
std::swap(pixel[0], pixel[2]);
8484
}
8585
}
8686

@@ -123,9 +123,9 @@ CMYKToRGB(T C, T M, T Y, T K, T* out) {
123123
unsigned b = (max_val - Y) * (max_val - K) / max_val;
124124

125125
// clamp values to [0..max_val]
126-
T red = (T)CLAMP(r, (unsigned)0, max_val);
127-
T green = (T)CLAMP(g, (unsigned)0, max_val);
128-
T blue = (T)CLAMP(b, (unsigned)0, max_val);
126+
T red = (T)std::clamp(r, (unsigned)0, max_val);
127+
T green = (T)std::clamp(g, (unsigned)0, max_val);
128+
T blue = (T)std::clamp(b, (unsigned)0, max_val);
129129

130130
assignRGB(red, green, blue, out);
131131
}
@@ -276,9 +276,9 @@ CIELabToRGB(float L, float a, float b, T *rgb) {
276276
XYZToRGB(X, Y, Z, &R, &G, &B);
277277

278278
// clamp values to [0..max_val]
279-
T red = (T)CLAMP(R * max_val, 0.0F, max_val);
280-
T green = (T)CLAMP(G * max_val, 0.0F, max_val);
281-
T blue = (T)CLAMP(B * max_val, 0.0F, max_val);
279+
T red = (T)std::clamp(R * max_val, 0.F, max_val);
280+
T green = (T)std::clamp(G * max_val, 0.F, max_val);
281+
T blue = (T)std::clamp(B * max_val, 0.F, max_val);
282282

283283
assignRGB(red, green, blue, rgb);
284284
}

Source/FreeImage/ConversionFloat.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ FreeImage_ConvertToFloat(FIBITMAP *dib, FIBOOL scale_linear) {
185185
case FIT_RGBF:
186186
if (scale_linear) {
187187
BitmapTransform<float, FIRGBF>(dst, src, [](const FIRGBF& p) {
188-
return CLAMP(LUMA_REC709(p.red, p.green, p.blue), 0.0F, 1.0F); });
188+
return std::clamp(LUMA_REC709(p.red, p.green, p.blue), 0.F, 1.F); });
189189
}
190190
else {
191191
BitmapTransform<float, FIRGBF>(dst, src, [](const FIRGBF& p) {
@@ -196,7 +196,7 @@ FreeImage_ConvertToFloat(FIBITMAP *dib, FIBOOL scale_linear) {
196196
case FIT_RGBAF:
197197
if (scale_linear) {
198198
BitmapTransform<float, FIRGBAF>(dst, src, [](const FIRGBAF& p) {
199-
return CLAMP(LUMA_REC709(p.red, p.green, p.blue), 0.0F, 1.0F); });
199+
return std::clamp(LUMA_REC709(p.red, p.green, p.blue), 0.F, 1.F); });
200200
}
201201
else {
202202
BitmapTransform<float, FIRGBAF>(dst, src, [](const FIRGBAF& p) {

Source/FreeImage/ConversionRGBAF.cpp

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,10 @@ FreeImage_ConvertToRGBAF(FIBITMAP *dib) {
121121
auto *dst_pixel = (FIRGBAF*)dst_bits;
122122
for (unsigned x = 0; x < width; x++) {
123123
// convert and scale to the range [0..1]
124-
dst_pixel->red = (float)(src_pixel[FI_RGBA_RED]) / 255.0F;
125-
dst_pixel->green = (float)(src_pixel[FI_RGBA_GREEN]) / 255.0F;
126-
dst_pixel->blue = (float)(src_pixel[FI_RGBA_BLUE]) / 255.0F;
127-
dst_pixel->alpha = (float)(src_pixel[FI_RGBA_ALPHA]) / 255.0F;
124+
dst_pixel->red = (float)(src_pixel[FI_RGBA_RED]) / 255.F;
125+
dst_pixel->green = (float)(src_pixel[FI_RGBA_GREEN]) / 255.F;
126+
dst_pixel->blue = (float)(src_pixel[FI_RGBA_BLUE]) / 255.F;
127+
dst_pixel->alpha = (float)(src_pixel[FI_RGBA_ALPHA]) / 255.F;
128128

129129
src_pixel += bytespp;
130130
dst_pixel++;
@@ -146,11 +146,11 @@ FreeImage_ConvertToRGBAF(FIBITMAP *dib) {
146146

147147
for (unsigned x = 0; x < width; x++) {
148148
// convert and scale to the range [0..1]
149-
const float dst_value = (float)src_pixel[x] / 65535.0F;
149+
const float dst_value = (float)src_pixel[x] / 65535.F;
150150
dst_pixel[x].red = dst_value;
151151
dst_pixel[x].green = dst_value;
152152
dst_pixel[x].blue = dst_value;
153-
dst_pixel[x].alpha = 1.0F;
153+
dst_pixel[x].alpha = 1.F;
154154
}
155155
src_bits += src_pitch;
156156
dst_bits += dst_pitch;
@@ -169,10 +169,10 @@ FreeImage_ConvertToRGBAF(FIBITMAP *dib) {
169169

170170
for (unsigned x = 0; x < width; x++) {
171171
// convert and scale to the range [0..1]
172-
dst_pixel[x].red = (float)(src_pixel[x].red) / 65535.0F;
173-
dst_pixel[x].green = (float)(src_pixel[x].green) / 65535.0F;
174-
dst_pixel[x].blue = (float)(src_pixel[x].blue) / 65535.0F;
175-
dst_pixel[x].alpha = 1.0F;
172+
dst_pixel[x].red = (float)(src_pixel[x].red) / 65535.F;
173+
dst_pixel[x].green = (float)(src_pixel[x].green) / 65535.F;
174+
dst_pixel[x].blue = (float)(src_pixel[x].blue) / 65535.F;
175+
dst_pixel[x].alpha = 1.F;
176176
}
177177
src_bits += src_pitch;
178178
dst_bits += dst_pitch;
@@ -191,10 +191,10 @@ FreeImage_ConvertToRGBAF(FIBITMAP *dib) {
191191

192192
for (unsigned x = 0; x < width; x++) {
193193
// convert and scale to the range [0..1]
194-
dst_pixel[x].red = (float)(src_pixel[x].red) / 65535.0F;
195-
dst_pixel[x].green = (float)(src_pixel[x].green) / 65535.0F;
196-
dst_pixel[x].blue = (float)(src_pixel[x].blue) / 65535.0F;
197-
dst_pixel[x].alpha = (float)(src_pixel[x].alpha) / 65535.0F;
194+
dst_pixel[x].red = (float)(src_pixel[x].red) / 65535.F;
195+
dst_pixel[x].green = (float)(src_pixel[x].green) / 65535.F;
196+
dst_pixel[x].blue = (float)(src_pixel[x].blue) / 65535.F;
197+
dst_pixel[x].alpha = (float)(src_pixel[x].alpha) / 65535.F;
198198
}
199199
src_bits += src_pitch;
200200
dst_bits += dst_pitch;
@@ -216,7 +216,7 @@ FreeImage_ConvertToRGBAF(FIBITMAP *dib) {
216216
dst_pixel[x].red = (float)(src_pixel[x].red / static_cast<double>(std::numeric_limits<uint32_t>::max()));
217217
dst_pixel[x].green = (float)(src_pixel[x].green / static_cast<double>(std::numeric_limits<uint32_t>::max()));
218218
dst_pixel[x].blue = (float)(src_pixel[x].blue / static_cast<double>(std::numeric_limits<uint32_t>::max()));
219-
dst_pixel[x].alpha = 1.0F;
219+
dst_pixel[x].alpha = 1.F;
220220
}
221221
src_bits += src_pitch;
222222
dst_bits += dst_pitch;
@@ -258,11 +258,11 @@ FreeImage_ConvertToRGBAF(FIBITMAP *dib) {
258258
for (unsigned x = 0; x < width; x++) {
259259
// convert by copying greyscale channel to each R, G, B channels
260260
// assume float values are in [0..1]
261-
const float value = CLAMP(src_pixel[x], 0.0F, 1.0F);
261+
const float value = std::clamp(src_pixel[x], 0.F, 1.F);
262262
dst_pixel[x].red = value;
263263
dst_pixel[x].green = value;
264264
dst_pixel[x].blue = value;
265-
dst_pixel[x].alpha = 1.0F;
265+
dst_pixel[x].alpha = 1.F;
266266
}
267267
src_bits += src_pitch;
268268
dst_bits += dst_pitch;
@@ -281,10 +281,10 @@ FreeImage_ConvertToRGBAF(FIBITMAP *dib) {
281281

282282
for (unsigned x = 0; x < width; x++) {
283283
// convert pixels directly, while adding a "dummy" alpha of 1.0
284-
dst_pixel[x].red = CLAMP(src_pixel[x].red, 0.0F, 1.0F);
285-
dst_pixel[x].green = CLAMP(src_pixel[x].green, 0.0F, 1.0F);
286-
dst_pixel[x].blue = CLAMP(src_pixel[x].blue, 0.0F, 1.0F);
287-
dst_pixel[x].alpha = 1.0F;
284+
dst_pixel[x].red = std::clamp(src_pixel[x].red, 0.F, 1.F);
285+
dst_pixel[x].green = std::clamp(src_pixel[x].green, 0.F, 1.F);
286+
dst_pixel[x].blue = std::clamp(src_pixel[x].blue, 0.F, 1.F);
287+
dst_pixel[x].alpha = 1.F;
288288
}
289289
src_bits += src_pitch;
290290
dst_bits += dst_pitch;

Source/FreeImage/ConversionRGBF.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,9 @@ FreeImage_ConvertToRGBF(FIBITMAP *dib) {
124124
auto *dst_pixel = (FIRGBF*)dst_bits;
125125
for (unsigned x = 0; x < width; x++) {
126126
// convert and scale to the range [0..1]
127-
dst_pixel->red = (float)(src_pixel[FI_RGBA_RED]) / 255.0F;
128-
dst_pixel->green = (float)(src_pixel[FI_RGBA_GREEN]) / 255.0F;
129-
dst_pixel->blue = (float)(src_pixel[FI_RGBA_BLUE]) / 255.0F;
127+
dst_pixel->red = (float)(src_pixel[FI_RGBA_RED]) / 255.F;
128+
dst_pixel->green = (float)(src_pixel[FI_RGBA_GREEN]) / 255.F;
129+
dst_pixel->blue = (float)(src_pixel[FI_RGBA_BLUE]) / 255.F;
130130

131131
src_pixel += bytespp;
132132
dst_pixel ++;
@@ -148,7 +148,7 @@ FreeImage_ConvertToRGBF(FIBITMAP *dib) {
148148

149149
for (unsigned x = 0; x < width; x++) {
150150
// convert and scale to the range [0..1]
151-
const float dst_value = (float)src_pixel[x] / 65535.0F;
151+
const float dst_value = (float)src_pixel[x] / 65535.F;
152152
dst_pixel[x].red = dst_value;
153153
dst_pixel[x].green = dst_value;
154154
dst_pixel[x].blue = dst_value;
@@ -170,9 +170,9 @@ FreeImage_ConvertToRGBF(FIBITMAP *dib) {
170170

171171
for (unsigned x = 0; x < width; x++) {
172172
// convert and scale to the range [0..1]
173-
dst_pixel[x].red = (float)(src_pixel[x].red) / 65535.0F;
174-
dst_pixel[x].green = (float)(src_pixel[x].green) / 65535.0F;
175-
dst_pixel[x].blue = (float)(src_pixel[x].blue) / 65535.0F;
173+
dst_pixel[x].red = (float)(src_pixel[x].red) / 65535.F;
174+
dst_pixel[x].green = (float)(src_pixel[x].green) / 65535.F;
175+
dst_pixel[x].blue = (float)(src_pixel[x].blue) / 65535.F;
176176
}
177177
src_bits += src_pitch;
178178
dst_bits += dst_pitch;
@@ -191,9 +191,9 @@ FreeImage_ConvertToRGBF(FIBITMAP *dib) {
191191

192192
for (unsigned x = 0; x < width; x++) {
193193
// convert and scale to the range [0..1]
194-
dst_pixel[x].red = (float)(src_pixel[x].red) / 65535.0F;
195-
dst_pixel[x].green = (float)(src_pixel[x].green) / 65535.0F;
196-
dst_pixel[x].blue = (float)(src_pixel[x].blue) / 65535.0F;
194+
dst_pixel[x].red = (float)(src_pixel[x].red) / 65535.F;
195+
dst_pixel[x].green = (float)(src_pixel[x].green) / 65535.F;
196+
dst_pixel[x].blue = (float)(src_pixel[x].blue) / 65535.F;
197197
}
198198
src_bits += src_pitch;
199199
dst_bits += dst_pitch;
@@ -255,7 +255,7 @@ FreeImage_ConvertToRGBF(FIBITMAP *dib) {
255255
for (unsigned x = 0; x < width; x++) {
256256
// convert by copying greyscale channel to each R, G, B channels
257257
// assume float values are in [0..1]
258-
const float value = CLAMP(src_pixel[x], 0.0F, 1.0F);
258+
const float value = std::clamp(src_pixel[x], 0.F, 1.F);
259259
dst_pixel[x].red = value;
260260
dst_pixel[x].green = value;
261261
dst_pixel[x].blue = value;
@@ -278,7 +278,7 @@ FreeImage_ConvertToRGBF(FIBITMAP *dib) {
278278
for (unsigned x = 0; x < width; x++) {
279279
// convert by copying greyscale channel to each R, G, B channels
280280
// assume float values are in [0..1]
281-
const float value = static_cast<float>(CLAMP(src_pixel[x], 0.0, 1.0));
281+
const float value = static_cast<float>(std::clamp(src_pixel[x], 0.0, 1.0));
282282
dst_pixel[x].red = value;
283283
dst_pixel[x].green = value;
284284
dst_pixel[x].blue = value;
@@ -300,9 +300,9 @@ FreeImage_ConvertToRGBF(FIBITMAP *dib) {
300300

301301
for (unsigned x = 0; x < width; x++) {
302302
// convert and skip alpha channel
303-
dst_pixel[x].red = CLAMP(src_pixel[x].red, 0.0F, 1.0F);
304-
dst_pixel[x].green = CLAMP(src_pixel[x].green, 0.0F, 1.0F);
305-
dst_pixel[x].blue = CLAMP(src_pixel[x].blue, 0.0F, 1.0F);
303+
dst_pixel[x].red = std::clamp(src_pixel[x].red, 0.F, 1.F);
304+
dst_pixel[x].green = std::clamp(src_pixel[x].green, 0.F, 1.F);
305+
dst_pixel[x].blue = std::clamp(src_pixel[x].blue, 0.F, 1.F);
306306
}
307307
src_bits += src_pitch;
308308
dst_bits += dst_pitch;

Source/FreeImage/ConversionType.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ CONVERT_TYPE<Tdst, Tsrc>::convert(FIBITMAP *src, FREE_IMAGE_TYPE dst_type) {
6767

6868
/** Convert a greyscale image of type Tsrc to a 8-bit grayscale dib.
6969
Conversion is done using either a linear scaling from [min, max] to [0, 255]
70-
or a rounding from src_pixel to (uint8_t) MIN(255, MAX(0, q)) where int q = int(src_pixel + 0.5);
70+
or a rounding from src_pixel to (uint8_t) clamp(q, 0, 255) where int q = int(src_pixel + 0.5);
7171
*/
7272
template<class Tsrc>
7373
class CONVERT_TO_BYTE
@@ -134,7 +134,7 @@ CONVERT_TO_BYTE<Tsrc>::convert(FIBITMAP *src, FIBOOL scale_linear) {
134134
for (x = 0; x < width; x++) {
135135
// rounding
136136
int q = int(src_bits[x] + 0.5);
137-
dst_bits[x] = (uint8_t) MIN(255, MAX(0, q));
137+
dst_bits[x] = (uint8_t) std::clamp(q, 0, 255);
138138
}
139139
}
140140
}

Source/FreeImage/MNGHelper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1206,7 +1206,7 @@ mng_WriteJNG(int format_id, FreeImageIO *io, FIBITMAP *dib, fi_handle handle, in
12061206
// write chunks
12071207
for (uint32_t k = 0; k < size_in_bytes;) {
12081208
uint32_t bytes_left = size_in_bytes - k;
1209-
uint32_t chunk_size = MIN(JPEG_CHUNK_SIZE, bytes_left);
1209+
uint32_t chunk_size = std::min(JPEG_CHUNK_SIZE, bytes_left);
12101210
mng_WriteChunk(mng_JDAT, &jpeg_data[k], chunk_size, hJngMemory);
12111211
k += chunk_size;
12121212
}

Source/FreeImage/Plugin.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#else
3535
#include <ctype.h>
3636
#endif // _WIN32
37+
#include <format>
3738

3839
#include <filesystem>
3940

@@ -787,9 +788,7 @@ namespace {
787788

788789
std::filesystem::path MakeRandomSuffix()
789790
{
790-
std::stringstream strs{};
791-
strs << ".fitmp" << std::hex << static_cast<uint32_t>(std::rand());
792-
return strs.str();
791+
return std::format(".fitmp{:x}", static_cast<uint32_t>(std::rand()));
793792
}
794793

795794

Source/FreeImage/tmoColorConvert.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,9 @@ LuminanceFromYxy(FIBITMAP *Yxy, float *maxLum, float *minLum, float *worldLum) {
243243
for (unsigned y = 0; y < height; y++) {
244244
auto *pixel = (const FIRGBF*)bits;
245245
for (unsigned x = 0; x < width; x++) {
246-
const float Y = MAX(0.0F, pixel[x].red);// avoid negative values
247-
max_lum = (max_lum < Y) ? Y : max_lum; // max Luminance in the scene
248-
min_lum = (min_lum < Y) ? min_lum : Y; // min Luminance in the scene
246+
const float Y = std::max(0.F, pixel[x].red);// avoid negative values
247+
max_lum = std::max(max_lum, Y); // max Luminance in the scene
248+
min_lum = std::min(min_lum, Y); // min Luminance in the scene
249249
sum += log(2.3e-5F + Y); // contrast constant in Tumblin paper
250250
}
251251
// next line
@@ -292,9 +292,9 @@ ClampConvertRGBFTo24(FIBITMAP *src) {
292292
const float green = (src_pixel[x].green > 1) ? 1 : src_pixel[x].green;
293293
const float blue = (src_pixel[x].blue > 1) ? 1 : src_pixel[x].blue;
294294

295-
dst_pixel[FI_RGBA_RED] = (uint8_t)(255.0F * red + 0.5F);
296-
dst_pixel[FI_RGBA_GREEN] = (uint8_t)(255.0F * green + 0.5F);
297-
dst_pixel[FI_RGBA_BLUE] = (uint8_t)(255.0F * blue + 0.5F);
295+
dst_pixel[FI_RGBA_RED] = (uint8_t)(255.F * red + 0.5F);
296+
dst_pixel[FI_RGBA_GREEN] = (uint8_t)(255.F * green + 0.5F);
297+
dst_pixel[FI_RGBA_BLUE] = (uint8_t)(255.F * blue + 0.5F);
298298
dst_pixel += 3;
299299
}
300300
src_bits += src_pitch;

Source/FreeImage/tmoFattal02.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ static FIBITMAP* tmoFattal02(FIBITMAP *Y, float alpha, float beta) {
513513
// get the number of levels for the pyramid
514514
const unsigned width = FreeImage_GetWidth(H);
515515
const unsigned height = FreeImage_GetHeight(H);
516-
unsigned minsize = MIN(width, height);
516+
unsigned minsize = std::min(width, height);
517517
while (minsize >= MIN_PYRAMID_SIZE) {
518518
nlevels++;
519519
minsize /= 2;
@@ -607,8 +607,8 @@ Apply the Gradient Domain High Dynamic Range Compression to a RGBF image and con
607607
FIBITMAP* DLL_CALLCONV
608608
FreeImage_TmoFattal02(FIBITMAP *dib, double color_saturation, double attenuation) {
609609
const float alpha = 0.1F; // parameter alpha = 0.1
610-
const float beta = (float)MAX(0.8, MIN(0.9, attenuation)); // parameter beta = [0.8..0.9]
611-
const float s = (float)MAX(0.4, MIN(0.6, color_saturation));// exponent s controls color saturation = [0.4..0.6]
610+
const float beta = (float)std::clamp(attenuation, 0.8, 0.9); // parameter beta = [0.8..0.9]
611+
const float s = (float)std::clamp(color_saturation, 0.4, 0.6);// exponent s controls color saturation = [0.4..0.6]
612612

613613
FIBITMAP *src{};
614614
FIBITMAP *Yin{};

0 commit comments

Comments
 (0)