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