1 // Copyright 2014 Google Inc. All Rights Reserved.
2 //
3 // Use of this source code is governed by a BSD-style license
4 // that can be found in the COPYING file in the root of the source
5 // tree. An additional intellectual property rights grant can be found
6 // in the file PATENTS. All contributing project authors may
7 // be found in the AUTHORS file in the root of the source tree.
8 // -----------------------------------------------------------------------------
9 //
10 // Near-lossless image preprocessing adjusts pixel values to help
11 // compressibility with a guarantee of maximum deviation between original and
12 // resulting pixel values.
13 //
14 // Author: Jyrki Alakuijala (jyrki@google.com)
15 // Converted to C by Aleksander Kramarz (akramarz@google.com)
16
17 #include <assert.h>
18 #include <stdlib.h>
19
20 #include "../dsp/lossless_common.h"
21 #include "../utils/utils.h"
22 #include "./vp8i_enc.h"
23
24 #define MIN_DIM_FOR_NEAR_LOSSLESS 64
25 #define MAX_LIMIT_BITS 5
26
27 // Quantizes the value up or down to a multiple of 1<<bits (or to 255),
28 // choosing the closer one, resolving ties using bankers' rounding.
FindClosestDiscretized(int a,int bits)29 static int FindClosestDiscretized(int a, int bits) {
30 const int mask = (1 << bits) - 1;
31 const int biased = a + (mask >> 1) + ((a >> bits) & 1);
32 assert(bits > 0);
33 if (biased > 0xff) return 0xff;
34 return biased & ~mask;
35 }
36
37 // Applies FindClosestDiscretized to all channels of pixel.
ClosestDiscretizedArgb(uint32_t a,int bits)38 static uint32_t ClosestDiscretizedArgb(uint32_t a, int bits) {
39 return
40 (FindClosestDiscretized(a >> 24, bits) << 24) |
41 (FindClosestDiscretized((a >> 16) & 0xff, bits) << 16) |
42 (FindClosestDiscretized((a >> 8) & 0xff, bits) << 8) |
43 (FindClosestDiscretized(a & 0xff, bits));
44 }
45
46 // Checks if distance between corresponding channel values of pixels a and b
47 // is within the given limit.
IsNear(uint32_t a,uint32_t b,int limit)48 static int IsNear(uint32_t a, uint32_t b, int limit) {
49 int k;
50 for (k = 0; k < 4; ++k) {
51 const int delta =
52 (int)((a >> (k * 8)) & 0xff) - (int)((b >> (k * 8)) & 0xff);
53 if (delta >= limit || delta <= -limit) {
54 return 0;
55 }
56 }
57 return 1;
58 }
59
IsSmooth(const uint32_t * const prev_row,const uint32_t * const curr_row,const uint32_t * const next_row,int ix,int limit)60 static int IsSmooth(const uint32_t* const prev_row,
61 const uint32_t* const curr_row,
62 const uint32_t* const next_row,
63 int ix, int limit) {
64 // Check that all pixels in 4-connected neighborhood are smooth.
65 return (IsNear(curr_row[ix], curr_row[ix - 1], limit) &&
66 IsNear(curr_row[ix], curr_row[ix + 1], limit) &&
67 IsNear(curr_row[ix], prev_row[ix], limit) &&
68 IsNear(curr_row[ix], next_row[ix], limit));
69 }
70
71 // Adjusts pixel values of image with given maximum error.
NearLossless(int xsize,int ysize,uint32_t * argb,int limit_bits,uint32_t * copy_buffer)72 static void NearLossless(int xsize, int ysize, uint32_t* argb,
73 int limit_bits, uint32_t* copy_buffer) {
74 int x, y;
75 const int limit = 1 << limit_bits;
76 uint32_t* prev_row = copy_buffer;
77 uint32_t* curr_row = prev_row + xsize;
78 uint32_t* next_row = curr_row + xsize;
79 memcpy(copy_buffer, argb, xsize * 2 * sizeof(argb[0]));
80
81 for (y = 1; y < ysize - 1; ++y) {
82 uint32_t* const curr_argb_row = argb + y * xsize;
83 uint32_t* const next_argb_row = curr_argb_row + xsize;
84 memcpy(next_row, next_argb_row, xsize * sizeof(argb[0]));
85 for (x = 1; x < xsize - 1; ++x) {
86 if (!IsSmooth(prev_row, curr_row, next_row, x, limit)) {
87 curr_argb_row[x] = ClosestDiscretizedArgb(curr_row[x], limit_bits);
88 }
89 }
90 {
91 // Three-way swap.
92 uint32_t* const temp = prev_row;
93 prev_row = curr_row;
94 curr_row = next_row;
95 next_row = temp;
96 }
97 }
98 }
99
VP8ApplyNearLossless(int xsize,int ysize,uint32_t * argb,int quality)100 int VP8ApplyNearLossless(int xsize, int ysize, uint32_t* argb, int quality) {
101 int i;
102 uint32_t* const copy_buffer =
103 (uint32_t*)WebPSafeMalloc(xsize * 3, sizeof(*copy_buffer));
104 const int limit_bits = VP8LNearLosslessBits(quality);
105 assert(argb != NULL);
106 assert(limit_bits >= 0);
107 assert(limit_bits <= MAX_LIMIT_BITS);
108 if (copy_buffer == NULL) {
109 return 0;
110 }
111 // For small icon images, don't attempt to apply near-lossless compression.
112 if (xsize < MIN_DIM_FOR_NEAR_LOSSLESS && ysize < MIN_DIM_FOR_NEAR_LOSSLESS) {
113 WebPSafeFree(copy_buffer);
114 return 1;
115 }
116
117 for (i = limit_bits; i != 0; --i) {
118 NearLossless(xsize, ysize, argb, i, copy_buffer);
119 }
120 WebPSafeFree(copy_buffer);
121 return 1;
122 }
123