[Tizen] Use OpenGL es 2.0 if eglCreateContext is fail, Prevent glFinish without makeC...
[platform/core/uifw/dali-adaptor.git] / third-party / resampler / resampler.h
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 *
4 #ifndef RESAMPLER_H
5 #define RESAMPLER_H
6
7 #define RESAMPLER_DEBUG_OPS 0
8
9 #define RESAMPLER_MAX_DIMENSION 16384
10
11 // float or double
12 typedef float Resample_Real;
13
14 class Resampler
15 {
16 public:
17    typedef Resample_Real Sample;
18
19    /**
20     * Supported filter types
21     */
22    enum Filter
23    {
24      BOX,
25      TENT,
26      BELL,
27      B_SPLINE,
28      MITCHELL,
29      LANCZOS3,
30      BLACKMAN,
31      LANCZOS4,
32      LANCZOS6,
33      LANCZOS12,
34      KAISER,
35      GAUSSIAN,
36      CATMULLROM,
37      QUADRATIC_INTERPOLATION,
38      QUADRATIC_APPROXIMATION,
39      QUADRATIC_MIX,
40    };
41
42    struct Contrib
43    {
44       Resample_Real weight;
45       unsigned short pixel;
46    };
47
48    struct Contrib_List
49    {
50       unsigned short n;
51       Contrib* p;
52    };
53
54    enum Boundary_Op
55    {
56       BOUNDARY_WRAP = 0,
57       BOUNDARY_REFLECT = 1,
58       BOUNDARY_CLAMP = 2
59    };
60
61    enum Status
62    {
63       STATUS_OKAY = 0,
64       STATUS_OUT_OF_MEMORY = 1,
65       STATUS_BAD_FILTER_TYPE = 2,
66       STATUS_SCAN_BUFFER_FULL = 3
67    };
68
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)
75    Resampler(
76       int src_x, int src_y,
77       int dst_x, int dst_y,
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);
87
88    ~Resampler();
89
90    // Reinits resampler so it can handle another frame.
91    void restart();
92
93    // false on out of memory.
94    bool put_line(const Sample* Psrc);
95
96    // NULL if no scanlines are currently available (give the resampler more scanlines!)
97    const Sample* get_line();
98
99    Status status() const { return m_status; }
100
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; }
105
106 private:
107    Resampler();
108    Resampler(const Resampler& o);
109    Resampler& operator= (const Resampler& o);
110
111 #ifdef RESAMPLER_DEBUG_OPS
112    int total_ops;
113 #endif
114
115    int m_intermediate_x;
116
117    int m_resample_src_x;
118    int m_resample_src_y;
119    int m_resample_dst_x;
120    int m_resample_dst_y;
121
122    Boundary_Op m_boundary_op;
123
124    Sample* m_Pdst_buf;
125    Sample* m_Ptmp_buf;
126
127    Contrib_List* m_Pclist_x;
128    Contrib_List* m_Pclist_y;
129
130    bool m_clist_x_forced;
131    bool m_clist_y_forced;
132
133    bool m_delay_x_resample;
134
135    int* m_Psrc_y_count;
136    unsigned char* m_Psrc_y_flag;
137
138    // The maximum number of scanlines that can be buffered at one time.
139    enum { MAX_SCAN_BUF_SIZE = RESAMPLER_MAX_DIMENSION };
140
141    struct Scan_Buf
142    {
143       int scan_buf_y[MAX_SCAN_BUF_SIZE];
144       Sample* scan_buf_l[MAX_SCAN_BUF_SIZE];
145    };
146
147    Scan_Buf* m_Pscan_buf;
148
149    int m_cur_src_y;
150    int m_cur_dst_y;
151
152    Status m_status;
153
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);
159
160    int reflect(const int j, const int src_x, const Boundary_Op boundary_op);
161
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);
168
169    inline int count_ops(Contrib_List* Pclist, int k)
170    {
171       int i, t = 0;
172       for (i = 0; i < k; i++)
173          t += Pclist[i].n;
174       return (t);
175    }
176
177    Resample_Real m_lo;
178    Resample_Real m_hi;
179
180    inline Resample_Real clamp_sample(Resample_Real f) const
181    {
182       if (f < m_lo)
183          f = m_lo;
184       else if (f > m_hi)
185          f = m_hi;
186       return f;
187    }
188 };
189
190 #endif // RESAMPLER_H
191
192 // This is free and unencumbered software released into the public domain.
193 //
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
197 // means.
198 //
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.
206 //
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.
214 //
215 // For more information, please refer to <http://unlicense.org/>