-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathoil_resample.c
1129 lines (999 loc) · 24.5 KB
/
oil_resample.c
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
/**
* Copyright (c) 2014-2019 Timothy Elliott
* 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.
*/
#include "oil_resample.h"
#include <math.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <stdio.h>
/**
* When shrinking a 10 million pixel wide scanline down to a single pixel, we
* reach the limits of single-precision floats. Limit input dimensions to one
* million by one million pixels to avoid this issue as well as overflow issues
* with 32-bit ints.
*/
#define MAX_DIMENSION 1000000
/**
* Bicubic interpolation. 2 base taps on either side.
*/
#define TAPS 4
static int max(int a, int b)
{
return a > b ? a : b;
}
static int min(int a, int b)
{
return a < b ? a : b;
}
/**
* Clamp a float between 0 and 1.
*/
static float clampf(float x)
{
if (x > 1.0f) {
return 1.0f;
} else if (x < 0.0f) {
return 0.0f;
}
return x;
}
/**
* Convert a float to an int. When compiling on x86 without march=native, this
* performs much better than roundf().
*/
static int f2i(float x)
{
return x + 0.5f;
}
/**
* Convert a float to 8-bit integer.
*/
static int clamp8(float x)
{
return f2i(clampf(x) * 255.0f);
}
/**
* Map from the discreet dest coordinate pos to a continuous source coordinate.
* The resulting coordinate can range from -0.5 to the maximum of the
* destination image dimension.
*/
static double map(int dim_in, int dim_out, int pos)
{
return (pos + 0.5) * ((double)dim_in / dim_out) - 0.5;
}
/**
* Returns the mapped input position and put the sub-pixel remainder in rest.
*/
static int split_map(int dim_in, int dim_out, int pos, float *rest)
{
double smp;
int smp_i;
smp = map(dim_in, dim_out, pos);
smp_i = smp < 0 ? -1 : smp;
*rest = smp - smp_i;
return smp_i;
}
/**
* Given input and output dimension, calculate the total number of taps that
* will be needed to calculate an output sample.
*
* When we reduce an image by a factor of two, we need to scale our resampling
* function by two as well in order to avoid aliasing.
*/
static int calc_taps(int dim_in, int dim_out)
{
int tmp;
if (dim_out > dim_in) {
return TAPS;
}
tmp = TAPS * dim_in / dim_out;
return tmp - (tmp & 1);
}
/**
* Catmull-Rom interpolator.
*/
static float catrom(float x)
{
if (x<1) {
return (1.5f*x - 2.5f)*x*x + 1;
}
return (((5 - x)*x - 8)*x + 4) / 2;
}
/**
* Given an offset tx, calculate taps coefficients.
*/
static void calc_coeffs(float *coeffs, float tx, int taps, int ltrim, int rtrim)
{
int i;
float tmp, tap_mult, fudge;
tap_mult = (float)taps / TAPS;
tx = 1 - tx - taps / 2 + ltrim;
fudge = 0.0f;
for (i=ltrim; i<taps-rtrim; i++) {
tmp = catrom(fabsf(tx) / tap_mult) / tap_mult;
fudge += tmp;
coeffs[i] = tmp;
tx += 1;
}
fudge = 1 / fudge;
for (i=ltrim; i<taps-rtrim; i++) {
coeffs[i] *= fudge;
}
}
/**
* Pre-calculated table of linear to srgb mappings. Initialized via build_l2s().
*
* catmull-rom interpolation can produce values from -17/64 to 81/64.
*
* The total allocated space will be split into three parts:
* * 17/98 of padding below zero
* * 64/98 of mapping
* * 17/98 of padding above one
*/
#define L2S_ALL_LEN 32768
static unsigned char l2s_map_all[L2S_ALL_LEN];
static int l2s_len;
static unsigned char *l2s_map;
static void build_l2s(void)
{
int i, padding;
double srgb_f, tmp, val;
padding = L2S_ALL_LEN * 17 / 98;
l2s_len = L2S_ALL_LEN - 2 * padding;
l2s_map = l2s_map_all + padding;
for (i=0; i<l2s_len; i++) {
srgb_f = (i + 0.5)/(l2s_len - 1);
if (srgb_f <= 0.00313) {
val = srgb_f * 12.92;
} else {
tmp = pow(srgb_f, 1/2.4);
val = 1.055 * tmp - 0.055;
}
l2s_map[i] = round(val * 255);
}
for (i=0; i<padding; i++) {
l2s_map[l2s_len + i] = 255;
}
}
/**
* Maps the given linear RGB float to sRGB integer.
*/
static unsigned char linear_sample_to_srgb(float in)
{
return l2s_map[(int)(in * (l2s_len - 1))];
}
/**
* Takes a sample value, an array of 4 coefficients & 4 accumulators, and
* adds the product of sample * coeffs[n] to each accumulator.
*/
static void add_sample_to_sum_f(float sample, float *coeffs, float *sum)
{
int i;
for (i=0; i<4; i++) {
sum[i] += sample * coeffs[i];
}
}
/**
* Takes an array of 4 floats and shifts them left. The rightmost element is
* set to the given value.
*/
static void push_f(float *f, float val)
{
f[0] = f[1];
f[1] = f[2];
f[2] = f[3];
f[3] = val;
}
/**
* Takes an array of 4 floats and shifts them left. The rightmost element is
* set to 0.0.
*/
static void shift_left_f(float *f)
{
push_f(f, 0.0f);
}
static void yscale_out_linear(float *sums, int len, unsigned char *out)
{
int i;
for (i=0; i<len; i++) {
out[i] = linear_sample_to_srgb(*sums);
shift_left_f(sums);
sums += 4;
}
}
static void yscale_out_nonlinear(float *sums, int sl_len, unsigned char *out)
{
int i;
for (i=0; i<sl_len; i++) {
out[i] = clamp8(*sums);
shift_left_f(sums);
sums += 4;
}
}
static void yscale_out_ga(float *sums, int width, unsigned char *out)
{
int i;
float alpha;
for (i=0; i<width; i++) {
alpha = clampf(sums[4]);
if (alpha != 0) {
sums[0] /= alpha;
}
out[0] = clamp8(sums[0]);
shift_left_f(sums);
out[1] = f2i(alpha * 255.0f);
shift_left_f(sums + 4);
sums += 8;
out += 2;
}
}
static void yscale_out_rgba(float *sums, int width, unsigned char *out)
{
int i, j;
float alpha;
for (i=0; i<width; i++) {
alpha = clampf(sums[12]);
if (alpha != 0) {
for (j=0; j<3; j++) {
sums[j * 4] /= alpha;
}
}
for (j=0; j<3; j++) {
out[j] = linear_sample_to_srgb(clampf(sums[j * 4]));
shift_left_f(sums + j * 4);
}
out[3] = round(alpha * 255.0f);
shift_left_f(sums + 12);
sums += 16;
out += 4;
}
}
static void yscale_out(float *sums, int width, unsigned char *out,
enum oil_colorspace cs)
{
int sl_len;
sl_len = width * OIL_CMP(cs);
switch(cs) {
case OIL_CS_G:
case OIL_CS_CMYK:
yscale_out_nonlinear(sums, sl_len, out);
break;
case OIL_CS_GA:
yscale_out_ga(sums, width, out);
break;
case OIL_CS_RGB:
case OIL_CS_RGBX:
yscale_out_linear(sums, sl_len, out);
break;
case OIL_CS_RGBA:
yscale_out_rgba(sums, width, out);
break;
case OIL_CS_UNKNOWN:
break;
}
}
/**
* Apply scanline "in" to y-scaling sums.
*/
static void yscale_down(float *in, int width, float *coeffs, float *sums,
enum oil_colorspace cs)
{
int i, sl_len;
sl_len = width * OIL_CMP(cs);
for (i=0; i<sl_len; i++) {
add_sample_to_sum_f(in[i], coeffs, sums);
sums += 4;
}
}
static void yscale_up_g_cmyk(float **in, int len, float *coeffs,
unsigned char *out)
{
int i;
float sum;
for (i=0; i<len; i++) {
sum = coeffs[0] * in[0][i] +
coeffs[1] * in[1][i] +
coeffs[2] * in[2][i] +
coeffs[3] * in[3][i];
out[i] = clamp8(sum);
}
}
static void yscale_up_ga(float **in, int len, float *coeffs,
unsigned char *out)
{
int i, j;
float alpha, sums[2];
for (i=0; i<len; i+=2) {
for (j=0; j<2; j++) {
sums[j] = coeffs[0] * in[0][i + j] +
coeffs[1] * in[1][i + j] +
coeffs[2] * in[2][i + j] +
coeffs[3] * in[3][i + j];
}
alpha = clampf(sums[1]);
if (alpha != 0) {
sums[0] /= alpha;
}
out[i] = clamp8(sums[0]);
out[i + 1] = f2i(alpha * 255.0f);
}
}
static void yscale_up_rgb(float **in, int len, float *coeffs,
unsigned char *out)
{
int i;
float sum;
for (i=0; i<len; i++) {
sum = coeffs[0] * in[0][i] +
coeffs[1] * in[1][i] +
coeffs[2] * in[2][i] +
coeffs[3] * in[3][i];
out[i] = linear_sample_to_srgb(sum);
}
}
static void yscale_up_rgbx(float **in, int len, float coeffs[4],
unsigned char *out)
{
int i, j;
float sum;
for (i=0; i<len; i+=4) {
for (j=0; j<3; j++) {
sum = coeffs[0] * in[0][i + j] +
coeffs[1] * in[1][i + j] +
coeffs[2] * in[2][i + j] +
coeffs[3] * in[3][i + j];
out[i + j] = linear_sample_to_srgb(sum);
}
out[i + 3] = 0;
}
}
static void yscale_up_rgba(float **in, int len, float *coeffs,
unsigned char *out)
{
int i, j;
float alpha, sums[4];
for (i=0; i<len; i+=4) {
for (j=0; j<4; j++) {
sums[j] = coeffs[0] * in[0][i + j] +
coeffs[1] * in[1][i + j] +
coeffs[2] * in[2][i + j] +
coeffs[3] * in[3][i + j];
}
alpha = clampf(sums[3]);
for (j=0; j<3; j++) {
if (alpha != 0 && alpha != 1.0f) {
sums[j] /= alpha;
sums[j] = clampf(sums[j]);
}
out[i + j] = linear_sample_to_srgb(sums[j]);
}
out[i + 3] = f2i(alpha * 255.0f);
}
}
/**
* Upscale a strip of scanlines. Branches to the correct interpolator using
* the given colorspace.
*/
static void yscale_up(float **in, int len, float *coeffs, unsigned char *out,
enum oil_colorspace cs)
{
switch(cs) {
case OIL_CS_G:
case OIL_CS_CMYK:
yscale_up_g_cmyk(in, len, coeffs, out);
break;
case OIL_CS_GA:
yscale_up_ga(in, len, coeffs, out);
break;
case OIL_CS_RGB:
yscale_up_rgb(in, len, coeffs, out);
break;
case OIL_CS_RGBX:
yscale_up_rgbx(in, len, coeffs, out);
break;
case OIL_CS_RGBA:
yscale_up_rgba(in, len, coeffs, out);
break;
case OIL_CS_UNKNOWN:
break;
}
}
/* horizontal scaling */
/**
* Holds pre-calculated mapping of sRGB chars to linear RGB floating point
* values.
*/
static float s2l_map[256];
/**
* Populates s2l_map.
*/
static void build_s2l(void)
{
int input;
double in_f, tmp, val;
for (input=0; input<=255; input++) {
in_f = input / 255.0;
if (in_f <= 0.040448236277) {
val = in_f / 12.92;
} else {
tmp = ((in_f + 0.055)/1.055);
val = pow(tmp, 2.4);
}
s2l_map[input] = val;
}
}
static float i2f_map[256];
static void build_i2f(void)
{
int i;
for (i=0; i<=255; i++) {
i2f_map[i] = i / 255.0f;
}
}
/**
* Given input & output dimensions, populate a buffer of coefficients and
* border counters.
*
* This method assumes that in_width >= out_width.
*
* It generates 4 * in_width coefficients -- 4 for every input sample.
*
* It generates out_width border counters, these indicate how many input
* samples to process before the next output sample is finished.
*/
static void xscale_calc_coeffs(int in_width, int out_width, float *coeff_buf,
int *border_buf, float *tmp_coeffs)
{
int smp_i, i, j, taps, offset, pos, ltrim, rtrim, smp_end, smp_start,
ends[4];
float tx;
taps = calc_taps(in_width, out_width);
for (i=0; i<4; i++) {
ends[i] = -1;
}
for (i=0; i<out_width; i++) {
smp_i = split_map(in_width, out_width, i, &tx);
smp_start = smp_i - (taps/2 - 1);
smp_end = smp_i + taps/2;
if (smp_end >= in_width) {
smp_end = in_width - 1;
}
ends[i%4] = smp_end;
border_buf[i] = smp_end - ends[(i+3)%4];
ltrim = 0;
if (smp_start < 0) {
ltrim = -1 * smp_start;
}
rtrim = smp_start + (taps - 1) - smp_end;
calc_coeffs(tmp_coeffs, tx, taps, ltrim, rtrim);
for (j=ltrim; j<taps - rtrim; j++) {
pos = smp_start + j;
offset = 3;
if (pos > ends[(i+3)%4]) {
offset = 0;
} else if (pos > ends[(i+2)%4]) {
offset = 1;
} else if (pos > ends[(i+1)%4]) {
offset = 2;
}
coeff_buf[pos * 4 + offset] = tmp_coeffs[j];
}
}
}
/**
* Precalculate coefficients and borders for an upscale.
*
* coeff_buf will be populated with 4 input coefficients for every output
* sample.
*
* border_buf will be populated with the number of output samples to produce
* for every input sample.
*
* users of coeff_buf & border_buf are expected to keep a buffer of the last 4
* input samples, and multiply them with each output sample's coefficients.
*/
static void scale_up_coeffs(int in_width, int out_width, float *coeff_buf,
int *border_buf)
{
int i, smp_i, start, end, ltrim, rtrim, safe_end, max_pos;
float tx;
max_pos = in_width - 1;
for (i=0; i<out_width; i++) {
smp_i = split_map(in_width, out_width, i, &tx);
start = smp_i - 1;
end = smp_i + 2;
// This is the border position at which we will tell the
// interpolator to calculate the output sample.
safe_end = min(end, max_pos);
ltrim = 0;
rtrim = 0;
if (start < 0) {
ltrim = -1 * start;
}
if (end > max_pos) {
rtrim = end - max_pos;
}
border_buf[safe_end] += 1;
// we offset coeff_buf by rtrim because the interpolator won't
// be pushing any more samples into its sample buffer.
calc_coeffs(coeff_buf + rtrim, tx, 4, ltrim, rtrim);
coeff_buf += 4;
}
}
/**
* Takes an array of n 4-element source arrays, writes the first element to the
* next n positions of the output address, and shifts the source arrays.
*/
static void dump_out(float *out, float sum[][4], int n)
{
int i;
for (i=0; i<n; i++) {
out[i] = sum[i][0];
shift_left_f(sum[i]);
}
}
static void xscale_down_rgbx(unsigned char *in, float *out,
int out_width, float *coeff_buf, int *border_buf)
{
int i, j, k;
float sum[3][4] = {{ 0.0f }};
for (i=0; i<out_width; i++) {
for (j=0; j<border_buf[i]; j++) {
for (k=0; k<3; k++) {
add_sample_to_sum_f(s2l_map[in[k]], coeff_buf, sum[k]);
}
in += 4;
coeff_buf += 4;
}
dump_out(out, sum, 3);
out += 4;
}
}
static void xscale_down_rgb(unsigned char *in, float *out,
int out_width, float *coeff_buf, int *border_buf)
{
int i, j, k;
float sum[3][4] = {{ 0.0f }};
for (i=0; i<out_width; i++) {
for (j=0; j<border_buf[i]; j++) {
for (k=0; k<3; k++) {
add_sample_to_sum_f(s2l_map[in[k]], coeff_buf, sum[k]);
}
in += 3;
coeff_buf += 4;
}
dump_out(out, sum, 3);
out += 3;
}
}
static void xscale_down_g(unsigned char *in, float *out,
int out_width, float *coeff_buf, int *border_buf)
{
int i, j;
float sum[4] = { 0.0f };
for (i=0; i<out_width; i++) {
for (j=0; j<border_buf[i]; j++) {
add_sample_to_sum_f(i2f_map[in[0]], coeff_buf, sum);
in += 1;
coeff_buf += 4;
}
out[0] = sum[0];
shift_left_f(sum);
out += 1;
}
}
static void xscale_down_cmyk(unsigned char *in, float *out,
int out_width, float *coeff_buf, int *border_buf)
{
int i, j, k;
float sum[4][4] = {{ 0.0f }};
for (i=0; i<out_width; i++) {
for (j=0; j<border_buf[i]; j++) {
for (k=0; k<4; k++) {
add_sample_to_sum_f(i2f_map[in[k]], coeff_buf, sum[k]);
}
in += 4;
coeff_buf += 4;
}
dump_out(out, sum, 4);
out += 4;
}
}
static void xscale_down_rgba(unsigned char *in, float *out,
int out_width, float *coeff_buf, int *border_buf)
{
int i, j, k;
float alpha, sum[4][4] = {{ 0.0f }};
for (i=0; i<out_width; i++) {
for (j=0; j<border_buf[i]; j++) {
alpha = i2f_map[in[3]];
for (k=0; k<3; k++) {
add_sample_to_sum_f(s2l_map[in[k]] * alpha, coeff_buf, sum[k]);
}
add_sample_to_sum_f(alpha, coeff_buf, sum[3]);
in += 4;
coeff_buf += 4;
}
dump_out(out, sum, 4);
out += 4;
}
}
static void xscale_down_ga(unsigned char *in, float *out,
int out_width, float *coeff_buf, int *border_buf)
{
int i, j;
float alpha, sum[2][4] = {{ 0.0f }};
for (i=0; i<out_width; i++) {
for (j=0; j<border_buf[i]; j++) {
alpha = i2f_map[in[1]];
add_sample_to_sum_f(i2f_map[in[0]] * alpha, coeff_buf, sum[0]);
add_sample_to_sum_f(alpha, coeff_buf, sum[1]);
in += 2;
coeff_buf += 4;
}
dump_out(out, sum, 2);
out += 2;
}
}
static void oil_xscale_down(unsigned char *in, float *out,
int width_out, enum oil_colorspace cs_in, float *coeff_buf,
int *border_buf)
{
switch(cs_in) {
case OIL_CS_RGBX:
xscale_down_rgbx(in, out, width_out, coeff_buf, border_buf);
break;
case OIL_CS_RGB:
xscale_down_rgb(in, out, width_out, coeff_buf, border_buf);
break;
case OIL_CS_G:
xscale_down_g(in, out, width_out, coeff_buf, border_buf);
break;
case OIL_CS_CMYK:
xscale_down_cmyk(in, out, width_out, coeff_buf, border_buf);
break;
case OIL_CS_RGBA:
xscale_down_rgba(in, out, width_out, coeff_buf, border_buf);
break;
case OIL_CS_GA:
xscale_down_ga(in, out, width_out, coeff_buf, border_buf);
break;
case OIL_CS_UNKNOWN:
break;
}
}
static void xscale_up_reduce_n(float in[][4], float *out, float *coeffs,
int cmp)
{
int i;
for (i=0; i<cmp; i++) {
out[i] = in[i][0] * coeffs[0] +
in[i][1] * coeffs[1] +
in[i][2] * coeffs[2] +
in[i][3] * coeffs[3];
}
}
static void xscale_up_rgbx(unsigned char *in, int width_in, float *out,
float *coeff_buf, int *border_buf)
{
int i, j;
float smp[3][4] = {{0}};
for (i=0; i<width_in; i++) {
for (j=0; j<3; j++) {
push_f(smp[j], s2l_map[in[j]]);
}
for (j=0; j<border_buf[i]; j++) {
xscale_up_reduce_n(smp, out, coeff_buf, 3);
out[3] = 0;
out += 4;
coeff_buf += 4;
}
in += 4;
}
}
static void xscale_up_rgb(unsigned char *in, int width_in, float *out,
float *coeff_buf, int *border_buf)
{
int i, j;
float smp[3][4] = {{0}};
for (i=0; i<width_in; i++) {
for (j=0; j<3; j++) {
push_f(smp[j], s2l_map[in[j]]);
}
for (j=0; j<border_buf[i]; j++) {
xscale_up_reduce_n(smp, out, coeff_buf, 3);
out += 3;
coeff_buf += 4;
}
in += 3;
}
}
static void xscale_up_cmyk(unsigned char *in, int width_in, float *out,
float *coeff_buf, int *border_buf)
{
int i, j;
float smp[4][4] = {{0}};
for (i=0; i<width_in; i++) {
for (j=0; j<4; j++) {
push_f(smp[j], in[j] / 255.0f);
}
for (j=0; j<border_buf[i]; j++) {
xscale_up_reduce_n(smp, out, coeff_buf, 4);
out += 4;
coeff_buf += 4;
}
in += 4;
}
}
static void xscale_up_rgba(unsigned char *in, int width_in, float *out,
float *coeff_buf, int *border_buf)
{
int i, j;
float smp[4][4] = {{0}};
for (i=0; i<width_in; i++) {
push_f(smp[3], in[3] / 255.0f);
for (j=0; j<3; j++) {
push_f(smp[j], smp[3][3] * s2l_map[in[j]]);
}
for (j=0; j<border_buf[i]; j++) {
xscale_up_reduce_n(smp, out, coeff_buf, 4);
out += 4;
coeff_buf += 4;
}
in += 4;
}
}
static void xscale_up_ga(unsigned char *in, int width_in, float *out,
float *coeff_buf, int *border_buf)
{
int i, j;
float smp[2][4] = {{0}};
for (i=0; i<width_in; i++) {
push_f(smp[1], in[1] / 255.0f);
push_f(smp[0], smp[1][3] * i2f_map[in[0]]);
for (j=0; j<border_buf[i]; j++) {
xscale_up_reduce_n(smp, out, coeff_buf, 2);
out += 2;
coeff_buf += 4;
}
in += 2;
}
}
static void xscale_up_g(unsigned char *in, int width_in, float *out,
float *coeff_buf, int *border_buf)
{
int i, j;
float smp[4] = {0};
for (i=0; i<width_in; i++) {
push_f(smp, in[i] / 255.0f);
for (j=0; j<border_buf[i]; j++) {
out[0] = smp[0] * coeff_buf[0] +
smp[1] * coeff_buf[1] +
smp[2] * coeff_buf[2] +
smp[3] * coeff_buf[3];
out += 1;
coeff_buf += 4;
}
}
}
static void oil_xscale_up(unsigned char *in, int width_in, float *out,
enum oil_colorspace cs_in, float *coeff_buf, int *border_buf)
{
switch(cs_in) {
case OIL_CS_RGBX:
xscale_up_rgbx(in, width_in, out, coeff_buf, border_buf);
break;
case OIL_CS_RGB:
xscale_up_rgb(in, width_in, out, coeff_buf, border_buf);
break;
case OIL_CS_G:
xscale_up_g(in, width_in, out, coeff_buf, border_buf);
break;
case OIL_CS_CMYK:
xscale_up_cmyk(in, width_in, out, coeff_buf, border_buf);
break;
case OIL_CS_RGBA:
xscale_up_rgba(in, width_in, out, coeff_buf, border_buf);
break;
case OIL_CS_GA:
xscale_up_ga(in, width_in, out, coeff_buf, border_buf);
break;
case OIL_CS_UNKNOWN:
break;
}
}
/* Global functions */
void oil_global_init()
{
build_s2l();
build_l2s();
build_i2f();
}
static int calc_coeffs_len(int in_dim, int out_dim)
{
return 4 * max(in_dim, out_dim) * sizeof(float);
}
static int calc_borders_len(int in_dim, int out_dim)
{
return min(in_dim, out_dim) * sizeof(int);
}
static void set_coeffs(int in_dim, int out_dim, float *coeffs, int *borders,
float *tmp)
{
if (out_dim <= in_dim) {
xscale_calc_coeffs(in_dim, out_dim, coeffs, borders, tmp);
} else {
scale_up_coeffs(in_dim, out_dim, coeffs, borders);
}
}
int oil_scale_init(struct oil_scale *os, int in_height, int out_height,
int in_width, int out_width, enum oil_colorspace cs)
{
int taps_x, taps_y, coeffs_x_len, coeffs_y_len, borders_x_len,
borders_y_len, rb_len, sums_len, tmp_len;
if (!os || in_height > MAX_DIMENSION || out_height > MAX_DIMENSION ||
in_height < 1 || out_height < 1 ||
in_width > MAX_DIMENSION || out_width > MAX_DIMENSION ||
in_width < 1 || out_width < 1) {
return -1;
}
// Lazy perform global init, in case oil_global_ini() hasn't been
// called yet.
if (!s2l_map[128]) {
oil_global_init();
}
taps_x = calc_taps(in_width, out_width);
taps_y = calc_taps(in_height, out_height);
coeffs_x_len = calc_coeffs_len(in_width, out_width);
borders_x_len = calc_borders_len(in_width, out_width);
coeffs_y_len = calc_coeffs_len(in_height, out_height);
borders_y_len = calc_borders_len(in_height, out_height);
rb_len = out_width * OIL_CMP(cs) * taps_y * sizeof(float);
tmp_len = max(taps_x, taps_y) * sizeof(float);
sums_len = 0;
if (out_height <= in_height) {
sums_len = out_width * OIL_CMP(cs) * 4 * sizeof(float);
rb_len = out_width * OIL_CMP(cs) * sizeof(float);
}
memset(os, 0, sizeof(struct oil_scale));
os->in_height = in_height;
os->out_height = out_height;
os->in_width = in_width;
os->out_width = out_width;
os->cs = cs;
os->coeffs_x = calloc(1, coeffs_x_len);
os->borders_x = calloc(1, borders_x_len);
os->coeffs_y = calloc(1, coeffs_y_len);
os->borders_y = calloc(1, borders_y_len);
os->rb = calloc(1, rb_len);
os->sums_y = calloc(1, sums_len);
os->tmp_coeffs = malloc(tmp_len);