@@ -4,8 +4,9 @@ use crate::hist::{HistogramInternal, Histogram};
44use crate :: image:: Image ;
55use crate :: kmeans:: Kmeans ;
66use crate :: mediancut:: mediancut;
7- use crate :: pal:: { PalF , PalIndexRemap , PalLen , PalPop , Palette , LIQ_WEIGHT_MSE , MAX_COLORS , MAX_TRANSP_A , RGBA } ;
8- use crate :: remap:: { mse_to_standard_mse, remap_to_palette, remap_to_palette_floyd, DitherMapMode , Remapped } ;
7+ use crate :: pal:: { PalF , PalIndexRemap , PalLen , PalPop , Palette , MAX_COLORS , RGBA } ;
8+ use crate :: pal:: { internal_mse_to_standard_mse, unit_mse_to_internal_mse} ;
9+ use crate :: remap:: { remap_to_palette, remap_to_palette_floyd, DitherMapMode , Remapped } ;
910use crate :: seacow:: RowBitmapMut ;
1011use crate :: OrdFloat ;
1112use arrayvec:: ArrayVec ;
@@ -42,9 +43,9 @@ impl QuantizationResult {
4243 if palette_error > max_mse {
4344 attr. verbose_print ( format ! (
4445 " image degradation MSE={:0.3} (Q={}) exceeded limit of {:0.3} ({})" ,
45- mse_to_standard_mse ( palette_error) ,
46+ internal_mse_to_standard_mse ( palette_error) ,
4647 mse_to_quality( palette_error) ,
47- mse_to_standard_mse ( max_mse) ,
48+ internal_mse_to_standard_mse ( max_mse) ,
4849 mse_to_quality( max_mse)
4950 ) ) ;
5051 return Err ( QualityTooLow ) ;
@@ -171,7 +172,7 @@ impl QuantizationResult {
171172 /// Approximate mean square error of the palette
172173 #[ must_use]
173174 pub fn quantization_error ( & self ) -> Option < f64 > {
174- self . palette_error . map ( mse_to_standard_mse )
175+ self . palette_error . map ( internal_mse_to_standard_mse )
175176 }
176177
177178 /// Approximate mean square error of the palette used for the most recent remapping
@@ -180,7 +181,7 @@ impl QuantizationResult {
180181 self . remapped . as_ref ( )
181182 . and_then ( |re| re. palette_error )
182183 . or ( self . palette_error )
183- . map ( mse_to_standard_mse )
184+ . map ( internal_mse_to_standard_mse )
184185 }
185186
186187 /// Palette remapping error mapped back to 0-100 scale, same as the scale in [`Attributes::set_quality()`]
@@ -333,8 +334,8 @@ fn sort_palette(attr: &Attributes, palette: &mut PalF) {
333334
334335 let mut tmp: ArrayVec < _ , { MAX_COLORS } > = palette. iter_mut ( ) . map ( |( c, p) | ( * c, * p) ) . collect ( ) ;
335336 tmp. sort_by_key ( |( color, pop) | {
336- let is_transparent = color. a <= MAX_TRANSP_A ;
337- ( is_transparent == last_index_transparent, Reverse ( OrdFloat :: new ( pop. popularity ( ) ) ) )
337+ let trns = ! color. is_fully_opaque ( ) ;
338+ ( trns == last_index_transparent, Reverse ( OrdFloat :: new ( pop. popularity ( ) ) ) )
338339 } ) ;
339340 palette. iter_mut ( ) . zip ( tmp) . for_each ( |( ( dcol, dpop) , ( scol, spop) ) | {
340341 * dcol = scol;
@@ -343,18 +344,17 @@ fn sort_palette(attr: &Attributes, palette: &mut PalF) {
343344
344345 if last_index_transparent {
345346 let alpha_index = palette. as_slice ( ) . iter ( ) . enumerate ( )
346- . filter ( |( _, c) | c . a <= MAX_TRANSP_A )
347+ . filter ( |( _, c) | !c . is_fully_opaque ( ) )
347348 . min_by_key ( |( _, c) | OrdFloat :: new ( c. a ) )
348349 . map ( |( i, _) | i) ;
349350 if let Some ( alpha_index) = alpha_index {
350351 let last_index = palette. as_slice ( ) . len ( ) - 1 ;
351352 palette. swap ( last_index, alpha_index) ;
352353 }
353354 } else {
354- let num_transparent = palette. as_slice ( ) . iter ( ) . enumerate ( )
355- . filter ( |( _, c) | c. a <= MAX_TRANSP_A )
356- . map ( |( i, _) | i + 1 ) // num entries, not index
357- . max ( ) ;
355+ let num_transparent = palette. as_slice ( ) . iter ( ) . enumerate ( ) . rev ( )
356+ . find ( |( _, c) | !c. is_fully_opaque ( ) )
357+ . map ( |( i, _) | i + 1 ) ; // num entries, not index
358358 if let Some ( num_transparent) = num_transparent {
359359 attr. verbose_print ( format ! ( " eliminated opaque tRNS-chunk entries...{} entr{} transparent" , num_transparent, if num_transparent == 1 { "y" } else { "ies" } ) ) ;
360360 }
@@ -469,7 +469,7 @@ pub(crate) fn quality_to_mse(quality: u8) -> f64 {
469469 }
470470 if quality >= 100 { return 0. ; }
471471 let extra_low_quality_fudge = ( 0.016 / ( 0.001 + f64:: from ( quality) ) - 0.001 ) . max ( 0. ) ;
472- LIQ_WEIGHT_MSE * ( extra_low_quality_fudge + 2.5 / ( 210. + f64:: from ( quality) ) . powf ( 1.2 ) * ( 100.1 - f64:: from ( quality) ) / 100. )
472+ unit_mse_to_internal_mse ( extra_low_quality_fudge + 2.5 / ( 210. + f64:: from ( quality) ) . powf ( 1.2 ) * ( 100.1 - f64:: from ( quality) ) / 100. )
473473}
474474
475475pub ( crate ) fn mse_to_quality ( mse : f64 ) -> u8 {
0 commit comments