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