2012-01-06 23:49:06 +01:00
|
|
|
// Copyright 2011 Google Inc. All Rights Reserved.
|
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
|
|
|
//
|
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.
|
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
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Alpha-plane compression.
|
|
|
|
//
|
|
|
|
// Author: Skal (pascal.massimino@gmail.com)
|
|
|
|
|
2011-05-03 02:19:00 +02:00
|
|
|
#include <assert.h>
|
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
|
|
|
#include <stdlib.h>
|
2011-12-01 11:00:03 +01:00
|
|
|
|
2017-01-19 09:21:40 +01:00
|
|
|
#include "./vp8i_enc.h"
|
2015-01-28 08:02:41 +01:00
|
|
|
#include "../dsp/dsp.h"
|
2017-01-19 09:21:40 +01:00
|
|
|
#include "../utils/filters_utils.h"
|
|
|
|
#include "../utils/quant_levels_utils.h"
|
2014-03-27 23:27:32 +01:00
|
|
|
#include "../utils/utils.h"
|
2012-05-24 11:21:37 +02:00
|
|
|
#include "../webp/format_constants.h"
|
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
|
|
|
|
2012-05-21 15:22:06 +02:00
|
|
|
// -----------------------------------------------------------------------------
|
2012-07-25 01:15:36 +02:00
|
|
|
// Encodes the given alpha data via specified compression method 'method'.
|
|
|
|
// The pre-processing (quantization) is performed if 'quality' is less than 100.
|
|
|
|
// For such cases, the encoding is lossy. The valid range is [0, 100] for
|
|
|
|
// 'quality' and [0, 1] for 'method':
|
2012-05-21 15:22:06 +02:00
|
|
|
// 'method = 0' - No compression;
|
|
|
|
// 'method = 1' - Use lossless coder on the alpha plane only
|
|
|
|
// 'filter' values [0, 4] correspond to prediction modes none, horizontal,
|
|
|
|
// vertical & gradient filters. The prediction mode 4 will try all the
|
2012-07-25 01:15:36 +02:00
|
|
|
// prediction modes 0 to 3 and pick the best one.
|
2012-05-24 10:54:12 +02:00
|
|
|
// 'effort_level': specifies how much effort must be spent to try and reduce
|
|
|
|
// the compressed output size. In range 0 (quick) to 6 (slow).
|
|
|
|
//
|
2012-05-21 15:22:06 +02:00
|
|
|
// 'output' corresponds to the buffer containing compressed alpha data.
|
|
|
|
// This buffer is allocated by this method and caller should call
|
2014-03-27 23:27:32 +01:00
|
|
|
// WebPSafeFree(*output) when done.
|
2012-05-21 15:22:06 +02:00
|
|
|
// 'output_size' corresponds to size of this compressed alpha buffer.
|
|
|
|
//
|
|
|
|
// Returns 1 on successfully encoding the alpha and
|
|
|
|
// 0 if either:
|
|
|
|
// invalid quality or method, or
|
|
|
|
// memory allocation for the compressed data fails.
|
|
|
|
|
2017-01-19 09:21:40 +01:00
|
|
|
#include "../enc/vp8li_enc.h"
|
2012-05-21 15:22:06 +02:00
|
|
|
|
2012-07-25 01:15:36 +02:00
|
|
|
static int EncodeLossless(const uint8_t* const data, int width, int height,
|
2012-05-24 10:54:12 +02:00
|
|
|
int effort_level, // in [0..6] range
|
2014-09-02 21:55:13 +02:00
|
|
|
VP8LBitWriter* const bw,
|
2012-07-25 01:15:36 +02:00
|
|
|
WebPAuxStats* const stats) {
|
2012-05-21 15:22:06 +02:00
|
|
|
int ok = 0;
|
|
|
|
WebPConfig config;
|
|
|
|
WebPPicture picture;
|
|
|
|
|
|
|
|
WebPPictureInit(&picture);
|
|
|
|
picture.width = width;
|
|
|
|
picture.height = height;
|
2012-07-18 23:58:53 +02:00
|
|
|
picture.use_argb = 1;
|
2012-07-25 01:15:36 +02:00
|
|
|
picture.stats = stats;
|
2012-05-21 15:22:06 +02:00
|
|
|
if (!WebPPictureAlloc(&picture)) return 0;
|
|
|
|
|
2012-05-23 17:01:44 +02:00
|
|
|
// Transfer the alpha values to the green channel.
|
2014-10-06 23:15:44 +02:00
|
|
|
WebPDispatchAlphaToGreen(data, width, picture.width, picture.height,
|
|
|
|
picture.argb, picture.argb_stride);
|
2012-05-21 15:22:06 +02:00
|
|
|
|
|
|
|
WebPConfigInit(&config);
|
|
|
|
config.lossless = 1;
|
2015-12-04 14:37:49 +01:00
|
|
|
// Enable exact, or it would alter RGB values of transparent alpha, which is
|
|
|
|
// normally OK but not here since we are not encoding the input image but an
|
|
|
|
// internal encoding-related image containing necessary exact information in
|
|
|
|
// RGB channels.
|
|
|
|
config.exact = 1;
|
2012-05-24 10:54:12 +02:00
|
|
|
config.method = effort_level; // impact is very small
|
2013-07-12 20:00:05 +02:00
|
|
|
// Set a low default quality for encoding alpha. Ensure that Alpha quality at
|
|
|
|
// lower methods (3 and below) is less than the threshold for triggering
|
|
|
|
// costly 'BackwardReferencesTraceBackwards'.
|
|
|
|
config.quality = 8.f * effort_level;
|
2013-02-05 19:48:30 +01:00
|
|
|
assert(config.quality >= 0 && config.quality <= 100.f);
|
2012-05-21 15:22:06 +02:00
|
|
|
|
2016-03-18 11:01:54 +01:00
|
|
|
// TODO(urvang): Temporary fix to avoid generating images that trigger
|
|
|
|
// a decoder bug related to alpha with color cache.
|
|
|
|
// See: https://code.google.com/p/webp/issues/detail?id=239
|
|
|
|
// Need to re-enable this later.
|
|
|
|
ok = (VP8LEncodeStream(&config, &picture, bw, 0 /*use_cache*/) == VP8_ENC_OK);
|
2012-05-21 15:22:06 +02:00
|
|
|
WebPPictureFree(&picture);
|
2014-09-02 21:55:13 +02:00
|
|
|
ok = ok && !bw->error_;
|
|
|
|
if (!ok) {
|
2014-10-23 15:35:16 +02:00
|
|
|
VP8LBitWriterWipeOut(bw);
|
2014-09-02 21:55:13 +02:00
|
|
|
return 0;
|
2012-05-23 17:01:44 +02:00
|
|
|
}
|
2014-09-02 21:55:13 +02:00
|
|
|
return 1;
|
2012-05-21 15:22:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
2013-06-24 11:03:01 +02:00
|
|
|
// Small struct to hold the result of a filter mode compression attempt.
|
|
|
|
typedef struct {
|
|
|
|
size_t score;
|
|
|
|
VP8BitWriter bw;
|
|
|
|
WebPAuxStats stats;
|
|
|
|
} FilterTrial;
|
|
|
|
|
2013-05-21 10:29:27 +02:00
|
|
|
// This function always returns an initialized 'bw' object, even upon error.
|
2012-07-25 01:15:36 +02:00
|
|
|
static int EncodeAlphaInternal(const uint8_t* const data, int width, int height,
|
2012-05-22 11:36:22 +02:00
|
|
|
int method, int filter, int reduce_levels,
|
2012-05-24 10:54:12 +02:00
|
|
|
int effort_level, // in [0..6] range
|
2012-07-25 01:15:36 +02:00
|
|
|
uint8_t* const tmp_alpha,
|
2013-06-24 11:03:01 +02:00
|
|
|
FilterTrial* result) {
|
2012-05-21 15:22:06 +02:00
|
|
|
int ok = 0;
|
|
|
|
const uint8_t* alpha_src;
|
|
|
|
WebPFilterFunc filter_func;
|
2012-05-22 11:36:22 +02:00
|
|
|
uint8_t header;
|
|
|
|
const size_t data_size = width * height;
|
2014-09-02 21:55:13 +02:00
|
|
|
const uint8_t* output = NULL;
|
|
|
|
size_t output_size = 0;
|
|
|
|
VP8LBitWriter tmp_bw;
|
2012-05-21 15:22:06 +02:00
|
|
|
|
2012-08-01 21:06:04 +02:00
|
|
|
assert((uint64_t)data_size == (uint64_t)width * height); // as per spec
|
2012-05-22 11:36:22 +02:00
|
|
|
assert(filter >= 0 && filter < WEBP_FILTER_LAST);
|
|
|
|
assert(method >= ALPHA_NO_COMPRESSION);
|
|
|
|
assert(method <= ALPHA_LOSSLESS_COMPRESSION);
|
|
|
|
assert(sizeof(header) == ALPHA_HEADER_LEN);
|
|
|
|
|
2012-05-21 15:22:06 +02:00
|
|
|
filter_func = WebPFilters[filter];
|
2013-03-14 20:26:39 +01:00
|
|
|
if (filter_func != NULL) {
|
|
|
|
filter_func(data, width, height, width, tmp_alpha);
|
2012-05-21 15:22:06 +02:00
|
|
|
alpha_src = tmp_alpha;
|
|
|
|
} else {
|
|
|
|
alpha_src = data;
|
|
|
|
}
|
|
|
|
|
2014-09-02 21:55:13 +02:00
|
|
|
if (method != ALPHA_NO_COMPRESSION) {
|
|
|
|
ok = VP8LBitWriterInit(&tmp_bw, data_size >> 3);
|
|
|
|
ok = ok && EncodeLossless(alpha_src, width, height, effort_level,
|
|
|
|
&tmp_bw, &result->stats);
|
|
|
|
if (ok) {
|
|
|
|
output = VP8LBitWriterFinish(&tmp_bw);
|
|
|
|
output_size = VP8LBitWriterNumBytes(&tmp_bw);
|
|
|
|
if (output_size > data_size) {
|
|
|
|
// compressed size is larger than source! Revert to uncompressed mode.
|
|
|
|
method = ALPHA_NO_COMPRESSION;
|
2014-10-23 15:35:16 +02:00
|
|
|
VP8LBitWriterWipeOut(&tmp_bw);
|
2014-09-02 21:55:13 +02:00
|
|
|
}
|
|
|
|
} else {
|
2014-10-23 15:35:16 +02:00
|
|
|
VP8LBitWriterWipeOut(&tmp_bw);
|
2014-09-02 21:55:13 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-21 15:22:06 +02:00
|
|
|
if (method == ALPHA_NO_COMPRESSION) {
|
2014-09-02 21:55:13 +02:00
|
|
|
output = alpha_src;
|
|
|
|
output_size = data_size;
|
|
|
|
ok = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emit final result.
|
|
|
|
header = method | (filter << 2);
|
|
|
|
if (reduce_levels) header |= ALPHA_PREPROCESSED_LEVELS << 4;
|
|
|
|
|
|
|
|
VP8BitWriterInit(&result->bw, ALPHA_HEADER_LEN + output_size);
|
|
|
|
ok = ok && VP8BitWriterAppend(&result->bw, &header, ALPHA_HEADER_LEN);
|
|
|
|
ok = ok && VP8BitWriterAppend(&result->bw, output, output_size);
|
|
|
|
|
|
|
|
if (method != ALPHA_NO_COMPRESSION) {
|
2014-10-23 15:35:16 +02:00
|
|
|
VP8LBitWriterWipeOut(&tmp_bw);
|
2012-05-21 15:22:06 +02:00
|
|
|
}
|
2014-09-02 21:55:13 +02:00
|
|
|
ok = ok && !result->bw.error_;
|
2013-06-24 11:03:01 +02:00
|
|
|
result->score = VP8BitWriterSize(&result->bw);
|
2012-05-21 15:22:06 +02:00
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
2013-05-09 02:19:04 +02:00
|
|
|
static int GetNumColors(const uint8_t* data, int width, int height,
|
|
|
|
int stride) {
|
|
|
|
int j;
|
|
|
|
int colors = 0;
|
|
|
|
uint8_t color[256] = { 0 };
|
|
|
|
|
|
|
|
for (j = 0; j < height; ++j) {
|
|
|
|
int i;
|
|
|
|
const uint8_t* const p = data + j * stride;
|
|
|
|
for (i = 0; i < width; ++i) {
|
|
|
|
color[p[i]] = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (j = 0; j < 256; ++j) {
|
|
|
|
if (color[j] > 0) ++colors;
|
|
|
|
}
|
|
|
|
return colors;
|
|
|
|
}
|
|
|
|
|
2013-06-24 11:03:01 +02:00
|
|
|
#define FILTER_TRY_NONE (1 << WEBP_FILTER_NONE)
|
|
|
|
#define FILTER_TRY_ALL ((1 << WEBP_FILTER_LAST) - 1)
|
|
|
|
|
2013-05-21 10:29:27 +02:00
|
|
|
// Given the input 'filter' option, return an OR'd bit-set of filters to try.
|
|
|
|
static uint32_t GetFilterMap(const uint8_t* alpha, int width, int height,
|
|
|
|
int filter, int effort_level) {
|
|
|
|
uint32_t bit_map = 0U;
|
|
|
|
if (filter == WEBP_FILTER_FAST) {
|
|
|
|
// Quick estimate of the best candidate.
|
|
|
|
int try_filter_none = (effort_level > 3);
|
|
|
|
const int kMinColorsForFilterNone = 16;
|
|
|
|
const int kMaxColorsForFilterNone = 192;
|
|
|
|
const int num_colors = GetNumColors(alpha, width, height, width);
|
2013-11-27 04:21:14 +01:00
|
|
|
// For low number of colors, NONE yields better compression.
|
2015-01-28 08:02:41 +01:00
|
|
|
filter = (num_colors <= kMinColorsForFilterNone)
|
|
|
|
? WEBP_FILTER_NONE
|
|
|
|
: WebPEstimateBestFilter(alpha, width, height, width);
|
2013-05-21 10:29:27 +02:00
|
|
|
bit_map |= 1 << filter;
|
|
|
|
// For large number of colors, try FILTER_NONE in addition to the best
|
|
|
|
// filter as well.
|
|
|
|
if (try_filter_none || num_colors > kMaxColorsForFilterNone) {
|
2013-06-24 11:03:01 +02:00
|
|
|
bit_map |= FILTER_TRY_NONE;
|
2013-05-21 10:29:27 +02:00
|
|
|
}
|
|
|
|
} else if (filter == WEBP_FILTER_NONE) {
|
2013-06-24 11:03:01 +02:00
|
|
|
bit_map = FILTER_TRY_NONE;
|
|
|
|
} else { // WEBP_FILTER_BEST -> try all
|
|
|
|
bit_map = FILTER_TRY_ALL;
|
2013-05-21 10:29:27 +02:00
|
|
|
}
|
|
|
|
return bit_map;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void InitFilterTrial(FilterTrial* const score) {
|
|
|
|
score->score = (size_t)~0U;
|
|
|
|
VP8BitWriterInit(&score->bw, 0);
|
|
|
|
}
|
|
|
|
|
2013-06-21 01:14:08 +02:00
|
|
|
static int ApplyFiltersAndEncode(const uint8_t* alpha, int width, int height,
|
|
|
|
size_t data_size, int method, int filter,
|
|
|
|
int reduce_levels, int effort_level,
|
|
|
|
uint8_t** const output,
|
|
|
|
size_t* const output_size,
|
|
|
|
WebPAuxStats* const stats) {
|
2013-05-21 10:29:27 +02:00
|
|
|
int ok = 1;
|
2013-06-24 11:03:01 +02:00
|
|
|
FilterTrial best;
|
2013-06-14 22:32:51 +02:00
|
|
|
uint32_t try_map =
|
|
|
|
GetFilterMap(alpha, width, height, filter, effort_level);
|
2013-06-24 11:03:01 +02:00
|
|
|
InitFilterTrial(&best);
|
2015-01-28 08:02:41 +01:00
|
|
|
|
2013-06-24 11:03:01 +02:00
|
|
|
if (try_map != FILTER_TRY_NONE) {
|
2014-03-27 23:27:32 +01:00
|
|
|
uint8_t* filtered_alpha = (uint8_t*)WebPSafeMalloc(1ULL, data_size);
|
2013-06-24 11:03:01 +02:00
|
|
|
if (filtered_alpha == NULL) return 0;
|
2013-05-21 10:29:27 +02:00
|
|
|
|
2013-06-24 11:03:01 +02:00
|
|
|
for (filter = WEBP_FILTER_NONE; ok && try_map; ++filter, try_map >>= 1) {
|
2013-05-21 10:29:27 +02:00
|
|
|
if (try_map & 1) {
|
|
|
|
FilterTrial trial;
|
|
|
|
ok = EncodeAlphaInternal(alpha, width, height, method, filter,
|
|
|
|
reduce_levels, effort_level, filtered_alpha,
|
2013-06-24 11:03:01 +02:00
|
|
|
&trial);
|
|
|
|
if (ok && trial.score < best.score) {
|
|
|
|
VP8BitWriterWipeOut(&best.bw);
|
|
|
|
best = trial;
|
2013-05-21 10:29:27 +02:00
|
|
|
} else {
|
|
|
|
VP8BitWriterWipeOut(&trial.bw);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-03-27 23:27:32 +01:00
|
|
|
WebPSafeFree(filtered_alpha);
|
2013-06-24 11:03:01 +02:00
|
|
|
} else {
|
|
|
|
ok = EncodeAlphaInternal(alpha, width, height, method, WEBP_FILTER_NONE,
|
|
|
|
reduce_levels, effort_level, NULL, &best);
|
|
|
|
}
|
|
|
|
if (ok) {
|
2015-02-13 04:44:25 +01:00
|
|
|
if (stats != NULL) {
|
|
|
|
stats->lossless_features = best.stats.lossless_features;
|
|
|
|
stats->histogram_bits = best.stats.histogram_bits;
|
|
|
|
stats->transform_bits = best.stats.transform_bits;
|
|
|
|
stats->cache_bits = best.stats.cache_bits;
|
|
|
|
stats->palette_size = best.stats.palette_size;
|
|
|
|
stats->lossless_size = best.stats.lossless_size;
|
|
|
|
stats->lossless_hdr_size = best.stats.lossless_hdr_size;
|
|
|
|
stats->lossless_data_size = best.stats.lossless_data_size;
|
|
|
|
}
|
2013-06-24 11:03:01 +02:00
|
|
|
*output_size = VP8BitWriterSize(&best.bw);
|
|
|
|
*output = VP8BitWriterBuf(&best.bw);
|
|
|
|
} else {
|
|
|
|
VP8BitWriterWipeOut(&best.bw);
|
2013-05-21 10:29:27 +02:00
|
|
|
}
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2012-07-25 01:15:36 +02:00
|
|
|
static int EncodeAlpha(VP8Encoder* const enc,
|
2012-05-21 15:22:06 +02:00
|
|
|
int quality, int method, int filter,
|
2012-05-24 10:54:12 +02:00
|
|
|
int effort_level,
|
2012-07-25 01:15:36 +02:00
|
|
|
uint8_t** const output, size_t* const output_size) {
|
|
|
|
const WebPPicture* const pic = enc->pic_;
|
|
|
|
const int width = pic->width;
|
|
|
|
const int height = pic->height;
|
|
|
|
|
2012-05-21 15:22:06 +02:00
|
|
|
uint8_t* quant_alpha = NULL;
|
2012-05-22 11:36:22 +02:00
|
|
|
const size_t data_size = width * height;
|
2012-07-25 01:15:36 +02:00
|
|
|
uint64_t sse = 0;
|
2012-05-21 15:22:06 +02:00
|
|
|
int ok = 1;
|
2012-05-22 11:36:22 +02:00
|
|
|
const int reduce_levels = (quality < 100);
|
2012-05-21 15:22:06 +02:00
|
|
|
|
|
|
|
// quick sanity checks
|
2012-08-01 21:06:04 +02:00
|
|
|
assert((uint64_t)data_size == (uint64_t)width * height); // as per spec
|
2012-07-25 01:15:36 +02:00
|
|
|
assert(enc != NULL && pic != NULL && pic->a != NULL);
|
|
|
|
assert(output != NULL && output_size != NULL);
|
2012-05-21 15:22:06 +02:00
|
|
|
assert(width > 0 && height > 0);
|
2012-07-25 01:15:36 +02:00
|
|
|
assert(pic->a_stride >= width);
|
2012-05-21 15:22:06 +02:00
|
|
|
assert(filter >= WEBP_FILTER_NONE && filter <= WEBP_FILTER_FAST);
|
|
|
|
|
|
|
|
if (quality < 0 || quality > 100) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (method < ALPHA_NO_COMPRESSION || method > ALPHA_LOSSLESS_COMPRESSION) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-06-14 22:32:51 +02:00
|
|
|
if (method == ALPHA_NO_COMPRESSION) {
|
|
|
|
// Don't filter, as filtering will make no impact on compressed size.
|
|
|
|
filter = WEBP_FILTER_NONE;
|
|
|
|
}
|
|
|
|
|
2014-03-27 23:27:32 +01:00
|
|
|
quant_alpha = (uint8_t*)WebPSafeMalloc(1ULL, data_size);
|
2012-05-21 15:22:06 +02:00
|
|
|
if (quant_alpha == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract alpha data (width x height) from raw_data (stride x height).
|
2015-11-13 00:28:09 +01:00
|
|
|
WebPCopyPlane(pic->a, pic->a_stride, quant_alpha, width, width, height);
|
2012-05-21 15:22:06 +02:00
|
|
|
|
2012-05-22 11:36:22 +02:00
|
|
|
if (reduce_levels) { // No Quantization required for 'quality = 100'.
|
2012-05-21 15:22:06 +02:00
|
|
|
// 16 alpha levels gives quite a low MSE w.r.t original alpha plane hence
|
|
|
|
// mapped to moderate quality 70. Hence Quality:[0, 70] -> Levels:[2, 16]
|
|
|
|
// and Quality:]70, 100] -> Levels:]16, 256].
|
|
|
|
const int alpha_levels = (quality <= 70) ? (2 + quality / 5)
|
|
|
|
: (16 + (quality - 70) * 8);
|
2012-07-25 01:15:36 +02:00
|
|
|
ok = QuantizeLevels(quant_alpha, width, height, alpha_levels, &sse);
|
2012-05-21 15:22:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ok) {
|
2015-01-28 08:02:41 +01:00
|
|
|
VP8FiltersInit();
|
2013-06-21 01:14:08 +02:00
|
|
|
ok = ApplyFiltersAndEncode(quant_alpha, width, height, data_size, method,
|
|
|
|
filter, reduce_levels, effort_level, output,
|
|
|
|
output_size, pic->stats);
|
2013-05-21 10:29:27 +02:00
|
|
|
if (pic->stats != NULL) { // need stats?
|
|
|
|
pic->stats->coded_size += (int)(*output_size);
|
|
|
|
enc->sse_[3] = sse;
|
2012-05-21 15:22:06 +02:00
|
|
|
}
|
|
|
|
}
|
2013-05-21 10:29:27 +02:00
|
|
|
|
2014-03-27 23:27:32 +01:00
|
|
|
WebPSafeFree(quant_alpha);
|
2012-05-21 15:22:06 +02:00
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2011-08-25 23:22:32 +02:00
|
|
|
//------------------------------------------------------------------------------
|
2012-05-21 15:22:06 +02:00
|
|
|
// Main calls
|
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
|
|
|
|
2013-03-01 01:21:34 +01:00
|
|
|
static int CompressAlphaJob(VP8Encoder* const enc, void* dummy) {
|
|
|
|
const WebPConfig* config = enc->config_;
|
|
|
|
uint8_t* alpha_data = NULL;
|
|
|
|
size_t alpha_size = 0;
|
|
|
|
const int effort_level = config->method; // maps to [0..6]
|
|
|
|
const WEBP_FILTER_TYPE filter =
|
|
|
|
(config->alpha_filtering == 0) ? WEBP_FILTER_NONE :
|
|
|
|
(config->alpha_filtering == 1) ? WEBP_FILTER_FAST :
|
|
|
|
WEBP_FILTER_BEST;
|
|
|
|
if (!EncodeAlpha(enc, config->alpha_quality, config->alpha_compression,
|
|
|
|
filter, effort_level, &alpha_data, &alpha_size)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (alpha_size != (uint32_t)alpha_size) { // Sanity check.
|
2014-03-27 23:27:32 +01:00
|
|
|
WebPSafeFree(alpha_data);
|
2013-03-01 01:21:34 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
enc->alpha_data_size_ = (uint32_t)alpha_size;
|
|
|
|
enc->alpha_data_ = alpha_data;
|
|
|
|
(void)dummy;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-07-25 01:15:36 +02:00
|
|
|
void VP8EncInitAlpha(VP8Encoder* const enc) {
|
2014-10-06 23:15:44 +02:00
|
|
|
WebPInitAlphaProcessing();
|
2012-06-05 00:50:05 +02:00
|
|
|
enc->has_alpha_ = WebPPictureHasTransparency(enc->pic_);
|
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
|
|
|
enc->alpha_data_ = NULL;
|
|
|
|
enc->alpha_data_size_ = 0;
|
2013-03-01 01:21:34 +01:00
|
|
|
if (enc->thread_level_ > 0) {
|
|
|
|
WebPWorker* const worker = &enc->alpha_worker_;
|
2014-06-12 11:35:44 +02:00
|
|
|
WebPGetWorkerInterface()->Init(worker);
|
2013-03-01 01:21:34 +01:00
|
|
|
worker->data1 = enc;
|
|
|
|
worker->data2 = NULL;
|
|
|
|
worker->hook = (WebPWorkerHook)CompressAlphaJob;
|
|
|
|
}
|
2011-05-03 02:19:00 +02:00
|
|
|
}
|
|
|
|
|
2013-03-01 01:21:34 +01:00
|
|
|
int VP8EncStartAlpha(VP8Encoder* const enc) {
|
2011-05-03 02:19:00 +02:00
|
|
|
if (enc->has_alpha_) {
|
2013-03-01 01:21:34 +01:00
|
|
|
if (enc->thread_level_ > 0) {
|
|
|
|
WebPWorker* const worker = &enc->alpha_worker_;
|
2014-06-12 11:35:44 +02:00
|
|
|
// Makes sure worker is good to go.
|
|
|
|
if (!WebPGetWorkerInterface()->Reset(worker)) {
|
2013-03-01 01:21:34 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2014-06-12 11:35:44 +02:00
|
|
|
WebPGetWorkerInterface()->Launch(worker);
|
2013-03-01 01:21:34 +01:00
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return CompressAlphaJob(enc, NULL); // just do the job right away
|
2011-04-26 10:03:58 +02:00
|
|
|
}
|
2013-03-01 01:21:34 +01:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int VP8EncFinishAlpha(VP8Encoder* const enc) {
|
|
|
|
if (enc->has_alpha_) {
|
|
|
|
if (enc->thread_level_ > 0) {
|
|
|
|
WebPWorker* const worker = &enc->alpha_worker_;
|
2014-06-12 11:35:44 +02:00
|
|
|
if (!WebPGetWorkerInterface()->Sync(worker)) return 0; // error
|
2011-12-01 07:44:15 +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
|
|
|
}
|
2012-05-29 08:02:02 +02:00
|
|
|
return WebPReportProgress(enc->pic_, enc->percent_ + 20, &enc->percent_);
|
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
|
|
|
}
|
|
|
|
|
2013-03-01 01:21:34 +01:00
|
|
|
int VP8EncDeleteAlpha(VP8Encoder* const enc) {
|
|
|
|
int ok = 1;
|
|
|
|
if (enc->thread_level_ > 0) {
|
|
|
|
WebPWorker* const worker = &enc->alpha_worker_;
|
2014-06-12 11:35:44 +02:00
|
|
|
// finish anything left in flight
|
|
|
|
ok = WebPGetWorkerInterface()->Sync(worker);
|
|
|
|
// still need to end the worker, even if !ok
|
|
|
|
WebPGetWorkerInterface()->End(worker);
|
2013-03-01 01:21:34 +01:00
|
|
|
}
|
2014-03-27 23:27:32 +01:00
|
|
|
WebPSafeFree(enc->alpha_data_);
|
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
|
|
|
enc->alpha_data_ = NULL;
|
|
|
|
enc->alpha_data_size_ = 0;
|
2011-05-03 02:19:00 +02:00
|
|
|
enc->has_alpha_ = 0;
|
2013-03-01 01:21:34 +01:00
|
|
|
return ok;
|
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
|
|
|
}
|