Added PixelBuffer for image loading and operations.
[platform/core/uifw/dali-adaptor.git] / third-party / image-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 #ifndef __RESAMPLER_H__
4 #define __RESAMPLER_H__
5
6 #define RESAMPLER_DEBUG_OPS 0
7 #define RESAMPLER_DEFAULT_FILTER "lanczos4"
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    struct Contrib
20    {
21       Resample_Real weight;
22       unsigned short pixel;
23    };
24
25    struct Contrib_List
26    {
27       unsigned short n;
28       Contrib* p;
29    };
30
31    enum Boundary_Op
32    {
33       BOUNDARY_WRAP = 0,
34       BOUNDARY_REFLECT = 1,
35       BOUNDARY_CLAMP = 2
36    };
37
38    enum Status
39    {
40       STATUS_OKAY = 0,
41       STATUS_OUT_OF_MEMORY = 1,
42       STATUS_BAD_FILTER_NAME = 2,
43       STATUS_SCAN_BUFFER_FULL = 3
44    };
45
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)
52    Resampler(
53       int src_x, int src_y,
54       int dst_x, int dst_y,
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);
64
65    ~Resampler();
66
67    // Reinits resampler so it can handle another frame.
68    void restart();
69
70    // false on out of memory.
71    bool put_line(const Sample* Psrc);
72
73    // NULL if no scanlines are currently available (give the resampler more scanlines!)
74    const Sample* get_line();
75
76    Status status() const { return m_status; }
77
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; }
82
83    // Filter accessors.
84    static int get_filter_num();
85    static char* get_filter_name(int filter_num);
86
87 private:
88    Resampler();
89    Resampler(const Resampler& o);
90    Resampler& operator= (const Resampler& o);
91
92 #ifdef RESAMPLER_DEBUG_OPS
93    int total_ops;
94 #endif
95
96    int m_intermediate_x;
97
98    int m_resample_src_x;
99    int m_resample_src_y;
100    int m_resample_dst_x;
101    int m_resample_dst_y;
102
103    Boundary_Op m_boundary_op;
104
105    Sample* m_Pdst_buf;
106    Sample* m_Ptmp_buf;
107
108    Contrib_List* m_Pclist_x;
109    Contrib_List* m_Pclist_y;
110
111    bool m_clist_x_forced;
112    bool m_clist_y_forced;
113
114    bool m_delay_x_resample;
115
116    int* m_Psrc_y_count;
117    unsigned char* m_Psrc_y_flag;
118
119    // The maximum number of scanlines that can be buffered at one time.
120    enum { MAX_SCAN_BUF_SIZE = RESAMPLER_MAX_DIMENSION };
121
122    struct Scan_Buf
123    {
124       int scan_buf_y[MAX_SCAN_BUF_SIZE];
125       Sample* scan_buf_l[MAX_SCAN_BUF_SIZE];
126    };
127
128    Scan_Buf* m_Pscan_buf;
129
130    int m_cur_src_y;
131    int m_cur_dst_y;
132
133    Status m_status;
134
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
141    int reflect(const int j, const int src_x, const Boundary_Op boundary_op);
142
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);
149
150    inline int count_ops(Contrib_List* Pclist, int k)
151    {
152       int i, t = 0;
153       for (i = 0; i < k; i++)
154          t += Pclist[i].n;
155       return (t);
156    }
157
158    Resample_Real m_lo;
159    Resample_Real m_hi;
160
161    inline Resample_Real clamp_sample(Resample_Real f) const
162    {
163       if (f < m_lo)
164          f = m_lo;
165       else if (f > m_hi)
166          f = m_hi;
167       return f;
168    }
169 };
170
171 #endif // __RESAMPLER_H__
172
173 // This is free and unencumbered software released into the public domain.
174 //
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
178 // means.
179 //
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.
187 //
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.
195 //
196 // For more information, please refer to <http://unlicense.org/>