1 // Written in the D programming language. 2 3 /** 4 This module contains image loading functions. 5 6 Currently uses FreeImage. 7 8 Usage of libpng is not feasible under linux due to conflicts of library and binding versions. 9 10 Synopsis: 11 12 ---- 13 import dlangui.graphics.images; 14 15 ---- 16 17 Copyright: Vadim Lopatin, 2014 18 License: Boost License 1.0 19 Authors: Vadim Lopatin, coolreader.org@gmail.com 20 */ 21 module dlangui.graphics.images; 22 23 public import dlangui.core.config; 24 static if (BACKEND_GUI): 25 26 import arsd.image; 27 28 import dlangui.core.logger; 29 import dlangui.core.types; 30 import dlangui.graphics.colors; 31 import dlangui.graphics.drawbuf; 32 import dlangui.core.streams; 33 import std.path; 34 import std.conv : to; 35 36 37 /// load and decode image from file to ColorDrawBuf, returns null if loading or decoding is failed 38 ColorDrawBuf loadImage(string filename) { 39 static import std.file; 40 try { 41 immutable ubyte[] data = cast(immutable ubyte[])std.file.read(filename); 42 return loadImage(data, filename); 43 } catch (Exception e) { 44 Log.e("exception while loading image from file ", filename); 45 Log.e(to!string(e)); 46 return null; 47 } 48 } 49 50 /// load and decode image from input stream to ColorDrawBuf, returns null if loading or decoding is failed 51 ColorDrawBuf loadImage(immutable ubyte[] data, string filename) { 52 Log.d("Loading image from file " ~ filename); 53 54 import std.algorithm : endsWith; 55 if (filename.endsWith(".xpm")) { 56 import dlangui.graphics.xpm.reader : parseXPM; 57 try { 58 return parseXPM(data); 59 } 60 catch(Exception e) { 61 Log.e("Failed to load image from file ", filename); 62 Log.e(to!string(e)); 63 return null; 64 } 65 } 66 67 auto image = loadImageFromMemory(data); 68 ColorDrawBuf buf = new ColorDrawBuf(image.width, image.height); 69 for(int j = 0; j < buf.height; j++) 70 { 71 auto scanLine = buf.scanLine(j); 72 for(int i = 0; i < buf.width; i++) 73 { 74 auto color = image.getPixel(i, j); 75 scanLine[i] = makeRGBA(color.r, color.g, color.b, 255 - color.a); 76 } 77 } 78 return buf; 79 }