Repository navigation
Expand file tree
/
Copy pathllimage.cpp
More file actions
2747 lines (2350 loc) · 82.8 KB
/
Copy pathllimage.cpp
File metadata and controls
2747 lines (2350 loc) · 82.8 KB
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
/**
* @file llimage.cpp
* @brief Base class for images.
*
* $LicenseInfo:firstyear=2001&license=viewerlgpl$
* Second Life Viewer Source Code
* Copyright (C) 2010, Linden Research, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License only.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
* $/LicenseInfo$
*/
#include "linden_common.h"
#include "llimageworker.h"
#include "llimage.h"
#include "llmath.h"
#include "v4coloru.h"
#include "v3color.h"
#include "llimagebmp.h"
#include "llimagetga.h"
#include "llimagej2c.h"
#include "llimagejpeg.h"
#include "llimagepng.h"
#include "llimagewebp.h"
#include "llimageavif.h"
#include "llimagedxt.h"
#include "llmemory.h"
#include <boost/preprocessor.hpp>
//..................................................................................
//..................................................................................
// Helper macrose's for generate cycle unwrap templates
//..................................................................................
#define _UNROL_GEN_TPL_arg_0(arg)
#define _UNROL_GEN_TPL_arg_1(arg) arg
#define _UNROL_GEN_TPL_comma_0
#define _UNROL_GEN_TPL_comma_1 BOOST_PP_COMMA()
//..................................................................................
#define _UNROL_GEN_TPL_ARGS_macro(z,n,seq) \
BOOST_PP_CAT(_UNROL_GEN_TPL_arg_, BOOST_PP_MOD(n, 2))(BOOST_PP_SEQ_ELEM(n, seq)) BOOST_PP_CAT(_UNROL_GEN_TPL_comma_, BOOST_PP_AND(BOOST_PP_MOD(n, 2), BOOST_PP_NOT_EQUAL(BOOST_PP_INC(n), BOOST_PP_SEQ_SIZE(seq))))
#define _UNROL_GEN_TPL_ARGS(seq) \
BOOST_PP_REPEAT(BOOST_PP_SEQ_SIZE(seq), _UNROL_GEN_TPL_ARGS_macro, seq)
//..................................................................................
#define _UNROL_GEN_TPL_TYPE_ARGS_macro(z,n,seq) \
BOOST_PP_SEQ_ELEM(n, seq) BOOST_PP_CAT(_UNROL_GEN_TPL_comma_, BOOST_PP_AND(BOOST_PP_MOD(n, 2), BOOST_PP_NOT_EQUAL(BOOST_PP_INC(n), BOOST_PP_SEQ_SIZE(seq))))
#define _UNROL_GEN_TPL_TYPE_ARGS(seq) \
BOOST_PP_REPEAT(BOOST_PP_SEQ_SIZE(seq), _UNROL_GEN_TPL_TYPE_ARGS_macro, seq)
//..................................................................................
#define _UNROLL_GEN_TPL_foreach_ee(z, n, seq) \
executor<n>(_UNROL_GEN_TPL_ARGS(seq));
#define _UNROLL_GEN_TPL(name, args_seq, operation, spec) \
template<> struct name<spec> { \
private: \
template<S32 _idx> inline void executor(_UNROL_GEN_TPL_TYPE_ARGS(args_seq)) { \
BOOST_PP_SEQ_ENUM(operation) ; \
} \
public: \
inline void operator()(_UNROL_GEN_TPL_TYPE_ARGS(args_seq)) { \
BOOST_PP_REPEAT(spec, _UNROLL_GEN_TPL_foreach_ee, args_seq) \
} \
};
//..................................................................................
#define _UNROLL_GEN_TPL_foreach_seq_macro(r, data, elem) \
_UNROLL_GEN_TPL(BOOST_PP_SEQ_ELEM(0, data), BOOST_PP_SEQ_ELEM(1, data), BOOST_PP_SEQ_ELEM(2, data), elem)
#define UNROLL_GEN_TPL(name, args_seq, operation, spec_seq) \
/*general specialization - should not be implemented!*/ \
template<U8> struct name { inline void operator()(_UNROL_GEN_TPL_TYPE_ARGS(args_seq)) { /*static_assert(!"Should not be instantiated.");*/ } }; \
BOOST_PP_SEQ_FOR_EACH(_UNROLL_GEN_TPL_foreach_seq_macro, (name)(args_seq)(operation), spec_seq)
//..................................................................................
//..................................................................................
//..................................................................................
// Generated unrolling loop templates with specializations
//..................................................................................
//example: for(c = 0; c < ch; ++c) comp[c] = cx[0] = 0;
UNROLL_GEN_TPL(uroll_zeroze_cx_comp, (S32 *)(cx)(S32 *)(comp), (cx[_idx] = comp[_idx] = 0), (1)(3)(4));
//example: for(c = 0; c < ch; ++c) comp[c] >>= 4;
UNROLL_GEN_TPL(uroll_comp_rshftasgn_constval, (S32 *)(comp)(const S32)(cval), (comp[_idx] >>= cval), (1)(3)(4));
//example: for(c = 0; c < ch; ++c) comp[c] = (cx[c] >> 5) * yap;
UNROLL_GEN_TPL(uroll_comp_asgn_cx_rshft_cval_all_mul_val, (S32 *)(comp)(S32 *)(cx)(const S32)(cval)(S32)(val), (comp[_idx] = (cx[_idx] >> cval) * val), (1)(3)(4));
//example: for(c = 0; c < ch; ++c) comp[c] += (cx[c] >> 5) * Cy;
UNROLL_GEN_TPL(uroll_comp_plusasgn_cx_rshft_cval_all_mul_val, (S32 *)(comp)(S32 *)(cx)(const S32)(cval)(S32)(val), (comp[_idx] += (cx[_idx] >> cval) * val), (1)(3)(4));
//example: for(c = 0; c < ch; ++c) comp[c] += pix[c] * info.xapoints[x];
UNROLL_GEN_TPL(uroll_inp_plusasgn_pix_mul_val, (S32 *)(comp)(const U8 *)(pix)(S32)(val), (comp[_idx] += pix[_idx] * val), (1)(3)(4));
//example: for(c = 0; c < ch; ++c) cx[c] = pix[c] * info.xapoints[x];
UNROLL_GEN_TPL(uroll_inp_asgn_pix_mul_val, (S32 *)(comp)(const U8 *)(pix)(S32)(val), (comp[_idx] = pix[_idx] * val), (1)(3)(4));
//example: for(c = 0; c < ch; ++c) comp[c] = ((cx[c] * info.yapoints[y]) + (comp[c] * (256 - info.yapoints[y]))) >> 16;
UNROLL_GEN_TPL(uroll_comp_asgn_cx_mul_apoint_plus_comp_mul_inv_apoint_allshifted_16_r, (S32 *)(comp)(S32 *)(cx)(S32)(apoint), (comp[_idx] = ((cx[_idx] * apoint) + (comp[_idx] * (256 - apoint))) >> 16), (1)(3)(4));
//example: for(c = 0; c < ch; ++c) comp[c] = (comp[c] + pix[c] * info.yapoints[y]) >> 8;
UNROLL_GEN_TPL(uroll_comp_asgn_comp_plus_pix_mul_apoint_allshifted_8_r, (S32 *)(comp)(const U8 *)(pix)(S32)(apoint), (comp[_idx] = (comp[_idx] + pix[_idx] * apoint) >> 8), (1)(3)(4));
//example: for(c = 0; c < ch; ++c) comp[c] = ((comp[c]*(256 - info.xapoints[x])) + ((cx[c] * info.xapoints[x]))) >> 12;
UNROLL_GEN_TPL(uroll_comp_asgn_comp_mul_inv_apoint_plus_cx_mul_apoint_allshifted_12_r, (S32 *)(comp)(S32)(apoint)(S32 *)(cx), (comp[_idx] = ((comp[_idx] * (256-apoint)) + (cx[_idx] * apoint)) >> 12), (1)(3)(4));
//example: for(c = 0; c < ch; ++c) *dptr++ = comp[c]&0xff;
UNROLL_GEN_TPL(uroll_uref_dptr_inc_asgn_comp_and_ff, (U8 *&)(dptr)(S32 *)(comp), (*dptr++ = comp[_idx]&0xff), (1)(3)(4));
//example: for(c = 0; c < ch; ++c) *dptr++ = (sptr[info.xpoints[x]*ch + c])&0xff;
UNROLL_GEN_TPL(uroll_uref_dptr_inc_asgn_sptr_apoint_plus_idx_alland_ff, (U8 *&)(dptr)(const U8 *)(sptr)(S32)(apoint), (*dptr++ = sptr[apoint + _idx]&0xff), (1)(3)(4));
//example: for(c = 0; c < ch; ++c) *dptr++ = (comp[c]>>10)&0xff;
UNROLL_GEN_TPL(uroll_uref_dptr_inc_asgn_comp_rshft_cval_and_ff, (U8 *&)(dptr)(S32 *)(comp)(const S32)(cval), (*dptr++ = (comp[_idx]>>cval)&0xff), (1)(3)(4));
//..................................................................................
template<U8 ch>
struct scale_info
{
public:
std::vector<S32> xpoints;
std::vector<const U8*> ystrides;
std::vector<S32> xapoints, yapoints;
S32 xup_yup;
public:
//unrolling loop types declaration
typedef uroll_zeroze_cx_comp<ch> uroll_zeroze_cx_comp_t;
typedef uroll_comp_rshftasgn_constval<ch> uroll_comp_rshftasgn_constval_t;
typedef uroll_comp_asgn_cx_rshft_cval_all_mul_val<ch> uroll_comp_asgn_cx_rshft_cval_all_mul_val_t;
typedef uroll_comp_plusasgn_cx_rshft_cval_all_mul_val<ch> uroll_comp_plusasgn_cx_rshft_cval_all_mul_val_t;
typedef uroll_inp_plusasgn_pix_mul_val<ch> uroll_inp_plusasgn_pix_mul_val_t;
typedef uroll_inp_asgn_pix_mul_val<ch> uroll_inp_asgn_pix_mul_val_t;
typedef uroll_comp_asgn_cx_mul_apoint_plus_comp_mul_inv_apoint_allshifted_16_r<ch> uroll_comp_asgn_cx_mul_apoint_plus_comp_mul_inv_apoint_allshifted_16_r_t;
typedef uroll_comp_asgn_comp_plus_pix_mul_apoint_allshifted_8_r<ch> uroll_comp_asgn_comp_plus_pix_mul_apoint_allshifted_8_r_t;
typedef uroll_comp_asgn_comp_mul_inv_apoint_plus_cx_mul_apoint_allshifted_12_r<ch> uroll_comp_asgn_comp_mul_inv_apoint_plus_cx_mul_apoint_allshifted_12_r_t;
typedef uroll_uref_dptr_inc_asgn_comp_and_ff<ch> uroll_uref_dptr_inc_asgn_comp_and_ff_t;
typedef uroll_uref_dptr_inc_asgn_sptr_apoint_plus_idx_alland_ff<ch> uroll_uref_dptr_inc_asgn_sptr_apoint_plus_idx_alland_ff_t;
typedef uroll_uref_dptr_inc_asgn_comp_rshft_cval_and_ff<ch> uroll_uref_dptr_inc_asgn_comp_rshft_cval_and_ff_t;
public:
scale_info(const U8 *src, U32 srcW, U32 srcH, U32 dstW, U32 dstH, U32 srcStride)
: xup_yup((dstW >= srcW) + ((dstH >= srcH) << 1))
{
calc_x_points(srcW, dstW);
calc_y_strides(src, srcStride, srcH, dstH);
calc_aa_points(srcW, dstW, xup_yup&1, xapoints);
calc_aa_points(srcH, dstH, xup_yup&2, yapoints);
}
private:
//...........................................................................................
void calc_x_points(U32 srcW, U32 dstW)
{
xpoints.resize(dstW+1);
S32 val = dstW >= srcW ? 0x8000 * srcW / dstW - 0x8000 : 0;
S32 inc = (srcW << 16) / dstW;
for(U32 i = 0, j = 0; i < dstW; ++i, ++j, val += inc)
{
xpoints[j] = llmax(0, val >> 16);
}
}
//...........................................................................................
void calc_y_strides(const U8 *src, U32 srcStride, U32 srcH, U32 dstH)
{
ystrides.resize(dstH+1);
S32 val = dstH >= srcH ? 0x8000 * srcH / dstH - 0x8000 : 0;
S32 inc = (srcH << 16) / dstH;
for(U32 i = 0, j = 0; i < dstH; ++i, ++j, val += inc)
{
ystrides[j] = src + llmax(0, val >> 16) * srcStride;
}
}
//...........................................................................................
void calc_aa_points(U32 srcSz, U32 dstSz, bool scale_up, std::vector<S32> &vp)
{
vp.resize(dstSz);
if(scale_up)
{
S32 val = 0x8000 * srcSz / dstSz - 0x8000;
S32 inc = (srcSz << 16) / dstSz;
U32 pos;
for(U32 i = 0, j = 0; i < dstSz; ++i, ++j, val += inc)
{
pos = val >> 16;
if (pos >= (srcSz - 1))
vp[j] = 0;
else
vp[j] = (val >> 8) - ((val >> 8) & 0xffffff00);
}
}
else
{
S32 inc = (srcSz << 16) / dstSz;
S32 Cp = ((dstSz << 14) / srcSz) + 1;
S32 ap;
for(U32 i = 0, j = 0, val = 0; i < dstSz; ++i, ++j, val += inc)
{
ap = ((0x100 - ((val >> 8) & 0xff)) * Cp) >> 8;
vp[j] = ap | (Cp << 16);
}
}
}
};
template<U8 ch>
inline void bilinear_scale(
const U8 *src, U32 srcW, U32 srcH, U32 srcStride
, U8 *dst, U32 dstW, U32 dstH, U32 dstStride
)
{
typedef scale_info<ch> scale_info_t;
scale_info_t info(src, srcW, srcH, dstW, dstH, srcStride);
const U8 *sptr;
U8 *dptr;
U32 x, y;
const U8 *pix;
S32 cx[ch], comp[ch];
if(3 == info.xup_yup)
{ //scale x/y - up
for(y = 0; y < dstH; ++y)
{
dptr = dst + (y * dstStride);
sptr = info.ystrides[y];
if(0 < info.yapoints[y])
{
for(x = 0; x < dstW; ++x)
{
//for(c = 0; c < ch; ++c) cx[c] = comp[c] = 0;
typename scale_info_t::uroll_zeroze_cx_comp_t()(cx, comp);
if(0 < info.xapoints[x])
{
pix = info.ystrides[y] + info.xpoints[x] * ch;
//for(c = 0; c < ch; ++c) comp[c] = pix[c] * (256 - info.xapoints[x]);
typename scale_info_t::uroll_inp_asgn_pix_mul_val_t()(comp, pix, 256 - info.xapoints[x]);
pix += ch;
//for(c = 0; c < ch; ++c) comp[c] += pix[c] * info.xapoints[x];
typename scale_info_t::uroll_inp_plusasgn_pix_mul_val_t()(comp, pix, info.xapoints[x]);
pix += srcStride;
//for(c = 0; c < ch; ++c) cx[c] = pix[c] * info.xapoints[x];
typename scale_info_t::uroll_inp_asgn_pix_mul_val_t()(cx, pix, info.xapoints[x]);
pix -= ch;
//for(c = 0; c < ch; ++c) {
// cx[c] += pix[c] * (256 - info.xapoints[x]);
// comp[c] = ((cx[c] * info.yapoints[y]) + (comp[c] * (256 - info.yapoints[y]))) >> 16;
// *dptr++ = comp[c]&0xff;
//}
typename scale_info_t::uroll_inp_plusasgn_pix_mul_val_t()(cx, pix, 256 - info.xapoints[x]);
typename scale_info_t::uroll_comp_asgn_cx_mul_apoint_plus_comp_mul_inv_apoint_allshifted_16_r_t()(comp, cx, info.yapoints[y]);
typename scale_info_t::uroll_uref_dptr_inc_asgn_comp_and_ff_t()(dptr, comp);
}
else
{
pix = info.ystrides[y] + info.xpoints[x] * ch;
//for(c = 0; c < ch; ++c) comp[c] = pix[c] * (256 - info.yapoints[y]);
typename scale_info_t::uroll_inp_asgn_pix_mul_val_t()(comp, pix, 256-info.yapoints[y]);
pix += srcStride;
//for(c = 0; c < ch; ++c) {
// comp[c] = (comp[c] + pix[c] * info.yapoints[y]) >> 8;
// *dptr++ = comp[c]&0xff;
//}
typename scale_info_t::uroll_comp_asgn_comp_plus_pix_mul_apoint_allshifted_8_r_t()(comp, pix, info.yapoints[y]);
typename scale_info_t::uroll_uref_dptr_inc_asgn_comp_and_ff_t()(dptr, comp);
}
}
}
else
{
for(x = 0; x < dstW; ++x)
{
if(0 < info.xapoints[x])
{
pix = info.ystrides[y] + info.xpoints[x] * ch;
//for(c = 0; c < ch; ++c) {
// comp[c] = pix[c] * (256 - info.xapoints[x]);
// comp[c] = (comp[c] + pix[c] * info.xapoints[x]) >> 8;
// *dptr++ = comp[c]&0xff;
//}
typename scale_info_t::uroll_inp_asgn_pix_mul_val_t()(comp, pix, 256 - info.xapoints[x]);
typename scale_info_t::uroll_comp_asgn_comp_plus_pix_mul_apoint_allshifted_8_r_t()(comp, pix, info.xapoints[x]);
typename scale_info_t::uroll_uref_dptr_inc_asgn_comp_and_ff_t()(dptr, comp);
}
else
{
//for(c = 0; c < ch; ++c) *dptr++ = (sptr[info.xpoints[x]*ch + c])&0xff;
typename scale_info_t::uroll_uref_dptr_inc_asgn_sptr_apoint_plus_idx_alland_ff_t()(dptr, sptr, info.xpoints[x]*ch);
}
}
}
}
}
else if(info.xup_yup == 1)
{ //scaling down vertically
S32 Cy, j;
S32 yap;
for(y = 0; y < dstH; y++)
{
Cy = info.yapoints[y] >> 16;
yap = info.yapoints[y] & 0xffff;
dptr = dst + (y * dstStride);
for(x = 0; x < dstW; x++)
{
pix = info.ystrides[y] + info.xpoints[x] * ch;
//for(c = 0; c < ch; ++c) comp[c] = pix[c] * yap;
typename scale_info_t::uroll_inp_asgn_pix_mul_val_t()(comp, pix, yap);
pix += srcStride;
for(j = (1 << 14) - yap; j > Cy; j -= Cy, pix += srcStride)
{
//for(c = 0; c < ch; ++c) comp[c] += pix[c] * Cy;
typename scale_info_t::uroll_inp_plusasgn_pix_mul_val_t()(comp, pix, Cy);
}
if(j > 0)
{
//for(c = 0; c < ch; ++c) comp[c] += pix[c] * j;
typename scale_info_t::uroll_inp_plusasgn_pix_mul_val_t()(comp, pix, j);
}
if(info.xapoints[x] > 0)
{
pix = info.ystrides[y] + info.xpoints[x]*ch + ch;
//for(c = 0; c < ch; ++c) cx[c] = pix[c] * yap;
typename scale_info_t::uroll_inp_asgn_pix_mul_val_t()(cx, pix, yap);
pix += srcStride;
for(j = (1 << 14) - yap; j > Cy; j -= Cy)
{
//for(c = 0; c < ch; ++c) cx[c] += pix[c] * Cy;
typename scale_info_t::uroll_inp_plusasgn_pix_mul_val_t()(cx, pix, Cy);
pix += srcStride;
}
if(j > 0)
{
//for(c = 0; c < ch; ++c) cx[c] += pix[c] * j;
typename scale_info_t::uroll_inp_plusasgn_pix_mul_val_t()(cx, pix, j);
}
//for(c = 0; c < ch; ++c) comp[c] = ((comp[c]*(256 - info.xapoints[x])) + ((cx[c] * info.xapoints[x]))) >> 12;
typename scale_info_t::uroll_comp_asgn_comp_mul_inv_apoint_plus_cx_mul_apoint_allshifted_12_r_t()(comp, info.xapoints[x], cx);
}
else
{
//for(c = 0; c < ch; ++c) comp[c] >>= 4;
typename scale_info_t::uroll_comp_rshftasgn_constval_t()(comp, 4);
}
//for(c = 0; c < ch; ++c) *dptr++ = (comp[c]>>10)&0xff;
typename scale_info_t::uroll_uref_dptr_inc_asgn_comp_rshft_cval_and_ff_t()(dptr, comp, 10);
}
}
}
else if(info.xup_yup == 2)
{ // scaling down horizontally
S32 Cx, j;
S32 xap;
for(y = 0; y < dstH; y++)
{
dptr = dst + (y * dstStride);
for(x = 0; x < dstW; x++)
{
Cx = info.xapoints[x] >> 16;
xap = info.xapoints[x] & 0xffff;
pix = info.ystrides[y] + info.xpoints[x] * ch;
//for(c = 0; c < ch; ++c) comp[c] = pix[c] * xap;
typename scale_info_t::uroll_inp_asgn_pix_mul_val_t()(comp, pix, xap);
pix+=ch;
for(j = (1 << 14) - xap; j > Cx; j -= Cx)
{
//for(c = 0; c < ch; ++c) comp[c] += pix[c] * Cx;
typename scale_info_t::uroll_inp_plusasgn_pix_mul_val_t()(comp, pix, Cx);
pix+=ch;
}
if(j > 0)
{
//for(c = 0; c < ch; ++c) comp[c] += pix[c] * j;
typename scale_info_t::uroll_inp_plusasgn_pix_mul_val_t()(comp, pix, j);
}
if(info.yapoints[y] > 0)
{
pix = info.ystrides[y] + info.xpoints[x]*ch + srcStride;
//for(c = 0; c < ch; ++c) cx[c] = pix[c] * xap;
typename scale_info_t::uroll_inp_asgn_pix_mul_val_t()(cx, pix, xap);
pix+=ch;
for(j = (1 << 14) - xap; j > Cx; j -= Cx)
{
//for(c = 0; c < ch; ++c) cx[c] += pix[c] * Cx;
typename scale_info_t::uroll_inp_plusasgn_pix_mul_val_t()(cx, pix, Cx);
pix+=ch;
}
if(j > 0)
{
//for(c = 0; c < ch; ++c) cx[c] += pix[c] * j;
typename scale_info_t::uroll_inp_plusasgn_pix_mul_val_t()(cx, pix, j);
}
//for(c = 0; c < ch; ++c) comp[c] = ((comp[c] * (256 - info.yapoints[y])) + ((cx[c] * info.yapoints[y]))) >> 12;
typename scale_info_t::uroll_comp_asgn_comp_mul_inv_apoint_plus_cx_mul_apoint_allshifted_12_r_t()(comp, info.yapoints[y], cx);
}
else
{
//for(c = 0; c < ch; ++c) comp[c] >>= 4;
typename scale_info_t::uroll_comp_rshftasgn_constval_t()(comp, 4);
}
//for(c = 0; c < ch; ++c) *dptr++ = (comp[c]>>10)&0xff;
typename scale_info_t::uroll_uref_dptr_inc_asgn_comp_rshft_cval_and_ff_t()(dptr, comp, 10);
}
}
}
else
{ //scale x/y - down
S32 Cx, Cy, i, j;
S32 xap, yap;
for(y = 0; y < dstH; y++)
{
Cy = info.yapoints[y] >> 16;
yap = info.yapoints[y] & 0xffff;
dptr = dst + (y * dstStride);
for(x = 0; x < dstW; x++)
{
Cx = info.xapoints[x] >> 16;
xap = info.xapoints[x] & 0xffff;
sptr = info.ystrides[y] + info.xpoints[x] * ch;
pix = sptr;
sptr += srcStride;
//for(c = 0; c < ch; ++c) cx[c] = pix[c] * xap;
typename scale_info_t::uroll_inp_asgn_pix_mul_val_t()(cx, pix, xap);
pix+=ch;
for(i = (1 << 14) - xap; i > Cx; i -= Cx)
{
//for(c = 0; c < ch; ++c) cx[c] += pix[c] * Cx;
typename scale_info_t::uroll_inp_plusasgn_pix_mul_val_t()(cx, pix, Cx);
pix+=ch;
}
if(i > 0)
{
//for(c = 0; c < ch; ++c) cx[c] += pix[c] * i;
typename scale_info_t::uroll_inp_plusasgn_pix_mul_val_t()(cx, pix, i);
}
//for(c = 0; c < ch; ++c) comp[c] = (cx[c] >> 5) * yap;
typename scale_info_t::uroll_comp_asgn_cx_rshft_cval_all_mul_val_t()(comp, cx, 5, yap);
for(j = (1 << 14) - yap; j > Cy; j -= Cy)
{
pix = sptr;
sptr += srcStride;
//for(c = 0; c < ch; ++c) cx[c] = pix[c] * xap;
typename scale_info_t::uroll_inp_asgn_pix_mul_val_t()(cx, pix, xap);
pix+=ch;
for(i = (1 << 14) - xap; i > Cx; i -= Cx)
{
//for(c = 0; c < ch; ++c) cx[c] += pix[c] * Cx;
typename scale_info_t::uroll_inp_plusasgn_pix_mul_val_t()(cx, pix, Cx);
pix+=ch;
}
if(i > 0)
{
//for(c = 0; c < ch; ++c) cx[c] += pix[c] * i;
typename scale_info_t::uroll_inp_plusasgn_pix_mul_val_t()(cx, pix, i);
}
//for(c = 0; c < ch; ++c) comp[c] += (cx[c] >> 5) * Cy;
typename scale_info_t::uroll_comp_plusasgn_cx_rshft_cval_all_mul_val_t()(comp, cx, 5, Cy);
}
if(j > 0)
{
pix = sptr;
sptr += srcStride;
//for(c = 0; c < ch; ++c) cx[c] = pix[c] * xap;
typename scale_info_t::uroll_inp_asgn_pix_mul_val_t()(cx, pix, xap);
pix+=ch;
for(i = (1 << 14) - xap; i > Cx; i -= Cx)
{
//for(c = 0; c < ch; ++c) cx[c] += pix[c] * Cx;
typename scale_info_t::uroll_inp_plusasgn_pix_mul_val_t()(cx, pix, Cx);
pix+=ch;
}
if(i > 0)
{
//for(c = 0; c < ch; ++c) cx[c] += pix[c] * i;
typename scale_info_t::uroll_inp_plusasgn_pix_mul_val_t()(cx, pix, i);
}
//for(c = 0; c < ch; ++c) comp[c] += (cx[c] >> 5) * j;
typename scale_info_t::uroll_comp_plusasgn_cx_rshft_cval_all_mul_val_t()(comp, cx, 5, j);
}
//for(c = 0; c < ch; ++c) *dptr++ = (comp[c]>>23)&0xff;
typename scale_info_t::uroll_uref_dptr_inc_asgn_comp_rshft_cval_and_ff_t()(dptr, comp, 23);
}
}
} //else
}
//wrapper
static void bilinear_scale(const U8 *src, U32 srcW, U32 srcH, U32 srcCh, U32 srcStride, U8 *dst, U32 dstW, U32 dstH, U32 dstCh, U32 dstStride)
{
llassert(srcCh == dstCh);
switch(srcCh)
{
case 1:
bilinear_scale<1>(src, srcW, srcH, srcStride, dst, dstW, dstH, dstStride);
break;
case 3:
bilinear_scale<3>(src, srcW, srcH, srcStride, dst, dstW, dstH, dstStride);
break;
case 4:
bilinear_scale<4>(src, srcW, srcH, srcStride, dst, dstW, dstH, dstStride);
break;
default:
llassert(!"Implement if need");
break;
}
}
//---------------------------------------------------------------------------
// LLImage
//---------------------------------------------------------------------------
//static
thread_local std::string LLImage::sLastThreadErrorMessage;
bool LLImage::sUseNewByteRange = false;
S32 LLImage::sMinimalReverseByteRangePercent = 75;
//static
void LLImage::initClass(bool use_new_byte_range, S32 minimal_reverse_byte_range_percent)
{
sUseNewByteRange = use_new_byte_range;
sMinimalReverseByteRangePercent = minimal_reverse_byte_range_percent;
}
//static
void LLImage::cleanupClass()
{
}
//static
const std::string& LLImage::getLastThreadError()
{
static const std::string noerr("No Error");
return sLastThreadErrorMessage.empty() ? noerr : sLastThreadErrorMessage;
}
//static
void LLImage::setLastError(const std::string& message)
{
sLastThreadErrorMessage = message;
}
//---------------------------------------------------------------------------
// LLImageBase
//---------------------------------------------------------------------------
LLImageBase::LLImageBase()
: mData(NULL),
mDataSize(0),
mWidth(0),
mHeight(0),
mComponents(0),
mBadBufferAllocation(false),
mAllowOverSize(false)
{}
// virtual
LLImageBase::~LLImageBase()
{
deleteData(); // virtual
}
// virtual
void LLImageBase::dump()
{
LL_INFOS() << "LLImageBase mComponents " << mComponents
<< " mData " << mData
<< " mDataSize " << mDataSize
<< " mWidth " << mWidth
<< " mHeight " << mHeight
<< LL_ENDL;
}
// virtual
void LLImageBase::sanityCheck()
{
if (mWidth > MAX_IMAGE_SIZE
|| mHeight > MAX_IMAGE_SIZE
|| mDataSize > (S32)MAX_IMAGE_DATA_SIZE
|| mComponents > (S8)MAX_IMAGE_COMPONENTS
)
{
LL_ERRS() << "Failed LLImageBase::sanityCheck "
<< "width " << mWidth
<< "height " << mHeight
<< "datasize " << mDataSize
<< "components " << mComponents
<< "data " << mData
<< LL_ENDL;
}
}
// virtual
void LLImageBase::deleteData()
{
ll_aligned_free_16(mData);
mDataSize = 0;
mData = NULL;
}
// virtual
U8* LLImageBase::allocateData(S32 size)
{
//make this function thread-safe.
static const U32 MAX_BUFFER_SIZE = 4096 * 4096 * 16; //256 MB
mBadBufferAllocation = false;
if (size < 0)
{
size = mWidth * mHeight * mComponents;
if (size <= 0)
{
LL_WARNS() << llformat("LLImageBase::allocateData called with bad dimensions: %dx%dx%d",mWidth,mHeight,(S32)mComponents) << LL_ENDL;
mBadBufferAllocation = true;
}
}
if (!mBadBufferAllocation && (size < 1 || size > MAX_BUFFER_SIZE))
{
LL_INFOS() << "width: " << mWidth << " height: " << mHeight << " components: " << mComponents << LL_ENDL ;
if(mAllowOverSize)
{
LL_INFOS() << "Oversize: " << size << LL_ENDL ;
}
else
{
LL_WARNS() << "LLImageBase::allocateData: bad size: " << size << LL_ENDL;
mBadBufferAllocation = true;
}
}
if (!mBadBufferAllocation && (!mData || size != mDataSize))
{
deleteData(); // virtual
mData = (U8*)ll_aligned_malloc_16(size);
if (!mData)
{
constexpr S32 MAX_TOLERANCE = 1024 * 1024 * 4; // 4 MB
if (size > MAX_TOLERANCE)
{
// If a big image failed to allocate, tollerate it for now.
// It's insightfull when crash logs without obvious cause are being analyzed,
// so a crash in a random location that normally is a mystery can get proper handling.
LL_WARNS() << "Failed to allocate image data size [" << size << "]" << LL_ENDL;
}
else
{
// We are too far gone if we can't allocate a small buffer.
LLError::LLUserWarningMsg::showOutOfMemory();
LL_ERRS() << "Failed to allocate image data size [" << size << "]" << LL_ENDL;
}
}
}
if (mBadBufferAllocation)
{
size = 0;
mWidth = mHeight = 0;
if (mData)
{
deleteData(); // virtual
mData = NULL;
}
}
mDataSize = size;
return mData;
}
// virtual
U8* LLImageBase::reallocateData(S32 size)
{
U8 *new_datap = (U8*)ll_aligned_malloc_16(size);
if (!new_datap)
{
LL_WARNS() << "Out of memory in LLImageBase::reallocateData" << LL_ENDL;
return 0;
}
if (mData)
{
S32 bytes = llmin(mDataSize, size);
memcpy(new_datap, mData, bytes); /* Flawfinder: ignore */
ll_aligned_free_16(mData) ;
}
mData = new_datap;
mDataSize = size;
mBadBufferAllocation = false;
return mData;
}
const U8* LLImageBase::getData() const
{
if(mBadBufferAllocation)
{
LL_WARNS() << "Bad memory allocation for the image buffer!" << LL_ENDL ;
return NULL;
}
return mData;
} // read only
U8* LLImageBase::getData()
{
if(mBadBufferAllocation)
{
LL_WARNS() << "Bad memory allocation for the image buffer!" << LL_ENDL;
return NULL;
}
return mData;
}
bool LLImageBase::isBufferInvalid() const
{
return mBadBufferAllocation || mData == NULL;
}
void LLImageBase::setSize(S32 width, S32 height, S32 ncomponents)
{
mWidth = width;
mHeight = height;
mComponents = ncomponents;
}
U8* LLImageBase::allocateDataSize(S32 width, S32 height, S32 ncomponents, S32 size)
{
setSize(width, height, ncomponents);
return allocateData(size); // virtual
}
//---------------------------------------------------------------------------
// LLImageRaw
//---------------------------------------------------------------------------
S32 LLImageRaw::sRawImageCount = 0;
LLImageRaw::LLImageRaw()
: LLImageBase()
{
++sRawImageCount;
}
LLImageRaw::LLImageRaw(U16 width, U16 height, S8 components)
: LLImageBase()
{
//llassert( S32(width) * S32(height) * S32(components) <= MAX_IMAGE_DATA_SIZE );
allocateDataSize(width, height, components);
++sRawImageCount;
}
LLImageRaw::LLImageRaw(const U8* data, U16 width, U16 height, S8 components)
: LLImageBase()
{
if (allocateDataSize(width, height, components))
{
memcpy(getData(), data, width * height * components);
}
}
LLImageRaw::LLImageRaw(U8 *data, U16 width, U16 height, S8 components, bool no_copy)
: LLImageBase()
{
if(no_copy)
{
setDataAndSize(data, width, height, components);
}
else if(allocateDataSize(width, height, components))
{
memcpy(getData(), data, width*height*components);
}
++sRawImageCount;
}
//LLImageRaw::LLImageRaw(const std::string& filename, bool j2c_lowest_mip_only)
// : LLImageBase()
//{
// createFromFile(filename, j2c_lowest_mip_only);
//}
LLImageRaw::~LLImageRaw()
{
// NOTE: ~LLimageBase() call to deleteData() calls LLImageBase::deleteData()
// NOT LLImageRaw::deleteData()
deleteData();
--sRawImageCount;
}
// virtual
U8* LLImageRaw::allocateData(S32 size)
{
LLImageDataLock lock(this);
U8* res = LLImageBase::allocateData(size);
return res;
}
// virtual
U8* LLImageRaw::reallocateData(S32 size)
{
LLImageDataLock lock(this);
U8* res = LLImageBase::reallocateData(size);
return res;
}
void LLImageRaw::releaseData()
{
LLImageDataLock lock(this);
LLImageBase::setSize(0, 0, 0);
LLImageBase::setDataAndSize(nullptr, 0);
}
// virtual
void LLImageRaw::deleteData()
{
LLImageDataLock lock(this);
LLImageBase::deleteData();
}
void LLImageRaw::setDataAndSize(U8 *data, S32 width, S32 height, S8 components)
{
LLImageDataLock lock(this);
if(data == getData())
{
return ;
}
deleteData();
LLImageBase::setSize(width, height, components) ;
LLImageBase::setDataAndSize(data, width * height * components) ;
}
bool LLImageRaw::resize(U16 width, U16 height, S8 components)
{
LLImageDataLock lock(this);
if ((getWidth() == width) && (getHeight() == height) && (getComponents() == components) && !isBufferInvalid())
{
return true;
}
// Reallocate the data buffer.
deleteData();
allocateDataSize(width,height,components);
return !isBufferInvalid();
}
bool LLImageRaw::setSubImage(U32 x_pos, U32 y_pos, U32 width, U32 height,
const U8 *data, U32 stride, bool reverse_y)
{
LLImageDataLock lock(this);
if (!getData())
{
return false;
}
if (!data)
{
return false;
}
// Should do some simple bounds checking
U32 i;
for (i = 0; i < height; i++)
{
const U32 row = reverse_y ? height - 1 - i : i;
const U32 from_offset = row * ((stride == 0) ? width*getComponents() : stride);
const U32 to_offset = (y_pos + i)*getWidth() + x_pos;
memcpy(getData() + to_offset*getComponents(), /* Flawfinder: ignore */
data + from_offset, getComponents()*width);
}
return true;
}
void LLImageRaw::clear(U8 r, U8 g, U8 b, U8 a)
{
llassert( getComponents() <= 4 );
LLImageDataLock lock(this);
// This is fairly bogus, but it'll do for now.
if (isBufferInvalid())
{
LL_WARNS() << "Invalid image buffer" << LL_ENDL;
return;
}
U8 *pos = getData();
U32 x, y;
for (x = 0; x < getWidth(); x++)
{
for (y = 0; y < getHeight(); y++)
{
*pos = r;
pos++;
if (getComponents() == 1)
{
continue;
}
*pos = g;
pos++;
if (getComponents() == 2)
{
continue;
}
*pos = b;
pos++;
if (getComponents() == 3)
{
continue;
}
*pos = a;
pos++;
}
}
}