PCL
PixelAllocator.h
Go to the documentation of this file.
1 // ____ ______ __
2 // / __ \ / ____// /
3 // / /_/ // / / /
4 // / ____// /___ / /___ PixInsight Class Library
5 // /_/ \____//_____/ PCL 2.7.0
6 // ----------------------------------------------------------------------------
7 // pcl/PixelAllocator.h - Released 2024-06-18T15:48:54Z
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_PixelAllocator_h
53 #define __PCL_PixelAllocator_h
54 
56 
57 #include <pcl/Defs.h>
58 #include <pcl/Diagnostics.h>
59 
60 #include <pcl/SharedPixelData.h>
61 
62 namespace pcl
63 {
64 
65 // ----------------------------------------------------------------------------
66 
67 template <class P> class PCL_CLASS GenericImage;
68 
69 // ----------------------------------------------------------------------------
70 
105 template <class P>
106 class PCL_CLASS PixelAllocator : public SharedPixelData
107 {
108 public:
109 
118  using pixel_traits = P;
119 
124  using sample = typename pixel_traits::sample;
125 
134  bool operator ==( const PixelAllocator<P>& x ) const noexcept
135  {
136  return SharedPixelData::operator ==( x );
137  }
138 
149  {
150  PCL_PRECONDITION( n != 0 )
151  return reinterpret_cast<sample*>( SharedPixelData::Allocate( n*sizeof( sample ) ) );
152  }
153 
165  sample* AllocatePixels( int width, int height ) const
166  {
167  return AllocatePixels( size_type( width )*size_type( height ) );
168  }
169 
182  {
183  PCL_PRECONDITION( n != 0 )
184  sample** cslots = reinterpret_cast<sample**>( SharedPixelData::Allocate( n*sizeof( sample* ) ) );
185  ::memset( cslots, 0, n*sizeof( sample* ) );
186  return cslots;
187  }
188 
199  template <typename T>
200  void Deallocate( T* p ) const
201  {
202  PCL_PRECONDITION( p != nullptr )
203  SharedPixelData::Deallocate( reinterpret_cast<void*>( p ) );
204  }
205 
206 private:
207 
208  PixelAllocator() = default;
209 
210  PixelAllocator( void* handle )
211  : SharedPixelData( handle, P::BitsPerSample(), P::IsFloatSample(), P::IsComplexSample() )
212  {
213  }
214 
215  PixelAllocator( const PixelAllocator<P>& x )
216  : SharedPixelData( x )
217  {
218  }
219 
220  PixelAllocator( int width, int height, int numberOfChannels, int colorSpace )
221  : SharedPixelData( width, height, numberOfChannels, P::BitsPerSample(), P::IsFloatSample(), colorSpace )
222  {
223  }
224 
225  PixelAllocator& operator =( const PixelAllocator<P>& x )
226  {
227  (void)SharedPixelData::operator =( x );
228  return *this;
229  }
230 
231  sample** GetSharedData() const
232  {
233  return reinterpret_cast<sample**>( SharedPixelData::GetSharedData() );
234  }
235 
236  void SetSharedData( sample** ptrToShared )
237  {
238  SharedPixelData::SetSharedData( reinterpret_cast<void**>( ptrToShared ) );
239  }
240 
241  friend class pcl::GenericImage<P>;
242 };
243 
244 // ----------------------------------------------------------------------------
245 
246 } // pcl
247 
248 #endif // __PCL_PixelAllocator_h
249 
250 // ----------------------------------------------------------------------------
251 // EOF pcl/PixelAllocator.h - Released 2024-06-18T15:48:54Z
Implements a generic, two-dimensional, shared or local image.
Definition: Image.h:278
Manages transparent allocation and deallocation of shared and local pixel data.
sample ** AllocateChannelSlots(size_type n) const
sample * AllocatePixels(int width, int height) const
void Deallocate(T *p) const
typename pixel_traits::sample sample
sample * AllocatePixels(size_type n) const
Handles transparent, type-independent allocation of local and shared pixel data blocks.
bool operator==(const SharedPixelData &x) const noexcept
bool operator==(const Array< T, A > &x1, const Array< T, A > &x2) noexcept
Definition: Array.h:2267
size_t size_type
Definition: Defs.h:609
PCL root namespace.
Definition: AbstractImage.h:77