1 // Copyright 2014 Google Inc. All Rights Reserved. 2 // 3 // Use of this source code is governed by a BSD-style license 4 // that can be found in the COPYING file in the root of the source 5 // tree. An additional intellectual property rights grant can be found 6 // in the file PATENTS. All contributing project authors may 7 // be found in the AUTHORS file in the root of the source tree. 8 // ----------------------------------------------------------------------------- 9 // 10 // GIF decode. 11 12 #ifndef WEBP_EXAMPLES_GIFDEC_H_ 13 #define WEBP_EXAMPLES_GIFDEC_H_ 14 15 #include <stdio.h> 16 #include "webp/types.h" 17 18 #ifdef HAVE_CONFIG_H 19 #include "webp/config.h" 20 #endif 21 22 #ifdef WEBP_HAVE_GIF 23 #include <gif_lib.h> 24 #endif 25 26 #ifdef __cplusplus 27 extern "C" { 28 #endif 29 30 // GIFLIB_MAJOR is only defined in libgif >= 4.2.0. 31 #if defined(GIFLIB_MAJOR) && defined(GIFLIB_MINOR) 32 # define LOCAL_GIF_VERSION ((GIFLIB_MAJOR << 8) | GIFLIB_MINOR) 33 # define LOCAL_GIF_PREREQ(maj, min) \ 34 (LOCAL_GIF_VERSION >= (((maj) << 8) | (min))) 35 #else 36 # define LOCAL_GIF_VERSION 0 37 # define LOCAL_GIF_PREREQ(maj, min) 0 38 #endif 39 40 #define GIF_INDEX_INVALID (-1) 41 42 typedef enum GIFDisposeMethod { 43 GIF_DISPOSE_NONE, 44 GIF_DISPOSE_BACKGROUND, 45 GIF_DISPOSE_RESTORE_PREVIOUS 46 } GIFDisposeMethod; 47 48 typedef struct { 49 int x_offset, y_offset, width, height; 50 } GIFFrameRect; 51 52 struct WebPData; 53 struct WebPPicture; 54 55 #ifndef WEBP_HAVE_GIF 56 struct ColorMapObject; 57 struct GifFileType; 58 typedef unsigned char GifByteType; 59 #endif 60 61 // Given the index of background color and transparent color, returns the 62 // corresponding background color (in BGRA format) in 'bgcolor'. 63 void GIFGetBackgroundColor(const struct ColorMapObject* const color_map, 64 int bgcolor_index, int transparent_index, 65 uint32_t* const bgcolor); 66 67 // Parses the given graphics extension data to get frame duration (in 1ms 68 // units), dispose method and transparent color index. 69 // Returns true on success. 70 int GIFReadGraphicsExtension(const GifByteType* const buf, int* const duration, 71 GIFDisposeMethod* const dispose, 72 int* const transparent_index); 73 74 // Reads the next GIF frame from 'gif' into 'picture'. Also, returns the GIF 75 // frame dimensions and offsets in 'rect'. 76 // Returns true on success. 77 int GIFReadFrame(struct GifFileType* const gif, int transparent_index, 78 GIFFrameRect* const gif_rect, 79 struct WebPPicture* const picture); 80 81 // Parses loop count from the given Netscape extension data. 82 int GIFReadLoopCount(struct GifFileType* const gif, GifByteType** const buf, 83 int* const loop_count); 84 85 // Parses the given ICC or XMP extension data and stores it into 'metadata'. 86 // Returns true on success. 87 int GIFReadMetadata(struct GifFileType* const gif, GifByteType** const buf, 88 struct WebPData* const metadata); 89 90 // Dispose the pixels within 'rect' of 'curr_canvas' based on 'dispose' method 91 // and 'prev_canvas'. 92 void GIFDisposeFrame(GIFDisposeMethod dispose, const GIFFrameRect* const rect, 93 const struct WebPPicture* const prev_canvas, 94 struct WebPPicture* const curr_canvas); 95 96 // Given 'src' picture and its frame rectangle 'rect', blend it into 'dst'. 97 void GIFBlendFrames(const struct WebPPicture* const src, 98 const GIFFrameRect* const rect, 99 struct WebPPicture* const dst); 100 101 // Prints an error string based on 'gif_error'. 102 void GIFDisplayError(const struct GifFileType* const gif, int gif_error); 103 104 // In the given 'pic', clear the pixels in 'rect' to transparent color. 105 void GIFClearPic(struct WebPPicture* const pic, const GIFFrameRect* const rect); 106 107 // Copy pixels from 'src' to 'dst' honoring strides. 'src' and 'dst' are assumed 108 // to be already allocated. 109 void GIFCopyPixels(const struct WebPPicture* const src, 110 struct WebPPicture* const dst); 111 112 #ifdef __cplusplus 113 } // extern "C" 114 #endif 115 116 #endif // WEBP_EXAMPLES_GIFDEC_H_ 117