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