PICCANTE  0.4
The hottest HDR imaging library!
rasterizer.hpp
Go to the documentation of this file.
1 /*
2 
3 PICCANTE
4 The hottest HDR imaging library!
5 http://vcg.isti.cnr.it/piccante
6 
7 Copyright (C) 2014
8 Visual Computing Laboratory - ISTI CNR
9 http://vcg.isti.cnr.it
10 First author: Francesco Banterle
11 
12 This Source Code Form is subject to the terms of the Mozilla Public
13 License, v. 2.0. If a copy of the MPL was not distributed with this
14 file, You can obtain one at http://mozilla.org/MPL/2.0/.
15 
16 */
17 
18 #ifndef PIC_UTIL_RASTERIZER_HPP
19 #define PIC_UTIL_RASTERIZER_HPP
20 
21 #include <algorithm>
22 #include <cstdlib>
23 
24 #include "../base.hpp"
25 #include "../image.hpp"
26 #include "../util/vec.hpp"
27 #include "../util/math.hpp"
28 
29 #ifndef PIC_EIGEN_NOT_BUNDLED
30  #include "../externals/Eigen/Dense"
31 #else
32  #include <Eigen/Dense>
33 #endif
34 
35 namespace pic {
36 
44 PIC_INLINE void drawLine(Image *img, Vec2i v0, Vec2i v1, float *color)
45 {
46  if(img == NULL || color == NULL) {
47  return;
48  }
49 
50  CLAMP(v0[0], img->width);
51  CLAMP(v1[0], img->width);
52  CLAMP(v0[1], img->height);
53  CLAMP(v1[1], img->height);
54 
55  float *data = img->data;
56 
57  if(v0[0] > v1[0]) {
58  std::swap(v0, v1);
59  }
60 
61  int dx = v1[0] - v0[0];
62  int dy = v1[1] - v0[1];
63 
64  //Vertical line
65  if(dx == 0){
66  if(v0[1] > v1[1]) {
67  std::swap(v0, v1);
68  }
69 
70  for(int y = v0[1]; y < v1[1]; y++) {
71  int ind = (v0[0] + y * img->width) * img->channels;
72 
73  for(int k = 0; k < img->channels; k++) {
74  data[ind + k] = color[k];
75  }
76  }
77  return;
78  }
79 
80  //Horizontal line
81  if(dy == 0) {
82  int ind_y = v0[1] * img->width;
83 
84  for(int x = v0[0]; x < v1[0]; x++) {
85  int ind = (x + ind_y) * img->channels;
86 
87  for(int k = 0; k < img->channels; k++) {
88  data[ind + k] = color[k];
89  }
90  }
91  }
92 
93  //General case
94  if(std::abs(dy) < std::abs(dx)) {
95  //m < 1
96  int e = 0;
97  int s;
98  if(dy < 0) {
99  s = -1;
100  dy = -dy;
101  } else {
102  s = 1;
103  }
104 
105  int y = v0[1];
106  for(int x = v0[0]; x <= v1[0]; x++) {
107  int ind = (x + y * img->width) * img->channels;
108 
109  for(int k = 0; k < img->channels; k++) {
110  data[ind + k] = color[k];
111  }
112 
113  e += dy;
114  if((e << 1) >= dx) {
115  y += s;
116  e -= dx;
117  }
118  }
119  } else {
120  //m > 1
121  if(v0[1] > v1[1]){
122  std::swap(v0, v1);
123  }
124 
125  dx = v1[0] - v0[0];
126  dy = v1[1] - v0[1];
127 
128  int e = 0;
129  int s;
130  if(dx < 0){
131  s = -1;
132  dx = -dx;
133  } else {
134  s = 1;
135  }
136 
137  int x = v0[0];
138 
139  for(int y = v0[1]; y <= v1[1]; y++) {
140 
141  int ind = (x + y * img->width) * img->channels;
142 
143  for(int k = 0; k < img->channels; k++) {
144  data[ind + k] = color[k];
145  }
146 
147  e += dx;
148  if((e << 1) >= dy) {
149  x += s;
150  e -= dy;
151  }
152  }
153  }
154 }
155 
161 #ifndef PIC_DISABLE_EIGEN
162 PIC_INLINE void drawPoints(Image *img, std::vector< Eigen::Vector2f > &points, float *color)
163 {
164  if(img == NULL) {
165  return;
166  }
167 
168  if(color == NULL) {
169  color = new float[img->channels];
170  for(int i = 0; i < img->channels; i++) {
171  color[i] = 1.0f;
172  }
173  }
174 
175  for(size_t i = 0; i < points.size(); i++) {
176  int x = int(points[i][0]);
177  int y = int(points[i][1]);
178  float *pixel = (*img)(x, y);
179 
180  for(int j = 0; j < img->channels; j++) {
181  pixel[j] = color[j];
182  }
183  }
184 }
185 #endif
186 
195 PIC_INLINE void evaluateGaussian(Image *img, float sigma = -1.0f,
196  bool bNormTerm = false)
197 {
198  if(img != NULL) {
199  return;
200  }
201 
202  if(sigma < 0.0f) {
203  sigma = float(MIN(img->width, img->height)) / 5.0f;
204  }
205 
206  float sigma2 = (sigma * sigma * 2.0f);
207 
208  int halfWidth = img->width >> 1;
209  int halfHeight = img->height >> 1;
210 
211  float normTerm = bNormTerm ? sigma * sqrtf(C_PI) : 1.0f ;
212 
213  #pragma omp parallel for
214 
215  for(int j = 0; j < img->height; j++) {
216  int j_squared = j - halfHeight;
217  j_squared = j_squared * j_squared;
218 
219  for(int i = 0; i < img->width; i++) {
220  int i_squared = i - halfWidth;
221  i_squared = i_squared * i_squared;
222 
223  float gaussVal = expf(-float(i_squared + j_squared) / sigma2) / normTerm;
224 
225  float *tmp_data = (*img)(i, j);
226 
227  for(int k = 0; k < img->channels; k++) {
228  tmp_data[k] = gaussVal;
229  }
230  }
231  }
232 }
233 
239 {
240  if(img == NULL) {
241  return;
242  }
243 
244  int halfWidth = img->width >> 1;
245  int halfHeight = img->height >> 1;
246 
247  int radius_squared = (halfWidth * halfWidth + halfHeight * halfHeight) >> 1;
248 
249  #pragma omp parallel for
250 
251  for(int j = 0; j < img->height; j++) {
252  int j_squared = j - halfHeight;
253  j_squared = j_squared * j_squared;
254 
255  for(int i = 0; i < img->width; i++) {
256  int i_squared = i - halfWidth;
257  i_squared = i_squared * i_squared;
258 
259  float val = 0.0f;
260 
261  if((i_squared + j_squared) < radius_squared) {
262  val = 1.0f;
263  }
264 
265  float *tmp_data = (*img)(i, j);
266 
267  for(int k = 0; k < img->channels; k++) {
268  tmp_data[k] = val;
269  }
270  }
271  }
272 }
273 
274 }
275 
276 #endif //
float * data
data is the main buffer where pixel values are stored.
Definition: image.hpp:91
int channels
Definition: image.hpp:80
const float C_PI
Definition: math.hpp:50
PIC_INLINE void drawPoints(Image *img, std::vector< Eigen::Vector2f > &points, float *color)
drawPoints
Definition: rasterizer.hpp:162
PIC_INLINE void drawLine(Image *img, Vec2i v0, Vec2i v1, float *color)
drawLine renders a line (v0, v1) with color into img.
Definition: rasterizer.hpp:44
#define PIC_INLINE
Definition: base.hpp:33
#define MIN(a, b)
Definition: math.hpp:69
The Image class stores an image as buffer of float.
Definition: image.hpp:60
PIC_INLINE void evaluateGaussian(Image *img, float sigma=-1.0f, bool bNormTerm=false)
evaluateGaussian renders a Gaussian function which is centred in the image.
Definition: rasterizer.hpp:195
Definition: bilateral_separation.hpp:25
#define CLAMP(x, a)
Definition: math.hpp:77
int width
Definition: image.hpp:80
PIC_INLINE void evaluateSolid(Image *img)
evaluateSolid renders a centred circle.
Definition: rasterizer.hpp:238
int height
Definition: image.hpp:80
The Vec class.
Definition: vec.hpp:35