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.png; 27 import ppc.backend.cfile; 28 import ppc.types.image; 29 import imageformats; 30 31 /// The bit depth of the png file 32 enum PNGDepth { 33 Bit16, 34 Bit8 35 } 36 37 /// The type of color in the PNG file 38 enum ColorType : ubyte { 39 Grayscale = 0, 40 RGB = 2, 41 Indexed = 3, 42 GrayscaleAlpha = 4, 43 RGBA = 6 44 } 45 46 /// The compression method used in the PNG file 47 enum CompressionMethod : ubyte { 48 InflateDeflate = 0 49 } 50 51 /// The filtering method used in the PNG file 52 enum FilterMethod : ubyte { 53 StandardAdaptive = 0 54 } 55 56 /// The type of interlacing used in the PNG file 57 enum Interlace : ubyte { 58 None = 0, 59 Adam7 = 1 60 } 61 62 /// PNG Header 63 struct PNGHeader { 64 public: 65 /// Width of image 66 int width; 67 68 /// Height of image 69 int height; 70 71 /// Bit depth of image 72 byte bitDepth; 73 74 /// Color type of image 75 ColorType type; 76 77 /// Compression method of image 78 CompressionMethod compressionMethod; 79 80 /// Filtering method of image 81 FilterMethod filterMethod; 82 83 /// Interlacing method of image 84 Interlace interlaceMethod; 85 } 86 87 /// Read PNG header from memory 88 PNGHeader loadPNGHeader(MemFile file) { 89 return cast(PNGHeader)read_png_header_from_mem(file.arrayptr[0..file.length]); 90 } 91 92 /** 93 Loads a PNG file from memory 94 loadPNG will automatically infer which bitdepth to load as (8 and 16 bit supported) 95 */ 96 void loadPNG(MemFile file, Image* oimg) { 97 immutable PNGHeader header = loadPNGHeader(file); 98 99 (*oimg).info.imageType = ImageType.PNG; 100 101 // Load PNG with the right bit depth. 102 if (header.bitDepth >= 16) { 103 IFImage16 img = read_png16_from_mem(file.arrayptr[0..file.length]); 104 105 (*oimg).info.colorFormat = cast(ColorFormat)img.c; 106 (*oimg).info.width = img.w; 107 (*oimg).info.height = img.h; 108 (*oimg).pixelData = cast(ubyte[])img.pixels; 109 } else { 110 IFImage img = read_png_from_mem(file.arrayptr[0..file.length]); 111 112 (*oimg).info.colorFormat = cast(ColorFormat)img.c; 113 (*oimg).info.width = img.w; 114 (*oimg).info.height = img.h; 115 (*oimg).pixelData = img.pixels; 116 } 117 } 118 119 /** 120 Loads a PNG file from memory 121 loadPNG will automatically infer which bitdepth to load as (8 and 16 bit supported) 122 */ 123 Image loadPNG(MemFile file) { 124 Image oimg; 125 loadPNG(file, &oimg); 126 return oimg; 127 } 128 129 /// Returns a writable PNG as a ubyte array 130 ubyte[] savePNG(Image img) { 131 ubyte[] o; 132 write_png_to_mem(img.width, img.height, o); 133 return o; 134 }