ome-files 0.2.2

PixelBuffer.h
1 /*
2 * #%L
3 * OME-FILES 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_FILES_PIXELBUFFER_H
39 #define OME_FILES_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/files/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 files
64 {
65
79 enum Dimensions
80 {
81 DIM_SPATIAL_X = 0,
82 DIM_SPATIAL_Y = 1,
83 DIM_SPATIAL_Z = 2,
84 DIM_TEMPORAL_T = 3,
85 DIM_CHANNEL = 4,
86 DIM_SUBCHANNEL = 5,
87 DIM_MODULO_Z = 6,
88 DIM_MODULO_T = 7,
89 DIM_MODULO_C = 8
90 };
91
104 class PixelBufferBase
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,
118 PixelBufferBase::dimensions> indices_type;
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:
134 PixelBufferBase(::ome::xml::model::enums::PixelType pixeltype,
135 EndianType endiantype):
136 pixeltype(pixeltype),
137 endiantype(endiantype)
138 {}
139
140 public:
142 virtual ~PixelBufferBase()
143 {}
144
150 ::ome::xml::model::enums::PixelType
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
180 make_storage_order(ome::xml::model::enums::DimensionOrder order,
181 bool interleaved);
182
191 static storage_order_type
192 default_storage_order();
193
194 private:
196 const ::ome::xml::model::enums::PixelType pixeltype;
197
199 const EndianType endiantype;
200 };
201
235 template<typename T>
236 class PixelBuffer : public PixelBufferBase
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],
264 PixelBufferBase::default_storage_order())))
265 {}
266
278 template<class ExtentList>
279 explicit
280 PixelBuffer(const ExtentList& extents,
281 ::ome::xml::model::enums::PixelType pixeltype = ::ome::xml::model::enums::PixelType::UINT8,
282 EndianType endiantype = ENDIAN_NATIVE,
283 const storage_order_type& storage = PixelBufferBase::default_storage_order()):
284 PixelBufferBase(pixeltype, endiantype),
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,
306 ::ome::xml::model::enums::PixelType pixeltype = ::ome::xml::model::enums::PixelType::UINT8,
307 EndianType endiantype = ENDIAN_NATIVE,
308 const storage_order_type& storage = PixelBufferBase::default_storage_order()):
309 PixelBufferBase(pixeltype, endiantype),
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,
326 ::ome::xml::model::enums::PixelType pixeltype = ::ome::xml::model::enums::PixelType::UINT8,
327 EndianType endiantype = ENDIAN_NATIVE,
328 const storage_order_type& storage = PixelBufferBase::default_storage_order()):
329 PixelBufferBase(pixeltype, endiantype),
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,
350 ::ome::xml::model::enums::PixelType pixeltype = ::ome::xml::model::enums::PixelType::UINT8,
351 EndianType endiantype = ENDIAN_NATIVE,
352 const storage_order_type& storage = PixelBufferBase::default_storage_order()):
353 PixelBufferBase(pixeltype, endiantype),
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 *
402 data()
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
474 num_dimensions() const
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&
541 storage_order() const
542 {
543 return array().storage_order();
544 }
545
556 PixelBuffer&
557 operator = (const PixelBuffer& rhs)
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>
842 typename PixelBuffer<T>::array_ref_type&
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&
875 PixelBuffer<T>::array()
876 {
877 detail::PixelBufferArrayVisitor<T> v;
878 return boost::apply_visitor(v, multiarray);
879 }
880
881 template<typename T>
882 const typename PixelBuffer<T>::array_ref_type&
883 PixelBuffer<T>::array() const
884 {
885 detail::PixelBufferConstArrayVisitor<T> v;
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,
905 ::ome::files::PixelBuffer<T>& buf)
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::files::PixelBuffer<T>& buf)
922 {
923 buf.write(os);
924 return os;
925 }
926
927 }
928
929 #endif // OME_FILES_PIXELBUFFER_H
930
931 /*
932 * Local Variables:
933 * mode:C++
934 * End:
935 */
variant.h
ome::files::PixelBuffer::origin
const value_type * origin() const
Get the origin of the array.
Definition: PixelBuffer.h:530
ome::xml::model::enums::PixelType::UINT8
UINT8
ome::files::PixelBuffer::array_ref_type
boost::multi_array_ref< value_type, dimensions > array_ref_type
Type for multi-dimensional pixel array view referencing external data.
Definition: PixelBuffer.h:247
ome::files::PixelBuffer::multiarray
boost::variant< ome::compat::shared_ptr< array_type >, ome::compat::shared_ptr< array_ref_type > > multiarray
Multi-dimensional pixel array.
Definition: PixelBuffer.h:825
ome::files::PixelBuffer::num_dimensions
size_type num_dimensions() const
Get the number of dimensions in the multi-dimensional array.
Definition: PixelBuffer.h:474
ome::files::PixelBuffer
Buffer for a specific pixel type.
Definition: PixelBuffer.h:236
ome::files::PixelBuffer::PixelBuffer
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
ome::files::PixelBuffer::assign
void assign(InputIterator begin, InputIterator end)
Assign pixel values.
Definition: PixelBuffer.h:734
ome::files::PixelBuffer::~PixelBuffer
virtual ~PixelBuffer()
Destructor.
Definition: PixelBuffer.h:372
ome::files::DIM_SPATIAL_Y
Spatial y dimension (Y).
Definition: PixelBuffer.h:82
ome::files::PixelBuffer::data
value_type * data()
Get the raw data.
Definition: PixelBuffer.h:402
std
STL namespace.
ome::files::PixelBufferBase::index
boost::multi_array_types::index index
Index type.
Definition: PixelBuffer.h:114
ome::files::PixelBufferBase
Base class for all PixelBuffer types.
Definition: PixelBuffer.h:104
ome::files::DIM_MODULO_T
Logical subdivision of the temporal t dimension (t).
Definition: PixelBuffer.h:88
ome::files::PixelBuffer::at
value_type & at(const indices_type &indices)
Get the pixel value at an index.
Definition: PixelBuffer.h:751
ome::files::PixelBufferBase::PixelBufferBase
PixelBufferBase(::ome::xml::model::enums::PixelType pixeltype, EndianType endiantype)
Constructor.
Definition: PixelBuffer.h:134
ome::files::PixelBuffer::index_bases
const boost::multi_array_types::index * index_bases() const
Get the index bases of the multi-dimensional array.
Definition: PixelBuffer.h:514
ome::files::DIM_MODULO_C
Logical subdivision of the logical channel dimension (c).
Definition: PixelBuffer.h:89
ome::files::PixelBufferBase::indices_type
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
ome::xml::model::enums::DimensionOrder
ome::files::DIM_SPATIAL_X
Spatial x dimension (X).
Definition: PixelBuffer.h:81
ome::files::PixelBufferBase::pixelType
::ome::xml::model::enums::PixelType pixelType() const
Get the pixel type in use.
Definition: PixelBuffer.h:151
ome
Open Microscopy Environment C++.
ome::files::PixelBufferBase::storage_order_type
boost::general_storage_order< dimensions > storage_order_type
Storage ordering type for controlling pixel memory layout.
Definition: PixelBuffer.h:121
ome::files::ENDIAN_NATIVE
Native endian.
Definition: Types.h:72
ome::files::detail::PixelBufferArrayVisitor
Find a PixelBuffer data array of a specific pixel type.
Definition: PixelBuffer.h:833
ome::files::PixelBufferBase::make_storage_order
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
ome::files::PixelBuffer::at
const value_type & at(const indices_type &indices) const
Get the pixel value at an index.
Definition: PixelBuffer.h:767
ome::files::PixelBuffer::PixelBuffer
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
boost
ome::files::PixelBufferBase::~PixelBufferBase
virtual ~PixelBufferBase()
Destructor.
Definition: PixelBuffer.h:142
ome::files::EndianType
EndianType
Endianness.
Definition: Types.h:68
memory.h
ome::files::PixelBuffer::strides
const boost::multi_array_types::index * strides() const
Get the strides of the multi-dimensional array.
Definition: PixelBuffer.h:500
ome::files::PixelBuffer::shape
const size_type * shape() const
Get the shape of the multi-dimensional array.
Definition: PixelBuffer.h:487
ome::files::PixelBuffer::write
void write(std::basic_ostream< charT, traits > &stream) const
Write raw pixel data to a stream in physical storage order.
Definition: PixelBuffer.h:802
ome::files::PixelBuffer::managed
bool managed() const
Check if the buffer is internally managed.
Definition: PixelBuffer.h:456
ome::files::PixelBufferBase::endianType
EndianType endianType() const
Get the endian type in use.
Definition: PixelBuffer.h:162
ome::files::PixelBuffer::num_elements
size_type num_elements() const
Get the number of pixel elements in the multi-dimensional array.
Definition: PixelBuffer.h:465
ome::files::PixelBuffer::read
void read(std::basic_istream< charT, traits > &stream)
Read raw pixel data from a stream in physical storage order.
Definition: PixelBuffer.h:784
cstdint.h
ome::files::DIM_TEMPORAL_T
Temporal t dimension (T).
Definition: PixelBuffer.h:84
ome::files::Dimensions
Dimensions
Dimensions.
Definition: PixelBuffer.h:79
ome::xml::model::enums::PixelType
ome::files::detail::PixelBufferConstArrayVisitor
Find a PixelBuffer data array of a specific pixel type.
Definition: PixelBuffer.h:853
ome::files::PixelBuffer::PixelBuffer
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
ome::files::PixelBufferBase::range_type
boost::detail::multi_array::extent_gen< dimensions > range_type
Extent range type.
Definition: PixelBuffer.h:124
ome::files::PixelBufferBase::size_type
boost::multi_array_types::size_type size_type
Size type.
Definition: PixelBuffer.h:111
ome::files::PixelBuffer::PixelBuffer
PixelBuffer(const PixelBuffer &buffer)
Copy constructor.
Definition: PixelBuffer.h:366
ome::files::PixelBuffer::array
array_ref_type & array()
Get the pixel data.
Definition: PixelBuffer.h:875
ome::files::PixelBufferBase::pixeltype
const ::ome::xml::model::enums::PixelType pixeltype
Pixel type stored in this buffer.
Definition: PixelBuffer.h:196
ome::files::PixelBufferBase::dimensions
static const uint16_t dimensions
Total number of supported dimensions.
Definition: PixelBuffer.h:108
ome::files::PixelBuffer::PixelBuffer
PixelBuffer()
Default constructor.
Definition: PixelBuffer.h:261
ome::files::DIM_SPATIAL_Z
Spatial z dimension (Z).
Definition: PixelBuffer.h:83
ome::files::PixelBuffer::array_type
boost::multi_array< value_type, dimensions > array_type
Type for multi-dimensional pixel array view.
Definition: PixelBuffer.h:253
ome::files::PixelBufferBase::default_storage_order
static storage_order_type default_storage_order()
Generate default storage ordering.
Definition: PixelBuffer.cpp:133
ome::files::PixelBufferBase::endiantype
const EndianType endiantype
Endian type stored in this buffer.
Definition: PixelBuffer.h:199
ome::files::DIM_MODULO_Z
Logical subdivision of the spatial z dimension (z).
Definition: PixelBuffer.h:87
array.h
ome::files::PixelBuffer::PixelBuffer
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
ome::files::DIM_CHANNEL
Logical channel (typically detectors of specific wavelengths) (C).
Definition: PixelBuffer.h:85
ome::files::PixelBuffer::data
const value_type * data() const
Get the raw data.
Definition: PixelBuffer.h:416
ome::files::PixelBuffer::storage_order
const storage_order_type & storage_order() const
Get the array storage order.
Definition: PixelBuffer.h:541
ome::files::PixelBuffer::value_type
T value_type
Pixel value type.
Definition: PixelBuffer.h:240
ome::common::xml
ome::files::PixelBuffer::valid
bool valid() const
Check the buffer validity.
Definition: PixelBuffer.h:432
ome::files::DIM_SUBCHANNEL
Logical sub-channel (typically used for RGB channel sub-components) (S).
Definition: PixelBuffer.h:86