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