1 // resampler.h, Separable filtering image rescaler v2.21, Rich Geldreich - richgel99@gmail.com
2 // See unlicense.org text at the bottom of this file.
3 // Modified to specify filters as enum rather than char *
7 #define RESAMPLER_DEBUG_OPS 0
9 #define RESAMPLER_MAX_DIMENSION 16384
12 typedef float Resample_Real;
17 typedef Resample_Real Sample;
20 * Supported filter types
37 QUADRATIC_INTERPOLATION,
38 QUADRATIC_APPROXIMATION,
64 STATUS_OUT_OF_MEMORY = 1,
65 STATUS_BAD_FILTER_TYPE = 2,
66 STATUS_SCAN_BUFFER_FULL = 3
69 // src_x/src_y - Input dimensions
70 // dst_x/dst_y - Output dimensions
71 // boundary_op - How to sample pixels near the image boundaries
72 // sample_low/sample_high - Clamp output samples to specified range, or disable clamping if sample_low >= sample_high
73 // Pclist_x/Pclist_y - Optional pointers to contributor lists from another instance of a Resampler
74 // src_x_ofs/src_y_ofs - Offset input image by specified amount (fractional values okay)
78 Boundary_Op boundary_op = BOUNDARY_CLAMP,
79 Resample_Real sample_low = 0.0f, Resample_Real sample_high = 0.0f,
80 Resampler::Filter = Resampler::LANCZOS3,
81 Contrib_List* Pclist_x = NULL,
82 Contrib_List* Pclist_y = NULL,
83 Resample_Real filter_x_scale = 1.0f,
84 Resample_Real filter_y_scale = 1.0f,
85 Resample_Real src_x_ofs = 0.0f,
86 Resample_Real src_y_ofs = 0.0f);
90 // Reinits resampler so it can handle another frame.
93 // false on out of memory.
94 bool put_line(const Sample* Psrc);
96 // NULL if no scanlines are currently available (give the resampler more scanlines!)
97 const Sample* get_line();
99 Status status() const { return m_status; }
101 // Returned contributor lists can be shared with another Resampler.
102 void get_clists(Contrib_List** ptr_clist_x, Contrib_List** ptr_clist_y);
103 Contrib_List* get_clist_x() const { return m_Pclist_x; }
104 Contrib_List* get_clist_y() const { return m_Pclist_y; }
108 Resampler(const Resampler& o);
109 Resampler& operator= (const Resampler& o);
111 #ifdef RESAMPLER_DEBUG_OPS
115 int m_intermediate_x;
117 int m_resample_src_x;
118 int m_resample_src_y;
119 int m_resample_dst_x;
120 int m_resample_dst_y;
122 Boundary_Op m_boundary_op;
127 Contrib_List* m_Pclist_x;
128 Contrib_List* m_Pclist_y;
130 bool m_clist_x_forced;
131 bool m_clist_y_forced;
133 bool m_delay_x_resample;
136 unsigned char* m_Psrc_y_flag;
138 // The maximum number of scanlines that can be buffered at one time.
139 enum { MAX_SCAN_BUF_SIZE = RESAMPLER_MAX_DIMENSION };
143 int scan_buf_y[MAX_SCAN_BUF_SIZE];
144 Sample* scan_buf_l[MAX_SCAN_BUF_SIZE];
147 Scan_Buf* m_Pscan_buf;
154 void resample_x(Sample* Pdst, const Sample* Psrc);
155 void scale_y_mov(Sample* Ptmp, const Sample* Psrc, Resample_Real weight, int dst_x);
156 void scale_y_add(Sample* Ptmp, const Sample* Psrc, Resample_Real weight, int dst_x);
157 void clamp(Sample* Pdst, int n);
158 void resample_y(Sample* Pdst);
160 int reflect(const int j, const int src_x, const Boundary_Op boundary_op);
162 Contrib_List* make_clist(
163 int src_x, int dst_x, Boundary_Op boundary_op,
164 Resample_Real (*Pfilter)(Resample_Real),
165 Resample_Real filter_support,
166 Resample_Real filter_scale,
167 Resample_Real src_ofs);
169 inline int count_ops(Contrib_List* Pclist, int k)
172 for (i = 0; i < k; i++)
180 inline Resample_Real clamp_sample(Resample_Real f) const
190 #endif // RESAMPLER_H
192 // This is free and unencumbered software released into the public domain.
194 // Anyone is free to copy, modify, publish, use, compile, sell, or
195 // distribute this software, either in source code form or as a compiled
196 // binary, for any purpose, commercial or non-commercial, and by any
199 // In jurisdictions that recognize copyright laws, the author or authors
200 // of this software dedicate any and all copyright interest in the
201 // software to the public domain. We make this dedication for the benefit
202 // of the public at large and to the detriment of our heirs and
203 // successors. We intend this dedication to be an overt act of
204 // relinquishment in perpetuity of all present and future rights to this
205 // software under copyright law.
207 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
208 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
209 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
210 // IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
211 // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
212 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
213 // OTHER DEALINGS IN THE SOFTWARE.
215 // For more information, please refer to <http://unlicense.org/>