2012-01-06 23:49:06 +01:00
|
|
|
// Copyright 2011 Google Inc. All Rights Reserved.
|
2011-02-19 08:33:46 +01:00
|
|
|
//
|
2013-06-07 08:05:58 +02:00
|
|
|
// Use of this source code is governed by a BSD-style license
|
|
|
|
// that can be found in the COPYING file in the root of the source
|
|
|
|
// tree. An additional intellectual property rights grant can be found
|
|
|
|
// in the file PATENTS. All contributing project authors may
|
|
|
|
// be found in the AUTHORS file in the root of the source tree.
|
2011-02-19 08:33:46 +01:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// WebPPicture utils: colorspace conversion, crop, ...
|
|
|
|
//
|
|
|
|
// Author: Skal (pascal.massimino@gmail.com)
|
|
|
|
|
2011-04-26 16:23:57 +02:00
|
|
|
#include <assert.h>
|
2011-02-19 08:33:46 +01:00
|
|
|
#include <stdlib.h>
|
2012-01-20 16:20:56 +01:00
|
|
|
#include <math.h>
|
2011-12-01 11:00:03 +01:00
|
|
|
|
|
|
|
#include "./vp8enci.h"
|
2013-10-30 10:00:33 +01:00
|
|
|
#include "../utils/random.h"
|
2012-08-01 21:06:04 +02:00
|
|
|
#include "../utils/utils.h"
|
2012-08-03 02:23:02 +02:00
|
|
|
#include "../dsp/yuv.h"
|
2011-02-19 08:33:46 +01:00
|
|
|
|
2013-10-18 21:28:05 +02:00
|
|
|
// Uncomment to disable gamma-compression during RGB->U/V averaging
|
|
|
|
#define USE_GAMMA_COMPRESSION
|
|
|
|
|
2011-11-23 23:17:40 +01:00
|
|
|
#define HALVE(x) (((x) + 1) >> 1)
|
|
|
|
|
2012-06-28 09:34:23 +02:00
|
|
|
static const union {
|
|
|
|
uint32_t argb;
|
|
|
|
uint8_t bytes[4];
|
|
|
|
} test_endian = { 0xff000000u };
|
|
|
|
#define ALPHA_IS_LAST (test_endian.bytes[3] == 0xff)
|
|
|
|
|
2013-04-03 04:14:14 +02:00
|
|
|
static WEBP_INLINE uint32_t MakeARGB32(int r, int g, int b) {
|
|
|
|
return (0xff000000u | (r << 16) | (g << 8) | b);
|
|
|
|
}
|
|
|
|
|
2011-08-25 23:22:32 +02:00
|
|
|
//------------------------------------------------------------------------------
|
2011-02-19 08:33:46 +01:00
|
|
|
// WebPPicture
|
2011-08-25 23:22:32 +02:00
|
|
|
//------------------------------------------------------------------------------
|
2011-02-19 08:33:46 +01:00
|
|
|
|
2012-07-18 00:01:30 +02:00
|
|
|
int WebPPictureAlloc(WebPPicture* picture) {
|
2012-01-23 09:58:09 +01:00
|
|
|
if (picture != NULL) {
|
2011-05-03 02:19:00 +02:00
|
|
|
const WebPEncCSP uv_csp = picture->colorspace & WEBP_CSP_UV_MASK;
|
|
|
|
const int has_alpha = picture->colorspace & WEBP_CSP_ALPHA_BIT;
|
2011-02-19 08:33:46 +01:00
|
|
|
const int width = picture->width;
|
|
|
|
const int height = picture->height;
|
2012-03-28 13:07:42 +02:00
|
|
|
|
2012-07-18 23:58:53 +02:00
|
|
|
if (!picture->use_argb) {
|
2012-03-28 13:07:42 +02:00
|
|
|
const int y_stride = width;
|
|
|
|
const int uv_width = HALVE(width);
|
|
|
|
const int uv_height = HALVE(height);
|
|
|
|
const int uv_stride = uv_width;
|
|
|
|
int a_width, a_stride;
|
2014-07-03 22:20:06 +02:00
|
|
|
uint64_t y_size, uv_size, a_size, total_size;
|
2012-03-28 13:07:42 +02:00
|
|
|
uint8_t* mem;
|
|
|
|
|
|
|
|
// U/V
|
|
|
|
switch (uv_csp) {
|
|
|
|
case WEBP_YUV420:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// alpha
|
|
|
|
a_width = has_alpha ? width : 0;
|
|
|
|
a_stride = a_width;
|
|
|
|
y_size = (uint64_t)y_stride * height;
|
|
|
|
uv_size = (uint64_t)uv_stride * uv_height;
|
|
|
|
a_size = (uint64_t)a_stride * height;
|
|
|
|
|
2014-07-03 22:20:06 +02:00
|
|
|
total_size = y_size + a_size + 2 * uv_size;
|
2012-03-28 13:07:42 +02:00
|
|
|
|
|
|
|
// Security and validation checks
|
2012-05-09 21:19:48 +02:00
|
|
|
if (width <= 0 || height <= 0 || // luma/alpha param error
|
2012-08-01 21:06:04 +02:00
|
|
|
uv_width < 0 || uv_height < 0) { // u/v param error
|
2011-05-03 02:19:00 +02:00
|
|
|
return 0;
|
2012-03-28 13:07:42 +02:00
|
|
|
}
|
2012-06-28 09:34:23 +02:00
|
|
|
// Clear previous buffer and allocate a new one.
|
2012-03-28 13:07:42 +02:00
|
|
|
WebPPictureFree(picture); // erase previous buffer
|
2012-08-01 21:06:04 +02:00
|
|
|
mem = (uint8_t*)WebPSafeMalloc(total_size, sizeof(*mem));
|
2012-03-28 13:07:42 +02:00
|
|
|
if (mem == NULL) return 0;
|
|
|
|
|
2012-06-28 09:34:23 +02:00
|
|
|
// From now on, we're in the clear, we can no longer fail...
|
2012-06-21 09:30:43 +02:00
|
|
|
picture->memory_ = (void*)mem;
|
2012-06-28 09:34:23 +02:00
|
|
|
picture->y_stride = y_stride;
|
|
|
|
picture->uv_stride = uv_stride;
|
|
|
|
picture->a_stride = a_stride;
|
2014-07-03 22:20:06 +02:00
|
|
|
|
2012-06-21 09:30:43 +02:00
|
|
|
// TODO(skal): we could align the y/u/v planes and adjust stride.
|
2012-03-28 13:07:42 +02:00
|
|
|
picture->y = mem;
|
|
|
|
mem += y_size;
|
|
|
|
|
|
|
|
picture->u = mem;
|
|
|
|
mem += uv_size;
|
|
|
|
picture->v = mem;
|
|
|
|
mem += uv_size;
|
|
|
|
|
2014-07-03 22:20:06 +02:00
|
|
|
if (a_size > 0) {
|
2012-03-28 13:07:42 +02:00
|
|
|
picture->a = mem;
|
|
|
|
mem += a_size;
|
|
|
|
}
|
2013-09-12 09:32:28 +02:00
|
|
|
(void)mem; // makes the static analyzer happy
|
2012-03-28 13:07:42 +02:00
|
|
|
} else {
|
2012-06-21 09:30:43 +02:00
|
|
|
void* memory;
|
2012-04-02 12:58:36 +02:00
|
|
|
const uint64_t argb_size = (uint64_t)width * height;
|
2012-08-01 21:06:04 +02:00
|
|
|
if (width <= 0 || height <= 0) {
|
2012-03-28 13:07:42 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2012-06-28 09:34:23 +02:00
|
|
|
// Clear previous buffer and allocate a new one.
|
2012-03-28 13:07:42 +02:00
|
|
|
WebPPictureFree(picture); // erase previous buffer
|
2012-08-01 21:06:04 +02:00
|
|
|
memory = WebPSafeMalloc(argb_size, sizeof(*picture->argb));
|
2012-06-21 09:30:43 +02:00
|
|
|
if (memory == NULL) return 0;
|
|
|
|
|
|
|
|
// TODO(skal): align plane to cache line?
|
2012-06-28 09:34:23 +02:00
|
|
|
picture->memory_argb_ = memory;
|
2012-06-21 09:30:43 +02:00
|
|
|
picture->argb = (uint32_t*)memory;
|
2012-03-28 13:07:42 +02:00
|
|
|
picture->argb_stride = width;
|
EXPERIMENTAL: add support for alpha channel
This is a (minor) bitstream change: if the 'color_space' bit is set to '1'
(which is normally an undefined/invalid behaviour), we add extra data at the
end of partition #0 (so-called 'extensions')
Namely, we add the size of the extension data as 3 bytes (little-endian),
followed by a set of bits telling which extensions we're incorporating.
The data then _preceeds_ this trailing tags.
This is all experimental, and you'll need to have
'#define WEBP_EXPERIMENTAL_FEATURES' in webp/types.h to enable this code
(at your own risk! :))
Still, this hack produces almost-valid WebP file for decoders that don't
check this color_space bit. In particular, previous 'dwebp' (and for instance
Chrome) will recognize this files and decode them, but without the alpha
of course. Other decoder will just see random extra stuff at the end of
partition #0.
To experiment with the alpha-channel, you need to compile on Unix platform
and use PNGs for input/output.
If 'alpha.png' is a source with alpha channel, then you can try (on Unix):
cwebp alpha.png -o alpha.webp
dwebp alpha.webp -o test.png
cwebp now has a '-noalpha' flag to ignore any alpha information from the
source, if present.
More hacking and experimenting welcome!
Change-Id: I3c7b1fd8411c9e7a9f77690e898479ad85c52f3e
2011-04-26 01:58:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-06-28 09:34:23 +02:00
|
|
|
// Remove reference to the ARGB buffer (doesn't free anything).
|
|
|
|
static void PictureResetARGB(WebPPicture* const picture) {
|
|
|
|
picture->memory_argb_ = NULL;
|
|
|
|
picture->argb = NULL;
|
|
|
|
picture->argb_stride = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove reference to the YUVA buffer (doesn't free anything).
|
|
|
|
static void PictureResetYUVA(WebPPicture* const picture) {
|
|
|
|
picture->memory_ = NULL;
|
|
|
|
picture->y = picture->u = picture->v = picture->a = NULL;
|
|
|
|
picture->y_stride = picture->uv_stride = 0;
|
|
|
|
picture->a_stride = 0;
|
|
|
|
}
|
|
|
|
|
2011-05-03 02:19:00 +02:00
|
|
|
// Grab the 'specs' (writer, *opaque, width, height...) from 'src' and copy them
|
2012-06-28 09:34:23 +02:00
|
|
|
// into 'dst'. Mark 'dst' as not owning any memory.
|
2014-07-12 18:13:33 +02:00
|
|
|
void WebPPictureGrabSpecs(const WebPPicture* const src,
|
|
|
|
WebPPicture* const dst) {
|
2012-06-28 09:34:23 +02:00
|
|
|
assert(src != NULL && dst != NULL);
|
|
|
|
*dst = *src;
|
|
|
|
PictureResetYUVA(dst);
|
|
|
|
PictureResetARGB(dst);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate a new argb buffer, discarding any existing one and preserving
|
|
|
|
// the other YUV(A) buffer.
|
|
|
|
static int PictureAllocARGB(WebPPicture* const picture) {
|
|
|
|
WebPPicture tmp;
|
2014-03-27 23:27:32 +01:00
|
|
|
WebPSafeFree(picture->memory_argb_);
|
2012-06-28 09:34:23 +02:00
|
|
|
PictureResetARGB(picture);
|
2012-07-18 23:58:53 +02:00
|
|
|
picture->use_argb = 1;
|
2012-06-28 09:34:23 +02:00
|
|
|
WebPPictureGrabSpecs(picture, &tmp);
|
|
|
|
if (!WebPPictureAlloc(&tmp)) {
|
|
|
|
return WebPEncodingSetError(picture, VP8_ENC_ERROR_OUT_OF_MEMORY);
|
|
|
|
}
|
|
|
|
picture->memory_argb_ = tmp.memory_argb_;
|
|
|
|
picture->argb = tmp.argb;
|
|
|
|
picture->argb_stride = tmp.argb_stride;
|
|
|
|
return 1;
|
2011-05-03 02:19:00 +02:00
|
|
|
}
|
|
|
|
|
2012-06-28 09:34:23 +02:00
|
|
|
// Release memory owned by 'picture' (both YUV and ARGB buffers).
|
2012-07-18 00:01:30 +02:00
|
|
|
void WebPPictureFree(WebPPicture* picture) {
|
2012-01-23 09:58:09 +01:00
|
|
|
if (picture != NULL) {
|
2014-03-27 23:27:32 +01:00
|
|
|
WebPSafeFree(picture->memory_);
|
|
|
|
WebPSafeFree(picture->memory_argb_);
|
2012-06-28 09:34:23 +02:00
|
|
|
PictureResetYUVA(picture);
|
|
|
|
PictureResetARGB(picture);
|
2011-02-19 08:33:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-25 23:22:32 +02:00
|
|
|
//------------------------------------------------------------------------------
|
2012-06-19 00:42:56 +02:00
|
|
|
// WebPMemoryWriter: Write-to-memory
|
2011-02-19 08:33:46 +01:00
|
|
|
|
2012-07-18 00:01:30 +02:00
|
|
|
void WebPMemoryWriterInit(WebPMemoryWriter* writer) {
|
2012-06-19 00:42:56 +02:00
|
|
|
writer->mem = NULL;
|
|
|
|
writer->size = 0;
|
2011-02-19 08:33:46 +01:00
|
|
|
writer->max_size = 0;
|
|
|
|
}
|
|
|
|
|
2012-06-19 00:42:56 +02:00
|
|
|
int WebPMemoryWrite(const uint8_t* data, size_t data_size,
|
2012-07-18 00:01:30 +02:00
|
|
|
const WebPPicture* picture) {
|
2011-02-19 08:33:46 +01:00
|
|
|
WebPMemoryWriter* const w = (WebPMemoryWriter*)picture->custom_ptr;
|
2012-08-01 21:06:04 +02:00
|
|
|
uint64_t next_size;
|
2011-02-19 08:33:46 +01:00
|
|
|
if (w == NULL) {
|
|
|
|
return 1;
|
|
|
|
}
|
2012-08-01 21:06:04 +02:00
|
|
|
next_size = (uint64_t)w->size + data_size;
|
2011-02-19 08:33:46 +01:00
|
|
|
if (next_size > w->max_size) {
|
|
|
|
uint8_t* new_mem;
|
2012-08-01 21:06:04 +02:00
|
|
|
uint64_t next_max_size = 2ULL * w->max_size;
|
2011-02-19 08:33:46 +01:00
|
|
|
if (next_max_size < next_size) next_max_size = next_size;
|
2012-08-01 21:06:04 +02:00
|
|
|
if (next_max_size < 8192ULL) next_max_size = 8192ULL;
|
|
|
|
new_mem = (uint8_t*)WebPSafeMalloc(next_max_size, 1);
|
2011-02-19 08:33:46 +01:00
|
|
|
if (new_mem == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
2012-06-19 00:42:56 +02:00
|
|
|
if (w->size > 0) {
|
|
|
|
memcpy(new_mem, w->mem, w->size);
|
2011-02-19 08:33:46 +01:00
|
|
|
}
|
2014-03-27 23:27:32 +01:00
|
|
|
WebPSafeFree(w->mem);
|
2012-06-19 00:42:56 +02:00
|
|
|
w->mem = new_mem;
|
2012-08-01 21:06:04 +02:00
|
|
|
// down-cast is ok, thanks to WebPSafeMalloc
|
|
|
|
w->max_size = (size_t)next_max_size;
|
2011-02-19 08:33:46 +01:00
|
|
|
}
|
2012-01-23 09:58:09 +01:00
|
|
|
if (data_size > 0) {
|
2012-06-19 00:42:56 +02:00
|
|
|
memcpy(w->mem + w->size, data, data_size);
|
|
|
|
w->size += data_size;
|
2011-02-19 08:33:46 +01:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-03-27 23:27:32 +01:00
|
|
|
void WebPMemoryWriterClear(WebPMemoryWriter* writer) {
|
|
|
|
if (writer != NULL) {
|
|
|
|
WebPSafeFree(writer->mem);
|
|
|
|
writer->mem = NULL;
|
|
|
|
writer->size = 0;
|
|
|
|
writer->max_size = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-28 09:34:23 +02:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Detection of non-trivial transparency
|
|
|
|
|
|
|
|
// Returns true if alpha[] has non-0xff values.
|
|
|
|
static int CheckNonOpaque(const uint8_t* alpha, int width, int height,
|
|
|
|
int x_step, int y_step) {
|
|
|
|
if (alpha == NULL) return 0;
|
|
|
|
while (height-- > 0) {
|
|
|
|
int x;
|
|
|
|
for (x = 0; x < width * x_step; x += x_step) {
|
|
|
|
if (alpha[x] != 0xff) return 1; // TODO(skal): check 4/8 bytes at a time.
|
|
|
|
}
|
|
|
|
alpha += y_step;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checking for the presence of non-opaque alpha.
|
2012-07-18 00:01:30 +02:00
|
|
|
int WebPPictureHasTransparency(const WebPPicture* picture) {
|
2012-06-28 09:34:23 +02:00
|
|
|
if (picture == NULL) return 0;
|
2012-07-18 23:58:53 +02:00
|
|
|
if (!picture->use_argb) {
|
2012-06-28 09:34:23 +02:00
|
|
|
return CheckNonOpaque(picture->a, picture->width, picture->height,
|
|
|
|
1, picture->a_stride);
|
|
|
|
} else {
|
|
|
|
int x, y;
|
|
|
|
const uint32_t* argb = picture->argb;
|
|
|
|
if (argb == NULL) return 0;
|
|
|
|
for (y = 0; y < picture->height; ++y) {
|
|
|
|
for (x = 0; x < picture->width; ++x) {
|
|
|
|
if (argb[x] < 0xff000000u) return 1; // test any alpha values != 0xff
|
|
|
|
}
|
|
|
|
argb += picture->argb_stride;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-08-25 23:22:32 +02:00
|
|
|
//------------------------------------------------------------------------------
|
2011-02-19 08:33:46 +01:00
|
|
|
// RGB -> YUV conversion
|
|
|
|
|
2013-10-17 22:36:49 +02:00
|
|
|
static int RGBToY(int r, int g, int b, VP8Random* const rg) {
|
2013-10-30 10:00:33 +01:00
|
|
|
return VP8RGBToY(r, g, b, VP8RandomBits(rg, YUV_FIX));
|
2013-10-17 22:36:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int RGBToU(int r, int g, int b, VP8Random* const rg) {
|
2013-10-30 10:00:33 +01:00
|
|
|
return VP8RGBToU(r, g, b, VP8RandomBits(rg, YUV_FIX + 2));
|
2013-10-17 22:36:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int RGBToV(int r, int g, int b, VP8Random* const rg) {
|
2013-10-30 10:00:33 +01:00
|
|
|
return VP8RGBToV(r, g, b, VP8RandomBits(rg, YUV_FIX + 2));
|
2013-10-17 22:36:49 +02:00
|
|
|
}
|
|
|
|
|
2013-10-18 21:28:05 +02:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#if defined(USE_GAMMA_COMPRESSION)
|
|
|
|
|
|
|
|
// gamma-compensates loss of resolution during chroma subsampling
|
|
|
|
#define kGamma 0.80
|
|
|
|
#define kGammaFix 12 // fixed-point precision for linear values
|
|
|
|
#define kGammaScale ((1 << kGammaFix) - 1)
|
|
|
|
#define kGammaTabFix 7 // fixed-point fractional bits precision
|
|
|
|
#define kGammaTabScale (1 << kGammaTabFix)
|
|
|
|
#define kGammaTabRounder (kGammaTabScale >> 1)
|
|
|
|
#define kGammaTabSize (1 << (kGammaFix - kGammaTabFix))
|
|
|
|
|
|
|
|
static int kLinearToGammaTab[kGammaTabSize + 1];
|
|
|
|
static uint16_t kGammaToLinearTab[256];
|
|
|
|
static int kGammaTablesOk = 0;
|
|
|
|
|
|
|
|
static void InitGammaTables(void) {
|
|
|
|
if (!kGammaTablesOk) {
|
|
|
|
int v;
|
|
|
|
const double scale = 1. / kGammaScale;
|
|
|
|
for (v = 0; v <= 255; ++v) {
|
|
|
|
kGammaToLinearTab[v] =
|
|
|
|
(uint16_t)(pow(v / 255., kGamma) * kGammaScale + .5);
|
|
|
|
}
|
|
|
|
for (v = 0; v <= kGammaTabSize; ++v) {
|
|
|
|
const double x = scale * (v << kGammaTabFix);
|
|
|
|
kLinearToGammaTab[v] = (int)(pow(x, 1. / kGamma) * 255. + .5);
|
|
|
|
}
|
|
|
|
kGammaTablesOk = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static WEBP_INLINE uint32_t GammaToLinear(uint8_t v) {
|
|
|
|
return kGammaToLinearTab[v];
|
|
|
|
}
|
|
|
|
|
2013-11-19 11:21:10 +01:00
|
|
|
// Convert a linear value 'v' to YUV_FIX+2 fixed-point precision
|
|
|
|
// U/V value, suitable for RGBToU/V calls.
|
|
|
|
static WEBP_INLINE int LinearToGamma(uint32_t base_value, int shift) {
|
|
|
|
const int v = base_value << shift; // final uplifted value
|
|
|
|
const int tab_pos = v >> (kGammaTabFix + 2); // integer part
|
|
|
|
const int x = v & ((kGammaTabScale << 2) - 1); // fractional part
|
2013-10-18 21:28:05 +02:00
|
|
|
const int v0 = kLinearToGammaTab[tab_pos];
|
|
|
|
const int v1 = kLinearToGammaTab[tab_pos + 1];
|
2013-11-19 11:21:10 +01:00
|
|
|
const int y = v1 * x + v0 * ((kGammaTabScale << 2) - x); // interpolate
|
|
|
|
return (y + kGammaTabRounder) >> kGammaTabFix; // descale
|
2013-10-18 21:28:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
static void InitGammaTables(void) {}
|
|
|
|
static WEBP_INLINE uint32_t GammaToLinear(uint8_t v) { return v; }
|
2013-11-19 11:21:10 +01:00
|
|
|
static WEBP_INLINE int LinearToGamma(uint32_t base_value, int shift) {
|
2014-06-27 17:02:24 +02:00
|
|
|
return (int)(base_value << shift);
|
2013-10-18 21:28:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // USE_GAMMA_COMPRESSION
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#define SUM4(ptr) LinearToGamma( \
|
|
|
|
GammaToLinear((ptr)[0]) + \
|
|
|
|
GammaToLinear((ptr)[step]) + \
|
|
|
|
GammaToLinear((ptr)[rgb_stride]) + \
|
2013-11-19 11:21:10 +01:00
|
|
|
GammaToLinear((ptr)[rgb_stride + step]), 0) \
|
2013-10-18 21:28:05 +02:00
|
|
|
|
|
|
|
#define SUM2H(ptr) \
|
|
|
|
LinearToGamma(GammaToLinear((ptr)[0]) + GammaToLinear((ptr)[step]), 1)
|
|
|
|
#define SUM2V(ptr) \
|
|
|
|
LinearToGamma(GammaToLinear((ptr)[0]) + GammaToLinear((ptr)[rgb_stride]), 1)
|
2013-11-19 11:21:10 +01:00
|
|
|
#define SUM1(ptr) \
|
|
|
|
LinearToGamma(GammaToLinear((ptr)[0]), 2)
|
2013-10-18 21:28:05 +02:00
|
|
|
|
2011-02-19 08:33:46 +01:00
|
|
|
#define RGB_TO_UV(x, y, SUM) { \
|
|
|
|
const int src = (2 * (step * (x) + (y) * rgb_stride)); \
|
|
|
|
const int dst = (x) + (y) * picture->uv_stride; \
|
|
|
|
const int r = SUM(r_ptr + src); \
|
|
|
|
const int g = SUM(g_ptr + src); \
|
|
|
|
const int b = SUM(b_ptr + src); \
|
2013-10-17 22:36:49 +02:00
|
|
|
picture->u[dst] = RGBToU(r, g, b, &rg); \
|
|
|
|
picture->v[dst] = RGBToV(r, g, b, &rg); \
|
2011-02-19 08:33:46 +01:00
|
|
|
}
|
|
|
|
|
2012-06-28 09:34:23 +02:00
|
|
|
static int ImportYUVAFromRGBA(const uint8_t* const r_ptr,
|
|
|
|
const uint8_t* const g_ptr,
|
|
|
|
const uint8_t* const b_ptr,
|
|
|
|
const uint8_t* const a_ptr,
|
|
|
|
int step, // bytes per pixel
|
|
|
|
int rgb_stride, // bytes per scanline
|
2013-10-17 22:36:49 +02:00
|
|
|
float dithering,
|
2012-06-28 09:34:23 +02:00
|
|
|
WebPPicture* const picture) {
|
2011-05-03 02:19:00 +02:00
|
|
|
const WebPEncCSP uv_csp = picture->colorspace & WEBP_CSP_UV_MASK;
|
2011-02-19 08:33:46 +01:00
|
|
|
int x, y;
|
2011-05-03 02:19:00 +02:00
|
|
|
const int width = picture->width;
|
|
|
|
const int height = picture->height;
|
2012-06-28 09:34:23 +02:00
|
|
|
const int has_alpha = CheckNonOpaque(a_ptr, width, height, step, rgb_stride);
|
2013-10-17 22:36:49 +02:00
|
|
|
VP8Random rg;
|
2011-02-19 08:33:46 +01:00
|
|
|
|
2012-06-28 09:34:23 +02:00
|
|
|
picture->colorspace = uv_csp;
|
2012-07-18 23:58:53 +02:00
|
|
|
picture->use_argb = 0;
|
2012-06-28 09:34:23 +02:00
|
|
|
if (has_alpha) {
|
2012-06-05 00:55:31 +02:00
|
|
|
picture->colorspace |= WEBP_CSP_ALPHA_BIT;
|
|
|
|
}
|
|
|
|
if (!WebPPictureAlloc(picture)) return 0;
|
|
|
|
|
2013-10-30 10:00:33 +01:00
|
|
|
VP8InitRandom(&rg, dithering);
|
2013-10-18 21:28:05 +02:00
|
|
|
InitGammaTables();
|
2013-10-17 22:36:49 +02:00
|
|
|
|
2012-06-28 09:34:23 +02:00
|
|
|
// Import luma plane
|
|
|
|
for (y = 0; y < height; ++y) {
|
|
|
|
for (x = 0; x < width; ++x) {
|
|
|
|
const int offset = step * x + y * rgb_stride;
|
|
|
|
picture->y[x + y * picture->y_stride] =
|
2013-10-17 22:36:49 +02:00
|
|
|
RGBToY(r_ptr[offset], g_ptr[offset], b_ptr[offset], &rg);
|
2011-02-19 08:33:46 +01:00
|
|
|
}
|
2012-06-28 09:34:23 +02:00
|
|
|
}
|
2012-03-28 13:07:42 +02:00
|
|
|
|
2012-06-28 09:34:23 +02:00
|
|
|
// Downsample U/V plane
|
2014-07-03 22:20:06 +02:00
|
|
|
for (y = 0; y < (height >> 1); ++y) {
|
|
|
|
for (x = 0; x < (width >> 1); ++x) {
|
|
|
|
RGB_TO_UV(x, y, SUM4);
|
2012-06-28 09:34:23 +02:00
|
|
|
}
|
2014-07-03 22:20:06 +02:00
|
|
|
if (width & 1) {
|
|
|
|
RGB_TO_UV(x, y, SUM2V);
|
2012-06-28 09:34:23 +02:00
|
|
|
}
|
2014-07-03 22:20:06 +02:00
|
|
|
}
|
|
|
|
if (height & 1) {
|
|
|
|
for (x = 0; x < (width >> 1); ++x) {
|
|
|
|
RGB_TO_UV(x, y, SUM2H);
|
|
|
|
}
|
|
|
|
if (width & 1) {
|
|
|
|
RGB_TO_UV(x, y, SUM1);
|
2012-03-28 13:07:42 +02:00
|
|
|
}
|
2012-06-28 09:34:23 +02:00
|
|
|
}
|
2012-03-28 13:07:42 +02:00
|
|
|
|
2012-06-28 09:34:23 +02:00
|
|
|
if (has_alpha) {
|
|
|
|
assert(step >= 4);
|
2013-09-12 09:32:28 +02:00
|
|
|
assert(picture->a != NULL);
|
2012-06-28 09:34:23 +02:00
|
|
|
for (y = 0; y < height; ++y) {
|
|
|
|
for (x = 0; x < width; ++x) {
|
|
|
|
picture->a[x + y * picture->a_stride] =
|
2012-03-28 13:07:42 +02:00
|
|
|
a_ptr[step * x + y * rgb_stride];
|
2011-05-03 02:19:00 +02:00
|
|
|
}
|
2011-02-19 08:33:46 +01:00
|
|
|
}
|
2012-06-28 09:34:23 +02:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int Import(WebPPicture* const picture,
|
|
|
|
const uint8_t* const rgb, int rgb_stride,
|
|
|
|
int step, int swap_rb, int import_alpha) {
|
|
|
|
const uint8_t* const r_ptr = rgb + (swap_rb ? 2 : 0);
|
|
|
|
const uint8_t* const g_ptr = rgb + 1;
|
|
|
|
const uint8_t* const b_ptr = rgb + (swap_rb ? 0 : 2);
|
|
|
|
const uint8_t* const a_ptr = import_alpha ? rgb + 3 : NULL;
|
|
|
|
const int width = picture->width;
|
|
|
|
const int height = picture->height;
|
|
|
|
|
2012-07-18 23:58:53 +02:00
|
|
|
if (!picture->use_argb) {
|
2012-06-28 09:34:23 +02:00
|
|
|
return ImportYUVAFromRGBA(r_ptr, g_ptr, b_ptr, a_ptr, step, rgb_stride,
|
2013-10-17 22:36:49 +02:00
|
|
|
0.f /* no dithering */, picture);
|
2012-06-28 09:34:23 +02:00
|
|
|
}
|
|
|
|
if (import_alpha) {
|
|
|
|
picture->colorspace |= WEBP_CSP_ALPHA_BIT;
|
2011-05-03 02:19:00 +02:00
|
|
|
} else {
|
2012-06-28 09:34:23 +02:00
|
|
|
picture->colorspace &= ~WEBP_CSP_ALPHA_BIT;
|
|
|
|
}
|
|
|
|
if (!WebPPictureAlloc(picture)) return 0;
|
|
|
|
|
|
|
|
if (!import_alpha) {
|
|
|
|
int x, y;
|
|
|
|
for (y = 0; y < height; ++y) {
|
|
|
|
for (x = 0; x < width; ++x) {
|
|
|
|
const int offset = step * x + y * rgb_stride;
|
|
|
|
const uint32_t argb =
|
2013-04-03 04:14:14 +02:00
|
|
|
MakeARGB32(r_ptr[offset], g_ptr[offset], b_ptr[offset]);
|
2012-06-28 09:34:23 +02:00
|
|
|
picture->argb[x + y * picture->argb_stride] = argb;
|
2012-03-28 13:07:42 +02:00
|
|
|
}
|
2012-06-28 09:34:23 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
int x, y;
|
|
|
|
assert(step >= 4);
|
|
|
|
for (y = 0; y < height; ++y) {
|
|
|
|
for (x = 0; x < width; ++x) {
|
|
|
|
const int offset = step * x + y * rgb_stride;
|
2013-04-13 19:33:41 +02:00
|
|
|
const uint32_t argb = ((uint32_t)a_ptr[offset] << 24) |
|
2012-06-28 09:34:23 +02:00
|
|
|
(r_ptr[offset] << 16) |
|
|
|
|
(g_ptr[offset] << 8) |
|
|
|
|
(b_ptr[offset]);
|
|
|
|
picture->argb[x + y * picture->argb_stride] = argb;
|
EXPERIMENTAL: add support for alpha channel
This is a (minor) bitstream change: if the 'color_space' bit is set to '1'
(which is normally an undefined/invalid behaviour), we add extra data at the
end of partition #0 (so-called 'extensions')
Namely, we add the size of the extension data as 3 bytes (little-endian),
followed by a set of bits telling which extensions we're incorporating.
The data then _preceeds_ this trailing tags.
This is all experimental, and you'll need to have
'#define WEBP_EXPERIMENTAL_FEATURES' in webp/types.h to enable this code
(at your own risk! :))
Still, this hack produces almost-valid WebP file for decoders that don't
check this color_space bit. In particular, previous 'dwebp' (and for instance
Chrome) will recognize this files and decode them, but without the alpha
of course. Other decoder will just see random extra stuff at the end of
partition #0.
To experiment with the alpha-channel, you need to compile on Unix platform
and use PNGs for input/output.
If 'alpha.png' is a source with alpha channel, then you can try (on Unix):
cwebp alpha.png -o alpha.webp
dwebp alpha.webp -o test.png
cwebp now has a '-noalpha' flag to ignore any alpha information from the
source, if present.
More hacking and experimenting welcome!
Change-Id: I3c7b1fd8411c9e7a9f77690e898479ad85c52f3e
2011-04-26 01:58:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-02-19 08:33:46 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#undef SUM4
|
|
|
|
#undef SUM2V
|
|
|
|
#undef SUM2H
|
|
|
|
#undef SUM1
|
|
|
|
#undef RGB_TO_UV
|
|
|
|
|
2014-07-12 18:13:33 +02:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2012-07-18 00:01:30 +02:00
|
|
|
int WebPPictureImportRGB(WebPPicture* picture,
|
|
|
|
const uint8_t* rgb, int rgb_stride) {
|
2011-04-26 16:23:57 +02:00
|
|
|
return Import(picture, rgb, rgb_stride, 3, 0, 0);
|
2011-02-19 08:33:46 +01:00
|
|
|
}
|
|
|
|
|
2012-07-18 00:01:30 +02:00
|
|
|
int WebPPictureImportBGR(WebPPicture* picture,
|
|
|
|
const uint8_t* rgb, int rgb_stride) {
|
2011-04-26 16:23:57 +02:00
|
|
|
return Import(picture, rgb, rgb_stride, 3, 1, 0);
|
2011-02-19 08:33:46 +01:00
|
|
|
}
|
|
|
|
|
2012-07-18 00:01:30 +02:00
|
|
|
int WebPPictureImportRGBA(WebPPicture* picture,
|
|
|
|
const uint8_t* rgba, int rgba_stride) {
|
2011-04-26 16:23:57 +02:00
|
|
|
return Import(picture, rgba, rgba_stride, 4, 0, 1);
|
2011-02-19 08:33:46 +01:00
|
|
|
}
|
|
|
|
|
2012-07-18 00:01:30 +02:00
|
|
|
int WebPPictureImportBGRA(WebPPicture* picture,
|
|
|
|
const uint8_t* rgba, int rgba_stride) {
|
2011-04-26 16:23:57 +02:00
|
|
|
return Import(picture, rgba, rgba_stride, 4, 1, 1);
|
2011-02-19 08:33:46 +01:00
|
|
|
}
|
|
|
|
|
2012-07-18 00:01:30 +02:00
|
|
|
int WebPPictureImportRGBX(WebPPicture* picture,
|
|
|
|
const uint8_t* rgba, int rgba_stride) {
|
2012-06-05 09:26:17 +02:00
|
|
|
return Import(picture, rgba, rgba_stride, 4, 0, 0);
|
|
|
|
}
|
|
|
|
|
2012-07-18 00:01:30 +02:00
|
|
|
int WebPPictureImportBGRX(WebPPicture* picture,
|
|
|
|
const uint8_t* rgba, int rgba_stride) {
|
2012-06-05 09:26:17 +02:00
|
|
|
return Import(picture, rgba, rgba_stride, 4, 1, 0);
|
|
|
|
}
|
|
|
|
|
2012-06-28 09:34:23 +02:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Automatic YUV <-> ARGB conversions.
|
|
|
|
|
2012-07-18 00:01:30 +02:00
|
|
|
int WebPPictureYUVAToARGB(WebPPicture* picture) {
|
2012-06-28 09:34:23 +02:00
|
|
|
if (picture == NULL) return 0;
|
2013-12-02 15:23:12 +01:00
|
|
|
if (picture->y == NULL || picture->u == NULL || picture->v == NULL) {
|
2012-06-28 09:34:23 +02:00
|
|
|
return WebPEncodingSetError(picture, VP8_ENC_ERROR_NULL_PARAMETER);
|
|
|
|
}
|
|
|
|
if ((picture->colorspace & WEBP_CSP_ALPHA_BIT) && picture->a == NULL) {
|
|
|
|
return WebPEncodingSetError(picture, VP8_ENC_ERROR_NULL_PARAMETER);
|
|
|
|
}
|
|
|
|
if ((picture->colorspace & WEBP_CSP_UV_MASK) != WEBP_YUV420) {
|
|
|
|
return WebPEncodingSetError(picture, VP8_ENC_ERROR_INVALID_CONFIGURATION);
|
|
|
|
}
|
|
|
|
// Allocate a new argb buffer (discarding the previous one).
|
|
|
|
if (!PictureAllocARGB(picture)) return 0;
|
|
|
|
|
|
|
|
// Convert
|
|
|
|
{
|
|
|
|
int y;
|
|
|
|
const int width = picture->width;
|
|
|
|
const int height = picture->height;
|
|
|
|
const int argb_stride = 4 * picture->argb_stride;
|
|
|
|
uint8_t* dst = (uint8_t*)picture->argb;
|
|
|
|
const uint8_t *cur_u = picture->u, *cur_v = picture->v, *cur_y = picture->y;
|
|
|
|
WebPUpsampleLinePairFunc upsample = WebPGetLinePairConverter(ALPHA_IS_LAST);
|
|
|
|
|
|
|
|
// First row, with replicated top samples.
|
2013-08-19 21:40:25 +02:00
|
|
|
upsample(cur_y, NULL, cur_u, cur_v, cur_u, cur_v, dst, NULL, width);
|
2012-06-28 09:34:23 +02:00
|
|
|
cur_y += picture->y_stride;
|
|
|
|
dst += argb_stride;
|
|
|
|
// Center rows.
|
|
|
|
for (y = 1; y + 1 < height; y += 2) {
|
|
|
|
const uint8_t* const top_u = cur_u;
|
|
|
|
const uint8_t* const top_v = cur_v;
|
|
|
|
cur_u += picture->uv_stride;
|
|
|
|
cur_v += picture->uv_stride;
|
|
|
|
upsample(cur_y, cur_y + picture->y_stride, top_u, top_v, cur_u, cur_v,
|
|
|
|
dst, dst + argb_stride, width);
|
|
|
|
cur_y += 2 * picture->y_stride;
|
|
|
|
dst += 2 * argb_stride;
|
|
|
|
}
|
|
|
|
// Last row (if needed), with replicated bottom samples.
|
|
|
|
if (height > 1 && !(height & 1)) {
|
|
|
|
upsample(cur_y, NULL, cur_u, cur_v, cur_u, cur_v, dst, NULL, width);
|
|
|
|
}
|
|
|
|
// Insert alpha values if needed, in replacement for the default 0xff ones.
|
|
|
|
if (picture->colorspace & WEBP_CSP_ALPHA_BIT) {
|
|
|
|
for (y = 0; y < height; ++y) {
|
2013-01-21 17:20:14 +01:00
|
|
|
uint32_t* const argb_dst = picture->argb + y * picture->argb_stride;
|
2012-06-28 09:34:23 +02:00
|
|
|
const uint8_t* const src = picture->a + y * picture->a_stride;
|
|
|
|
int x;
|
|
|
|
for (x = 0; x < width; ++x) {
|
2013-04-13 19:33:41 +02:00
|
|
|
argb_dst[x] = (argb_dst[x] & 0x00ffffffu) | ((uint32_t)src[x] << 24);
|
2012-06-28 09:34:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-10-17 22:36:49 +02:00
|
|
|
int WebPPictureARGBToYUVADithered(WebPPicture* picture, WebPEncCSP colorspace,
|
|
|
|
float dithering) {
|
2012-06-28 09:34:23 +02:00
|
|
|
if (picture == NULL) return 0;
|
|
|
|
if (picture->argb == NULL) {
|
|
|
|
return WebPEncodingSetError(picture, VP8_ENC_ERROR_NULL_PARAMETER);
|
|
|
|
} else {
|
|
|
|
const uint8_t* const argb = (const uint8_t*)picture->argb;
|
|
|
|
const uint8_t* const r = ALPHA_IS_LAST ? argb + 2 : argb + 1;
|
|
|
|
const uint8_t* const g = ALPHA_IS_LAST ? argb + 1 : argb + 2;
|
|
|
|
const uint8_t* const b = ALPHA_IS_LAST ? argb + 0 : argb + 3;
|
|
|
|
const uint8_t* const a = ALPHA_IS_LAST ? argb + 3 : argb + 0;
|
|
|
|
// We work on a tmp copy of 'picture', because ImportYUVAFromRGBA()
|
|
|
|
// would be calling WebPPictureFree(picture) otherwise.
|
|
|
|
WebPPicture tmp = *picture;
|
|
|
|
PictureResetARGB(&tmp); // reset ARGB buffer so that it's not free()'d.
|
2012-07-18 23:58:53 +02:00
|
|
|
tmp.use_argb = 0;
|
2012-06-28 09:34:23 +02:00
|
|
|
tmp.colorspace = colorspace & WEBP_CSP_UV_MASK;
|
2013-10-17 22:36:49 +02:00
|
|
|
if (!ImportYUVAFromRGBA(r, g, b, a, 4, 4 * picture->argb_stride, dithering,
|
|
|
|
&tmp)) {
|
2012-06-28 09:34:23 +02:00
|
|
|
return WebPEncodingSetError(picture, VP8_ENC_ERROR_OUT_OF_MEMORY);
|
|
|
|
}
|
|
|
|
// Copy back the YUV specs into 'picture'.
|
|
|
|
tmp.argb = picture->argb;
|
|
|
|
tmp.argb_stride = picture->argb_stride;
|
|
|
|
tmp.memory_argb_ = picture->memory_argb_;
|
|
|
|
*picture = tmp;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-10-17 22:36:49 +02:00
|
|
|
int WebPPictureARGBToYUVA(WebPPicture* picture, WebPEncCSP colorspace) {
|
|
|
|
return WebPPictureARGBToYUVADithered(picture, colorspace, 0.f);
|
|
|
|
}
|
|
|
|
|
2012-01-20 16:20:56 +01:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Simplest high-level calls:
|
2011-02-19 08:33:46 +01:00
|
|
|
|
|
|
|
typedef int (*Importer)(WebPPicture* const, const uint8_t* const, int);
|
|
|
|
|
EXPERIMENTAL: add support for alpha channel
This is a (minor) bitstream change: if the 'color_space' bit is set to '1'
(which is normally an undefined/invalid behaviour), we add extra data at the
end of partition #0 (so-called 'extensions')
Namely, we add the size of the extension data as 3 bytes (little-endian),
followed by a set of bits telling which extensions we're incorporating.
The data then _preceeds_ this trailing tags.
This is all experimental, and you'll need to have
'#define WEBP_EXPERIMENTAL_FEATURES' in webp/types.h to enable this code
(at your own risk! :))
Still, this hack produces almost-valid WebP file for decoders that don't
check this color_space bit. In particular, previous 'dwebp' (and for instance
Chrome) will recognize this files and decode them, but without the alpha
of course. Other decoder will just see random extra stuff at the end of
partition #0.
To experiment with the alpha-channel, you need to compile on Unix platform
and use PNGs for input/output.
If 'alpha.png' is a source with alpha channel, then you can try (on Unix):
cwebp alpha.png -o alpha.webp
dwebp alpha.webp -o test.png
cwebp now has a '-noalpha' flag to ignore any alpha information from the
source, if present.
More hacking and experimenting welcome!
Change-Id: I3c7b1fd8411c9e7a9f77690e898479ad85c52f3e
2011-04-26 01:58:04 +02:00
|
|
|
static size_t Encode(const uint8_t* rgba, int width, int height, int stride,
|
2012-07-17 20:56:24 +02:00
|
|
|
Importer import, float quality_factor, int lossless,
|
|
|
|
uint8_t** output) {
|
2011-02-19 08:33:46 +01:00
|
|
|
WebPPicture pic;
|
|
|
|
WebPConfig config;
|
|
|
|
WebPMemoryWriter wrt;
|
|
|
|
int ok;
|
|
|
|
|
|
|
|
if (!WebPConfigPreset(&config, WEBP_PRESET_DEFAULT, quality_factor) ||
|
|
|
|
!WebPPictureInit(&pic)) {
|
|
|
|
return 0; // shouldn't happen, except if system installation is broken
|
|
|
|
}
|
|
|
|
|
2012-07-17 20:56:24 +02:00
|
|
|
config.lossless = !!lossless;
|
2012-07-18 23:58:53 +02:00
|
|
|
pic.use_argb = !!lossless;
|
2011-02-19 08:33:46 +01:00
|
|
|
pic.width = width;
|
|
|
|
pic.height = height;
|
|
|
|
pic.writer = WebPMemoryWrite;
|
|
|
|
pic.custom_ptr = &wrt;
|
2012-01-23 09:58:09 +01:00
|
|
|
WebPMemoryWriterInit(&wrt);
|
2011-02-19 08:33:46 +01:00
|
|
|
|
EXPERIMENTAL: add support for alpha channel
This is a (minor) bitstream change: if the 'color_space' bit is set to '1'
(which is normally an undefined/invalid behaviour), we add extra data at the
end of partition #0 (so-called 'extensions')
Namely, we add the size of the extension data as 3 bytes (little-endian),
followed by a set of bits telling which extensions we're incorporating.
The data then _preceeds_ this trailing tags.
This is all experimental, and you'll need to have
'#define WEBP_EXPERIMENTAL_FEATURES' in webp/types.h to enable this code
(at your own risk! :))
Still, this hack produces almost-valid WebP file for decoders that don't
check this color_space bit. In particular, previous 'dwebp' (and for instance
Chrome) will recognize this files and decode them, but without the alpha
of course. Other decoder will just see random extra stuff at the end of
partition #0.
To experiment with the alpha-channel, you need to compile on Unix platform
and use PNGs for input/output.
If 'alpha.png' is a source with alpha channel, then you can try (on Unix):
cwebp alpha.png -o alpha.webp
dwebp alpha.webp -o test.png
cwebp now has a '-noalpha' flag to ignore any alpha information from the
source, if present.
More hacking and experimenting welcome!
Change-Id: I3c7b1fd8411c9e7a9f77690e898479ad85c52f3e
2011-04-26 01:58:04 +02:00
|
|
|
ok = import(&pic, rgba, stride) && WebPEncode(&config, &pic);
|
2011-02-19 08:33:46 +01:00
|
|
|
WebPPictureFree(&pic);
|
|
|
|
if (!ok) {
|
2014-03-27 23:27:32 +01:00
|
|
|
WebPMemoryWriterClear(&wrt);
|
2011-02-19 08:33:46 +01:00
|
|
|
*output = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
2012-06-19 00:42:56 +02:00
|
|
|
*output = wrt.mem;
|
|
|
|
return wrt.size;
|
2011-02-19 08:33:46 +01:00
|
|
|
}
|
|
|
|
|
2012-07-17 20:56:24 +02:00
|
|
|
#define ENCODE_FUNC(NAME, IMPORTER) \
|
|
|
|
size_t NAME(const uint8_t* in, int w, int h, int bps, float q, \
|
|
|
|
uint8_t** out) { \
|
|
|
|
return Encode(in, w, h, bps, IMPORTER, q, 0, out); \
|
2011-02-19 08:33:46 +01:00
|
|
|
}
|
|
|
|
|
2013-04-13 19:49:35 +02:00
|
|
|
ENCODE_FUNC(WebPEncodeRGB, WebPPictureImportRGB)
|
|
|
|
ENCODE_FUNC(WebPEncodeBGR, WebPPictureImportBGR)
|
|
|
|
ENCODE_FUNC(WebPEncodeRGBA, WebPPictureImportRGBA)
|
|
|
|
ENCODE_FUNC(WebPEncodeBGRA, WebPPictureImportBGRA)
|
2011-02-19 08:33:46 +01:00
|
|
|
|
|
|
|
#undef ENCODE_FUNC
|
|
|
|
|
2012-07-17 20:56:24 +02:00
|
|
|
#define LOSSLESS_DEFAULT_QUALITY 70.
|
|
|
|
#define LOSSLESS_ENCODE_FUNC(NAME, IMPORTER) \
|
|
|
|
size_t NAME(const uint8_t* in, int w, int h, int bps, uint8_t** out) { \
|
|
|
|
return Encode(in, w, h, bps, IMPORTER, LOSSLESS_DEFAULT_QUALITY, 1, out); \
|
|
|
|
}
|
|
|
|
|
2013-04-13 19:49:35 +02:00
|
|
|
LOSSLESS_ENCODE_FUNC(WebPEncodeLosslessRGB, WebPPictureImportRGB)
|
|
|
|
LOSSLESS_ENCODE_FUNC(WebPEncodeLosslessBGR, WebPPictureImportBGR)
|
|
|
|
LOSSLESS_ENCODE_FUNC(WebPEncodeLosslessRGBA, WebPPictureImportRGBA)
|
|
|
|
LOSSLESS_ENCODE_FUNC(WebPEncodeLosslessBGRA, WebPPictureImportBGRA)
|
2012-07-17 20:56:24 +02:00
|
|
|
|
|
|
|
#undef LOSSLESS_ENCODE_FUNC
|
|
|
|
|
2011-08-25 23:22:32 +02:00
|
|
|
//------------------------------------------------------------------------------
|