1 module daque.graphics.image;
2 
3 import daque.math.linear;
4 
5 /++
6 	Represents an array of pixels
7 +/
8 class Image
9 {
10 public:
11 	/++
12 		Creates a new empty image with the specified width and height and optional fill
13 
14 		params:
15 		width = width of the new image
16 		height = height of the new image
17 		fill = color to fill the new image
18 	+/
19 	this(uint width, uint height, uint fill = 0xffffffff)
20 	{
21 		m_width = width;
22 		m_height = height;
23 
24 		m_pixel.length = m_width;
25 		for (uint i; i < m_width; i++)
26 		{
27 			m_pixel[i].length = m_height;
28 
29 			for (uint j; j < m_height; j++)
30 			{
31 				m_pixel[i][j] = fill;
32 			}
33 		}
34 	}
35 
36 	/++
37 		Gets a reference to the pixel located at position (x, y)
38 
39 		params:
40 		x = x position of pixel from left to right
41 		y = y position of pixel from top to bottom
42 	+/
43 	ref uint opIndex(uint x, uint y)
44 	{
45 		return m_pixel[x][y];
46 	}
47 
48 	/++
49 		Gets a linear representation of the image according to the matrixOrder rule
50 		that is, if matrixOrder is RowMajor, then the image is given row by row
51 		if matrixOrder is ColumnMajor, then the image is given column by column
52 	+/
53 	uint[] linearize(MatrixOrder matrixOrder)()
54 	{
55 		uint[] linearization;
56 
57 		static if (matrixOrder == MatrixOrder.RowMajor)
58 		{
59 			for (uint y; y < m_height; y++)
60 			{
61 				for (uint x; x < m_width; x++)
62 				{
63 					linearization ~= this[x, y];
64 				}
65 			}
66 		}
67 		static if (matrixOrder == MatrixOrder.ColumnMajor)
68 		{
69 			for (uint x; x < m_width; x++)
70 			{
71 				for (uint y; y < m_height; y++)
72 				{
73 					linearization ~= this[x, y];
74 				}
75 			}
76 		}
77 		return linearization;
78 	}
79 
80 private:
81 	uint[][] m_pixel;
82 	const uint m_width, m_height;
83 }