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#ifndef WEBP_UTILS_RESCALER_UTILS_H_
15#define WEBP_UTILS_RESCALER_UTILS_H_
16
17#ifdef __cplusplus
18extern "C" {
19#endif
20
21#include "src/webp/types.h"
22
23#define WEBP_RESCALER_RFIX 32 // fixed-point precision for multiplies
24#define WEBP_RESCALER_ONE (1ull << WEBP_RESCALER_RFIX)
25#define WEBP_RESCALER_FRAC(x, y) \
26 ((uint32_t)(((uint64_t)(x) << WEBP_RESCALER_RFIX) / (y)))
27
28// Structure used for on-the-fly rescaling
29typedef uint32_t rescaler_t; // type for side-buffer
30typedef struct WebPRescaler WebPRescaler;
31struct WebPRescaler {
32 int x_expand; // true if we're expanding in the x direction
33 int y_expand; // true if we're expanding in the y direction
34 int num_channels; // bytes to jump between pixels
35 uint32_t fx_scale; // fixed-point scaling factors
36 uint32_t fy_scale; // ''
37 uint32_t fxy_scale; // ''
38 int y_accum; // vertical accumulator
39 int y_add, y_sub; // vertical increments
40 int x_add, x_sub; // horizontal increments
41 int src_width, src_height; // source dimensions
42 int dst_width, dst_height; // destination dimensions
43 int src_y, dst_y; // row counters for input and output
44 uint8_t* dst;
45 int dst_stride;
46 rescaler_t* irow, *frow; // work buffer
47};
48
49// Initialize a rescaler given scratch area 'work' and dimensions of src & dst.
50// Returns false in case of error.
51int WebPRescalerInit(WebPRescaler* const rescaler,
52 int src_width, int src_height,
53 uint8_t* const dst,
54 int dst_width, int dst_height, int dst_stride,
55 int num_channels,
56 rescaler_t* const work);
57
58// If either 'scaled_width' or 'scaled_height' (but not both) is 0 the value
59// will be calculated preserving the aspect ratio, otherwise the values are
60// left unmodified. Returns true on success, false if either value is 0 after
61// performing the scaling calculation.
62int WebPRescalerGetScaledDimensions(int src_width, int src_height,
63 int* const scaled_width,
64 int* const scaled_height);
65
66// Returns the number of input lines needed next to produce one output line,
67// considering that the maximum available input lines are 'max_num_lines'.
68int WebPRescaleNeededLines(const WebPRescaler* const rescaler,
69 int max_num_lines);
70
71// Import multiple rows over all channels, until at least one row is ready to
72// be exported. Returns the actual number of lines that were imported.
73int WebPRescalerImport(WebPRescaler* const rescaler, int num_rows,
74 const uint8_t* src, int src_stride);
75
76// Export as many rows as possible. Return the numbers of rows written.
77int WebPRescalerExport(WebPRescaler* const rescaler);
78
79// Return true if input is finished
80static WEBP_INLINE
81int WebPRescalerInputDone(const WebPRescaler* const rescaler) {
82 return (rescaler->src_y >= rescaler->src_height);
83}
84// Return true if output is finished
85static WEBP_INLINE
86int WebPRescalerOutputDone(const WebPRescaler* const rescaler) {
87 return (rescaler->dst_y >= rescaler->dst_height);
88}
89
90// Return true if there are pending output rows ready.
91static WEBP_INLINE
92int WebPRescalerHasPendingOutput(const WebPRescaler* const rescaler) {
93 return !WebPRescalerOutputDone(rescaler) && (rescaler->y_accum <= 0);
94}
95
96//------------------------------------------------------------------------------
97
98#ifdef __cplusplus
99} // extern "C"
100#endif
101
102#endif // WEBP_UTILS_RESCALER_UTILS_H_
103