1 /** 2 Copyright (c) 2018 Clipsey (clipseypone@gmail.com) 3 4 Permission is hereby granted, free of charge, to any person or organization 5 obtaining a copy of the software and accompanying documentation covered by 6 this license (the "Software") to use, reproduce, display, distribute, 7 execute, and transmit the Software, and to prepare derivative works of the 8 Software, and to permit third-parties to whom the Software is furnished to 9 do so, all subject to the following: 10 11 The copyright notices in the Software and this entire statement, including 12 the above license grant, this restriction and the following disclaimer, 13 must be included in all copies of the Software, in whole or in part, and 14 all derivative works of the Software, unless such copies or derivative 15 works are solely in the form of machine-executable object code generated by 16 a source language processor. 17 18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 21 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 22 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 23 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 24 DEALINGS IN THE SOFTWARE. 25 */ 26 module ppc.backend.loaders.image.pti; 27 import ppc.backend.cfile; 28 import ppc.backend.signatures; 29 import ppc.types.image; 30 /** 31 Polyplex Tagged Image Format 32 A lossless image format with tags. 33 */ 34 35 /** 36 Loads an PTI image from memory. 37 */ 38 void loadPTI(MemFile file, Image* oimg) { 39 uint width; 40 uint height; 41 uint tagMapLength; 42 ColorFormat colorFormat; 43 ubyte[] header = new ubyte[WritableFileSigs.ImagePTI.length]; 44 45 (*oimg).info.imageType = ImageType.PTI; 46 47 // Check that this is actually an PTI file. 48 file.read(header.ptr, ubyte.sizeof, WritableFileSigs.ImagePTI.length, &file); 49 if (header != WritableFileSigs.ImagePTI) throw new Exception("Invalid PTI texture file! (header mismatch)"); 50 51 // Read width/height and prepare pixel data 52 file.read(&width, uint.sizeof, 1, &file); 53 file.read(&height, uint.sizeof, 1, &file); 54 55 // Read color format 56 file.read(&colorFormat, ColorFormat.sizeof, 1, &file); 57 58 // TODO: Use tagMap. 59 // skips the tag map for now 60 file.read(&tagMapLength, uint.sizeof, 1, &file); 61 file.seek(&file, tagMapLength, SeekCurrent); 62 63 size_t cur = cast(size_t)file.tell(&file); 64 (*oimg).pixelData = new ubyte[file.length - cur]; 65 66 // Read pixel data 67 file.read(oimg.pixelData.ptr, ubyte.sizeof, oimg.pixelData.length, &file); 68 69 // Set image info 70 (*oimg).info.colorFormat = colorFormat; 71 (*oimg).info.width = width; 72 (*oimg).info.height = height; 73 } 74 75 /** 76 Loads an PTI image from memory. 77 */ 78 Image loadPTI(MemFile file) { 79 Image oimg; 80 loadPTI(file, &oimg); 81 return oimg; 82 } 83 84 import std.stdio; 85 /// Returns a writable PTI as a ubyte array 86 ubyte[] savePTI(Image img) { 87 uint tagMapLength = 0; 88 MemFile mf; 89 90 // Write file header 91 mf.write(WritableFileSigs.ImagePTI.ptr, ubyte.sizeof, WritableFileSigs.ImagePTI.length, &mf); 92 93 // Set data in a known byte-size 94 uint width = cast(uint)img.width; 95 uint height = cast(uint)img.height; 96 97 // Set image info 98 mf.write(&width, uint.sizeof, 1, &mf); 99 mf.write(&height, uint.sizeof, 1, &mf); 100 mf.write(&img.info.colorFormat, ColorFormat.sizeof, 1, &mf); 101 102 // TODO: implement image tags 103 mf.write(&tagMapLength, uint.sizeof, 1, &mf); 104 105 // Write raw pixel data. 106 mf.write(img.pixelData.ptr, ubyte.sizeof, img.pixelData.length, &mf); 107 108 return mf.toArray; 109 }