1 module caLib_util.initialConditions;
2 
3 import std.stdio : writeln;
4 import core.stdc.stdlib : exit;
5 import std.exception : Exception;
6 import caLib_util.image : Image, Color;
7 
8 struct InitialConditionFromImage(CellStateType = Color)
9 {
10 
11 private:
12 
13 	alias Ct = CellStateType;
14 
15 	Image image;
16 
17 	Color[Color] corectionLUT;
18 
19 public:
20 
21 	this(string imagePath, int latticeWidth, int latticeHeight)
22 	{
23 		this(imagePath, latticeHeight, latticeHeight, null);
24 	}
25 
26 	this(string imagePath, int latticeWidth, int latticeHeight, Color[Color] corectionLUT)
27 	in
28 	{ assert(latticeWidth >= 0 && latticeHeight >= 0); }
29 	body
30 	{
31 		this.corectionLUT = corectionLUT;
32 
33 		try
34 		{
35 			image = Image.fromFile(imagePath);
36 			image.rescale(latticeWidth, latticeHeight);
37 		}
38 		catch(Exception e)
39 		{
40 			writeln("An error occured while reading image: " ~ imagePath);
41 			writeln("Error message: ", e.msg,);
42 			writeln("A empty image will be used as the initialCondition instead");
43 
44 			image = new Image(latticeWidth, latticeWidth);
45 		}
46 	}
47 
48 	Ct initialCondition(int x, int y)
49 	{
50 		if(corectionLUT == null)
51 			return cast(Ct) image.getPixel(x, y);
52 		else
53 			return cast(Ct) corectionLUT.get(image.getPixel(x, y), image.getPixel(x, y));
54 	}
55 }