/src/libwebp/src/utils/rescaler_utils.c
Line | Count | Source |
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 "src/utils/rescaler_utils.h" |
15 | | |
16 | | #include <assert.h> |
17 | | #include <limits.h> |
18 | | #include <stdlib.h> |
19 | | #include <string.h> |
20 | | |
21 | | #include "src/dsp/dsp.h" |
22 | | #include "src/utils/bounds_safety.h" |
23 | | #include "src/utils/utils.h" |
24 | | #include "src/webp/types.h" |
25 | | |
26 | | WEBP_ASSUME_UNSAFE_INDEXABLE_ABI |
27 | | |
28 | | //------------------------------------------------------------------------------ |
29 | | |
30 | | int WebPRescalerNeeds64Bit(int src_width, int src_height, int dst_width, |
31 | 0 | int dst_height) { |
32 | 0 | if (src_height >= dst_height && dst_height > 0) { |
33 | | // In horizontal downscaling (src_width >= dst_width), each input row |
34 | | // contributes at most 255 * src_width + dst_width / 2 to frow (due to |
35 | | // MULT_FIX rounding). In horizontal upscaling (src_width < dst_width), |
36 | | // bilinear expansion scales contributions by x_add = dst_width - 1, giving |
37 | | // at most 255 * (dst_width - 1) per row. |
38 | | // During vertical downscaling, EXPORT_ROW_SHRINK initializes irow with the |
39 | | // fractional carry-over 'frac' (< frow) from the previous export cycle, and |
40 | | // WebPRescalerImport then adds up to ceil(src_height / dst_height) full |
41 | | // frow rows to irow before the next export subtracts the new frac. |
42 | 0 | const uint64_t max_row_contrib = |
43 | 0 | (src_width >= dst_width) |
44 | 0 | ? (uint64_t)255 * src_width + (uint64_t)dst_width / 2 |
45 | 0 | : (uint64_t)255 * (dst_width - 1); |
46 | 0 | const uint64_t num_rows = |
47 | 0 | ((uint64_t)src_height + dst_height - 1) / dst_height + 1; |
48 | 0 | const uint64_t max_accum = max_row_contrib * num_rows; |
49 | 0 | return max_accum > UINT_MAX; |
50 | 0 | } |
51 | 0 | return 0; |
52 | 0 | } |
53 | | |
54 | | uint64_t WebPRescalerWorkSize(int src_width, int src_height, int dst_width, |
55 | 0 | int dst_height, int num_channels) { |
56 | 0 | const uint64_t num_elements = (uint64_t)dst_width * num_channels; |
57 | 0 | const size_t element_size = |
58 | 0 | (WebPRescalerNeeds64Bit(src_width, src_height, dst_width, dst_height) |
59 | 0 | ? sizeof(rescaler_accum_t) |
60 | 0 | : sizeof(rescaler_t)) + |
61 | 0 | sizeof(rescaler_t); |
62 | 0 | const uint64_t work_size = num_elements * element_size; |
63 | 0 | return (work_size + WEBP_ALIGN_CST) & ~(uint64_t)WEBP_ALIGN_CST; |
64 | 0 | } |
65 | | |
66 | | int WebPRescalerInit(WebPRescaler* const rescaler, int src_width, |
67 | | int src_height, uint8_t* const dst, int dst_width, |
68 | | int dst_height, int dst_stride, int num_channels, |
69 | 0 | rescaler_t* const work) { |
70 | 0 | const int x_add = src_width, x_sub = dst_width; |
71 | 0 | const int y_add = src_height, y_sub = dst_height; |
72 | 0 | const uint64_t total_size_bytes = WebPRescalerWorkSize( |
73 | 0 | src_width, src_height, dst_width, dst_height, num_channels); |
74 | 0 | if (!CheckSizeOverflow(total_size_bytes)) return 0; |
75 | | |
76 | 0 | { |
77 | 0 | rescaler_t* const WEBP_BIDI_INDEXABLE bounded_work = |
78 | 0 | WEBP_UNSAFE_FORGE_BIDI_INDEXABLE(rescaler_t*, work, |
79 | 0 | (size_t)total_size_bytes); |
80 | |
|
81 | 0 | rescaler->x_expand = (src_width < dst_width); |
82 | 0 | rescaler->y_expand = (src_height < dst_height); |
83 | 0 | rescaler->use_64bit = |
84 | 0 | WebPRescalerNeeds64Bit(src_width, src_height, dst_width, dst_height); |
85 | 0 | rescaler->src_width = src_width; |
86 | 0 | rescaler->src_height = src_height; |
87 | 0 | rescaler->src_y = 0; |
88 | 0 | rescaler->dst_y = 0; |
89 | 0 | rescaler->dst = dst; |
90 | 0 | rescaler->dst_stride = dst_stride; |
91 | 0 | rescaler->dst_height = dst_height; |
92 | 0 | if (rescaler->use_64bit) { |
93 | 0 | assert(WEBP_IS_ALIGNED(work, rescaler_accum_t)); |
94 | 0 | rescaler->dst_width = dst_width; |
95 | 0 | rescaler->num_channels = num_channels; |
96 | 0 | rescaler->irow = NULL; |
97 | 0 | rescaler->frow = WEBP_UNSAFE_FORGE_BIDI_INDEXABLE( |
98 | 0 | rescaler_t*, |
99 | 0 | (uint8_t*)work + |
100 | 0 | (size_t)num_channels * dst_width * sizeof(rescaler_accum_t), |
101 | 0 | (size_t)num_channels * dst_width * sizeof(rescaler_t)); |
102 | 0 | rescaler->irow64 = WEBP_UNSAFE_FORGE_BIDI_INDEXABLE( |
103 | 0 | rescaler_accum_t*, work, |
104 | 0 | (size_t)num_channels * dst_width * sizeof(rescaler_accum_t)); |
105 | 0 | } else { |
106 | 0 | assert(WEBP_IS_ALIGNED(work, rescaler_t)); |
107 | 0 | rescaler->dst_width = dst_width; |
108 | 0 | rescaler->num_channels = num_channels; |
109 | 0 | rescaler->irow = WEBP_UNSAFE_FORGE_BIDI_INDEXABLE( |
110 | 0 | rescaler_t*, work, |
111 | 0 | (size_t)num_channels * dst_width * sizeof(rescaler_t)); |
112 | 0 | rescaler->frow = WEBP_UNSAFE_FORGE_BIDI_INDEXABLE( |
113 | 0 | rescaler_t*, work + num_channels * dst_width, |
114 | 0 | (size_t)num_channels * dst_width * sizeof(rescaler_t)); |
115 | 0 | rescaler->irow64 = NULL; |
116 | 0 | } |
117 | 0 | memset(bounded_work, 0, (size_t)total_size_bytes); |
118 | 0 | } |
119 | | |
120 | | // for 'x_expand', we use bilinear interpolation |
121 | 0 | rescaler->x_add = rescaler->x_expand ? (x_sub - 1) : x_add; |
122 | 0 | rescaler->x_sub = rescaler->x_expand ? (x_add - 1) : x_sub; |
123 | 0 | if (!rescaler->x_expand) { // fx_scale is not used otherwise |
124 | 0 | rescaler->fx_scale = WEBP_RESCALER_FRAC(1, rescaler->x_sub); |
125 | 0 | } |
126 | | // vertical scaling parameters |
127 | 0 | rescaler->y_add = rescaler->y_expand ? y_add - 1 : y_add; |
128 | 0 | rescaler->y_sub = rescaler->y_expand ? y_sub - 1 : y_sub; |
129 | 0 | rescaler->y_accum = rescaler->y_expand ? rescaler->y_sub : rescaler->y_add; |
130 | 0 | if (!rescaler->y_expand) { |
131 | | // This is WEBP_RESCALER_FRAC(dst_height, x_add * y_add) without the cast. |
132 | | // Its value is <= WEBP_RESCALER_ONE, because dst_height <= rescaler->y_add |
133 | | // and rescaler->x_add >= 1; |
134 | 0 | const uint64_t num = (uint64_t)dst_height * WEBP_RESCALER_ONE; |
135 | 0 | const uint64_t den = (uint64_t)rescaler->x_add * rescaler->y_add; |
136 | 0 | const uint64_t ratio = num / den; |
137 | 0 | if (ratio != (uint32_t)ratio) { |
138 | | // When ratio == WEBP_RESCALER_ONE, we can't represent the ratio with the |
139 | | // current fixed-point precision. This happens when src_height == |
140 | | // rescaler->y_add (which == src_height), and rescaler->x_add == 1. |
141 | | // => We special-case fxy_scale = 0, in WebPRescalerExportRow(). |
142 | 0 | rescaler->fxy_scale = 0; |
143 | 0 | } else { |
144 | 0 | rescaler->fxy_scale = (uint32_t)ratio; |
145 | 0 | } |
146 | 0 | rescaler->fy_scale = WEBP_RESCALER_FRAC(1, rescaler->y_sub); |
147 | 0 | } else { |
148 | 0 | rescaler->fy_scale = WEBP_RESCALER_FRAC(1, rescaler->x_add); |
149 | | // rescaler->fxy_scale is unused here. |
150 | 0 | } |
151 | |
|
152 | 0 | WebPRescalerDspInit(); |
153 | 0 | return 1; |
154 | 0 | } |
155 | | |
156 | | int WebPRescalerGetScaledDimensions(int src_width, int src_height, |
157 | | int* const scaled_width, |
158 | 0 | int* const scaled_height) { |
159 | 0 | assert(scaled_width != NULL); |
160 | 0 | assert(scaled_height != NULL); |
161 | 0 | if (src_width < 0 || src_height < 0 || *scaled_width < 0 || |
162 | 0 | *scaled_height < 0) { |
163 | 0 | return 0; |
164 | 0 | } |
165 | 0 | { |
166 | 0 | int width = *scaled_width; |
167 | 0 | int height = *scaled_height; |
168 | 0 | const int max_size = INT_MAX / 2; |
169 | | |
170 | | // if width is unspecified, scale original proportionally to height ratio. |
171 | 0 | if (width == 0 && src_height > 0) { |
172 | 0 | width = |
173 | 0 | (int)(((uint64_t)src_width * height + src_height - 1) / src_height); |
174 | 0 | } |
175 | | // if height is unspecified, scale original proportionally to width ratio. |
176 | 0 | if (height == 0 && src_width > 0) { |
177 | 0 | height = |
178 | 0 | (int)(((uint64_t)src_height * width + src_width - 1) / src_width); |
179 | 0 | } |
180 | | // Check if the overall dimensions still make sense. |
181 | 0 | if (width <= 0 || height <= 0 || width > max_size || height > max_size) { |
182 | 0 | return 0; |
183 | 0 | } |
184 | | |
185 | 0 | *scaled_width = width; |
186 | 0 | *scaled_height = height; |
187 | 0 | return 1; |
188 | 0 | } |
189 | 0 | } |
190 | | |
191 | | //------------------------------------------------------------------------------ |
192 | | // all-in-one calls |
193 | | |
194 | | int WebPRescaleNeededLines(const WebPRescaler* const rescaler, |
195 | 0 | int max_num_lines) { |
196 | 0 | const int num_lines = |
197 | 0 | (rescaler->y_accum + rescaler->y_sub - 1) / rescaler->y_sub; |
198 | 0 | return (num_lines > max_num_lines) ? max_num_lines : num_lines; |
199 | 0 | } |
200 | | |
201 | | int WebPRescalerImport(WebPRescaler* const rescaler, int num_lines, |
202 | 0 | const uint8_t* src, int src_stride) { |
203 | 0 | int total_imported = 0; |
204 | 0 | while (total_imported < num_lines && |
205 | 0 | !WebPRescalerHasPendingOutput(rescaler)) { |
206 | 0 | if (rescaler->y_expand) { |
207 | 0 | rescaler_t* const tmp = rescaler->irow; |
208 | 0 | rescaler->irow = rescaler->frow; |
209 | 0 | rescaler->frow = WEBP_UNSAFE_FORGE_BIDI_INDEXABLE( |
210 | 0 | rescaler_t*, tmp, |
211 | 0 | (size_t)rescaler->num_channels * rescaler->dst_width * sizeof(*tmp)); |
212 | 0 | WEBP_SELF_ASSIGN(rescaler->dst_width); |
213 | 0 | WEBP_SELF_ASSIGN(rescaler->num_channels); |
214 | 0 | WEBP_SELF_ASSIGN(rescaler->irow64); |
215 | 0 | } |
216 | 0 | WebPRescalerImportRow(rescaler, src); |
217 | 0 | if (!rescaler->y_expand) { // Accumulate the contribution of the new row. |
218 | 0 | int x; |
219 | 0 | if (rescaler->use_64bit) { |
220 | 0 | for (x = 0; x < rescaler->num_channels * rescaler->dst_width; ++x) { |
221 | 0 | rescaler->irow64[x] += rescaler->frow[x]; |
222 | 0 | } |
223 | 0 | } else { |
224 | 0 | for (x = 0; x < rescaler->num_channels * rescaler->dst_width; ++x) { |
225 | 0 | rescaler->irow[x] += rescaler->frow[x]; |
226 | 0 | } |
227 | 0 | } |
228 | 0 | } |
229 | 0 | ++rescaler->src_y; |
230 | 0 | src += src_stride; |
231 | 0 | ++total_imported; |
232 | 0 | rescaler->y_accum -= rescaler->y_sub; |
233 | 0 | } |
234 | 0 | return total_imported; |
235 | 0 | } |
236 | | |
237 | 0 | int WebPRescalerExport(WebPRescaler* const rescaler) { |
238 | 0 | int total_exported = 0; |
239 | 0 | while (WebPRescalerHasPendingOutput(rescaler)) { |
240 | 0 | WebPRescalerExportRow(rescaler); |
241 | 0 | ++total_exported; |
242 | 0 | } |
243 | 0 | return total_exported; |
244 | 0 | } |
245 | | |
246 | | //------------------------------------------------------------------------------ |