Path: blob/master/3rdparty/libwebp/src/utils/rescaler_utils.c
16358 views
// Copyright 2012 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// Rescaling functions10//11// Author: Skal ([email protected])1213#include <assert.h>14#include <stdlib.h>15#include <string.h>16#include "src/dsp/dsp.h"17#include "src/utils/rescaler_utils.h"1819//------------------------------------------------------------------------------2021void WebPRescalerInit(WebPRescaler* const wrk, int src_width, int src_height,22uint8_t* const dst,23int dst_width, int dst_height, int dst_stride,24int num_channels, rescaler_t* const work) {25const int x_add = src_width, x_sub = dst_width;26const int y_add = src_height, y_sub = dst_height;27wrk->x_expand = (src_width < dst_width);28wrk->y_expand = (src_height < dst_height);29wrk->src_width = src_width;30wrk->src_height = src_height;31wrk->dst_width = dst_width;32wrk->dst_height = dst_height;33wrk->src_y = 0;34wrk->dst_y = 0;35wrk->dst = dst;36wrk->dst_stride = dst_stride;37wrk->num_channels = num_channels;3839// for 'x_expand', we use bilinear interpolation40wrk->x_add = wrk->x_expand ? (x_sub - 1) : x_add;41wrk->x_sub = wrk->x_expand ? (x_add - 1) : x_sub;42if (!wrk->x_expand) { // fx_scale is not used otherwise43wrk->fx_scale = WEBP_RESCALER_FRAC(1, wrk->x_sub);44}45// vertical scaling parameters46wrk->y_add = wrk->y_expand ? y_add - 1 : y_add;47wrk->y_sub = wrk->y_expand ? y_sub - 1 : y_sub;48wrk->y_accum = wrk->y_expand ? wrk->y_sub : wrk->y_add;49if (!wrk->y_expand) {50// This is WEBP_RESCALER_FRAC(dst_height, x_add * y_add) without the cast.51// Its value is <= WEBP_RESCALER_ONE, because dst_height <= wrk->y_add, and52// wrk->x_add >= 1;53const uint64_t ratio =54(uint64_t)dst_height * WEBP_RESCALER_ONE / (wrk->x_add * wrk->y_add);55if (ratio != (uint32_t)ratio) {56// When ratio == WEBP_RESCALER_ONE, we can't represent the ratio with the57// current fixed-point precision. This happens when src_height ==58// wrk->y_add (which == src_height), and wrk->x_add == 1.59// => We special-case fxy_scale = 0, in WebPRescalerExportRow().60wrk->fxy_scale = 0;61} else {62wrk->fxy_scale = (uint32_t)ratio;63}64wrk->fy_scale = WEBP_RESCALER_FRAC(1, wrk->y_sub);65} else {66wrk->fy_scale = WEBP_RESCALER_FRAC(1, wrk->x_add);67// wrk->fxy_scale is unused here.68}69wrk->irow = work;70wrk->frow = work + num_channels * dst_width;71memset(work, 0, 2 * dst_width * num_channels * sizeof(*work));7273WebPRescalerDspInit();74}7576int WebPRescalerGetScaledDimensions(int src_width, int src_height,77int* const scaled_width,78int* const scaled_height) {79assert(scaled_width != NULL);80assert(scaled_height != NULL);81{82int width = *scaled_width;83int height = *scaled_height;8485// if width is unspecified, scale original proportionally to height ratio.86if (width == 0) {87width =88(int)(((uint64_t)src_width * height + src_height / 2) / src_height);89}90// if height is unspecified, scale original proportionally to width ratio.91if (height == 0) {92height =93(int)(((uint64_t)src_height * width + src_width / 2) / src_width);94}95// Check if the overall dimensions still make sense.96if (width <= 0 || height <= 0) {97return 0;98}99100*scaled_width = width;101*scaled_height = height;102return 1;103}104}105106//------------------------------------------------------------------------------107// all-in-one calls108109int WebPRescaleNeededLines(const WebPRescaler* const wrk, int max_num_lines) {110const int num_lines = (wrk->y_accum + wrk->y_sub - 1) / wrk->y_sub;111return (num_lines > max_num_lines) ? max_num_lines : num_lines;112}113114int WebPRescalerImport(WebPRescaler* const wrk, int num_lines,115const uint8_t* src, int src_stride) {116int total_imported = 0;117while (total_imported < num_lines && !WebPRescalerHasPendingOutput(wrk)) {118if (wrk->y_expand) {119rescaler_t* const tmp = wrk->irow;120wrk->irow = wrk->frow;121wrk->frow = tmp;122}123WebPRescalerImportRow(wrk, src);124if (!wrk->y_expand) { // Accumulate the contribution of the new row.125int x;126for (x = 0; x < wrk->num_channels * wrk->dst_width; ++x) {127wrk->irow[x] += wrk->frow[x];128}129}130++wrk->src_y;131src += src_stride;132++total_imported;133wrk->y_accum -= wrk->y_sub;134}135return total_imported;136}137138int WebPRescalerExport(WebPRescaler* const rescaler) {139int total_exported = 0;140while (WebPRescalerHasPendingOutput(rescaler)) {141WebPRescalerExportRow(rescaler);142++total_exported;143}144return total_exported;145}146147//------------------------------------------------------------------------------148149150