Path: blob/master/thirdparty/libwebp/src/utils/quant_levels_utils.c
20969 views
// Copyright 2011 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// Quantize levels for specified number of quantization-levels ([2, 256]).10// Min and max values are preserved (usual 0 and 255 for alpha plane).11//12// Author: Skal ([email protected])1314#include <assert.h>15#include <stddef.h>1617#include "src/webp/types.h"18#include "src/utils/quant_levels_utils.h"1920#define NUM_SYMBOLS 2562122#define MAX_ITER 6 // Maximum number of convergence steps.23#define ERROR_THRESHOLD 1e-4 // MSE stopping criterion.2425// -----------------------------------------------------------------------------26// Quantize levels.2728int QuantizeLevels(uint8_t* const data, int width, int height,29int num_levels, uint64_t* const sse) {30int freq[NUM_SYMBOLS] = { 0 };31int q_level[NUM_SYMBOLS] = { 0 };32double inv_q_level[NUM_SYMBOLS] = { 0 };33int min_s = 255, max_s = 0;34const size_t data_size = height * width;35int i, num_levels_in, iter;36double last_err = 1.e38, err = 0.;37const double err_threshold = ERROR_THRESHOLD * data_size;3839if (data == NULL) {40return 0;41}4243if (width <= 0 || height <= 0) {44return 0;45}4647if (num_levels < 2 || num_levels > 256) {48return 0;49}5051{52size_t n;53num_levels_in = 0;54for (n = 0; n < data_size; ++n) {55num_levels_in += (freq[data[n]] == 0);56if (min_s > data[n]) min_s = data[n];57if (max_s < data[n]) max_s = data[n];58++freq[data[n]];59}60}6162if (num_levels_in <= num_levels) goto End; // nothing to do!6364// Start with uniformly spread centroids.65for (i = 0; i < num_levels; ++i) {66inv_q_level[i] = min_s + (double)(max_s - min_s) * i / (num_levels - 1);67}6869// Fixed values. Won't be changed.70q_level[min_s] = 0;71q_level[max_s] = num_levels - 1;72assert(inv_q_level[0] == min_s);73assert(inv_q_level[num_levels - 1] == max_s);7475// k-Means iterations.76for (iter = 0; iter < MAX_ITER; ++iter) {77double q_sum[NUM_SYMBOLS] = { 0 };78double q_count[NUM_SYMBOLS] = { 0 };79int s, slot = 0;8081// Assign classes to representatives.82for (s = min_s; s <= max_s; ++s) {83// Keep track of the nearest neighbour 'slot'84while (slot < num_levels - 1 &&852 * s > inv_q_level[slot] + inv_q_level[slot + 1]) {86++slot;87}88if (freq[s] > 0) {89q_sum[slot] += s * freq[s];90q_count[slot] += freq[s];91}92q_level[s] = slot;93}9495// Assign new representatives to classes.96if (num_levels > 2) {97for (slot = 1; slot < num_levels - 1; ++slot) {98const double count = q_count[slot];99if (count > 0.) {100inv_q_level[slot] = q_sum[slot] / count;101}102}103}104105// Compute convergence error.106err = 0.;107for (s = min_s; s <= max_s; ++s) {108const double error = s - inv_q_level[q_level[s]];109err += freq[s] * error * error;110}111112// Check for convergence: we stop as soon as the error is no113// longer improving.114if (last_err - err < err_threshold) break;115last_err = err;116}117118// Remap the alpha plane to quantized values.119{120// double->int rounding operation can be costly, so we do it121// once for all before remapping. We also perform the data[] -> slot122// mapping, while at it (avoid one indirection in the final loop).123uint8_t map[NUM_SYMBOLS];124int s;125size_t n;126for (s = min_s; s <= max_s; ++s) {127const int slot = q_level[s];128map[s] = (uint8_t)(inv_q_level[slot] + .5);129}130// Final pass.131for (n = 0; n < data_size; ++n) {132data[n] = map[data[n]];133}134}135End:136// Store sum of squared error if needed.137if (sse != NULL) *sse = (uint64_t)err;138139return 1;140}141142143