Geogram  Version 1.9.1-rc
A programming library of geometric algorithms
image_rasterizer.h
1 /*
2  * Copyright (c) 2000-2022 Inria
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * * Neither the name of the ALICE Project-Team nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  *
29  * Contact: Bruno Levy
30  *
31  * https://www.inria.fr/fr/bruno-levy
32  *
33  * Inria,
34  * Domaine de Voluceau,
35  * 78150 Le Chesnay - Rocquencourt
36  * FRANCE
37  *
38  */
39 
40 #ifndef H_OGF_IMAGE_IO_IMAGE_RASTERIZER_H
41 #define H_OGF_IMAGE_IO_IMAGE_RASTERIZER_H
42 
43 #include <geogram/basic/common.h>
44 #include <geogram/image/image.h>
45 
51 namespace GEO {
52 
56  class GEOGRAM_API ImageRasterizer {
57  public:
58 
64 
68  void clear();
69 
80  void triangle(
81  const vec2i& P1, const Color& c1,
82  const vec2i& P2, const Color& c2,
83  const vec2i& P3, const Color& c3
84  );
85 
86 
96  void triangle(
97  const vec2& p1, const Color& c1,
98  const vec2& p2, const Color& c2,
99  const vec2& p3, const Color& c3
100  ) {
101  triangle(
102  transform(p1),c1,
103  transform(p2),c2,
104  transform(p3),c3
105  );
106  }
107 
117  void segment(const vec2i& P1, const vec2i& P2, const Color& c);
118 
127  void segment(const vec2& p1, const vec2& p2, const Color& c) {
128  segment(transform(p1), transform(p2), c);
129  }
130 
140  void fillcircle(const vec2i& C, int radius, const Color& c);
141 
151  void fillcircle(const vec2& C, double radius, const Color& c) {
152  fillcircle(
153  transform(C),
154  int(radius*double(image_->width())),
155  c
156  );
157  }
158 
164  void flood_fill(int x, int y, const Color& c);
165 
174  void set_pixel(int x, int y, const Color& c) {
175  geo_debug_assert(x >= 0 && x < int(image_->width()));
176  geo_debug_assert(y >= 0 && y < int(image_->height()));
177  switch(component_encoding_) {
178  case Image::BYTE: {
179  Memory::byte* pixel_ptr =
180  (Memory::byte*)(
181  image_->pixel_base(index_t(x),index_t(y))
182  );
183  for(index_t comp=0; comp<nb_components_; ++comp) {
184  pixel_ptr[comp] = Memory::byte(c[comp] * 255.0);
185  }
186  } break;
187  case Image::FLOAT32: {
188  Numeric::float32* pixel_ptr =
189  (Numeric::float32*)(void*)(
190  image_->pixel_base(index_t(x),index_t(y))
191  );
192  for(index_t comp=0; comp<nb_components_; ++comp) {
193  pixel_ptr[comp] = Numeric::float32(c[comp]);
194  }
195  } break;
196  case Image::FLOAT64: {
197  Numeric::float64* pixel_ptr =
198  (Numeric::float64*)(void*)(
199  image_->pixel_base(index_t(x),index_t(y))
200  );
201  for(index_t comp=0; comp<nb_components_; ++comp) {
202  pixel_ptr[comp] = Numeric::float64(c[comp]);
203  }
204  } break;
205  case Image::INT16:
206  case Image::INT32: {
208  }
209  }
210  }
211 
219  bool pixel_is_black(int x, int y) const {
220  Memory::byte* p = image_->pixel_base_byte_ptr(
221  index_t(x),index_t(y)
222  );
223  bool result = true;
224  for(size_t c=0; c<image_->components_per_pixel(); ++c) {
225  result = result && (*p == 0);
226  }
227  return result;
228  }
229 
230  protected:
231 
237  vec2i transform(const vec2& p) const {
238  return vec2i(
239  Numeric::int32(double(image_->width()-1)*p.x),
240  Numeric::int32(double(image_->height()-1)*p.y)
241  );
242  }
243 
251  const Color& c1, const Color& c2, const Color& c3,
252  double l1, double l2, double l3,
253  Color& c
254  ) const {
255  c[0] = l1*c1[0] + l2*c2[0] + l3*c3[0];
256  c[1] = l1*c1[1] + l2*c2[1] + l3*c3[1];
257  c[2] = l1*c1[2] + l2*c2[2] + l3*c3[2];
258  c[3] = l1*c1[3] + l2*c2[3] + l3*c3[3];
259  }
260 
261  private:
262  Image* image_;
263  Image::ComponentEncoding component_encoding_;
264  index_t nb_components_;
265  };
266 }
267 
268 #endif
#define geo_assert_not_reached
Sets a non reachable point in the program.
Definition: assert.h:177
#define geo_debug_assert(x)
Verifies that a condition is met.
Definition: assert.h:196
A generic color type.
Definition: color.h:57
Draws triangles in an image.
void fillcircle(const vec2 &C, double radius, const Color &c)
Fills a circle in the target image.
ImageRasterizer(Image *image)
ImageRasterizer constructor.
void triangle(const vec2i &P1, const Color &c1, const vec2i &P2, const Color &c2, const vec2i &P3, const Color &c3)
Draws a triangle in the target image.
vec2i transform(const vec2 &p) const
Transforms a 2d point from world space to pixel coordinates.
void fillcircle(const vec2i &C, int radius, const Color &c)
Fills a circle in the target image.
bool pixel_is_black(int x, int y) const
Tests whether a given pixel is black.
void segment(const vec2 &p1, const vec2 &p2, const Color &c)
Draws a segment in the target image.
void set_pixel(int x, int y, const Color &c)
Sets a pixel of the image.
void flood_fill(int x, int y, const Color &c)
Flood-fill from a given pixel.
void clear()
Clears the target image.
void segment(const vec2i &P1, const vec2i &P2, const Color &c)
Draws a segment in the target image.
void triangle(const vec2 &p1, const Color &c1, const vec2 &p2, const Color &c2, const vec2 &p3, const Color &c3)
Draws a triangle in the target image.
void interpolate_color(const Color &c1, const Color &c2, const Color &c3, double l1, double l2, double l3, Color &c) const
Computes the linear interpolation between three colors.
An image.
Definition: image.h:59
ComponentEncoding
Indicates the datatype used to encode each component of the colors.
Definition: image.h:74
Generic maths vector.
Definition: vecg.h:70
Common include file, providing basic definitions. Should be included before anything else by all head...
unsigned char byte
Unsigned byte type.
Definition: memory.h:92
float float32
Definition: numeric.h:147
double float64
Definition: numeric.h:150
int32_t int32
Definition: numeric.h:129
Global Vorpaline namespace.
Definition: basic.h:55
geo_index_t index_t
The type for storing and manipulating indices.
Definition: numeric.h:329
vecng< 2, Numeric::int32 > vec2i
Represents points and vectors in 2d with integer coordinates.
Definition: geometry.h:100