1 | // tImageXPM.h  |
2 | //  |
3 | // This class knows how to load and save an X-Windows Pix Map (.xpm) file. It knows the details of the xpm file format  |
4 | // and loads the data into a tPixel array. These tPixels may be 'stolen' by the tPicture's constructor if a xpm file is  |
5 | // specified. After the array is stolen the tImageXPM 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 tImageXPM  |
26 | {  |
27 | public:  |
28 | // Creates an invalid tImageXPM. You must call Load manually.  |
29 | tImageXPM() { }  |
30 |   |
31 | tImageXPM(const tString& xpmFile) { Load(xpmFile); }  |
32 |   |
33 | // The data is copied out of xpmFileInMemory. Go ahead and delete after if you want.  |
34 | tImageXPM(const uint8* xpmFileInMemory, int numBytes) { Set(xpmFileInMemory, numBytes); }  |
35 |   |
36 | // This one sets from a supplied pixel array. If steal is true it takes ownership of the pixels pointer. Otherwise  |
37 | // it just copies the data out.  |
38 | tImageXPM(tPixel* pixels, int width, int height, bool steal = false) { Set(pixels, width, height, steal); }  |
39 |   |
40 | virtual ~tImageXPM() { Clear(); }  |
41 |   |
42 | // Clears the current tImageXPM before loading. Returns success. If false returned, object is invalid.  |
43 | bool Load(const tString& xpmFile);  |
44 | bool Set(const uint8* xpmFileInMemory, int numBytes);  |
45 |   |
46 | // This one sets from a supplied pixel array. If steal is true it takes ownership of the pixels pointer. Otherwise  |
47 | // it just copies the data out.  |
48 | bool Set(tPixel*, int width, int height, bool steal = false);  |
49 |   |
50 | // After this call no memory will be consumed by the object and it will be invalid.  |
51 | void Clear();  |
52 | bool IsValid() const { return Pixels ? true : false; }  |
53 |   |
54 | int GetWidth() const { return Width; }  |
55 | int GetHeight() const { return Height; }  |
56 |   |
57 | bool IsOpaque() const { return true; }  |
58 |   |
59 | // After this call you are the owner of the pixels and must eventually delete[] them. This tImageXPM object is  |
60 | // invalid afterwards.  |
61 | tPixel* StealPixels();  |
62 | tPixel* GetPixels() const { return Pixels; }  |
63 | tPixelFormat SrcPixelFormat = tPixelFormat::Invalid;  |
64 |   |
65 | private:  |
66 | int Width = 0;  |
67 | int Height = 0;  |
68 | tPixel* Pixels = nullptr;  |
69 | };  |
70 |   |
71 |   |
72 | // Implementation below this line.  |
73 |   |
74 |   |
75 | inline void tImageXPM::Clear()  |
76 | {  |
77 | Width = 0;  |
78 | Height = 0;  |
79 | delete[] Pixels;  |
80 | Pixels = nullptr;  |
81 | SrcPixelFormat = tPixelFormat::Invalid;  |
82 | }  |
83 |   |
84 |   |
85 | }  |
86 | |