1*b2055c35SXin Li // Copyright 2012 Google Inc. All Rights Reserved.
2*b2055c35SXin Li //
3*b2055c35SXin Li // Use of this source code is governed by a BSD-style license
4*b2055c35SXin Li // that can be found in the COPYING file in the root of the source
5*b2055c35SXin Li // tree. An additional intellectual property rights grant can be found
6*b2055c35SXin Li // in the file PATENTS. All contributing project authors may
7*b2055c35SXin Li // be found in the AUTHORS file in the root of the source tree.
8*b2055c35SXin Li // -----------------------------------------------------------------------------
9*b2055c35SXin Li //
10*b2055c35SXin Li // Rescaling functions
11*b2055c35SXin Li //
12*b2055c35SXin Li // Author: Skal ([email protected])
13*b2055c35SXin Li
14*b2055c35SXin Li #include <assert.h>
15*b2055c35SXin Li #include <limits.h>
16*b2055c35SXin Li #include <stdlib.h>
17*b2055c35SXin Li #include <string.h>
18*b2055c35SXin Li #include "src/dsp/dsp.h"
19*b2055c35SXin Li #include "src/utils/rescaler_utils.h"
20*b2055c35SXin Li #include "src/utils/utils.h"
21*b2055c35SXin Li
22*b2055c35SXin Li //------------------------------------------------------------------------------
23*b2055c35SXin Li
WebPRescalerInit(WebPRescaler * const rescaler,int src_width,int src_height,uint8_t * const dst,int dst_width,int dst_height,int dst_stride,int num_channels,rescaler_t * const work)24*b2055c35SXin Li int WebPRescalerInit(WebPRescaler* const rescaler,
25*b2055c35SXin Li int src_width, int src_height,
26*b2055c35SXin Li uint8_t* const dst,
27*b2055c35SXin Li int dst_width, int dst_height, int dst_stride,
28*b2055c35SXin Li int num_channels, rescaler_t* const work) {
29*b2055c35SXin Li const int x_add = src_width, x_sub = dst_width;
30*b2055c35SXin Li const int y_add = src_height, y_sub = dst_height;
31*b2055c35SXin Li const uint64_t total_size = 2ull * dst_width * num_channels * sizeof(*work);
32*b2055c35SXin Li if (!CheckSizeOverflow(total_size)) return 0;
33*b2055c35SXin Li
34*b2055c35SXin Li rescaler->x_expand = (src_width < dst_width);
35*b2055c35SXin Li rescaler->y_expand = (src_height < dst_height);
36*b2055c35SXin Li rescaler->src_width = src_width;
37*b2055c35SXin Li rescaler->src_height = src_height;
38*b2055c35SXin Li rescaler->dst_width = dst_width;
39*b2055c35SXin Li rescaler->dst_height = dst_height;
40*b2055c35SXin Li rescaler->src_y = 0;
41*b2055c35SXin Li rescaler->dst_y = 0;
42*b2055c35SXin Li rescaler->dst = dst;
43*b2055c35SXin Li rescaler->dst_stride = dst_stride;
44*b2055c35SXin Li rescaler->num_channels = num_channels;
45*b2055c35SXin Li
46*b2055c35SXin Li // for 'x_expand', we use bilinear interpolation
47*b2055c35SXin Li rescaler->x_add = rescaler->x_expand ? (x_sub - 1) : x_add;
48*b2055c35SXin Li rescaler->x_sub = rescaler->x_expand ? (x_add - 1) : x_sub;
49*b2055c35SXin Li if (!rescaler->x_expand) { // fx_scale is not used otherwise
50*b2055c35SXin Li rescaler->fx_scale = WEBP_RESCALER_FRAC(1, rescaler->x_sub);
51*b2055c35SXin Li }
52*b2055c35SXin Li // vertical scaling parameters
53*b2055c35SXin Li rescaler->y_add = rescaler->y_expand ? y_add - 1 : y_add;
54*b2055c35SXin Li rescaler->y_sub = rescaler->y_expand ? y_sub - 1 : y_sub;
55*b2055c35SXin Li rescaler->y_accum = rescaler->y_expand ? rescaler->y_sub : rescaler->y_add;
56*b2055c35SXin Li if (!rescaler->y_expand) {
57*b2055c35SXin Li // This is WEBP_RESCALER_FRAC(dst_height, x_add * y_add) without the cast.
58*b2055c35SXin Li // Its value is <= WEBP_RESCALER_ONE, because dst_height <= rescaler->y_add
59*b2055c35SXin Li // and rescaler->x_add >= 1;
60*b2055c35SXin Li const uint64_t num = (uint64_t)dst_height * WEBP_RESCALER_ONE;
61*b2055c35SXin Li const uint64_t den = (uint64_t)rescaler->x_add * rescaler->y_add;
62*b2055c35SXin Li const uint64_t ratio = num / den;
63*b2055c35SXin Li if (ratio != (uint32_t)ratio) {
64*b2055c35SXin Li // When ratio == WEBP_RESCALER_ONE, we can't represent the ratio with the
65*b2055c35SXin Li // current fixed-point precision. This happens when src_height ==
66*b2055c35SXin Li // rescaler->y_add (which == src_height), and rescaler->x_add == 1.
67*b2055c35SXin Li // => We special-case fxy_scale = 0, in WebPRescalerExportRow().
68*b2055c35SXin Li rescaler->fxy_scale = 0;
69*b2055c35SXin Li } else {
70*b2055c35SXin Li rescaler->fxy_scale = (uint32_t)ratio;
71*b2055c35SXin Li }
72*b2055c35SXin Li rescaler->fy_scale = WEBP_RESCALER_FRAC(1, rescaler->y_sub);
73*b2055c35SXin Li } else {
74*b2055c35SXin Li rescaler->fy_scale = WEBP_RESCALER_FRAC(1, rescaler->x_add);
75*b2055c35SXin Li // rescaler->fxy_scale is unused here.
76*b2055c35SXin Li }
77*b2055c35SXin Li rescaler->irow = work;
78*b2055c35SXin Li rescaler->frow = work + num_channels * dst_width;
79*b2055c35SXin Li memset(work, 0, (size_t)total_size);
80*b2055c35SXin Li
81*b2055c35SXin Li WebPRescalerDspInit();
82*b2055c35SXin Li return 1;
83*b2055c35SXin Li }
84*b2055c35SXin Li
WebPRescalerGetScaledDimensions(int src_width,int src_height,int * const scaled_width,int * const scaled_height)85*b2055c35SXin Li int WebPRescalerGetScaledDimensions(int src_width, int src_height,
86*b2055c35SXin Li int* const scaled_width,
87*b2055c35SXin Li int* const scaled_height) {
88*b2055c35SXin Li assert(scaled_width != NULL);
89*b2055c35SXin Li assert(scaled_height != NULL);
90*b2055c35SXin Li {
91*b2055c35SXin Li int width = *scaled_width;
92*b2055c35SXin Li int height = *scaled_height;
93*b2055c35SXin Li const int max_size = INT_MAX / 2;
94*b2055c35SXin Li
95*b2055c35SXin Li // if width is unspecified, scale original proportionally to height ratio.
96*b2055c35SXin Li if (width == 0 && src_height > 0) {
97*b2055c35SXin Li width =
98*b2055c35SXin Li (int)(((uint64_t)src_width * height + src_height - 1) / src_height);
99*b2055c35SXin Li }
100*b2055c35SXin Li // if height is unspecified, scale original proportionally to width ratio.
101*b2055c35SXin Li if (height == 0 && src_width > 0) {
102*b2055c35SXin Li height =
103*b2055c35SXin Li (int)(((uint64_t)src_height * width + src_width - 1) / src_width);
104*b2055c35SXin Li }
105*b2055c35SXin Li // Check if the overall dimensions still make sense.
106*b2055c35SXin Li if (width <= 0 || height <= 0 || width > max_size || height > max_size) {
107*b2055c35SXin Li return 0;
108*b2055c35SXin Li }
109*b2055c35SXin Li
110*b2055c35SXin Li *scaled_width = width;
111*b2055c35SXin Li *scaled_height = height;
112*b2055c35SXin Li return 1;
113*b2055c35SXin Li }
114*b2055c35SXin Li }
115*b2055c35SXin Li
116*b2055c35SXin Li //------------------------------------------------------------------------------
117*b2055c35SXin Li // all-in-one calls
118*b2055c35SXin Li
WebPRescaleNeededLines(const WebPRescaler * const rescaler,int max_num_lines)119*b2055c35SXin Li int WebPRescaleNeededLines(const WebPRescaler* const rescaler,
120*b2055c35SXin Li int max_num_lines) {
121*b2055c35SXin Li const int num_lines =
122*b2055c35SXin Li (rescaler->y_accum + rescaler->y_sub - 1) / rescaler->y_sub;
123*b2055c35SXin Li return (num_lines > max_num_lines) ? max_num_lines : num_lines;
124*b2055c35SXin Li }
125*b2055c35SXin Li
WebPRescalerImport(WebPRescaler * const rescaler,int num_lines,const uint8_t * src,int src_stride)126*b2055c35SXin Li int WebPRescalerImport(WebPRescaler* const rescaler, int num_lines,
127*b2055c35SXin Li const uint8_t* src, int src_stride) {
128*b2055c35SXin Li int total_imported = 0;
129*b2055c35SXin Li while (total_imported < num_lines &&
130*b2055c35SXin Li !WebPRescalerHasPendingOutput(rescaler)) {
131*b2055c35SXin Li if (rescaler->y_expand) {
132*b2055c35SXin Li rescaler_t* const tmp = rescaler->irow;
133*b2055c35SXin Li rescaler->irow = rescaler->frow;
134*b2055c35SXin Li rescaler->frow = tmp;
135*b2055c35SXin Li }
136*b2055c35SXin Li WebPRescalerImportRow(rescaler, src);
137*b2055c35SXin Li if (!rescaler->y_expand) { // Accumulate the contribution of the new row.
138*b2055c35SXin Li int x;
139*b2055c35SXin Li for (x = 0; x < rescaler->num_channels * rescaler->dst_width; ++x) {
140*b2055c35SXin Li rescaler->irow[x] += rescaler->frow[x];
141*b2055c35SXin Li }
142*b2055c35SXin Li }
143*b2055c35SXin Li ++rescaler->src_y;
144*b2055c35SXin Li src += src_stride;
145*b2055c35SXin Li ++total_imported;
146*b2055c35SXin Li rescaler->y_accum -= rescaler->y_sub;
147*b2055c35SXin Li }
148*b2055c35SXin Li return total_imported;
149*b2055c35SXin Li }
150*b2055c35SXin Li
WebPRescalerExport(WebPRescaler * const rescaler)151*b2055c35SXin Li int WebPRescalerExport(WebPRescaler* const rescaler) {
152*b2055c35SXin Li int total_exported = 0;
153*b2055c35SXin Li while (WebPRescalerHasPendingOutput(rescaler)) {
154*b2055c35SXin Li WebPRescalerExportRow(rescaler);
155*b2055c35SXin Li ++total_exported;
156*b2055c35SXin Li }
157*b2055c35SXin Li return total_exported;
158*b2055c35SXin Li }
159*b2055c35SXin Li
160*b2055c35SXin Li //------------------------------------------------------------------------------
161