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 #ifndef __RESAMPLER_H__
4 #define __RESAMPLER_H__
6 #define RESAMPLER_DEBUG_OPS 0
7 #define RESAMPLER_DEFAULT_FILTER "lanczos4"
9 #define RESAMPLER_MAX_DIMENSION 16384
12 typedef float Resample_Real;
17 typedef Resample_Real Sample;
41 STATUS_OUT_OF_MEMORY = 1,
42 STATUS_BAD_FILTER_NAME = 2,
43 STATUS_SCAN_BUFFER_FULL = 3
46 // src_x/src_y - Input dimensions
47 // dst_x/dst_y - Output dimensions
48 // boundary_op - How to sample pixels near the image boundaries
49 // sample_low/sample_high - Clamp output samples to specified range, or disable clamping if sample_low >= sample_high
50 // Pclist_x/Pclist_y - Optional pointers to contributor lists from another instance of a Resampler
51 // src_x_ofs/src_y_ofs - Offset input image by specified amount (fractional values okay)
55 Boundary_Op boundary_op = BOUNDARY_CLAMP,
56 Resample_Real sample_low = 0.0f, Resample_Real sample_high = 0.0f,
57 const char* Pfilter_name = RESAMPLER_DEFAULT_FILTER,
58 Contrib_List* Pclist_x = NULL,
59 Contrib_List* Pclist_y = NULL,
60 Resample_Real filter_x_scale = 1.0f,
61 Resample_Real filter_y_scale = 1.0f,
62 Resample_Real src_x_ofs = 0.0f,
63 Resample_Real src_y_ofs = 0.0f);
67 // Reinits resampler so it can handle another frame.
70 // false on out of memory.
71 bool put_line(const Sample* Psrc);
73 // NULL if no scanlines are currently available (give the resampler more scanlines!)
74 const Sample* get_line();
76 Status status() const { return m_status; }
78 // Returned contributor lists can be shared with another Resampler.
79 void get_clists(Contrib_List** ptr_clist_x, Contrib_List** ptr_clist_y);
80 Contrib_List* get_clist_x() const { return m_Pclist_x; }
81 Contrib_List* get_clist_y() const { return m_Pclist_y; }
84 static int get_filter_num();
85 static char* get_filter_name(int filter_num);
89 Resampler(const Resampler& o);
90 Resampler& operator= (const Resampler& o);
92 #ifdef RESAMPLER_DEBUG_OPS
100 int m_resample_dst_x;
101 int m_resample_dst_y;
103 Boundary_Op m_boundary_op;
108 Contrib_List* m_Pclist_x;
109 Contrib_List* m_Pclist_y;
111 bool m_clist_x_forced;
112 bool m_clist_y_forced;
114 bool m_delay_x_resample;
117 unsigned char* m_Psrc_y_flag;
119 // The maximum number of scanlines that can be buffered at one time.
120 enum { MAX_SCAN_BUF_SIZE = RESAMPLER_MAX_DIMENSION };
124 int scan_buf_y[MAX_SCAN_BUF_SIZE];
125 Sample* scan_buf_l[MAX_SCAN_BUF_SIZE];
128 Scan_Buf* m_Pscan_buf;
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);
141 int reflect(const int j, const int src_x, const Boundary_Op boundary_op);
143 Contrib_List* make_clist(
144 int src_x, int dst_x, Boundary_Op boundary_op,
145 Resample_Real (*Pfilter)(Resample_Real),
146 Resample_Real filter_support,
147 Resample_Real filter_scale,
148 Resample_Real src_ofs);
150 inline int count_ops(Contrib_List* Pclist, int k)
153 for (i = 0; i < k; i++)
161 inline Resample_Real clamp_sample(Resample_Real f) const
171 #endif // __RESAMPLER_H__
173 // This is free and unencumbered software released into the public domain.
175 // Anyone is free to copy, modify, publish, use, compile, sell, or
176 // distribute this software, either in source code form or as a compiled
177 // binary, for any purpose, commercial or non-commercial, and by any
180 // In jurisdictions that recognize copyright laws, the author or authors
181 // of this software dedicate any and all copyright interest in the
182 // software to the public domain. We make this dedication for the benefit
183 // of the public at large and to the detriment of our heirs and
184 // successors. We intend this dedication to be an overt act of
185 // relinquishment in perpetuity of all present and future rights to this
186 // software under copyright law.
188 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
189 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
190 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
191 // IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
192 // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
193 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
194 // OTHER DEALINGS IN THE SOFTWARE.
196 // For more information, please refer to <http://unlicense.org/>