1 module armos.graphics.pixel;
2 /++
3     カラーフォーマットを表します.
4 +/
5 enum ColorFormat{
6     Gray, 
7     GrayAlpha, 
8     RGB,
9     BGR,
10     RGBA,
11     BGRA, 
12     Depth, 
13 }
14 
15 /++
16     カラーフォーマットのパラメータの数を返します.
17 +/
18 int numColorFormatElements(in ColorFormat ColorFormat){
19     int num;
20     switch (ColorFormat) {
21         case ColorFormat.Gray:
22             num = 1;
23             break;
24         case ColorFormat.GrayAlpha:
25             num = 2;
26             break;
27         case ColorFormat.RGB:
28             num = 3;
29             break;
30         case ColorFormat.BGR:
31             num = 3;
32             break;
33         case ColorFormat.RGBA:
34             num = 4;
35             break;
36         case ColorFormat.BGRA:
37             num = 4;
38             break;
39         case ColorFormat.Depth:
40             num = GL_DEPTH_COMPONENT;
41             break;
42         default : assert(0, "case is not defined");
43     }
44     return num;
45 }
46 
47 import derelict.opengl3.gl;
48 /++
49     ColorFormatを元にGLで用いられる定数を返します
50 +/
51 GLuint getGLInternalFormat(in ColorFormat format){
52     GLuint num;
53     switch(format){
54         case ColorFormat.Gray:
55             num = GL_RED;
56             break;
57         case ColorFormat.GrayAlpha:
58             num = GL_RG;
59             break;
60         case ColorFormat.RGB:
61             num = GL_RGB;
62             break;
63         case ColorFormat.BGR:
64             num = GL_BGR;
65             break;
66         case ColorFormat.RGBA:
67             num = GL_RGBA;
68             break;
69         case ColorFormat.BGRA:
70             num = GL_BGRA;
71             break;
72         case ColorFormat.Depth:
73             num = GL_DEPTH_COMPONENT;
74             break;
75         default: 
76             num = GL_RED;
77             break;
78     }
79     return num;
80 }
81 
82 /++
83     画素を表すstructです.
84 +/
85 struct Pixel(T){
86     public{
87         /++
88             +/
89             this(in ColorFormat colorType){
90                 _colorType = colorType;
91                 _elements = new T[](numColorFormatElements(colorType));
92             }
93         unittest{
94             assert(__traits(compiles, (){
95                         auto p = Pixel!float(ColorFormat.RGB);
96                         }));
97 
98             assert(__traits(compiles, (){
99                         const p = Pixel!float(ColorFormat.RGB);
100                         }));
101 
102             assert(!__traits(compiles, (){
103                         immutable p = Pixel!float(ColorFormat.RGB);
104                         }));
105         }
106 
107         /++
108             +/
109             void element(in Pixel!(T) px){
110                 for (int i = 0; i < _elements.length; i++) {
111                     _elements[i] = px.element(i);
112                 }
113                 // _elements = px.element;
114             }
115 
116         /++
117             +/
118             void element(in int index, in T level)
119             in{
120                 assert(index>=0);
121                 assert(index<_elements.length);
122             }body{
123                 _elements[index] = level;
124             }
125 
126         /++
127             +/
128             T element(in int index)const
129             in{
130                 assert(index>=0);
131                 assert(index<_elements.length);
132             }body{
133                 return _elements[index];
134             }
135         unittest{
136             auto p = Pixel!float(ColorFormat.RGB);
137             p.element(0, 1.0f);
138             p.element(1, 0.5f);
139             p.element(2, 0.1f);
140             assert(p.element(0) == 1.0f);
141             assert(p.element(1) == 0.5f);
142             assert(p.element(2) == 0.1f);
143         }
144 
145         /++
146             +/
147             int numElements()const{
148                 return numColorFormatElements(_colorType);
149             }
150         unittest{
151             auto p = Pixel!float(ColorFormat.RGB);
152             assert(p.numElements == 3);
153         }
154     }
155 
156     private{
157         T[] _elements;
158         ColorFormat _colorType;
159     }
160 }
161