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 (at) gmail.com) 13 14 #ifndef WEBP_UTILS_RESCALER_H_ 15 #define WEBP_UTILS_RESCALER_H_ 16 17 #ifdef __cplusplus 18 extern "C" { 19 #endif 20 21 #include "../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 29 typedef uint32_t rescaler_t; // type for side-buffer 30 typedef struct WebPRescaler WebPRescaler; 31 struct 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 void WebPRescalerInit(WebPRescaler* const rescaler, 51 int src_width, int src_height, 52 uint8_t* const dst, 53 int dst_width, int dst_height, int dst_stride, 54 int num_channels, 55 rescaler_t* const work); 56 57 // If either 'scaled_width' or 'scaled_height' (but not both) is 0 the value 58 // will be calculated preserving the aspect ratio, otherwise the values are 59 // left unmodified. Returns true on success, false if either value is 0 after 60 // performing the scaling calculation. 61 int WebPRescalerGetScaledDimensions(int src_width, int src_height, 62 int* const scaled_width, 63 int* const scaled_height); 64 65 // Returns the number of input lines needed next to produce one output line, 66 // considering that the maximum available input lines are 'max_num_lines'. 67 int WebPRescaleNeededLines(const WebPRescaler* const rescaler, 68 int max_num_lines); 69 70 // Import multiple rows over all channels, until at least one row is ready to 71 // be exported. Returns the actual number of lines that were imported. 72 int WebPRescalerImport(WebPRescaler* const rescaler, int num_rows, 73 const uint8_t* src, int src_stride); 74 75 // Export as many rows as possible. Return the numbers of rows written. 76 int WebPRescalerExport(WebPRescaler* const rescaler); 77 78 // Return true if input is finished 79 static WEBP_INLINE 80 int WebPRescalerInputDone(const WebPRescaler* const rescaler) { 81 return (rescaler->src_y >= rescaler->src_height); 82 } 83 // Return true if output is finished 84 static WEBP_INLINE 85 int WebPRescalerOutputDone(const WebPRescaler* const rescaler) { 86 return (rescaler->dst_y >= rescaler->dst_height); 87 } 88 89 // Return true if there are pending output rows ready. 90 static WEBP_INLINE 91 int WebPRescalerHasPendingOutput(const WebPRescaler* const rescaler) { 92 return !WebPRescalerOutputDone(rescaler) && (rescaler->y_accum <= 0); 93 } 94 95 //------------------------------------------------------------------------------ 96 97 #ifdef __cplusplus 98 } // extern "C" 99 #endif 100 101 #endif /* WEBP_UTILS_RESCALER_H_ */ 102