Skip to content

Commit

Permalink
Expose background color for GIF inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
skidder committed Nov 11, 2024
1 parent 051b253 commit c2c6891
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
8 changes: 8 additions & 0 deletions giflib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ struct giflib_decoder_struct {
uint8_t bg_red;
uint8_t bg_blue;
uint8_t bg_alpha;
uint32_t background_color;
bool have_read_first_frame;
bool seek_clear_extensions;
};
Expand Down Expand Up @@ -133,6 +134,11 @@ int giflib_decoder_get_prev_frame_disposal(const giflib_decoder d)
}
}

uint32_t giflib_decoder_get_background_color(const giflib_decoder d)
{
return d->background_color;
}

void giflib_decoder_release(giflib_decoder d)
{
if (d->pixels) {
Expand Down Expand Up @@ -564,6 +570,8 @@ bool giflib_decoder_decode_frame(giflib_decoder d, opencv_mat mat)
else {
d->bg_red = d->bg_green = d->bg_blue = d->bg_alpha = 255;
}
d->background_color = (uint32_t)d->bg_alpha << 24 | ((uint32_t)d->bg_red << 16) |
((uint32_t)d->bg_green << 8) | (uint32_t)d->bg_blue;
}

if (!giflib_decoder_render_frame(d, &gcb, mat)) {
Expand Down
2 changes: 1 addition & 1 deletion giflib.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (d *gifDecoder) Duration() time.Duration {
}

func (d *gifDecoder) BackgroundColor() uint32 {
return 0x00FFFFFF // use a non-opaque alpha value so we can see the background if needed
return uint32(C.giflib_decoder_get_background_color(d.decoder))
}

// readAnimationInfo reads the GIF info from the decoder and caches it
Expand Down
1 change: 1 addition & 0 deletions giflib.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ void giflib_encoder_release(giflib_encoder e);
int giflib_encoder_get_output_length(giflib_encoder e);
struct GifAnimationInfo giflib_decoder_get_animation_info(const giflib_decoder d);
int giflib_decoder_get_prev_frame_disposal(const giflib_decoder d);
uint32_t giflib_decoder_get_background_color(const giflib_decoder d);
#ifdef __cplusplus
}
#endif
Expand Down

0 comments on commit c2c6891

Please sign in to comment.