Subversion Repositories Games.Descent

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 pmbaty 1
// Copyright 2011 Google Inc.
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, XMP metadata, Animation & Tiling.
15
//
16
// Code Example#1: Creating a MUX with image data, color profile & XMP metadata.
17
//
18
//   int copy_data = 0;
19
//   WebPMux* mux = WebPMuxNew();
20
//   // ... (Prepare image data).
21
//   WebPMuxSetImage(mux, image_data, image_data_size, alpha_data, alpha_size,
22
//                   copy_data);
23
//   // ... (Prepare ICCP color profile data).
24
//   WebPMuxSetColorProfile(mux, icc_data, icc_data_size, copy_data);
25
//   // ... (Prepare XMP metadata).
26
//   WebPMuxSetMetadata(mux, xmp_data, xmp_data_size, copy_data);
27
//   // Get data from mux in WebP RIFF format.
28
//   WebPMuxAssemble(mux, &output_data, &output_data_size);
29
//   WebPMuxDelete(mux);
30
//   // ... (Consume output_data; e.g. write output_data to file).
31
//   free(output_data);
32
//
33
// Code Example#2: Get image & color profile data from a WebP file.
34
//
35
//   int copy_data = 0;
36
//   // ... (Read data from file).
37
//   WebPMux* mux = WebPMuxCreate(data, data_size, copy_data);
38
//   WebPMuxGetImage(mux, &image_data, &image_data_size,
39
//                   &alpha_data, &alpha_size);
40
//   // ... (Consume image_data; e.g. call WebPDecode() to decode the data).
41
//   WebPMuxGetColorProfile(mux, &icc_data, &icc_data_size);
42
//   // ... (Consume icc_data).
43
//   WebPMuxDelete(mux);
44
//   free(data);
45
 
46
#ifndef WEBP_WEBP_MUX_H_
47
#define WEBP_WEBP_MUX_H_
48
 
49
#include "./types.h"
50
 
51
#if defined(__cplusplus) || defined(c_plusplus)
52
extern "C" {
53
#endif
54
 
55
// Error codes
56
typedef enum {
57
  WEBP_MUX_OK                 =  1,
58
  WEBP_MUX_ERROR              =  0,
59
  WEBP_MUX_NOT_FOUND          = -1,
60
  WEBP_MUX_INVALID_ARGUMENT   = -2,
61
  WEBP_MUX_INVALID_PARAMETER  = -3,
62
  WEBP_MUX_BAD_DATA           = -4,
63
  WEBP_MUX_MEMORY_ERROR       = -5
64
} WebPMuxError;
65
 
66
// Flag values for different features used in VP8X chunk.
67
typedef enum {
68
  TILE_FLAG       = 0x00000001,
69
  ANIMATION_FLAG  = 0x00000002,
70
  ICCP_FLAG       = 0x00000004,
71
  META_FLAG       = 0x00000008,
72
  ALPHA_FLAG      = 0x00000010
73
} FeatureFlags;
74
 
75
typedef struct WebPMux WebPMux;   // main opaque object.
76
 
77
//------------------------------------------------------------------------------
78
// Life of a Mux object
79
 
80
// Creates an empty mux object.
81
// Returns:
82
//   A pointer to the newly created empty mux object.
83
WEBP_EXTERN(WebPMux*) WebPMuxNew(void);
84
 
85
// Deletes the mux object.
86
// Parameters:
87
//   mux - (in/out) object to be deleted
88
WEBP_EXTERN(void) WebPMuxDelete(WebPMux* const mux);
89
 
90
//------------------------------------------------------------------------------
91
// Mux creation.
92
 
93
// Creates a mux object from raw data given in WebP RIFF format.
94
// Parameters:
95
//   data - (in) the raw data in WebP RIFF format
96
//   size - (in) size of raw data
97
//   copy_data - (in) value 1 indicates given data WILL copied to the mux, and
98
//               value 0 indicates data will NOT be copied.
99
// Returns:
100
//   A pointer to the mux object created from given data - on success.
101
//   NULL - In case of invalid data or memory error.
102
WEBP_EXTERN(WebPMux*) WebPMuxCreate(const uint8_t* data, uint32_t size,
103
                                    int copy_data);
104
 
105
//------------------------------------------------------------------------------
106
// Single Image.
107
 
108
// Sets the image in the mux object. Any existing images (including frame/tile)
109
// will be removed.
110
// Parameters:
111
//   mux - (in/out) object in which the image is to be set
112
//   data - (in) the image data to be set. The data can be either a VP8
113
//          bitstream or a single-image WebP file (non-animated & non-tiled)
114
//   size - (in) size of the image data
115
//   alpha_data - (in) the alpha data corresponding to the image (if present)
116
//   alpha_size - (in) size of alpha chunk data
117
//   copy_data - (in) value 1 indicates given data WILL copied to the mux, and
118
//               value 0 indicates data will NOT be copied.
119
// Returns:
120
//   WEBP_MUX_INVALID_ARGUMENT - if mux is NULL or data is NULL.
121
//   WEBP_MUX_MEMORY_ERROR - on memory allocation error.
122
//   WEBP_MUX_OK - on success.
123
WEBP_EXTERN(WebPMuxError) WebPMuxSetImage(WebPMux* const mux,
124
                                          const uint8_t* data, uint32_t size,
125
                                          const uint8_t* alpha_data,
126
                                          uint32_t alpha_size, int copy_data);
127
 
128
// Gets a reference to the image in the mux object.
129
// The caller should NOT free the returned data.
130
// Parameters:
131
//   mux - (in) object from which the image is to be fetched
132
//   data - (out) the returned image data
133
//   size - (out) size of the returned image data
134
//   alpha_data - (in) the returned alpha data of the image (if present)
135
//   alpha_size - (in) size of alpha chunk data
136
// Returns:
137
//   WEBP_MUX_INVALID_ARGUMENT - if either of mux, data or size is NULL
138
//                               OR if mux contains animation/tiling.
139
//   WEBP_MUX_NOT_FOUND - if image is not present in mux object.
140
//   WEBP_MUX_OK - on success.
141
WEBP_EXTERN(WebPMuxError) WebPMuxGetImage(const WebPMux* const mux,
142
                                          const uint8_t** data, uint32_t* size,
143
                                          const uint8_t** alpha_data,
144
                                          uint32_t* alpha_size);
145
 
146
// Deletes the image in the mux object.
147
// Parameters:
148
//   mux - (in/out) object from which the image is to be deleted
149
// Returns:
150
//   WEBP_MUX_INVALID_ARGUMENT - if mux is NULL
151
//                               OR if mux contains animation/tiling.
152
//   WEBP_MUX_NOT_FOUND - if image is not present in mux object.
153
//   WEBP_MUX_OK - on success.
154
WEBP_EXTERN(WebPMuxError) WebPMuxDeleteImage(WebPMux* const mux);
155
 
156
//------------------------------------------------------------------------------
157
// XMP Metadata.
158
 
159
// Sets the XMP metadata in the mux object. Any existing metadata chunk(s) will
160
// be removed.
161
// Parameters:
162
//   mux - (in/out) object to which the XMP metadata is to be added
163
//   data - (in) the XMP metadata data to be added
164
//   size - (in) size of the XMP metadata data
165
//   copy_data - (in) value 1 indicates given data WILL copied to the mux, and
166
//               value 0 indicates data will NOT be copied.
167
// Returns:
168
//   WEBP_MUX_INVALID_ARGUMENT - if mux is NULL or data is NULL.
169
//   WEBP_MUX_MEMORY_ERROR - on memory allocation error.
170
//   WEBP_MUX_OK - on success.
171
WEBP_EXTERN(WebPMuxError) WebPMuxSetMetadata(WebPMux* const mux,
172
                                             const uint8_t* data,
173
                                             uint32_t size, int copy_data);
174
 
175
// Gets a reference to the XMP metadata in the mux object.
176
// The caller should NOT free the returned data.
177
// Parameters:
178
//   mux - (in) object from which the XMP metadata is to be fetched
179
//   data - (out) the returned XMP metadata
180
//   size - (out) size of the returned XMP metadata
181
// Returns:
182
//   WEBP_MUX_INVALID_ARGUMENT - if either of mux, data or size is NULL
183
//   WEBP_MUX_NOT_FOUND - if metadata is not present in mux object.
184
//   WEBP_MUX_OK - on success.
185
WEBP_EXTERN(WebPMuxError) WebPMuxGetMetadata(const WebPMux* const mux,
186
                                             const uint8_t** data,
187
                                             uint32_t* size);
188
 
189
// Deletes the XMP metadata in the mux object.
190
// Parameters:
191
//   mux - (in/out) object from which XMP metadata is to be deleted
192
// Returns:
193
//   WEBP_MUX_INVALID_ARGUMENT - if mux is NULL
194
//   WEBP_MUX_NOT_FOUND - If mux does not contain metadata.
195
//   WEBP_MUX_OK - on success.
196
WEBP_EXTERN(WebPMuxError) WebPMuxDeleteMetadata(WebPMux* const mux);
197
 
198
//------------------------------------------------------------------------------
199
// ICC Color Profile.
200
 
201
// Sets the color profile in the mux object. Any existing color profile chunk(s)
202
// will be removed.
203
// Parameters:
204
//   mux - (in/out) object to which the color profile is to be added
205
//   data - (in) the color profile data to be added
206
//   size - (in) size of the color profile data
207
//   copy_data - (in) value 1 indicates given data WILL copied to the mux, and
208
//               value 0 indicates data will NOT be copied.
209
// Returns:
210
//   WEBP_MUX_INVALID_ARGUMENT - if mux is NULL or data is NULL
211
//   WEBP_MUX_MEMORY_ERROR - on memory allocation error
212
//   WEBP_MUX_OK - on success
213
WEBP_EXTERN(WebPMuxError) WebPMuxSetColorProfile(WebPMux* const mux,
214
                                                 const uint8_t* data,
215
                                                 uint32_t size, int copy_data);
216
 
217
// Gets a reference to the color profile in the mux object.
218
// The caller should NOT free the returned data.
219
// Parameters:
220
//   mux - (in) object from which the color profile data is to be fetched
221
//   data - (out) the returned color profile data
222
//   size - (out) size of the returned color profile data
223
// Returns:
224
//   WEBP_MUX_INVALID_ARGUMENT - if either of mux, data or size is NULL
225
//   WEBP_MUX_NOT_FOUND - if color profile is not present in mux object.
226
//   WEBP_MUX_OK - on success.
227
WEBP_EXTERN(WebPMuxError) WebPMuxGetColorProfile(const WebPMux* const mux,
228
                                                 const uint8_t** data,
229
                                                 uint32_t* size);
230
 
231
// Deletes the color profile in the mux object.
232
// Parameters:
233
//   mux - (in/out) object from which color profile is to be deleted
234
// Returns:
235
//   WEBP_MUX_INVALID_ARGUMENT - if mux is NULL
236
//   WEBP_MUX_NOT_FOUND - If mux does not contain color profile.
237
//   WEBP_MUX_OK - on success.
238
WEBP_EXTERN(WebPMuxError) WebPMuxDeleteColorProfile(WebPMux* const mux);
239
 
240
//------------------------------------------------------------------------------
241
// Animation.
242
 
243
// Adds an animation frame to the mux object.
244
// nth=0 has a special meaning - last position.
245
// Parameters:
246
//   mux - (in/out) object to which an animation frame is to be added
247
//   nth - (in) The position at which the frame is to be added.
248
//   data - (in) the raw VP8 image data corresponding to frame image. The data
249
//          can be either a VP8 bitstream or a single-image WebP file
250
//          (non-animated & non-tiled)
251
//   size - (in) size of frame chunk data
252
//   alpha_data - (in) the alpha data corresponding to frame image (if present)
253
//   alpha_size - (in) size of alpha chunk data
254
//   x_offset - (in) x-offset of the frame to be added
255
//   y_offset - (in) y-offset of the frame to be added
256
//   duration - (in) duration of the frame to be added (in milliseconds)
257
//   copy_data - (in) value 1 indicates given data WILL copied to the mux, and
258
//               value 0 indicates data will NOT be copied.
259
// Returns:
260
//   WEBP_MUX_INVALID_ARGUMENT - if mux is NULL or data is NULL
261
//   WEBP_MUX_NOT_FOUND - If we have less than (nth-1) frames before adding.
262
//   WEBP_MUX_MEMORY_ERROR - on memory allocation error.
263
//   WEBP_MUX_OK - on success.
264
WEBP_EXTERN(WebPMuxError) WebPMuxAddFrame(WebPMux* const mux, uint32_t nth,
265
                                          const uint8_t* data, uint32_t size,
266
                                          const uint8_t* alpha_data,
267
                                          uint32_t alpha_size,
268
                                          uint32_t x_offset, uint32_t y_offset,
269
                                          uint32_t duration, int copy_data);
270
 
271
// TODO(urvang): Create a struct as follows to reduce argument list size:
272
// typedef struct {
273
//  int nth;
274
//  uint8_t* data;
275
//  uint32_t data_size;
276
//  uint8_t* alpha;
277
//  uint32_t alpha_size;
278
//  uint32_t x_offset, y_offset;
279
//  uint32_t duration;
280
// } FrameInfo;
281
 
282
// Gets a reference to the nth animation frame from the mux object.
283
// The caller should NOT free the returned data.
284
// nth=0 has a special meaning - last position.
285
// Parameters:
286
//   mux - (in) object from which the info is to be fetched
287
//   nth - (in) index of the frame in the mux object
288
//   data - (out) the returned image data
289
//   size - (out) size of the returned image data
290
//   alpha_data - (in) the alpha data corresponding to frame image (if present)
291
//   alpha_size - (in) size of alpha chunk data
292
//   x_offset - (out) x-offset of the returned frame
293
//   y_offset - (out) y-offset of the returned frame
294
//   duration - (out) duration of the returned frame (in milliseconds)
295
// Returns:
296
//   WEBP_MUX_INVALID_ARGUMENT - if either mux, data, size, x_offset,
297
//                               y_offset, or duration is NULL
298
//   WEBP_MUX_NOT_FOUND - if there are less than nth frames in the mux object.
299
//   WEBP_MUX_BAD_DATA - if nth frame chunk in mux is invalid.
300
//   WEBP_MUX_OK - on success.
301
WEBP_EXTERN(WebPMuxError) WebPMuxGetFrame(const WebPMux* const mux,
302
                                          uint32_t nth,
303
                                          const uint8_t** data, uint32_t* size,
304
                                          const uint8_t** alpha_data,
305
                                          uint32_t* alpha_size,
306
                                          uint32_t* x_offset,
307
                                          uint32_t* y_offset,
308
                                          uint32_t* duration);
309
 
310
// Deletes an animation frame from the mux object.
311
// nth=0 has a special meaning - last position.
312
// Parameters:
313
//   mux - (in/out) object from which a frame is to be deleted
314
//   nth - (in) The position from which the frame is to be deleted
315
// Returns:
316
//   WEBP_MUX_INVALID_ARGUMENT - if mux is NULL
317
//   WEBP_MUX_NOT_FOUND - If there are less than nth frames in the mux object
318
//                        before deletion.
319
//   WEBP_MUX_OK - on success.
320
WEBP_EXTERN(WebPMuxError) WebPMuxDeleteFrame(WebPMux* const mux, uint32_t nth);
321
 
322
// Sets the animation loop count in the mux object. Any existing loop count
323
// value(s) will be removed.
324
// Parameters:
325
//   mux - (in/out) object in which loop chunk is to be set/added
326
//   loop_count - (in) animation loop count value.
327
//                Note that loop_count of zero denotes infinite loop.
328
// Returns:
329
//   WEBP_MUX_INVALID_ARGUMENT - if mux is NULL
330
//   WEBP_MUX_MEMORY_ERROR - on memory allocation error.
331
//   WEBP_MUX_OK - on success.
332
WEBP_EXTERN(WebPMuxError) WebPMuxSetLoopCount(WebPMux* const mux,
333
                                              uint32_t loop_count);
334
 
335
// Gets the animation loop count from the mux object.
336
// Parameters:
337
//   mux - (in) object from which the loop count is to be fetched
338
//   loop_count - (out) the loop_count value present in the LOOP chunk
339
// Returns:
340
//   WEBP_MUX_INVALID_ARGUMENT - if either of mux or loop_count is NULL
341
//   WEBP_MUX_NOT_FOUND - if loop chunk is not present in mux object.
342
//   WEBP_MUX_OK - on success.
343
WEBP_EXTERN(WebPMuxError) WebPMuxGetLoopCount(const WebPMux* const mux,
344
                                              uint32_t* loop_count);
345
 
346
//------------------------------------------------------------------------------
347
// Tiling.
348
 
349
// Adds a tile to the mux object.
350
// nth=0 has a special meaning - last position.
351
// Parameters:
352
//   mux - (in/out) object to which a tile is to be added
353
//   nth - (in) The position at which the tile is to be added.
354
//   data - (in) the raw VP8 image data corresponding to tile image.  The data
355
//          can be either a VP8 bitstream or a single-image WebP file
356
//          (non-animated & non-tiled)
357
//   size - (in) size of tile chunk data
358
//   alpha_data - (in) the alpha data corresponding to tile image (if present)
359
//   alpha_size - (in) size of alpha chunk data
360
//   x_offset - (in) x-offset of the tile to be added
361
//   y_offset - (in) y-offset of the tile to be added
362
//   copy_data - (in) value 1 indicates given data WILL copied to the mux, and
363
//               value 0 indicates data will NOT be copied.
364
// Returns:
365
//   WEBP_MUX_INVALID_ARGUMENT - if mux is NULL or data is NULL
366
//   WEBP_MUX_NOT_FOUND - If we have less than (nth-1) tiles before adding.
367
//   WEBP_MUX_MEMORY_ERROR - on memory allocation error.
368
//   WEBP_MUX_OK - on success.
369
WEBP_EXTERN(WebPMuxError) WebPMuxAddTile(WebPMux* const mux, uint32_t nth,
370
                                         const uint8_t* data, uint32_t size,
371
                                         const uint8_t* alpha_data,
372
                                         uint32_t alpha_size,
373
                                         uint32_t x_offset, uint32_t y_offset,
374
                                         int copy_data);
375
 
376
// Gets a reference to the nth tile from the mux object.
377
// The caller should NOT free the returned data.
378
// nth=0 has a special meaning - last position.
379
// Parameters:
380
//   mux - (in) object from which the info is to be fetched
381
//   nth - (in) index of the tile in the mux object
382
//   data - (out) the returned image data
383
//   size - (out) size of the returned image data
384
//   alpha_data - (in) the alpha data corresponding to tile image (if present)
385
//   alpha_size - (in) size of alpha chunk data
386
//   x_offset - (out) x-offset of the returned tile
387
//   y_offset - (out) y-offset of the returned tile
388
// Returns:
389
//   WEBP_MUX_INVALID_ARGUMENT - if either mux, data, size, x_offset or
390
//                               y_offset is NULL
391
//   WEBP_MUX_NOT_FOUND - if there are less than nth tiles in the mux object.
392
//   WEBP_MUX_BAD_DATA - if nth tile chunk in mux is invalid.
393
//   WEBP_MUX_OK - on success.
394
WEBP_EXTERN(WebPMuxError) WebPMuxGetTile(const WebPMux* const mux, uint32_t nth,
395
                                         const uint8_t** data, uint32_t* size,
396
                                         const uint8_t** alpha_data,
397
                                         uint32_t* alpha_size,
398
                                         uint32_t* x_offset,
399
                                         uint32_t* y_offset);
400
 
401
// Deletes a tile from the mux object.
402
// nth=0 has a special meaning - last position
403
// Parameters:
404
//   mux - (in/out) object from which a tile is to be deleted
405
//   nth - (in) The position from which the tile is to be deleted
406
// Returns:
407
//   WEBP_MUX_INVALID_ARGUMENT - if mux is NULL
408
//   WEBP_MUX_NOT_FOUND - If there are less than nth tiles in the mux object
409
//                        before deletion.
410
//   WEBP_MUX_OK - on success.
411
WEBP_EXTERN(WebPMuxError) WebPMuxDeleteTile(WebPMux* const mux, uint32_t nth);
412
 
413
//------------------------------------------------------------------------------
414
// Misc Utilities.
415
 
416
// Gets the feature flags from the mux object.
417
// Parameters:
418
//   mux - (in) object from which the features are to be fetched
419
//   flags - (out) the flags specifying which features are present in the
420
//           mux object. This will be an OR of various flag values.
421
//           Enum 'FeatureFlags' can be used to test for individual flag values.
422
// Returns:
423
//   WEBP_MUX_INVALID_ARGUMENT - if mux is NULL or flags is NULL
424
//   WEBP_MUX_NOT_FOUND - if VP8X chunk is not present in mux object.
425
//   WEBP_MUX_BAD_DATA - if VP8X chunk in mux is invalid.
426
//   WEBP_MUX_OK - on success.
427
WEBP_EXTERN(WebPMuxError) WebPMuxGetFeatures(const WebPMux* const mux,
428
                                             uint32_t* flags);
429
 
430
// Gets number of chunks having tag value tag in the mux object.
431
// Parameters:
432
//   mux - (in) object from which the info is to be fetched
433
//   tag - (in) tag name specifying the type of chunk
434
//   num_elements - (out) number of chunks corresponding to the specified tag
435
// Returns:
436
//   WEBP_MUX_INVALID_ARGUMENT - if either mux, tag or num_elements is NULL
437
//   WEBP_MUX_OK - on success.
438
WEBP_EXTERN(WebPMuxError) WebPMuxNumNamedElements(const WebPMux* const mux,
439
                                                  const char* tag,
440
                                                  int* num_elements);
441
 
442
// Assembles all chunks in WebP RIFF format and returns in output_data.
443
// This function also validates the mux object.
444
// The content of '*output_data' is allocated using malloc(), and NOT
445
// owned by the 'mux' object.
446
// It MUST be deallocated by the caller by calling free().
447
// Parameters:
448
//   mux - (in/out) object whose chunks are to be assembled
449
//   output_data - (out) byte array where assembled WebP data is returned
450
//   output_size - (out) size of returned data
451
// Returns:
452
//   WEBP_MUX_BAD_DATA - if mux object is invalid.
453
//   WEBP_MUX_INVALID_ARGUMENT - if either mux, output_data or output_size is
454
//                               NULL.
455
//   WEBP_MUX_MEMORY_ERROR - on memory allocation error.
456
//   WEBP_MUX_OK - on success
457
WEBP_EXTERN(WebPMuxError) WebPMuxAssemble(WebPMux* const mux,
458
                                          uint8_t** output_data,
459
                                          uint32_t* output_size);
460
 
461
//------------------------------------------------------------------------------
462
 
463
#if defined(__cplusplus) || defined(c_plusplus)
464
}    // extern "C"
465
#endif
466
 
467
#endif  /* WEBP_WEBP_MUX_H_ */