Merge "mux: add WebPData type"

This commit is contained in:
James Zern 2012-01-23 18:00:21 -08:00 committed by Gerrit Code Review
commit 18d959fa9a
5 changed files with 135 additions and 163 deletions

View File

@ -91,11 +91,10 @@ Example#2 (pseudo code): Get image & color profile data from a WebP file.
int copy_data = 0; int copy_data = 0;
// ... (Read data from file). // ... (Read data from file).
WebPMux* mux = WebPMuxCreate(data, data_size, copy_data); WebPMux* mux = WebPMuxCreate(data, data_size, copy_data);
WebPMuxGetImage(mux, &image_data, &image_data_size, WebPMuxGetImage(mux, &image, &alpha);
&alpha_data, &alpha_size); // ... (Consume image; e.g. call WebPDecode() to decode the data).
// ... (Consume image_data; e.g. call WebPDecode() to decode the data). WebPMuxGetColorProfile(mux, &icc_chunkdata);
WebPMuxGetColorProfile(mux, &icc_data, &icc_data_size); // ... (Consume icc_chunkdata).
// ... (Consume icc_data).
WebPMuxDelete(mux); WebPMuxDelete(mux);
free(data); free(data);

View File

@ -153,12 +153,6 @@ static int IsNotCompatible(int count1, int count2) {
} while (0) } while (0)
static WebPMuxError DisplayInfo(const WebPMux* mux) { static WebPMuxError DisplayInfo(const WebPMux* mux) {
int nFrames;
int nTiles;
const uint8_t* data = NULL;
uint32_t size = 0;
const uint8_t* alpha_data;
uint32_t alpha_size;
uint32_t flag; uint32_t flag;
WebPMuxError err = WebPMuxGetFeatures(mux, &flag); WebPMuxError err = WebPMuxGetFeatures(mux, &flag);
@ -179,6 +173,7 @@ static WebPMuxError DisplayInfo(const WebPMux* mux) {
fprintf(stderr, "\n"); fprintf(stderr, "\n");
if (flag & ANIMATION_FLAG) { if (flag & ANIMATION_FLAG) {
int nFrames;
uint32_t loop_count; uint32_t loop_count;
err = WebPMuxGetLoopCount(mux, &loop_count); err = WebPMuxGetLoopCount(mux, &loop_count);
RETURN_IF_ERROR("Failed to retrieve loop count\n"); RETURN_IF_ERROR("Failed to retrieve loop count\n");
@ -195,17 +190,19 @@ static WebPMuxError DisplayInfo(const WebPMux* mux) {
if (flag & ALPHA_FLAG) fprintf(stderr, " alpha_size"); if (flag & ALPHA_FLAG) fprintf(stderr, " alpha_size");
fprintf(stderr, "\n"); fprintf(stderr, "\n");
for (i = 1; i <= nFrames; i++) { for (i = 1; i <= nFrames; i++) {
err = WebPMuxGetFrame(mux, i, &data, &size, &alpha_data, &alpha_size, WebPData image, alpha;
err = WebPMuxGetFrame(mux, i, &image, &alpha,
&x_offset, &y_offset, &duration); &x_offset, &y_offset, &duration);
RETURN_IF_ERROR2("Failed to retrieve frame#%d\n", i); RETURN_IF_ERROR2("Failed to retrieve frame#%d\n", i);
fprintf(stderr, "%3d: %8d %8d %8d", i, x_offset, y_offset, duration); fprintf(stderr, "%3d: %8d %8d %8d", i, x_offset, y_offset, duration);
if (flag & ALPHA_FLAG) fprintf(stderr, " %10d", alpha_size); if (flag & ALPHA_FLAG) fprintf(stderr, " %10u", alpha.size_);
fprintf(stderr, "\n"); fprintf(stderr, "\n");
} }
} }
} }
if (flag & TILE_FLAG) { if (flag & TILE_FLAG) {
int nTiles;
err = WebPMuxNumNamedElements(mux, "tile", &nTiles); err = WebPMuxNumNamedElements(mux, "tile", &nTiles);
RETURN_IF_ERROR("Failed to retrieve number of tiles\n"); RETURN_IF_ERROR("Failed to retrieve number of tiles\n");
@ -217,32 +214,35 @@ static WebPMuxError DisplayInfo(const WebPMux* mux) {
if (flag & ALPHA_FLAG) fprintf(stderr, " alpha_size"); if (flag & ALPHA_FLAG) fprintf(stderr, " alpha_size");
fprintf(stderr, "\n"); fprintf(stderr, "\n");
for (i = 1; i <= nTiles; i++) { for (i = 1; i <= nTiles; i++) {
err = WebPMuxGetTile(mux, i, &data, &size, &alpha_data, &alpha_size, WebPData image, alpha;
&x_offset, &y_offset); err = WebPMuxGetTile(mux, i, &image, &alpha, &x_offset, &y_offset);
RETURN_IF_ERROR2("Failed to retrieve tile#%d\n", i); RETURN_IF_ERROR2("Failed to retrieve tile#%d\n", i);
fprintf(stderr, "%3d: %8d %8d", i, x_offset, y_offset); fprintf(stderr, "%3d: %8d %8d", i, x_offset, y_offset);
if (flag & ALPHA_FLAG) fprintf(stderr, " %10d", alpha_size); if (flag & ALPHA_FLAG) fprintf(stderr, " %10u", alpha.size_);
fprintf(stderr, "\n"); fprintf(stderr, "\n");
} }
} }
} }
if (flag & ICCP_FLAG) { if (flag & ICCP_FLAG) {
err = WebPMuxGetColorProfile(mux, &data, &size); WebPData icc_profile;
err = WebPMuxGetColorProfile(mux, &icc_profile);
RETURN_IF_ERROR("Failed to retrieve the color profile\n"); RETURN_IF_ERROR("Failed to retrieve the color profile\n");
fprintf(stderr, "Size of the color profile data: %d\n", size); fprintf(stderr, "Size of the color profile data: %u\n", icc_profile.size_);
} }
if (flag & META_FLAG) { if (flag & META_FLAG) {
err = WebPMuxGetMetadata(mux, &data, &size); WebPData metadata;
err = WebPMuxGetMetadata(mux, &metadata);
RETURN_IF_ERROR("Failed to retrieve the XMP metadata\n"); RETURN_IF_ERROR("Failed to retrieve the XMP metadata\n");
fprintf(stderr, "Size of the XMP metadata: %d\n", size); fprintf(stderr, "Size of the XMP metadata: %u\n", metadata.size_);
} }
if ((flag & ALPHA_FLAG) && !(flag & (ANIMATION_FLAG | TILE_FLAG))) { if ((flag & ALPHA_FLAG) && !(flag & (ANIMATION_FLAG | TILE_FLAG))) {
err = WebPMuxGetImage(mux, &data, &size, &alpha_data, &alpha_size); WebPData image, alpha;
err = WebPMuxGetImage(mux, &image, &alpha);
RETURN_IF_ERROR("Failed to retrieve the image\n"); RETURN_IF_ERROR("Failed to retrieve the image\n");
fprintf(stderr, "Size of the alpha data: %d\n", alpha_size); fprintf(stderr, "Size of the alpha data: %u\n", alpha.size_);
} }
return WEBP_MUX_OK; return WEBP_MUX_OK;
@ -373,8 +373,7 @@ static int ReadImage(const char* filename,
const uint8_t** alpha_data_ptr, uint32_t* alpha_size_ptr) { const uint8_t** alpha_data_ptr, uint32_t* alpha_size_ptr) {
void* data = NULL; void* data = NULL;
uint32_t size = 0; uint32_t size = 0;
const uint8_t* alpha_data = NULL; WebPData image, alpha;
uint32_t alpha_size = 0;
WebPMux* mux; WebPMux* mux;
WebPMuxError err; WebPMuxError err;
int ok = 0; int ok = 0;
@ -390,25 +389,25 @@ static int ReadImage(const char* filename,
filename, mux_state); filename, mux_state);
return 0; return 0;
} }
err = WebPMuxGetImage(mux, (const uint8_t**)&data, &size, err = WebPMuxGetImage(mux, &image, &alpha);
&alpha_data, &alpha_size);
if (err == WEBP_MUX_OK) { if (err == WEBP_MUX_OK) {
uint8_t* const data_mem = (uint8_t*)malloc(size); uint8_t* const data_mem = (uint8_t*)malloc(image.size_);
uint8_t* const alpha_mem = (uint8_t*)malloc(alpha_size); uint8_t* const alpha_mem = (uint8_t*)malloc(alpha.size_);
if ((data_mem != NULL) && (alpha_mem != NULL)) { if ((data_mem != NULL) && (alpha_mem != NULL)) {
memcpy(data_mem, data, size); memcpy(data_mem, image.bytes_, image.size_);
memcpy(alpha_mem, alpha_data, alpha_size); memcpy(alpha_mem, alpha.bytes_, alpha.size_);
*data_ptr = data_mem; *data_ptr = data_mem;
*size_ptr = size; *size_ptr = image.size_;
*alpha_data_ptr = alpha_mem; *alpha_data_ptr = alpha_mem;
*alpha_size_ptr = alpha_size; *alpha_size_ptr = alpha.size_;
ok = 1; ok = 1;
} else { } else {
free(data_mem); free(data_mem);
free(alpha_mem); free(alpha_mem);
err = WEBP_MUX_MEMORY_ERROR; err = WEBP_MUX_MEMORY_ERROR;
fprintf(stderr, "Failed to allocate %d bytes to extract image data from" fprintf(stderr, "Failed to allocate %u bytes to extract image data from"
" file %s. Error: %d\n", size + alpha_size, filename, err); " file %s. Error: %d\n",
image.size_ + alpha.size_, filename, err);
} }
} 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",
@ -418,7 +417,7 @@ static int ReadImage(const char* filename,
return ok; return ok;
} }
static int WriteData(const char* filename, void* data, uint32_t size) { static int WriteData(const char* filename, const void* data, uint32_t size) {
int ok = 0; int ok = 0;
FILE* fout = strcmp(filename, "-") ? fopen(filename, "wb") : stdout; FILE* fout = strcmp(filename, "-") ? fopen(filename, "wb") : stdout;
if (!fout) { if (!fout) {
@ -783,10 +782,7 @@ static int InitializeConfig(int argc, const char* argv[],
static int GetFrameTile(const WebPMux* mux, static int GetFrameTile(const WebPMux* mux,
const WebPMuxConfig* config, int isFrame) { const WebPMuxConfig* config, int isFrame) {
const uint8_t* data = NULL; WebPData image, alpha;
uint32_t size = 0;
const uint8_t* alpha_data = NULL;
uint32_t alpha_size = 0;
uint32_t x_offset = 0; uint32_t x_offset = 0;
uint32_t y_offset = 0; uint32_t y_offset = 0;
uint32_t duration = 0; uint32_t duration = 0;
@ -801,14 +797,13 @@ static int GetFrameTile(const WebPMux* mux,
} }
if (isFrame) { if (isFrame) {
err = WebPMuxGetFrame(mux, num, &data, &size, &alpha_data, &alpha_size, err = WebPMuxGetFrame(mux, num, &image, &alpha,
&x_offset, &y_offset, &duration); &x_offset, &y_offset, &duration);
if (err != WEBP_MUX_OK) { if (err != WEBP_MUX_OK) {
ERROR_GOTO3("ERROR#%d: Could not get frame %ld.\n", err, num, ErrGet); ERROR_GOTO3("ERROR#%d: Could not get frame %ld.\n", err, num, ErrGet);
} }
} else { } else {
err = WebPMuxGetTile(mux, num, &data, &size, &alpha_data, &alpha_size, err = WebPMuxGetTile(mux, num, &image, &alpha, &x_offset, &y_offset);
&x_offset, &y_offset);
if (err != WEBP_MUX_OK) { if (err != WEBP_MUX_OK) {
ERROR_GOTO3("ERROR#%d: Could not get frame %ld.\n", err, num, ErrGet); ERROR_GOTO3("ERROR#%d: Could not get frame %ld.\n", err, num, ErrGet);
} }
@ -819,7 +814,8 @@ static int GetFrameTile(const WebPMux* mux,
err = WEBP_MUX_MEMORY_ERROR; err = WEBP_MUX_MEMORY_ERROR;
ERROR_GOTO2("ERROR#%d: Could not allocate a mux object.\n", err, ErrGet); ERROR_GOTO2("ERROR#%d: Could not allocate a mux object.\n", err, ErrGet);
} }
err = WebPMuxSetImage(mux_single, data, size, alpha_data, alpha_size, 1); err = WebPMuxSetImage(mux_single, image.bytes_, image.size_,
alpha.bytes_, alpha.size_, 1);
if (err != WEBP_MUX_OK) { if (err != WEBP_MUX_OK) {
ERROR_GOTO2("ERROR#%d: Could not create single image mux object.\n", err, ERROR_GOTO2("ERROR#%d: Could not create single image mux object.\n", err,
ErrGet); ErrGet);
@ -834,6 +830,7 @@ static int GetFrameTile(const WebPMux* mux,
// Read and process config. // Read and process config.
static int Process(const WebPMuxConfig* config) { static int Process(const WebPMuxConfig* config) {
WebPMux* mux = NULL; WebPMux* mux = NULL;
WebPData webpdata;
const uint8_t* data = NULL; const uint8_t* data = NULL;
uint32_t size = 0; uint32_t size = 0;
const uint8_t* alpha_data = NULL; const uint8_t* alpha_data = NULL;
@ -862,19 +859,18 @@ static int Process(const WebPMuxConfig* config) {
break; break;
case FEATURE_ICCP: case FEATURE_ICCP:
err = WebPMuxGetColorProfile(mux, &data, &size); err = WebPMuxGetColorProfile(mux, &webpdata);
if (err != WEBP_MUX_OK) { if (err != WEBP_MUX_OK) {
ERROR_GOTO2("ERROR#%d: Could not get color profile.\n", err, Err2); ERROR_GOTO2("ERROR#%d: Could not get color profile.\n", err, Err2);
} }
ok = WriteData(config->output_, (void*)data, size); ok = WriteData(config->output_, webpdata.bytes_, webpdata.size_);
break; break;
case FEATURE_XMP: case FEATURE_XMP:
err = WebPMuxGetMetadata(mux, &data, &size); err = WebPMuxGetMetadata(mux, &webpdata);
if (err != WEBP_MUX_OK) { if (err != WEBP_MUX_OK) {
ERROR_GOTO2("ERROR#%d: Could not get XMP metadata.\n", err, Err2); ERROR_GOTO2("ERROR#%d: Could not get XMP metadata.\n", err, Err2);
} }
ok = WriteData(config->output_, (void*)data, size); ok = WriteData(config->output_, webpdata.bytes_, webpdata.size_);
break; break;
default: default:

View File

@ -167,14 +167,11 @@ static WebPMuxError CreateDataFromImageInfo(WebPImageInfo* image_info,
// Outputs image data given data from a webp file (including RIFF header). // Outputs image data given data from a webp file (including RIFF header).
static WebPMuxError GetImageData(const uint8_t* data, uint32_t size, static WebPMuxError GetImageData(const uint8_t* data, uint32_t size,
const uint8_t** image_data, WebPData* const image, WebPData* const alpha) {
uint32_t* image_size,
const uint8_t** alpha_data,
uint32_t* alpha_size) {
if ((size < TAG_SIZE) || (memcmp(data, "RIFF", TAG_SIZE))) { if ((size < TAG_SIZE) || (memcmp(data, "RIFF", TAG_SIZE))) {
// It is NOT webp file data. Return input data as is. // It is NOT webp file data. Return input data as is.
*image_data = data; image->bytes_ = data;
*image_size = size; image->size_ = size;
return WEBP_MUX_OK; return WEBP_MUX_OK;
} else { } else {
// It is webp file data. Extract image data from it. // It is webp file data. Extract image data from it.
@ -186,7 +183,7 @@ static WebPMuxError GetImageData(const uint8_t* data, uint32_t size,
return WEBP_MUX_BAD_DATA; return WEBP_MUX_BAD_DATA;
} }
err = WebPMuxGetImage(mux, image_data, image_size, alpha_data, alpha_size); err = WebPMuxGetImage(mux, image, alpha);
WebPMuxDelete(mux); WebPMuxDelete(mux);
return err; return err;
} }
@ -237,8 +234,7 @@ WebPMuxError WebPMuxSetImage(WebPMux* const mux,
WebPMuxError err; WebPMuxError err;
WebPChunk chunk; WebPChunk chunk;
WebPMuxImage wpi; WebPMuxImage wpi;
const uint8_t* vp8_data; WebPData image;
uint32_t vp8_size;
const int has_alpha = (alpha_data != NULL && alpha_size != 0); const int has_alpha = (alpha_data != NULL && alpha_size != 0);
if ((mux == NULL) || (data == NULL) || (size > MAX_CHUNK_PAYLOAD)) { if ((mux == NULL) || (data == NULL) || (size > MAX_CHUNK_PAYLOAD)) {
@ -246,7 +242,7 @@ WebPMuxError WebPMuxSetImage(WebPMux* const mux,
} }
// If given data is for a whole webp file, extract only the VP8 data from it. // If given data is for a whole webp file, extract only the VP8 data from it.
err = GetImageData(data, size, &vp8_data, &vp8_size, NULL, NULL); err = GetImageData(data, size, &image, NULL);
if (err != WEBP_MUX_OK) return err; if (err != WEBP_MUX_OK) return err;
// Delete the existing images. // Delete the existing images.
@ -265,8 +261,8 @@ WebPMuxError WebPMuxSetImage(WebPMux* const mux,
// Add image chunk. // Add image chunk.
ChunkInit(&chunk); ChunkInit(&chunk);
err = ChunkAssignDataImageInfo(&chunk, vp8_data, vp8_size, NULL, copy_data, err = ChunkAssignDataImageInfo(&chunk, image.bytes_, image.size_, NULL,
kChunks[IMAGE_ID].chunkTag); copy_data, kChunks[IMAGE_ID].chunkTag);
if (err != WEBP_MUX_OK) return err; if (err != WEBP_MUX_OK) return err;
err = ChunkSetNth(&chunk, &wpi.vp8_, 1); err = ChunkSetNth(&chunk, &wpi.vp8_, 1);
if (err != WEBP_MUX_OK) return err; if (err != WEBP_MUX_OK) return err;
@ -337,13 +333,12 @@ static WebPMuxError MuxAddFrameTileInternal(WebPMux* const mux, uint32_t nth,
uint32_t duration, uint32_t duration,
int copy_data, uint32_t tag) { int copy_data, uint32_t tag) {
WebPChunk chunk; WebPChunk chunk;
WebPData image;
WebPMuxImage wpi; WebPMuxImage wpi;
WebPMuxError err; WebPMuxError err;
WebPImageInfo* image_info = NULL; WebPImageInfo* image_info = NULL;
uint8_t* frame_tile_data = NULL; uint8_t* frame_tile_data = NULL;
uint32_t frame_tile_data_size = 0; uint32_t frame_tile_data_size = 0;
const uint8_t* vp8_data = NULL;
uint32_t vp8_size = 0;
const int is_frame = (tag == kChunks[FRAME_ID].chunkTag) ? 1 : 0; const int is_frame = (tag == kChunks[FRAME_ID].chunkTag) ? 1 : 0;
const int has_alpha = (alpha_data != NULL && alpha_size != 0); const int has_alpha = (alpha_data != NULL && alpha_size != 0);
@ -352,7 +347,7 @@ static WebPMuxError MuxAddFrameTileInternal(WebPMux* const mux, uint32_t nth,
} }
// If given data is for a whole webp file, extract only the VP8 data from it. // If given data is for a whole webp file, extract only the VP8 data from it.
err = GetImageData(data, size, &vp8_data, &vp8_size, NULL, NULL); err = GetImageData(data, size, &image, NULL);
if (err != WEBP_MUX_OK) return err; if (err != WEBP_MUX_OK) return err;
ChunkInit(&chunk); ChunkInit(&chunk);
@ -369,15 +364,15 @@ static WebPMuxError MuxAddFrameTileInternal(WebPMux* const mux, uint32_t nth,
} }
// Create image_info object. // Create image_info object.
image_info = CreateImageInfo(x_offset, y_offset, duration, vp8_data, image_info = CreateImageInfo(x_offset, y_offset, duration, image.bytes_,
vp8_size); image.size_);
if (image_info == NULL) { if (image_info == NULL) {
MuxImageRelease(&wpi); MuxImageRelease(&wpi);
return WEBP_MUX_MEMORY_ERROR; return WEBP_MUX_MEMORY_ERROR;
} }
// Add image chunk. // Add image chunk.
err = ChunkAssignDataImageInfo(&chunk, vp8_data, vp8_size, image_info, err = ChunkAssignDataImageInfo(&chunk, image.bytes_, image.size_, image_info,
copy_data, kChunks[IMAGE_ID].chunkTag); copy_data, kChunks[IMAGE_ID].chunkTag);
if (err != WEBP_MUX_OK) goto Err; if (err != WEBP_MUX_OK) goto Err;
image_info = NULL; // Owned by 'chunk' now. image_info = NULL; // Owned by 'chunk' now.

View File

@ -26,8 +26,8 @@ extern "C" {
const WebPChunk* const chunk = ChunkSearchList((LIST), nth, \ const WebPChunk* const chunk = ChunkSearchList((LIST), nth, \
kChunks[(ID)].chunkTag); \ kChunks[(ID)].chunkTag); \
if (chunk) { \ if (chunk) { \
*data = chunk->data_; \ data->bytes_ = chunk->data_; \
*data_size = chunk->payload_size_; \ data->size_ = chunk->payload_size_; \
return WEBP_MUX_OK; \ return WEBP_MUX_OK; \
} else { \ } else { \
return WEBP_MUX_NOT_FOUND; \ return WEBP_MUX_NOT_FOUND; \
@ -35,10 +35,9 @@ extern "C" {
} }
static WebPMuxError MuxGet(const WebPMux* const mux, TAG_ID id, uint32_t nth, static WebPMuxError MuxGet(const WebPMux* const mux, TAG_ID id, uint32_t nth,
const uint8_t** data, uint32_t* data_size) { WebPData* const data) {
assert(mux != NULL); assert(mux != NULL);
*data = NULL; memset(data, 0, sizeof(*data));
*data_size = 0;
assert(!IsWPI(id)); assert(!IsWPI(id));
SWITCH_ID_LIST(VP8X_ID, mux->vp8x_); SWITCH_ID_LIST(VP8X_ID, mux->vp8x_);
@ -196,18 +195,17 @@ WebPMux* WebPMuxCreate(const uint8_t* data, uint32_t size, int copy_data,
// Get API(s). // Get API(s).
WebPMuxError WebPMuxGetFeatures(const WebPMux* const mux, uint32_t* flags) { WebPMuxError WebPMuxGetFeatures(const WebPMux* const mux, uint32_t* flags) {
const uint8_t* data; WebPData data;
uint32_t data_size;
WebPMuxError err; WebPMuxError err;
if (mux == NULL || flags == NULL) return WEBP_MUX_INVALID_ARGUMENT; if (mux == NULL || flags == NULL) return WEBP_MUX_INVALID_ARGUMENT;
*flags = 0; *flags = 0;
// Check if VP8X chunk is present. // Check if VP8X chunk is present.
err = MuxGet(mux, VP8X_ID, 1, &data, &data_size); err = MuxGet(mux, VP8X_ID, 1, &data);
if (err == WEBP_MUX_NOT_FOUND) { if (err == WEBP_MUX_NOT_FOUND) {
// Check if VP8 chunk is present. // Check if VP8 chunk is present.
err = WebPMuxGetImage(mux, &data, &data_size, NULL, NULL); err = WebPMuxGetImage(mux, &data, NULL);
if (err == WEBP_MUX_NOT_FOUND && // Data not available (yet). if (err == WEBP_MUX_NOT_FOUND && // Data not available (yet).
mux->state_ == WEBP_MUX_STATE_PARTIAL) { // Incremental case. mux->state_ == WEBP_MUX_STATE_PARTIAL) { // Incremental case.
return WEBP_MUX_NOT_ENOUGH_DATA; return WEBP_MUX_NOT_ENOUGH_DATA;
@ -220,25 +218,24 @@ WebPMuxError WebPMuxGetFeatures(const WebPMux* const mux, uint32_t* flags) {
// TODO(urvang): Add a '#define CHUNK_SIZE_BYTES 4' and use it instead of // TODO(urvang): Add a '#define CHUNK_SIZE_BYTES 4' and use it instead of
// hard-coded value of 4 everywhere. // hard-coded value of 4 everywhere.
if (data_size < 4) return WEBP_MUX_BAD_DATA; if (data.size_ < 4) return WEBP_MUX_BAD_DATA;
// All OK. Fill up flags. // All OK. Fill up flags.
*flags = GetLE32(data); *flags = GetLE32(data.bytes_);
return WEBP_MUX_OK; return WEBP_MUX_OK;
} }
WebPMuxError WebPMuxGetImage(const WebPMux* const mux, WebPMuxError WebPMuxGetImage(const WebPMux* const mux,
const uint8_t** data, uint32_t* size, WebPData* const image,
const uint8_t** alpha_data, uint32_t* alpha_size) { WebPData* const alpha) {
WebPMuxError err; WebPMuxError err;
WebPMuxImage* wpi = NULL; WebPMuxImage* wpi = NULL;
if (mux == NULL || data == NULL || size == NULL) { if (mux == NULL || image == NULL) {
return WEBP_MUX_INVALID_ARGUMENT; return WEBP_MUX_INVALID_ARGUMENT;
} }
*data = NULL; memset(image, 0, sizeof(*image));
*size = 0;
err = ValidateForImage(mux); err = ValidateForImage(mux);
if (err != WEBP_MUX_OK) return err; if (err != WEBP_MUX_OK) return err;
@ -248,63 +245,59 @@ WebPMuxError WebPMuxGetImage(const WebPMux* const mux,
assert(err == WEBP_MUX_OK); // Already tested above. assert(err == WEBP_MUX_OK); // Already tested above.
// Get alpha chunk (if present & requested). // Get alpha chunk (if present & requested).
if (alpha_data != NULL && alpha_size != NULL) { if (alpha != NULL) {
*alpha_data = NULL; memset(alpha, 0, sizeof(*alpha));
*alpha_size = 0;
if (wpi->alpha_ != NULL) { if (wpi->alpha_ != NULL) {
*alpha_data = wpi->alpha_->data_; alpha->bytes_ = wpi->alpha_->data_;
*alpha_size = wpi->alpha_->payload_size_; alpha->size_ = wpi->alpha_->payload_size_;
} }
} }
// Get image chunk. // Get image chunk.
if (wpi->vp8_ != NULL) { if (wpi->vp8_ != NULL) {
*data = wpi->vp8_->data_; image->bytes_ = wpi->vp8_->data_;
*size = wpi->vp8_->payload_size_; image->size_ = wpi->vp8_->payload_size_;
} }
return WEBP_MUX_OK; return WEBP_MUX_OK;
} }
WebPMuxError WebPMuxGetMetadata(const WebPMux* const mux, const uint8_t** data, WebPMuxError WebPMuxGetMetadata(const WebPMux* const mux,
uint32_t* size) { WebPData* const metadata) {
if (mux == NULL || data == NULL || size == NULL) { if (mux == NULL || metadata == NULL) {
return WEBP_MUX_INVALID_ARGUMENT; return WEBP_MUX_INVALID_ARGUMENT;
} }
return MuxGet(mux, META_ID, 1, data, size); return MuxGet(mux, META_ID, 1, metadata);
} }
WebPMuxError WebPMuxGetColorProfile(const WebPMux* const mux, WebPMuxError WebPMuxGetColorProfile(const WebPMux* const mux,
const uint8_t** data, uint32_t* size) { WebPData* const color_profile) {
if (mux == NULL || data == NULL || size == NULL) { if (mux == NULL || color_profile == NULL) {
return WEBP_MUX_INVALID_ARGUMENT; return WEBP_MUX_INVALID_ARGUMENT;
} }
return MuxGet(mux, ICCP_ID, 1, data, size); return MuxGet(mux, ICCP_ID, 1, color_profile);
} }
WebPMuxError WebPMuxGetLoopCount(const WebPMux* const mux, WebPMuxError WebPMuxGetLoopCount(const WebPMux* const mux,
uint32_t* loop_count) { uint32_t* loop_count) {
const uint8_t* data; WebPData image;
uint32_t data_size;
WebPMuxError err; WebPMuxError err;
if (mux == NULL || loop_count == NULL) return WEBP_MUX_INVALID_ARGUMENT; if (mux == NULL || loop_count == NULL) return WEBP_MUX_INVALID_ARGUMENT;
err = MuxGet(mux, LOOP_ID, 1, &data, &data_size); err = MuxGet(mux, LOOP_ID, 1, &image);
if (err != WEBP_MUX_OK) return err; if (err != WEBP_MUX_OK) return err;
if (data_size < kChunks[LOOP_ID].chunkSize) return WEBP_MUX_BAD_DATA; if (image.size_ < kChunks[LOOP_ID].chunkSize) return WEBP_MUX_BAD_DATA;
*loop_count = GetLE32(data); *loop_count = GetLE32(image.bytes_);
return WEBP_MUX_OK; return WEBP_MUX_OK;
} }
static WebPMuxError MuxGetFrameTileInternal(const WebPMux* const mux, static WebPMuxError MuxGetFrameTileInternal(const WebPMux* const mux,
uint32_t nth, uint32_t nth,
const uint8_t** data, WebPData* const image,
uint32_t* size, WebPData* const alpha,
const uint8_t** alpha_data,
uint32_t* alpha_size,
uint32_t* x_offset, uint32_t* x_offset,
uint32_t* y_offset, uint32_t* y_offset,
uint32_t* duration, uint32_t tag) { uint32_t* duration, uint32_t tag) {
@ -316,7 +309,7 @@ static WebPMuxError MuxGetFrameTileInternal(const WebPMux* const mux,
const int is_frame = (tag == kChunks[FRAME_ID].chunkTag) ? 1 : 0; const int is_frame = (tag == kChunks[FRAME_ID].chunkTag) ? 1 : 0;
const TAG_ID id = is_frame ? FRAME_ID : TILE_ID; const TAG_ID id = is_frame ? FRAME_ID : TILE_ID;
if (mux == NULL || data == NULL || size == NULL || if (mux == NULL || image == NULL ||
x_offset == NULL || y_offset == NULL || (is_frame && duration == NULL)) { x_offset == NULL || y_offset == NULL || (is_frame && duration == NULL)) {
return WEBP_MUX_INVALID_ARGUMENT; return WEBP_MUX_INVALID_ARGUMENT;
} }
@ -336,41 +329,37 @@ static WebPMuxError MuxGetFrameTileInternal(const WebPMux* const mux,
if (is_frame) *duration = GetLE32(frame_tile_data + 16); if (is_frame) *duration = GetLE32(frame_tile_data + 16);
// Get alpha chunk (if present & requested). // Get alpha chunk (if present & requested).
if (alpha_data != NULL && alpha_size != NULL) { if (alpha != NULL) {
*alpha_data = NULL; memset(alpha, 0, sizeof(*alpha));
*alpha_size = 0;
if (wpi->alpha_ != NULL) { if (wpi->alpha_ != NULL) {
*alpha_data = wpi->alpha_->data_; alpha->bytes_ = wpi->alpha_->data_;
*alpha_size = wpi->alpha_->payload_size_; alpha->size_ = wpi->alpha_->payload_size_;
} }
} }
// Get image chunk. // Get image chunk.
*data = NULL; memset(image, 0, sizeof(*image));
*size = 0;
if (wpi->vp8_ != NULL) { if (wpi->vp8_ != NULL) {
*data = wpi->vp8_->data_; image->bytes_ = wpi->vp8_->data_;
*size = wpi->vp8_->payload_size_; image->size_ = wpi->vp8_->payload_size_;
} }
return WEBP_MUX_OK; return WEBP_MUX_OK;
} }
WebPMuxError WebPMuxGetFrame(const WebPMux* const mux, uint32_t nth, WebPMuxError WebPMuxGetFrame(const WebPMux* const mux, uint32_t nth,
const uint8_t** data, uint32_t* size, WebPData* const image, WebPData* const alpha,
const uint8_t** alpha_data, uint32_t* alpha_size,
uint32_t* x_offset, uint32_t* y_offset, uint32_t* x_offset, uint32_t* y_offset,
uint32_t* duration) { uint32_t* duration) {
return MuxGetFrameTileInternal(mux, nth, data, size, alpha_data, alpha_size, return MuxGetFrameTileInternal(mux, nth, image, alpha,
x_offset, y_offset, duration, x_offset, y_offset, duration,
kChunks[FRAME_ID].chunkTag); kChunks[FRAME_ID].chunkTag);
} }
WebPMuxError WebPMuxGetTile(const WebPMux* const mux, uint32_t nth, WebPMuxError WebPMuxGetTile(const WebPMux* const mux, uint32_t nth,
const uint8_t** data, uint32_t* size, WebPData* const image, WebPData* const alpha,
const uint8_t** alpha_data, uint32_t* alpha_size,
uint32_t* x_offset, uint32_t* y_offset) { uint32_t* x_offset, uint32_t* y_offset) {
return MuxGetFrameTileInternal(mux, nth, data, size, alpha_data, alpha_size, return MuxGetFrameTileInternal(mux, nth, image, alpha,
x_offset, y_offset, NULL, x_offset, y_offset, NULL,
kChunks[TILE_ID].chunkTag); kChunks[TILE_ID].chunkTag);
} }

View File

@ -35,10 +35,9 @@
// int copy_data = 0; // int copy_data = 0;
// // ... (Read data from file). // // ... (Read data from file).
// WebPMux* mux = WebPMuxCreate(data, data_size, copy_data, NULL); // WebPMux* mux = WebPMuxCreate(data, data_size, copy_data, NULL);
// WebPMuxGetImage(mux, &image_data, &image_data_size, // WebPMuxGetImage(mux, &image, &alpha);
// &alpha_data, &alpha_size); // // ... (Consume image; e.g. call WebPDecode() to decode the data).
// // ... (Consume image_data; e.g. call WebPDecode() to decode the data). // WebPMuxGetColorProfile(mux, &icc_profile);
// WebPMuxGetColorProfile(mux, &icc_data, &icc_data_size);
// // ... (Consume icc_data). // // ... (Consume icc_data).
// WebPMuxDelete(mux); // WebPMuxDelete(mux);
// free(data); // free(data);
@ -81,6 +80,13 @@ typedef enum {
typedef struct WebPMux WebPMux; // main opaque object. typedef struct WebPMux WebPMux; // main opaque object.
// Data type used to describe 'raw' data, e.g., chunk data
// (ICC profile, metadata) and WebP compressed image data.
typedef struct {
const uint8_t* bytes_;
uint32_t size_;
} WebPData;
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Life of a Mux object // Life of a Mux object
@ -138,19 +144,16 @@ WEBP_EXTERN(WebPMuxError) WebPMuxSetImage(WebPMux* const mux,
// The caller should NOT free the returned data. // The caller should NOT free the returned data.
// Parameters: // Parameters:
// mux - (in) object from which the image is to be fetched // mux - (in) object from which the image is to be fetched
// data - (out) the returned image data // image - (out) the image data
// size - (out) size of the returned image data // alpha - (out) the alpha data of the image (if present)
// alpha_data - (in) the returned alpha data of the image (if present)
// alpha_size - (in) size of alpha chunk data
// Returns: // Returns:
// WEBP_MUX_INVALID_ARGUMENT - if either of mux, data or size is NULL // WEBP_MUX_INVALID_ARGUMENT - if either mux or image is NULL
// OR if mux contains animation/tiling. // OR if mux contains animation/tiling.
// WEBP_MUX_NOT_FOUND - if image is not present in mux object. // WEBP_MUX_NOT_FOUND - if image is not present in mux object.
// WEBP_MUX_OK - on success. // WEBP_MUX_OK - on success.
WEBP_EXTERN(WebPMuxError) WebPMuxGetImage(const WebPMux* const mux, WEBP_EXTERN(WebPMuxError) WebPMuxGetImage(const WebPMux* const mux,
const uint8_t** data, uint32_t* size, WebPData* const image,
const uint8_t** alpha_data, WebPData* const alpha);
uint32_t* alpha_size);
// Deletes the image in the mux object. // Deletes the image in the mux object.
// Parameters: // Parameters:
@ -185,15 +188,13 @@ WEBP_EXTERN(WebPMuxError) WebPMuxSetMetadata(WebPMux* const mux,
// The caller should NOT free the returned data. // The caller should NOT free the returned data.
// Parameters: // Parameters:
// mux - (in) object from which the XMP metadata is to be fetched // mux - (in) object from which the XMP metadata is to be fetched
// data - (out) the returned XMP metadata // metadata - (out) XMP metadata
// size - (out) size of the returned XMP metadata
// Returns: // Returns:
// WEBP_MUX_INVALID_ARGUMENT - if either of mux, data or size is NULL // WEBP_MUX_INVALID_ARGUMENT - if either mux or metadata is NULL.
// WEBP_MUX_NOT_FOUND - if metadata is not present in mux object. // WEBP_MUX_NOT_FOUND - if metadata is not present in mux object.
// WEBP_MUX_OK - on success. // WEBP_MUX_OK - on success.
WEBP_EXTERN(WebPMuxError) WebPMuxGetMetadata(const WebPMux* const mux, WEBP_EXTERN(WebPMuxError) WebPMuxGetMetadata(
const uint8_t** data, const WebPMux* const mux, WebPData* const metadata);
uint32_t* size);
// Deletes the XMP metadata in the mux object. // Deletes the XMP metadata in the mux object.
// Parameters: // Parameters:
@ -227,15 +228,13 @@ WEBP_EXTERN(WebPMuxError) WebPMuxSetColorProfile(WebPMux* const mux,
// The caller should NOT free the returned data. // The caller should NOT free the returned data.
// Parameters: // Parameters:
// mux - (in) object from which the color profile data is to be fetched // mux - (in) object from which the color profile data is to be fetched
// data - (out) the returned color profile data // color_profile - (out) color profile data
// size - (out) size of the returned color profile data
// Returns: // Returns:
// WEBP_MUX_INVALID_ARGUMENT - if either of mux, data or size is NULL // WEBP_MUX_INVALID_ARGUMENT - if either mux or color_profile is NULL.
// WEBP_MUX_NOT_FOUND - if color profile is not present in mux object. // WEBP_MUX_NOT_FOUND - if color profile is not present in mux object.
// WEBP_MUX_OK - on success. // WEBP_MUX_OK - on success.
WEBP_EXTERN(WebPMuxError) WebPMuxGetColorProfile(const WebPMux* const mux, WEBP_EXTERN(WebPMuxError) WebPMuxGetColorProfile(
const uint8_t** data, const WebPMux* const mux, WebPData* const color_profile);
uint32_t* size);
// Deletes the color profile in the mux object. // Deletes the color profile in the mux object.
// Parameters: // Parameters:
@ -294,24 +293,21 @@ WEBP_EXTERN(WebPMuxError) WebPMuxAddFrame(WebPMux* const mux, uint32_t nth,
// Parameters: // Parameters:
// mux - (in) object from which the info is to be fetched // mux - (in) object from which the info is to be fetched
// nth - (in) index of the frame in the mux object // nth - (in) index of the frame in the mux object
// data - (out) the returned image data // image - (out) the image data
// size - (out) size of the returned image data // alpha - (out) the alpha data corresponding to frame image (if present)
// alpha_data - (in) the alpha data corresponding to frame image (if present)
// alpha_size - (in) size of alpha chunk data
// x_offset - (out) x-offset of the returned frame // x_offset - (out) x-offset of the returned frame
// y_offset - (out) y-offset of the returned frame // y_offset - (out) y-offset of the returned frame
// duration - (out) duration of the returned frame (in milliseconds) // duration - (out) duration of the returned frame (in milliseconds)
// Returns: // Returns:
// WEBP_MUX_INVALID_ARGUMENT - if either mux, data, size, x_offset, // WEBP_MUX_INVALID_ARGUMENT - if either mux, image, x_offset,
// y_offset, or duration is NULL // y_offset, or duration is NULL
// WEBP_MUX_NOT_FOUND - if there are less than nth frames in the mux object. // WEBP_MUX_NOT_FOUND - if there are less than nth frames in the mux object.
// WEBP_MUX_BAD_DATA - if nth frame chunk in mux is invalid. // WEBP_MUX_BAD_DATA - if nth frame chunk in mux is invalid.
// WEBP_MUX_OK - on success. // WEBP_MUX_OK - on success.
WEBP_EXTERN(WebPMuxError) WebPMuxGetFrame(const WebPMux* const mux, WEBP_EXTERN(WebPMuxError) WebPMuxGetFrame(const WebPMux* const mux,
uint32_t nth, uint32_t nth,
const uint8_t** data, uint32_t* size, WebPData* const image,
const uint8_t** alpha_data, WebPData* const alpha,
uint32_t* alpha_size,
uint32_t* x_offset, uint32_t* x_offset,
uint32_t* y_offset, uint32_t* y_offset,
uint32_t* duration); uint32_t* duration);
@ -388,22 +384,19 @@ WEBP_EXTERN(WebPMuxError) WebPMuxAddTile(WebPMux* const mux, uint32_t nth,
// Parameters: // Parameters:
// mux - (in) object from which the info is to be fetched // mux - (in) object from which the info is to be fetched
// nth - (in) index of the tile in the mux object // nth - (in) index of the tile in the mux object
// data - (out) the returned image data // image - (out) the image data
// size - (out) size of the returned image data // alpha - (out) the alpha data corresponding to tile image (if present)
// alpha_data - (in) the alpha data corresponding to tile image (if present)
// alpha_size - (in) size of alpha chunk data
// x_offset - (out) x-offset of the returned tile // x_offset - (out) x-offset of the returned tile
// y_offset - (out) y-offset of the returned tile // y_offset - (out) y-offset of the returned tile
// Returns: // Returns:
// WEBP_MUX_INVALID_ARGUMENT - if either mux, data, size, x_offset or // WEBP_MUX_INVALID_ARGUMENT - if either mux, image, x_offset or
// y_offset is NULL // y_offset is NULL
// WEBP_MUX_NOT_FOUND - if there are less than nth tiles in the mux object. // WEBP_MUX_NOT_FOUND - if there are less than nth tiles in the mux object.
// WEBP_MUX_BAD_DATA - if nth tile chunk in mux is invalid. // WEBP_MUX_BAD_DATA - if nth tile chunk in mux is invalid.
// WEBP_MUX_OK - on success. // WEBP_MUX_OK - on success.
WEBP_EXTERN(WebPMuxError) WebPMuxGetTile(const WebPMux* const mux, uint32_t nth, WEBP_EXTERN(WebPMuxError) WebPMuxGetTile(const WebPMux* const mux, uint32_t nth,
const uint8_t** data, uint32_t* size, WebPData* const image,
const uint8_t** alpha_data, WebPData* const alpha,
uint32_t* alpha_size,
uint32_t* x_offset, uint32_t* x_offset,
uint32_t* y_offset); uint32_t* y_offset);