HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ImfImage.h
Go to the documentation of this file.
1 //
2 // SPDX-License-Identifier: BSD-3-Clause
3 // Copyright (c) Contributors to the OpenEXR Project.
4 //
5 
6 #ifndef INCLUDED_IMF_IMAGE_H
7 #define INCLUDED_IMF_IMAGE_H
8 
9 //----------------------------------------------------------------------------
10 //
11 // class Image -- an in-memory data structure that can hold an arbitrary
12 // OpenEXR image, flat or deep, with one or multiple resolution levels,
13 // and with an arbitrary set of channels.
14 //
15 // An image is a container for a set of image levels, and an image level
16 // is a container for a set of image channels. An image channel contains
17 // an array of pixel values of type half, float or unsigned int.
18 //
19 // For example:
20 //
21 // image --+-- level 0 --+-- channel "R" --- pixel data
22 // | |
23 // | +-- channel "G" --- pixel data
24 // | |
25 // | +-- channel "B" --- pixel data
26 // |
27 // +-- level 1 --+-- channel "R" --- pixel data
28 // | |
29 // | +-- channel "G" --- pixel data
30 // | |
31 // | +-- channel "B" --- pixel data
32 // |
33 // +-- level 2 --+-- channel "R" --- pixel data
34 // |
35 // +-- channel "G" --- pixel data
36 // |
37 // +-- channel "B" --- pixel data
38 //
39 // An image has a level mode, which can be ONE_LEVEL, MIPMAP_LEVELS or
40 // RIPMAP_LEVELS, and a level rounding mode, which can be ROUND_UP or
41 // ROUND_DOWN. Together, the level mode and the level rounding mode
42 // determine how many levels an image contains, and how large the data
43 // window for each level is. All levels in an image have the same set
44 // of channels.
45 //
46 // An image channel has a name (e.g. "R", "Z", or "xVelocity"), a type
47 // (HALF, FLOAT or UINT) and x and y sampling rates. A channel stores
48 // samples for a pixel if the pixel is inside the data window of the
49 // level to which the channel belongs, and the x and y coordinates of
50 // the pixel are divisible by the x and y sampling rates of the channel.
51 //
52 // An image can be either flat or deep. In a flat image each channel
53 // in each level stores at most one value per pixel. In a deep image
54 // each channel in each level stores an arbitrary number of values per
55 // pixel. As an exception, each level of a deep image has a sample count
56 // channel with a single value per pixel; this value determines how many
57 // values each of the other channels in the same level has at the same
58 // pixel location.
59 //
60 // The classes Image, ImageLevel and ImageChannel are abstract base
61 // classes. Two sets of concrete classes, one for flat and one for
62 // deep images, are derived from the base classes.
63 //
64 //----------------------------------------------------------------------------
65 
66 #include "ImfUtilExport.h"
67 #include "ImfNamespace.h"
68 
69 #include "ImfImageLevel.h"
70 #include "ImfTileDescription.h"
71 #include "ImfArray.h"
72 
74 
75 struct Channel;
76 
77 
79 {
80  public:
81 
82  //
83  // Constructor and destructor
84  //
85 
87  IMFUTIL_EXPORT virtual ~Image ();
88 
89 
90  //
91  // Access to the image's level mode and level rounding mode.
92  //
93 
94  IMFUTIL_EXPORT LevelMode levelMode() const;
95  IMFUTIL_EXPORT LevelRoundingMode levelRoundingMode() const;
96 
97 
98  //
99  // Number of levels:
100  //
101  // numXLevels() returns the image's number of levels in the x direction.
102  //
103  // if levelMode() == ONE_LEVEL:
104  // return value is: 1
105  //
106  // if levelMode() == MIPMAP_LEVELS:
107  // return value is: rfunc (log (max (w, h)) / log (2)) + 1
108  //
109  // if levelMode() == RIPMAP_LEVELS:
110  // return value is: rfunc (log (w) / log (2)) + 1
111  //
112  // where
113  // w is the width of the image's data window, max.x - min.x + 1,
114  // h is the height of the image's data window, max.y - min.y + 1,
115  // and rfunc(x) is either floor(x), or ceil(x), depending on
116  // whether levelRoundingMode() returns ROUND_DOWN or ROUND_UP.
117  //
118  // numYLevels() returns the image's number of levels in the y direction.
119  //
120  // if levelMode() == ONE_LEVEL or levelMode() == MIPMAP_LEVELS:
121  // return value is the same as for numXLevels()
122  //
123  // if levelMode() == RIPMAP_LEVELS:
124  // return value is: rfunc (log (h) / log (2)) + 1
125  //
126  //
127  // numLevels() is a convenience function for use with MIPMAP_LEVELS images.
128  //
129  // if levelMode() == ONE_LEVEL or levelMode() == MIPMAP_LEVELS:
130  // return value is the same as for numXLevels()
131  //
132  // if levelMode() == RIPMAP_LEVELS:
133  // a LogicExc exception is thrown
134  //
135 
136  IMFUTIL_EXPORT int numLevels() const;
137  IMFUTIL_EXPORT int numXLevels() const;
138  IMFUTIL_EXPORT int numYLevels() const;
139 
140 
141  //
142  // Per-level data windows
143  //
144  // dataWindow() returns the data window for the image; this is the
145  // same as the data window for the level with level number (0, 0).
146  //
147  // dataWindowForLevel(lx, ly) returns the data window for level x,
148  // that is, the window for which the image level with level number
149  // (lx, ly) has allocated pixel storage.
150  //
151  // return value is a Box2i with min value:
152  // (dataWindow().min.x,
153  // dataWindow().min.y)
154  //
155  // and max value:
156  // (dataWindow().min.x + levelWidth(lx) - 1,
157  // dataWindow().min.y + levelHeight(ly) - 1)
158  //
159  // dataWindowForLevel(l) is a convenience function used for ONE_LEVEL
160  // and MIPMAP_LEVELS files. It returns dataWindowForLevel(l,l)).
161  //
163  const IMATH_NAMESPACE::Box2i & dataWindow() const;
165  const IMATH_NAMESPACE::Box2i & dataWindowForLevel(int l) const;
167  const IMATH_NAMESPACE::Box2i & dataWindowForLevel(int lx, int ly) const;
168 
169 
170  //
171  // Size of a level:
172  //
173  // levelWidth(lx) returns the width of a level with level
174  // number (lx, *), where * is any number.
175  //
176  // return value is:
177  // max (1, rfunc (w / pow (2, lx)))
178  //
179  //
180  // levelHeight(ly) returns the height of a level with level
181  // number (*, ly), where * is any number.
182  //
183  // return value is:
184  // max (1, rfunc (h / pow (2, ly)))
185  //
186 
188  int levelWidth (int lx) const;
190  int levelHeight (int ly) const;
191 
192 
193  //
194  // Resize the image:
195  //
196  // resize(dw,lm,lrm) sets the data window of the image to dw,
197  // sets the level mode to lm and the level rounding mode to lrm,
198  // and allocates new storage for image levels and image channels.
199  // The set of channels in the image does not change.
200  //
201  // The contents of the image are lost; pixel data are not preserved
202  // across the resize operation. If resizing fails, then the image
203  // will be left with an empty data window and no image levels.
204  //
205  // resize(dw) is the same as resize(dw,levelMode(),levelRoundingMode())
206  //
208  void resize(const IMATH_NAMESPACE::Box2i &dataWindow);
210  virtual void resize(const IMATH_NAMESPACE::Box2i &dataWindow,
211  LevelMode levelMode,
212  LevelRoundingMode levelRoundingMode);
213 
214  //
215  // Shift the pixels and the data window of an image:
216  //
217  // shiftPixels(dx,dy) shifts the image by dx pixels horizontally and
218  // dy pixels vertically. A pixel at location (x,y) moves to position
219  // (x+dx, y+dy). The data window of the image is shifted along with
220  // the pixels. No pixel data are lost.
221  //
222  // The horizontal and vertical shift distances must be multiples of
223  // the x and y sampling rates of all image channels. If they are not,
224  // shiftPixels() throws an ArgExc exception.
225  //
227  void shiftPixels(int dx, int dy);
228 
229 
230  //
231  // Insert a new channel into the image.
232  //
233  // The arguments to this function are the same as for adding a
234  // a channel to an OpenEXR file: channel name, x and y sampling
235  // rates, and a "perceptually approximately linear" flag.
236  //
237  // If the image already contains a channel with the same name
238  // as the new name then the existing channel is deleted before
239  // the new channel is added.
240  //
242  void insertChannel (const std::string &name,
243  PixelType type,
244  int xSampling = 1,
245  int ySampling = 1,
246  bool pLinear = false);
248  void insertChannel (const std::string &name,
249  const Channel &channel);
250 
251  //
252  // Erase channels from an image:
253  //
254  // eraseChannel(n) erases the channel with name n.
255  // clearChannels() erases all channels.
256  //
258  void eraseChannel(const std::string &name);
260  void clearChannels();
261 
262 
263  //
264  // Rename an image channel:
265  //
266  // renameChannel(nOld,nNew) changes the name of the image channel
267  // with name nOld to nNew.
268  //
269  // If the image already contains a channel called nNew, or if the
270  // image does not contain a channel called nOld, then renameChannel()
271  // throws an ArgExc exception.
272  //
273  // In the (unlikely) event that renaming the image channel causes
274  // the program to run out of memory, renameChannel() erases the
275  // channel that is being renamed, and throws an exception.
276  //
278  void renameChannel(const std::string &oldName,
279  const std::string &newName);
280 
281  //
282  // Rename multiple image channels at the same time:
283  //
284  // Given a map, m, from old to new channel names, renameChannels(m)
285  // assigns new names to the channels in the image. If m has an entry
286  // for a channel named c, then the channel will be renamed to m[c].
287  // If m has no entry for c, then the channel keeps its old name.
288  //
289  // If the same name would be assigned to more than one channel, then
290  // renameChannels() does not rename any channels but throws an ArgExc
291  // exception instead.
292  //
293  // In the (unlikely) event that renaming the image channel causes the
294  // program to run out of memory, renameChannels() erases all channels
295  // in the image and throws an exception.
296  //
298  void renameChannels(const RenamingMap &oldToNewNames);
299 
300 
301  //
302  // Accessing image levels by level number.
303  //
304  // level(lx,ly) returns a reference to the image level
305  // with level number (lx,ly).
306  //
307  // level(l) returns level(l,l).
308  //
309 
310  IMFUTIL_EXPORT virtual ImageLevel & level (int l = 0);
311  IMFUTIL_EXPORT virtual const ImageLevel & level (int l = 0) const;
312 
313  IMFUTIL_EXPORT virtual ImageLevel & level (int lx, int ly);
314  IMFUTIL_EXPORT virtual const ImageLevel & level (int lx, int ly) const;
315 
316 
317  protected:
318 
319  virtual ImageLevel *
320  newLevel (int lx, int ly, const IMATH_NAMESPACE::Box2i &dataWindow) = 0;
321 
322  private:
323  IMFUTIL_HIDDEN bool levelNumberIsValid (int lx, int ly) const;
324  IMFUTIL_HIDDEN void clearLevels ();
325 
326  struct IMFUTIL_HIDDEN ChannelInfo
327  {
328  ChannelInfo (PixelType type = HALF,
329  int xSampling = 1,
330  int ySampling = 1,
331  bool pLinear = false);
332 
333  PixelType type;
334  int xSampling;
335  int ySampling;
336  bool pLinear;
337  };
338 
339  typedef std::map <std::string, ChannelInfo> ChannelMap;
340 
341  IMATH_NAMESPACE::Box2i _dataWindow;
342  LevelMode _levelMode;
343  LevelRoundingMode _levelRoundingMode;
344  ChannelMap _channels;
345  Array2D<ImageLevel *> _levels;
346 };
347 
348 
350 
351 #endif
#define OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_EXIT
Definition: ImfNamespace.h:80
HALF
Definition: ImfPixelType.h:25
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
GLint level
Definition: glcorearb.h:108
Definition: Image.h:45
enum IMF_EXPORT_ENUM LevelRoundingMode
OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER typedef std::map< std::string, std::string > RenamingMap
OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER enum IMF_EXPORT_ENUM PixelType
Definition: ImfPixelType.h:22
Box< V2i > Box2i
2D box of base type int.
Definition: ImathBox.h:143
#define IMFUTIL_EXPORT_TYPE
Definition: ImfUtilExport.h:54
GLuint const GLchar * name
Definition: glcorearb.h:786
#define IMFUTIL_EXPORT
Definition: ImfUtilExport.h:51
ImageBuf OIIO_API resize(const ImageBuf &src, string_view filtername="", float filterwidth=0.0f, ROI roi={}, int nthreads=0)
#define IMFUTIL_HIDDEN
Definition: ImfUtilExport.h:52
#define OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER
Definition: ImfNamespace.h:79
OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER enum IMF_EXPORT_ENUM LevelMode
std::unordered_map< char, int > ChannelMap
Definition: TypeDesc.h:16
type
Definition: core.h:1059