1 | // tImageEXR.h  |
2 | //  |
3 | // This knows how to load and save OpenEXR images (.exr). It knows the details of the exr high dynamic range  |
4 | // file format and loads the data into a tPixel array. These tPixels may be 'stolen' by the tPicture's constructor if  |
5 | // an EXR file is specified. After the array is stolen the tImageEXR is invalid. This is purely for performance.  |
6 | //  |
7 | // Copyright (c) 2020 Tristan Grimmer.  |
8 | // Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby  |
9 | // granted, provided that the above copyright notice and this permission notice appear in all copies.  |
10 | //  |
11 | // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL  |
12 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,  |
13 | // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN  |
14 | // AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR  |
15 | // PERFORMANCE OF THIS SOFTWARE.  |
16 |   |
17 | #pragma once  |
18 | #include <Foundation/tString.h>  |
19 | #include <Math/tColour.h>  |
20 | #include <Image/tPixelFormat.h>  |
21 | namespace tImage  |
22 | {  |
23 |   |
24 |   |
25 | class tImageEXR  |
26 | {  |
27 | public:  |
28 | // Gamma range [0.6, 3.0]  |
29 | inline const static float DefaultExposure = 1.0f; // [ -10.0, 10.0 ]  |
30 | inline const static float DefaultDefog = 0.0f; // [ 0.0, 0.1 ] Try to keep below 0.01.  |
31 | inline const static float DefaultKneeLow = 0.0f; // [ -3.0, 3.0 ]  |
32 | inline const static float DefaultKneeHigh = 3.5f; // [ 3.5, 7.5 ]  |
33 |   |
34 | // Creates an invalid tImageEXR. You must call Load manually.  |
35 | tImageEXR() { }  |
36 |   |
37 | // If something went wrong (like partNum not existing) image will be invalid.  |
38 | tImageEXR  |
39 | (  |
40 | const tString& exrFile,  |
41 | int partNum = 0,  |
42 | float gamma = tMath::DefaultGamma,  |
43 | float exposure = DefaultExposure,  |
44 | float defog = DefaultDefog,  |
45 | float kneeLow = DefaultKneeLow,  |
46 | float kneeHigh = DefaultKneeHigh  |
47 | ) { Load(exrFile, partNum, gamma, exposure, defog, kneeLow, kneeHigh); }  |
48 |   |
49 | // This one sets from a supplied pixel array. It just reads the data (or steals the array if steal set).  |
50 | tImageEXR(tPixel* pixels, int width, int height, bool steal = false) { Set(pixels, width, height, steal); }  |
51 |   |
52 | virtual ~tImageEXR() { Clear(); }  |
53 |   |
54 | // Clears the current tImageEXR before loading. If false returned object is invalid.  |
55 | bool Load  |
56 | (  |
57 | const tString& exrFile,  |
58 | int partNum = 0, // Part num to load. 0-based. Returns false if didn't exist.  |
59 | float gamma = tMath::DefaultGamma,  |
60 | float exposure = DefaultExposure,  |
61 | float defog = DefaultDefog,  |
62 | float kneeLow = DefaultKneeLow,  |
63 | float kneeHigh = DefaultKneeHigh  |
64 | );  |
65 |   |
66 | // This one sets from a supplied pixel array.  |
67 | bool Set(tPixel* pixels, int width, int height, bool steal = false);  |
68 |   |
69 | // Saves the tImageEXR to the exr file specified. The extension of filename must be "exr". Returns success.  |
70 | bool Save(const tString& exrFile) const;  |
71 |   |
72 | // After this call no memory will be consumed by the object and it will be invalid.  |
73 | void Clear();  |
74 | bool IsValid() const { return Pixels ? true : false; }  |
75 |   |
76 | int GetWidth() const { return Width; }  |
77 | int GetHeight() const { return Height; }  |
78 |   |
79 | // After this call you are the owner of the pixels and must eventually delete[] them. This tImageEXR object is  |
80 | // invalid afterwards.  |
81 | tPixel* StealPixels();  |
82 | tPixel* GetPixels() const { return Pixels; }  |
83 | tPixelFormat SrcPixelFormat = tPixelFormat::Invalid;  |
84 |   |
85 | private:  |
86 | int Width = 0;  |
87 | int Height = 0;  |
88 | tPixel* Pixels = nullptr;  |
89 | };  |
90 |   |
91 |   |
92 | // Implementation blow this line.  |
93 |   |
94 |   |
95 | inline void tImageEXR::Clear()  |
96 | {  |
97 | Width = 0;  |
98 | Height = 0;  |
99 | delete[] Pixels;  |
100 | Pixels = nullptr;  |
101 | SrcPixelFormat = tPixelFormat::Invalid;  |
102 | }  |
103 |   |
104 |   |
105 | }  |
106 | |