HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ImfTiledRgbaFile.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_TILED_RGBA_FILE_H
7 #define INCLUDED_IMF_TILED_RGBA_FILE_H
8 
9 //-----------------------------------------------------------------------------
10 //
11 // Simplified RGBA image I/O for tiled files
12 //
13 // class TiledRgbaOutputFile
14 // class TiledRgbaInputFile
15 //
16 //-----------------------------------------------------------------------------
17 
18 #include "ImfForward.h"
19 
20 #include "ImfCompression.h"
21 #include "ImfLineOrder.h"
22 #include "ImfRgba.h"
23 #include "ImfThreading.h"
24 #include "ImfTileDescription.h"
25 
26 #include "ImfContextInit.h"
27 
28 #include <ImathBox.h>
29 #include <ImathVec.h>
30 #include <half.h>
31 
32 #include <string>
33 
35 
36 //
37 // Tiled RGBA output file.
38 //
39 
41 {
42 public:
43  //---------------------------------------------------
44  // Constructor -- rgbaChannels, tileXSize, tileYSize,
45  // levelMode, and levelRoundingMode overwrite the
46  // channel list and tile description attribute in the
47  // header that is passed as an argument to the
48  // constructor.
49  //---------------------------------------------------
50 
53  const char name[],
54  const Header& header,
55  RgbaChannels rgbaChannels,
56  int tileXSize,
57  int tileYSize,
60  int numThreads = globalThreadCount ());
61 
62  //---------------------------------------------------
63  // Constructor -- like the previous one, but the new
64  // TiledRgbaOutputFile is attached to a file that has
65  // already been opened by the caller. Destroying
66  // TiledRgbaOutputFileObjects constructed with this
67  // constructor does not automatically close the
68  // corresponding files.
69  //---------------------------------------------------
70 
72  TiledRgbaOutputFile (
74  const Header& header,
75  RgbaChannels rgbaChannels,
76  int tileXSize,
77  int tileYSize,
78  LevelMode mode,
80  int numThreads = globalThreadCount ());
81 
82  //------------------------------------------------------
83  // Constructor -- header data are explicitly specified
84  // as function call arguments (an empty dataWindow means
85  // "same as displayWindow")
86  //------------------------------------------------------
87 
89  TiledRgbaOutputFile (
90  const char name[],
91  int tileXSize,
92  int tileYSize,
93  LevelMode mode,
94  LevelRoundingMode rmode,
95  const IMATH_NAMESPACE::Box2i& displayWindow,
96  const IMATH_NAMESPACE::Box2i& dataWindow = IMATH_NAMESPACE::Box2i (),
97  RgbaChannels rgbaChannels = WRITE_RGBA,
98  float pixelAspectRatio = 1,
99  const IMATH_NAMESPACE::V2f screenWindowCenter =
100  IMATH_NAMESPACE::V2f (0, 0),
101  float screenWindowWidth = 1,
102  LineOrder lineOrder = INCREASING_Y,
103  Compression compression = ZIP_COMPRESSION,
104  int numThreads = globalThreadCount ());
105 
106  //-----------------------------------------------
107  // Constructor -- like the previous one, but both
108  // the display window and the data window are
109  // Box2i (V2i (0, 0), V2i (width - 1, height -1))
110  //-----------------------------------------------
111 
112  IMF_EXPORT
113  TiledRgbaOutputFile (
114  const char name[],
115  int width,
116  int height,
117  int tileXSize,
118  int tileYSize,
119  LevelMode mode,
121  RgbaChannels rgbaChannels = WRITE_RGBA,
122  float pixelAspectRatio = 1,
123  const IMATH_NAMESPACE::V2f screenWindowCenter =
124  IMATH_NAMESPACE::V2f (0, 0),
125  float screenWindowWidth = 1,
126  LineOrder lineOrder = INCREASING_Y,
127  Compression compression = ZIP_COMPRESSION,
128  int numThreads = globalThreadCount ());
129 
130  IMF_EXPORT
131  virtual ~TiledRgbaOutputFile ();
132 
133  //------------------------------------------------
134  // Define a frame buffer as the pixel data source:
135  // Pixel (x, y) is at address
136  //
137  // base + x * xStride + y * yStride
138  //
139  //------------------------------------------------
140 
141  IMF_EXPORT
142  void setFrameBuffer (const Rgba* base, size_t xStride, size_t yStride);
143 
144  //--------------------------
145  // Access to the file header
146  //--------------------------
147 
148  IMF_EXPORT
149  const Header& header () const;
150  IMF_EXPORT
151  const FrameBuffer& frameBuffer () const;
152  IMF_EXPORT
153  const IMATH_NAMESPACE::Box2i& displayWindow () const;
154  IMF_EXPORT
155  const IMATH_NAMESPACE::Box2i& dataWindow () const;
156  IMF_EXPORT
157  float pixelAspectRatio () const;
158  IMF_EXPORT
159  const IMATH_NAMESPACE::V2f screenWindowCenter () const;
160  IMF_EXPORT
161  float screenWindowWidth () const;
162  IMF_EXPORT
163  LineOrder lineOrder () const;
164  IMF_EXPORT
165  Compression compression () const;
166  IMF_EXPORT
167  RgbaChannels channels () const;
168 
169  //----------------------------------------------------
170  // Utility functions (same as in Imf::TiledOutputFile)
171  //----------------------------------------------------
172 
173  IMF_EXPORT
174  unsigned int tileXSize () const;
175  IMF_EXPORT
176  unsigned int tileYSize () const;
177  IMF_EXPORT
178  LevelMode levelMode () const;
179  IMF_EXPORT
180  LevelRoundingMode levelRoundingMode () const;
181 
182  IMF_EXPORT
183  int numLevels () const;
184  IMF_EXPORT
185  int numXLevels () const;
186  IMF_EXPORT
187  int numYLevels () const;
188  IMF_EXPORT
189  bool isValidLevel (int lx, int ly) const;
190 
191  IMF_EXPORT
192  int levelWidth (int lx) const;
193  IMF_EXPORT
194  int levelHeight (int ly) const;
195 
196  IMF_EXPORT
197  int numXTiles (int lx = 0) const;
198  IMF_EXPORT
199  int numYTiles (int ly = 0) const;
200 
201  IMF_EXPORT
202  IMATH_NAMESPACE::Box2i dataWindowForLevel (int l = 0) const;
203  IMF_EXPORT
204  IMATH_NAMESPACE::Box2i dataWindowForLevel (int lx, int ly) const;
205 
206  IMF_EXPORT
207  IMATH_NAMESPACE::Box2i dataWindowForTile (int dx, int dy, int l = 0) const;
208 
209  IMF_EXPORT
211  dataWindowForTile (int dx, int dy, int lx, int ly) const;
212 
213  //------------------------------------------------------------------
214  // Write pixel data:
215  //
216  // writeTile(dx, dy, lx, ly) writes the tile with tile
217  // coordinates (dx, dy), and level number (lx, ly) to
218  // the file.
219  //
220  // dx must lie in the interval [0, numXTiles(lx)-1]
221  // dy must lie in the interval [0, numYTiles(ly)-1]
222  //
223  // lx must lie in the interval [0, numXLevels()-1]
224  // ly must lie in the interval [0, numYLevels()-1]
225  //
226  // writeTile(dx, dy, level) is a convenience function
227  // used for ONE_LEVEL and MIPMAP_LEVEL files. It calls
228  // writeTile(dx, dy, level, level).
229  //
230  // The two writeTiles(dx1, dx2, dy1, dy2, ...) functions allow
231  // writing multiple tiles at once. If multi-threading is used
232  // multiple tiles are written concurrently.
233  //
234  // Pixels that are outside the pixel coordinate range for the tile's
235  // level, are never accessed by writeTile().
236  //
237  // Each tile in the file must be written exactly once.
238  //
239  //------------------------------------------------------------------
240 
241  IMF_EXPORT
242  void writeTile (int dx, int dy, int l = 0);
243  IMF_EXPORT
244  void writeTile (int dx, int dy, int lx, int ly);
245 
246  IMF_EXPORT
247  void
248  writeTiles (int dxMin, int dxMax, int dyMin, int dyMax, int lx, int ly);
249 
250  IMF_EXPORT
251  void writeTiles (int dxMin, int dxMax, int dyMin, int dyMax, int l = 0);
252 
253  // -------------------------------------------------------------------------
254  // Update the preview image (see Imf::TiledOutputFile::updatePreviewImage())
255  // -------------------------------------------------------------------------
256 
257  IMF_EXPORT
258  void updatePreviewImage (const PreviewRgba[]);
259 
260  //------------------------------------------------
261  // Break a tile -- for testing and debugging only
262  // (see Imf::TiledOutputFile::breakTile())
263  //
264  // Warning: Calling this function usually results
265  // in a broken image file. The file or parts of
266  // it may not be readable, or the file may contain
267  // bad data.
268  //
269  //------------------------------------------------
270 
271  IMF_EXPORT
272  void
273  breakTile (int dx, int dy, int lx, int ly, int offset, int length, char c);
274 
275 private:
276  //
277  // Copy constructor and assignment are not implemented
278  //
279 
280  TiledRgbaOutputFile (const TiledRgbaOutputFile&) = delete;
281  TiledRgbaOutputFile& operator= (const TiledRgbaOutputFile&) = delete;
282  TiledRgbaOutputFile (TiledRgbaOutputFile&&) = delete;
283  TiledRgbaOutputFile& operator= (TiledRgbaOutputFile&&) = delete;
284 
285  class IMF_HIDDEN ToYa;
286 
287  TiledOutputFile* _outputFile;
288  ToYa* _toYa;
289 };
290 
291 //
292 // Tiled RGBA input file
293 //
294 
296 {
297 public:
298  //--------------------------------------------------------
299  // Constructor -- opens the file with the specified name.
300  // Destroying TiledRgbaInputFile objects constructed with
301  // this constructor automatically closes the corresponding
302  // files.
303  //--------------------------------------------------------
304 
305  IMF_EXPORT
307  const char name[],
308  const ContextInitializer &ctxt,
309  const std::string& layerName,
310  int numThreads = globalThreadCount ());
311 
312  //--------------------------------------------------------
313  // Constructor -- opens the file with the specified name.
314  // Destroying TiledRgbaInputFile objects constructed with
315  // this constructor automatically closes the corresponding
316  // files.
317  //--------------------------------------------------------
318 
319  IMF_EXPORT
320  TiledRgbaInputFile (
321  const char name[], int numThreads = globalThreadCount ());
322 
323  //-------------------------------------------------------
324  // Constructor -- attaches the new TiledRgbaInputFile
325  // object to a file that has already been opened by the
326  // caller.
327  // Destroying TiledRgbaInputFile objects constructed with
328  // this constructor does not automatically close the
329  // corresponding files.
330  //-------------------------------------------------------
331 
332  IMF_EXPORT
333  TiledRgbaInputFile (
335  int numThreads = globalThreadCount ());
336 
337  //------------------------------------------------------------
338  // Constructors -- the same as the previous two, but the names
339  // of the red, green, blue, alpha, and luminance channels are
340  // expected to be layerName.R, layerName.G, etc.
341  //------------------------------------------------------------
342 
343  IMF_EXPORT
344  TiledRgbaInputFile (
345  const char name[],
346  const std::string& layerName,
347  int numThreads = globalThreadCount ());
348 
349  IMF_EXPORT
350  TiledRgbaInputFile (
352  const std::string& layerName,
353  int numThreads = globalThreadCount ());
354 
355  //-----------
356  // Destructor
357  //-----------
358 
359  IMF_EXPORT
360  virtual ~TiledRgbaInputFile ();
361 
362  //-----------------------------------------------------
363  // Define a frame buffer as the pixel data destination:
364  // Pixel (x, y) is at address
365  //
366  // base + x * xStride + y * yStride
367  //
368  //-----------------------------------------------------
369 
370  IMF_EXPORT
371  void setFrameBuffer (Rgba* base, size_t xStride, size_t yStride);
372 
373  //-------------------------------------------------------------------
374  // Switch to a different layer -- subsequent calls to readTile()
375  // and readTiles() will read channels layerName.R, layerName.G, etc.
376  // After each call to setLayerName(), setFrameBuffer() must be called
377  // at least once before the next call to readTile() or readTiles().
378  //-------------------------------------------------------------------
379 
380  IMF_EXPORT
381  void setLayerName (const std::string& layerName);
382 
383  //--------------------------
384  // Access to the file header
385  //--------------------------
386 
387  IMF_EXPORT
388  const Header& header () const;
389  IMF_EXPORT
390  const FrameBuffer& frameBuffer () const;
391  IMF_EXPORT
392  const IMATH_NAMESPACE::Box2i& displayWindow () const;
393  IMF_EXPORT
394  const IMATH_NAMESPACE::Box2i& dataWindow () const;
395  IMF_EXPORT
396  float pixelAspectRatio () const;
397  IMF_EXPORT
398  const IMATH_NAMESPACE::V2f screenWindowCenter () const;
399  IMF_EXPORT
400  float screenWindowWidth () const;
401  IMF_EXPORT
402  LineOrder lineOrder () const;
403  IMF_EXPORT
404  Compression compression () const;
405  IMF_EXPORT
406  RgbaChannels channels () const;
407  IMF_EXPORT
408  const char* fileName () const;
409  IMF_EXPORT
410  bool isComplete () const;
411 
412  //----------------------------------
413  // Access to the file format version
414  //----------------------------------
415 
416  IMF_EXPORT
417  int version () const;
418 
419  //---------------------------------------------------
420  // Utility functions (same as in Imf::TiledInputFile)
421  //---------------------------------------------------
422 
423  IMF_EXPORT
424  unsigned int tileXSize () const;
425  IMF_EXPORT
426  unsigned int tileYSize () const;
427  IMF_EXPORT
428  LevelMode levelMode () const;
429  IMF_EXPORT
430  LevelRoundingMode levelRoundingMode () const;
431 
432  IMF_EXPORT
433  int numLevels () const;
434  IMF_EXPORT
435  int numXLevels () const;
436  IMF_EXPORT
437  int numYLevels () const;
438  IMF_EXPORT
439  bool isValidLevel (int lx, int ly) const;
440 
441  IMF_EXPORT
442  int levelWidth (int lx) const;
443  IMF_EXPORT
444  int levelHeight (int ly) const;
445 
446  IMF_EXPORT
447  int numXTiles (int lx = 0) const;
448  IMF_EXPORT
449  int numYTiles (int ly = 0) const;
450 
451  IMF_EXPORT
452  IMATH_NAMESPACE::Box2i dataWindowForLevel (int l = 0) const;
453  IMF_EXPORT
454  IMATH_NAMESPACE::Box2i dataWindowForLevel (int lx, int ly) const;
455 
456  IMF_EXPORT
457  IMATH_NAMESPACE::Box2i dataWindowForTile (int dx, int dy, int l = 0) const;
458 
459  IMF_EXPORT
461  dataWindowForTile (int dx, int dy, int lx, int ly) const;
462 
463  //----------------------------------------------------------------
464  // Read pixel data:
465  //
466  // readTile(dx, dy, lx, ly) reads the tile with tile
467  // coordinates (dx, dy), and level number (lx, ly),
468  // and stores it in the current frame buffer.
469  //
470  // dx must lie in the interval [0, numXTiles(lx)-1]
471  // dy must lie in the interval [0, numYTiles(ly)-1]
472  //
473  // lx must lie in the interval [0, numXLevels()-1]
474  // ly must lie in the interval [0, numYLevels()-1]
475  //
476  // readTile(dx, dy, level) is a convenience function used
477  // for ONE_LEVEL and MIPMAP_LEVELS files. It calls
478  // readTile(dx, dy, level, level).
479  //
480  // The two readTiles(dx1, dx2, dy1, dy2, ...) functions allow
481  // reading multiple tiles at once. If multi-threading is used
482  // multiple tiles are read concurrently.
483  //
484  // Pixels that are outside the pixel coordinate range for the
485  // tile's level, are never accessed by readTile().
486  //
487  // Attempting to access a tile that is not present in the file
488  // throws an InputExc exception.
489  //
490  //----------------------------------------------------------------
491 
492  IMF_EXPORT
493  void readTile (int dx, int dy, int l = 0);
494  IMF_EXPORT
495  void readTile (int dx, int dy, int lx, int ly);
496 
497  IMF_EXPORT
498  void readTiles (int dxMin, int dxMax, int dyMin, int dyMax, int lx, int ly);
499 
500  IMF_EXPORT
501  void readTiles (int dxMin, int dxMax, int dyMin, int dyMax, int l = 0);
502 
503 private:
504  //
505  // Copy constructor and assignment are not implemented
506  //
507 
508  TiledRgbaInputFile (const TiledRgbaInputFile&) = delete;
509  TiledRgbaInputFile& operator= (const TiledRgbaInputFile&) = delete;
510  TiledRgbaInputFile (TiledRgbaInputFile&&) = delete;
511  TiledRgbaInputFile& operator= (TiledRgbaInputFile&&) = delete;
512 
513  class FromYa;
514 
515  TiledInputFile* _inputFile;
516  FromYa* _fromYa;
517  std::string _channelNamePrefix;
518 };
519 
521 
522 #endif
ZIP_COMPRESSION
#define OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_EXIT
Definition: ImfNamespace.h:83
OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER enum IMF_EXPORT_ENUM Compression
OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER enum IMF_EXPORT_ENUM LineOrder
Definition: ImfLineOrder.h:19
GLuint GLsizei GLsizei * length
Definition: glcorearb.h:795
#define IMF_HIDDEN
Definition: ImfExport.h:55
ContextInitializer provides a basic type to initialize a Context with.
enum IMF_EXPORT_ENUM LevelRoundingMode
GLint GLsizei GLsizei height
Definition: glcorearb.h:103
GLintptr offset
Definition: glcorearb.h:665
Box< V2i > Box2i
2D box of base type int.
Definition: ImathBox.h:143
#define IMF_EXPORT
Definition: ImfExport.h:54
GLuint const GLchar * name
Definition: glcorearb.h:786
HUSD_API const char * pixelAspectRatio()
class IMF_EXPORT_TYPE OStream
Definition: ImfForward.h:86
GLenum mode
Definition: glcorearb.h:99
GT_API const UT_StringHolder version
Vec2< float > V2f
Vec2 of float.
Definition: ImathVec.h:834
OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER IMF_EXPORT int globalThreadCount()
LeafData & operator=(const LeafData &)=delete
#define OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER
Definition: ImfNamespace.h:80
GLint GLsizei width
Definition: glcorearb.h:103
Definition: ImfRgba.h:26
OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER enum IMF_EXPORT_ENUM LevelMode
INCREASING_Y
Definition: ImfLineOrder.h:21
ROUND_DOWN
#define IMF_EXPORT_TYPE
Definition: ImfExport.h:57
enum IMF_EXPORT_ENUM RgbaChannels
Definition: ImfRgba.h:41
WRITE_RGBA
Definition: ImfRgba.h:55
ImageBuf OIIO_API channels(const ImageBuf &src, int nchannels, cspan< int > channelorder, cspan< float > channelvalues={}, cspan< std::string > newchannelnames={}, bool shuffle_channel_names=false, int nthreads=0)
class IMF_EXPORT_TYPE IStream
Definition: ImfForward.h:87