1 module ppc.image; 2 import ppc; 3 import imageformats; 4 import std.stdio; 5 6 public class ImageFactory : ContentFactory { 7 public this() { 8 super(TypeId.Texture2D); 9 } 10 11 public override Content Construct(ubyte[] data) { 12 return new Image(data); 13 } 14 } 15 16 public class Image : Content { 17 public long Width; 18 public long Height; 19 public ubyte[] Colors; 20 21 public this(string name) { 22 super(TypeId.Texture2D, name); 23 } 24 25 this(ubyte[] data) { 26 super(data); 27 } 28 29 public override void Convert(ubyte[] data) { 30 IFImage im = read_image_from_mem(data, ColFmt.RGBA); 31 this.Width = im.w; 32 this.Height = im.h; 33 this.Colors = im.pixels; 34 } 35 36 public override ubyte[] Compile() { 37 return pp_write_img("png"); 38 } 39 40 public void SavePng(string name) { 41 write_image(name~".png", this.Width, this.Height, this.Colors); 42 } 43 44 private ubyte[] pp_write_img(string extr) { 45 const(char)[] ext = extr; 46 47 void function(Writer, long, long, in ubyte[], long) write_image; 48 switch (ext) { 49 case "png": write_image = &write_png; break; 50 case "tga": write_image = &write_tga; break; 51 case "bmp": write_image = &write_bmp; break; 52 default: throw new ImageIOException("unknown image extension/type"); 53 } 54 scope writer = new MemWriter(); 55 write_image(writer, this.Width, this.Height, this.Colors, 0); 56 return writer.result; 57 } 58 }