Skip to content

Commit e4271e1

Browse files
committed
cgifsave: pick the best quantizer instead of the first good enough
1 parent 9c22d74 commit e4271e1

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

libvips/foreign/cgifsave.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -337,19 +337,23 @@ vips_foreign_save_cgif_pick_quantiser( VipsForeignSaveCgif *cgif,
337337
const VipsQuantisePalette *prev = vips__quantise_get_palette(
338338
cgif->previous_quantisation_result );
339339

340+
int global_diff = vips__cgif_compare_palettes( this, global );
341+
int prev_diff = ( prev == global ) ? global_diff :
342+
vips__cgif_compare_palettes( this, prev );
343+
340344
#ifdef DEBUG_VERBOSE
341345
printf( "vips_foreign_save_cgif_write_frame: "
342346
"this -> global distance = %d\n",
343-
vips__cgif_compare_palettes( this, global ) );
347+
global_diff );
344348
printf( "vips_foreign_save_cgif_write_frame: "
345349
"this -> prev distance = %d\n",
346-
vips__cgif_compare_palettes( this, prev ) );
350+
prev_diff );
347351
printf( "vips_foreign_save_cgif_write_frame: "
348352
"threshold = %g\n", cgif->interpalette_maxerror );
349353
#endif/*DEBUG_VERBOSE*/
350354

351-
if( vips__cgif_compare_palettes( this, global ) <
352-
cgif->interpalette_maxerror ) {
355+
if( global_diff <= prev_diff &&
356+
global_diff < cgif->interpalette_maxerror ) {
353357
/* Global is good enough, use that.
354358
*/
355359
#ifdef DEBUG_VERBOSE
@@ -365,8 +369,7 @@ vips_foreign_save_cgif_pick_quantiser( VipsForeignSaveCgif *cgif,
365369
*result = cgif->quantisation_result;
366370
*use_local = FALSE;
367371
}
368-
else if( vips__cgif_compare_palettes( this, prev ) <
369-
cgif->interpalette_maxerror ) {
372+
else if( prev_diff < cgif->interpalette_maxerror ) {
370373
/* Previous is good enough, use that again.
371374
*/
372375
#ifdef DEBUG_VERBOSE

0 commit comments

Comments
 (0)