Urvang Joshi | d538cea | 2013-09-12 13:41:09 -0700 | [diff] [blame^] | 1 | // Copyright 2013 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 | // Helper structs and methods for gif2webp tool. |
| 11 | // |
| 12 | // Author: Urvang (urvang@google.com) |
| 13 | |
| 14 | #ifndef WEBP_EXAMPLES_GIF2WEBP_UTIL_H_ |
| 15 | #define WEBP_EXAMPLES_GIF2WEBP_UTIL_H_ |
| 16 | |
| 17 | #include <stdlib.h> |
| 18 | |
| 19 | #include "webp/mux.h" |
| 20 | |
| 21 | #if defined(__cplusplus) || defined(c_plusplus) |
| 22 | extern "C" { |
| 23 | #endif |
| 24 | |
| 25 | //------------------------------------------------------------------------------ |
| 26 | // Frame cache. |
| 27 | |
| 28 | typedef struct WebPFrameCache WebPFrameCache; |
| 29 | |
| 30 | // Given the minimum distance between key frames 'kmin' and maximum distance |
| 31 | // between key frames 'kmax', returns an appropriately allocated cache object. |
| 32 | // Use WebPFrameCacheDelete() to deallocate the 'cache'. |
| 33 | WebPFrameCache* WebPFrameCacheNew(size_t kmin, size_t kmax); |
| 34 | |
| 35 | // Release all the frame data from 'cache' and free 'cache'. |
| 36 | void WebPFrameCacheDelete(WebPFrameCache* const cache); |
| 37 | |
| 38 | // Add encoded frame in the cache. 'sub_frame_info' and 'sub_frame_pic' are used |
| 39 | // to encode the frame rectangle, while 'key_frame_info' and 'key_frame_pic' are |
| 40 | // used to encode the key frame. Either 'sub_frame_pic' (and 'sub_frame_info') |
| 41 | // or 'key_frame_pic' (and 'key_frame_info') can be NULL; in which case the |
| 42 | // corresponding variant will be omitted. |
| 43 | // Returns true on success. |
| 44 | int WebPFrameCacheAddFrame(WebPFrameCache* const cache, |
| 45 | const WebPConfig* const config, |
| 46 | const WebPMuxFrameInfo* const sub_frame_info, |
| 47 | WebPPicture* const sub_frame_pic, |
| 48 | const WebPMuxFrameInfo* const key_frame_info, |
| 49 | WebPPicture* const key_frame_pic); |
| 50 | |
| 51 | // Flush the *ready* frames from cache and add them to 'mux'. If 'verbose' is |
| 52 | // true, prints the information about these frames. |
| 53 | WebPMuxError WebPFrameCacheFlush(WebPFrameCache* const cache, int verbose, |
| 54 | WebPMux* const mux); |
| 55 | |
| 56 | // Similar to 'WebPFrameCacheFlushFrames()', but flushes *all* the frames. |
| 57 | WebPMuxError WebPFrameCacheFlushAll(WebPFrameCache* const cache, int verbose, |
| 58 | WebPMux* const mux); |
| 59 | |
| 60 | // Returns true if subsequent call to WebPFrameCacheAddFrame() should |
| 61 | // incorporate a potential keyframe. |
| 62 | int WebPFrameCacheShouldTryKeyFrame(const WebPFrameCache* const cache); |
| 63 | |
| 64 | //------------------------------------------------------------------------------ |
| 65 | // Frame rectangle and related utilities. |
| 66 | |
| 67 | #define TRANSPARENT_COLOR 0x00ffffff |
| 68 | |
| 69 | typedef struct { |
| 70 | int x_offset, y_offset, width, height; |
| 71 | } WebPFrameRect; |
| 72 | |
| 73 | struct WebPPicture; |
| 74 | |
| 75 | // Clear pixels in 'picture' within given 'rect' to transparent color. |
| 76 | void WebPUtilClearPic(struct WebPPicture* const picture, |
| 77 | const WebPFrameRect* const rect); |
| 78 | |
| 79 | // Copy pixels from 'src' to 'dst' honoring strides. 'src' and 'dst' are assumed |
| 80 | // to be already allocated. |
| 81 | void WebPUtilCopyPixels(const struct WebPPicture* const src, |
| 82 | WebPPicture* const dst); |
| 83 | |
| 84 | // Given 'src' picture and its frame rectangle 'rect', blend it into 'dst'. |
| 85 | void WebPUtilBlendPixels(const struct WebPPicture* const src, |
| 86 | const WebPFrameRect* const src_rect, |
| 87 | struct WebPPicture* const dst); |
| 88 | |
| 89 | //------------------------------------------------------------------------------ |
| 90 | // Key frame related. |
| 91 | |
| 92 | // Returns true if 'curr' frame with frame rectangle 'curr_rect' is a key frame, |
| 93 | // that is, it can be decoded independently of 'prev' canvas. |
| 94 | int WebPUtilIsKeyFrame(const WebPPicture* const curr, |
| 95 | const WebPFrameRect* const curr_rect, |
| 96 | const WebPPicture* const prev); |
| 97 | |
| 98 | // Given 'prev' frame and current frame rectangle 'rect', convert 'curr' frame |
| 99 | // to a key frame. |
| 100 | void WebPUtilConvertToKeyFrame(const WebPPicture* const prev, |
| 101 | WebPFrameRect* const rect, |
| 102 | WebPPicture* const curr); |
| 103 | |
| 104 | //------------------------------------------------------------------------------ |
| 105 | |
| 106 | #if defined(__cplusplus) || defined(c_plusplus) |
| 107 | } // extern "C" |
| 108 | #endif |
| 109 | |
| 110 | #endif // WEBP_EXAMPLES_GIF2WEBP_UTIL_H_ |