@@ -337,19 +337,23 @@ vips_foreign_save_cgif_pick_quantiser( VipsForeignSaveCgif *cgif,
337
337
const VipsQuantisePalette * prev = vips__quantise_get_palette (
338
338
cgif -> previous_quantisation_result );
339
339
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
+
340
344
#ifdef DEBUG_VERBOSE
341
345
printf ( "vips_foreign_save_cgif_write_frame: "
342
346
"this -> global distance = %d\n" ,
343
- vips__cgif_compare_palettes ( this , global ) );
347
+ global_diff );
344
348
printf ( "vips_foreign_save_cgif_write_frame: "
345
349
"this -> prev distance = %d\n" ,
346
- vips__cgif_compare_palettes ( this , prev ) );
350
+ prev_diff );
347
351
printf ( "vips_foreign_save_cgif_write_frame: "
348
352
"threshold = %g\n" , cgif -> interpalette_maxerror );
349
353
#endif /*DEBUG_VERBOSE*/
350
354
351
- if ( vips__cgif_compare_palettes ( this , global ) <
352
- cgif -> interpalette_maxerror ) {
355
+ if ( global_diff <= prev_diff &&
356
+ global_diff < cgif -> interpalette_maxerror ) {
353
357
/* Global is good enough, use that.
354
358
*/
355
359
#ifdef DEBUG_VERBOSE
@@ -365,8 +369,7 @@ vips_foreign_save_cgif_pick_quantiser( VipsForeignSaveCgif *cgif,
365
369
* result = cgif -> quantisation_result ;
366
370
* use_local = FALSE;
367
371
}
368
- else if ( vips__cgif_compare_palettes ( this , prev ) <
369
- cgif -> interpalette_maxerror ) {
372
+ else if ( prev_diff < cgif -> interpalette_maxerror ) {
370
373
/* Previous is good enough, use that again.
371
374
*/
372
375
#ifdef DEBUG_VERBOSE
0 commit comments