/src/libwebp/src/utils/quant_levels_utils.c
Line | Count | Source |
1 | | // Copyright 2011 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 | | // Quantize levels for specified number of quantization-levels ([2, 256]). |
11 | | // Min and max values are preserved (usual 0 and 255 for alpha plane). |
12 | | // |
13 | | // Author: Skal (pascal.massimino@gmail.com) |
14 | | |
15 | | #include "src/utils/quant_levels_utils.h" |
16 | | |
17 | | #include <assert.h> |
18 | | #include <stddef.h> |
19 | | |
20 | | #include "src/utils/bounds_safety.h" |
21 | | #include "src/webp/types.h" |
22 | | |
23 | | #define NUM_SYMBOLS 256 |
24 | | |
25 | 0 | #define MAX_ITER 6 // Maximum number of convergence steps. |
26 | 0 | #define ERROR_THRESHOLD 1e-4 // MSE stopping criterion. |
27 | | |
28 | | WEBP_ASSUME_UNSAFE_INDEXABLE_ABI |
29 | | |
30 | | // ----------------------------------------------------------------------------- |
31 | | // Quantize levels. |
32 | | |
33 | | int QuantizeLevels(uint8_t* const WEBP_COUNTED_BY((size_t)width* height) data, |
34 | 0 | int width, int height, int num_levels, uint64_t* const sse) { |
35 | 0 | int freq[NUM_SYMBOLS] = {0}; |
36 | 0 | int q_level[NUM_SYMBOLS] = {0}; |
37 | 0 | double inv_q_level[NUM_SYMBOLS] = {0}; |
38 | 0 | int min_s = 255, max_s = 0; |
39 | 0 | const size_t data_size = height * width; |
40 | 0 | int i, num_levels_in, iter; |
41 | 0 | double last_err = 1.e38, err = 0.; |
42 | 0 | const double err_threshold = ERROR_THRESHOLD * data_size; |
43 | |
|
44 | 0 | if (data == NULL) { |
45 | 0 | return 0; |
46 | 0 | } |
47 | | |
48 | 0 | if (width <= 0 || height <= 0) { |
49 | 0 | return 0; |
50 | 0 | } |
51 | | |
52 | 0 | if (num_levels < 2 || num_levels > 256) { |
53 | 0 | return 0; |
54 | 0 | } |
55 | | |
56 | 0 | { |
57 | 0 | size_t n; |
58 | 0 | num_levels_in = 0; |
59 | 0 | for (n = 0; n < data_size; ++n) { |
60 | 0 | num_levels_in += (freq[data[n]] == 0); |
61 | 0 | if (min_s > data[n]) min_s = data[n]; |
62 | 0 | if (max_s < data[n]) max_s = data[n]; |
63 | 0 | ++freq[data[n]]; |
64 | 0 | } |
65 | 0 | } |
66 | |
|
67 | 0 | if (num_levels_in <= num_levels) goto End; // nothing to do! |
68 | | |
69 | | // Start with uniformly spread centroids. |
70 | 0 | for (i = 0; i < num_levels; ++i) { |
71 | 0 | inv_q_level[i] = min_s + (double)(max_s - min_s) * i / (num_levels - 1); |
72 | 0 | } |
73 | | |
74 | | // Fixed values. Won't be changed. |
75 | 0 | q_level[min_s] = 0; |
76 | 0 | q_level[max_s] = num_levels - 1; |
77 | 0 | assert(inv_q_level[0] == min_s); |
78 | 0 | assert(inv_q_level[num_levels - 1] == max_s); |
79 | | |
80 | | // k-Means iterations. |
81 | 0 | for (iter = 0; iter < MAX_ITER; ++iter) { |
82 | 0 | double q_sum[NUM_SYMBOLS] = {0}; |
83 | 0 | double q_count[NUM_SYMBOLS] = {0}; |
84 | 0 | int s, slot = 0; |
85 | | |
86 | | // Assign classes to representatives. |
87 | 0 | for (s = min_s; s <= max_s; ++s) { |
88 | | // Keep track of the nearest neighbour 'slot' |
89 | 0 | while (slot < num_levels - 1 && |
90 | 0 | 2 * s > inv_q_level[slot] + inv_q_level[slot + 1]) { |
91 | 0 | ++slot; |
92 | 0 | } |
93 | 0 | if (freq[s] > 0) { |
94 | 0 | q_sum[slot] += s * freq[s]; |
95 | 0 | q_count[slot] += freq[s]; |
96 | 0 | } |
97 | 0 | q_level[s] = slot; |
98 | 0 | } |
99 | | |
100 | | // Assign new representatives to classes. |
101 | 0 | if (num_levels > 2) { |
102 | 0 | for (slot = 1; slot < num_levels - 1; ++slot) { |
103 | 0 | const double count = q_count[slot]; |
104 | 0 | if (count > 0.) { |
105 | 0 | inv_q_level[slot] = q_sum[slot] / count; |
106 | 0 | } |
107 | 0 | } |
108 | 0 | } |
109 | | |
110 | | // Compute convergence error. |
111 | 0 | err = 0.; |
112 | 0 | for (s = min_s; s <= max_s; ++s) { |
113 | 0 | const double error = s - inv_q_level[q_level[s]]; |
114 | 0 | err += freq[s] * error * error; |
115 | 0 | } |
116 | | |
117 | | // Check for convergence: we stop as soon as the error is no |
118 | | // longer improving. |
119 | 0 | if (last_err - err < err_threshold) break; |
120 | 0 | last_err = err; |
121 | 0 | } |
122 | | |
123 | | // Remap the alpha plane to quantized values. |
124 | 0 | { |
125 | | // double->int rounding operation can be costly, so we do it |
126 | | // once for all before remapping. We also perform the data[] -> slot |
127 | | // mapping, while at it (avoid one indirection in the final loop). |
128 | 0 | uint8_t map[NUM_SYMBOLS]; |
129 | 0 | int s; |
130 | 0 | size_t n; |
131 | 0 | for (s = min_s; s <= max_s; ++s) { |
132 | 0 | const int slot = q_level[s]; |
133 | 0 | map[s] = (uint8_t)(inv_q_level[slot] + .5); |
134 | 0 | } |
135 | | // Final pass. |
136 | 0 | for (n = 0; n < data_size; ++n) { |
137 | 0 | data[n] = map[data[n]]; |
138 | 0 | } |
139 | 0 | } |
140 | 0 | End: |
141 | | // Store sum of squared error if needed. |
142 | 0 | if (sse != NULL) *sse = (uint64_t)err; |
143 | |
|
144 | 0 | return 1; |
145 | 0 | } |