rescaler_utils.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. #include <assert.h>
  14. #include <limits.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include "../dsp/dsp.h"
  18. #include "./rescaler_utils.h"
  19. #include "./utils.h"
  20. //------------------------------------------------------------------------------
  21. int WebPRescalerInit(WebPRescaler* const rescaler,
  22. 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. const uint64_t total_size = 2ull * dst_width * num_channels * sizeof(*work);
  29. if (!CheckSizeOverflow(total_size)) return 0;
  30. rescaler->x_expand = (src_width < dst_width);
  31. rescaler->y_expand = (src_height < dst_height);
  32. rescaler->src_width = src_width;
  33. rescaler->src_height = src_height;
  34. rescaler->dst_width = dst_width;
  35. rescaler->dst_height = dst_height;
  36. rescaler->src_y = 0;
  37. rescaler->dst_y = 0;
  38. rescaler->dst = dst;
  39. rescaler->dst_stride = dst_stride;
  40. rescaler->num_channels = num_channels;
  41. // for 'x_expand', we use bilinear interpolation
  42. rescaler->x_add = rescaler->x_expand ? (x_sub - 1) : x_add;
  43. rescaler->x_sub = rescaler->x_expand ? (x_add - 1) : x_sub;
  44. if (!rescaler->x_expand) { // fx_scale is not used otherwise
  45. rescaler->fx_scale = WEBP_RESCALER_FRAC(1, rescaler->x_sub);
  46. }
  47. // vertical scaling parameters
  48. rescaler->y_add = rescaler->y_expand ? y_add - 1 : y_add;
  49. rescaler->y_sub = rescaler->y_expand ? y_sub - 1 : y_sub;
  50. rescaler->y_accum = rescaler->y_expand ? rescaler->y_sub : rescaler->y_add;
  51. if (!rescaler->y_expand) {
  52. // This is WEBP_RESCALER_FRAC(dst_height, x_add * y_add) without the cast.
  53. // Its value is <= WEBP_RESCALER_ONE, because dst_height <= rescaler->y_add
  54. // and rescaler->x_add >= 1;
  55. const uint64_t num = (uint64_t)dst_height * WEBP_RESCALER_ONE;
  56. const uint64_t den = (uint64_t)rescaler->x_add * rescaler->y_add;
  57. const uint64_t ratio = num / den;
  58. if (ratio != (uint32_t)ratio) {
  59. // When ratio == WEBP_RESCALER_ONE, we can't represent the ratio with the
  60. // current fixed-point precision. This happens when src_height ==
  61. // rescaler->y_add (which == src_height), and rescaler->x_add == 1.
  62. // => We special-case fxy_scale = 0, in WebPRescalerExportRow().
  63. rescaler->fxy_scale = 0;
  64. } else {
  65. rescaler->fxy_scale = (uint32_t)ratio;
  66. }
  67. rescaler->fy_scale = WEBP_RESCALER_FRAC(1, rescaler->y_sub);
  68. } else {
  69. rescaler->fy_scale = WEBP_RESCALER_FRAC(1, rescaler->x_add);
  70. // rescaler->fxy_scale is unused here.
  71. }
  72. rescaler->irow = work;
  73. rescaler->frow = work + num_channels * dst_width;
  74. memset(work, 0, (size_t)total_size);
  75. WebPRescalerDspInit();
  76. return 1;
  77. }
  78. int WebPRescalerGetScaledDimensions(int src_width, int src_height,
  79. int* const scaled_width,
  80. int* const scaled_height) {
  81. assert(scaled_width != NULL);
  82. assert(scaled_height != NULL);
  83. {
  84. int width = *scaled_width;
  85. int height = *scaled_height;
  86. const int max_size = INT_MAX / 2;
  87. // if width is unspecified, scale original proportionally to height ratio.
  88. if (width == 0 && src_height > 0) {
  89. width =
  90. (int)(((uint64_t)src_width * height + src_height - 1) / src_height);
  91. }
  92. // if height is unspecified, scale original proportionally to width ratio.
  93. if (height == 0 && src_width > 0) {
  94. height =
  95. (int)(((uint64_t)src_height * width + src_width - 1) / src_width);
  96. }
  97. // Check if the overall dimensions still make sense.
  98. if (width <= 0 || height <= 0 || width > max_size || height > max_size) {
  99. return 0;
  100. }
  101. *scaled_width = width;
  102. *scaled_height = height;
  103. return 1;
  104. }
  105. }
  106. //------------------------------------------------------------------------------
  107. // all-in-one calls
  108. int WebPRescaleNeededLines(const WebPRescaler* const rescaler,
  109. int max_num_lines) {
  110. const int num_lines =
  111. (rescaler->y_accum + rescaler->y_sub - 1) / rescaler->y_sub;
  112. return (num_lines > max_num_lines) ? max_num_lines : num_lines;
  113. }
  114. int WebPRescalerImport(WebPRescaler* const rescaler, int num_lines,
  115. const uint8_t* src, int src_stride) {
  116. int total_imported = 0;
  117. while (total_imported < num_lines &&
  118. !WebPRescalerHasPendingOutput(rescaler)) {
  119. if (rescaler->y_expand) {
  120. rescaler_t* const tmp = rescaler->irow;
  121. rescaler->irow = rescaler->frow;
  122. rescaler->frow = tmp;
  123. }
  124. WebPRescalerImportRow(rescaler, src);
  125. if (!rescaler->y_expand) { // Accumulate the contribution of the new row.
  126. int x;
  127. for (x = 0; x < rescaler->num_channels * rescaler->dst_width; ++x) {
  128. rescaler->irow[x] += rescaler->frow[x];
  129. }
  130. }
  131. ++rescaler->src_y;
  132. src += src_stride;
  133. ++total_imported;
  134. rescaler->y_accum -= rescaler->y_sub;
  135. }
  136. return total_imported;
  137. }
  138. int WebPRescalerExport(WebPRescaler* const rescaler) {
  139. int total_exported = 0;
  140. while (WebPRescalerHasPendingOutput(rescaler)) {
  141. WebPRescalerExportRow(rescaler);
  142. ++total_exported;
  143. }
  144. return total_exported;
  145. }
  146. //------------------------------------------------------------------------------