| 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 | |
| 18 | #define BASISU_RESAMPLER_DEBUG_OPS (0) |
| 19 | #define BASISU_RESAMPLER_DEFAULT_FILTER "lanczos4" |
| 20 | #define BASISU_RESAMPLER_MAX_DIMENSION (16384) |
| 21 | |
| 22 | namespace basisu |
| 23 | { |
| 24 | // float or double |
| 25 | typedef float Resample_Real; |
| 26 | |
| 27 | class Resampler |
| 28 | { |
| 29 | public: |
| 30 | typedef Resample_Real Sample; |
| 31 | |
| 32 | struct Contrib |
| 33 | { |
| 34 | Resample_Real weight; |
| 35 | uint16_t pixel; |
| 36 | }; |
| 37 | |
| 38 | struct Contrib_List |
| 39 | { |
| 40 | uint16_t n; |
| 41 | Contrib *p; |
| 42 | }; |
| 43 | |
| 44 | enum Boundary_Op |
| 45 | { |
| 46 | BOUNDARY_WRAP = 0, |
| 47 | BOUNDARY_REFLECT = 1, |
| 48 | BOUNDARY_CLAMP = 2 |
| 49 | }; |
| 50 | |
| 51 | enum Status |
| 52 | { |
| 53 | STATUS_OKAY = 0, |
| 54 | STATUS_OUT_OF_MEMORY = 1, |
| 55 | STATUS_BAD_FILTER_NAME = 2, |
| 56 | STATUS_SCAN_BUFFER_FULL = 3 |
| 57 | }; |
| 58 | |
| 59 | // src_x/src_y - Input dimensions |
| 60 | // dst_x/dst_y - Output dimensions |
| 61 | // boundary_op - How to sample pixels near the image boundaries |
| 62 | // sample_low/sample_high - Clamp output samples to specified range, or disable clamping if sample_low >= sample_high |
| 63 | // Pclist_x/Pclist_y - Optional pointers to contributor lists from another instance of a Resampler |
| 64 | // src_x_ofs/src_y_ofs - Offset input image by specified amount (fractional values okay) |
| 65 | Resampler( |
| 66 | int src_x, int src_y, |
| 67 | int dst_x, int dst_y, |
| 68 | Boundary_Op boundary_op = BOUNDARY_CLAMP, |
| 69 | Resample_Real sample_low = 0.0f, Resample_Real sample_high = 0.0f, |
| 70 | const char *Pfilter_name = BASISU_RESAMPLER_DEFAULT_FILTER, |
| 71 | Contrib_List *Pclist_x = NULL, |
| 72 | Contrib_List *Pclist_y = NULL, |
| 73 | Resample_Real filter_x_scale = 1.0f, |
| 74 | Resample_Real filter_y_scale = 1.0f, |
| 75 | Resample_Real src_x_ofs = 0.0f, |
| 76 | Resample_Real src_y_ofs = 0.0f); |
| 77 | |
| 78 | ~Resampler(); |
| 79 | |
| 80 | // Reinits resampler so it can handle another frame. |
| 81 | void restart(); |
| 82 | |
| 83 | // false on out of memory. |
| 84 | bool put_line(const Sample *Psrc); |
| 85 | |
| 86 | // NULL if no scanlines are currently available (give the resampler more scanlines!) |
| 87 | const Sample *get_line(); |
| 88 | |
| 89 | Status status() const |
| 90 | { |
| 91 | return m_status; |
| 92 | } |
| 93 | |
| 94 | // Returned contributor lists can be shared with another Resampler. |
| 95 | void get_clists(Contrib_List **ptr_clist_x, Contrib_List **ptr_clist_y); |
| 96 | Contrib_List *get_clist_x() const |
| 97 | { |
| 98 | return m_Pclist_x; |
| 99 | } |
| 100 | Contrib_List *get_clist_y() const |
| 101 | { |
| 102 | return m_Pclist_y; |
| 103 | } |
| 104 | |
| 105 | // Filter accessors. |
| 106 | static int get_filter_num(); |
| 107 | static const char *get_filter_name(int filter_num); |
| 108 | |
| 109 | static Contrib_List *make_clist( |
| 110 | int src_x, int dst_x, Boundary_Op boundary_op, |
| 111 | Resample_Real(*Pfilter)(Resample_Real), |
| 112 | Resample_Real filter_support, |
| 113 | Resample_Real filter_scale, |
| 114 | Resample_Real src_ofs); |
| 115 | |
| 116 | private: |
| 117 | Resampler(); |
| 118 | Resampler(const Resampler &o); |
| 119 | Resampler &operator=(const Resampler &o); |
| 120 | |
| 121 | #ifdef BASISU_RESAMPLER_DEBUG_OPS |
| 122 | int total_ops; |
| 123 | #endif |
| 124 | |
| 125 | int m_intermediate_x; |
| 126 | |
| 127 | int m_resample_src_x; |
| 128 | int m_resample_src_y; |
| 129 | int m_resample_dst_x; |
| 130 | int m_resample_dst_y; |
| 131 | |
| 132 | Boundary_Op m_boundary_op; |
| 133 | |
| 134 | Sample *m_Pdst_buf; |
| 135 | Sample *m_Ptmp_buf; |
| 136 | |
| 137 | Contrib_List *m_Pclist_x; |
| 138 | Contrib_List *m_Pclist_y; |
| 139 | |
| 140 | bool m_clist_x_forced; |
| 141 | bool m_clist_y_forced; |
| 142 | |
| 143 | bool m_delay_x_resample; |
| 144 | |
| 145 | int *m_Psrc_y_count; |
| 146 | uint8_t *m_Psrc_y_flag; |
| 147 | |
| 148 | // The maximum number of scanlines that can be buffered at one time. |
| 149 | enum |
| 150 | { |
| 151 | MAX_SCAN_BUF_SIZE = BASISU_RESAMPLER_MAX_DIMENSION |
| 152 | }; |
| 153 | |
| 154 | struct Scan_Buf |
| 155 | { |
| 156 | int scan_buf_y[MAX_SCAN_BUF_SIZE]; |
| 157 | Sample *scan_buf_l[MAX_SCAN_BUF_SIZE]; |
| 158 | }; |
| 159 | |
| 160 | Scan_Buf *m_Pscan_buf; |
| 161 | |
| 162 | int m_cur_src_y; |
| 163 | int m_cur_dst_y; |
| 164 | |
| 165 | Status m_status; |
| 166 | |
| 167 | void resample_x(Sample *Pdst, const Sample *Psrc); |
| 168 | void scale_y_mov(Sample *Ptmp, const Sample *Psrc, Resample_Real weight, int dst_x); |
| 169 | void scale_y_add(Sample *Ptmp, const Sample *Psrc, Resample_Real weight, int dst_x); |
| 170 | void clamp(Sample *Pdst, int n); |
| 171 | void resample_y(Sample *Pdst); |
| 172 | |
| 173 | static int reflect(const int j, const int src_x, const Boundary_Op boundary_op); |
| 174 | |
| 175 | inline int count_ops(Contrib_List *Pclist, int k) |
| 176 | { |
| 177 | int i, t = 0; |
| 178 | for (i = 0; i < k; i++) |
| 179 | t += Pclist[i].n; |
| 180 | return (t); |
| 181 | } |
| 182 | |
| 183 | Resample_Real m_lo; |
| 184 | Resample_Real m_hi; |
| 185 | |
| 186 | inline Resample_Real clamp_sample(Resample_Real f) const |
| 187 | { |
| 188 | if (f < m_lo) |
| 189 | f = m_lo; |
| 190 | else if (f > m_hi) |
| 191 | f = m_hi; |
| 192 | return f; |
| 193 | } |
| 194 | }; |
| 195 | |
| 196 | } // namespace basisu |
| 197 | |