Path: blob/master/thirdparty/libwebp/src/enc/near_lossless_enc.c
9913 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>1819#include "src/dsp/lossless_common.h"20#include "src/utils/utils.h"21#include "src/enc/vp8li_enc.h"2223#if (WEBP_NEAR_LOSSLESS == 1)2425#define MIN_DIM_FOR_NEAR_LOSSLESS 6426#define MAX_LIMIT_BITS 52728// Quantizes the value up or down to a multiple of 1<<bits (or to 255),29// choosing the closer one, resolving ties using bankers' rounding.30static uint32_t FindClosestDiscretized(uint32_t a, int bits) {31const uint32_t mask = (1u << bits) - 1;32const uint32_t biased = a + (mask >> 1) + ((a >> bits) & 1);33assert(bits > 0);34if (biased > 0xff) return 0xff;35return biased & ~mask;36}3738// Applies FindClosestDiscretized to all channels of pixel.39static uint32_t ClosestDiscretizedArgb(uint32_t a, int bits) {40return41(FindClosestDiscretized(a >> 24, bits) << 24) |42(FindClosestDiscretized((a >> 16) & 0xff, bits) << 16) |43(FindClosestDiscretized((a >> 8) & 0xff, bits) << 8) |44(FindClosestDiscretized(a & 0xff, bits));45}4647// Checks if distance between corresponding channel values of pixels a and b48// is within the given limit.49static int IsNear(uint32_t a, uint32_t b, int limit) {50int k;51for (k = 0; k < 4; ++k) {52const int delta =53(int)((a >> (k * 8)) & 0xff) - (int)((b >> (k * 8)) & 0xff);54if (delta >= limit || delta <= -limit) {55return 0;56}57}58return 1;59}6061static int IsSmooth(const uint32_t* const prev_row,62const uint32_t* const curr_row,63const uint32_t* const next_row,64int ix, int limit) {65// Check that all pixels in 4-connected neighborhood are smooth.66return (IsNear(curr_row[ix], curr_row[ix - 1], limit) &&67IsNear(curr_row[ix], curr_row[ix + 1], limit) &&68IsNear(curr_row[ix], prev_row[ix], limit) &&69IsNear(curr_row[ix], next_row[ix], limit));70}7172// Adjusts pixel values of image with given maximum error.73static void NearLossless(int xsize, int ysize, const uint32_t* argb_src,74int stride, int limit_bits, uint32_t* copy_buffer,75uint32_t* argb_dst) {76int x, y;77const int limit = 1 << limit_bits;78uint32_t* prev_row = copy_buffer;79uint32_t* curr_row = prev_row + xsize;80uint32_t* next_row = curr_row + xsize;81memcpy(curr_row, argb_src, xsize * sizeof(argb_src[0]));82memcpy(next_row, argb_src + stride, xsize * sizeof(argb_src[0]));8384for (y = 0; y < ysize; ++y, argb_src += stride, argb_dst += xsize) {85if (y == 0 || y == ysize - 1) {86memcpy(argb_dst, argb_src, xsize * sizeof(argb_src[0]));87} else {88memcpy(next_row, argb_src + stride, xsize * sizeof(argb_src[0]));89argb_dst[0] = argb_src[0];90argb_dst[xsize - 1] = argb_src[xsize - 1];91for (x = 1; x < xsize - 1; ++x) {92if (IsSmooth(prev_row, curr_row, next_row, x, limit)) {93argb_dst[x] = curr_row[x];94} else {95argb_dst[x] = ClosestDiscretizedArgb(curr_row[x], limit_bits);96}97}98}99{100// Three-way swap.101uint32_t* const temp = prev_row;102prev_row = curr_row;103curr_row = next_row;104next_row = temp;105}106}107}108109int VP8ApplyNearLossless(const WebPPicture* const picture, int quality,110uint32_t* const argb_dst) {111int i;112const int xsize = picture->width;113const int ysize = picture->height;114const int stride = picture->argb_stride;115uint32_t* const copy_buffer =116(uint32_t*)WebPSafeMalloc(xsize * 3, sizeof(*copy_buffer));117const int limit_bits = VP8LNearLosslessBits(quality);118assert(argb_dst != NULL);119assert(limit_bits > 0);120assert(limit_bits <= MAX_LIMIT_BITS);121if (copy_buffer == NULL) {122return 0;123}124// For small icon images, don't attempt to apply near-lossless compression.125if ((xsize < MIN_DIM_FOR_NEAR_LOSSLESS &&126ysize < MIN_DIM_FOR_NEAR_LOSSLESS) ||127ysize < 3) {128for (i = 0; i < ysize; ++i) {129memcpy(argb_dst + i * xsize, picture->argb + i * picture->argb_stride,130xsize * sizeof(*argb_dst));131}132WebPSafeFree(copy_buffer);133return 1;134}135136NearLossless(xsize, ysize, picture->argb, stride, limit_bits, copy_buffer,137argb_dst);138for (i = limit_bits - 1; i != 0; --i) {139NearLossless(xsize, ysize, argb_dst, xsize, i, copy_buffer, argb_dst);140}141WebPSafeFree(copy_buffer);142return 1;143}144#else // (WEBP_NEAR_LOSSLESS == 1)145146// Define a stub to suppress compiler warnings.147extern void VP8LNearLosslessStub(void);148void VP8LNearLosslessStub(void) {}149150#endif // (WEBP_NEAR_LOSSLESS == 1)151152153