-
Notifications
You must be signed in to change notification settings - Fork 17
/
packing.h
202 lines (148 loc) · 4.92 KB
/
packing.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
Copyright (c) 2015 Harm Hanemaaijer <fgenfb@yahoo.com>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdint.h>
// Define some short functions for pixel packing/unpacking. The compiler will take care of optimization by
// inlining and removing unused functions.
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ || !defined(__BYTE_ORDER__)
static unsigned int pack_rgba(int r, int g, int b, int a) {
return (unsigned int)r | ((unsigned int)g << 8) | ((unsigned int)b << 16) |
((unsigned int)a << 24);
}
static unsigned int pack_rgb_alpha_0xff(int r, int g, int b) {
return pack_rgba(r, g, b, 0xFF);
}
static unsigned int pack_r(int r) {
return (unsigned int)r;
}
static unsigned int pack_g(int g) {
return (unsigned int)g << 8;
}
static unsigned int pack_b(int b) {
return (unsigned int)b << 16;
}
static unsigned int pack_a(int a) {
return (unsigned int)a << 24;
}
static unsigned int pack_half_float(int r16, int g16) {
return (unsigned int)r16 | ((unsigned int)g16 << 16);
}
static uint64_t pack_r16(unsigned int r16) {
return r16;
}
static uint64_t pack_g16(unsigned int g16) {
return g16 << 16;
}
static uint64_t pack_b16(unsigned int b16) {
return (uint64_t)b16 << 32;
}
static uint64_t pack_a16(unsigned int a16) {
return (uint64_t)a16 << 48;
}
static uint64_t pack_rgb16(uint16_t r16, uint16_t g16, uint16_t b16) {
return (uint64_t)r16 | ((uint64_t)g16 << 16) | ((uint64_t)b16 << 32);
}
static uint64_t pack_rgba16(uint16_t r16, uint16_t g16, uint16_t b16, uint16_t a16) {
return (uint64_t)r16 | ((uint64_t)g16 << 16) | ((uint64_t)b16 << 32) | ((uint64_t)a16 << 48);
}
static int pixel_get_r(unsigned int pixel) {
return pixel & 0xFF;
}
static int pixel_get_g(unsigned int pixel) {
return (pixel & 0xFF00) >> 8;
}
static int pixel_get_b(unsigned int pixel) {
return (pixel & 0xFF0000) >> 16;
}
static int pixel_get_a(unsigned int pixel) {
return (pixel & 0xFF000000) >> 24;
}
static int pixel_get_signed_r8(unsigned int pixel) {
return (char)(pixel & 0xFF);
}
static int pixel_get_signed_g8(unsigned int pixel) {
return (char)((pixel & 0xFF00) >> 8);
}
static unsigned int pixel_get_r16(unsigned int pixel) {
return pixel & 0x0000FFFF;
}
static unsigned int pixel_get_g16(unsigned int pixel) {
return (pixel & 0xFFFF0000) >> 16;
}
static int pixel_get_signed_r16(unsigned int pixel) {
return (short)(pixel & 0xFFFF);
}
static int pixel_get_signed_g16(unsigned int pixel) {
return (short)((pixel & 0xFFFF0000) >> 16);
}
static uint64_t pixel64_get_r16(uint64_t pixel) {
return pixel & 0xFFFF;
}
static uint64_t pixel64_get_g16(uint64_t pixel) {
return (pixel & 0xFFFF0000) >> 16;
}
static uint64_t pixel64_get_b16(uint64_t pixel) {
return (pixel & 0xFFFF00000000) >> 32;
}
static uint64_t pixel64_get_a16(uint64_t pixel) {
return (pixel & 0xFFFF000000000000) >> 48;
}
#define alpha_byte_offset 3
#else
static unsigned int pack_rgba(int r, int g, int b, int a) {
return a | ((unsigned int)b << 8) | ((unsigned int)g << 16) | ((unsigned int )r << 24);
}
static unsigned int pack_rgb_alpha_0xff(int r, int g, int b) {
return pack_rgba(r, g, b, 0xFF);
}
static unsigned int pack_r(int r) {
return (unsigned int)r << 24;
}
static unsigned int pack_g(int g) {
return (unsigned int)g << 16;
}
static unsigned int pack_b(int b) {
return (unsigned int)b << 8;
}
static unsigned int pack_a(int a) {
return a;
}
static unsigned int pack_half_float(int r16, int g16) {
return (unsigned int)g16 | ((unsigned int)r16 << 16);
}
static int pixel_get_r(unsigned int pixel) {
return (pixel & 0xFF000000) >> 24;
}
static int pixel_get_g(unsigned int pixel) {
return (pixel & 0xFF0000) >> 16;
}
static int pixel_get_b(unsigned int pixel) {
return (pixel & 0xFF00) >> 8;
}
static int pixel_get_a(unsigned int pixel) {
return pixel & 0xFF;
}
static unsigned int pixel_get_r16(unsigned int pixel) {
return ((pixel & 0xFF000000) >> 24) | ((pixel & 0x00FF0000) >> 8);
}
static unsigned int pixel_get_g16(unsigned int pixel) {
return ((pixel & 0x0000FF00) >> 8) | ((pixel & 0x000000FF) << 8);
}
static int pixel_get_signed_r16(unsigned int pixel) {
return (short)(((pixel & 0xFF000000) >> 24) | ((pixel & 0x00FF0000) >> 8));
}
static int pixel_get_signed_g16(unsigned int pixel) {
return (short)(((pixel & 0x0000FF00) >> 8) | ((pixel & 0x000000FF) << 8));
}
#define alpha_byte_offset 0
#endif