login

<     >

2020-09-19 18:02:40 (UTC-03:00)

Marcel Rodrigues <marcelgmr@gmail.com>

Add function gd_is_bgcolor().

diff --git a/README b/README
index 794b2a8..6295126 100644
--- a/README
+++ b/README
@@ -143,7 +143,32 @@ A simplified skeleton of a GIF viewer may look like this:
     free(buffer);
     gd_close_gif(gif);
 
-7. Reading streamed metadata with extension hooks
+7. Transparent Background
+
+GIFs can mark a certain color in the palette as the "Background Color".
+Pixels having this  color are usually treated as  transparent pixels by
+applications.
+
+The function `gd_is_bgcolor()`  can be used to check whether  a pixel in
+the canvas currently has background color.
+
+    int gd_is_bgcolor(gd_GIF *gif, uint8_t color[3]);
+
+Here's an example of how to use it:
+
+    gd_render_frame(gif, buffer);
+    color = buffer;
+    for (y = 0; y < gif->height; y++) {
+        for (x = 0; x < gif->width; x++) {
+            if (gd_is_bgcolor(gif, color))
+                transparent_pixel(x, y);
+            else
+                opaque_pixel(x, y, color);
+            color += 3;
+        }
+    }
+
+8. Reading streamed metadata with extension hooks
 
 Some  metadata blocks  may occur  any number  of times  in GIF  files in
 between frames.  By default, gifdec  ignore these blocks.  However, it's

diff --git a/example.c b/example.c
index c5d8c93..f0b427e 100644
--- a/example.c
+++ b/example.c
@@ -89,7 +89,7 @@ main(int argc, char *argv[])
         color = frame;
         for (i = 0; i < gif->height; i++) {
             for (j = 0; j < gif->width; j++) {
-                if (memcmp(&gif->palette->colors[gif->bgindex*3], color, 3))
+                if (!gd_is_bgcolor(gif, color))
                     pixel = SDL_MapRGB(surface->format, color[0], color[1], color[2]);
                 else if (((i >> 2) + (j >> 2)) & 1)
                     pixel = SDL_MapRGB(surface->format, 0x7F, 0x7F, 0x7F);

diff --git a/gifdec.c b/gifdec.c
index fefd667..0c43b10 100644
--- a/gifdec.c
+++ b/gifdec.c
@@ -481,6 +481,12 @@ gd_render_frame(gd_GIF *gif, uint8_t *buffer)
     render_frame_rect(gif, buffer);
 }
 
+int
+gd_is_bgcolor(gd_GIF *gif, uint8_t color[3])
+{
+    return !memcmp(&gif->palette->colors[gif->bgindex*3], color, 3);
+}
+
 void
 gd_rewind(gd_GIF *gif)
 {

diff --git a/gifdec.h b/gifdec.h
index 32ee40e..c870b30 100644
--- a/gifdec.h
+++ b/gifdec.h
@@ -41,6 +41,7 @@ typedef struct gd_GIF {
 gd_GIF *gd_open_gif(const char *fname);
 int gd_get_frame(gd_GIF *gif);
 void gd_render_frame(gd_GIF *gif, uint8_t *buffer);
+int gd_is_bgcolor(gd_GIF *gif, uint8_t color[3]);
 void gd_rewind(gd_GIF *gif);
 void gd_close_gif(gd_GIF *gif);