2014-08-05 19:14:57 +02:00
|
|
|
// Copyright 2014 Google Inc. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Near-lossless image preprocessing adjusts pixel values to help
|
|
|
|
// compressibility with a guarantee of maximum deviation between original and
|
|
|
|
// resulting pixel values.
|
|
|
|
//
|
|
|
|
// Author: Jyrki Alakuijala (jyrki@google.com)
|
|
|
|
// Converted to C by Aleksander Kramarz (akramarz@google.com)
|
|
|
|
|
2016-03-02 12:45:54 +01:00
|
|
|
#include <assert.h>
|
2014-08-05 19:14:57 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2017-10-07 23:15:11 +02:00
|
|
|
#include "src/dsp/lossless_common.h"
|
|
|
|
#include "src/utils/utils.h"
|
|
|
|
#include "src/enc/vp8li_enc.h"
|
2014-08-05 19:14:57 +02:00
|
|
|
|
2017-08-31 14:02:05 +02:00
|
|
|
#if (WEBP_NEAR_LOSSLESS == 1)
|
|
|
|
|
2015-01-26 23:55:54 +01:00
|
|
|
#define MIN_DIM_FOR_NEAR_LOSSLESS 64
|
|
|
|
#define MAX_LIMIT_BITS 5
|
|
|
|
|
2016-03-02 12:45:54 +01:00
|
|
|
// Quantizes the value up or down to a multiple of 1<<bits (or to 255),
|
|
|
|
// choosing the closer one, resolving ties using bankers' rounding.
|
2017-05-22 12:11:03 +02:00
|
|
|
static uint32_t FindClosestDiscretized(uint32_t a, int bits) {
|
2017-05-22 17:21:44 +02:00
|
|
|
const uint32_t mask = (1u << bits) - 1;
|
2017-05-22 12:11:03 +02:00
|
|
|
const uint32_t biased = a + (mask >> 1) + ((a >> bits) & 1);
|
2016-03-02 12:45:54 +01:00
|
|
|
assert(bits > 0);
|
|
|
|
if (biased > 0xff) return 0xff;
|
|
|
|
return biased & ~mask;
|
2014-08-05 19:14:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Applies FindClosestDiscretized to all channels of pixel.
|
2015-01-26 23:55:54 +01:00
|
|
|
static uint32_t ClosestDiscretizedArgb(uint32_t a, int bits) {
|
|
|
|
return
|
|
|
|
(FindClosestDiscretized(a >> 24, bits) << 24) |
|
|
|
|
(FindClosestDiscretized((a >> 16) & 0xff, bits) << 16) |
|
|
|
|
(FindClosestDiscretized((a >> 8) & 0xff, bits) << 8) |
|
|
|
|
(FindClosestDiscretized(a & 0xff, bits));
|
2014-08-05 19:14:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Checks if distance between corresponding channel values of pixels a and b
|
2015-01-26 23:55:54 +01:00
|
|
|
// is within the given limit.
|
|
|
|
static int IsNear(uint32_t a, uint32_t b, int limit) {
|
2014-08-05 19:14:57 +02:00
|
|
|
int k;
|
|
|
|
for (k = 0; k < 4; ++k) {
|
2015-01-26 23:55:54 +01:00
|
|
|
const int delta =
|
|
|
|
(int)((a >> (k * 8)) & 0xff) - (int)((b >> (k * 8)) & 0xff);
|
2014-08-05 19:14:57 +02:00
|
|
|
if (delta >= limit || delta <= -limit) {
|
2015-01-26 23:55:54 +01:00
|
|
|
return 0;
|
2014-08-05 19:14:57 +02:00
|
|
|
}
|
|
|
|
}
|
2015-01-26 23:55:54 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int IsSmooth(const uint32_t* const prev_row,
|
|
|
|
const uint32_t* const curr_row,
|
|
|
|
const uint32_t* const next_row,
|
|
|
|
int ix, int limit) {
|
|
|
|
// Check that all pixels in 4-connected neighborhood are smooth.
|
|
|
|
return (IsNear(curr_row[ix], curr_row[ix - 1], limit) &&
|
|
|
|
IsNear(curr_row[ix], curr_row[ix + 1], limit) &&
|
|
|
|
IsNear(curr_row[ix], prev_row[ix], limit) &&
|
|
|
|
IsNear(curr_row[ix], next_row[ix], limit));
|
2014-08-05 19:14:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Adjusts pixel values of image with given maximum error.
|
2017-04-21 15:35:11 +02:00
|
|
|
static void NearLossless(int xsize, int ysize, const uint32_t* argb_src,
|
|
|
|
int stride, int limit_bits, uint32_t* copy_buffer,
|
|
|
|
uint32_t* argb_dst) {
|
2014-08-05 19:14:57 +02:00
|
|
|
int x, y;
|
|
|
|
const int limit = 1 << limit_bits;
|
2015-01-26 23:55:54 +01:00
|
|
|
uint32_t* prev_row = copy_buffer;
|
|
|
|
uint32_t* curr_row = prev_row + xsize;
|
|
|
|
uint32_t* next_row = curr_row + xsize;
|
2017-04-21 15:35:11 +02:00
|
|
|
memcpy(curr_row, argb_src, xsize * sizeof(argb_src[0]));
|
|
|
|
memcpy(next_row, argb_src + stride, xsize * sizeof(argb_src[0]));
|
2015-01-26 23:55:54 +01:00
|
|
|
|
2017-04-21 15:35:11 +02:00
|
|
|
for (y = 0; y < ysize; ++y, argb_src += stride, argb_dst += xsize) {
|
|
|
|
if (y == 0 || y == ysize - 1) {
|
|
|
|
memcpy(argb_dst, argb_src, xsize * sizeof(argb_src[0]));
|
|
|
|
} else {
|
|
|
|
memcpy(next_row, argb_src + stride, xsize * sizeof(argb_src[0]));
|
|
|
|
argb_dst[0] = argb_src[0];
|
|
|
|
argb_dst[xsize - 1] = argb_src[xsize - 1];
|
|
|
|
for (x = 1; x < xsize - 1; ++x) {
|
|
|
|
if (IsSmooth(prev_row, curr_row, next_row, x, limit)) {
|
|
|
|
argb_dst[x] = curr_row[x];
|
|
|
|
} else {
|
|
|
|
argb_dst[x] = ClosestDiscretizedArgb(curr_row[x], limit_bits);
|
|
|
|
}
|
2014-08-05 19:14:57 +02:00
|
|
|
}
|
|
|
|
}
|
2015-01-26 23:55:54 +01:00
|
|
|
{
|
|
|
|
// Three-way swap.
|
|
|
|
uint32_t* const temp = prev_row;
|
|
|
|
prev_row = curr_row;
|
|
|
|
curr_row = next_row;
|
|
|
|
next_row = temp;
|
|
|
|
}
|
2014-08-05 19:14:57 +02:00
|
|
|
}
|
|
|
|
}
|
2014-10-15 14:28:19 +02:00
|
|
|
|
2017-04-21 15:35:11 +02:00
|
|
|
int VP8ApplyNearLossless(const WebPPicture* const picture, int quality,
|
|
|
|
uint32_t* const argb_dst) {
|
2014-08-05 19:14:57 +02:00
|
|
|
int i;
|
2017-04-21 15:35:11 +02:00
|
|
|
const int xsize = picture->width;
|
|
|
|
const int ysize = picture->height;
|
|
|
|
const int stride = picture->argb_stride;
|
2014-08-05 19:14:57 +02:00
|
|
|
uint32_t* const copy_buffer =
|
2015-01-26 23:55:54 +01:00
|
|
|
(uint32_t*)WebPSafeMalloc(xsize * 3, sizeof(*copy_buffer));
|
2016-05-18 13:20:45 +02:00
|
|
|
const int limit_bits = VP8LNearLosslessBits(quality);
|
2017-04-21 15:35:11 +02:00
|
|
|
assert(argb_dst != NULL);
|
|
|
|
assert(limit_bits > 0);
|
2015-01-26 23:55:54 +01:00
|
|
|
assert(limit_bits <= MAX_LIMIT_BITS);
|
2014-08-05 19:14:57 +02:00
|
|
|
if (copy_buffer == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
2015-01-26 23:55:54 +01:00
|
|
|
// For small icon images, don't attempt to apply near-lossless compression.
|
2017-04-21 15:35:11 +02:00
|
|
|
if ((xsize < MIN_DIM_FOR_NEAR_LOSSLESS &&
|
|
|
|
ysize < MIN_DIM_FOR_NEAR_LOSSLESS) ||
|
|
|
|
ysize < 3) {
|
|
|
|
for (i = 0; i < ysize; ++i) {
|
|
|
|
memcpy(argb_dst + i * xsize, picture->argb + i * picture->argb_stride,
|
|
|
|
xsize * sizeof(*argb_dst));
|
|
|
|
}
|
2015-01-26 23:55:54 +01:00
|
|
|
WebPSafeFree(copy_buffer);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-04-21 15:35:11 +02:00
|
|
|
NearLossless(xsize, ysize, picture->argb, stride, limit_bits, copy_buffer,
|
|
|
|
argb_dst);
|
|
|
|
for (i = limit_bits - 1; i != 0; --i) {
|
|
|
|
NearLossless(xsize, ysize, argb_dst, xsize, i, copy_buffer, argb_dst);
|
2014-08-05 19:14:57 +02:00
|
|
|
}
|
|
|
|
WebPSafeFree(copy_buffer);
|
|
|
|
return 1;
|
|
|
|
}
|
2017-08-31 14:02:05 +02:00
|
|
|
#else // (WEBP_NEAR_LOSSLESS == 1)
|
|
|
|
|
|
|
|
// Define a stub to suppress compiler warnings.
|
|
|
|
extern void VP8LNearLosslessStub(void);
|
2018-04-17 05:19:54 +02:00
|
|
|
void VP8LNearLosslessStub(void) {}
|
2017-08-31 14:02:05 +02:00
|
|
|
|
|
|
|
#endif // (WEBP_NEAR_LOSSLESS == 1)
|