-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathPixelToasterConversion.h
executable file
·610 lines (492 loc) · 19.1 KB
/
PixelToasterConversion.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
// Pixel Format Conversion Routines
// Copyright © 2004-2007 Glenn Fiedler
// Part of the PixelToaster Framebuffer Library - http://www.pixeltoaster.com
#ifndef PIXELTOASTER_CONVERSION_H
#define PIXELTOASTER_CONVERSION_H
#include "PixelToaster.h"
#ifndef PIXELTOASTER_NO_CRT
# include <memory.h>
#endif
#ifdef PIXELTOASTER_USE_SSE2
# include <emmintrin.h>
#endif
namespace PixelToaster {
// floating point tricks!
union FloatInteger
{
float f;
int i;
};
inline integer32 clamp_positive(integer32 value)
{
return (value - (value & (((int)value) >> 31)));
}
#ifdef PIXELTOASTER_USE_SSE2
inline __m128i clamp_positive(__m128i value)
{
auto x = _mm_srai_epi32(value, 31);
return _mm_andnot_si128(x, value);
}
#endif
inline integer32 clamped_fraction_8(float input)
{
FloatInteger value;
value.f = input;
value.i = clamp_positive(value.i);
if (value.i >= 0x3F7FFFFF)
return 0x07F8000;
value.f += 1.0f;
return value.i & 0x07F8000;
}
#ifdef PIXELTOASTER_USE_SSE2
inline __m128i clamped_fraction_8(__m128 input)
{
auto maskRet = _mm_set1_epi32(0x07F8000);
auto onei = _mm_set1_epi32(0x3F7FFFFF);
auto onef = _mm_castsi128_ps(onei);
auto xi = clamp_positive(_mm_castps_si128(input));
auto xf = _mm_castsi128_ps(xi);
auto mask = _mm_cmpgt_epi32(xi, onei);
// Compute either cases simultaneously and merge them later
auto y0 = _mm_and_si128(mask, onei);
auto y1 = _mm_andnot_si128(mask, _mm_castps_si128(_mm_add_ps(xf, onef)));
return _mm_and_si128(_mm_or_si128(y0, y1), maskRet);
}
#endif
inline integer32 clamped_fraction_6(float input)
{
FloatInteger value;
value.f = input;
value.i = clamp_positive(value.i);
if (value.i >= 0x3F7FFFFF)
return 0x07E0000;
value.f += 1.0f;
return value.i & 0x07E0000;
}
inline integer32 clamped_fraction_5(float input)
{
FloatInteger value;
value.f = input;
value.i = clamp_positive(value.i);
if (value.i >= 0x3F7FFFFF)
return 0x07C0000;
value.f += 1.0f;
return value.i & 0x07C0000;
}
inline float uint8ToFloat(integer8 input)
{
FloatInteger value;
value.i = input | (142L << 23);
value.f -= 32768.0f;
return value.f;
}
// floating point conversion routines
inline void convert_XBGRFFFF_to_XRGB8888(const Pixel source[], integer32 destination[], unsigned int count)
{
unsigned int offset = 0;
#ifdef PIXELTOASTER_USE_SSE2
integer32 output[16];
const unsigned int numBlocks = count / 4;
for (unsigned int i = 0; i < numBlocks; ++i)
{
__m128 p0 = _mm_load_ps(reinterpret_cast<const float*>(&source[4 * i + 0]));
__m128 p1 = _mm_load_ps(reinterpret_cast<const float*>(&source[4 * i + 1]));
__m128 p2 = _mm_load_ps(reinterpret_cast<const float*>(&source[4 * i + 2]));
__m128 p3 = _mm_load_ps(reinterpret_cast<const float*>(&source[4 * i + 3]));
__m128i f0 = clamped_fraction_8(p0);
__m128i f1 = clamped_fraction_8(p1);
__m128i f2 = clamped_fraction_8(p2);
__m128i f3 = clamped_fraction_8(p3);
_mm_store_si128(reinterpret_cast<__m128i*>(&output[0]), f0);
_mm_store_si128(reinterpret_cast<__m128i*>(&output[4]), f1);
_mm_store_si128(reinterpret_cast<__m128i*>(&output[8]), f2);
_mm_store_si128(reinterpret_cast<__m128i*>(&output[12]), f3);
const integer32 r0 = output[0] << 1;
const integer32 g0 = output[1] >> 7;
const integer32 b0 = output[2] >> 15;
const integer32 r1 = output[4] << 1;
const integer32 g1 = output[5] >> 7;
const integer32 b1 = output[6] >> 15;
const integer32 r2 = output[8] << 1;
const integer32 g2 = output[9] >> 7;
const integer32 b2 = output[10] >> 15;
const integer32 r3 = output[12] << 1;
const integer32 g3 = output[13] >> 7;
const integer32 b3 = output[14] >> 15;
destination[4 * i + 0] = r0 | g0 | b0;
destination[4 * i + 1] = r1 | g1 | b1;
destination[4 * i + 2] = r2 | g2 | b2;
destination[4 * i + 3] = r3 | g3 | b3;
}
offset += 4 * count;
#endif
for (unsigned int i = offset; i < count; ++i)
{
const integer32 r = clamped_fraction_8(source[i].r) << 1;
const integer32 g = clamped_fraction_8(source[i].g) >> 7;
const integer32 b = clamped_fraction_8(source[i].b) >> 15;
destination[i] = r | g | b;
}
}
inline void convert_XRGB8888_to_XBGRFFFF(const integer32 source[], Pixel destination[], unsigned int count)
{
for (unsigned int i = 0; i < count; ++i)
{
destination[i].r = uint8ToFloat(source[i] >> 16);
destination[i].g = uint8ToFloat(source[i] >> 8);
destination[i].b = uint8ToFloat(source[i]);
}
}
inline void convert_XBGRFFFF_to_XBGR8888(const Pixel source[], integer32 destination[], unsigned int count)
{
for (unsigned int i = 0; i < count; ++i)
{
const integer32 r = clamped_fraction_8(source[i].r) >> 15;
const integer32 g = clamped_fraction_8(source[i].g) >> 7;
const integer32 b = clamped_fraction_8(source[i].b) << 1;
destination[i] = r | g | b;
}
}
inline void convert_XBGR8888_to_XBGRFFFF(const integer32 source[], Pixel destination[], unsigned int count)
{
for (unsigned int i = 0; i < count; ++i)
{
destination[i].r = uint8ToFloat(source[i]);
destination[i].g = uint8ToFloat(source[i] >> 8);
destination[i].b = uint8ToFloat(source[i] >> 16);
}
}
inline void convert_XBGRFFFF_to_RGB888(const Pixel source[], integer8 destination[], unsigned int count)
{
for (unsigned int i = 0; i < count; ++i)
{
const integer32 r = clamped_fraction_8(source[i].r) >> 15;
const integer32 g = clamped_fraction_8(source[i].g) >> 15;
const integer32 b = clamped_fraction_8(source[i].b) >> 15;
destination[0] = (integer8)r;
destination[1] = (integer8)g;
destination[2] = (integer8)b;
destination += 3;
}
}
inline void convert_RGB888_to_XBGRFFFF(const integer8 source[], Pixel destination[], unsigned int count)
{
for (unsigned int i = 0; i < count; ++i)
{
destination[i].r = uint8ToFloat(source[0]);
destination[i].g = uint8ToFloat(source[1]);
destination[i].b = uint8ToFloat(source[2]);
source += 3;
}
}
inline void convert_XBGRFFFF_to_BGR888(const Pixel source[], integer8 destination[], unsigned int count)
{
for (unsigned int i = 0; i < count; ++i)
{
const integer32 r = clamped_fraction_8(source[i].r) >> 15;
const integer32 g = clamped_fraction_8(source[i].g) >> 15;
const integer32 b = clamped_fraction_8(source[i].b) >> 15;
destination[0] = (integer8)b;
destination[1] = (integer8)g;
destination[2] = (integer8)r;
destination += 3;
}
}
inline void convert_BGR888_to_XBGRFFFF(const integer8 source[], Pixel destination[], unsigned int count)
{
for (unsigned int i = 0; i < count; ++i)
{
destination[i].r = uint8ToFloat(source[2]);
destination[i].g = uint8ToFloat(source[1]);
destination[i].b = uint8ToFloat(source[0]);
source += 3;
}
}
inline void convert_XBGRFFFF_to_RGB565(const Pixel source[], integer16 destination[], unsigned int count)
{
for (unsigned int i = 0; i < count; ++i)
{
const integer32 r = clamped_fraction_5(source[i].r) >> 7;
const integer32 g = clamped_fraction_6(source[i].g) >> 12;
const integer32 b = clamped_fraction_5(source[i].b) >> 18;
destination[i] = (integer16)(r | g | b);
}
}
inline void convert_RGB565_to_XBGRFFFF(const integer16 source[], Pixel destination[], unsigned int count)
{
for (unsigned int i = 0; i < count; ++i)
{
const auto color = (integer32)source[i];
const integer8 r = (color & 0x0000F800) >> 8;
const integer8 g = (color & 0x000007E0) >> 3;
const integer8 b = (color & 0x0000001F) << 3;
destination[i].r = uint8ToFloat(r);
destination[i].g = uint8ToFloat(g);
destination[i].b = uint8ToFloat(b);
}
}
inline void convert_XBGRFFFF_to_BGR565(const Pixel source[], integer16 destination[], unsigned int count)
{
for (unsigned int i = 0; i < count; ++i)
{
const integer32 r = clamped_fraction_5(source[i].r) >> 18;
const integer32 g = clamped_fraction_6(source[i].g) >> 12;
const integer32 b = clamped_fraction_5(source[i].b) >> 7;
destination[i] = (integer16)(r | g | b);
}
}
inline void convert_BGR565_to_XBGRFFFF(const integer16 source[], Pixel destination[], unsigned int count)
{
for (unsigned int i = 0; i < count; ++i)
{
const auto color = (integer32)source[i];
const integer8 b = (color & 0x0000F800) >> 8;
const integer8 g = (color & 0x000007E0) >> 3;
const integer8 r = (color & 0x0000001F) << 3;
destination[i].r = uint8ToFloat(r);
destination[i].g = uint8ToFloat(g);
destination[i].b = uint8ToFloat(b);
}
}
inline void convert_XBGRFFFF_to_XRGB1555(const Pixel source[], integer16 destination[], unsigned int count)
{
for (unsigned int i = 0; i < count; ++i)
{
const integer32 r = clamped_fraction_5(source[i].r) >> 8;
const integer32 g = clamped_fraction_5(source[i].g) >> 13;
const integer32 b = clamped_fraction_5(source[i].b) >> 18;
destination[i] = (integer16)(r | g | b);
}
}
inline void convert_XRGB1555_to_XBGRFFFF(const integer16 source[], Pixel destination[], unsigned int count)
{
for (unsigned int i = 0; i < count; ++i)
{
const auto color = (integer32)source[i];
const integer8 r = (color & 0x00007C00) >> 7;
const integer8 g = (color & 0x000003E0) >> 2;
const integer8 b = (color & 0x0000001F) << 3;
destination[i].r = uint8ToFloat(r);
destination[i].g = uint8ToFloat(g);
destination[i].b = uint8ToFloat(b);
}
}
inline void convert_XBGRFFFF_to_XBGR1555(const Pixel source[], integer16 destination[], unsigned int count)
{
for (unsigned int i = 0; i < count; ++i)
{
const integer32 r = clamped_fraction_5(source[i].r) >> 18;
const integer32 g = clamped_fraction_5(source[i].g) >> 13;
const integer32 b = clamped_fraction_5(source[i].b) >> 8;
destination[i] = (integer16)(r | g | b);
}
}
inline void convert_XBGR1555_to_XBGRFFFF(const integer16 source[], Pixel destination[], unsigned int count)
{
for (unsigned int i = 0; i < count; ++i)
{
const auto color = (integer32)source[i];
const integer8 b = (color & 0x00007C00) >> 7;
const integer8 g = (color & 0x000003E0) >> 2;
const integer8 r = (color & 0x0000001F) << 3;
destination[i].r = uint8ToFloat(r);
destination[i].g = uint8ToFloat(g);
destination[i].b = uint8ToFloat(b);
}
}
// integer to integer converters
inline void convert_XRGB8888_to_XBGR8888(const integer32 source[], integer32 destination[], unsigned int count)
{
for (unsigned int i = 0; i < count; ++i)
{
integer32 r = (source[i] & 0x00FF0000) >> 16;
integer32 g = (source[i] & 0x0000FF00);
integer32 b = (source[i] & 0x000000FF) << 16;
destination[i] = r | g | b;
}
}
inline void convert_XBGR8888_to_XRGB8888(const integer32 source[], integer32 destination[], unsigned int count)
{
convert_XRGB8888_to_XBGR8888(source, destination, count);
}
inline void convert_XRGB8888_to_RGB888(const integer32 source[], integer8 destination[], unsigned int count)
{
for (unsigned int i = 0; i < count; ++i)
{
destination[0] = (integer8)((source[i] & 0x00FF0000) >> 16);
destination[1] = (integer8)((source[i] & 0x0000FF00) >> 8);
destination[2] = (integer8)((source[i] & 0x000000FF));
destination += 3;
}
}
inline void convert_RGB888_to_XRGB8888(const integer8 source[], integer32 destination[], unsigned int count)
{
for (unsigned int i = 0; i < count; ++i)
{
integer32 r = source[0];
integer32 g = source[1];
integer32 b = source[2];
destination[i] = (r << 16) | (g << 8) | b;
source += 3;
}
}
inline void convert_XRGB8888_to_BGR888(const integer32 source[], integer8 destination[], unsigned int count)
{
for (unsigned int i = 0; i < count; ++i)
{
destination[0] = (integer8)((source[i] & 0x000000FF));
destination[1] = (integer8)((source[i] & 0x0000FF00) >> 8);
destination[2] = (integer8)((source[i] & 0x00FF0000) >> 16);
destination += 3;
}
}
inline void convert_BGR888_to_XRGB8888(const integer8 source[], integer32 destination[], unsigned int count)
{
for (unsigned int i = 0; i < count; ++i)
{
integer32 r = source[0];
integer32 g = source[1];
integer32 b = source[2];
destination[i] = (b << 16) | (g << 8) | r;
source += 3;
}
}
inline void convert_XRGB8888_to_RGB565(const integer32 source[], integer16 destination[], unsigned int count)
{
for (unsigned int i = 0; i < count; ++i)
{
integer32 r = (source[i] & 0x00F80000) >> 8;
integer32 g = (source[i] & 0x0000FC00) >> 5;
integer32 b = (source[i] & 0x000000F8) >> 3;
destination[i] = (integer16)(r | g | b);
}
}
inline void convert_RGB565_to_XRGB8888(const integer16 source[], integer32 destination[], unsigned int count)
{
for (unsigned int i = 0; i < count; ++i)
{
auto color = (integer32)source[i];
integer32 r = (color & 0x0000F800) << 8;
integer32 g = (color & 0x000007E0) << 5;
integer32 b = (color & 0x0000001F) << 3;
destination[i] = r | g | b;
}
}
inline void convert_XRGB8888_to_BGR565(const integer32 source[], integer16 destination[], unsigned int count)
{
for (unsigned int i = 0; i < count; ++i)
{
integer32 r = (source[i] & 0x00F80000) >> 19;
integer32 g = (source[i] & 0x0000FC00) >> 5;
integer32 b = (source[i] & 0x000000F8) << 8;
destination[i] = (integer16)(r | g | b);
}
}
inline void convert_BGR565_to_XRGB8888(const integer16 source[], integer32 destination[], unsigned int count)
{
for (unsigned int i = 0; i < count; ++i)
{
auto color = (integer32)source[i];
integer32 r = ((color & 0x0000F800) << 8) >> 16;
integer32 g = ((color & 0x000007E0) << 5);
integer32 b = ((color & 0x0000001F) << 3) << 16;
destination[i] = r | g | b;
}
}
inline void convert_XRGB8888_to_XRGB1555(const integer32 source[], integer16 destination[], unsigned int count)
{
for (unsigned int i = 0; i < count; ++i)
{
integer32 r = (source[i] & 0x00F80000) >> 9;
integer32 g = (source[i] & 0x0000F800) >> 6;
integer32 b = (source[i] & 0x000000F8) >> 3;
destination[i] = (integer16)(r | g | b);
}
}
inline void convert_XRGB1555_to_XRGB8888(const integer16 source[], integer32 destination[], unsigned int count)
{
for (unsigned int i = 0; i < count; ++i)
{
auto color = (integer32)source[i];
integer32 r = (color & 0x00007C00) << 9;
integer32 g = (color & 0x000003E0) << 6;
integer32 b = (color & 0x0000001F) << 3;
destination[i] = r | g | b;
}
}
inline void convert_XRGB8888_to_XBGR1555(const integer32 source[], integer16 destination[], unsigned int count)
{
for (unsigned int i = 0; i < count; ++i)
{
integer32 r = (source[i] & 0x00F80000) >> 19;
integer32 g = (source[i] & 0x0000F800) >> 6;
integer32 b = (source[i] & 0x000000F8) << 7;
destination[i] = (integer16)(r | g | b);
}
}
inline void convert_XBGR1555_to_XRGB8888(const integer16 source[], integer32 destination[], unsigned int count)
{
for (unsigned int i = 0; i < count; ++i)
{
auto color = (integer32)source[i];
integer32 r = (color & 0x00007C00) >> 7;
integer32 g = (color & 0x000003E0) << 6;
integer32 b = (color & 0x0000001F) << 19;
destination[i] = r | g | b;
}
}
// copy converters
inline void convert_XRGB8888_to_XRGB8888(const integer32 source[], integer32 destination[], unsigned int count)
{
#ifndef PIXELTOASTER_NO_CRT
memcpy(destination, source, count * 4);
#else
for (unsigned int i = 0; i < count; ++i)
destination[i] = source[i];
#endif
}
inline void convert_XBGRFFFF_to_XBGRFFFF(const Pixel source[], Pixel destination[], unsigned int count)
{
#ifndef PIXELTOASTER_NO_CRT
memcpy(destination, source, count * 16);
#else
for (unsigned int i = 0; i < count; ++i)
destination[i] = source[i];
#endif
}
// declare set of converter classes
class ConverterAdapter : public Converter
{
virtual void begin() override {}
virtual void end() override {}
};
#define PIXELTOASTER_CONVERTER(type, source_type, destination_type) \
\
class Converter_##type : public ConverterAdapter \
{ \
void convert(const void* source, void* destination, int pixels) \
{ \
convert_##type((const source_type*)source, (destination_type*)destination, pixels); \
} \
};
PIXELTOASTER_CONVERTER(XBGRFFFF_to_XBGRFFFF, Pixel, Pixel);
PIXELTOASTER_CONVERTER(XBGRFFFF_to_XRGB8888, Pixel, integer32);
PIXELTOASTER_CONVERTER(XBGRFFFF_to_XBGR8888, Pixel, integer32);
PIXELTOASTER_CONVERTER(XBGRFFFF_to_RGB888, Pixel, integer8);
PIXELTOASTER_CONVERTER(XBGRFFFF_to_BGR888, Pixel, integer8);
PIXELTOASTER_CONVERTER(XBGRFFFF_to_RGB565, Pixel, integer16);
PIXELTOASTER_CONVERTER(XBGRFFFF_to_BGR565, Pixel, integer16);
PIXELTOASTER_CONVERTER(XBGRFFFF_to_XRGB1555, Pixel, integer16);
PIXELTOASTER_CONVERTER(XBGRFFFF_to_XBGR1555, Pixel, integer16);
PIXELTOASTER_CONVERTER(XRGB8888_to_XBGRFFFF, integer32, Pixel);
PIXELTOASTER_CONVERTER(XRGB8888_to_XRGB8888, integer32, integer32);
PIXELTOASTER_CONVERTER(XRGB8888_to_XBGR8888, integer32, integer32);
PIXELTOASTER_CONVERTER(XRGB8888_to_RGB888, integer32, integer8);
PIXELTOASTER_CONVERTER(XRGB8888_to_BGR888, integer32, integer8);
PIXELTOASTER_CONVERTER(XRGB8888_to_RGB565, integer32, integer16);
PIXELTOASTER_CONVERTER(XRGB8888_to_BGR565, integer32, integer16);
PIXELTOASTER_CONVERTER(XRGB8888_to_XRGB1555, integer32, integer16);
PIXELTOASTER_CONVERTER(XRGB8888_to_XBGR1555, integer32, integer16);
#undef PIXELTOASTER_CONVERTER
} // namespace PixelToaster
#endif