ImfFlatImageChannel.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. //
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. // Copyright (c) Contributors to the OpenEXR Project.
  4. //
  5. #ifndef INCLUDED_IMF_FLAT_IMAGE_CHANNEL_H
  6. #define INCLUDED_IMF_FLAT_IMAGE_CHANNEL_H
  7. //----------------------------------------------------------------------------
  8. //
  9. // class FlatImageChannel,
  10. // template class TypedFlatImageChannel<T>
  11. //
  12. // For an explanation of images, levels and channels,
  13. // see the comments in header file Image.h.
  14. //
  15. //----------------------------------------------------------------------------
  16. #include "ImfImageChannel.h"
  17. #include "ImfUtilExport.h"
  18. #include "ImfImageLevel.h"
  19. #include "ImfPixelType.h"
  20. #include "ImfFrameBuffer.h"
  21. #include <ImathBox.h>
  22. #include <half.h>
  23. OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER
  24. class FlatImageLevel;
  25. //
  26. // Image channels:
  27. //
  28. // A TypedFlatImageChannel<T> holds the pixel data for a single channel
  29. // of one level of a flat image. The pixels in the channel are of type T,
  30. // where T is either half, float or unsigned int. Storage is allocated
  31. // only for pixels within the data window of the level.
  32. //
  33. class IMFUTIL_EXPORT_TYPE FlatImageChannel: public ImageChannel
  34. {
  35. public:
  36. //
  37. // Construct an OpenEXR frame buffer slice for this channel.
  38. // This function is needed reading an image from an OpenEXR
  39. // file and for saving an image in an OpenEXR file.
  40. //
  41. virtual Slice slice () const = 0;
  42. //
  43. // Access to the flat image level to which this channel belongs.
  44. //
  45. IMFUTIL_EXPORT FlatImageLevel & flatLevel ();
  46. IMFUTIL_EXPORT const FlatImageLevel & flatLevel () const;
  47. protected:
  48. friend class FlatImageLevel;
  49. IMFUTIL_EXPORT
  50. FlatImageChannel (FlatImageLevel &level,
  51. int xSampling,
  52. int ySampling,
  53. bool pLinear);
  54. IMFUTIL_EXPORT virtual ~FlatImageChannel();
  55. FlatImageChannel (const FlatImageChannel& other) = delete;
  56. FlatImageChannel& operator = (const FlatImageChannel& other) = delete;
  57. FlatImageChannel (FlatImageChannel&& other) = delete;
  58. FlatImageChannel& operator = (FlatImageChannel&& other) = delete;
  59. IMFUTIL_EXPORT
  60. virtual void resize ();
  61. virtual void resetBasePointer () = 0;
  62. };
  63. template <class T>
  64. class IMFUTIL_EXPORT_TEMPLATE_TYPE TypedFlatImageChannel: public FlatImageChannel
  65. {
  66. public:
  67. //
  68. // The OpenEXR pixel type of this channel (HALF, FLOAT or UINT).
  69. //
  70. virtual PixelType pixelType () const;
  71. //
  72. // Construct an OpenEXR frame buffer slice for this channel.
  73. //
  74. virtual Slice slice () const;
  75. //
  76. // Access to the pixel at pixel space location (x, y), without
  77. // bounds checking. Accessing a location outside the data window
  78. // of the image level results in undefined behavior.
  79. //
  80. T & operator () (int x, int y);
  81. const T & operator () (int x, int y) const;
  82. //
  83. // Access to the pixel at pixel space location (x, y), with bounds
  84. // checking. Accessing a location outside the data window of the
  85. // image level throws an Iex::ArgExc exception.
  86. //
  87. T & at (int x, int y);
  88. const T & at (int x, int y) const;
  89. //
  90. // Faster access to all pixels in a single horizontal row of the
  91. // channel. Rows are numbered from 0 to pixelsPerColumn()-1, and
  92. // each row contains pixelsPerRow() values.
  93. // Access is not bounds checked; accessing out of bounds rows or
  94. // pixels results in undefined behavior.
  95. //
  96. T * row (int r);
  97. const T * row (int r) const;
  98. private:
  99. friend class FlatImageLevel;
  100. //
  101. // The constructor and destructor are not public because flat
  102. // image channels exist only as parts of a flat image level.
  103. //
  104. IMFUTIL_HIDDEN
  105. TypedFlatImageChannel (FlatImageLevel &level,
  106. int xSampling,
  107. int ySampling,
  108. bool pLinear);
  109. IMFUTIL_HIDDEN
  110. virtual ~TypedFlatImageChannel ();
  111. TypedFlatImageChannel (const TypedFlatImageChannel& other) = delete;
  112. TypedFlatImageChannel& operator = (const TypedFlatImageChannel& other) = delete;
  113. TypedFlatImageChannel (TypedFlatImageChannel&& other) = delete;
  114. TypedFlatImageChannel& operator = (TypedFlatImageChannel&& other) = delete;
  115. IMFUTIL_HIDDEN
  116. virtual void resize ();
  117. IMFUTIL_HIDDEN
  118. virtual void resetBasePointer ();
  119. T * _pixels; // Pointer to allocated storage
  120. T * _base; // Base pointer for faster pixel access
  121. };
  122. //
  123. // Channel typedefs for the pixel data types supported by OpenEXR.
  124. //
  125. typedef TypedFlatImageChannel<half> FlatHalfChannel;
  126. typedef TypedFlatImageChannel<float> FlatFloatChannel;
  127. typedef TypedFlatImageChannel<unsigned int> FlatUIntChannel;
  128. //-----------------------------------------------------------------------------
  129. // Implementation of templates and inline functions
  130. //-----------------------------------------------------------------------------
  131. template <class T>
  132. inline T &
  133. TypedFlatImageChannel<T>::operator () (int x, int y)
  134. {
  135. return _base[(y / ySampling()) * pixelsPerRow() + (x / xSampling())];
  136. }
  137. template <class T>
  138. inline const T &
  139. TypedFlatImageChannel<T>::operator () (int x, int y) const
  140. {
  141. return _base[(y / ySampling()) * pixelsPerRow() + (x / xSampling())];
  142. }
  143. template <class T>
  144. inline T &
  145. TypedFlatImageChannel<T>::at (int x, int y)
  146. {
  147. boundsCheck (x, y);
  148. return _base[(y / ySampling()) * pixelsPerRow() + (x / xSampling())];
  149. }
  150. template <class T>
  151. inline const T &
  152. TypedFlatImageChannel<T>::at (int x, int y) const
  153. {
  154. boundsCheck (x, y);
  155. return _base[(y / ySampling()) * pixelsPerRow() + (x / xSampling())];
  156. }
  157. template <class T>
  158. inline T *
  159. TypedFlatImageChannel<T>::row (int r)
  160. {
  161. return _base + r * pixelsPerRow();
  162. }
  163. template <class T>
  164. inline const T *
  165. TypedFlatImageChannel<T>::row (int n) const
  166. {
  167. return _base + n * pixelsPerRow();
  168. }
  169. #ifndef COMPILING_IMF_FLAT_IMAGE_CHANNEL
  170. extern template class IMFUTIL_EXPORT_EXTERN_TEMPLATE TypedFlatImageChannel<half>;
  171. extern template class IMFUTIL_EXPORT_EXTERN_TEMPLATE TypedFlatImageChannel<float>;
  172. extern template class IMFUTIL_EXPORT_EXTERN_TEMPLATE TypedFlatImageChannel<unsigned int>;
  173. #endif
  174. OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_EXIT
  175. #endif