WebPMuxAssemble() returns WebPData*.

Also add an API 'WebPDataFree()'

Change-Id: I00dc4c67fd78a541a18aaf0e65b8ef62d9769803
This commit is contained in:
Urvang Joshi 2012-06-07 11:04:57 +05:30
parent 814a063925
commit f1df5587d9
5 changed files with 51 additions and 45 deletions

View File

@ -81,10 +81,10 @@ profile & XMP metadata.
// ... (Prepare XMP metadata). // ... (Prepare XMP metadata).
WebPMuxSetMetadata(mux, &xmp, copy_data); WebPMuxSetMetadata(mux, &xmp, copy_data);
// Get data from mux in WebP RIFF format. // Get data from mux in WebP RIFF format.
WebPMuxAssemble(mux, &output_data, &output_data_size); WebPMuxAssemble(mux, &output_data);
WebPMuxDelete(mux); WebPMuxDelete(mux);
// ... (Consume output_data; e.g. write output_data to file). // ... (Consume output_data; e.g. write output_data.bytes_ to file).
free(output_data); WebPDataClear(&output_data);
Example#2 (pseudo code): Get image & color profile data from a WebP file. Example#2 (pseudo code): Get image & color profile data from a WebP file.

View File

@ -135,12 +135,6 @@ static int WebPDataCopy(const WebPData* const src, WebPData* const dst) {
return 1; return 1;
} }
// Frees data allocated by WebPDataCopy.
static void WebPDataFree(WebPData* const webpdata) {
free((void*)webpdata->bytes_);
memset(webpdata, 0, sizeof(*webpdata));
}
#define RETURN_IF_ERROR(ERR_MSG) \ #define RETURN_IF_ERROR(ERR_MSG) \
if (err != WEBP_MUX_OK) { \ if (err != WEBP_MUX_OK) { \
fprintf(stderr, ERR_MSG); \ fprintf(stderr, ERR_MSG); \
@ -370,8 +364,8 @@ static int ReadImage(const char* filename,
if (!ok) { if (!ok) {
fprintf(stderr, "Error allocating storage for image (%zu bytes) " fprintf(stderr, "Error allocating storage for image (%zu bytes) "
"and alpha (%zu bytes) data\n", image.size_, alpha.size_); "and alpha (%zu bytes) data\n", image.size_, alpha.size_);
WebPDataFree(image_ptr); WebPDataClear(image_ptr);
WebPDataFree(alpha_ptr); WebPDataClear(alpha_ptr);
} }
} else { } else {
fprintf(stderr, "Failed to extract image data from file %s. Error: %d\n", fprintf(stderr, "Failed to extract image data from file %s. Error: %d\n",
@ -399,17 +393,15 @@ static int WriteData(const char* filename, const WebPData* const webpdata) {
} }
static int WriteWebP(WebPMux* const mux, const char* filename) { static int WriteWebP(WebPMux* const mux, const char* filename) {
WebPData webpdata;
int ok; int ok;
WebPData webp_data;
const WebPMuxError err = WebPMuxAssemble( const WebPMuxError err = WebPMuxAssemble(mux, &webp_data);
mux, (uint8_t**)&webpdata.bytes_, &webpdata.size_);
if (err != WEBP_MUX_OK) { if (err != WEBP_MUX_OK) {
fprintf(stderr, "Error (%d) assembling the WebP file.\n", err); fprintf(stderr, "Error (%d) assembling the WebP file.\n", err);
return 0; return 0;
} }
ok = WriteData(filename, &webpdata); ok = WriteData(filename, &webp_data);
WebPDataFree(&webpdata); WebPDataClear(&webp_data);
return ok; return ok;
} }
@ -862,14 +854,14 @@ static int Process(const WebPMuxConfig* config) {
ok = ParseFrameArgs(feature->args_[index].params_, ok = ParseFrameArgs(feature->args_[index].params_,
&x_offset, &y_offset, &duration); &x_offset, &y_offset, &duration);
if (!ok) { if (!ok) {
WebPDataFree(&image); WebPDataClear(&image);
WebPDataFree(&alpha); WebPDataClear(&alpha);
ERROR_GOTO1("ERROR: Could not parse frame properties.\n", Err2); ERROR_GOTO1("ERROR: Could not parse frame properties.\n", Err2);
} }
err = WebPMuxSetFrame(mux, 0, &image, &alpha, err = WebPMuxSetFrame(mux, 0, &image, &alpha,
x_offset, y_offset, duration, 1); x_offset, y_offset, duration, 1);
WebPDataFree(&image); WebPDataClear(&image);
WebPDataFree(&alpha); WebPDataClear(&alpha);
if (err != WEBP_MUX_OK) { if (err != WEBP_MUX_OK) {
ERROR_GOTO3("ERROR#%d: Could not add a frame at index %d.\n", ERROR_GOTO3("ERROR#%d: Could not add a frame at index %d.\n",
err, index, Err2); err, index, Err2);
@ -893,13 +885,13 @@ static int Process(const WebPMuxConfig* config) {
ok = ParseTileArgs(feature->args_[index].params_, &x_offset, ok = ParseTileArgs(feature->args_[index].params_, &x_offset,
&y_offset); &y_offset);
if (!ok) { if (!ok) {
WebPDataFree(&image); WebPDataClear(&image);
WebPDataFree(&alpha); WebPDataClear(&alpha);
ERROR_GOTO1("ERROR: Could not parse tile properties.\n", Err2); ERROR_GOTO1("ERROR: Could not parse tile properties.\n", Err2);
} }
err = WebPMuxSetTile(mux, 0, &image, &alpha, x_offset, y_offset, 1); err = WebPMuxSetTile(mux, 0, &image, &alpha, x_offset, y_offset, 1);
WebPDataFree(&image); WebPDataClear(&image);
WebPDataFree(&alpha); WebPDataClear(&alpha);
if (err != WEBP_MUX_OK) { if (err != WEBP_MUX_OK) {
ERROR_GOTO3("ERROR#%d: Could not add a tile at index %d.\n", ERROR_GOTO3("ERROR#%d: Could not add a tile at index %d.\n",
err, index, Err2); err, index, Err2);

View File

@ -270,7 +270,8 @@ WebPMuxError WebPMuxSetImage(WebPMux* const mux,
// Add image chunk. // Add image chunk.
ChunkInit(&chunk); ChunkInit(&chunk);
err = ChunkAssignDataImageInfo(&chunk, &image_raw, NULL, copy_data, image_tag); err = ChunkAssignDataImageInfo(&chunk, &image_raw, NULL, copy_data,
image_tag);
if (err != WEBP_MUX_OK) return err; if (err != WEBP_MUX_OK) return err;
err = ChunkSetNth(&chunk, &wpi.img_, 1); err = ChunkSetNth(&chunk, &wpi.img_, 1);
if (err != WEBP_MUX_OK) return err; if (err != WEBP_MUX_OK) return err;
@ -645,7 +646,7 @@ static WebPMuxError CreateVP8XChunk(WebPMux* const mux) {
} }
WebPMuxError WebPMuxAssemble(WebPMux* const mux, WebPMuxError WebPMuxAssemble(WebPMux* const mux,
uint8_t** output_data, size_t* output_size) { WebPData* const assembled_data) {
size_t size = 0; size_t size = 0;
uint8_t* data = NULL; uint8_t* data = NULL;
uint8_t* dst = NULL; uint8_t* dst = NULL;
@ -653,13 +654,10 @@ WebPMuxError WebPMuxAssemble(WebPMux* const mux,
int num_loop_chunks; int num_loop_chunks;
WebPMuxError err; WebPMuxError err;
if (mux == NULL || output_data == NULL || output_size == NULL) { if (mux == NULL || assembled_data == NULL) {
return WEBP_MUX_INVALID_ARGUMENT; return WEBP_MUX_INVALID_ARGUMENT;
} }
*output_data = NULL;
*output_size = 0;
// Remove LOOP chunk if unnecessary. // Remove LOOP chunk if unnecessary.
err = WebPMuxNumNamedElements(mux, kChunks[IDX_LOOP].name, err = WebPMuxNumNamedElements(mux, kChunks[IDX_LOOP].name,
&num_loop_chunks); &num_loop_chunks);
@ -715,8 +713,8 @@ WebPMuxError WebPMuxAssemble(WebPMux* const mux,
} }
// Finalize. // Finalize.
*output_data = data; assembled_data->bytes_ = data;
*output_size = size; assembled_data->size_ = size;
return err; return err;
} }

View File

@ -225,6 +225,16 @@ uint8_t* ChunkListEmit(const WebPChunk* chunk_list, uint8_t* dst) {
return dst; return dst;
} }
//------------------------------------------------------------------------------
// Life of a WebPData object.
void WebPDataClear(WebPData* const webp_data) {
if (webp_data != NULL) {
free((void*)webp_data->bytes_);
memset(webp_data, 0, sizeof(*webp_data));
}
}
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Life of a MuxImage object. // Life of a MuxImage object.

View File

@ -24,10 +24,10 @@
// // ... (Prepare XMP metadata). // // ... (Prepare XMP metadata).
// WebPMuxSetMetadata(mux, &xmp, copy_data); // WebPMuxSetMetadata(mux, &xmp, copy_data);
// // Get data from mux in WebP RIFF format. // // Get data from mux in WebP RIFF format.
// WebPMuxAssemble(mux, &output_data, &output_data_size); // WebPMuxAssemble(mux, &output_data);
// WebPMuxDelete(mux); // WebPMuxDelete(mux);
// // ... (Consume output_data; e.g. write output_data to file). // // ... (Consume output_data; e.g. write output_data.bytes_ to file).
// free(output_data); // WebPDataClear(&output_data);
// //
// Code Example#2: Get image & color profile data from a WebP file. // Code Example#2: Get image & color profile data from a WebP file.
// //
@ -88,6 +88,13 @@ typedef struct {
size_t size_; size_t size_;
} WebPData; } WebPData;
//------------------------------------------------------------------------------
// Life of a WebPData object.
// Clears the contents of the 'webp_data' object by calling free(). Does not
// deallocate the object itself.
WEBP_EXTERN(void) WebPDataClear(WebPData* const webp_data);
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Life of a Mux object // Life of a Mux object
@ -438,15 +445,15 @@ WEBP_EXTERN(WebPMuxError) WebPMuxNumNamedElements(const WebPMux* const mux,
const char* name, const char* name,
int* num_elements); int* num_elements);
// Assembles all chunks in WebP RIFF format and returns in output_data. // Assembles all chunks in WebP RIFF format and returns in 'assembled_data'.
// This function also validates the mux object. // This function also validates the mux object.
// The content of '*output_data' is allocated using malloc(), and NOT // Note: The content of 'assembled_data' will be ignored and overwritten.
// owned by the 'mux' object. // Also, the content of 'assembled_data' is allocated using malloc(), and NOT
// It MUST be deallocated by the caller by calling free(). // owned by the 'mux' object. It MUST be deallocated by the caller by calling
// WebPDataClear().
// Parameters: // Parameters:
// mux - (in/out) object whose chunks are to be assembled // mux - (in/out) object whose chunks are to be assembled
// output_data - (out) byte array where assembled WebP data is returned // assembled_data - (out) assembled WebP data
// output_size - (out) size of returned data
// Returns: // Returns:
// WEBP_MUX_BAD_DATA - if mux object is invalid. // WEBP_MUX_BAD_DATA - if mux object is invalid.
// WEBP_MUX_INVALID_ARGUMENT - if either mux, output_data or output_size is // WEBP_MUX_INVALID_ARGUMENT - if either mux, output_data or output_size is
@ -454,8 +461,7 @@ WEBP_EXTERN(WebPMuxError) WebPMuxNumNamedElements(const WebPMux* const mux,
// WEBP_MUX_MEMORY_ERROR - on memory allocation error. // WEBP_MUX_MEMORY_ERROR - on memory allocation error.
// WEBP_MUX_OK - on success // WEBP_MUX_OK - on success
WEBP_EXTERN(WebPMuxError) WebPMuxAssemble(WebPMux* const mux, WEBP_EXTERN(WebPMuxError) WebPMuxAssemble(WebPMux* const mux,
uint8_t** output_data, WebPData* const assembled_data);
size_t* output_size);
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------