OLD | NEW |
(Empty) | |
| 1 // Copyright 2011 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 // RIFF container manipulation for WEBP images. |
| 9 // |
| 10 // Authors: Urvang (urvang@google.com) |
| 11 // Vikas (vikasa@google.com) |
| 12 |
| 13 // This API allows manipulation of WebP container images containing features |
| 14 // like color profile, metadata, animation and fragmented images. |
| 15 // |
| 16 // Code Example#1: Creating a MUX with image data, color profile and XMP |
| 17 // metadata. |
| 18 // |
| 19 // int copy_data = 0; |
| 20 // WebPMux* mux = WebPMuxNew(); |
| 21 // // ... (Prepare image data). |
| 22 // WebPMuxSetImage(mux, &image, copy_data); |
| 23 // // ... (Prepare ICCP color profile data). |
| 24 // WebPMuxSetChunk(mux, "ICCP", &icc_profile, copy_data); |
| 25 // // ... (Prepare XMP metadata). |
| 26 // WebPMuxSetChunk(mux, "XMP ", &xmp, copy_data); |
| 27 // // Get data from mux in WebP RIFF format. |
| 28 // WebPMuxAssemble(mux, &output_data); |
| 29 // WebPMuxDelete(mux); |
| 30 // // ... (Consume output_data; e.g. write output_data.bytes to file). |
| 31 // WebPDataClear(&output_data); |
| 32 // |
| 33 // Code Example#2: Get image and color profile data from a WebP file. |
| 34 // |
| 35 // int copy_data = 0; |
| 36 // // ... (Read data from file). |
| 37 // WebPMux* mux = WebPMuxCreate(&data, copy_data); |
| 38 // WebPMuxGetFrame(mux, 1, &image); |
| 39 // // ... (Consume image; e.g. call WebPDecode() to decode the data). |
| 40 // WebPMuxGetChunk(mux, "ICCP", &icc_profile); |
| 41 // // ... (Consume icc_data). |
| 42 // WebPMuxDelete(mux); |
| 43 // free(data); |
| 44 |
| 45 #ifndef WEBP_WEBP_MUX_H_ |
| 46 #define WEBP_WEBP_MUX_H_ |
| 47 |
| 48 #include "./mux_types.h" |
| 49 |
| 50 #if defined(__cplusplus) || defined(c_plusplus) |
| 51 extern "C" { |
| 52 #endif |
| 53 |
| 54 #define WEBP_MUX_ABI_VERSION 0x0100 // MAJOR(8b) + MINOR(8b) |
| 55 |
| 56 typedef struct WebPMux WebPMux; // main opaque object. |
| 57 #if !(defined(__cplusplus) || defined(c_plusplus)) |
| 58 typedef enum WebPMuxError WebPMuxError; |
| 59 typedef enum WebPChunkId WebPChunkId; |
| 60 #endif |
| 61 typedef struct WebPMuxFrameInfo WebPMuxFrameInfo; |
| 62 typedef struct WebPMuxAnimParams WebPMuxAnimParams; |
| 63 |
| 64 // Error codes |
| 65 enum WebPMuxError { |
| 66 WEBP_MUX_OK = 1, |
| 67 WEBP_MUX_NOT_FOUND = 0, |
| 68 WEBP_MUX_INVALID_ARGUMENT = -1, |
| 69 WEBP_MUX_BAD_DATA = -2, |
| 70 WEBP_MUX_MEMORY_ERROR = -3, |
| 71 WEBP_MUX_NOT_ENOUGH_DATA = -4 |
| 72 }; |
| 73 |
| 74 // IDs for different types of chunks. |
| 75 enum WebPChunkId { |
| 76 WEBP_CHUNK_VP8X, // VP8X |
| 77 WEBP_CHUNK_ICCP, // ICCP |
| 78 WEBP_CHUNK_ANIM, // ANIM |
| 79 WEBP_CHUNK_ANMF, // ANMF |
| 80 WEBP_CHUNK_FRGM, // FRGM |
| 81 WEBP_CHUNK_ALPHA, // ALPH |
| 82 WEBP_CHUNK_IMAGE, // VP8/VP8L |
| 83 WEBP_CHUNK_EXIF, // EXIF |
| 84 WEBP_CHUNK_XMP, // XMP |
| 85 WEBP_CHUNK_UNKNOWN, // Other chunks. |
| 86 WEBP_CHUNK_NIL |
| 87 }; |
| 88 |
| 89 //------------------------------------------------------------------------------ |
| 90 |
| 91 // Returns the version number of the mux library, packed in hexadecimal using |
| 92 // 8bits or each of major/minor/revision. E.g: v2.5.7 is 0x020507. |
| 93 WEBP_EXTERN(int) WebPGetMuxVersion(void); |
| 94 |
| 95 //------------------------------------------------------------------------------ |
| 96 // Life of a Mux object |
| 97 |
| 98 // Internal, version-checked, entry point |
| 99 WEBP_EXTERN(WebPMux*) WebPNewInternal(int); |
| 100 |
| 101 // Creates an empty mux object. |
| 102 // Returns: |
| 103 // A pointer to the newly created empty mux object. |
| 104 static WEBP_INLINE WebPMux* WebPMuxNew(void) { |
| 105 return WebPNewInternal(WEBP_MUX_ABI_VERSION); |
| 106 } |
| 107 |
| 108 // Deletes the mux object. |
| 109 // Parameters: |
| 110 // mux - (in/out) object to be deleted |
| 111 WEBP_EXTERN(void) WebPMuxDelete(WebPMux* mux); |
| 112 |
| 113 //------------------------------------------------------------------------------ |
| 114 // Mux creation. |
| 115 |
| 116 // Internal, version-checked, entry point |
| 117 WEBP_EXTERN(WebPMux*) WebPMuxCreateInternal(const WebPData*, int, int); |
| 118 |
| 119 // Creates a mux object from raw data given in WebP RIFF format. |
| 120 // Parameters: |
| 121 // bitstream - (in) the bitstream data in WebP RIFF format |
| 122 // copy_data - (in) value 1 indicates given data WILL be copied to the mux |
| 123 // and value 0 indicates data will NOT be copied. |
| 124 // Returns: |
| 125 // A pointer to the mux object created from given data - on success. |
| 126 // NULL - In case of invalid data or memory error. |
| 127 static WEBP_INLINE WebPMux* WebPMuxCreate(const WebPData* bitstream, |
| 128 int copy_data) { |
| 129 return WebPMuxCreateInternal(bitstream, copy_data, WEBP_MUX_ABI_VERSION); |
| 130 } |
| 131 |
| 132 //------------------------------------------------------------------------------ |
| 133 // Non-image chunks. |
| 134 |
| 135 // Note: Only non-image related chunks should be managed through chunk APIs. |
| 136 // (Image related chunks are: "ANMF", "FRGM", "VP8 ", "VP8L" and "ALPH"). |
| 137 // To add, get and delete images, use APIs WebPMuxSetImage(), |
| 138 // WebPMuxPushFrame(), WebPMuxGetFrame() and WebPMuxDeleteFrame(). |
| 139 |
| 140 // Adds a chunk with id 'fourcc' and data 'chunk_data' in the mux object. |
| 141 // Any existing chunk(s) with the same id will be removed. |
| 142 // Parameters: |
| 143 // mux - (in/out) object to which the chunk is to be added |
| 144 // fourcc - (in) a character array containing the fourcc of the given chunk; |
| 145 // e.g., "ICCP", "XMP ", "EXIF" etc. |
| 146 // chunk_data - (in) the chunk data to be added |
| 147 // copy_data - (in) value 1 indicates given data WILL be copied to the mux |
| 148 // and value 0 indicates data will NOT be copied. |
| 149 // Returns: |
| 150 // WEBP_MUX_INVALID_ARGUMENT - if mux, fourcc or chunk_data is NULL |
| 151 // or if fourcc corresponds to an image chunk. |
| 152 // WEBP_MUX_MEMORY_ERROR - on memory allocation error. |
| 153 // WEBP_MUX_OK - on success. |
| 154 WEBP_EXTERN(WebPMuxError) WebPMuxSetChunk( |
| 155 WebPMux* mux, const char fourcc[4], const WebPData* chunk_data, |
| 156 int copy_data); |
| 157 |
| 158 // Gets a reference to the data of the chunk with id 'fourcc' in the mux object. |
| 159 // The caller should NOT free the returned data. |
| 160 // Parameters: |
| 161 // mux - (in) object from which the chunk data is to be fetched |
| 162 // fourcc - (in) a character array containing the fourcc of the chunk; |
| 163 // e.g., "ICCP", "XMP ", "EXIF" etc. |
| 164 // chunk_data - (out) returned chunk data |
| 165 // Returns: |
| 166 // WEBP_MUX_INVALID_ARGUMENT - if either mux, fourcc or chunk_data is NULL |
| 167 // or if fourcc corresponds to an image chunk. |
| 168 // WEBP_MUX_NOT_FOUND - If mux does not contain a chunk with the given id. |
| 169 // WEBP_MUX_OK - on success. |
| 170 WEBP_EXTERN(WebPMuxError) WebPMuxGetChunk( |
| 171 const WebPMux* mux, const char fourcc[4], WebPData* chunk_data); |
| 172 |
| 173 // Deletes the chunk with the given 'fourcc' from the mux object. |
| 174 // Parameters: |
| 175 // mux - (in/out) object from which the chunk is to be deleted |
| 176 // fourcc - (in) a character array containing the fourcc of the chunk; |
| 177 // e.g., "ICCP", "XMP ", "EXIF" etc. |
| 178 // Returns: |
| 179 // WEBP_MUX_INVALID_ARGUMENT - if mux or fourcc is NULL |
| 180 // or if fourcc corresponds to an image chunk. |
| 181 // WEBP_MUX_NOT_FOUND - If mux does not contain a chunk with the given fourcc. |
| 182 // WEBP_MUX_OK - on success. |
| 183 WEBP_EXTERN(WebPMuxError) WebPMuxDeleteChunk( |
| 184 WebPMux* mux, const char fourcc[4]); |
| 185 |
| 186 //------------------------------------------------------------------------------ |
| 187 // Images. |
| 188 |
| 189 // Encapsulates data about a single frame/fragment. |
| 190 struct WebPMuxFrameInfo { |
| 191 WebPData bitstream; // image data: can either be a raw VP8/VP8L bitstream |
| 192 // or a single-image WebP file. |
| 193 int x_offset; // x-offset of the frame. |
| 194 int y_offset; // y-offset of the frame. |
| 195 int duration; // duration of the frame (in milliseconds). |
| 196 |
| 197 WebPChunkId id; // frame type: should be one of WEBP_CHUNK_ANMF, |
| 198 // WEBP_CHUNK_FRGM or WEBP_CHUNK_IMAGE |
| 199 WebPMuxAnimDispose dispose_method; // Disposal method for the frame. |
| 200 uint32_t pad[2]; // padding for later use |
| 201 }; |
| 202 |
| 203 // Sets the (non-animated and non-fragmented) image in the mux object. |
| 204 // Note: Any existing images (including frames/fragments) will be removed. |
| 205 // Parameters: |
| 206 // mux - (in/out) object in which the image is to be set |
| 207 // bitstream - (in) can either be a raw VP8/VP8L bitstream or a single-image |
| 208 // WebP file (non-animated and non-fragmented) |
| 209 // copy_data - (in) value 1 indicates given data WILL be copied to the mux |
| 210 // and value 0 indicates data will NOT be copied. |
| 211 // Returns: |
| 212 // WEBP_MUX_INVALID_ARGUMENT - if mux is NULL or bitstream is NULL. |
| 213 // WEBP_MUX_MEMORY_ERROR - on memory allocation error. |
| 214 // WEBP_MUX_OK - on success. |
| 215 WEBP_EXTERN(WebPMuxError) WebPMuxSetImage( |
| 216 WebPMux* mux, const WebPData* bitstream, int copy_data); |
| 217 |
| 218 // Adds a frame at the end of the mux object. |
| 219 // Notes: (1) frame.id should be one of WEBP_CHUNK_ANMF or WEBP_CHUNK_FRGM |
| 220 // (2) For setting a non-animated non-fragmented image, use |
| 221 // WebPMuxSetImage() instead. |
| 222 // (3) Type of frame being pushed must be same as the frames in mux. |
| 223 // (4) As WebP only supports even offsets, any odd offset will be snapped |
| 224 // to an even location using: offset &= ~1 |
| 225 // Parameters: |
| 226 // mux - (in/out) object to which the frame is to be added |
| 227 // frame - (in) frame data. |
| 228 // copy_data - (in) value 1 indicates given data WILL be copied to the mux |
| 229 // and value 0 indicates data will NOT be copied. |
| 230 // Returns: |
| 231 // WEBP_MUX_INVALID_ARGUMENT - if mux or frame is NULL |
| 232 // or if content of 'frame' is invalid. |
| 233 // WEBP_MUX_MEMORY_ERROR - on memory allocation error. |
| 234 // WEBP_MUX_OK - on success. |
| 235 WEBP_EXTERN(WebPMuxError) WebPMuxPushFrame( |
| 236 WebPMux* mux, const WebPMuxFrameInfo* frame, int copy_data); |
| 237 |
| 238 // Gets the nth frame from the mux object. |
| 239 // The content of 'frame->bitstream' is allocated using malloc(), and NOT |
| 240 // owned by the 'mux' object. It MUST be deallocated by the caller by calling |
| 241 // WebPDataClear(). |
| 242 // nth=0 has a special meaning - last position. |
| 243 // Parameters: |
| 244 // mux - (in) object from which the info is to be fetched |
| 245 // nth - (in) index of the frame in the mux object |
| 246 // frame - (out) data of the returned frame |
| 247 // Returns: |
| 248 // WEBP_MUX_INVALID_ARGUMENT - if mux or frame is NULL. |
| 249 // WEBP_MUX_NOT_FOUND - if there are less than nth frames in the mux object. |
| 250 // WEBP_MUX_BAD_DATA - if nth frame chunk in mux is invalid. |
| 251 // WEBP_MUX_OK - on success. |
| 252 WEBP_EXTERN(WebPMuxError) WebPMuxGetFrame( |
| 253 const WebPMux* mux, uint32_t nth, WebPMuxFrameInfo* frame); |
| 254 |
| 255 // Deletes a frame from the mux object. |
| 256 // nth=0 has a special meaning - last position. |
| 257 // Parameters: |
| 258 // mux - (in/out) object from which a frame is to be deleted |
| 259 // nth - (in) The position from which the frame is to be deleted |
| 260 // Returns: |
| 261 // WEBP_MUX_INVALID_ARGUMENT - if mux is NULL. |
| 262 // WEBP_MUX_NOT_FOUND - If there are less than nth frames in the mux object |
| 263 // before deletion. |
| 264 // WEBP_MUX_OK - on success. |
| 265 WEBP_EXTERN(WebPMuxError) WebPMuxDeleteFrame(WebPMux* mux, uint32_t nth); |
| 266 |
| 267 //------------------------------------------------------------------------------ |
| 268 // Animation. |
| 269 |
| 270 // Animation parameters. |
| 271 struct WebPMuxAnimParams { |
| 272 uint32_t bgcolor; // Background color of the canvas stored (in MSB order) as: |
| 273 // Bits 00 to 07: Alpha. |
| 274 // Bits 08 to 15: Red. |
| 275 // Bits 16 to 23: Green. |
| 276 // Bits 24 to 31: Blue. |
| 277 int loop_count; // Number of times to repeat the animation [0 = infinite]. |
| 278 }; |
| 279 |
| 280 // Sets the animation parameters in the mux object. Any existing ANIM chunks |
| 281 // will be removed. |
| 282 // Parameters: |
| 283 // mux - (in/out) object in which ANIM chunk is to be set/added |
| 284 // params - (in) animation parameters. |
| 285 // Returns: |
| 286 // WEBP_MUX_INVALID_ARGUMENT - if either mux or params is NULL |
| 287 // WEBP_MUX_MEMORY_ERROR - on memory allocation error. |
| 288 // WEBP_MUX_OK - on success. |
| 289 WEBP_EXTERN(WebPMuxError) WebPMuxSetAnimationParams( |
| 290 WebPMux* mux, const WebPMuxAnimParams* params); |
| 291 |
| 292 // Gets the animation parameters from the mux object. |
| 293 // Parameters: |
| 294 // mux - (in) object from which the animation parameters to be fetched |
| 295 // params - (out) animation parameters extracted from the ANIM chunk |
| 296 // Returns: |
| 297 // WEBP_MUX_INVALID_ARGUMENT - if either of mux or params is NULL |
| 298 // WEBP_MUX_NOT_FOUND - if ANIM chunk is not present in mux object. |
| 299 // WEBP_MUX_OK - on success. |
| 300 WEBP_EXTERN(WebPMuxError) WebPMuxGetAnimationParams( |
| 301 const WebPMux* mux, WebPMuxAnimParams* params); |
| 302 |
| 303 //------------------------------------------------------------------------------ |
| 304 // Misc Utilities. |
| 305 |
| 306 // Gets the feature flags from the mux object. |
| 307 // Parameters: |
| 308 // mux - (in) object from which the features are to be fetched |
| 309 // flags - (out) the flags specifying which features are present in the |
| 310 // mux object. This will be an OR of various flag values. |
| 311 // Enum 'WebPFeatureFlags' can be used to test individual flag values. |
| 312 // Returns: |
| 313 // WEBP_MUX_INVALID_ARGUMENT - if mux or flags is NULL |
| 314 // WEBP_MUX_NOT_FOUND - if VP8X chunk is not present in mux object. |
| 315 // WEBP_MUX_BAD_DATA - if VP8X chunk in mux is invalid. |
| 316 // WEBP_MUX_OK - on success. |
| 317 WEBP_EXTERN(WebPMuxError) WebPMuxGetFeatures(const WebPMux* mux, |
| 318 uint32_t* flags); |
| 319 |
| 320 // Gets number of chunks having tag value tag in the mux object. |
| 321 // Parameters: |
| 322 // mux - (in) object from which the info is to be fetched |
| 323 // id - (in) chunk id specifying the type of chunk |
| 324 // num_elements - (out) number of chunks with the given chunk id |
| 325 // Returns: |
| 326 // WEBP_MUX_INVALID_ARGUMENT - if either mux, or num_elements is NULL |
| 327 // WEBP_MUX_OK - on success. |
| 328 WEBP_EXTERN(WebPMuxError) WebPMuxNumChunks(const WebPMux* mux, |
| 329 WebPChunkId id, int* num_elements); |
| 330 |
| 331 // Assembles all chunks in WebP RIFF format and returns in 'assembled_data'. |
| 332 // This function also validates the mux object. |
| 333 // Note: The content of 'assembled_data' will be ignored and overwritten. |
| 334 // Also, the content of 'assembled_data' is allocated using malloc(), and NOT |
| 335 // owned by the 'mux' object. It MUST be deallocated by the caller by calling |
| 336 // WebPDataClear(). |
| 337 // Parameters: |
| 338 // mux - (in/out) object whose chunks are to be assembled |
| 339 // assembled_data - (out) assembled WebP data |
| 340 // Returns: |
| 341 // WEBP_MUX_BAD_DATA - if mux object is invalid. |
| 342 // WEBP_MUX_INVALID_ARGUMENT - if either mux, output_data or output_size is |
| 343 // NULL. |
| 344 // WEBP_MUX_MEMORY_ERROR - on memory allocation error. |
| 345 // WEBP_MUX_OK - on success |
| 346 WEBP_EXTERN(WebPMuxError) WebPMuxAssemble(WebPMux* mux, |
| 347 WebPData* assembled_data); |
| 348 |
| 349 //------------------------------------------------------------------------------ |
| 350 |
| 351 #if defined(__cplusplus) || defined(c_plusplus) |
| 352 } // extern "C" |
| 353 #endif |
| 354 |
| 355 #endif /* WEBP_WEBP_MUX_H_ */ |
OLD | NEW |