Path: blob/master/thirdparty/libwebp/src/utils/rescaler_utils.c
9912 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 <limits.h>15#include <stdlib.h>16#include <string.h>17#include "src/dsp/dsp.h"18#include "src/utils/rescaler_utils.h"19#include "src/utils/utils.h"2021//------------------------------------------------------------------------------2223int WebPRescalerInit(WebPRescaler* const rescaler,24int src_width, int src_height,25uint8_t* const dst,26int dst_width, int dst_height, int dst_stride,27int num_channels, rescaler_t* const work) {28const int x_add = src_width, x_sub = dst_width;29const int y_add = src_height, y_sub = dst_height;30const uint64_t total_size = 2ull * dst_width * num_channels * sizeof(*work);31if (!CheckSizeOverflow(total_size)) return 0;3233rescaler->x_expand = (src_width < dst_width);34rescaler->y_expand = (src_height < dst_height);35rescaler->src_width = src_width;36rescaler->src_height = src_height;37rescaler->dst_width = dst_width;38rescaler->dst_height = dst_height;39rescaler->src_y = 0;40rescaler->dst_y = 0;41rescaler->dst = dst;42rescaler->dst_stride = dst_stride;43rescaler->num_channels = num_channels;4445// for 'x_expand', we use bilinear interpolation46rescaler->x_add = rescaler->x_expand ? (x_sub - 1) : x_add;47rescaler->x_sub = rescaler->x_expand ? (x_add - 1) : x_sub;48if (!rescaler->x_expand) { // fx_scale is not used otherwise49rescaler->fx_scale = WEBP_RESCALER_FRAC(1, rescaler->x_sub);50}51// vertical scaling parameters52rescaler->y_add = rescaler->y_expand ? y_add - 1 : y_add;53rescaler->y_sub = rescaler->y_expand ? y_sub - 1 : y_sub;54rescaler->y_accum = rescaler->y_expand ? rescaler->y_sub : rescaler->y_add;55if (!rescaler->y_expand) {56// This is WEBP_RESCALER_FRAC(dst_height, x_add * y_add) without the cast.57// Its value is <= WEBP_RESCALER_ONE, because dst_height <= rescaler->y_add58// and rescaler->x_add >= 1;59const uint64_t num = (uint64_t)dst_height * WEBP_RESCALER_ONE;60const uint64_t den = (uint64_t)rescaler->x_add * rescaler->y_add;61const uint64_t ratio = num / den;62if (ratio != (uint32_t)ratio) {63// When ratio == WEBP_RESCALER_ONE, we can't represent the ratio with the64// current fixed-point precision. This happens when src_height ==65// rescaler->y_add (which == src_height), and rescaler->x_add == 1.66// => We special-case fxy_scale = 0, in WebPRescalerExportRow().67rescaler->fxy_scale = 0;68} else {69rescaler->fxy_scale = (uint32_t)ratio;70}71rescaler->fy_scale = WEBP_RESCALER_FRAC(1, rescaler->y_sub);72} else {73rescaler->fy_scale = WEBP_RESCALER_FRAC(1, rescaler->x_add);74// rescaler->fxy_scale is unused here.75}76rescaler->irow = work;77rescaler->frow = work + num_channels * dst_width;78memset(work, 0, (size_t)total_size);7980WebPRescalerDspInit();81return 1;82}8384int WebPRescalerGetScaledDimensions(int src_width, int src_height,85int* const scaled_width,86int* const scaled_height) {87assert(scaled_width != NULL);88assert(scaled_height != NULL);89{90int width = *scaled_width;91int height = *scaled_height;92const int max_size = INT_MAX / 2;9394// if width is unspecified, scale original proportionally to height ratio.95if (width == 0 && src_height > 0) {96width =97(int)(((uint64_t)src_width * height + src_height - 1) / src_height);98}99// if height is unspecified, scale original proportionally to width ratio.100if (height == 0 && src_width > 0) {101height =102(int)(((uint64_t)src_height * width + src_width - 1) / src_width);103}104// Check if the overall dimensions still make sense.105if (width <= 0 || height <= 0 || width > max_size || height > max_size) {106return 0;107}108109*scaled_width = width;110*scaled_height = height;111return 1;112}113}114115//------------------------------------------------------------------------------116// all-in-one calls117118int WebPRescaleNeededLines(const WebPRescaler* const rescaler,119int max_num_lines) {120const int num_lines =121(rescaler->y_accum + rescaler->y_sub - 1) / rescaler->y_sub;122return (num_lines > max_num_lines) ? max_num_lines : num_lines;123}124125int WebPRescalerImport(WebPRescaler* const rescaler, int num_lines,126const uint8_t* src, int src_stride) {127int total_imported = 0;128while (total_imported < num_lines &&129!WebPRescalerHasPendingOutput(rescaler)) {130if (rescaler->y_expand) {131rescaler_t* const tmp = rescaler->irow;132rescaler->irow = rescaler->frow;133rescaler->frow = tmp;134}135WebPRescalerImportRow(rescaler, src);136if (!rescaler->y_expand) { // Accumulate the contribution of the new row.137int x;138for (x = 0; x < rescaler->num_channels * rescaler->dst_width; ++x) {139rescaler->irow[x] += rescaler->frow[x];140}141}142++rescaler->src_y;143src += src_stride;144++total_imported;145rescaler->y_accum -= rescaler->y_sub;146}147return total_imported;148}149150int WebPRescalerExport(WebPRescaler* const rescaler) {151int total_exported = 0;152while (WebPRescalerHasPendingOutput(rescaler)) {153WebPRescalerExportRow(rescaler);154++total_exported;155}156return total_exported;157}158159//------------------------------------------------------------------------------160161162