1 module pixelatrix.bpp8;
2 
3 import pixelatrix.common;
4 import siryul;
5 
6 /++
7 + 8 bit per pixel tile format with palette. Each row has its bitplanes stored
8 + adjacent to one another. Commonly used by the SNES.
9 +/
10 align(1) struct Linear8BPP {
11 	align(1):
12 	ubyte[8 * 8] raw;
13 	@SerializationMethod
14 	string toBase64() const @safe {
15 		import std.base64 : Base64;
16 		return Base64.encode(raw[]);
17 	}
18 	ubyte[8][8] pixelMatrix() const @safe pure
19 		out(result; result.isValidBitmap!8)
20 	{
21 		ubyte[8][8] output;
22 		foreach (x; 0..8) {
23 			output[x] = raw[x * 8 .. (x * 8) + 8];
24 		}
25 		return output;
26 	}
27 }
28 ///
29 @safe pure unittest {
30 	//import std.string : representation;
31 	//const data = Intertwined4BPP(import("bpp4-sample1.bin").representation[0 .. 8 * 4]);
32 	//const ubyte[8][8] finaldata = [
33 	//	[0x0, 0xF, 0x2, 0xE, 0xE, 0xE, 0xF, 0xA],
34 	//	[0x0, 0x0, 0xF, 0x6, 0xE, 0xE, 0xE, 0xE],
35 	//	[0x0, 0x0, 0xF, 0xF, 0xF, 0x8, 0xF, 0xF],
36 	//	[0x0, 0xF, 0xF, 0xF, 0x8, 0x8, 0x8, 0xF],
37 	//	[0x0, 0xF, 0x8, 0xF, 0x7, 0x7, 0xF, 0x7],
38 	//	[0x0, 0xF, 0x8, 0x7, 0x7, 0x7, 0xF, 0x7],
39 	//	[0x0, 0x0, 0xF, 0x8, 0x9, 0x9, 0x7, 0x7],
40 	//	[0x0, 0x0, 0x0, 0xF, 0x9, 0x9, 0xF, 0xA]
41 	//];
42 	//assert(data.pixelMatrix() == finaldata);
43 }
44 /++
45 + 8 bit per pixel tile format with palette. Each row has its bitplanes stored
46 + adjacent to one another. Commonly used by the SNES.
47 +/
48 align(1) struct Intertwined8BPP {
49 	align(1):
50 	ubyte[8 * 8] raw;
51 	@SerializationMethod
52 	string toBase64() const @safe {
53 		import std.base64 : Base64;
54 		return Base64.encode(raw[]);
55 	}
56 	ubyte[8][8] pixelMatrix() const @safe pure
57 		out(result; result.isValidBitmap!8)
58 	{
59 		ubyte[8][8] output;
60 		foreach (x; 0..8) {
61 			foreach (y; 0..8) {
62 				output[x][7-y] = cast(ubyte)
63 					(((raw[(8 * 0) + x*2]&(1<<y))>>y) +
64 					(((raw[(8 * 0) + x*2+1]&(1<<y))>>y)<<1) +
65 					(((raw[(8 * 2) + x*2]&(1<<y))>>y)<<2) +
66 					(((raw[(8 * 2) + x*2 + 1]&(1<<y))>>y)<<3) +
67 					(((raw[(8 * 4) + x*2]&(1<<y))>>y)<<4) +
68 					(((raw[(8 * 4) + x*2 + 1]&(1<<y))>>y)<<5) +
69 					(((raw[(8 * 6) + x*2]&(1<<y))>>y)<<6) +
70 					(((raw[(8 * 6) + x*2 + 1]&(1<<y))>>y)<<7)
71 				);
72 			}
73 		}
74 		return output;
75 	}
76 }
77 ///
78 @safe pure unittest {
79 	//import std.string : representation;
80 	//const data = Intertwined4BPP(import("bpp4-sample1.bin").representation[0 .. 8 * 4]);
81 	//const ubyte[8][8] finaldata = [
82 	//	[0x0, 0xF, 0x2, 0xE, 0xE, 0xE, 0xF, 0xA],
83 	//	[0x0, 0x0, 0xF, 0x6, 0xE, 0xE, 0xE, 0xE],
84 	//	[0x0, 0x0, 0xF, 0xF, 0xF, 0x8, 0xF, 0xF],
85 	//	[0x0, 0xF, 0xF, 0xF, 0x8, 0x8, 0x8, 0xF],
86 	//	[0x0, 0xF, 0x8, 0xF, 0x7, 0x7, 0xF, 0x7],
87 	//	[0x0, 0xF, 0x8, 0x7, 0x7, 0x7, 0xF, 0x7],
88 	//	[0x0, 0x0, 0xF, 0x8, 0x9, 0x9, 0x7, 0x7],
89 	//	[0x0, 0x0, 0x0, 0xF, 0x9, 0x9, 0xF, 0xA]
90 	//];
91 	//assert(data.pixelMatrix() == finaldata);
92 }