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.types.image; 27 import ppc.backend.loaders.image.bmp; 28 import ppc.backend.loaders.image.png; 29 import ppc.backend.loaders.image.tga; 30 import ppc.backend.loaders.image.pti; 31 import ppc.backend; 32 import ppc.backend.cfile; 33 import ppc.backend.signatures; 34 35 /// The color format of the image 36 enum ColorFormat : ubyte{ 37 Grayscale = 1, 38 GrayscaleAlpha = 2, 39 RGB = 3, 40 RGBA = 4 41 } 42 43 /// The type of the image 44 enum ImageType : ubyte { 45 BMP, 46 PNG, 47 PTI, 48 TGA 49 } 50 51 /// Information about the image 52 /// This struct is tightly packed with no padding 53 struct ImageInfo { 54 align(1): 55 56 /// Image type 57 ImageType imageType; 58 59 /// Color format. 60 ColorFormat colorFormat; 61 62 /// Width 63 size_t width; 64 65 /// Height 66 size_t height; 67 68 } 69 70 /// A generic image 71 public struct Image { 72 public: 73 /// Information about the image 74 ImageInfo info; 75 76 /// Pixel Data. 77 ubyte[] pixelData; 78 79 /// Width of image 80 size_t width() { 81 return info.width; 82 } 83 84 /// Hight of image 85 size_t height() { 86 return info.height; 87 } 88 89 /// Creates an image from file 90 this(string file) { 91 MemFile f = loadFile(file); 92 this(f); 93 } 94 95 /// Converts image to a type, returns byte array of image data (to be written to disk or otherwise manipulated) 96 ubyte[] convertTo(ImageType type) { 97 switch(type) { 98 case ImageType.PTI: 99 return savePTI(this); 100 case ImageType.TGA: 101 return saveTGA(this); 102 case ImageType.PNG: 103 return savePNG(this); 104 default: 105 throw new Exception("This image format is not implemented yet."); 106 } 107 } 108 109 /// Creates an image from memory 110 this(MemFile file) { 111 if (file.hasSignature(FileSignature.ImagePNG)) { 112 loadPNG(file, &this); 113 } else if (file.hasSignature(FileSignature.ImagePTI)) { 114 loadPTI(file, &this); 115 } else { 116 /// Must be TARGA, has no file signature. 117 loadTGA(file, &this); 118 } 119 } 120 }