basisu_resampler.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // basisu_resampler.h
  2. // Copyright (C) 2019 Binomial LLC. All Rights Reserved.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #pragma once
  16. #include "../transcoder/basisu.h"
  17. #define BASISU_RESAMPLER_DEBUG_OPS (0)
  18. #define BASISU_RESAMPLER_DEFAULT_FILTER "lanczos4"
  19. #define BASISU_RESAMPLER_MAX_DIMENSION (16384)
  20. namespace basisu
  21. {
  22. // float or double
  23. typedef float Resample_Real;
  24. class Resampler
  25. {
  26. public:
  27. typedef Resample_Real Sample;
  28. struct Contrib
  29. {
  30. Resample_Real weight;
  31. uint16_t pixel;
  32. };
  33. struct Contrib_List
  34. {
  35. uint16_t n;
  36. Contrib *p;
  37. };
  38. enum Boundary_Op
  39. {
  40. BOUNDARY_WRAP = 0,
  41. BOUNDARY_REFLECT = 1,
  42. BOUNDARY_CLAMP = 2
  43. };
  44. enum Status
  45. {
  46. STATUS_OKAY = 0,
  47. STATUS_OUT_OF_MEMORY = 1,
  48. STATUS_BAD_FILTER_NAME = 2,
  49. STATUS_SCAN_BUFFER_FULL = 3
  50. };
  51. // src_x/src_y - Input dimensions
  52. // dst_x/dst_y - Output dimensions
  53. // boundary_op - How to sample pixels near the image boundaries
  54. // sample_low/sample_high - Clamp output samples to specified range, or disable clamping if sample_low >= sample_high
  55. // Pclist_x/Pclist_y - Optional pointers to contributor lists from another instance of a Resampler
  56. // src_x_ofs/src_y_ofs - Offset input image by specified amount (fractional values okay)
  57. Resampler(
  58. int src_x, int src_y,
  59. int dst_x, int dst_y,
  60. Boundary_Op boundary_op = BOUNDARY_CLAMP,
  61. Resample_Real sample_low = 0.0f, Resample_Real sample_high = 0.0f,
  62. const char *Pfilter_name = BASISU_RESAMPLER_DEFAULT_FILTER,
  63. Contrib_List *Pclist_x = NULL,
  64. Contrib_List *Pclist_y = NULL,
  65. Resample_Real filter_x_scale = 1.0f,
  66. Resample_Real filter_y_scale = 1.0f,
  67. Resample_Real src_x_ofs = 0.0f,
  68. Resample_Real src_y_ofs = 0.0f);
  69. ~Resampler();
  70. // Reinits resampler so it can handle another frame.
  71. void restart();
  72. // false on out of memory.
  73. bool put_line(const Sample *Psrc);
  74. // NULL if no scanlines are currently available (give the resampler more scanlines!)
  75. const Sample *get_line();
  76. Status status() const
  77. {
  78. return m_status;
  79. }
  80. // Returned contributor lists can be shared with another Resampler.
  81. void get_clists(Contrib_List **ptr_clist_x, Contrib_List **ptr_clist_y);
  82. Contrib_List *get_clist_x() const
  83. {
  84. return m_Pclist_x;
  85. }
  86. Contrib_List *get_clist_y() const
  87. {
  88. return m_Pclist_y;
  89. }
  90. // Filter accessors.
  91. static int get_filter_num();
  92. static const char *get_filter_name(int filter_num);
  93. static Contrib_List *make_clist(
  94. int src_x, int dst_x, Boundary_Op boundary_op,
  95. Resample_Real(*Pfilter)(Resample_Real),
  96. Resample_Real filter_support,
  97. Resample_Real filter_scale,
  98. Resample_Real src_ofs);
  99. private:
  100. Resampler();
  101. Resampler(const Resampler &o);
  102. Resampler &operator=(const Resampler &o);
  103. #ifdef BASISU_RESAMPLER_DEBUG_OPS
  104. int total_ops;
  105. #endif
  106. int m_intermediate_x;
  107. int m_resample_src_x;
  108. int m_resample_src_y;
  109. int m_resample_dst_x;
  110. int m_resample_dst_y;
  111. Boundary_Op m_boundary_op;
  112. Sample *m_Pdst_buf;
  113. Sample *m_Ptmp_buf;
  114. Contrib_List *m_Pclist_x;
  115. Contrib_List *m_Pclist_y;
  116. bool m_clist_x_forced;
  117. bool m_clist_y_forced;
  118. bool m_delay_x_resample;
  119. int *m_Psrc_y_count;
  120. uint8_t *m_Psrc_y_flag;
  121. // The maximum number of scanlines that can be buffered at one time.
  122. enum
  123. {
  124. MAX_SCAN_BUF_SIZE = BASISU_RESAMPLER_MAX_DIMENSION
  125. };
  126. struct Scan_Buf
  127. {
  128. int scan_buf_y[MAX_SCAN_BUF_SIZE];
  129. Sample *scan_buf_l[MAX_SCAN_BUF_SIZE];
  130. };
  131. Scan_Buf *m_Pscan_buf;
  132. int m_cur_src_y;
  133. int m_cur_dst_y;
  134. Status m_status;
  135. void resample_x(Sample *Pdst, const Sample *Psrc);
  136. void scale_y_mov(Sample *Ptmp, const Sample *Psrc, Resample_Real weight, int dst_x);
  137. void scale_y_add(Sample *Ptmp, const Sample *Psrc, Resample_Real weight, int dst_x);
  138. void clamp(Sample *Pdst, int n);
  139. void resample_y(Sample *Pdst);
  140. static int reflect(const int j, const int src_x, const Boundary_Op boundary_op);
  141. inline int count_ops(Contrib_List *Pclist, int k)
  142. {
  143. int i, t = 0;
  144. for (i = 0; i < k; i++)
  145. t += Pclist[i].n;
  146. return (t);
  147. }
  148. Resample_Real m_lo;
  149. Resample_Real m_hi;
  150. inline Resample_Real clamp_sample(Resample_Real f) const
  151. {
  152. if (f < m_lo)
  153. f = m_lo;
  154. else if (f > m_hi)
  155. f = m_hi;
  156. return f;
  157. }
  158. };
  159. } // namespace basisu