From 91b7a8c754203bd52569857b3ace9abb187a1275 Mon Sep 17 00:00:00 2001 From: Pascal Massimino Date: Mon, 18 Jun 2012 15:42:56 -0700 Subject: [PATCH] Bring the special writer 'WebPMemoryWriter' to public API => WebPMemoryWriter, WebPMemoryWriterInit(), WebPMemoryWrite() Change-Id: I142fb22b0290ece7a6f6d74f00964a2e9e58ec9b --- src/enc/picture.c | 41 ++++++++++++++++------------------------- src/webp/encode.h | 16 ++++++++++++++++ 2 files changed, 32 insertions(+), 25 deletions(-) diff --git a/src/enc/picture.c b/src/enc/picture.c index 9386adb9..826800ec 100644 --- a/src/enc/picture.c +++ b/src/enc/picture.c @@ -356,28 +356,22 @@ int WebPPictureRescale(WebPPicture* const pic, int width, int height) { } //------------------------------------------------------------------------------ -// Write-to-memory +// WebPMemoryWriter: Write-to-memory -typedef struct { - uint8_t** mem; - size_t max_size; - size_t* size; -} WebPMemoryWriter; - -static void WebPMemoryWriterInit(WebPMemoryWriter* const writer) { - *writer->mem = NULL; - *writer->size = 0; +void WebPMemoryWriterInit(WebPMemoryWriter* const writer) { + writer->mem = NULL; + writer->size = 0; writer->max_size = 0; } -static int WebPMemoryWrite(const uint8_t* data, size_t data_size, - const WebPPicture* const picture) { +int WebPMemoryWrite(const uint8_t* data, size_t data_size, + const WebPPicture* const picture) { WebPMemoryWriter* const w = (WebPMemoryWriter*)picture->custom_ptr; size_t next_size; if (w == NULL) { return 1; } - next_size = (*w->size) + data_size; + next_size = w->size + data_size; if (next_size > w->max_size) { uint8_t* new_mem; size_t next_max_size = w->max_size * 2; @@ -387,16 +381,16 @@ static int WebPMemoryWrite(const uint8_t* data, size_t data_size, if (new_mem == NULL) { return 0; } - if ((*w->size) > 0) { - memcpy(new_mem, *w->mem, *w->size); + if (w->size > 0) { + memcpy(new_mem, w->mem, w->size); } - free(*w->mem); - *w->mem = new_mem; + free(w->mem); + w->mem = new_mem; w->max_size = next_max_size; } if (data_size > 0) { - memcpy((*w->mem) + (*w->size), data, data_size); - *w->size += data_size; + memcpy(w->mem + w->size, data, data_size); + w->size += data_size; } return 1; } @@ -774,7 +768,6 @@ typedef int (*Importer)(WebPPicture* const, const uint8_t* const, int); static size_t Encode(const uint8_t* rgba, int width, int height, int stride, Importer import, float quality_factor, uint8_t** output) { - size_t output_size = 0; WebPPicture pic; WebPConfig config; WebPMemoryWriter wrt; @@ -789,19 +782,17 @@ static size_t Encode(const uint8_t* rgba, int width, int height, int stride, pic.height = height; pic.writer = WebPMemoryWrite; pic.custom_ptr = &wrt; - - wrt.mem = output; - wrt.size = &output_size; WebPMemoryWriterInit(&wrt); ok = import(&pic, rgba, stride) && WebPEncode(&config, &pic); WebPPictureFree(&pic); if (!ok) { - free(*output); + free(wrt.mem); *output = NULL; return 0; } - return output_size; + *output = wrt.mem; + return wrt.size; } #define ENCODE_FUNC(NAME, IMPORTER) \ diff --git a/src/webp/encode.h b/src/webp/encode.h index 49e6b102..a9b054b7 100644 --- a/src/webp/encode.h +++ b/src/webp/encode.h @@ -146,6 +146,22 @@ typedef struct { typedef int (*WebPWriterFunction)(const uint8_t* data, size_t data_size, const WebPPicture* const picture); +// WebPMemoryWrite: a special WebPWriterFunction that writes to memory using +// the following WebPMemoryWriter object (to be set as a custom_ptr). +typedef struct { + uint8_t* mem; // final buffer (of size 'max_size', larger than 'size'). + size_t size; // final size + size_t max_size; // total capacity +} WebPMemoryWriter; + +// The following must be called first before any use. +WEBP_EXTERN(void) WebPMemoryWriterInit(WebPMemoryWriter* const writer); + +// The custom writer to be used with WebPMemoryWriter as custom_ptr. Upon +// completion, writer.mem and writer.size will hold the coded data. +WEBP_EXTERN(int) WebPMemoryWrite(const uint8_t* data, size_t data_size, + const WebPPicture* const picture); + // Progress hook, called from time to time to report progress. It can return 0 // to request an abort of the encoding process, or 1 otherwise if all is OK. typedef int (*WebPProgressHook)(int percent, const WebPPicture* const picture);