Geogram  Version 1.9.1
A programming library of geometric algorithms
image.h
Go to the documentation of this file.
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_TYPES_IMAGE_H
41 #define H_OGF_IMAGE_TYPES_IMAGE_H
42 
43 #include <geogram/basic/common.h>
44 #include <geogram/image/colormap.h>
45 
51 namespace GEO {
52 
53 //_________________________________________________________
54 
55 
59  class GEOGRAM_API Image : public Counted {
60  public:
61 
67  GRAY, INDEXED, RGB, BGR, RGBA, YUV
68  };
69 
75  BYTE, INT16, INT32, FLOAT32, FLOAT64
76  };
77 
78 
83  Image();
84 
95  ColorEncoding color_rep, ComponentEncoding component_rep,
96  index_t width, index_t height=1, index_t depth=1
97  ) {
98  base_mem_ = nullptr;
99  initialize(color_rep, component_rep, width, height, depth);
100  }
101 
105  ~Image() override;
106 
113  virtual void acquire();
114 
121  index_t dimension() const {
122  return dimension_;
123  }
124 
130  index_t size(index_t axis) const {
131  geo_assert(axis < 3);
132  return size_[axis];
133  }
134 
139  index_t width() const {
140  return size_[0];
141  }
142 
148  index_t height() const {
149  return size_[1];
150  }
151 
157  index_t depth() const {
158  return size_[2];
159  }
160 
165  size_t bytes_per_pixel() const {
166  return bytes_per_pixel_;
167  }
168 
173  size_t components_per_pixel() const {
174  return nb_components(color_encoding());
175  }
176 
181  size_t nb_pixels() const {
182  return size_t(size_[0]) * size_t(size_[1]) * size_t(size_[2]);
183  }
184 
190  size_t bytes() const {
191  return nb_pixels() * bytes_per_pixel();
192  }
193 
199  return component_encoding_;
200  }
201 
207  return color_encoding_;
208  }
209 
214  const Colormap* colormap() const {
215  return colormap_;
216  }
217 
223  return colormap_;
224  }
225 
231  void set_colormap(Colormap* colormap) {
232  colormap_ = colormap;
233  }
234 
241  return base_mem_;
242  }
243 
251  return byte_ptr(base_mem_);
252  }
253 
261  return int16_ptr(base_mem_);
262  }
263 
271  return int32_ptr(base_mem_);
272  }
273 
281  return float32_ptr(base_mem_);
282  }
283 
291  return float64_ptr(base_mem_);
292  }
293 
301  return base_mem() + x * factor_[0];
302  }
303 
304 
312  return byte_ptr(base_mem() + x * factor_[0]);
313  }
314 
322  return int16_ptr(base_mem() + x * factor_[0]);
323  }
324 
332  return int32_ptr(base_mem() + x * factor_[0]);
333  }
334 
342  return float32_ptr(base_mem() + x * factor_[0]);
343  }
344 
352  return float64_ptr(base_mem() + x * factor_[0]);
353  }
354 
355 
363  return base_mem() + x * factor_[0] + y * factor_[1];
364  }
365 
373  return byte_ptr(base_mem() + x * factor_[0] + y * factor_[1]);
374  }
375 
383  return int16_ptr(base_mem() + x * factor_[0] + y * factor_[1]);
384  }
385 
393  return int32_ptr(base_mem() + x * factor_[0] + y * factor_[1]);
394  }
395 
403  return float32_ptr(base_mem() + x * factor_[0] + y * factor_[1]);
404  }
405 
413  return float64_ptr(base_mem() + x * factor_[0] + y * factor_[1]);
414  }
415 
423  return base_mem() +
424  x * factor_[0] + y * factor_[1] + z * factor_[2];
425  }
426 
435  return byte_ptr(base_mem() +
436  x * factor_[0] + y * factor_[1] + z * factor_[2]
437  );
438  }
439 
448  return int16_ptr(base_mem() +
449  x * factor_[0] + y * factor_[1] + z * factor_[2]
450  );
451  }
452 
461  return int32_ptr(base_mem() +
462  x * factor_[0] + y * factor_[1] + z * factor_[2]
463  );
464  }
465 
475  index_t x, index_t y, index_t z
476  ) {
477  return float32_ptr(base_mem() +
478  x * factor_[0] + y * factor_[1] + z * factor_[2]
479  );
480  }
481 
491  index_t x, index_t y, index_t z
492  ) {
493  return float64_ptr(base_mem() +
494  x * factor_[0] + y * factor_[1] + z * factor_[2]
495  );
496  }
497 
504  static size_t nb_components(ColorEncoding color_rep);
505 
512  static size_t bytes_per_component(ComponentEncoding component_rep);
513 
524  geo_debug_assert(component_encoding_ == BYTE);
525  return ptr;
526  }
527 
538  geo_debug_assert(component_encoding_ == INT16);
539  return (Numeric::int16*)(void*)(ptr);
540  }
541 
552  geo_debug_assert(component_encoding_ == INT32);
553  return (Numeric::int32*)(void*)(ptr);
554  }
555 
567  geo_debug_assert(component_encoding_ == FLOAT32);
568  return (Numeric::float32*)(void*)(ptr);
569  }
570 
582  geo_debug_assert(component_encoding_ == FLOAT64);
583  return (Numeric::float64*)(void*)(ptr);
584  }
585 
590 
598  void swap_components(index_t channel1, index_t channel2);
599 
611  ColorEncoding color_rep, ComponentEncoding component_rep,
612  index_t size_x, index_t size_y=1, index_t size_z=1
613  );
614 
615  protected:
616  ColorEncoding color_encoding_;
617  ComponentEncoding component_encoding_;
618  Colormap_var colormap_;
619  size_t factor_[3];
620  Memory::pointer base_mem_;
621  index_t dimension_;
622  index_t size_[3];
623  size_t bytes_per_pixel_;
624 
625  private:
629  Image(const Image& rhs);
630 
634  Image& operator=(const Image& rhs);
635  };
636 
638 
639 //_________________________________________________________
640 
641 }
642 #endif
#define geo_assert(x)
Verifies that a condition is met.
Definition: assert.h:149
#define geo_debug_assert(x)
Verifies that a condition is met.
Definition: assert.h:196
A Colormap.
Definition: colormap.h:61
Base class for reference-counted objects.
Definition: counted.h:71
An image.
Definition: image.h:59
Numeric::float64 * pixel_base_float64_ptr(index_t x, index_t y, index_t z)
Gets the address of a pixel in a 3D image as a float64 pointer.
Definition: image.h:490
Numeric::float32 * pixel_base_float32_ptr(index_t x, index_t y)
Gets the address of a pixel in a 2D image as a float32 pointer.
Definition: image.h:402
Image()
Image constructor.
Memory::pointer pixel_base(index_t x)
Gets the address of a pixel in a 1D image.
Definition: image.h:300
Numeric::float64 * base_mem_float64_ptr() const
Gets the base memory as a 64 bits floating point pointer.
Definition: image.h:290
void swap_components(index_t channel1, index_t channel2)
Swaps two color components of this image.
ComponentEncoding component_encoding() const
Gets the ComponentEncoding.
Definition: image.h:198
Numeric::float32 * float32_ptr(Memory::pointer ptr) const
Converts an untyped pointer into a 32 bits floating point pointer.
Definition: image.h:566
Numeric::int16 * pixel_base_int16_ptr(index_t x)
Gets the address of a pixel in a 1D image as a int16 pointer.
Definition: image.h:321
Numeric::float32 * base_mem_float32_ptr() const
Gets the base memory as a 32 bits floating point pointer.
Definition: image.h:280
Numeric::int16 * base_mem_int16_ptr() const
Gets the base memory as a 16 bits integer pointer.
Definition: image.h:260
Numeric::int32 * pixel_base_int32_ptr(index_t x, index_t y, index_t z)
Gets the address of a pixel in a 3D image as an int32 pointer.
Definition: image.h:460
Numeric::float32 * pixel_base_float32_ptr(index_t x, index_t y, index_t z)
Gets the address of a pixel in a 3D image as a float32 pointer.
Definition: image.h:474
~Image() override
Image destructor.
ColorEncoding color_encoding() const
Gets the ColorEncoding.
Definition: image.h:206
index_t height() const
Gets the height of the image.
Definition: image.h:148
Numeric::float32 * pixel_base_float32_ptr(index_t x)
Gets the address of a pixel in a 1D image as a float32 pointer.
Definition: image.h:341
static size_t bytes_per_component(ComponentEncoding component_rep)
Gets the number of bytes used by a ComponentEncoding.
Memory::byte * pixel_base_byte_ptr(index_t x, index_t y, index_t z)
Gets the address of a pixel in a 3D image as a byte pointer.
Definition: image.h:434
static size_t nb_components(ColorEncoding color_rep)
Gets the number of components associated with a ColorEncoding.
size_t nb_pixels() const
Gets the number of pixels.
Definition: image.h:181
Memory::byte * pixel_base_byte_ptr(index_t x, index_t y)
Gets the address of a pixel in a 2D image as a byte pointer.
Definition: image.h:372
const Colormap * colormap() const
Gets the Colormap.
Definition: image.h:214
Numeric::float64 * pixel_base_float64_ptr(index_t x)
Gets the address of a pixel in a 1D image as a float64 pointer.
Definition: image.h:351
index_t depth() const
Gets the depth of the image.
Definition: image.h:157
Numeric::int32 * base_mem_int32_ptr() const
Gets the base memory as a 32 bits integer pointer.
Definition: image.h:270
Numeric::int32 * pixel_base_int32_ptr(index_t x)
Gets the address of a pixel in a 1D image as a int32 pointer.
Definition: image.h:331
index_t dimension() const
Gets the dimension of the image.
Definition: image.h:121
Image(ColorEncoding color_rep, ComponentEncoding component_rep, index_t width, index_t height=1, index_t depth=1)
Image constructor.
Definition: image.h:94
virtual void acquire()
Some implementations get the image from some sources. Default implementation does nothing.
Memory::byte * byte_ptr(Memory::pointer ptr) const
Converts an untyped pointer into a byte pointer.
Definition: image.h:523
Numeric::int32 * pixel_base_int32_ptr(index_t x, index_t y)
Gets the address of a pixel in a 2D image as an int32 pointer.
Definition: image.h:392
void flip_vertically()
Flips this image along the y axis.
void initialize(ColorEncoding color_rep, ComponentEncoding component_rep, index_t size_x, index_t size_y=1, index_t size_z=1)
Creates storage for the specified encoding and image dimensions.
size_t bytes() const
Gets the number of bytes.
Definition: image.h:190
Memory::pointer pixel_base(index_t x, index_t y)
Gets the address of a pixel in a 2D image.
Definition: image.h:362
Numeric::float64 * pixel_base_float64_ptr(index_t x, index_t y)
Gets the address of a pixel in a 2D image as a float64 pointer.
Definition: image.h:412
Numeric::int16 * pixel_base_int16_ptr(index_t x, index_t y)
Gets the address of a pixel in a 2D image as an int16 pointer.
Definition: image.h:382
Numeric::int32 * int32_ptr(Memory::pointer ptr) const
Converts an untyped pointer into a 32 bits integer pointer.
Definition: image.h:551
Numeric::float64 * float64_ptr(Memory::pointer ptr) const
Converts an untyped pointer into a 64 bits floating point pointer.
Definition: image.h:581
ComponentEncoding
Indicates the datatype used to encode each component of the colors.
Definition: image.h:74
index_t size(index_t axis) const
Gets the size of the image along one of the axes.
Definition: image.h:130
void set_colormap(Colormap *colormap)
Sets the Colormap.
Definition: image.h:231
Memory::pointer base_mem() const
Gets the base memory.
Definition: image.h:240
Colormap * colormap()
Gets the Colormap.
Definition: image.h:222
index_t width() const
Gets the width of the image.
Definition: image.h:139
Memory::byte * base_mem_byte_ptr() const
Gets the base memory as a byte pointer.
Definition: image.h:250
size_t components_per_pixel() const
Gets the number of components per pixel.
Definition: image.h:173
ColorEncoding
Indicates how colors are encoded within the image.
Definition: image.h:66
size_t bytes_per_pixel() const
Gets the number of bytes per pixel.
Definition: image.h:165
Memory::pointer pixel_base(index_t x, index_t y, index_t z)
Gets the address of a pixel in a 3D image.
Definition: image.h:422
Numeric::int16 * pixel_base_int16_ptr(index_t x, index_t y, index_t z)
Gets the address of a pixel in a 3D image as an int16 pointer.
Definition: image.h:447
Memory::byte * pixel_base_byte_ptr(index_t x)
Gets the address of a pixel in a 1D image as a byte pointer.
Definition: image.h:311
Numeric::int16 * int16_ptr(Memory::pointer ptr) const
Converts an untyped pointer into a 16 bits integer pointer.
Definition: image.h:537
Colormap type.
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
byte * pointer
Pointer to unsigned byte(s)
Definition: memory.h:104
float float32
Definition: numeric.h:147
double float64
Definition: numeric.h:150
int32_t int32
Definition: numeric.h:129
int16_t int16
Definition: numeric.h:126
Global Vorpaline namespace.
Definition: basic.h:55
geo_index_t index_t
The type for storing and manipulating indices.
Definition: numeric.h:329
void initialize(int flags=GEOGRAM_INSTALL_NONE)
Initialize Geogram.