Skip to content

cgifsave: remove regressions from 2853 #2870

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 19, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 28 additions & 17 deletions libvips/foreign/cgifsave.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,18 +214,25 @@ vips_foreign_save_cgif_set_transparent( VipsForeignSaveCgif *cgif,
}
}

if( index[i] != trans ) {
old[0] = new[0];
old[1] = new[1];
old[2] = new[2];
old[3] = new[3];
}

old += 4;
new += 4;
}
}

static int
static double
vips__cgif_compare_palettes( const VipsQuantisePalette *new,
const VipsQuantisePalette *old )
{
int i, j;
int best_dist, dist, rd, gd, bd;
int total_dist;
double best_dist, dist, rd, gd, bd;
double total_dist;

g_assert( new->count <= 256 );
g_assert( old->count <= 256 );
Expand Down Expand Up @@ -337,19 +344,23 @@ vips_foreign_save_cgif_pick_quantiser( VipsForeignSaveCgif *cgif,
const VipsQuantisePalette *prev = vips__quantise_get_palette(
cgif->previous_quantisation_result );

double global_diff = vips__cgif_compare_palettes( this, global );
double prev_diff = ( prev == global ) ? global_diff :
vips__cgif_compare_palettes( this, prev );

#ifdef DEBUG_VERBOSE
printf( "vips_foreign_save_cgif_write_frame: "
"this -> global distance = %d\n",
vips__cgif_compare_palettes( this, global ) );
"this -> global distance = %g\n",
global_diff );
printf( "vips_foreign_save_cgif_write_frame: "
"this -> prev distance = %d\n",
vips__cgif_compare_palettes( this, prev ) );
"this -> prev distance = %g\n",
prev_diff );
printf( "vips_foreign_save_cgif_write_frame: "
"threshold = %g\n", cgif->interpalette_maxerror );
#endif/*DEBUG_VERBOSE*/

if( vips__cgif_compare_palettes( this, global ) <
cgif->interpalette_maxerror ) {
if( global_diff <= prev_diff &&
global_diff < cgif->interpalette_maxerror ) {
/* Global is good enough, use that.
*/
#ifdef DEBUG_VERBOSE
Expand All @@ -365,8 +376,7 @@ vips_foreign_save_cgif_pick_quantiser( VipsForeignSaveCgif *cgif,
*result = cgif->quantisation_result;
*use_local = FALSE;
}
else if( vips__cgif_compare_palettes( this, prev ) <
cgif->interpalette_maxerror ) {
else if( prev_diff < cgif->interpalette_maxerror ) {
/* Previous is good enough, use that again.
*/
#ifdef DEBUG_VERBOSE
Expand Down Expand Up @@ -564,6 +574,11 @@ vips_foreign_save_cgif_write_frame( VipsForeignSaveCgif *cgif )
frame_config.attrFlags |= CGIF_FRAME_ATTR_HAS_SET_TRANS;
frame_config.transIndex = trans;
}
else {
/* Take a copy of the RGBA frame.
*/
memcpy( cgif->previous_frame, frame_bytes, 4 * n_pels );
}

if( cgif->delay &&
page_index < cgif->delay_length )
Expand All @@ -583,10 +598,6 @@ vips_foreign_save_cgif_write_frame( VipsForeignSaveCgif *cgif )
frame_config.pImageData = cgif->index;
cgif_addframe( cgif->cgif_context, &frame_config );

/* Take a copy of the RGBA frame.
*/
memcpy( cgif->previous_frame, frame_bytes, 4 * n_pels );

return( 0 );
}

Expand Down Expand Up @@ -861,7 +872,7 @@ vips_foreign_save_cgif_class_init( VipsForeignSaveCgifClass *class )
_( "Maximum inter-palette error for palette reusage" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsForeignSaveCgif, interpalette_maxerror ),
0, 256, 40.0 );
0, 256, 3.0 );
}

static void
Expand All @@ -872,7 +883,7 @@ vips_foreign_save_cgif_init( VipsForeignSaveCgif *gif )
gif->bitdepth = 8;
gif->interframe_maxerror = 0.0;
gif->reoptimise = FALSE;
gif->interpalette_maxerror = 40.0;
gif->interpalette_maxerror = 3.0;
gif->mode = VIPS_FOREIGN_SAVE_CGIF_MODE_GLOBAL;
}

Expand Down