Update to 2.0.0 tree from current Fremantle build
[opencv] / 3rdparty / include / OpenEXR / ImfTiledRgbaFile.h
1 ///////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2004, Industrial Light & Magic, a division of Lucas
4 // Digital Ltd. LLC
5 // 
6 // All rights reserved.
7 // 
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions are
10 // met:
11 // *       Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // *       Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
16 // distribution.
17 // *       Neither the name of Industrial Light & Magic nor the names of
18 // its contributors may be used to endorse or promote products derived
19 // from this software without specific prior written permission. 
20 // 
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 //
33 ///////////////////////////////////////////////////////////////////////////
34
35
36 #ifndef INCLUDED_IMF_TILED_RGBA_FILE_H
37 #define INCLUDED_IMF_TILED_RGBA_FILE_H
38
39 //-----------------------------------------------------------------------------
40 //
41 //      Simplified RGBA image I/O for tiled files
42 //
43 //      class TiledRgbaOutputFile
44 //      class TiledRgbaInputFile
45 //
46 //-----------------------------------------------------------------------------
47
48 #include <ImfHeader.h>
49 #include <ImfFrameBuffer.h>
50 #include "ImathVec.h"
51 #include "ImathBox.h"
52 #include "half.h"
53 #include <ImfTileDescription.h>
54 #include <ImfRgba.h>
55 #include <ImfThreading.h>
56
57 namespace Imf {
58
59 class TiledOutputFile;
60 class TiledInputFile;
61 struct PreviewRgba;
62
63
64 //
65 // Tiled RGBA output file.
66 //
67
68 class TiledRgbaOutputFile
69 {
70   public:
71
72     //---------------------------------------------------
73     // Constructor -- rgbaChannels, tileXSize, tileYSize,
74     // levelMode, and levelRoundingMode overwrite the
75     // channel list and tile description attribute in the
76     // header that is passed as an argument to the
77     // constructor.
78     //---------------------------------------------------
79
80     TiledRgbaOutputFile (const char name[],
81                          const Header &header,
82                          RgbaChannels rgbaChannels,
83                          int tileXSize,
84                          int tileYSize,
85                          LevelMode mode,
86                          LevelRoundingMode rmode = ROUND_DOWN,
87                          int numThreads = globalThreadCount ());
88
89
90     //---------------------------------------------------
91     // Constructor -- like the previous one, but the new
92     // TiledRgbaOutputFile is attached to a file that has
93     // already been opened by the caller.  Destroying
94     // TiledRgbaOutputFileObjects constructed with this
95     // constructor does not automatically close the
96     // corresponding files.
97     //---------------------------------------------------
98
99     TiledRgbaOutputFile (OStream &os,
100                          const Header &header,
101                          RgbaChannels rgbaChannels,
102                          int tileXSize,
103                          int tileYSize,
104                          LevelMode mode,
105                          LevelRoundingMode rmode = ROUND_DOWN,
106                          int numThreads = globalThreadCount ());
107
108
109     //------------------------------------------------------
110     // Constructor -- header data are explicitly specified
111     // as function call arguments (an empty dataWindow means
112     // "same as displayWindow")
113     //------------------------------------------------------
114
115     TiledRgbaOutputFile (const char name[],
116                          int tileXSize,
117                          int tileYSize,
118                          LevelMode mode,
119                          LevelRoundingMode rmode,
120                          const Imath::Box2i &displayWindow,
121                          const Imath::Box2i &dataWindow = Imath::Box2i(),
122                          RgbaChannels rgbaChannels = WRITE_RGBA,
123                          float pixelAspectRatio = 1,
124                          const Imath::V2f screenWindowCenter =
125                                                     Imath::V2f (0, 0),
126                          float screenWindowWidth = 1,
127                          LineOrder lineOrder = INCREASING_Y,
128                          Compression compression = ZIP_COMPRESSION,
129                          int numThreads = globalThreadCount ());
130
131
132     //-----------------------------------------------
133     // Constructor -- like the previous one, but both
134     // the display window and the data window are
135     // Box2i (V2i (0, 0), V2i (width - 1, height -1))
136     //-----------------------------------------------
137
138     TiledRgbaOutputFile (const char name[],
139                          int width,
140                          int height,
141                          int tileXSize,
142                          int tileYSize,
143                          LevelMode mode,
144                          LevelRoundingMode rmode = ROUND_DOWN,
145                          RgbaChannels rgbaChannels = WRITE_RGBA,
146                          float pixelAspectRatio = 1,
147                          const Imath::V2f screenWindowCenter =
148                                                     Imath::V2f (0, 0),
149                          float screenWindowWidth = 1,
150                          LineOrder lineOrder = INCREASING_Y,
151                          Compression compression = ZIP_COMPRESSION,
152                          int numThreads = globalThreadCount ());
153
154
155     virtual ~TiledRgbaOutputFile ();
156
157
158     //------------------------------------------------
159     // Define a frame buffer as the pixel data source:
160     // Pixel (x, y) is at address
161     //
162     //  base + x * xStride + y * yStride
163     //
164     //------------------------------------------------
165
166     void                setFrameBuffer (const Rgba *base,
167                                         size_t xStride,
168                                         size_t yStride);
169
170     //--------------------------
171     // Access to the file header
172     //--------------------------
173
174     const Header &              header () const;
175     const FrameBuffer &         frameBuffer () const;
176     const Imath::Box2i &        displayWindow () const;
177     const Imath::Box2i &        dataWindow () const;
178     float                       pixelAspectRatio () const;
179     const Imath::V2f            screenWindowCenter () const;
180     float                       screenWindowWidth () const;
181     LineOrder                   lineOrder () const;
182     Compression                 compression () const;
183     RgbaChannels                channels () const;
184
185
186     //----------------------------------------------------
187     // Utility functions (same as in Imf::TiledOutputFile)
188     //----------------------------------------------------
189
190     unsigned int        tileXSize () const;
191     unsigned int        tileYSize () const;
192     LevelMode           levelMode () const;
193     LevelRoundingMode   levelRoundingMode () const;
194
195     int                 numLevels () const;
196     int                 numXLevels () const;
197     int                 numYLevels () const;
198     bool                isValidLevel (int lx, int ly) const;
199
200     int                 levelWidth  (int lx) const;
201     int                 levelHeight (int ly) const;
202
203     int                 numXTiles (int lx = 0) const;
204     int                 numYTiles (int ly = 0) const;
205
206     Imath::Box2i        dataWindowForLevel (int l = 0) const;
207     Imath::Box2i        dataWindowForLevel (int lx, int ly) const;
208
209     Imath::Box2i        dataWindowForTile (int dx, int dy,
210                                            int l = 0) const;
211
212     Imath::Box2i        dataWindowForTile (int dx, int dy,
213                                            int lx, int ly) const;
214
215     //------------------------------------------------------------------
216     // Write pixel data:
217     //
218     // writeTile(dx, dy, lx, ly) writes the tile with tile
219     // coordinates (dx, dy), and level number (lx, ly) to
220     // the file.
221     //
222     //   dx must lie in the interval [0, numXTiles(lx)-1]
223     //   dy must lie in the interval [0, numYTiles(ly)-1]
224     //
225     //   lx must lie in the interval [0, numXLevels()-1]
226     //   ly must lie in the inverval [0, numYLevels()-1]
227     //
228     // writeTile(dx, dy, level) is a convenience function
229     // used for ONE_LEVEL and MIPMAP_LEVEL files.  It calls
230     // writeTile(dx, dy, level, level).
231     //
232     // The two writeTiles(dx1, dx2, dy1, dy2, ...) functions allow
233     // writing multiple tiles at once.  If multi-threading is used
234     // multiple tiles are written concurrently.
235     //
236     // Pixels that are outside the pixel coordinate range for the tile's
237     // level, are never accessed by writeTile().
238     //
239     // Each tile in the file must be written exactly once.
240     //
241     //------------------------------------------------------------------
242
243     void                writeTile (int dx, int dy, int l = 0);
244     void                writeTile (int dx, int dy, int lx, int ly);
245
246     void                writeTiles (int dxMin, int dxMax, int dyMin, int dyMax,
247                                     int lx, int ly);
248
249     void                writeTiles (int dxMin, int dxMax, int dyMin, int dyMax,
250                                     int l = 0);
251
252
253     // -------------------------------------------------------------------------
254     // Update the preview image (see Imf::TiledOutputFile::updatePreviewImage())
255     // -------------------------------------------------------------------------
256
257     void                updatePreviewImage (const PreviewRgba[]);
258
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     void                breakTile  (int dx, int dy,
272                                     int lx, int ly,
273                                     int offset,
274                                     int length,
275                                     char c);
276   private:
277
278     //
279     // Copy constructor and assignment are not implemented
280     //
281
282     TiledRgbaOutputFile (const TiledRgbaOutputFile &);  
283     TiledRgbaOutputFile & operator = (const TiledRgbaOutputFile &);
284
285     class ToYa;
286
287     TiledOutputFile *            _outputFile;
288     ToYa *                      _toYa;
289 };
290
291
292
293 //
294 // Tiled RGBA input file
295 //
296
297 class TiledRgbaInputFile
298 {
299   public:
300
301     //--------------------------------------------------------
302     // Constructor -- opens the file with the specified name.
303     // Destroying TiledRgbaInputFile objects constructed with
304     // this constructor automatically closes the corresponding
305     // files.
306     //--------------------------------------------------------
307
308     TiledRgbaInputFile (const char name[],
309                         int numThreads = globalThreadCount ());
310
311
312     //-------------------------------------------------------
313     // Constructor -- attaches the new TiledRgbaInputFile
314     // object to a file that has already been opened by the
315     // caller.
316     // Destroying TiledRgbaInputFile objects constructed with
317     // this constructor does not automatically close the
318     // corresponding files.
319     //-------------------------------------------------------
320
321     TiledRgbaInputFile (IStream &is, int numThreads = globalThreadCount ());
322
323
324     //-----------
325     // Destructor
326     //-----------
327
328     virtual ~TiledRgbaInputFile ();
329
330
331     //-----------------------------------------------------
332     // Define a frame buffer as the pixel data destination:
333     // Pixel (x, y) is at address
334     //
335     //  base + x * xStride + y * yStride
336     //
337     //-----------------------------------------------------
338
339     void                        setFrameBuffer (Rgba *base,
340                                                 size_t xStride,
341                                                 size_t yStride);
342
343     //--------------------------
344     // Access to the file header
345     //--------------------------
346
347     const Header &              header () const;
348     const FrameBuffer &         frameBuffer () const;
349     const Imath::Box2i &        displayWindow () const;
350     const Imath::Box2i &        dataWindow () const;
351     float                       pixelAspectRatio () const;
352     const Imath::V2f            screenWindowCenter () const;
353     float                       screenWindowWidth () const;
354     LineOrder                   lineOrder () const;
355     Compression                 compression () const;
356     RgbaChannels                channels () const;
357     const char *                fileName () const;
358     bool                        isComplete () const;
359
360     //----------------------------------
361     // Access to the file format version
362     //----------------------------------
363
364     int                         version () const;
365
366
367     //---------------------------------------------------
368     // Utility functions (same as in Imf::TiledInputFile)
369     //---------------------------------------------------
370
371     unsigned int        tileXSize () const;
372     unsigned int        tileYSize () const;
373     LevelMode           levelMode () const;
374     LevelRoundingMode   levelRoundingMode () const;
375
376     int                 numLevels () const;
377     int                 numXLevels () const;
378     int                 numYLevels () const;
379     bool                isValidLevel (int lx, int ly) const;
380
381     int                 levelWidth  (int lx) const;
382     int                 levelHeight (int ly) const;
383
384     int                 numXTiles (int lx = 0) const;
385     int                 numYTiles (int ly = 0) const;
386
387     Imath::Box2i        dataWindowForLevel (int l = 0) const;
388     Imath::Box2i        dataWindowForLevel (int lx, int ly) const;
389
390     Imath::Box2i        dataWindowForTile (int dx, int dy,
391                                            int l = 0) const;
392
393     Imath::Box2i        dataWindowForTile (int dx, int dy,
394                                            int lx, int ly) const;
395                                            
396
397     //----------------------------------------------------------------
398     // Read pixel data:
399     //
400     // readTile(dx, dy, lx, ly) reads the tile with tile
401     // coordinates (dx, dy), and level number (lx, ly),
402     // and stores it in the current frame buffer.
403     //
404     //   dx must lie in the interval [0, numXTiles(lx)-1]
405     //   dy must lie in the interval [0, numYTiles(ly)-1]
406     //
407     //   lx must lie in the interval [0, numXLevels()-1]
408     //   ly must lie in the inverval [0, numYLevels()-1]
409     //
410     // readTile(dx, dy, level) is a convenience function used
411     // for ONE_LEVEL and MIPMAP_LEVELS files.  It calls
412     // readTile(dx, dy, level, level).
413     //
414     // The two readTiles(dx1, dx2, dy1, dy2, ...) functions allow
415     // reading multiple tiles at once.  If multi-threading is used
416     // multiple tiles are read concurrently.
417     //
418     // Pixels that are outside the pixel coordinate range for the
419     // tile's level, are never accessed by readTile().
420     //
421     // Attempting to access a tile that is not present in the file
422     // throws an InputExc exception.
423     //
424     //----------------------------------------------------------------
425
426     void                readTile (int dx, int dy, int l = 0);
427     void                readTile (int dx, int dy, int lx, int ly);
428
429     void                readTiles (int dxMin, int dxMax,
430                                    int dyMin, int dyMax, int lx, int ly);
431
432     void                readTiles (int dxMin, int dxMax,
433                                    int dyMin, int dyMax, int l = 0);
434
435   private:
436
437     //
438     // Copy constructor and assignment are not implemented
439     //
440
441     TiledRgbaInputFile (const TiledRgbaInputFile &);
442     TiledRgbaInputFile & operator = (const TiledRgbaInputFile &);
443
444     class FromYa;
445
446     TiledInputFile *    _inputFile;
447     FromYa *            _fromYa;
448 };
449
450
451 } // namespace Imf
452
453 #endif