Beatmup
interpolation.h
Go to the documentation of this file.
1 /*
2  Beatmup image and signal processing library
3  Copyright (C) 2019, lnstadrum
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #pragma once
20 #include "bitmap_access.h"
21 
22 namespace Beatmup {
23 
24  /**
25  Nearest neighbour bitmap interpolation
26  */
27  template<class scanner, typename pixel> class NearestNeighborInterpolator : public scanner {
28  public:
29  NearestNeighborInterpolator(const AbstractBitmap& bitmap, int x = 0, int y = 0) : scanner(bitmap, x, y) {}
30  inline pixel operator()() const {
31  return scanner::operator()();
32  }
33 
34  inline pixel operator()(float x, float y) const {
35  return scanner::operator() (roundf_fast(x), roundf_fast(y));
36  }
37  };
38 
45 
46  /**
47  Single byte bitmap bilinear interpolation
48  */
50  public:
52 
53  inline pixint1 operator()() const {
55  }
56 
57  inline pixint1 operator()(float x, float y) const {
58  int ix = (int)x, iy = (int)y;
59  pixbyte* p = jump(ix, iy);
60  if (ix < width-1) {
61  float fx = x - ix;
62  if (iy < height-1) {
63  float fy = y - iy;
64  return pixint1{ roundf_fast((p[0] * (1 - fx) + p[1] * fx) * (1 - fy) + (p[width] * (1 - fx) + p[width + 1] * fx) * fy) };
65  } else
66  return pixint1{ roundf_fast(p[0] * (1 - fx) + p[1] * fx) };
67  }
68  else
69  if (iy < height-1) {
70  float fy = y - iy;
71  return pixint1{ roundf_fast(p[0] * (1 - fy) + p[width] * fy) };
72  }
73  else
74  return pixint1{ p[0] };
75  }
76  };
77 
78  /**
79  Triple byte bitmap bilinear interpolation
80  */
82  public:
84 
85  inline pixint3 operator()() const {
87  }
88 
89  inline pixint3 operator()(float x, float y) const {
90  int
91  ix = (int)x, iy = (int)y,
92  i1 = 3 * width;
93  pixbyte* p = jump(ix, iy);
94  if (ix < width-1) {
95  float fx = x - ix;
96  if (iy < height-1) {
97  float
98  fy = y - iy,
99  w0 = (1 - fx) * (1 - fy),
100  w1 = fx * (1 - fy),
101  w2 = (1 - fx) * fy,
102  w3 = fx*fy;
103  return pixint3 {
104  roundf_fast(p[0] * w0 + p[3] * w1 + p[i1 ] * w2 + p[i1+3] * w3),
105  roundf_fast(p[1] * w0 + p[4] * w1 + p[i1+1] * w2 + p[i1+4] * w3),
106  roundf_fast(p[2] * w0 + p[5] * w1 + p[i1+2] * w2 + p[i1+5] * w3)
107  };
108  }
109  else
110  return pixint3{
111  roundf_fast(p[0] * (1 - fx) + p[3] * fx),
112  roundf_fast(p[1] * (1 - fx) + p[4] * fx),
113  roundf_fast(p[2] * (1 - fx) + p[5] * fx)
114  };
115  }
116  else
117  if (iy < height-1) {
118  float fy = y - iy;
119  return pixint3{
120  roundf_fast(p[0] * (1 - fy) + p[i1 ] * fy),
121  roundf_fast(p[1] * (1 - fy) + p[i1+1] * fy),
122  roundf_fast(p[2] * (1 - fy) + p[i1+2] * fy)
123  };
124  }
125  else
126  return pixint3{ p[0], p[1], p[2] };
127  }
128  };
129 
130  /**
131  Quad byte bitmap bilinear interpolation
132  */
134  public:
136 
137  inline pixint4 operator()() const {
139  }
140 
141  inline pixint4 operator()(float x, float y) const {
142  int
143  ix = (int)x, iy = (int)y,
144  i1 = 4 * width;
145  pixbyte* p = jump(ix, iy);
146  if (ix < width-1) {
147  float fx = x - ix;
148  if (iy < height-1) {
149  float
150  fy = y - iy,
151  w0 = (1 - fx) * (1 - fy),
152  w1 = fx * (1 - fy),
153  w2 = (1 - fx) * fy,
154  w3 = fx*fy;
155  return pixint4 {
156  roundf_fast(p[0] * w0 + p[4] * w1 + p[i1 ] * w2 + p[i1 + 4] * w3),
157  roundf_fast(p[1] * w0 + p[5] * w1 + p[i1 + 1] * w2 + p[i1 + 5] * w3),
158  roundf_fast(p[2] * w0 + p[6] * w1 + p[i1 + 2] * w2 + p[i1 + 6] * w3),
159  roundf_fast(p[3] * w0 + p[7] * w1 + p[i1 + 3] * w2 + p[i1 + 7] * w3)
160  };
161  }
162  else {
163  float w = 1 - fx;
164  return pixint4 {
165  roundf_fast(p[0] * w + p[4] * fx),
166  roundf_fast(p[1] * w + p[5] * fx),
167  roundf_fast(p[2] * w + p[6] * fx),
168  roundf_fast(p[3] * w + p[7] * fx)
169  };
170  }
171  }
172  else
173  if (iy < height-1) {
174  float fy = y - iy, w = 1 - fy;
175  return pixint4 {
176  roundf_fast(p[0] * w + p[i1 ] * fy),
177  roundf_fast(p[1] * w + p[i1 + 1] * fy),
178  roundf_fast(p[2] * w + p[i1 + 2] * fy),
179  roundf_fast(p[3] * w + p[i1 + 3] * fy)
180  };
181  }
182  else
183  return pixint4{ p[0], p[1], p[2], p[3] };
184  }
185  };
186 
187  /**
188  Floating point bitmap bilinear interpolation, implemented using pixel arithmetics
189  */
190  template<class scanner, typename pixel> class FloatBilinearInterpolator : public scanner {
191  public:
192  FloatBilinearInterpolator(const AbstractBitmap& bitmap, int x = 0, int y = 0) : scanner(bitmap, x, y) {}
193 
194  inline pixel operator()() const {
195  return scanner::operator()();
196  }
197 
198  inline pixel operator()(float x, float y) const {
199  int ix = (int)x, iy = (int)y;
200  pixel* p = (pixel*)scanner::jump(ix, iy);
201  if (ix < scanner::width-1) {
202  float fx = x - ix;
203  if (iy < scanner::height-1) {
204  float fy = y - iy;
205  return (p[0] * (1 - fx) + p[1] * fx) * (1 - fy) + (p[scanner::width] * (1 - fx) + p[scanner::width + 1] * fx) * fy;
206  }
207  else
208  return p[0] * (1 - fx) + p[1] * fx;
209  }
210  else
211  if (iy < scanner::height-1) {
212  float fy = y - iy;
213  return p[0] * (1 - fy) + p[scanner::width] * fy;
214  }
215  else
216  return p[0];
217  }
218  };
219 
223 }
A very basic class for any image.
pixbyte * jump(int x, int y) const
Retrieves pixel address at a given position;.
Definition: bitmap_access.h:75
Floating point bitmap bilinear interpolation, implemented using pixel arithmetics.
FloatBilinearInterpolator(const AbstractBitmap &bitmap, int x=0, int y=0)
pixel operator()(float x, float y) const
Nearest neighbour bitmap interpolation.
Definition: interpolation.h:27
NearestNeighborInterpolator(const AbstractBitmap &bitmap, int x=0, int y=0)
Definition: interpolation.h:29
pixel operator()(float x, float y) const
Definition: interpolation.h:34
Quad byte bitmap bilinear interpolation.
QuadByteBilinearInterpolator(const AbstractBitmap &bitmap, int x=0, int y=0)
pixint4 operator()(float x, float y) const
Quad byte bitmap reader.
Single byte bitmap bilinear interpolation.
Definition: interpolation.h:49
pixint1 operator()(float x, float y) const
Definition: interpolation.h:57
SingleByteBilinearInterpolator(const AbstractBitmap &bitmap, int x=0, int y=0)
Definition: interpolation.h:51
Single byte bitmap reader.
pixint1 operator()() const
Returns value at current position.
Triple byte bitmap bilinear interpolation.
Definition: interpolation.h:81
TripleByteBilinearInterpolator(const AbstractBitmap &bitmap, int x=0, int y=0)
Definition: interpolation.h:83
pixint3 operator()(float x, float y) const
Definition: interpolation.h:89
Triple byte bitmap reader.
NearestNeighborInterpolator< QuadByteBitmapReader, pixint4 > QuadByteNearestNeighborInterpolator
Definition: interpolation.h:41
uint8_t pixbyte
Definition: basic_types.h:34
FloatBilinearInterpolator< SingleFloatBitmapReader, pixfloat1 > SingleFloatBilinearInterpolator
NearestNeighborInterpolator< SingleByteBitmapReader, pixint1 > SingleByteNearestNeighborInterpolator
Definition: interpolation.h:39
FloatBilinearInterpolator< QuadFloatBitmapReader, pixfloat4 > QuadFloatBilinearInterpolator
NearestNeighborInterpolator< QuadFloatBitmapReader, pixfloat4 > QuadFloatNearestNeighborInterpolator
Definition: interpolation.h:44
FloatBilinearInterpolator< TripleFloatBitmapReader, pixfloat3 > TripleFloatBilinearInterpolator
NearestNeighborInterpolator< TripleFloatBitmapReader, pixfloat3 > TripleFloatNearestNeighborInterpolator
Definition: interpolation.h:43
NearestNeighborInterpolator< SingleFloatBitmapReader, pixfloat1 > SingleFloatNearestNeighborInterpolator
Definition: interpolation.h:42
NearestNeighborInterpolator< TripleByteBitmapReader, pixint3 > TripleByteNearestNeighborInterpolator
Definition: interpolation.h:40
Monochromatic integer arithmetic.
Trichromatic integer arithmetic.
4-channel integer arithmetic
#define roundf_fast(X)
rounding (nearest integer)
Definition: utils.hpp:22
jobject jlong jint jint y
jlong jstring jint jint jint jint w
jlong jint width
jlong jint jint height
jobject jlong jint x
Beatmup::InternalBitmap * bitmap
Beatmup::IntPoint p((int) x,(int) y)