1 module ws.file.freeimage; 2 3 4 import 5 std.string, 6 std.file, 7 ws.exception, 8 ws.testing, 9 derelict.freeimage.freeimage; 10 11 12 shared static this(){ 13 DerelictFI.load(); 14 } 15 16 17 class FIImage { 18 19 FREE_IMAGE_FORMAT type; 20 FIBITMAP* dib; 21 int width, height; 22 int colors, depth; 23 FREE_IMAGE_COLOR_TYPE format; 24 byte[] data; 25 26 this(string path){ 27 if(!exists(path)) 28 exception(path ~ " does not exist"); 29 type = FreeImage_GetFileType(path.toStringz); 30 dib = FreeImage_Load(type, path.toStringz); 31 assert(dib, path ~ " not a valid image file"); 32 format = FreeImage_GetColorType(dib); 33 assert(format == FIC_RGB || format == FIC_RGBALPHA); 34 colors = (format == FIC_RGB ? 3 : 4); 35 depth = FreeImage_GetBPP(dib)/colors; 36 width = FreeImage_GetWidth(dib); 37 height = FreeImage_GetHeight(dib); 38 39 assert(depth == 8); 40 41 int bytespp = FreeImage_GetLine(dib) / width; 42 for(int y = 0; y < height; y++) { 43 BYTE *bits = FreeImage_GetScanLine(dib, y); 44 for(int x = 0; x < width; x++) { 45 data ~= bits[FI_RGBA_RED]; 46 data ~= bits[FI_RGBA_GREEN]; 47 data ~= bits[FI_RGBA_BLUE]; 48 if(colors == 4) 49 data ~= bits[FI_RGBA_ALPHA]; 50 bits += bytespp; 51 } 52 } 53 54 } 55 56 ~this(){ 57 FreeImage_Unload(dib); 58 } 59 60 }