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