1 | // tImageTIFF.h  |
2 | //  |
3 | // This knows how to load TIFFs. It knows the details of the tiff file format and loads the data into multiple tPixel  |
4 | // arrays, one for each frame (in a TIFF thay are called pages). These arrays may be 'stolen' by tPictures.  |
5 | //  |
6 | // Copyright (c) 2020 Tristan Grimmer.  |
7 | // Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby  |
8 | // granted, provided that the above copyright notice and this permission notice appear in all copies.  |
9 | //  |
10 | // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL  |
11 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,  |
12 | // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN  |
13 | // AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR  |
14 | // PERFORMANCE OF THIS SOFTWARE.  |
15 |   |
16 | #pragma once  |
17 | #include <Foundation/tString.h>  |
18 | #include <Math/tColour.h>  |
19 | #include <Image/tPixelFormat.h>  |
20 | namespace tImage  |
21 | {  |
22 |   |
23 |   |
24 | class tImageTIFF  |
25 | {  |
26 | public:  |
27 | // Creates an invalid tImageTIFF. You must call Load manually.  |
28 | tImageTIFF() { }  |
29 | tImageTIFF(const tString& tiffFile) { Load(tiffFile); }  |
30 |   |
31 | virtual ~tImageTIFF() { Clear(); }  |
32 |   |
33 | // Clears the current tImageTIFF before loading. If false returned object is invalid.  |
34 | bool Load(const tString& tiffFile);  |
35 |   |
36 | // After this call no memory will be consumed by the object and it will be invalid.  |
37 | void Clear();  |
38 | bool IsValid() const { return (GetNumFrames() >= 1); }  |
39 | int GetNumFrames() const { return Frames.GetNumItems(); }  |
40 |   |
41 | struct Frame : public tLink<Frame>  |
42 | {  |
43 | int Width = 0;  |
44 | int Height = 0;  |
45 | tPixel* Pixels = nullptr;  |
46 | tPixelFormat SrcPixelFormat = tPixelFormat::Invalid;  |
47 | };  |
48 |   |
49 | // After this call you are the owner of the frame and must eventually delete it. The frame you stole will no  |
50 | // longer be a valid frame of the tImageTIFF, but the remaining ones will still be valid.  |
51 | Frame* StealFrame(int );  |
52 | Frame* GetFrame(int );  |
53 | tPixelFormat SrcPixelFormat = tPixelFormat::Invalid;  |
54 |   |
55 | private:  |
56 | tList<Frame> Frames;  |
57 | };  |
58 |   |
59 |   |
60 | // Implementation only below.  |
61 |   |
62 |   |
63 | inline tImageTIFF::Frame* tImage::tImageTIFF::StealFrame(int )  |
64 | {  |
65 | Frame* f = GetFrame(frameNum);  |
66 | if (!f)  |
67 | return nullptr;  |
68 |   |
69 | return Frames.Remove(f);  |
70 | }  |
71 |   |
72 |   |
73 | inline tImageTIFF::Frame* tImage::tImageTIFF::GetFrame(int )  |
74 | {  |
75 | if ((frameNum >= Frames.GetNumItems()) || (frameNum < 0))  |
76 | return nullptr;  |
77 |   |
78 | Frame* f = Frames.First();  |
79 | while (frameNum--)  |
80 | f = f->Next();  |
81 |   |
82 | return f;  |
83 | }  |
84 |   |
85 |   |
86 | inline void tImageTIFF::Clear()  |
87 | {  |
88 | while (Frame* frame = Frames.Remove())  |
89 | {  |
90 | delete[] frame->Pixels;  |
91 | delete frame;  |
92 | }  |
93 |   |
94 | SrcPixelFormat = tPixelFormat::Invalid;  |
95 | }  |
96 |   |
97 |   |
98 | }  |
99 | |