Path: blob/master/thirdparty/libwebp/src/utils/quant_levels_utils.c
9912 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>1516#include "src/utils/quant_levels_utils.h"1718#define NUM_SYMBOLS 2561920#define MAX_ITER 6 // Maximum number of convergence steps.21#define ERROR_THRESHOLD 1e-4 // MSE stopping criterion.2223// -----------------------------------------------------------------------------24// Quantize levels.2526int QuantizeLevels(uint8_t* const data, int width, int height,27int num_levels, uint64_t* const sse) {28int freq[NUM_SYMBOLS] = { 0 };29int q_level[NUM_SYMBOLS] = { 0 };30double inv_q_level[NUM_SYMBOLS] = { 0 };31int min_s = 255, max_s = 0;32const size_t data_size = height * width;33int i, num_levels_in, iter;34double last_err = 1.e38, err = 0.;35const double err_threshold = ERROR_THRESHOLD * data_size;3637if (data == NULL) {38return 0;39}4041if (width <= 0 || height <= 0) {42return 0;43}4445if (num_levels < 2 || num_levels > 256) {46return 0;47}4849{50size_t n;51num_levels_in = 0;52for (n = 0; n < data_size; ++n) {53num_levels_in += (freq[data[n]] == 0);54if (min_s > data[n]) min_s = data[n];55if (max_s < data[n]) max_s = data[n];56++freq[data[n]];57}58}5960if (num_levels_in <= num_levels) goto End; // nothing to do!6162// Start with uniformly spread centroids.63for (i = 0; i < num_levels; ++i) {64inv_q_level[i] = min_s + (double)(max_s - min_s) * i / (num_levels - 1);65}6667// Fixed values. Won't be changed.68q_level[min_s] = 0;69q_level[max_s] = num_levels - 1;70assert(inv_q_level[0] == min_s);71assert(inv_q_level[num_levels - 1] == max_s);7273// k-Means iterations.74for (iter = 0; iter < MAX_ITER; ++iter) {75double q_sum[NUM_SYMBOLS] = { 0 };76double q_count[NUM_SYMBOLS] = { 0 };77int s, slot = 0;7879// Assign classes to representatives.80for (s = min_s; s <= max_s; ++s) {81// Keep track of the nearest neighbour 'slot'82while (slot < num_levels - 1 &&832 * s > inv_q_level[slot] + inv_q_level[slot + 1]) {84++slot;85}86if (freq[s] > 0) {87q_sum[slot] += s * freq[s];88q_count[slot] += freq[s];89}90q_level[s] = slot;91}9293// Assign new representatives to classes.94if (num_levels > 2) {95for (slot = 1; slot < num_levels - 1; ++slot) {96const double count = q_count[slot];97if (count > 0.) {98inv_q_level[slot] = q_sum[slot] / count;99}100}101}102103// Compute convergence error.104err = 0.;105for (s = min_s; s <= max_s; ++s) {106const double error = s - inv_q_level[q_level[s]];107err += freq[s] * error * error;108}109110// Check for convergence: we stop as soon as the error is no111// longer improving.112if (last_err - err < err_threshold) break;113last_err = err;114}115116// Remap the alpha plane to quantized values.117{118// double->int rounding operation can be costly, so we do it119// once for all before remapping. We also perform the data[] -> slot120// mapping, while at it (avoid one indirection in the final loop).121uint8_t map[NUM_SYMBOLS];122int s;123size_t n;124for (s = min_s; s <= max_s; ++s) {125const int slot = q_level[s];126map[s] = (uint8_t)(inv_q_level[slot] + .5);127}128// Final pass.129for (n = 0; n < data_size; ++n) {130data[n] = map[data[n]];131}132}133End:134// Store sum of squared error if needed.135if (sse != NULL) *sse = (uint64_t)err;136137return 1;138}139140141142