13 #ifndef IMAGE_RESAMPLE_HPP
14 #define IMAGE_RESAMPLE_HPP
20 static inline double lerp(
double a,
double b,
double t)
22 return b * t + a * (1 - t);
27 static inline double cerp(
double a,
double b,
double c,
double d,
double t)
29 return b + (-a / 2 + c / 2 ) * t
30 + ( a - 5 * b / 2 + 2 * c - d / 2) * t * t
31 + (-a / 2 + 3 * b / 2 - 3 * c / 2 + d / 2) * t * t * t;
42 :
image(L), height(height), width(width), mode(mode) { }
47 virtual void tweak(
int a,
int v)
49 if (a == 0) width -= v;
50 if (a == 1) height -= v;
71 virtual double get(
int i,
int j,
int k)
const
74 const long long ww = (
long long)
L->
get_width();
76 return L->
get(
int((
long long) i * hh / (
long long) height),
77 int((
long long) j * ww / (
long long) width), k);
80 virtual void doc(std::ostream& out)
const
82 out <<
"nearest " << height <<
" " << width;
97 :
resample(height, width, mode, L) { }
99 virtual double get(
int i,
int j,
int k)
const
104 double ii = double(i) * double(hh) / double(height);
105 double jj = double(j) * double(ww) / double(width);
107 double s = ii - floor(ii);
108 double t = jj - floor(jj);
110 int ia = wrap(
int(floor(ii)), hh, mode & 1);
111 int ib = wrap(
int( ceil(ii)), hh, mode & 1);
112 int ja = wrap(
int(floor(jj)), ww, mode & 2);
113 int jb = wrap(
int( ceil(jj)), ww, mode & 2);
115 double aa =
L->
get(ia, ja, k);
116 double ab =
L->
get(ia, jb, k);
117 double ba =
L->
get(ib, ja, k);
118 double bb =
L->
get(ib, jb, k);
120 return lerp(lerp(aa, ab, t),
124 virtual void doc(std::ostream& out)
const
126 out <<
"linear " << height <<
" " << width <<
" " << mode;
141 :
resample(height, width, mode, L) { }
143 virtual double get(
int i,
int j,
int k)
const
148 double ii = double(i) * double(hh) / double(height);
149 double jj = double(j) * double(ww) / double(width);
151 double s = ii - floor(ii);
152 double t = jj - floor(jj);
154 int ib = wrap(
int(floor(ii)), hh, mode & 1);
155 int ic = wrap(
int( ceil(ii)), hh, mode & 1);
156 int jb = wrap(
int(floor(jj)), ww, mode & 2);
157 int jc = wrap(
int( ceil(jj)), ww, mode & 2);
159 int ia = wrap(ib - 1, hh, mode & 1);
160 int id = wrap(ic + 1, hh, mode & 1);
161 int ja = wrap(jb - 1, ww, mode & 2);
162 int jd = wrap(jc + 1, ww, mode & 2);
164 double aa =
L->
get(ia, ja, k);
165 double ab =
L->
get(ia, jb, k);
166 double ac =
L->
get(ia, jc, k);
167 double ad =
L->
get(ia, jd, k);
168 double ba =
L->
get(ib, ja, k);
169 double bb =
L->
get(ib, jb, k);
170 double bc =
L->
get(ib, jc, k);
171 double bd =
L->
get(ib, jd, k);
172 double ca =
L->
get(ic, ja, k);
173 double cb =
L->
get(ic, jb, k);
174 double cc =
L->
get(ic, jc, k);
175 double cd =
L->
get(ic, jd, k);
176 double da =
L->
get(
id, ja, k);
177 double db =
L->
get(
id, jb, k);
178 double dc =
L->
get(
id, jc, k);
179 double dd =
L->
get(
id, jd, k);
181 return cerp(cerp(aa, ab, ac, ad, t),
182 cerp(ba, bb, bc, bd, t),
183 cerp(ca, cb, cc, cd, t),
184 cerp(da, db, dc, dd, t), s);
187 virtual void doc(std::ostream& out)
const
189 out <<
"cubic " << height <<
" " << width <<
" " << mode;
Resampling filter base class.
Definition: image_resample.hpp:38
virtual double get(int i, int j, int k) const =0
Return the value of the sample at row i, column j, channel k.
image * L
Left child.
Definition: image.hpp:117
image(image *L=0, image *R=0)
Create a new image object with left child L and right child R. The parents of L and R are set to this...
Definition: image.hpp:26
cubic(int height, int width, int mode, image *L)
Resample L to the given height and width using cubic-interpolated sampling. Mode gives the wrap mode...
Definition: image_resample.hpp:140
Base class for all image sources, filters, and operators.
Definition: image.hpp:20
Cubic-interpolated resampling filter.
Definition: image_resample.hpp:134
virtual int get_height() const
Return the height of this image.
Definition: image.hpp:46
virtual int get_width() const
Return the height of this image.
Definition: image_resample.hpp:45
nearest(int height, int width, image *L)
Resample L to the given height and width using nearest-neighbor sampling. The wrap mode for neast sam...
Definition: image_resample.hpp:69
virtual int get_height() const
Return the height of this image.
Definition: image_resample.hpp:44
virtual int get_width() const
Return the height of this image.
Definition: image.hpp:56
Linearly-interpolated resampling filter.
Definition: image_resample.hpp:90
virtual void doc(std::ostream &out) const
Produce a string documenting the function of this object.
Definition: image_resample.hpp:187
virtual void doc(std::ostream &out) const
Produce a string documenting the function of this object.
Definition: image_resample.hpp:80
virtual void doc(std::ostream &out) const
Produce a string documenting the function of this object.
Definition: image_resample.hpp:124
linear(int height, int width, int mode, image *L)
Resample L to the given height and width using linearly- interpolated sampling. Mode gives the wrap m...
Definition: image_resample.hpp:96
virtual void tweak(int a, int v)
Tweak image parameter a, changing the value by a factor of v.
Definition: image_resample.hpp:47
Nearest value resampling filter.
Definition: image_resample.hpp:63