| 1 | // tImageXPM.cpp  |
| 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, 2024 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 | #include <System/tFile.h>  |
| 18 | #include "Image/tImageXPM.h"  |
| 19 |   |
| 20 |   |
| 21 | using namespace tSystem;  |
| 22 | namespace tImage  |
| 23 | {  |
| 24 |   |
| 25 |   |
| 26 | bool tImageXPM::Load(const tString& xpmFile)  |
| 27 | {  |
| 28 | Clear();  |
| 29 |   |
| 30 | if (tSystem::tGetFileType(file: xpmFile) != tSystem::tFileType::XPM)  |
| 31 | return false;  |
| 32 |   |
| 33 | if (!tFileExists(file: xpmFile))  |
| 34 | return false;  |
| 35 |   |
| 36 | int numBytes = 0;  |
| 37 | uint8* xpmFileInMemory = tLoadFile(file: xpmFile, buffer: nullptr, fileSize: &numBytes);  |
| 38 | bool success = Load(xpmFileInMemory, numBytes);  |
| 39 | delete[] xpmFileInMemory;  |
| 40 |   |
| 41 | return success;  |
| 42 | }  |
| 43 |   |
| 44 |   |
| 45 | bool tImageXPM::Load(const uint8* xpmFileInMemory, int numBytes)  |
| 46 | {  |
| 47 | Clear();  |
| 48 | if ((numBytes <= 0) || !xpmFileInMemory)  |
| 49 | return false;  |
| 50 |   |
| 51 | // Set Width, Height, Pixels, and SrcPixelFormat.  |
| 52 | // @todo I'm going to build my own xpm parser here. I really found the official libxpm  |
| 53 | // very akward and it required unnecessary platform headers that are not needed for text file parsing.  |
| 54 | Width = 256;  |
| 55 | Height = 256;  |
| 56 | int numPixels = Width * Height;  |
| 57 | Pixels = new tPixel4b[numPixels];  |
| 58 | for (int p = 0; p < Width*Height; p++)  |
| 59 | {  |
| 60 | Pixels[p] = tPixel4b::blue;  |
| 61 | }  |
| 62 | PixelFormatSrc = tPixelFormat::R8G8B8A8;  |
| 63 | PixelFormat = tPixelFormat::R8G8B8A8;  |
| 64 |   |
| 65 | // XPM files are assumed to be in sRGB.  |
| 66 | ColourProfileSrc = tColourProfile::sRGB;  |
| 67 | ColourProfile = tColourProfile::sRGB;  |
| 68 |   |
| 69 | return true;  |
| 70 | }  |
| 71 |   |
| 72 |   |
| 73 | bool tImageXPM::Set(tPixel4b* pixels, int width, int height, bool steal)  |
| 74 | {  |
| 75 | Clear();  |
| 76 | if (!pixels || (width <= 0) || (height <= 0))  |
| 77 | return false;  |
| 78 |   |
| 79 | Width = width;  |
| 80 | Height = height;  |
| 81 | if (steal)  |
| 82 | {  |
| 83 | Pixels = pixels;  |
| 84 | }  |
| 85 | else  |
| 86 | {  |
| 87 | Pixels = new tPixel4b[Width*Height];  |
| 88 | tStd::tMemcpy(dest: Pixels, src: pixels, numBytes: Width*Height*sizeof(tPixel4b));  |
| 89 | }  |
| 90 |   |
| 91 | PixelFormatSrc = tPixelFormat::R8G8B8A8;  |
| 92 | PixelFormat = tPixelFormat::R8G8B8A8;  |
| 93 | ColourProfileSrc = tColourProfile::sRGB; // We assume pixels must be sRGB.  |
| 94 | ColourProfile = tColourProfile::sRGB;  |
| 95 |   |
| 96 | return true;  |
| 97 | }  |
| 98 |   |
| 99 |   |
| 100 | tPixel4b* tImageXPM::StealPixels()  |
| 101 | {  |
| 102 | tPixel4b* pixels = Pixels;  |
| 103 | Pixels = nullptr;  |
| 104 | Width = 0;  |
| 105 | Height = 0;  |
| 106 | return pixels;  |
| 107 | }  |
| 108 |   |
| 109 |   |
| 110 | }  |
| 111 | |