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