bioformats  5.1.8
PixelBuffer.h
1 /*
2  * #%L
3  * OME-BIOFORMATS C++ library for image IO.
4  * Copyright © 2006 - 2016 Open Microscopy Environment:
5  * - Massachusetts Institute of Technology
6  * - National Institutes of Health
7  * - University of Dundee
8  * - Board of Regents of the University of Wisconsin-Madison
9  * - Glencoe Software, Inc.
10  * %%
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions are met:
13  *
14  * 1. Redistributions of source code must retain the above copyright notice,
15  * this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright notice,
17  * this list of conditions and the following disclaimer in the documentation
18  * and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  * The views and conclusions contained in the software and documentation are
33  * those of the authors and should not be interpreted as representing official
34  * policies, either expressed or implied, of any organization.
35  * #L%
36  */
37 
38 #ifndef OME_BIOFORMATS_PIXELBUFFER_H
39 #define OME_BIOFORMATS_PIXELBUFFER_H
40 
41 #include <istream>
42 #include <limits>
43 #include <ostream>
44 #include <stdexcept>
45 #include <string>
46 
47 // Disable expensive bounds checking
48 #define BOOST_DISABLE_ASSERTS 1
49 #include <boost/multi_array.hpp>
50 
51 #include <ome/bioformats/PixelProperties.h>
52 
53 #include <ome/common/variant.h>
54 
55 #include <ome/compat/array.h>
56 #include <ome/compat/cstdint.h>
57 #include <ome/compat/memory.h>
58 
59 #include <ome/xml/model/enums/DimensionOrder.h>
60 
61 namespace ome
62 {
63  namespace bioformats
64  {
65 
80  {
90  };
91 
105  {
106  public:
108  static const uint16_t dimensions = 9;
109 
111  typedef boost::multi_array_types::size_type size_type;
112 
114  typedef boost::multi_array_types::index index;
115 
117  typedef ome::compat::array<boost::multi_array_types::index,
119 
121  typedef boost::general_storage_order<dimensions> storage_order_type;
122 
124  typedef boost::detail::multi_array::extent_gen<dimensions> range_type;
125 
126  protected:
136  pixeltype(pixeltype),
137  endiantype(endiantype)
138  {}
139 
140  public:
143  {}
144 
151  pixelType() const
152  {
153  return pixeltype;
154  }
155 
161  EndianType
162  endianType() const
163  {
164  return endiantype;
165  }
166 
179  static storage_order_type
181  bool interleaved);
182 
191  static storage_order_type
193 
194  private:
196  const ::ome::xml::model::enums::PixelType pixeltype;
197 
200  };
201 
235  template<typename T>
237  {
238  public:
240  typedef T value_type;
241 
247  typedef boost::multi_array_ref<value_type, dimensions> array_ref_type;
248 
253  typedef boost::multi_array<value_type, dimensions> array_type;
254 
261  explicit PixelBuffer():
262  PixelBufferBase(::ome::xml::model::enums::PixelType::UINT8, ENDIAN_NATIVE),
263  multiarray(ome::compat::shared_ptr<array_type>(new array_type(boost::extents[1][1][1][1][1][1][1][1][1],
265  {}
266 
278  template<class ExtentList>
279  explicit
280  PixelBuffer(const ExtentList& extents,
285  multiarray(ome::compat::shared_ptr<array_type>(new array_type(extents, storage)))
286  {}
287 
302  template<class ExtentList>
303  explicit
304  PixelBuffer(value_type *pixeldata,
305  const ExtentList& extents,
310  multiarray(ome::compat::shared_ptr<array_ref_type>(new array_ref_type(pixeldata, extents, storage)))
311  {}
312 
324  explicit
325  PixelBuffer(const range_type& range,
330  multiarray(ome::compat::shared_ptr<array_type>(new array_type(range, storage)))
331  {}
332 
347  explicit
348  PixelBuffer(value_type *pixeldata,
349  const range_type& range,
354  multiarray(ome::compat::shared_ptr<array_ref_type>(new array_ref_type(pixeldata, range, storage)))
355  {}
356 
365  explicit
366  PixelBuffer(const PixelBuffer& buffer):
367  PixelBufferBase(buffer),
368  multiarray(buffer.multiarray)
369  {}
370 
372  virtual ~PixelBuffer()
373  {}
374 
381  array_ref_type&
382  array();
383 
390  const array_ref_type&
391  array() const;
392 
401  value_type *
403  {
404  return array().data();
405  }
406 
415  const value_type *
416  data() const
417  {
418  return array().data();
419  }
420 
431  bool
432  valid() const
433  {
434  bool is_valid = true;
435 
436  try
437  {
438  array();
439  }
440  catch (const std::runtime_error&)
441  {
442  is_valid = false;
443  }
444 
445  return is_valid;
446  }
447 
455  bool
456  managed() const
457  {
458  return (boost::get<ome::compat::shared_ptr<array_type> >(&multiarray) != 0);
459  }
460 
464  size_type
465  num_elements() const
466  {
467  return array().num_elements();
468  }
469 
473  size_type
475  {
476  return array().num_dimensions();
477  }
478 
486  const size_type *
487  shape() const
488  {
489  return array().shape();
490  }
491 
499  const boost::multi_array_types::index *
500  strides() const
501  {
502  return array().strides();
503  }
504 
513  const boost::multi_array_types::index *
514  index_bases() const
515  {
516  return array().index_bases();
517  }
518 
529  const value_type *
530  origin() const
531  {
532  return array().origin();
533  }
534 
540  const storage_order_type&
542  {
543  return array().storage_order();
544  }
545 
556  PixelBuffer&
558  {
559  array() = rhs.array();
560  return *this;
561  }
562 
573  PixelBuffer&
574  operator = (const array_ref_type& rhs)
575  {
576  array() = rhs;
577  return *this;
578  }
579 
586  bool
587  operator == (const PixelBuffer& rhs) const
588  {
589  return array() == rhs.array();
590  }
591 
598  bool
599  operator == (const array_ref_type& rhs) const
600  {
601  return array() == rhs;
602  }
603 
610  bool
611  operator != (const PixelBuffer& rhs) const
612  {
613  return array() != rhs.array();
614  }
615 
622  bool
623  operator != (const array_ref_type& rhs) const
624  {
625  return array() != rhs;
626  }
627 
634  bool
635  operator < (const PixelBuffer& rhs) const
636  {
637  return array() < rhs.array();
638  }
639 
646  bool
647  operator < (const array_ref_type& rhs) const
648  {
649  return array() < rhs;
650  }
651 
658  bool
659  operator <= (const PixelBuffer& rhs) const
660  {
661  return array() <= rhs.array();
662  }
663 
670  bool
671  operator <= (const array_ref_type& rhs) const
672  {
673  return array() <= rhs;
674  }
675 
682  bool
683  operator > (const PixelBuffer& rhs) const
684  {
685  return array() > rhs.array();
686  }
687 
694  bool
695  operator > (const array_ref_type& rhs) const
696  {
697  return array() > rhs;
698  }
699 
706  bool
707  operator >= (const PixelBuffer& rhs) const
708  {
709  return array() >= rhs.array();
710  }
711 
718  bool
719  operator >= (const array_ref_type& rhs) const
720  {
721  return array() >= rhs;
722  }
723 
732  template <typename InputIterator>
733  void
734  assign(InputIterator begin,
735  InputIterator end)
736  {
737  array().assign(begin, end);
738  }
739 
750  value_type&
751  at(const indices_type& indices)
752  {
753  return array()(indices);
754  }
755 
766  const value_type&
767  at(const indices_type& indices) const
768  {
769  return array()(indices);
770  }
771 
782  template<class charT, class traits>
783  inline void
784  read(std::basic_istream<charT,traits>& stream)
785  {
786  stream.read(reinterpret_cast<char *>(data()),
787  static_cast<std::streamsize>(num_elements() * sizeof(value_type)));
788  }
789 
800  template<class charT, class traits>
801  inline void
802  write(std::basic_ostream<charT,traits>& stream) const
803  {
804  stream.write(reinterpret_cast<const char *>(data()),
805  static_cast<std::streamsize>(num_elements() * sizeof(value_type)));
806  }
807 
808  private:
824  boost::variant<ome::compat::shared_ptr<array_type>,
825  ome::compat::shared_ptr<array_ref_type> > multiarray;
826  };
827 
828  namespace detail
829  {
830 
832  template<typename T>
833  struct PixelBufferArrayVisitor : public boost::static_visitor<typename PixelBuffer<T>::array_ref_type&>
834  {
841  template <typename U>
843  operator() (U& v) const
844  {
845  if (!v)
846  throw std::runtime_error("Null array type");
847  return *v;
848  }
849  };
850 
852  template<typename T>
853  struct PixelBufferConstArrayVisitor : public boost::static_visitor<const typename PixelBuffer<T>::array_ref_type&>
854  {
861  template <typename U>
862  const typename PixelBuffer<T>::array_ref_type&
863  operator() (U& v) const
864  {
865  if (!v)
866  throw std::runtime_error("Null array type");
867  return *v;
868  }
869  };
870 
871  }
872 
873  template<typename T>
874  typename PixelBuffer<T>::array_ref_type&
876  {
878  return boost::apply_visitor(v, multiarray);
879  }
880 
881  template<typename T>
882  const typename PixelBuffer<T>::array_ref_type&
884  {
886  return boost::apply_visitor(v, multiarray);
887  }
888 
889  }
890 }
891 
892 namespace std
893 {
894 
902  template<typename T, class charT, class traits>
903  inline std::basic_istream<charT,traits>&
904  operator>> (std::basic_istream<charT,traits>& is,
906  {
907  buf.read(is);
908  return is;
909  }
910 
918  template<typename T, class charT, class traits>
919  inline std::basic_ostream<charT,traits>&
920  operator<< (std::basic_ostream<charT,traits>& os,
921  const ::ome::bioformats::PixelBuffer<T>& buf)
922  {
923  buf.write(os);
924  return os;
925  }
926 
927 }
928 
929 #endif // OME_BIOFORMATS_PIXELBUFFER_H
930 
931 /*
932  * Local Variables:
933  * mode:C++
934  * End:
935  */
uint8
Definition: PixelType.h:82
PixelBuffer & operator=(const PixelBuffer &rhs)
Assign a pixel buffer.
Definition: PixelBuffer.h:557
EndianType
Endianness.
Definition: Types.h:68
boost::multi_array_types::index index
Index type.
Definition: PixelBuffer.h:114
Memory type substitution.
virtual ~PixelBuffer()
Destructor.
Definition: PixelBuffer.h:372
Definition: length.h:576
bool operator!=(const PixelBuffer &rhs) const
Compare a pixel buffer for inequality.
Definition: PixelBuffer.h:611
bool operator>=(const PixelBuffer &rhs) const
Greater than or equal comparison with a pixel buffer.
Definition: PixelBuffer.h:707
bool operator<(const PixelBuffer &rhs) const
Less than comparison with a pixel buffer.
Definition: PixelBuffer.h:635
Native endian.
Definition: Types.h:72
bool operator==(const PixelBuffer &rhs) const
Compare a pixel buffer for equality.
Definition: PixelBuffer.h:587
virtual ~PixelBufferBase()
Destructor.
Definition: PixelBuffer.h:142
bool valid() const
Check the buffer validity.
Definition: PixelBuffer.h:432
::ome::xml::model::enums::PixelType pixelType() const
Get the pixel type in use.
Definition: PixelBuffer.h:151
static storage_order_type default_storage_order()
Generate default storage ordering.
Definition: PixelBuffer.cpp:133
const boost::multi_array_types::index * strides() const
Get the strides of the multi-dimensional array.
Definition: PixelBuffer.h:500
size_type num_dimensions() const
Get the number of dimensions in the multi-dimensional array.
Definition: PixelBuffer.h:474
STL namespace.
static storage_order_type make_storage_order(ome::xml::model::enums::DimensionOrder order, bool interleaved)
Generate storage ordering for a given dimension order.
Definition: PixelBuffer.cpp:54
Temporal t dimension (T).
Definition: PixelBuffer.h:84
const value_type * data() const
Get the raw data.
Definition: PixelBuffer.h:416
ome::compat::array< boost::multi_array_types::index, PixelBufferBase::dimensions > indices_type
Type used to index all dimensions in public interfaces.
Definition: PixelBuffer.h:118
const PixelBuffer< T >::array_ref_type & operator()(U &v) const
PixelBuffer of any type.
Definition: PixelBuffer.h:863
Dimensions
Dimensions.
Definition: PixelBuffer.h:79
void write(std::basic_ostream< charT, traits > &stream) const
Write raw pixel data to a stream in physical storage order.
Definition: PixelBuffer.h:802
PixelBuffer()
Default constructor.
Definition: PixelBuffer.h:261
Logical sub-channel (typically used for RGB channel sub-components) (S).
Definition: PixelBuffer.h:86
PixelBuffer(const range_type &range,::ome::xml::model::enums::PixelType pixeltype=::ome::xml::model::enums::PixelType::UINT8, EndianType endiantype=ENDIAN_NATIVE, const storage_order_type &storage=PixelBufferBase::default_storage_order())
Construct from ranges (internal storage).
Definition: PixelBuffer.h:325
Spatial y dimension (Y).
Definition: PixelBuffer.h:82
Find a PixelBuffer data array of a specific pixel type.
Definition: PixelBuffer.h:853
Array type substitution.
bool managed() const
Check if the buffer is internally managed.
Definition: PixelBuffer.h:456
PixelBuffer(const ExtentList &extents,::ome::xml::model::enums::PixelType pixeltype=::ome::xml::model::enums::PixelType::UINT8, EndianType endiantype=ENDIAN_NATIVE, const storage_order_type &storage=PixelBufferBase::default_storage_order())
Construct from extents (internal storage).
Definition: PixelBuffer.h:280
Buffer for a specific pixel type.
Definition: PixelBuffer.h:236
PixelBuffer(value_type *pixeldata, const range_type &range,::ome::xml::model::enums::PixelType pixeltype=::ome::xml::model::enums::PixelType::UINT8, EndianType endiantype=ENDIAN_NATIVE, const storage_order_type &storage=PixelBufferBase::default_storage_order())
Construct from ranges (external storage).
Definition: PixelBuffer.h:348
DimensionOrder enumeration.
Definition: DimensionOrder.h:69
Logical subdivision of the temporal t dimension (t).
Definition: PixelBuffer.h:88
Variant type limit workaround.
Open Microscopy Environment C++ implementation.
Definition: CoreMetadata.cpp:40
Logical channel (typically detectors of specific wavelengths) (C).
Definition: PixelBuffer.h:85
const ::ome::xml::model::enums::PixelType pixeltype
Pixel type stored in this buffer.
Definition: PixelBuffer.h:196
value_type * data()
Get the raw data.
Definition: PixelBuffer.h:402
const size_type * shape() const
Get the shape of the multi-dimensional array.
Definition: PixelBuffer.h:487
boost::multi_array_types::size_type size_type
Size type.
Definition: PixelBuffer.h:111
value_type & at(const indices_type &indices)
Get the pixel value at an index.
Definition: PixelBuffer.h:751
array_ref_type & array()
Get the pixel data.
Definition: PixelBuffer.h:875
PixelBuffer(value_type *pixeldata, const ExtentList &extents,::ome::xml::model::enums::PixelType pixeltype=::ome::xml::model::enums::PixelType::UINT8, EndianType endiantype=ENDIAN_NATIVE, const storage_order_type &storage=PixelBufferBase::default_storage_order())
Construct from extents (external storage).
Definition: PixelBuffer.h:304
PixelBuffer< T >::array_ref_type & operator()(U &v) const
PixelBuffer of any type.
Definition: PixelBuffer.h:843
Spatial x dimension (X).
Definition: PixelBuffer.h:81
const storage_order_type & storage_order() const
Get the array storage order.
Definition: PixelBuffer.h:541
Logical subdivision of the logical channel dimension (c).
Definition: PixelBuffer.h:89
void read(std::basic_istream< charT, traits > &stream)
Read raw pixel data from a stream in physical storage order.
Definition: PixelBuffer.h:784
PixelType enumeration.
Definition: PixelType.h:69
EndianType endianType() const
Get the endian type in use.
Definition: PixelBuffer.h:162
T value_type
Pixel value type.
Definition: PixelBuffer.h:240
boost::variant< ome::compat::shared_ptr< array_type >, ome::compat::shared_ptr< array_ref_type > > multiarray
Multi-dimensional pixel array.
Definition: PixelBuffer.h:825
Standard integer types.
Base class for all PixelBuffer types.
Definition: PixelBuffer.h:104
static const uint16_t dimensions
Total number of supported dimensions.
Definition: PixelBuffer.h:108
boost::multi_array< value_type, dimensions > array_type
Type for multi-dimensional pixel array view.
Definition: PixelBuffer.h:253
bool operator<=(const PixelBuffer &rhs) const
Less than or equal comparison with a pixel buffer.
Definition: PixelBuffer.h:659
PixelBufferBase(::ome::xml::model::enums::PixelType pixeltype, EndianType endiantype)
Constructor.
Definition: PixelBuffer.h:134
Find a PixelBuffer data array of a specific pixel type.
Definition: PixelBuffer.h:833
const value_type * origin() const
Get the origin of the array.
Definition: PixelBuffer.h:530
Logical subdivision of the spatial z dimension (z).
Definition: PixelBuffer.h:87
PixelBuffer(const PixelBuffer &buffer)
Copy constructor.
Definition: PixelBuffer.h:366
boost::multi_array_ref< value_type, dimensions > array_ref_type
Type for multi-dimensional pixel array view referencing external data.
Definition: PixelBuffer.h:247
Spatial z dimension (Z).
Definition: PixelBuffer.h:83
const EndianType endiantype
Endian type stored in this buffer.
Definition: PixelBuffer.h:199
const boost::multi_array_types::index * index_bases() const
Get the index bases of the multi-dimensional array.
Definition: PixelBuffer.h:514
void assign(InputIterator begin, InputIterator end)
Assign pixel values.
Definition: PixelBuffer.h:734
boost::general_storage_order< dimensions > storage_order_type
Storage ordering type for controlling pixel memory layout.
Definition: PixelBuffer.h:121
boost::detail::multi_array::extent_gen< dimensions > range_type
Extent range type.
Definition: PixelBuffer.h:124
bool operator>(const PixelBuffer &rhs) const
Greater than comparison with a pixel buffer.
Definition: PixelBuffer.h:683
size_type num_elements() const
Get the number of pixel elements in the multi-dimensional array.
Definition: PixelBuffer.h:465
Xerces-C modern C++ wrapper.
Definition: Base.h:53
const value_type & at(const indices_type &indices) const
Get the pixel value at an index.
Definition: PixelBuffer.h:767