1 module pixelatrix.common;
2 
3 bool isValidBitmap(size_t size)(const ubyte[8][8] input) {
4 	foreach (row; input) {
5 		foreach (pixel; row) {
6 			if (pixel > (1<<size))  {
7 				return false;
8 			}
9 		}
10 	}
11 	return true;
12 }
13 
14 enum TileFormat {
15 	simple1BPP,
16 	linear2BPP,
17 	intertwined2BPP,
18 	intertwined3BPP,
19 	intertwined4BPP,
20 	gba4BPP,
21 	linear8BPP,
22 	intertwined8BPP,
23 }
24 
25 size_t colours(const TileFormat format) @safe pure {
26 	final switch(format) {
27 		case TileFormat.simple1BPP:
28 			return 2^^1;
29 		case TileFormat.linear2BPP:
30 			return 2^^2;
31 		case TileFormat.intertwined2BPP:
32 			return 2^^2;
33 		case TileFormat.intertwined3BPP:
34 			return 2^^3;
35 		case TileFormat.intertwined4BPP:
36 			return 2^^4;
37 		case TileFormat.gba4BPP:
38 			return 2^^4;
39 		case TileFormat.intertwined8BPP:
40 			return 2^^8;
41 		case TileFormat.linear8BPP:
42 			return 2^^8;
43 	}
44 }
45 
46 ubyte[8][8] pixelMatrix(const ubyte[] data, TileFormat format) @safe pure {
47 	import pixelatrix.bpp1 : Simple1BPP;
48 	import pixelatrix.bpp2 : Linear2BPP, Intertwined2BPP;
49 	import pixelatrix.bpp3 : Intertwined3BPP;
50 	import pixelatrix.bpp4 : Intertwined4BPP, GBA4BPP;
51 	import pixelatrix.bpp8 : Linear8BPP, Intertwined8BPP;
52 	static ubyte[8][8] fromFormat(T)(const ubyte[] data) {
53 		return T(data[0 .. T.sizeof]).pixelMatrix;
54 	}
55 	final switch (format) {
56 		case TileFormat.simple1BPP:
57 			return fromFormat!Simple1BPP(data);
58 		case TileFormat.linear2BPP:
59 			return fromFormat!Linear2BPP(data);
60 		case TileFormat.intertwined2BPP:
61 			return fromFormat!Intertwined2BPP(data);
62 		case TileFormat.intertwined3BPP:
63 			return fromFormat!Intertwined3BPP(data);
64 		case TileFormat.intertwined4BPP:
65 			return fromFormat!Intertwined4BPP(data);
66 		case TileFormat.intertwined8BPP:
67 			return fromFormat!Intertwined8BPP(data);
68 		case TileFormat.linear8BPP:
69 			return fromFormat!Linear8BPP(data);
70 		case TileFormat.gba4BPP:
71 			return fromFormat!GBA4BPP(data);
72 	}
73 }
74 
75 ubyte[8][8][] pixelMatrices(const ubyte[] data, TileFormat format) @safe pure {
76 	import pixelatrix.bpp1 : Simple1BPP;
77 	import pixelatrix.bpp2 : Linear2BPP, Intertwined2BPP;
78 	import pixelatrix.bpp3 : Intertwined3BPP;
79 	import pixelatrix.bpp4 : Intertwined4BPP, GBA4BPP;
80 	import pixelatrix.bpp8 : Linear8BPP, Intertwined8BPP;
81 	static ubyte[8][8][] fromFormat(T)(const ubyte[] data) {
82 		auto output = new ubyte[8][8][](data.length / T.sizeof);
83 		foreach (idx, ref tile; output) {
84 			tile = T(data[idx * T.sizeof .. (idx + 1) * T.sizeof][0 .. T.sizeof]).pixelMatrix;
85 		}
86 		return output;
87 	}
88 	final switch (format) {
89 		case TileFormat.simple1BPP:
90 			return fromFormat!Simple1BPP(data);
91 		case TileFormat.linear2BPP:
92 			return fromFormat!Linear2BPP(data);
93 		case TileFormat.intertwined2BPP:
94 			return fromFormat!Intertwined2BPP(data);
95 		case TileFormat.intertwined3BPP:
96 			return fromFormat!Intertwined3BPP(data);
97 		case TileFormat.intertwined4BPP:
98 			return fromFormat!Intertwined4BPP(data);
99 		case TileFormat.intertwined8BPP:
100 			return fromFormat!Intertwined8BPP(data);
101 		case TileFormat.linear8BPP:
102 			return fromFormat!Linear8BPP(data);
103 		case TileFormat.gba4BPP:
104 			return fromFormat!GBA4BPP(data);
105 	}
106 }