@@ -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 */
612578void 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
0 commit comments