rescaler_utils.h 4.0 KB

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