forked from mattiasgustavsson/libs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
paldither.h
682 lines (556 loc) · 19.4 KB
/
paldither.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
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
/*
------------------------------------------------------------------------------
Licensing information can be found at the end of the file.
------------------------------------------------------------------------------
paldither.h - v0.1 - Convert true-color image to custom palette, with dither.
Do this:
#define PALDITHER_IMPLEMENTATION
before you include this file in *one* C/C++ file to create the implementation.
*/
#ifndef paldither_h
#define paldither_h
#define _CRT_NONSTDC_NO_DEPRECATE
#define _CRT_SECURE_NO_WARNINGS
#include <stddef.h>
#ifndef PALDITHER_U32
#define PALDITHER_U32 unsigned int
#endif
#ifndef PALDITHER_U8
#define PALDITHER_U8 unsigned char
#endif
typedef enum paldither_type_t
{
PALDITHER_TYPE_DEFAULT,
PALDITHER_TYPE_BAYER,
PALDITHER_TYPE_NONE,
} paldither_type_t;
typedef struct paldither_palette_t
{
void* memctx;
int color_count;
PALDITHER_U32 colortable[ 256 ];
} paldither_palette_t;
paldither_palette_t* paldither_palette_create( PALDITHER_U32 const* xbgr, int count, size_t* palette_size, void* memctx );
paldither_palette_t* paldither_palette_create_from_data( void const* data, size_t size, void* memctx );
void paldither_palette_destroy( paldither_palette_t* palette );
void paldither_palettize( PALDITHER_U32* abgr, int width, int height, paldither_palette_t const* palette,
paldither_type_t dither_type, PALDITHER_U8* output );
#endif /* paldither_h */
/*
----------------------
IMPLEMENTATION
----------------------
*/
#ifdef PALDITHER_IMPLEMENTATION
#undef PALDITHER_IMPLEMENTATION
#ifndef PALDITHER_MALLOC
#define _CRT_NONSTDC_NO_DEPRECATE
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#if defined(__cplusplus)
#define PALDITHER_MALLOC( ctx, size ) ( ::malloc( size ) )
#define PALDITHER_FREE( ctx, ptr ) ( ::free( ptr ) )
#else
#define PALDITHER_MALLOC( ctx, size ) ( malloc( size ) )
#define PALDITHER_FREE( ctx, ptr ) ( free( ptr ) )
#endif
#endif
#ifndef PALDITHER_ASSERT
#undef _CRT_NONSTDC_NO_DEPRECATE
#define _CRT_NONSTDC_NO_DEPRECATE
#undef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#include <assert.h>
#define PALDITHER_ASSERT( expression, message ) assert( ( expression ) && ( message ) )
#endif
#include <stdlib.h>
typedef struct paldither_mix_t
{
int first;
int second;
int d;
unsigned char ratio;
unsigned char r;
unsigned char g;
unsigned char b;
unsigned char l;
} paldither_mix_t;
static void paldither_internal_mix( void* memctx, PALDITHER_U32 const* xbgr, int count, int mix_levels,
paldither_mix_t** out_mix, int* out_mix_count )
{
(void) memctx;
int mix_count = ( ( count / 2 ) * ( count + 1 ) ) * mix_levels;
paldither_mix_t* mix = (paldither_mix_t*) PALDITHER_MALLOC( memctx, mix_count * sizeof( paldither_mix_t ) );
int c = 0;
for( int i = 0; i < count; ++i )
{
PALDITHER_U32 fcolor = xbgr[ i ] & 0x00ffffff;
int fr = (int)( ( fcolor & 0x000000ff ) );
int fg = (int)( ( fcolor & 0x0000ff00 ) >> 8 );
int fb = (int)( ( fcolor & 0x00ff0000 ) >> 16 );
int fl = (int)( ( 54 * fr + 183 * fg + 19 * fb + 127 ) >> 8 );
PALDITHER_ASSERT( fl <= 0xff && fl >= 0, "Value out of range" );
for( int j = i + 1; j < count; ++j )
{
PALDITHER_U32 scolor = xbgr[ j ] & 0x00ffffff;
int sr = (int)( ( scolor & 0x000000ff ) );
int sg = (int)( ( scolor & 0x0000ff00 ) >> 8 );
int sb = (int)( ( scolor & 0x00ff0000 ) >> 16 );
int sl = (int)( ( 54 * sr + 183 * sg + 19 * sb + 127 ) >> 8 );
PALDITHER_ASSERT( sl <= 0xff && sl >= 0, "Value out of range" );
int dr = fr - sr;
int dg = fg - sg;
int db = fb - sb;
int dl = fl - sl;
int d = ( ( ( ( dr*dr + dg*dg + db*db ) >> 1 ) + dl*dl ) + 127 ) >> 8;
for( int k = 0; k < mix_levels; ++k )
{
int r = fr;
int g = fg;
int b = fb;
int l = fl;
if( mix_levels > 1 )
{
int s = mix_levels - 1;
int ik = s - k;
r = ( r * ik + sr * k + ( mix_levels / 2 ) ) / s;
g = ( g * ik + sg * k + ( mix_levels / 2 ) ) / s;
b = ( b * ik + sb * k + ( mix_levels / 2 ) ) / s;
l = ( 54 * r + 183 * g + 19 * b + 127 ) >> 8;
PALDITHER_ASSERT( r <= 0xff && g <= 0xff && b <= 0xff && r >= 0 && g >= 0 && b >= 0 && l <= 0xff && l >= 0, "Value out of range" );
}
mix[ c ].first = i;
mix[ c ].second = j;
mix[ c ].ratio = (unsigned char) k;
mix[ c ].r = (unsigned char) r;
mix[ c ].g = (unsigned char) g;
mix[ c ].b = (unsigned char) b;
mix[ c ].l = (unsigned char) l;
mix[ c ].d = d;
++c;
PALDITHER_ASSERT( mix_count >= c, "Out of range" );
}
}
}
*out_mix = mix;
*out_mix_count = c;
}
static void paldither_internal_list( void* memctx, paldither_mix_t* mix, int mix_count,
int out_map[ 16 * 16 * 16 ], int** out_list, int* out_list_count )
{
(void) memctx;
int cube[ 17 * 17 * 17 ];
for( int r = 0; r < 17; ++r )
{
for( int g = 0; g < 17; ++g )
{
for( int b = 0; b < 17; ++b )
{
int best_diff = 0x7FFFFFFF;
int best_mix = 0;
paldither_mix_t const* m = mix;
int l = ( 54 * (r * 16) + 183 * (g * 16) + 19 * (b * 16) + 127 ) >> 8;
for( int i = 0; i < mix_count; ++i, ++m )
{
int dr = r * 16 - m->r;
int dg = g * 16 - m->g;
int db = b * 16 - m->b;
int dl = l - m->l;
int d = ( ( ( dr*dr + dg*dg + db*db ) >> 1 ) + dl*dl) + ( m->d * 3 );
if( i == 0 || d < best_diff ) { best_diff = d; best_mix = i; }
}
cube[ r * 17 * 17 + g * 17 + b ] = best_mix;
}
}
}
int map[ 16 * 16 * 16 ];
int list_capacity = mix_count * 256;
int* list = (int*) PALDITHER_MALLOC( memctx, list_capacity * sizeof( int ) );
int list_count = 0;
int* mapptr = map;
for( int r = 0; r < 16; ++r )
{
for( int g = 0; g < 16; ++g )
{
for( int b = 0; b < 16; ++b )
{
paldither_mix_t const* m = mix;
*mapptr++ = list_count;
if( list_count == list_capacity )
{
list_capacity *= 2;
int* new_list = (int*) PALDITHER_MALLOC( memctx, list_capacity * sizeof( int ) );
memcpy( new_list, list, list_count * sizeof( int ) );
PALDITHER_FREE( memctx, list );
list = new_list;
}
int count_index = list_count++;
list[ count_index ] = 0;
for( int i = 0; i < mix_count; ++i, ++m )
{
paldither_mix_t const* best_mix = &mix[ cube[ ( r ) * 17 * 17 + ( g ) * 17 + ( b ) ] ];
paldither_mix_t const* best_mix2 = &mix[ cube[ ( r + 1 ) * 17 * 17 + ( g + 1 ) * 17 + ( b + 1 ) ] ];
int pass =
( m->r >= best_mix->r && m->r <= best_mix2->r )
&& ( m->g >= best_mix->g && m->g <= best_mix2->g )
&& ( m->b >= best_mix->b && m->b <= best_mix2->b );
if( pass )
{
list[ count_index ]++;
if( list_count == list_capacity )
{
list_capacity *= 2;
int* new_list = (int*) PALDITHER_MALLOC( memctx, list_capacity * sizeof( int ) );
memcpy( new_list, list, list_count * sizeof( int ) );
PALDITHER_FREE( memctx, list );
list = new_list;
}
list[ list_count++ ] = i;
}
}
}
}
}
memcpy( out_map, map, sizeof( map ) );
*out_list = list;
*out_list_count = list_count;
}
paldither_palette_t* paldither_palette_create( PALDITHER_U32 const* xbgr, int count, size_t* palette_size, void* memctx )
{
(void) memctx;
int default_mix_count;
paldither_mix_t* default_mix;
paldither_internal_mix( memctx, xbgr, count, 11, &default_mix, &default_mix_count );
int default_map[ 16 * 16 * 16 ];
int* default_list;
int default_list_count = 0;
paldither_internal_list( memctx, default_mix, default_mix_count, default_map, &default_list, &default_list_count );
int bayer_mix_count;
paldither_mix_t* bayer_mix;
paldither_internal_mix( memctx, xbgr, count, 17, &bayer_mix, &bayer_mix_count );
int bayer_map[ 16 * 16 * 16 ];
int* bayer_list;
int bayer_list_count = 0;
paldither_internal_list( memctx, bayer_mix, bayer_mix_count, bayer_map, &bayer_list, &bayer_list_count );
int nodither_mix_count;
paldither_mix_t* nodither_mix;
paldither_internal_mix( memctx, xbgr, count, 1, &nodither_mix, &nodither_mix_count );
int nodither_map[ 16 * 16 * 16 ];
int* nodither_list;
int nodither_list_count = 0;
paldither_internal_list( memctx, nodither_mix, nodither_mix_count, nodither_map, &nodither_list, &nodither_list_count );
size_t size = sizeof( paldither_palette_t ) +
sizeof( int ) + default_list_count * sizeof( int ) +
sizeof( int ) + default_mix_count * sizeof( paldither_mix_t ) +
sizeof( default_map ) +
sizeof( int ) + bayer_list_count * sizeof( int ) +
sizeof( int ) + bayer_mix_count * sizeof( paldither_mix_t ) +
sizeof( bayer_map ) +
sizeof( int ) + nodither_list_count * sizeof( int ) +
sizeof( int ) + nodither_mix_count * sizeof( paldither_mix_t ) +
sizeof( nodither_map );
paldither_palette_t* palette = (paldither_palette_t*) PALDITHER_MALLOC( memctx, size );
palette->memctx = memctx;
palette->color_count = count;
memcpy( palette->colortable, xbgr, sizeof( *xbgr ) * count );
uintptr_t dest = (uintptr_t)( palette + 1) ;
*(int*) dest = default_mix_count; dest += sizeof( int );
memcpy( (void*)dest, default_mix, default_mix_count * sizeof( paldither_mix_t ) );
dest += default_mix_count * sizeof( paldither_mix_t );
memcpy( (void*)dest, default_map, sizeof( default_map ) );
dest += sizeof( default_map );
*(int*) dest = default_list_count; dest += sizeof( int );
memcpy( (void*)dest, default_list, default_list_count * sizeof( int ) );
dest += default_list_count * sizeof( int );
PALDITHER_FREE( memctx, default_list );
PALDITHER_FREE( memctx, default_mix );
*(int*) dest = bayer_mix_count; dest += sizeof( int );
memcpy( (void*)dest, bayer_mix, bayer_mix_count * sizeof( paldither_mix_t ) );
dest += bayer_mix_count * sizeof( paldither_mix_t );
memcpy( (void*)dest, bayer_map, sizeof( bayer_map ) );
dest += sizeof( bayer_map );
*(int*) dest = bayer_list_count; dest += sizeof( int );
memcpy( (void*)dest, bayer_list, bayer_list_count * sizeof( int ) );
dest += bayer_list_count * sizeof( int );
PALDITHER_FREE( memctx, bayer_list );
PALDITHER_FREE( memctx, bayer_mix );
*(int*) dest = nodither_mix_count; dest += sizeof( int );
memcpy( (void*)dest, nodither_mix, nodither_mix_count * sizeof( paldither_mix_t ) );
dest += nodither_mix_count * sizeof( paldither_mix_t );
memcpy( (void*)dest, nodither_map, sizeof( nodither_map ) );
dest += sizeof( nodither_map );
*(int*) dest = nodither_list_count; dest += sizeof( int );
memcpy( (void*)dest, nodither_list, nodither_list_count * sizeof( int ) );
dest += nodither_list_count * sizeof( int );
PALDITHER_FREE( memctx, nodither_list );
PALDITHER_FREE( memctx, nodither_mix );
if( palette_size ) *palette_size = size;
return palette;
}
paldither_palette_t* paldither_palette_create_from_data( void const* data, size_t size, void* memctx )
{
paldither_palette_t* palette = (paldither_palette_t*)PALDITHER_MALLOC( memctx, size );
palette->memctx = memctx;
memcpy( &palette->color_count, data, size );
return palette;
}
void paldither_palette_destroy( paldither_palette_t* palette )
{
PALDITHER_FREE( palette->memctx, palette );
}
void paldither_palettize( PALDITHER_U32* abgr, int width, int height, paldither_palette_t const* palette,
paldither_type_t dither_type, PALDITHER_U8* output )
{
unsigned char default_dither_pattern[ 4 * 4 * 11 ] =
{
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,1,
1,0,1,0,
0,1,0,0,
1,0,1,0,
0,0,0,1,
1,0,1,0,
0,1,0,1,
1,0,1,0,
0,1,0,1,
1,0,1,0,
1,1,0,1,
1,0,1,0,
0,1,1,1,
1,1,1,1,
1,1,1,1,
1,1,1,1,
1,1,1,1,
1,1,1,1,
1,1,1,1,
1,1,1,1,
1,1,1,1,
1,1,1,1,
1,1,1,1,
1,1,1,1,
1,1,1,1,
1,1,1,1,
1,1,1,1,
1,1,1,1,
1,1,1,1,
};
unsigned char bayer_dither_pattern[ 4 * 4 * 17 ] =
{
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
1, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
1, 0, 0, 0,
0, 0, 0, 0,
0, 0, 1, 0,
0, 0, 0, 0,
1, 0, 1, 0,
0, 0, 0, 0,
0, 0, 1, 0,
0, 0, 0, 0,
1, 0, 1, 0,
0, 0, 0, 0,
1, 0, 1, 0,
0, 0, 0, 0,
1, 0, 1, 0,
0, 1, 0, 0,
1, 0, 1, 0,
0, 0, 0, 0,
1, 0, 1, 0,
0, 1, 0, 0,
1, 0, 1, 0,
0, 0, 0, 1,
1, 0, 1, 0,
0, 1, 0, 1,
1, 0, 1, 0,
0, 0, 0, 1,
1, 0, 1, 0,
0, 1, 0, 1,
1, 0, 1, 0,
0, 1, 0, 1,
1, 1, 1, 0,
0, 1, 0, 1,
1, 0, 1, 0,
0, 1, 0, 1,
1, 1, 1, 0,
0, 1, 0, 1,
1, 0, 1, 1,
0, 1, 0, 1,
1, 1, 1, 1,
0, 1, 0, 1,
1, 0, 1, 1,
0, 1, 0, 1,
1, 1, 1, 1,
0, 1, 0, 1,
1, 1, 1, 1,
0, 1, 0, 1,
1, 1, 1, 1,
1, 1, 0, 1,
1, 1, 1, 1,
0, 1, 0, 1,
1, 1, 1, 1,
1, 1, 0, 1,
1, 1, 1, 1,
0, 1, 1, 1,
1, 1, 1, 1,
1, 1, 1, 1,
1, 1, 1, 1,
0, 1, 1, 1,
1, 1, 1, 1,
1, 1, 1, 1,
1, 1, 1, 1,
1, 1, 1, 1,
};
unsigned char none_dither_pattern[ 4 * 4 * 2 ] =
{
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
};
uintptr_t ptr = (uintptr_t)( palette + 1 );
int default_pal_mix_count = *(int*)ptr; ptr += sizeof( int );
paldither_mix_t const* default_pal_mix = (paldither_mix_t*) ptr; ptr += default_pal_mix_count * sizeof( paldither_mix_t );
int const* default_pal_map = (int*) ptr; ptr += 16 * 16 * 16 * sizeof( int );
int default_pal_list_count = *(int*)ptr; ptr += sizeof( int );
int const* default_pal_list = (int*) ptr; ptr += default_pal_list_count * sizeof( int );
int bayer_pal_mix_count = *(int*)ptr; ptr += sizeof( int );
paldither_mix_t const* bayer_pal_mix = (paldither_mix_t*) ptr; ptr += bayer_pal_mix_count * sizeof( paldither_mix_t );
int const* bayer_pal_map = (int*) ptr; ptr += 16 * 16 * 16 * sizeof( int );
int bayer_pal_list_count = *(int*)ptr; ptr += sizeof( int );
int const* bayer_pal_list = (int*) ptr; ptr += bayer_pal_list_count * sizeof( int );
int nodither_pal_mix_count = *(int*)ptr; ptr += sizeof( int );
paldither_mix_t const* nodither_pal_mix = (paldither_mix_t*) ptr; ptr += nodither_pal_mix_count * sizeof( paldither_mix_t );
int const* nodither_pal_map = (int*) ptr; ptr += 16 * 16 * 16 * sizeof( int );
int nodither_pal_list_count = *(int*)ptr; ptr += sizeof( int );
int const* nodither_pal_list = (int*) ptr; ptr += nodither_pal_list_count * sizeof( int );
unsigned char* dither_pattern = default_dither_pattern;
int pal_mix_count = default_pal_mix_count;
paldither_mix_t const* pal_mix = default_pal_mix;
int const* pal_map = default_pal_map;
int const* pal_list = default_pal_list;
if( dither_type == PALDITHER_TYPE_BAYER )
{
dither_pattern = bayer_dither_pattern;
pal_mix_count = bayer_pal_mix_count;
pal_mix = bayer_pal_mix;
pal_map = bayer_pal_map;
pal_list = bayer_pal_list;
}
else if( dither_type == PALDITHER_TYPE_NONE )
{
dither_pattern = none_dither_pattern;
pal_mix_count = nodither_pal_mix_count;
pal_mix = nodither_pal_mix;
pal_map = nodither_pal_map;
pal_list = nodither_pal_list;
}
for( int y = 0; y < height; ++y )
{
for( int x = 0; x < width; ++x )
{
PALDITHER_U32 color = abgr[ x + y * width ];
int r = (int)( ( color & 0x000000ff ) );
int g = (int)( ( color & 0x0000ff00 ) >> 8 );
int b = (int)( ( color & 0x00ff0000 ) >> 16 );
int l = (int)( ( 54 * r + 183 * g + 19 * b + 127 ) >> 8 );
PALDITHER_ASSERT( l <= 0xff && l >= 0, "Value out of range" );
paldither_mix_t const* best_mix = 0;
int pal_index = pal_map[ ( r >> 4 ) * 16 * 16 + ( g >> 4 ) * 16 + ( b >> 4 ) ];
int count = pal_list[ pal_index++ ];
if( count != 0 )
{
int best_diff = 0x7FFFFFFF;
int const* index = &pal_list[ pal_index ];
for( int i = 0; i < count; ++i, ++index )
{
paldither_mix_t const* m = &pal_mix[ *index ];
int dr = r - m->r;
int dg = g - m->g;
int db = b - m->b;
int dl = l - m->l;
int d = ( ( ( dr*dr + dg*dg + db*db ) >> 1 ) + dl*dl) + ( m->d * 3 );
if( i == 0 || d < best_diff ) { best_diff = d; best_mix = m; }
}
}
else
{
int best_diff = 0x7FFFFFFF;
paldither_mix_t const* m = pal_mix;
for( int i = 0; i < pal_mix_count; ++i, ++m )
{
int dr = r - m->r;
int dg = g - m->g;
int db = b - m->b;
int dl = l - m->l;
int d = ( ( ( dr*dr + dg*dg + db*db ) >> 1 ) + dl*dl) + ( m->d * 3 );
if( i == 0 || d < best_diff ) { best_diff = d; best_mix = m; }
}
}
int index = dither_pattern[ best_mix->ratio * 4 * 4 + ( x & 3 ) + ( y & 3 ) * 4 ] ?
best_mix->second : best_mix->first;
if( output) output[ x + y * width ] = (PALDITHER_U8) index;
abgr[ x + y * width ] = ( abgr[ x + y * width ] & 0xff000000 ) | ( palette->colortable[ index ] & 0x00ffffff );
}
}
}
#endif /* PALDITHER_IMPLEMENTATION */
/*
------------------------------------------------------------------------------
This software is available under 2 licenses - you may choose the one you like.
------------------------------------------------------------------------------
ALTERNATIVE A - MIT License
Copyright (c) 2015 Mattias Gustavsson
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
------------------------------------------------------------------------------
ALTERNATIVE B - Public Domain (www.unlicense.org)
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
software, either in source code form or as a compiled binary, for any purpose,
commercial or non-commercial, and by any means.
In jurisdictions that recognize copyright laws, the author or authors of this
software dedicate any and all copyright interest in the software to the public
domain. We make this dedication for the benefit of the public at large and to
the detriment of our heirs and successors. We intend this dedication to be an
overt act of relinquishment in perpetuity of all present and future rights to
this software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
------------------------------------------------------------------------------
*/