Home | History | Annotate | Download | only in webp
      1 // Copyright 2012 Google Inc. All Rights Reserved.
      2 //
      3 // This code is licensed under the same terms as WebM:
      4 //  Software License Agreement:  http://www.webmproject.org/license/software/
      5 //  Additional IP Rights Grant:  http://www.webmproject.org/license/additional/
      6 // -----------------------------------------------------------------------------
      7 //
      8 // Internal header for some common data-types used by mux and demux libraries.
      9 //
     10 // Author: Urvang (urvang (at) google.com)
     11 
     12 #ifndef WEBP_WEBP_MUX_TYPES_H_
     13 #define WEBP_WEBP_MUX_TYPES_H_
     14 
     15 #include <stdlib.h>  // free()
     16 #include <string.h>  // memset()
     17 #include "./types.h"
     18 
     19 #if defined(__cplusplus) || defined(c_plusplus)
     20 extern "C" {
     21 #endif
     22 
     23 // Create fourcc of the chunk from the chunk tag characters.
     24 #define MKFOURCC(a, b, c, d) ((uint32_t)(a) | (b) << 8 | (c) << 16 | (d) << 24)
     25 
     26 // Dispose method (animation only). Indicates how the area used by the current
     27 // frame is to be treated before rendering the next frame on the canvas.
     28 #if !(defined(__cplusplus) || defined(c_plusplus))
     29 typedef enum WebPMuxAnimDispose WebPMuxAnimDispose;
     30 #endif
     31 enum WebPMuxAnimDispose {
     32   WEBP_MUX_DISPOSE_NONE,       // Do not dispose.
     33   WEBP_MUX_DISPOSE_BACKGROUND  // Dispose to background color.
     34 };
     35 
     36 // Data type used to describe 'raw' data, e.g., chunk data
     37 // (ICC profile, metadata) and WebP compressed image data.
     38 typedef struct WebPData WebPData;
     39 struct WebPData {
     40   const uint8_t* bytes;
     41   size_t size;
     42 };
     43 
     44 // Initializes the contents of the 'webp_data' object with default values.
     45 static WEBP_INLINE void WebPDataInit(WebPData* webp_data) {
     46   if (webp_data != NULL) {
     47     memset(webp_data, 0, sizeof(*webp_data));
     48   }
     49 }
     50 
     51 // Clears the contents of the 'webp_data' object by calling free(). Does not
     52 // deallocate the object itself.
     53 static WEBP_INLINE void WebPDataClear(WebPData* webp_data) {
     54   if (webp_data != NULL) {
     55     free((void*)webp_data->bytes);
     56     WebPDataInit(webp_data);
     57   }
     58 }
     59 
     60 // Allocates necessary storage for 'dst' and copies the contents of 'src'.
     61 // Returns true on success.
     62 static WEBP_INLINE int WebPDataCopy(const WebPData* src, WebPData* dst) {
     63   if (src == NULL || dst == NULL) return 0;
     64   WebPDataInit(dst);
     65   if (src->bytes != NULL && src->size != 0) {
     66     dst->bytes = (uint8_t*)malloc(src->size);
     67     if (dst->bytes == NULL) return 0;
     68     memcpy((void*)dst->bytes, src->bytes, src->size);
     69     dst->size = src->size;
     70   }
     71   return 1;
     72 }
     73 
     74 #if defined(__cplusplus) || defined(c_plusplus)
     75 }    // extern "C"
     76 #endif
     77 
     78 #endif  /* WEBP_WEBP_MUX_TYPES_H_ */
     79