Path: blob/master/thirdparty/libwebp/src/enc/near_lossless_enc.c
22205 views
// Copyright 2014 Google Inc. All Rights Reserved.1//2// Use of this source code is governed by a BSD-style license3// that can be found in the COPYING file in the root of the source4// tree. An additional intellectual property rights grant can be found5// in the file PATENTS. All contributing project authors may6// be found in the AUTHORS file in the root of the source tree.7// -----------------------------------------------------------------------------8//9// Near-lossless image preprocessing adjusts pixel values to help10// compressibility with a guarantee of maximum deviation between original and11// resulting pixel values.12//13// Author: Jyrki Alakuijala ([email protected])14// Converted to C by Aleksander Kramarz ([email protected])1516#include <assert.h>17#include <stdlib.h>18#include <string.h>1920#include "src/dsp/lossless_common.h"21#include "src/webp/types.h"22#include "src/enc/vp8li_enc.h"23#include "src/utils/utils.h"24#include "src/webp/encode.h"2526#if (WEBP_NEAR_LOSSLESS == 1)2728#define MIN_DIM_FOR_NEAR_LOSSLESS 6429#define MAX_LIMIT_BITS 53031// Quantizes the value up or down to a multiple of 1<<bits (or to 255),32// choosing the closer one, resolving ties using bankers' rounding.33static uint32_t FindClosestDiscretized(uint32_t a, int bits) {34const uint32_t mask = (1u << bits) - 1;35const uint32_t biased = a + (mask >> 1) + ((a >> bits) & 1);36assert(bits > 0);37if (biased > 0xff) return 0xff;38return biased & ~mask;39}4041// Applies FindClosestDiscretized to all channels of pixel.42static uint32_t ClosestDiscretizedArgb(uint32_t a, int bits) {43return44(FindClosestDiscretized(a >> 24, bits) << 24) |45(FindClosestDiscretized((a >> 16) & 0xff, bits) << 16) |46(FindClosestDiscretized((a >> 8) & 0xff, bits) << 8) |47(FindClosestDiscretized(a & 0xff, bits));48}4950// Checks if distance between corresponding channel values of pixels a and b51// is within the given limit.52static int IsNear(uint32_t a, uint32_t b, int limit) {53int k;54for (k = 0; k < 4; ++k) {55const int delta =56(int)((a >> (k * 8)) & 0xff) - (int)((b >> (k * 8)) & 0xff);57if (delta >= limit || delta <= -limit) {58return 0;59}60}61return 1;62}6364static int IsSmooth(const uint32_t* const prev_row,65const uint32_t* const curr_row,66const uint32_t* const next_row,67int ix, int limit) {68// Check that all pixels in 4-connected neighborhood are smooth.69return (IsNear(curr_row[ix], curr_row[ix - 1], limit) &&70IsNear(curr_row[ix], curr_row[ix + 1], limit) &&71IsNear(curr_row[ix], prev_row[ix], limit) &&72IsNear(curr_row[ix], next_row[ix], limit));73}7475// Adjusts pixel values of image with given maximum error.76static void NearLossless(int xsize, int ysize, const uint32_t* argb_src,77int stride, int limit_bits, uint32_t* copy_buffer,78uint32_t* argb_dst) {79int x, y;80const int limit = 1 << limit_bits;81uint32_t* prev_row = copy_buffer;82uint32_t* curr_row = prev_row + xsize;83uint32_t* next_row = curr_row + xsize;84memcpy(curr_row, argb_src, xsize * sizeof(argb_src[0]));85memcpy(next_row, argb_src + stride, xsize * sizeof(argb_src[0]));8687for (y = 0; y < ysize; ++y, argb_src += stride, argb_dst += xsize) {88if (y == 0 || y == ysize - 1) {89memcpy(argb_dst, argb_src, xsize * sizeof(argb_src[0]));90} else {91memcpy(next_row, argb_src + stride, xsize * sizeof(argb_src[0]));92argb_dst[0] = argb_src[0];93argb_dst[xsize - 1] = argb_src[xsize - 1];94for (x = 1; x < xsize - 1; ++x) {95if (IsSmooth(prev_row, curr_row, next_row, x, limit)) {96argb_dst[x] = curr_row[x];97} else {98argb_dst[x] = ClosestDiscretizedArgb(curr_row[x], limit_bits);99}100}101}102{103// Three-way swap.104uint32_t* const temp = prev_row;105prev_row = curr_row;106curr_row = next_row;107next_row = temp;108}109}110}111112int VP8ApplyNearLossless(const WebPPicture* const picture, int quality,113uint32_t* const argb_dst) {114int i;115const int xsize = picture->width;116const int ysize = picture->height;117const int stride = picture->argb_stride;118uint32_t* const copy_buffer =119(uint32_t*)WebPSafeMalloc(xsize * 3, sizeof(*copy_buffer));120const int limit_bits = VP8LNearLosslessBits(quality);121assert(argb_dst != NULL);122assert(limit_bits > 0);123assert(limit_bits <= MAX_LIMIT_BITS);124if (copy_buffer == NULL) {125return 0;126}127// For small icon images, don't attempt to apply near-lossless compression.128if ((xsize < MIN_DIM_FOR_NEAR_LOSSLESS &&129ysize < MIN_DIM_FOR_NEAR_LOSSLESS) ||130ysize < 3) {131for (i = 0; i < ysize; ++i) {132memcpy(argb_dst + i * xsize, picture->argb + i * picture->argb_stride,133xsize * sizeof(*argb_dst));134}135WebPSafeFree(copy_buffer);136return 1;137}138139NearLossless(xsize, ysize, picture->argb, stride, limit_bits, copy_buffer,140argb_dst);141for (i = limit_bits - 1; i != 0; --i) {142NearLossless(xsize, ysize, argb_dst, xsize, i, copy_buffer, argb_dst);143}144WebPSafeFree(copy_buffer);145return 1;146}147#else // (WEBP_NEAR_LOSSLESS == 1)148149// Define a stub to suppress compiler warnings.150extern void VP8LNearLosslessStub(void);151void VP8LNearLosslessStub(void) {}152153#endif // (WEBP_NEAR_LOSSLESS == 1)154155156