PCL
ImageGeometry.h
Go to the documentation of this file.
1 // ____ ______ __
2 // / __ \ / ____// /
3 // / /_/ // / / /
4 // / ____// /___ / /___ PixInsight Class Library
5 // /_/ \____//_____/ PCL 2.6.11
6 // ----------------------------------------------------------------------------
7 // pcl/ImageGeometry.h - Released 2024-05-07T15:27:32Z
8 // ----------------------------------------------------------------------------
9 // This file is part of the PixInsight Class Library (PCL).
10 // PCL is a multiplatform C++ framework for development of PixInsight modules.
11 //
12 // Copyright (c) 2003-2024 Pleiades Astrophoto S.L. All Rights Reserved.
13 //
14 // Redistribution and use in both source and binary forms, with or without
15 // modification, is permitted provided that the following conditions are met:
16 //
17 // 1. All redistributions of source code must retain the above copyright
18 // notice, this list of conditions and the following disclaimer.
19 //
20 // 2. All redistributions in binary form must reproduce the above copyright
21 // notice, this list of conditions and the following disclaimer in the
22 // documentation and/or other materials provided with the distribution.
23 //
24 // 3. Neither the names "PixInsight" and "Pleiades Astrophoto", nor the names
25 // of their contributors, may be used to endorse or promote products derived
26 // from this software without specific prior written permission. For written
27 // permission, please contact info@pixinsight.com.
28 //
29 // 4. All products derived from this software, in any form whatsoever, must
30 // reproduce the following acknowledgment in the end-user documentation
31 // and/or other materials provided with the product:
32 //
33 // "This product is based on software from the PixInsight project, developed
34 // by Pleiades Astrophoto and its contributors (https://pixinsight.com/)."
35 //
36 // Alternatively, if that is where third-party acknowledgments normally
37 // appear, this acknowledgment must be reproduced in the product itself.
38 //
39 // THIS SOFTWARE IS PROVIDED BY PLEIADES ASTROPHOTO AND ITS CONTRIBUTORS
40 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
41 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PLEIADES ASTROPHOTO OR ITS
43 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
44 // EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, BUSINESS
45 // INTERRUPTION; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; AND LOSS OF USE,
46 // DATA OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49 // POSSIBILITY OF SUCH DAMAGE.
50 // ----------------------------------------------------------------------------
51 
52 #ifndef __PCL_ImageGeometry_h
53 #define __PCL_ImageGeometry_h
54 
56 
57 #include <pcl/Defs.h>
58 #include <pcl/Diagnostics.h>
59 
60 #include <pcl/Point.h>
61 #include <pcl/Rectangle.h>
62 
63 namespace pcl
64 {
65 
66 // ----------------------------------------------------------------------------
67 
68 #define m_width m_geometry->width
69 #define m_height m_geometry->height
70 #define m_numberOfChannels m_geometry->numberOfChannels
71 
72 // ----------------------------------------------------------------------------
73 
83 class PCL_CLASS ImageGeometry
84 {
85 public:
86 
90  int Width() const noexcept
91  {
92  return m_width;
93  }
94 
98  int Height() const noexcept
99  {
100  return m_height;
101  }
102 
107  int NumberOfChannels() const noexcept
108  {
109  return m_numberOfChannels;
110  }
111 
118  int LastChannel() const noexcept
119  {
120  return m_numberOfChannels-1;
121  }
122 
127  bool IsValidChannelIndex( int c ) const noexcept
128  {
129  return c >= 0 && c < m_numberOfChannels;
130  }
131 
135  bool IsEmpty() const noexcept
136  {
137  return m_width <= 0 || m_height <= 0 || m_numberOfChannels <= 0;
138  }
139 
145  Rect Bounds() const noexcept
146  {
147  return Rect( m_width, m_height );
148  }
149 
154  template <typename T>
155  bool Includes( const GenericPoint<T>& p ) const noexcept
156  {
157  return Includes( p.x, p.y );
158  }
159 
164  template <typename T>
165  bool Includes( const GenericRectangle<T>& r ) const noexcept
166  {
167  return Includes( r.x0, r.y0, r.x1, r.y1 );
168  }
169 
177  template <typename T>
178  bool Includes( T x0, T y0, T x1, T y1 ) const noexcept
179  {
180  pcl::OrderRect( x0, y0, x1, y1 );
181  return Includes( x0, y0 ) && Includes( x1-T( 1 ), y1-T( 1 ) );
182  }
183 
191  template <typename T>
192  bool Includes( T x, T y ) const noexcept
193  {
194  return x < m_width && y < m_height && x >= 0 && y >= 0;
195  }
196 
201  template <typename T>
202  bool Intersects( const pcl::GenericRectangle<T>& r ) const noexcept
203  {
204  return Intersects( r.x0, r.y0, r.x1, r.y1 );
205  }
206 
214  template <typename T>
215  bool Intersects( T x0, T y0, T x1, T y1 ) const noexcept
216  {
217  pcl::OrderRect( x0, y0, x1, y1 );
218  return x0 < m_width && y0 < m_height && x1 > T( 0 ) && y1 > T( 0 );
219  }
220 
225  template <typename T>
226  bool Clip( pcl::GenericPoint<T>& p ) const noexcept
227  {
228  return Clip( p.x, p.y );
229  }
230 
240  template <typename T>
241  bool Clip( T& x, T& y ) const noexcept
242  {
243  bool in = false;
244 
245  if ( x >= T( m_width ) ) x = T( m_width-1 );
246  else if ( x < T( 0 ) ) x = T( 0 );
247  else in = true;
248 
249  if ( y >= T( m_height ) ) y = T( m_height-1 );
250  else if ( y < T( 0 ) ) y = T( 0 );
251  else return in;
252 
253  return false;
254  }
255 
263  template <typename T>
264  bool Clip( pcl::GenericRectangle<T>& r ) const noexcept
265  {
266  return Clip( r.x0, r.y0, r.x1, r.y1 );
267  }
268 
282  template <typename T>
283  bool Clip( T& x0, T& y0, T& x1, T& y1 ) const noexcept
284  {
285  pcl::OrderRect( x0, y0, x1, y1 );
286 
287  int out = 0;
288 
289  if ( x0 >= T( m_width ) ) x0 = T( m_width ), ++out;
290  else if ( x0 < T( 0 ) ) x0 = T( 0 );
291 
292  if ( y0 >= T( m_height ) ) y0 = T( m_height ), ++out;
293  else if ( y0 < T( 0 ) ) y0 = T( 0 );
294 
295  if ( x1 > T( m_width ) ) x1 = T( m_width );
296  else if ( x1 <= T( 0 ) ) x1 = T( 0 ), ++out;
297 
298  if ( y1 > T( m_height ) ) y1 = T( m_height );
299  else if ( y1 <= T( 0 ) ) y1 = T( 0 ), ++out;
300 
301  return !out;
302  }
303 
307  size_type NumberOfPixels() const noexcept
308  {
309  return size_type( m_width )*size_type( m_height );
310  }
311 
316  size_type NumberOfSamples() const noexcept
317  {
318  return NumberOfPixels()*size_type( m_numberOfChannels );
319  }
320 
325  distance_type RowOffset( int y ) const noexcept
326  {
327  return distance_type( y )*distance_type( m_width );
328  }
329 
337  distance_type PixelOffset( int x, int y ) const noexcept
338  {
339  return RowOffset( y ) + distance_type( x );
340  }
341 
342 protected:
343 
344  /*
345  * Image geometry
346  */
347  struct Geometry
348  {
349  int width = 0;
350  int height = 0;
351  int numberOfChannels = 0;
352 
353  Geometry() = default;
354  Geometry( const Geometry& ) = default;
355 
356  size_type NumberOfPixels() const noexcept
357  {
358  return size_type( width )*size_type( height );
359  }
360 
361  void Assign( const Geometry& x ) noexcept
362  {
363  width = x.width;
364  height = x.height;
365  numberOfChannels = x.numberOfChannels;
366  }
367 
368  void Reset() noexcept
369  {
370  width = height = numberOfChannels = 0;
371  }
372  };
373 
374  Geometry* m_geometry = nullptr;
375 
376  ImageGeometry() = default;
377  ImageGeometry( const ImageGeometry& ) = default;
378  ImageGeometry& operator =( const ImageGeometry& ) = default;
379 
380  virtual ~ImageGeometry()
381  {
382  m_geometry = nullptr;
383  }
384 
385  void Swap( ImageGeometry& image ) noexcept
386  {
387  pcl::Swap( m_geometry, image.m_geometry );
388  }
389 };
390 
391 // ----------------------------------------------------------------------------
392 
393 #undef m_width
394 #undef m_height
395 #undef m_numberOfChannels
396 
397 // ----------------------------------------------------------------------------
398 
399 } // pcl
400 
401 #endif // __PCL_ImageGeometry_h
402 
403 // ----------------------------------------------------------------------------
404 // EOF pcl/ImageGeometry.h - Released 2024-05-07T15:27:32Z
A generic point in the two-dimensional space.
Definition: Point.h:100
A generic rectangle in the two-dimensional space.
Definition: Rectangle.h:314
Implements geometric properties of two-dimensional images.
Definition: ImageGeometry.h:84
int LastChannel() const noexcept
bool Intersects(const pcl::GenericRectangle< T > &r) const noexcept
bool Clip(pcl::GenericRectangle< T > &r) const noexcept
bool Includes(const GenericRectangle< T > &r) const noexcept
int Width() const noexcept
Definition: ImageGeometry.h:90
bool Intersects(T x0, T y0, T x1, T y1) const noexcept
bool Clip(T &x0, T &y0, T &x1, T &y1) const noexcept
distance_type PixelOffset(int x, int y) const noexcept
bool Clip(T &x, T &y) const noexcept
int Height() const noexcept
Definition: ImageGeometry.h:98
bool Includes(T x0, T y0, T x1, T y1) const noexcept
size_type NumberOfSamples() const noexcept
bool Includes(T x, T y) const noexcept
bool Clip(pcl::GenericPoint< T > &p) const noexcept
size_type NumberOfPixels() const noexcept
distance_type RowOffset(int y) const noexcept
int NumberOfChannels() const noexcept
Rect Bounds() const noexcept
bool IsEmpty() const noexcept
bool IsValidChannelIndex(int c) const noexcept
bool Includes(const GenericPoint< T > &p) const noexcept
void Swap(GenericPoint< T > &p1, GenericPoint< T > &p2) noexcept
Definition: Point.h:1459
void OrderRect(T &x0, T &y0, T &x1, T &y1) noexcept
Definition: Rectangle.h:260
ptrdiff_t distance_type
Definition: Defs.h:615
size_t size_type
Definition: Defs.h:609
PCL root namespace.
Definition: AbstractImage.h:77