bioformats  5.1.3
PixelBuffer.h
1 /*
2  * #%L
3  * OME-BIOFORMATS C++ library for image IO.
4  * Copyright © 2006 - 2015 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 #include <boost/multi_array.hpp>
48 
49 #include <ome/bioformats/PixelProperties.h>
50 
51 #include <ome/common/variant.h>
52 
53 #include <ome/compat/array.h>
54 #include <ome/compat/cstdint.h>
55 #include <ome/compat/memory.h>
56 
57 #include <ome/xml/model/enums/DimensionOrder.h>
58 
59 namespace ome
60 {
61  namespace bioformats
62  {
63 
78  {
88  };
89 
103  {
104  public:
106  static const uint16_t dimensions = 9;
107 
109  typedef boost::multi_array_types::size_type size_type;
110 
112  typedef boost::multi_array_types::index index;
113 
115  typedef ome::compat::array<boost::multi_array_types::index,
117 
119  typedef boost::general_storage_order<dimensions> storage_order_type;
120 
122  typedef boost::detail::multi_array::extent_gen<dimensions> range_type;
123 
124  protected:
134  pixeltype(pixeltype),
135  endiantype(endiantype)
136  {}
137 
138  public:
141  {}
142 
149  pixelType() const
150  {
151  return pixeltype;
152  }
153 
159  EndianType
160  endianType() const
161  {
162  return endiantype;
163  }
164 
177  static storage_order_type
179  bool interleaved);
180 
189  static storage_order_type
191 
192  private:
194  const ::ome::xml::model::enums::PixelType pixeltype;
195 
198  };
199 
233  template<typename T>
235  {
236  public:
238  typedef T value_type;
239 
245  typedef boost::multi_array_ref<value_type, dimensions> array_ref_type;
246 
251  typedef boost::multi_array<value_type, dimensions> array_type;
252 
259  explicit PixelBuffer():
260  PixelBufferBase(::ome::xml::model::enums::PixelType::UINT8, ENDIAN_NATIVE),
261  multiarray(ome::compat::shared_ptr<array_type>(new array_type(boost::extents[1][1][1][1][1][1][1][1][1],
263  {}
264 
276  template<class ExtentList>
277  explicit
278  PixelBuffer(const ExtentList& extents,
283  multiarray(ome::compat::shared_ptr<array_type>(new array_type(extents, storage)))
284  {}
285 
300  template<class ExtentList>
301  explicit
302  PixelBuffer(value_type *pixeldata,
303  const ExtentList& extents,
308  multiarray(ome::compat::shared_ptr<array_ref_type>(new array_ref_type(pixeldata, extents, storage)))
309  {}
310 
322  explicit
323  PixelBuffer(const range_type& range,
328  multiarray(ome::compat::shared_ptr<array_type>(new array_type(range, storage)))
329  {}
330 
345  explicit
346  PixelBuffer(value_type *pixeldata,
347  const range_type& range,
352  multiarray(ome::compat::shared_ptr<array_ref_type>(new array_ref_type(pixeldata, range, storage)))
353  {}
354 
363  explicit
364  PixelBuffer(const PixelBuffer& buffer):
365  PixelBufferBase(buffer),
366  multiarray(buffer.multiarray)
367  {}
368 
370  virtual ~PixelBuffer()
371  {}
372 
379  array_ref_type&
380  array();
381 
388  const array_ref_type&
389  array() const;
390 
399  value_type *
401  {
402  return array().data();
403  }
404 
413  const value_type *
414  data() const
415  {
416  return array().data();
417  }
418 
429  bool
430  valid() const
431  {
432  bool is_valid = true;
433 
434  try
435  {
436  array();
437  }
438  catch (const std::runtime_error& e)
439  {
440  is_valid = false;
441  }
442 
443  return is_valid;
444  }
445 
453  bool
454  managed() const
455  {
456  return (boost::get<ome::compat::shared_ptr<array_type> >(&multiarray) != 0);
457  }
458 
462  size_type
463  num_elements() const
464  {
465  return array().num_elements();
466  }
467 
471  size_type
473  {
474  return array().num_dimensions();
475  }
476 
484  const size_type *
485  shape() const
486  {
487  return array().shape();
488  }
489 
497  const boost::multi_array_types::index *
498  strides() const
499  {
500  return array().strides();
501  }
502 
511  const boost::multi_array_types::index *
512  index_bases() const
513  {
514  return array().index_bases();
515  }
516 
527  const value_type *
528  origin() const
529  {
530  return array().origin();
531  }
532 
538  const storage_order_type&
540  {
541  return array().storage_order();
542  }
543 
554  PixelBuffer&
556  {
557  array() = rhs.array();
558  return *this;
559  }
560 
571  PixelBuffer&
572  operator = (const array_ref_type& rhs)
573  {
574  array() = rhs;
575  return *this;
576  }
577 
584  bool
585  operator == (const PixelBuffer& rhs) const
586  {
587  return array() == rhs.array();
588  }
589 
596  bool
597  operator == (const array_ref_type& rhs) const
598  {
599  return array() == rhs;
600  }
601 
608  bool
609  operator != (const PixelBuffer& rhs) const
610  {
611  return array() != rhs.array();
612  }
613 
620  bool
621  operator != (const array_ref_type& rhs) const
622  {
623  return array() != rhs;
624  }
625 
632  bool
633  operator < (const PixelBuffer& rhs) const
634  {
635  return array() < rhs.array();
636  }
637 
644  bool
645  operator < (const array_ref_type& rhs) const
646  {
647  return array() < rhs;
648  }
649 
656  bool
657  operator <= (const PixelBuffer& rhs) const
658  {
659  return array() <= rhs.array();
660  }
661 
668  bool
669  operator <= (const array_ref_type& rhs) const
670  {
671  return array() <= rhs;
672  }
673 
680  bool
681  operator > (const PixelBuffer& rhs) const
682  {
683  return array() > rhs.array();
684  }
685 
692  bool
693  operator > (const array_ref_type& rhs) const
694  {
695  return array() > rhs;
696  }
697 
704  bool
705  operator >= (const PixelBuffer& rhs) const
706  {
707  return array() >= rhs.array();
708  }
709 
716  bool
717  operator >= (const array_ref_type& rhs) const
718  {
719  return array() >= rhs;
720  }
721 
730  template <typename InputIterator>
731  void
732  assign(InputIterator begin,
733  InputIterator end)
734  {
735  array().assign(begin, end);
736  }
737 
748  value_type&
749  at(const indices_type& indices)
750  {
751  return array()(indices);
752  }
753 
764  const value_type&
765  at(const indices_type& indices) const
766  {
767  return array()(indices);
768  }
769 
780  template<class charT, class traits>
781  inline void
782  read(std::basic_istream<charT,traits>& stream)
783  {
784  stream.read(reinterpret_cast<char *>(data()),
785  static_cast<std::streamsize>(num_elements() * sizeof(value_type)));
786  }
787 
798  template<class charT, class traits>
799  inline void
800  write(std::basic_ostream<charT,traits>& stream) const
801  {
802  stream.write(reinterpret_cast<const char *>(data()),
803  static_cast<std::streamsize>(num_elements() * sizeof(value_type)));
804  }
805 
806  private:
822  boost::variant<ome::compat::shared_ptr<array_type>,
823  ome::compat::shared_ptr<array_ref_type> > multiarray;
824  };
825 
826  namespace detail
827  {
828 
830  template<typename T>
831  struct PixelBufferArrayVisitor : public boost::static_visitor<typename PixelBuffer<T>::array_ref_type&>
832  {
839  template <typename U>
841  operator() (U& v) const
842  {
843  if (!v)
844  throw std::runtime_error("Null array type");
845  return *v;
846  }
847  };
848 
850  template<typename T>
851  struct PixelBufferConstArrayVisitor : public boost::static_visitor<const typename PixelBuffer<T>::array_ref_type&>
852  {
859  template <typename U>
860  const typename PixelBuffer<T>::array_ref_type&
861  operator() (U& v) const
862  {
863  if (!v)
864  throw std::runtime_error("Null array type");
865  return *v;
866  }
867  };
868 
869  }
870 
871  template<typename T>
872  typename PixelBuffer<T>::array_ref_type&
874  {
876  return boost::apply_visitor(v, multiarray);
877  }
878 
879  template<typename T>
880  const typename PixelBuffer<T>::array_ref_type&
882  {
884  return boost::apply_visitor(v, multiarray);
885  }
886 
887  }
888 }
889 
890 namespace std
891 {
892 
900  template<typename T, class charT, class traits>
901  inline std::basic_istream<charT,traits>&
902  operator>> (std::basic_istream<charT,traits>& is,
904  {
905  buf.read(is);
906  return is;
907  }
908 
916  template<typename T, class charT, class traits>
917  inline std::basic_ostream<charT,traits>&
918  operator<< (std::basic_ostream<charT,traits>& os,
919  const ::ome::bioformats::PixelBuffer<T>& buf)
920  {
921  buf.write(os);
922  return os;
923  }
924 
925 }
926 
927 #endif // OME_BIOFORMATS_PIXELBUFFER_H
928 
929 /*
930  * Local Variables:
931  * mode:C++
932  * End:
933  */
uint8
Definition: PixelType.h:83
PixelBuffer & operator=(const PixelBuffer &rhs)
Assign a pixel buffer.
Definition: PixelBuffer.h:555
EndianType
Endianness.
Definition: Types.h:68
boost::multi_array_types::index index
Index type.
Definition: PixelBuffer.h:112
Memory type substitution.
virtual ~PixelBuffer()
Destructor.
Definition: PixelBuffer.h:370
Definition: length.h:576
bool operator!=(const PixelBuffer &rhs) const
Compare a pixel buffer for inequality.
Definition: PixelBuffer.h:609
bool operator>=(const PixelBuffer &rhs) const
Greater than or equal comparison with a pixel buffer.
Definition: PixelBuffer.h:705
bool operator<(const PixelBuffer &rhs) const
Less than comparison with a pixel buffer.
Definition: PixelBuffer.h:633
Native endian.
Definition: Types.h:72
bool operator==(const PixelBuffer &rhs) const
Compare a pixel buffer for equality.
Definition: PixelBuffer.h:585
virtual ~PixelBufferBase()
Destructor.
Definition: PixelBuffer.h:140
bool valid() const
Check the buffer validity.
Definition: PixelBuffer.h:430
::ome::xml::model::enums::PixelType pixelType() const
Get the pixel type in use.
Definition: PixelBuffer.h:149
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:498
size_type num_dimensions() const
Get the number of dimensions in the multi-dimensional array.
Definition: PixelBuffer.h:472
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:82
const value_type * data() const
Get the raw data.
Definition: PixelBuffer.h:414
ome::compat::array< boost::multi_array_types::index, PixelBufferBase::dimensions > indices_type
Type used to index all dimensions in public interfaces.
Definition: PixelBuffer.h:116
const PixelBuffer< T >::array_ref_type & operator()(U &v) const
PixelBuffer of any type.
Definition: PixelBuffer.h:861
Dimensions
Dimensions.
Definition: PixelBuffer.h:77
void write(std::basic_ostream< charT, traits > &stream) const
Write raw pixel data to a stream in physical storage order.
Definition: PixelBuffer.h:800
PixelBuffer()
Default constructor.
Definition: PixelBuffer.h:259
Logical sub-channel (typically used for RGB channel sub-components) (S).
Definition: PixelBuffer.h:84
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:323
Spatial y dimension (Y).
Definition: PixelBuffer.h:80
Find a PixelBuffer data array of a specific pixel type.
Definition: PixelBuffer.h:851
Array type substitution.
bool managed() const
Check if the buffer is internally managed.
Definition: PixelBuffer.h:454
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:278
Buffer for a specific pixel type.
Definition: PixelBuffer.h:234
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:346
DimensionOrder enumeration.
Definition: DimensionOrder.h:70
Logical subdivision of the temporal t dimension (t).
Definition: PixelBuffer.h:86
Variant type limit workaround.
Open Microscopy Environment C++ implementation.
Definition: CoreMetadata.cpp:40
Logical channel (typically detectors of specific wavelengths) (C).
Definition: PixelBuffer.h:83
const ::ome::xml::model::enums::PixelType pixeltype
Pixel type stored in this buffer.
Definition: PixelBuffer.h:194
value_type * data()
Get the raw data.
Definition: PixelBuffer.h:400
const size_type * shape() const
Get the shape of the multi-dimensional array.
Definition: PixelBuffer.h:485
boost::multi_array_types::size_type size_type
Size type.
Definition: PixelBuffer.h:109
value_type & at(const indices_type &indices)
Get the pixel value at an index.
Definition: PixelBuffer.h:749
array_ref_type & array()
Get the pixel data.
Definition: PixelBuffer.h:873
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:302
PixelBuffer< T >::array_ref_type & operator()(U &v) const
PixelBuffer of any type.
Definition: PixelBuffer.h:841
Spatial x dimension (X).
Definition: PixelBuffer.h:79
const storage_order_type & storage_order() const
Get the array storage order.
Definition: PixelBuffer.h:539
Logical subdivision of the logical channel dimension (c).
Definition: PixelBuffer.h:87
void read(std::basic_istream< charT, traits > &stream)
Read raw pixel data from a stream in physical storage order.
Definition: PixelBuffer.h:782
PixelType enumeration.
Definition: PixelType.h:70
EndianType endianType() const
Get the endian type in use.
Definition: PixelBuffer.h:160
T value_type
Pixel value type.
Definition: PixelBuffer.h:238
boost::variant< ome::compat::shared_ptr< array_type >, ome::compat::shared_ptr< array_ref_type > > multiarray
Multi-dimensional pixel array.
Definition: PixelBuffer.h:823
Standard integer types.
Base class for all PixelBuffer types.
Definition: PixelBuffer.h:102
static const uint16_t dimensions
Total number of supported dimensions.
Definition: PixelBuffer.h:106
boost::multi_array< value_type, dimensions > array_type
Type for multi-dimensional pixel array view.
Definition: PixelBuffer.h:251
bool operator<=(const PixelBuffer &rhs) const
Less than or equal comparison with a pixel buffer.
Definition: PixelBuffer.h:657
PixelBufferBase(::ome::xml::model::enums::PixelType pixeltype, EndianType endiantype)
Constructor.
Definition: PixelBuffer.h:132
Find a PixelBuffer data array of a specific pixel type.
Definition: PixelBuffer.h:831
const value_type * origin() const
Get the origin of the array.
Definition: PixelBuffer.h:528
Logical subdivision of the spatial z dimension (z).
Definition: PixelBuffer.h:85
PixelBuffer(const PixelBuffer &buffer)
Copy constructor.
Definition: PixelBuffer.h:364
boost::multi_array_ref< value_type, dimensions > array_ref_type
Type for multi-dimensional pixel array view referencing external data.
Definition: PixelBuffer.h:245
Spatial z dimension (Z).
Definition: PixelBuffer.h:81
const EndianType endiantype
Endian type stored in this buffer.
Definition: PixelBuffer.h:197
const boost::multi_array_types::index * index_bases() const
Get the index bases of the multi-dimensional array.
Definition: PixelBuffer.h:512
void assign(InputIterator begin, InputIterator end)
Assign pixel values.
Definition: PixelBuffer.h:732
boost::general_storage_order< dimensions > storage_order_type
Storage ordering type for controlling pixel memory layout.
Definition: PixelBuffer.h:119
boost::detail::multi_array::extent_gen< dimensions > range_type
Extent range type.
Definition: PixelBuffer.h:122
bool operator>(const PixelBuffer &rhs) const
Greater than comparison with a pixel buffer.
Definition: PixelBuffer.h:681
size_type num_elements() const
Get the number of pixel elements in the multi-dimensional array.
Definition: PixelBuffer.h:463
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:765