KrisLibrary  1.0.0
formats.h
1 
2 typedef unsigned int COLOROPTYPE [4];
3 typedef void (*PIXELGETPROC) (const unsigned char* bits, COLOROPTYPE col);
4 typedef void (*PIXELSETPROC) (unsigned char* bits, const COLOROPTYPE col);
5 
6 #include <memory.h>
7 
8 struct r5g6b5
9 {
10  unsigned int r :5;
11  unsigned int g :6;
12  unsigned int b :5;
13 };
14 
15 struct x1r5g5b5
16 {
17  unsigned int x :1;
18  unsigned int r :5;
19  unsigned int g :5;
20  unsigned int b :5;
21 };
22 
23 
24 void argb_get (const unsigned char* bits, COLOROPTYPE col)
25 {
26  col[0] = *bits;
27  col[1] = *(bits+1);
28  col[2] = *(bits+2);
29  col[3] = *(bits+3);
30 }
31 
32 void argb_set (unsigned char* bits, const COLOROPTYPE col)
33 {
34  *bits = col[0];
35  *(bits+1) = col[1];
36  *(bits+2) = col[2];
37  *(bits+3) = col[3];
38 }
39 
40 void rgb8_get (const unsigned char* bits, COLOROPTYPE col)
41 {
42  col[0] = *bits;
43  col[1] = *(bits+1);
44  col[2] = *(bits+2);
45 }
46 
47 void rgb8_set (unsigned char* bits, const COLOROPTYPE col)
48 {
49  *bits = col[0];
50  *(bits+1) = col[1];
51  *(bits+2) = col[2];
52 }
53 
54 void r5g6b5_get (const unsigned char* bits, COLOROPTYPE col)
55 {
56  r5g6b5 b;
57  memcpy(&b, bits, sizeof(short));
58  col[0] = b.r;
59  col[1] = b.g;
60  col[2] = b.b;
61 }
62 
63 void r5g6b5_set (unsigned char* bits, const COLOROPTYPE col)
64 {
65  r5g6b5 c;
66  c.r = col[0];
67  c.g = col[1];
68  c.b = col[2];
69  memcpy(bits, &c, sizeof(short));
70 }
71 
72 void x1r5g5b5_get (const unsigned char* bits, COLOROPTYPE col)
73 {
74  x1r5g5b5 c;
75  memcpy(&c, bits, sizeof(short));
76  col[0] = c.r;
77  col[1] = c.g;
78  col[2] = c.b;
79 }
80 
81 void x1r5g5b5_set (unsigned char* bits, const COLOROPTYPE col)
82 {
83  x1r5g5b5 c;
84  c.r = col[0];
85  c.g = col[1];
86  c.b = col[2];
87  memcpy(bits, &c, sizeof(short));
88 }
89 
90 void a8_get (const unsigned char* bits, COLOROPTYPE col)
91 {
92  col[0] = *bits;
93  col[1] = col[2] = col[0];
94 }
95 
96 void a8_set (unsigned char* bits, const COLOROPTYPE col)
97 {
98  *bits = col[0];
99 }
100 
101 
102 const static float one_over_255 = 1.0/255.0;
103 
104 void frgb_get (const unsigned char* bits, COLOROPTYPE col)
105 {
106  const float* fbits = (const float*)bits;
107  col[0] = (unsigned int)(fbits[0]*255.0);
108  col[1] = (unsigned int)(fbits[1]*255.0);
109  col[2] = (unsigned int)(fbits[2]*255.0);
110 }
111 
112 void frgb_set (unsigned char* bits, const COLOROPTYPE col)
113 {
114  float* fbits = (float*)bits;
115  fbits[0] = float(col[0])*one_over_255;
116  fbits[1] = float(col[1])*one_over_255;
117  fbits[2] = float(col[2])*one_over_255;
118 }
119 
120 void frgba_get (const unsigned char* bits, COLOROPTYPE col)
121 {
122  const float* fbits = (const float*)bits;
123  col[0] = (unsigned int)(fbits[0]*255.0);
124  col[1] = (unsigned int)(fbits[1]*255.0);
125  col[2] = (unsigned int)(fbits[2]*255.0);
126  col[3] = (unsigned int)(fbits[3]*255.0);
127 }
128 
129 void frgba_set (unsigned char* bits, const COLOROPTYPE col)
130 {
131  float* fbits = (float*)bits;
132  fbits[0] = float(col[0])*one_over_255;
133  fbits[1] = float(col[1])*one_over_255;
134  fbits[2] = float(col[2])*one_over_255;
135  fbits[3] = float(col[3])*one_over_255;
136 }
137 
138 
139 void fa_get (const unsigned char* bits, COLOROPTYPE col)
140 {
141  const float* fbits = (const float*)bits;
142  col[0] = (unsigned int)(fbits[0]*255.0);
143  col[1] = col[2] = col[0];
144 }
145 
146 void fa_set (unsigned char* bits, const COLOROPTYPE col)
147 {
148  float* fbits = (float*)bits;
149  fbits[0] = float(col[0])*one_over_255;
150 }
151 
152 
153 inline void zero_color(COLOROPTYPE x)
154 {
155  x[0] = x[1] = x[2] = x[3] = 0;
156 }
157 
158 inline void add_color(COLOROPTYPE x, const COLOROPTYPE a)
159 {
160  x[0]+=a[0];
161  x[1]+=a[1];
162  x[2]+=a[2];
163  x[3]+=a[3];
164 }
165 
166 inline void shift_right_color(COLOROPTYPE x, unsigned int shift)
167 {
168  x[0] = x[0] >> shift;
169  x[1] = x[1] >> shift;
170  x[2] = x[2] >> shift;
171  x[3] = x[3] >> shift;
172 }
Definition: formats.h:15
Definition: formats.h:8