From 18a9a0ab5783385c397d8207e5a1b9ab673ea71f Mon Sep 17 00:00:00 2001 From: Vincent Rabaud Date: Fri, 23 Sep 2016 13:23:10 +0200 Subject: [PATCH] Add an API to import a color-mapped image. Change-Id: I71421a1a706a74d184a24dc9276b55005d7457b5 --- extras/extras.c | 31 +++++++++++++++++++++++++++++++ extras/extras.h | 9 +++++++++ 2 files changed, 40 insertions(+) diff --git a/extras/extras.c b/extras/extras.c index 2f42450a..9358bbe8 100644 --- a/extras/extras.c +++ b/extras/extras.c @@ -11,7 +11,9 @@ // #include "./extras.h" +#include "webp/format_constants.h" +#include #include #define XTRA_MAJ_VERSION 0 @@ -108,4 +110,33 @@ int WebPImportRGB4444(const uint8_t* rgb4444, WebPPicture* pic) { return 1; } +int WebPImportColorMappedARGB(const uint8_t* indexed, int indexed_stride, + const uint32_t palette[], int palette_size, + WebPPicture* pic) { + int x, y; + uint32_t* dst; + // 256 as the input buffer is uint8_t. + assert(MAX_PALETTE_SIZE <= 256); + if (pic == NULL || indexed == NULL || indexed_stride < pic->width || + palette == NULL || palette_size > MAX_PALETTE_SIZE || palette_size <= 0) { + return 0; + } + pic->use_argb = 1; + if (!WebPPictureAlloc(pic)) return 0; + dst = pic->argb; + for (y = 0; y < pic->height; ++y) { + for (x = 0; x < pic->width; ++x) { + // Make sure we are within the palette. + if (indexed[x] >= palette_size) { + WebPPictureFree(pic); + return 0; + } + dst[x] = palette[indexed[x]]; + } + indexed += indexed_stride; + dst += pic->argb_stride; + } + return 1; +} + //------------------------------------------------------------------------------ diff --git a/extras/extras.h b/extras/extras.h index 6bb7d02a..265ae230 100644 --- a/extras/extras.h +++ b/extras/extras.h @@ -42,6 +42,15 @@ WEBP_EXTERN(int) WebPImportRGB565(const uint8_t* rgb565, WebPPicture* pic); // width and height must be set prior to calling this function. WEBP_EXTERN(int) WebPImportRGB4444(const uint8_t* rgb4444, WebPPicture* pic); +// Import a color mapped image. The number of colors is less or equal to +// MAX_PALETTE_SIZE. 'pic' must have been initialized. Its content, if any, +// will be discarded. Returns 'false' in case of error, or if indexed[] contains +// invalid indices. +WEBP_EXTERN(int) +WebPImportColorMappedARGB(const uint8_t* indexed, int indexed_stride, + const uint32_t palette[], int palette_size, + WebPPicture* pic); + //------------------------------------------------------------------------------ // Parse a bitstream, search for VP8 (lossy) header and report a