Skip to content

Commit 8f36c61

Browse files
committed
CLI supports writing fstop exposure LDR images
1 parent baa27b5 commit 8f36c61

6 files changed

Lines changed: 359 additions & 120 deletions

File tree

‎Docs/ChangeLog-6x.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ header from the same release to avoid compatibility issues.
2929
using the specified error color for the format in use for the bad blocks.
3030
The command line tool will write the image, emit a warning, and exit with
3131
code 2 for these failures. Functional errors continue to use exit code 1.
32+
* **CLI Change:** Writing to an LDR output file format, such as PNG, when
33+
testing HDR input is now supported. The tool will output multiple files,
34+
one for each active integer f-stop exposure level.
3235
* **Feature:** Add AVX-512 SIMD backends to library, with corresponding
3336
command line tool variants. Implementation provides "AVX-256", which uses
3437
an enhanced version of the AVX2 backend operating on 256-bit vectors, and

‎Source/astcenccli_error_metrics.cpp‎

Lines changed: 17 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -73,45 +73,18 @@ static void accumulate_hdr_luminance(
7373
}
7474
}
7575

76-
/**
77-
* @brief Select an HDR metric exposure using the original image's geometric mean luminance.
78-
*
79-
* @param img The original image, including any crop view.
80-
*
81-
* @return The nearest whole fstop mapping the geometric mean to 18% grey, clamped to [-123, 123],
82-
* or zero for black images.
83-
*/
84-
static int compute_hdr_exposure(
76+
/* See header for documentation. */
77+
int compute_hdr_exposure(
8578
const astcenc_image* img
8679
) {
80+
assert((img->data_type == ASTCENC_TYPE_F16) || (img->data_type == ASTCENC_TYPE_F32));
81+
8782
// Average log luminance in a fixed order; exclude black texels and alpha
8883
double log_luminance_sum = 0.0;
8984
size_t luminance_count = 0;
9085

9186
// Select the source format outside the loops and respect cropped row strides
92-
if (img->data_type == ASTCENC_TYPE_U8)
93-
{
94-
size_t stride = img->row_stride;
95-
if (stride == 0)
96-
{
97-
stride = img->dim_x * 4;
98-
}
99-
100-
for (size_t z = 0; z < img->dim_z; z++)
101-
{
102-
const uint8_t* data = static_cast<const uint8_t*>(img->data[z]);
103-
for (size_t y = 0; y < img->dim_y; y++)
104-
{
105-
for (size_t x = 0; x < img->dim_x; x++)
106-
{
107-
const uint8_t* texel = data + y * stride + 4 * x;
108-
vfloat4 color = vfloat4(texel[0], texel[1], texel[2], texel[3]) / 255.0f;
109-
accumulate_hdr_luminance(color, log_luminance_sum, luminance_count);
110-
}
111-
}
112-
}
113-
}
114-
else if (img->data_type == ASTCENC_TYPE_F16)
87+
if (img->data_type == ASTCENC_TYPE_F16)
11588
{
11689
size_t stride = img->row_stride / sizeof(uint16_t);
11790
if (stride == 0)
@@ -136,7 +109,6 @@ static int compute_hdr_exposure(
136109
}
137110
else
138111
{
139-
assert(img->data_type == ASTCENC_TYPE_F32);
140112
size_t stride = img->row_stride / sizeof(float);
141113
if (stride == 0)
142114
{
@@ -164,8 +136,9 @@ static int compute_hdr_exposure(
164136
return 0;
165137
}
166138

139+
// Clamp this with abs value two less than our limit to allow +-2 range
167140
double fstop = std::round(std::log2(0.18) - log_luminance_sum / luminance_count);
168-
return static_cast<int>(astc::clamp(fstop, -123.0, 123.0));
141+
return static_cast<int>(astc::clamp(fstop, -10.0, 10.0));
169142
}
170143

171144
/**
@@ -187,15 +160,8 @@ static error_accum4& operator+=(
187160
return val;
188161
}
189162

190-
/**
191-
* @brief mPSNR tone-mapping operator for HDR images.
192-
*
193-
* @param val The color value to tone map.
194-
* @param fstop The exposure fstop; should be in range [-125, 125].
195-
*
196-
* @return The mapped color value in [0.0f, 255.0f] range.
197-
*/
198-
static float mpsnr_operator(
163+
/* See header for documentation. */
164+
float tone_map_hdr(
199165
float val,
200166
int fstop
201167
) {
@@ -215,8 +181,8 @@ static float mpsnr_operator(
215181
*
216182
* @param val1 The first color value.
217183
* @param val2 The second color value.
218-
* @param fstop_lo The low exposure fstop; should be in range [-125, 125].
219-
* @param fstop_hi The high exposure fstop; should be in range [-125, 125].
184+
* @param fstop_lo The low exposure fstop, in range [-12, 12].
185+
* @param fstop_hi The high exposure fstop, in range [-12, 12].
220186
*
221187
* @return The summed squared difference across all active fstop levels.
222188
*/
@@ -229,8 +195,8 @@ static float mpsnr_sumdiff(
229195
float summa = 0.0f;
230196
for (int i = fstop_lo; i <= fstop_hi; i++)
231197
{
232-
float mval1 = mpsnr_operator(val1, i);
233-
float mval2 = mpsnr_operator(val2, i);
198+
float mval1 = tone_map_hdr(val1, i);
199+
float mval2 = tone_map_hdr(val2, i);
234200
float mdiff = mval1 - mval2;
235201
summa += mdiff * mdiff;
236202
}
@@ -279,7 +245,7 @@ static void load_ssim_row(
279245
for (size_t x = 0; x < width; x++)
280246
{
281247
float value = data[4 * x] / 255.0f;
282-
row[x + radius] = mpsnr_operator(value, fstop) / 255.0f - 0.5f;
248+
row[x + radius] = tone_map_hdr(value, fstop) / 255.0f - 0.5f;
283249
}
284250
}
285251
else
@@ -307,7 +273,7 @@ static void load_ssim_row(
307273
vint4 valuei(data[4 * x]);
308274
float value = float16_to_float(valuei).lane<0>();
309275
value = astc::clamp(value, 0.0f, 65504.0f);
310-
row[x + radius] = mpsnr_operator(value, fstop) / 255.0f - 0.5f;
276+
row[x + radius] = tone_map_hdr(value, fstop) / 255.0f - 0.5f;
311277
}
312278
}
313279
else
@@ -336,7 +302,7 @@ static void load_ssim_row(
336302
for (size_t x = 0; x < width; x++)
337303
{
338304
float value = astc::clamp(data[4 * x], 0.0f, 65504.0f);
339-
row[x + radius] = mpsnr_operator(value, fstop) / 255.0f - 0.5f;
305+
row[x + radius] = tone_map_hdr(value, fstop) / 255.0f - 0.5f;
340306
}
341307
}
342308
else
@@ -371,7 +337,7 @@ static void load_ssim_row(
371337
* @param img2 The compressed image.
372338
* @param component The color component index, in RGBA order.
373339
* @param is_hdr True to apply the mPSNR tone map before computing SSIM.
374-
* @param fstop The exposure fstop for HDR tone mapping, in range [-125, 125].
340+
* @param fstop The exposure fstop for HDR tone mapping, in range [-12, 12].
375341
*
376342
* @return The mean SSIM, where 1 indicates identical images.
377343
*/
@@ -611,22 +577,13 @@ static ssim_stats compute_ssim_stats(
611577
/* See header for documentation */
612578
void compute_error_metrics(
613579
bool is_hdr,
614-
bool is_hdr_fstop_auto,
615580
bool is_normal_map,
616581
int input_components,
617582
const astcenc_image* img1,
618583
const astcenc_image* img2,
619584
int fstop_lo,
620585
int fstop_hi
621586
) {
622-
// Use one reference-derived exposure range for both metrics and both images
623-
if (is_hdr && is_hdr_fstop_auto)
624-
{
625-
int fstop = compute_hdr_exposure(img1);
626-
fstop_lo = fstop - 2;
627-
fstop_hi = fstop + 2;
628-
}
629-
630587
static const int componentmasks[5] { 0x00, 0x07, 0x0C, 0x07, 0x0F };
631588
int component_mask = componentmasks[input_components];
632589

‎Source/astcenccli_internal.h‎

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -362,21 +362,42 @@ void astcenc_print_shorthelp();
362362
*/
363363
void astcenc_print_longhelp();
364364

365+
/**
366+
* @brief Select an HDR exposure using the original image's geometric mean luminance.
367+
*
368+
* @param img The FP16 or FP32 original image, including any crop view.
369+
*
370+
* @return The nearest whole fstop mapping the geometric mean to 18% grey, clamped to [-10, 10],
371+
* or zero for black images.
372+
*/
373+
int compute_hdr_exposure(
374+
const astcenc_image* img);
375+
376+
/**
377+
* @brief Tone map a nonnegative HDR color component for exposure previews and metrics.
378+
*
379+
* @param val The nonnegative color value to tone map.
380+
* @param fstop The exposure fstop, in range [-12, 12].
381+
*
382+
* @return The mapped color value in [0.0f, 255.0f] range.
383+
*/
384+
float tone_map_hdr(
385+
float val,
386+
int fstop);
387+
365388
/**
366389
* @brief Compute error metrics comparing two images.
367390
*
368-
* @param is_hdr @c true if HDR metrics should be computed.
369-
* @param is_hdr_fstop_auto @c true to auto-select HDR exposures from the original image.
370-
* @param is_normal_map @c true if normal map metrics should be computed.
371-
* @param input_components The number of input color components.
372-
* @param img1 The original image.
373-
* @param img2 The compressed image.
374-
* @param fstop_lo The low exposure fstop (HDR only).
375-
* @param fstop_hi The high exposure fstop (HDR only).
391+
* @param is_hdr @c true if HDR metrics should be computed.
392+
* @param is_normal_map @c true if normal map metrics should be computed.
393+
* @param input_components The number of input color components.
394+
* @param img1 The original image.
395+
* @param img2 The compressed image.
396+
* @param fstop_lo The low exposure fstop (HDR only).
397+
* @param fstop_hi The high exposure fstop (HDR only).
376398
*/
377399
void compute_error_metrics(
378400
bool is_hdr,
379-
bool is_hdr_fstop_auto,
380401
bool is_normal_map,
381402
int input_components,
382403
const astcenc_image* img1,

0 commit comments

Comments
 (0)