forked from NatronGitHub/Natron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lut.cpp
1728 lines (1546 loc) · 57.4 KB
/
Lut.cpp
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
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ***** BEGIN LICENSE BLOCK *****
* This file is part of Natron <https://natrongithub.github.io/>,
* (C) 2018-2021 The Natron developers
* (C) 2013-2018 INRIA and Alexandre Gauthier-Foichat
*
* Natron is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Natron is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Natron. If not, see <http://www.gnu.org/licenses/gpl-2.0.html>
* ***** END LICENSE BLOCK ***** */
// ***** BEGIN PYTHON BLOCK *****
// from <https://docs.python.org/3/c-api/intro.html#include-files>:
// "Since Python may define some pre-processor definitions which affect the standard headers on some systems, you must include Python.h before any standard headers are included."
#include <Python.h>
// ***** END PYTHON BLOCK *****
#include "Lut.h"
#include <cstring> // for std::memcpy
#include <algorithm> // min, max
#include <cassert>
#include <stdexcept>
#include "Engine/RectI.h"
/*
* The to_byte* and from_byte* functions implement and generalize the algorithm
* described in:
*
* http://dx.doi.org/10.1145/1242073.1242206 Spitzak, B. (2002, July). High-speed conversion of floating point images to 8-bit. In ACM SIGGRAPH 2002 conference abstracts and applications (pp. 193-193). ACM.
* https://spitzak.github.io/conversion/sketches_0265.pdf
*
* Related patents:
* http://www.google.com/patents/US5528741 "Method and apparatus for converting floating-point pixel values to byte pixel values by table lookup" (Caduc)
* http://www.google.com/patents/US20040100475 http://www.google.com/patents/US6999098 "Apparatus for converting floating point values to gamma corrected fixed point values "
* https://www.google.fr/patents/US7456845 https://www.google.fr/patents/US7405736 "Efficient perceptual/physical color space conversion "
*/
NATRON_NAMESPACE_ENTER
namespace Color {
// compile-time endianness checking found on:
// http://stackoverflow.com/questions/2100331/c-macro-definition-to-determine-big-endian-or-little-endian-machine
// if(O32_HOST_ORDER == O32_BIG_ENDIAN) will always be optimized by gcc -O2
enum
{
O32_LITTLE_ENDIAN = 0x03020100ul,
O32_BIG_ENDIAN = 0x00010203ul,
O32_PDP_ENDIAN = 0x01000302ul
};
static const union
{
uint8_t bytes[4];
uint32_t value;
}
o32_host_order = {
{ 0, 1, 2, 3 }
};
#define O32_HOST_ORDER (o32_host_order.value)
static unsigned short
hipart(const float f)
{
union
{
float f;
unsigned short us[2];
}
tmp;
tmp.us[0] = tmp.us[1] = 0;
tmp.f = f;
if (O32_HOST_ORDER == O32_BIG_ENDIAN) {
return tmp.us[0];
} else if (O32_HOST_ORDER == O32_LITTLE_ENDIAN) {
return tmp.us[1];
} else {
assert( (O32_HOST_ORDER == O32_LITTLE_ENDIAN) || (O32_HOST_ORDER == O32_BIG_ENDIAN) );
return 0;
}
}
static float
index_to_float(const unsigned short i)
{
union
{
float f;
unsigned short us[2];
}
tmp;
/* positive and negative zeros, and all gradual underflow, turn into zero: */
if ( ( i < 0x80) || ( ( i >= 0x8000) && ( i < 0x8080) ) ) {
return 0;
}
/* All NaN's and infinity turn into the largest possible legal float: */
if ( ( i >= 0x7f80) && ( i < 0x8000) ) {
return std::numeric_limits<float>::max();
}
if (i >= 0xff80) {
return -std::numeric_limits<float>::max();
}
if (O32_HOST_ORDER == O32_BIG_ENDIAN) {
tmp.us[0] = i;
tmp.us[1] = 0x8000;
} else if (O32_HOST_ORDER == O32_LITTLE_ENDIAN) {
tmp.us[0] = 0x8000;
tmp.us[1] = i;
} else {
assert( (O32_HOST_ORDER == O32_LITTLE_ENDIAN) || (O32_HOST_ORDER == O32_BIG_ENDIAN) );
}
return tmp.f;
}
///initialize the singleton
LutManager LutManager::m_instance = LutManager();
LutManager::LutManager()
: luts()
{
}
const Lut*
LutManager::getLut(const std::string & name,
fromColorSpaceFunctionV1 fromFunc,
toColorSpaceFunctionV1 toFunc)
{
LutsMap::iterator found = LutManager::m_instance.luts.find(name);
if ( found != LutManager::m_instance.luts.end() ) {
return found->second;
} else {
std::pair<LutsMap::iterator, bool> ret =
LutManager::m_instance.luts.insert( std::make_pair( name, new Lut(name, fromFunc, toFunc) ) );
assert(ret.second);
return ret.first->second;
}
return NULL;
}
LutManager::~LutManager()
{
////the luts must all have been released before!
////This is because the Lut holds a OFX::MultiThread::Mutex and it can't be deleted
//// by this singleton because it makes their destruction time uncertain regarding to
///the host multi-thread suite.
for (LutsMap::iterator it = luts.begin(); it != luts.end(); ++it) {
delete it->second;
}
}
static bool
clip(RectI* what,
const RectI & to)
{
return what->intersect(to, what);
}
#ifdef DEAD_CODE
static bool
intersects(const RectI & what,
const RectI & other)
{
return what.intersects(other);
}
#endif // DEAD_CODE
static void
getOffsetsForPacking(PixelPackingEnum format,
int *r,
int *g,
int *b,
int *a)
{
if (format == ePixelPackingBGRA) {
*b = 0;
*g = 1;
*r = 2;
*a = 3;
} else if (format == ePixelPackingRGBA) {
*r = 0;
*g = 1;
*b = 2;
*a = 3;
} else if (format == ePixelPackingRGB) {
*r = 0;
*g = 1;
*b = 2;
*a = -1;
} else if (format == ePixelPackingBGR) {
*r = 0;
*g = 1;
*b = 2;
*a = -1;
} else if (format == ePixelPackingPLANAR) {
*r = 0;
*g = 1;
*b = 2;
*a = -1;
} else {
*r = -1;
*g = -1;
*b = -1;
*a = -1;
throw std::runtime_error("Unsupported pixel packing format");
}
}
float
Lut::fromColorSpaceUint8ToLinearFloatFast(unsigned char v) const
{
assert(init_);
return fromFunc_uint8_to_float[v];
}
#ifdef DEAD_CODE
// It is not recommended to use this function, because the output is quantized
// If one really needs float, one has to use the full function (or OpenColorIO)
float
Lut::toColorSpaceFloatFromLinearFloatFast(float v) const
{
assert(init_);
return Color::intToFloat<0xff01>(toFunc_hipart_to_uint8xx[hipart(v)]);
}
#endif // DEAD_CODE
unsigned char
Lut::toColorSpaceUint8FromLinearFloatFast(float v) const
{
assert(init_);
return Color::uint8xxToChar(toFunc_hipart_to_uint8xx[hipart(v)]);
}
unsigned short
Lut::toColorSpaceUint8xxFromLinearFloatFast(float v) const
{
assert(init_);
return toFunc_hipart_to_uint8xx[hipart(v)];
}
// the following only works for increasing LUTs
unsigned short
Lut::toColorSpaceUint16FromLinearFloatFast(float v) const
{
assert(init_);
// algorithm:
// - convert to 8 bits -> val8u
// - convert val8u-1, val8u and val8u+1 to float
// - interpolate linearly in the right interval
unsigned char v8u = toColorSpaceUint8FromLinearFloatFast(v);
unsigned char v8u_next, v8u_prev;
float v32f_next, v32f_prev;
if (v8u == 0) {
v8u_prev = 0;
v8u_next = 1;
v32f_prev = fromColorSpaceUint8ToLinearFloatFast(0);
v32f_next = fromColorSpaceUint8ToLinearFloatFast(1);
} else if (v8u == 255) {
v8u_prev = 254;
v8u_next = 255;
v32f_prev = fromColorSpaceUint8ToLinearFloatFast(254);
v32f_next = fromColorSpaceUint8ToLinearFloatFast(255);
} else {
float v32f = fromColorSpaceUint8ToLinearFloatFast(v8u);
// we suppose the LUT is an increasing func
if (v < v32f) {
v8u_prev = v8u - 1;
v32f_prev = fromColorSpaceUint8ToLinearFloatFast(v8u_prev);
v8u_next = v8u;
v32f_next = v32f;
} else {
v8u_prev = v8u;
v32f_prev = v32f;
v8u_next = v8u + 1;
v32f_next = fromColorSpaceUint8ToLinearFloatFast(v8u_next);
}
}
// interpolate linearly
return (v8u_prev << 8) + v8u_prev + (v - v32f_prev) * ( ( (v8u_next - v8u_prev) << 8 ) + (v8u_next + v8u_prev) ) / (v32f_next - v32f_prev) + 0.5;
}
float
Lut::fromColorSpaceUint16ToLinearFloatFast(unsigned short v) const
{
assert(init_);
// the following is from ImageMagick's quantum.h
unsigned char v8u_prev = ( v - (v >> 8) ) >> 8;
unsigned char v8u_next = v8u_prev + 1;
unsigned short v16u_prev = (v8u_prev << 8) + v8u_prev;
unsigned short v16u_next = (v8u_next << 8) + v8u_next;
float v32f_prev = fromColorSpaceUint8ToLinearFloatFast(v8u_prev);
float v32f_next = fromColorSpaceUint8ToLinearFloatFast(v8u_next);
// interpolate linearly
return v32f_prev + (v - v16u_prev) * (v32f_next - v32f_prev) / (v16u_next - v16u_prev);
}
void
Lut::fillTables() const
{
if (init_) {
return;
}
// fill all
for (int i = 0; i < 0x10000; ++i) {
float inp = index_to_float( (unsigned short)i );
float f = _toFunc(inp);
toFunc_hipart_to_uint8xx[i] = Color::floatToInt<0xff01>(f);
}
// fill fromFunc_uint8_to_float, and make sure that
// the entries of toFunc_hipart_to_uint8xx corresponding
// to the transform of each byte value contain the same value,
// so that toFunc(fromFunc(b)) is identity
//
for (int b = 0; b < 256; ++b) {
float f = _fromFunc( Color::intToFloat<256>(b) );
fromFunc_uint8_to_float[b] = f;
int i = hipart(f);
toFunc_hipart_to_uint8xx[i] = Color::charToUint8xx(b);
}
}
#ifdef DEAD_CODE
void
Lut::to_byte_planar(unsigned char* to,
const float* from,
int W,
const float* alpha,
int inDelta,
int outDelta) const
{
validate();
unsigned char *end = to + W * outDelta;
// coverity[dont_call]
int start = rand() % W;
const float *q;
unsigned char *p;
unsigned error;
if (!alpha) {
/* go forwards from starting point to end of line: */
error = 0x80;
for (p = to + start * outDelta, q = from + start * inDelta; p < end; p += outDelta, q += inDelta) {
error = (error & 0xff) + toFunc_hipart_to_uint8xx[hipart(*q)];
*p = (unsigned char)(error >> 8);
}
/* go backwards from starting point to start of line: */
error = 0x80;
for (p = to + (start - 1) * outDelta, q = from + start * inDelta; p >= to; p -= outDelta) {
q -= inDelta;
error = (error & 0xff) + toFunc_hipart_to_uint8xx[hipart(*q)];
*p = (unsigned char)(error >> 8);
}
} else {
const float *a = alpha;
/* go forwards from starting point to end of line: */
error = 0x80;
for (p = to + start * outDelta, q = from + start * inDelta, a += start * inDelta; p < end; p += outDelta, q += inDelta, a += inDelta) {
const float v = *q * *a;
error = (error & 0xff) + toFunc_hipart_to_uint8xx[hipart(v)];
++a;
*p = (unsigned char)(error >> 8);
}
/* go backwards from starting point to start of line: */
error = 0x80;
for (p = to + (start - 1) * outDelta, q = from + start * inDelta, a = alpha + start * inDelta; p >= to; p -= outDelta) {
const float v = *q * *a;
q -= inDelta;
q -= inDelta;
error = (error & 0xff) + toFunc_hipart_to_uint8xx[hipart(v)];
*p = (unsigned char)(error >> 8);
}
}
}
#endif // DEAD_CODE
#ifdef DEAD_CODE
void
Lut::to_short_planar(unsigned short* /*to*/,
const float* /*from*/,
int /*W*/,
const float* /*alpha*/,
int /*inDelta*/,
int /*outDelta*/) const
{
throw std::runtime_error("Lut::to_short_planar not implemented yet.");
}
#endif // DEAD_CODE
void
Lut::to_float_planar(float* to,
const float* from,
int W,
const float* alpha,
int inDelta,
int outDelta) const
{
validate();
if (!alpha) {
for (int f = 0, t = 0; f < W; f += inDelta, t += outDelta) {
to[t] = toColorSpaceFloatFromLinearFloat(from[f]);
}
} else {
for (int f = 0, t = 0; f < W; f += inDelta, t += outDelta) {
to[t] = toColorSpaceFloatFromLinearFloat(from[f] * alpha[f]);
}
}
}
void
Lut::to_byte_packed(unsigned char* to,
const float* from,
const RectI & conversionRect,
const RectI & srcBounds,
const RectI & dstBounds,
PixelPackingEnum inputPacking,
PixelPackingEnum outputPacking,
bool invertY,
bool premult) const
{
///clip the conversion rect to srcBounds and dstBounds
RectI rect = conversionRect;
if ( !clip(&rect, srcBounds) || !clip(&rect, dstBounds) ) {
return;
}
bool inputHasAlpha = inputPacking == ePixelPackingBGRA || inputPacking == ePixelPackingRGBA;
bool outputHasAlpha = outputPacking == ePixelPackingBGRA || outputPacking == ePixelPackingRGBA;
int inROffset, inGOffset, inBOffset, inAOffset;
int outROffset, outGOffset, outBOffset, outAOffset;
getOffsetsForPacking(inputPacking, &inROffset, &inGOffset, &inBOffset, &inAOffset);
getOffsetsForPacking(outputPacking, &outROffset, &outGOffset, &outBOffset, &outAOffset);
int inPackingSize, outPackingSize;
inPackingSize = inputHasAlpha ? 4 : 3;
outPackingSize = outputHasAlpha ? 4 : 3;
validate();
for (int y = rect.y1; y < rect.y2; ++y) {
// coverity[dont_call]
int start = rand() % (rect.x2 - rect.x1) + rect.x1;
unsigned error_r, error_g, error_b;
error_r = error_g = error_b = 0x80;
int srcY = y;
if (!invertY) {
srcY = srcBounds.y2 - y - 1;
}
int dstY = dstBounds.y2 - y - 1;
const float *src_pixels = from + (srcY * (srcBounds.x2 - srcBounds.x1) * inPackingSize);
unsigned char *dst_pixels = to + (dstY * (dstBounds.x2 - dstBounds.x1) * outPackingSize);
/* go forwards from starting point to end of line: */
for (int x = start; x < rect.x2; ++x) {
int inCol = x * inPackingSize;
int outCol = x * outPackingSize;
float a = (inputHasAlpha && premult) ? src_pixels[inCol + inAOffset] : 1.f;
error_r = (error_r & 0xff) + toFunc_hipart_to_uint8xx[hipart(src_pixels[inCol + inROffset] * a)];
error_g = (error_g & 0xff) + toFunc_hipart_to_uint8xx[hipart(src_pixels[inCol + inGOffset] * a)];
error_b = (error_b & 0xff) + toFunc_hipart_to_uint8xx[hipart(src_pixels[inCol + inBOffset] * a)];
assert(error_r < 0x10000 && error_g < 0x10000 && error_b < 0x10000);
dst_pixels[outCol + outROffset] = (unsigned char)(error_r >> 8);
dst_pixels[outCol + outGOffset] = (unsigned char)(error_g >> 8);
dst_pixels[outCol + outBOffset] = (unsigned char)(error_b >> 8);
if (outputHasAlpha) {
// alpha is linear and should not be dithered
dst_pixels[outCol + outAOffset] = floatToInt<256>(a);
}
}
/* go backwards from starting point to start of line: */
error_r = error_g = error_b = 0x80;
for (int x = start - 1; x >= rect.x1; --x) {
int inCol = x * inPackingSize;
int outCol = x * outPackingSize;
float a = (inputHasAlpha && premult) ? src_pixels[inCol + inAOffset] : 1.f;
error_r = (error_r & 0xff) + toFunc_hipart_to_uint8xx[hipart(src_pixels[inCol + inROffset] * a)];
error_g = (error_g & 0xff) + toFunc_hipart_to_uint8xx[hipart(src_pixels[inCol + inGOffset] * a)];
error_b = (error_b & 0xff) + toFunc_hipart_to_uint8xx[hipart(src_pixels[inCol + inBOffset] * a)];
assert(error_r < 0x10000 && error_g < 0x10000 && error_b < 0x10000);
dst_pixels[outCol + outROffset] = (unsigned char)(error_r >> 8);
dst_pixels[outCol + outGOffset] = (unsigned char)(error_g >> 8);
dst_pixels[outCol + outBOffset] = (unsigned char)(error_b >> 8);
if (outputHasAlpha) {
// alpha is linear and should not be dithered
dst_pixels[outCol + outAOffset] = floatToInt<256>(a);
}
}
}
} // to_byte_packed
#ifdef DEAD_CODE
void
Lut::to_short_packed(unsigned short* /*to*/,
const float* /*from*/,
const RectI & /*conversionRect*/,
const RectI & /*srcBounds*/,
const RectI & /*dstBounds*/,
PixelPackingEnum /*inputPacking*/,
PixelPackingEnum /*outputPacking*/,
bool /*invertY*/,
bool /*premult*/) const
{
throw std::runtime_error("Lut::to_short_packed not implemented yet.");
}
#endif // DEAD_CODE
void
Lut::to_float_packed(float* to,
const float* from,
const RectI & conversionRect,
const RectI & srcBounds,
const RectI & dstBounds,
PixelPackingEnum inputPacking,
PixelPackingEnum outputPacking,
bool invertY,
bool premult) const
{
///clip the conversion rect to srcBounds and dstBounds
RectI rect = conversionRect;
if ( !clip(&rect, srcBounds) || !clip(&rect, dstBounds) ) {
return;
}
bool inputHasAlpha = inputPacking == ePixelPackingBGRA || inputPacking == ePixelPackingRGBA;
bool outputHasAlpha = outputPacking == ePixelPackingBGRA || outputPacking == ePixelPackingRGBA;
int inROffset, inGOffset, inBOffset, inAOffset;
int outROffset, outGOffset, outBOffset, outAOffset;
getOffsetsForPacking(inputPacking, &inROffset, &inGOffset, &inBOffset, &inAOffset);
getOffsetsForPacking(outputPacking, &outROffset, &outGOffset, &outBOffset, &outAOffset);
int inPackingSize, outPackingSize;
inPackingSize = inputHasAlpha ? 4 : 3;
outPackingSize = outputHasAlpha ? 4 : 3;
validate();
for (int y = rect.y1; y < rect.y2; ++y) {
int srcY = y;
if (invertY) {
srcY = srcBounds.y2 - y - 1;
}
int dstY = dstBounds.y2 - y - 1;
const float *src_pixels = from + (srcY * (srcBounds.x2 - srcBounds.x1) * inPackingSize);
float *dst_pixels = to + (dstY * (dstBounds.x2 - dstBounds.x1) * outPackingSize);
/* go forwards from starting point to end of line: */
for (int x = rect.x1; x < rect.x2; ++x) {
int inCol = x * inPackingSize;
int outCol = x * outPackingSize;
float a = (inputHasAlpha && premult) ? src_pixels[inCol + inAOffset] : 1.f;;
dst_pixels[outCol + outROffset] = toColorSpaceFloatFromLinearFloat(src_pixels[inCol + inROffset] * a);
dst_pixels[outCol + outGOffset] = toColorSpaceFloatFromLinearFloat(src_pixels[inCol + inGOffset] * a);
dst_pixels[outCol + outBOffset] = toColorSpaceFloatFromLinearFloat(src_pixels[inCol + inBOffset] * a);
if (outputHasAlpha) {
// alpha is linear and should not be dithered
dst_pixels[outCol + outAOffset] = a;
}
}
}
}
void
Lut::from_byte_planar(float* to,
const unsigned char* from,
int W,
const unsigned char* alpha,
int inDelta,
int outDelta) const
{
validate();
if (!alpha) {
for (int f = 0, t = 0; f < W; f += inDelta, t += outDelta) {
to[f] = fromFunc_uint8_to_float[(int)from[f]];
}
} else {
for (int f = 0, t = 0; f < W; f += inDelta, t += outDelta) {
to[t] = alpha[f] <= 0 ? 0 : Color::intToFloat<256>(fromFunc_uint8_to_float[(from[f] * 255 + 128) / alpha[f]] * alpha[f]);
}
}
}
void
Lut::from_short_planar(float* /*to*/,
const unsigned short* /*from*/,
int /*W*/,
const unsigned short*/* alpha*/,
int /*inDelta*/,
int /* outDelta*/) const
{
throw std::runtime_error("Lut::from_short_planar not implemented yet.");
}
void
Lut::from_float_planar(float* to,
const float* from,
int W,
const float* alpha,
int inDelta,
int outDelta) const
{
validate();
if (!alpha) {
for (int f = 0, t = 0; f < W; f += inDelta, t += outDelta) {
to[t] = fromColorSpaceFloatToLinearFloat(from[f]);
}
} else {
for (int f = 0, t = 0; f < W; f += inDelta, t += outDelta) {
float a = alpha[f];
to[t] = a <= 0. ? 0. : fromColorSpaceFloatToLinearFloat(from[f] / a) * a;
}
}
}
void
Lut::from_byte_packed(float* to,
const unsigned char* from,
const RectI & conversionRect,
const RectI & srcBounds,
const RectI & dstBounds,
PixelPackingEnum inputPacking,
PixelPackingEnum outputPacking,
bool invertY,
bool premult) const
{
if ( ( inputPacking == ePixelPackingPLANAR) || ( outputPacking == ePixelPackingPLANAR) ) {
throw std::runtime_error("Invalid pixel format.");
}
///clip the conversion rect to srcBounds and dstBounds
RectI rect = conversionRect;
if ( !clip(&rect, srcBounds) || !clip(&rect, dstBounds) ) {
return;
}
bool inputHasAlpha = inputPacking == ePixelPackingBGRA || inputPacking == ePixelPackingRGBA;
bool outputHasAlpha = outputPacking == ePixelPackingBGRA || outputPacking == ePixelPackingRGBA;
int inROffset, inGOffset, inBOffset, inAOffset;
int outROffset, outGOffset, outBOffset, outAOffset;
getOffsetsForPacking(inputPacking, &inROffset, &inGOffset, &inBOffset, &inAOffset);
getOffsetsForPacking(outputPacking, &outROffset, &outGOffset, &outBOffset, &outAOffset);
int inPackingSize, outPackingSize;
inPackingSize = inputHasAlpha ? 4 : 3;
outPackingSize = outputHasAlpha ? 4 : 3;
validate();
for (int y = rect.y1; y < rect.y2; ++y) {
int srcY = y;
if (invertY) {
srcY = srcBounds.y2 - y - 1;
}
const unsigned char *src_pixels = from + (srcY * (srcBounds.x2 - srcBounds.x1) * inPackingSize);
float *dst_pixels = to + (y * (dstBounds.x2 - dstBounds.x1) * outPackingSize);
for (int x = rect.x1; x < rect.x2; ++x) {
int inCol = x * inPackingSize;
int outCol = x * outPackingSize;
if (inputHasAlpha && premult) {
float rf = 0., gf = 0., bf = 0.;
float a = Color::intToFloat<256>(src_pixels[inCol + inAOffset]);
if (a > 0) {
rf = Color::intToFloat<256>(src_pixels[inCol + inROffset]) / a;
gf = Color::intToFloat<256>(src_pixels[inCol + inGOffset]) / a;
bf = Color::intToFloat<256>(src_pixels[inCol + inBOffset]) / a;
}
// we may lose a bit of information, but hey, it's 8-bits anyway, who cares?
dst_pixels[outCol + outROffset] = fromColorSpaceUint8ToLinearFloatFast( Color::floatToInt<256>(rf) ) * a;
dst_pixels[outCol + outGOffset] = fromColorSpaceUint8ToLinearFloatFast( Color::floatToInt<256>(gf) ) * a;
dst_pixels[outCol + outBOffset] = fromColorSpaceUint8ToLinearFloatFast( Color::floatToInt<256>(bf) ) * a;
if (outputHasAlpha) {
// alpha is linear
dst_pixels[outCol + outAOffset] = a;
}
} else {
int r8 = 0, g8 = 0, b8 = 0;
r8 = src_pixels[inCol + inROffset];
g8 = src_pixels[inCol + inGOffset];
b8 = src_pixels[inCol + inBOffset];
assert(r8 >= 0 && r8 < 256 && g8 >= 0 && g8 < 256 && b8 >= 0 && b8 < 256);
dst_pixels[outCol + outROffset] = fromFunc_uint8_to_float[r8];
dst_pixels[outCol + outGOffset] = fromFunc_uint8_to_float[g8];
dst_pixels[outCol + outBOffset] = fromFunc_uint8_to_float[b8];
if (outputHasAlpha) {
// alpha is linear
float a = Color::intToFloat<256>(src_pixels[inCol + inAOffset]);
dst_pixels[outCol + outAOffset] = a;
}
}
}
}
} // from_byte_packed
void
Lut::from_short_packed(float* /*to*/,
const unsigned short* /*from*/,
const RectI & /*conversionRect*/,
const RectI & /*srcBounds*/,
const RectI & /*dstBounds*/,
PixelPackingEnum /*inputPacking*/,
PixelPackingEnum /*outputPacking*/,
bool /*invertY*/,
bool /*premult*/) const
{
throw std::runtime_error("Lut::from_short_packed not implemented yet.");
}
void
Lut::from_float_packed(float* to,
const float* from,
const RectI & conversionRect,
const RectI & srcBounds,
const RectI & dstBounds,
PixelPackingEnum inputPacking,
PixelPackingEnum outputPacking,
bool invertY,
bool premult) const
{
if ( ( inputPacking == ePixelPackingPLANAR) || ( outputPacking == ePixelPackingPLANAR) ) {
throw std::runtime_error("Invalid pixel format.");
}
///clip the conversion rect to srcBounds and dstBounds
RectI rect = conversionRect;
if ( !clip(&rect, srcBounds) || !clip(&rect, dstBounds) ) {
return;
}
bool inputHasAlpha = inputPacking == ePixelPackingBGRA || inputPacking == ePixelPackingRGBA;
bool outputHasAlpha = outputPacking == ePixelPackingBGRA || outputPacking == ePixelPackingRGBA;
int inROffset, inGOffset, inBOffset, inAOffset;
int outROffset, outGOffset, outBOffset, outAOffset;
getOffsetsForPacking(inputPacking, &inROffset, &inGOffset, &inBOffset, &inAOffset);
getOffsetsForPacking(outputPacking, &outROffset, &outGOffset, &outBOffset, &outAOffset);
int inPackingSize, outPackingSize;
inPackingSize = inputHasAlpha ? 4 : 3;
outPackingSize = outputHasAlpha ? 4 : 3;
validate();
for (int y = rect.y1; y < rect.y2; ++y) {
int srcY = y;
if (invertY) {
srcY = srcBounds.y2 - y - 1;
}
const float *src_pixels = from + (srcY * (srcBounds.x2 - srcBounds.x1) * inPackingSize);
float *dst_pixels = to + (y * (dstBounds.x2 - dstBounds.x1) * outPackingSize);
for (int x = rect.x1; x < rect.x2; ++x) {
int inCol = x * inPackingSize;
int outCol = x * outPackingSize;
float a = (inputHasAlpha && premult) ? src_pixels[inCol + inAOffset] : 1.f;;
float rf = 0., gf = 0., bf = 0.;
if (a > 0.) {
rf = src_pixels[inCol + inROffset] / a;
gf = src_pixels[inCol + inGOffset] / a;
bf = src_pixels[inCol + inBOffset] / a;
}
dst_pixels[outCol + outROffset] = fromColorSpaceFloatToLinearFloat(rf) * a;
dst_pixels[outCol + outGOffset] = fromColorSpaceFloatToLinearFloat(gf) * a;
dst_pixels[outCol + outBOffset] = fromColorSpaceFloatToLinearFloat(bf) * a;
if (outputHasAlpha) {
// alpha is linear
dst_pixels[outCol + outAOffset] = a;
}
}
}
} // from_float_packed
///////////////////////
/////////////////////////////////////////// LINEAR //////////////////////////////////////////////
///////////////////////
namespace Linear {
void
from_byte_planar(float *to,
const unsigned char *from,
int W,
int inDelta,
int outDelta)
{
from += (W - 1) * inDelta;
to += W * outDelta;
for (; --W >= 0; from -= inDelta) {
to -= outDelta;
*to = intToFloat<256>(*from);
}
}
void
from_short_planar(float *to,
const unsigned short *from,
int W,
int inDelta,
int outDelta)
{
for (int f = 0, t = 0; f < W; f += inDelta, t += outDelta) {
to[t] = intToFloat<65536>(from[f]);
}
}
void
from_float_planar(float *to,
const float *from,
int W,
int inDelta,
int outDelta)
{
if ( ( inDelta == 1) && ( outDelta == 1) ) {
std::memcpy( to, from, W * sizeof(float) );
} else {
for (int f = 0, t = 0; f < W; f += inDelta, t += outDelta) {
to[t] = from[f];
}
}
}
void
from_byte_packed(float *to,
const unsigned char *from,
const RectI &conversionRect,
const RectI &srcBounds,
const RectI &dstBounds,
PixelPackingEnum inputPacking,
PixelPackingEnum outputPacking,
bool invertY )
{
if ( ( inputPacking == ePixelPackingPLANAR) || ( outputPacking == ePixelPackingPLANAR) ) {
throw std::runtime_error("Invalid pixel format.");
}
///clip the conversion rect to srcBounds and dstBounds
RectI rect = conversionRect;
if ( !clip(&rect, srcBounds) || !clip(&rect, dstBounds) ) {
return;
}
bool inputHasAlpha = inputPacking == ePixelPackingBGRA || inputPacking == ePixelPackingRGBA;
bool outputHasAlpha = outputPacking == ePixelPackingBGRA || outputPacking == ePixelPackingRGBA;
int inROffset, inGOffset, inBOffset, inAOffset;
int outROffset, outGOffset, outBOffset, outAOffset;
getOffsetsForPacking(inputPacking, &inROffset, &inGOffset, &inBOffset, &inAOffset);
getOffsetsForPacking(outputPacking, &outROffset, &outGOffset, &outBOffset, &outAOffset);
int inPackingSize, outPackingSize;
inPackingSize = inputHasAlpha ? 4 : 3;
outPackingSize = outputHasAlpha ? 4 : 3;
for (int y = rect.y1; y < rect.y2; ++y) {
int srcY = y;
if (invertY) {
srcY = srcBounds.y2 - y - 1;
}
const unsigned char *src_pixels = from + (srcY * (srcBounds.x2 - srcBounds.x1) * inPackingSize);
float *dst_pixels = to + (y * (dstBounds.x2 - dstBounds.x1) * outPackingSize);
for (int x = rect.x1; x < rect.x2; ++x) {
int inCol = x * inPackingSize;
int outCol = x * outPackingSize;
unsigned char a = inputHasAlpha ? src_pixels[inCol + inAOffset] : 255;
dst_pixels[outCol + outROffset] = Color::intToFloat<256>(src_pixels[inCol + inROffset]);
dst_pixels[outCol + outGOffset] = Color::intToFloat<256>(src_pixels[inCol + inGOffset]);
dst_pixels[outCol + outBOffset] = Color::intToFloat<256>(src_pixels[inCol + inBOffset]);
if (outputHasAlpha) {
// alpha is linear
dst_pixels[outCol + outAOffset] = Color::intToFloat<256>(a);
}
}
}
}
void
from_short_packed(float */*to*/,
const unsigned short */*from*/,
const RectI & /*rect*/,
const RectI & /*srcRod*/,
const RectI & /*rod*/,
PixelPackingEnum /*inputFormat*/,
PixelPackingEnum /*format*/,
bool /*invertY*/)
{
throw std::runtime_error("Linear::from_short_packed not yet implemented.");
}
void
from_float_packed(float *to,
const float *from,
const RectI &conversionRect,
const RectI &srcBounds,
const RectI &dstBounds,
PixelPackingEnum inputPacking,
PixelPackingEnum outputPacking,
bool invertY)
{
if ( ( inputPacking == ePixelPackingPLANAR) || ( outputPacking == ePixelPackingPLANAR) ) {
throw std::runtime_error("This function is not meant for planar buffers.");
}
///clip the conversion rect to srcBounds and dstBounds
RectI rect = conversionRect;
if ( !clip(&rect, srcBounds) || !clip(&rect, dstBounds) ) {
return;
}
if ( ( inputPacking == ePixelPackingPLANAR) || ( outputPacking == ePixelPackingPLANAR) ) {
throw std::runtime_error("Invalid pixel format.");
}
bool inputHasAlpha = inputPacking == ePixelPackingBGRA || inputPacking == ePixelPackingRGBA;
bool outputHasAlpha = outputPacking == ePixelPackingBGRA || outputPacking == ePixelPackingRGBA;
int inROffset, inGOffset, inBOffset, inAOffset;
int outROffset, outGOffset, outBOffset, outAOffset;
getOffsetsForPacking(inputPacking, &inROffset, &inGOffset, &inBOffset, &inAOffset);
getOffsetsForPacking(outputPacking, &outROffset, &outGOffset, &outBOffset, &outAOffset);
int inPackingSize, outPackingSize;
inPackingSize = inputHasAlpha ? 4 : 3;
outPackingSize = outputHasAlpha ? 4 : 3;
for (int y = rect.y1; y < rect.y2; ++y) {
int srcY = y;
if (invertY) {
srcY = srcBounds.y2 - y - 1;
}
const float *src_pixels = from + (srcY * (srcBounds.x2 - srcBounds.x1) * inPackingSize);
float *dst_pixels = to + (y * (dstBounds.x2 - dstBounds.x1) * outPackingSize);
if (inputPacking == outputPacking) {
std::memcpy( dst_pixels, src_pixels, (rect.x2 - rect.x1) * sizeof(float) );
} else {
for (int x = rect.x1; x < rect.x2; ++x) {
int inCol = x * inPackingSize;
int outCol = x * outPackingSize;
float a = inputHasAlpha ? src_pixels[inCol + inAOffset] : 1.f;
dst_pixels[outCol + outROffset] = src_pixels[inCol + inROffset];
dst_pixels[outCol + outGOffset] = src_pixels[inCol + inGOffset];
dst_pixels[outCol + outBOffset] = src_pixels[inCol + inBOffset];
if (outputHasAlpha) {
// alpha is linear
dst_pixels[outCol + outAOffset] = a;
}
}
}
}
} // from_float_packed
#if 0
void
to_byte_planar(unsigned char *to,
const float *from,
int W,
const float* alpha,
int inDelta,
int outDelta)
{
if (!alpha) {
unsigned char *end = to + W * outDelta;
// coverity[dont_call]
int start = rand() % W;
const float *q;
unsigned char *p;
/* go forwards from starting point to end of line: */
float error = .5;
for (p = to + start * outDelta, q = from + start * inDelta; p < end; p += outDelta, q += inDelta) {