2012-03-22 11:30:20 +01:00
|
|
|
// Copyright 2012 Google Inc. All Rights Reserved.
|
|
|
|
//
|
2013-06-07 08:05:58 +02:00
|
|
|
// Use of this source code is governed by a BSD-style license
|
|
|
|
// that can be found in the COPYING file in the root of the source
|
|
|
|
// tree. An additional intellectual property rights grant can be found
|
|
|
|
// in the file PATENTS. All contributing project authors may
|
|
|
|
// be found in the AUTHORS file in the root of the source tree.
|
2012-03-22 11:30:20 +01:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Rescaling functions
|
|
|
|
//
|
|
|
|
// Author: Skal (pascal.massimino@gmail.com)
|
|
|
|
|
|
|
|
#include <assert.h>
|
2021-06-14 21:20:45 +02:00
|
|
|
#include <limits.h>
|
2012-03-22 11:30:20 +01:00
|
|
|
#include <stdlib.h>
|
2015-08-15 04:07:13 +02:00
|
|
|
#include <string.h>
|
2017-10-07 23:15:11 +02:00
|
|
|
#include "src/dsp/dsp.h"
|
|
|
|
#include "src/utils/rescaler_utils.h"
|
2021-06-25 15:23:16 +02:00
|
|
|
#include "src/utils/utils.h"
|
2012-03-22 11:30:20 +01:00
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
2015-01-10 15:46:00 +01:00
|
|
|
|
2021-06-25 15:23:16 +02:00
|
|
|
int WebPRescalerInit(WebPRescaler* const rescaler,
|
|
|
|
int src_width, int src_height,
|
2021-06-28 11:08:50 +02:00
|
|
|
uint8_t* const dst,
|
|
|
|
int dst_width, int dst_height, int dst_stride,
|
|
|
|
int num_channels, rescaler_t* const work) {
|
2015-08-05 15:11:48 +02:00
|
|
|
const int x_add = src_width, x_sub = dst_width;
|
|
|
|
const int y_add = src_height, y_sub = dst_height;
|
2021-06-25 15:23:16 +02:00
|
|
|
const uint64_t total_size = 2ull * dst_width * num_channels * sizeof(*work);
|
|
|
|
if (!CheckSizeOverflow(total_size)) return 0;
|
|
|
|
|
|
|
|
rescaler->x_expand = (src_width < dst_width);
|
|
|
|
rescaler->y_expand = (src_height < dst_height);
|
|
|
|
rescaler->src_width = src_width;
|
|
|
|
rescaler->src_height = src_height;
|
|
|
|
rescaler->dst_width = dst_width;
|
|
|
|
rescaler->dst_height = dst_height;
|
|
|
|
rescaler->src_y = 0;
|
|
|
|
rescaler->dst_y = 0;
|
|
|
|
rescaler->dst = dst;
|
|
|
|
rescaler->dst_stride = dst_stride;
|
|
|
|
rescaler->num_channels = num_channels;
|
2015-08-19 06:33:18 +02:00
|
|
|
|
2015-01-10 15:46:00 +01:00
|
|
|
// for 'x_expand', we use bilinear interpolation
|
2021-06-25 15:23:16 +02:00
|
|
|
rescaler->x_add = rescaler->x_expand ? (x_sub - 1) : x_add;
|
|
|
|
rescaler->x_sub = rescaler->x_expand ? (x_add - 1) : x_sub;
|
|
|
|
if (!rescaler->x_expand) { // fx_scale is not used otherwise
|
|
|
|
rescaler->fx_scale = WEBP_RESCALER_FRAC(1, rescaler->x_sub);
|
2015-08-05 15:11:48 +02:00
|
|
|
}
|
2015-08-19 06:33:18 +02:00
|
|
|
// vertical scaling parameters
|
2021-06-25 15:23:16 +02:00
|
|
|
rescaler->y_add = rescaler->y_expand ? y_add - 1 : y_add;
|
|
|
|
rescaler->y_sub = rescaler->y_expand ? y_sub - 1 : y_sub;
|
|
|
|
rescaler->y_accum = rescaler->y_expand ? rescaler->y_sub : rescaler->y_add;
|
|
|
|
if (!rescaler->y_expand) {
|
2016-08-11 20:28:40 +02:00
|
|
|
// This is WEBP_RESCALER_FRAC(dst_height, x_add * y_add) without the cast.
|
2021-06-25 15:23:16 +02:00
|
|
|
// Its value is <= WEBP_RESCALER_ONE, because dst_height <= rescaler->y_add
|
|
|
|
// and rescaler->x_add >= 1;
|
2021-04-20 23:26:06 +02:00
|
|
|
const uint64_t num = (uint64_t)dst_height * WEBP_RESCALER_ONE;
|
2021-06-25 15:23:16 +02:00
|
|
|
const uint64_t den = (uint64_t)rescaler->x_add * rescaler->y_add;
|
2021-04-20 23:26:06 +02:00
|
|
|
const uint64_t ratio = num / den;
|
2015-10-06 09:15:18 +02:00
|
|
|
if (ratio != (uint32_t)ratio) {
|
2016-08-11 20:28:40 +02:00
|
|
|
// When ratio == WEBP_RESCALER_ONE, we can't represent the ratio with the
|
|
|
|
// current fixed-point precision. This happens when src_height ==
|
2021-06-25 15:23:16 +02:00
|
|
|
// rescaler->y_add (which == src_height), and rescaler->x_add == 1.
|
2015-10-06 09:15:18 +02:00
|
|
|
// => We special-case fxy_scale = 0, in WebPRescalerExportRow().
|
2021-06-25 15:23:16 +02:00
|
|
|
rescaler->fxy_scale = 0;
|
2015-10-06 09:15:18 +02:00
|
|
|
} else {
|
2021-06-25 15:23:16 +02:00
|
|
|
rescaler->fxy_scale = (uint32_t)ratio;
|
2015-10-06 09:15:18 +02:00
|
|
|
}
|
2021-06-25 15:23:16 +02:00
|
|
|
rescaler->fy_scale = WEBP_RESCALER_FRAC(1, rescaler->y_sub);
|
2015-09-18 10:45:03 +02:00
|
|
|
} else {
|
2021-06-25 15:23:16 +02:00
|
|
|
rescaler->fy_scale = WEBP_RESCALER_FRAC(1, rescaler->x_add);
|
|
|
|
// rescaler->fxy_scale is unused here.
|
2015-09-18 10:45:03 +02:00
|
|
|
}
|
2021-06-25 15:23:16 +02:00
|
|
|
rescaler->irow = work;
|
|
|
|
rescaler->frow = work + num_channels * dst_width;
|
|
|
|
memset(work, 0, (size_t)total_size);
|
2015-01-10 15:46:00 +01:00
|
|
|
|
|
|
|
WebPRescalerDspInit();
|
2021-06-25 15:23:16 +02:00
|
|
|
return 1;
|
2015-01-10 15:46:00 +01:00
|
|
|
}
|
|
|
|
|
2015-08-14 05:50:38 +02:00
|
|
|
int WebPRescalerGetScaledDimensions(int src_width, int src_height,
|
|
|
|
int* const scaled_width,
|
|
|
|
int* const scaled_height) {
|
|
|
|
assert(scaled_width != NULL);
|
|
|
|
assert(scaled_height != NULL);
|
|
|
|
{
|
|
|
|
int width = *scaled_width;
|
|
|
|
int height = *scaled_height;
|
2021-06-14 21:20:45 +02:00
|
|
|
const int max_size = INT_MAX / 2;
|
2015-08-14 05:50:38 +02:00
|
|
|
|
|
|
|
// if width is unspecified, scale original proportionally to height ratio.
|
2019-02-18 14:34:03 +01:00
|
|
|
if (width == 0 && src_height > 0) {
|
2017-08-31 11:56:28 +02:00
|
|
|
width =
|
2019-02-18 14:34:03 +01:00
|
|
|
(int)(((uint64_t)src_width * height + src_height - 1) / src_height);
|
2015-08-14 05:50:38 +02:00
|
|
|
}
|
|
|
|
// if height is unspecified, scale original proportionally to width ratio.
|
2019-02-18 14:34:03 +01:00
|
|
|
if (height == 0 && src_width > 0) {
|
2017-08-31 11:56:28 +02:00
|
|
|
height =
|
2019-02-18 14:34:03 +01:00
|
|
|
(int)(((uint64_t)src_height * width + src_width - 1) / src_width);
|
2015-08-14 05:50:38 +02:00
|
|
|
}
|
|
|
|
// Check if the overall dimensions still make sense.
|
2021-06-14 21:20:45 +02:00
|
|
|
if (width <= 0 || height <= 0 || width > max_size || height > max_size) {
|
2015-08-14 05:50:38 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
*scaled_width = width;
|
|
|
|
*scaled_height = height;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-10 15:46:00 +01:00
|
|
|
//------------------------------------------------------------------------------
|
2012-03-22 11:30:20 +01:00
|
|
|
// all-in-one calls
|
|
|
|
|
2021-02-10 22:14:45 +01:00
|
|
|
int WebPRescaleNeededLines(const WebPRescaler* const rescaler,
|
|
|
|
int max_num_lines) {
|
|
|
|
const int num_lines =
|
|
|
|
(rescaler->y_accum + rescaler->y_sub - 1) / rescaler->y_sub;
|
2013-07-26 21:05:42 +02:00
|
|
|
return (num_lines > max_num_lines) ? max_num_lines : num_lines;
|
|
|
|
}
|
|
|
|
|
2021-02-10 22:14:45 +01:00
|
|
|
int WebPRescalerImport(WebPRescaler* const rescaler, int num_lines,
|
2012-03-22 11:30:20 +01:00
|
|
|
const uint8_t* src, int src_stride) {
|
|
|
|
int total_imported = 0;
|
2021-02-10 22:14:45 +01:00
|
|
|
while (total_imported < num_lines &&
|
|
|
|
!WebPRescalerHasPendingOutput(rescaler)) {
|
|
|
|
if (rescaler->y_expand) {
|
|
|
|
rescaler_t* const tmp = rescaler->irow;
|
|
|
|
rescaler->irow = rescaler->frow;
|
|
|
|
rescaler->frow = tmp;
|
2015-09-18 10:45:03 +02:00
|
|
|
}
|
2021-02-10 22:14:45 +01:00
|
|
|
WebPRescalerImportRow(rescaler, src);
|
|
|
|
if (!rescaler->y_expand) { // Accumulate the contribution of the new row.
|
2015-09-19 18:59:32 +02:00
|
|
|
int x;
|
2021-02-10 22:14:45 +01:00
|
|
|
for (x = 0; x < rescaler->num_channels * rescaler->dst_width; ++x) {
|
|
|
|
rescaler->irow[x] += rescaler->frow[x];
|
2015-09-18 10:45:03 +02:00
|
|
|
}
|
|
|
|
}
|
2021-02-10 22:14:45 +01:00
|
|
|
++rescaler->src_y;
|
2012-03-22 11:30:20 +01:00
|
|
|
src += src_stride;
|
|
|
|
++total_imported;
|
2021-02-10 22:14:45 +01:00
|
|
|
rescaler->y_accum -= rescaler->y_sub;
|
2012-03-22 11:30:20 +01:00
|
|
|
}
|
|
|
|
return total_imported;
|
|
|
|
}
|
|
|
|
|
|
|
|
int WebPRescalerExport(WebPRescaler* const rescaler) {
|
|
|
|
int total_exported = 0;
|
|
|
|
while (WebPRescalerHasPendingOutput(rescaler)) {
|
2015-09-18 10:45:03 +02:00
|
|
|
WebPRescalerExportRow(rescaler);
|
2012-03-22 11:30:20 +01:00
|
|
|
++total_exported;
|
|
|
|
}
|
|
|
|
return total_exported;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|