1 module gfm.image; 2 3 public import gfm.image.stb_truetype; 4 5 import imageformats; 6 import ae.utils.graphics.image; 7 import ae.utils.graphics.color; 8 9 /// The one function you probably want to use. 10 /// Loads an image from a static array. 11 /// Might throw internally. 12 /// Throws: $(D ImageIOException) on error. 13 Image!RGBA loadImage(in void[] imageData) 14 { 15 IFImage ifImage = read_image_from_mem(cast(const(ubyte[])) imageData, 4); 16 int width = cast(int)ifImage.w; 17 int height = cast(int)ifImage.h; 18 19 Image!RGBA loaded; 20 loaded.size(width, height); 21 loaded.pixels = cast(RGBA[]) ifImage.pixels; // no pixel copy, GC does the job 22 return loaded; 23 } 24 25 deprecated("Use loadImage instead") alias stbiLoadImageAE = loadImage; 26 27 28 /// Loads two different images: 29 /// - the first the 2nd is interpreted as greyscale 30 /// and fetch in the alpha channel of the result. 31 Image!RGBA loadImageSeparateAlpha(in void[] imageDataRGB, in void[] imageDataAlpha) 32 { 33 IFImage ifImageRGB = read_image_from_mem(cast(const(ubyte[])) imageDataRGB, 3); 34 int widthRGB = cast(int)ifImageRGB.w; 35 int heightRGB = cast(int)ifImageRGB.h; 36 37 IFImage ifImageA = read_image_from_mem(cast(const(ubyte[])) imageDataAlpha, 1); 38 int widthA = cast(int)ifImageA.w; 39 int heightA = cast(int)ifImageA.h; 40 41 if ( (widthA != widthRGB) || (heightRGB != heightA) ) 42 { 43 throw new ImageIOException("Image size mismatch"); 44 } 45 46 int width = widthA; 47 int height = heightA; 48 49 Image!RGBA loaded; 50 loaded.size(width, height); 51 52 for (int j = 0; j < height; ++j) 53 { 54 RGB* rgbscan = cast(RGB*)(&ifImageRGB.pixels[3 * (j * width)]); 55 ubyte* ascan = &ifImageA.pixels[j * width]; 56 RGBA[] outscan = loaded.scanline(j); 57 for (int i = 0; i < width; ++i) 58 { 59 RGB rgb = rgbscan[i]; 60 outscan[i] = RGBA(rgb.r, rgb.g, rgb.b, ascan[i]); 61 } 62 } 63 return loaded; 64 } 65 66