-
Notifications
You must be signed in to change notification settings - Fork 0
/
dip.c
2462 lines (2289 loc) · 65.8 KB
/
dip.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
/* dip.c
* Digital Image Processing routines
* (c) 2000-2002 Karel 'Clock' Kulhavy
* This file is a part of the Links program, released under GPL.
* This does various utilities for digital image processing including font
* rendering.
*/
#include "cfg.h"
#ifdef G
#include "links.h"
#ifdef HAVE_MATH_H
#include <math.h>
#endif /* HAVE_MATH_H */
/* #define this if you want to report missing letters to stderr.
* Leave it commented up for normal operation and releases! */
/* #define REPORT_UNKNOWN 1 */
unsigned aspect=65536; /* aspect=65536 for 320x240
* aspect=157286 for 640x200 (tall pixels)
* Defines aspect ratio of screen pixels.
* aspect=(196608*xsize+ysize<<1)/(ysize<<1);
* Default is 65536 because we assume square pixel
* when not specified otherwise. Aspect is ratio of
* the pixel height (in milimeters) to pixel width,
* multiplied by 65536. */
/* Limitation: No input image's dimension may exceed 2^(32-1-8) pixels.
*/
/* Each input byte represents 1 byte (gray). The question whether 0 is
* black or 255 is black doesn't matter.
*/
/* These constants represent contrast-enhancement and sharpen filter (which is one filter
* together) that is applied onto the letters to enhance their appearance at low height.
* They were determined by experiment for several values, interpolated, checked and tuned.
* If you change them, don't wonder if the letters start to look strange.
* The numers in the comments denote which height the line applies for.
*/
static_const float fancy_constants[64]={
(float)0,(float)3, /* 1 */
(float).1,(float)3, /* 2 */
(float).2,(float)3, /* 3 */
(float).3,(float)2.9, /* 4 */
(float).4,(float)2.7, /* 5 */
(float).4,(float)2.5, /* 6 */
(float).4,(float)2, /* 7 */
(float).5,(float)2, /* 8 */
(float).4,(float)2, /* 9 */
(float).38,(float)1.9, /* 10 */
(float).36,(float)1.8, /* 11 */
(float).33,(float)1.7, /* 12 */
(float).30,(float)1.6, /* 13 */
(float).25,(float)1.5, /* 14 */
(float).20,(float)1.5, /* 15 */
(float).15,(float)1.5, /* 16 */
(float).14,(float)1.5, /* 17 */
(float).13,(float)1.5, /* 18 */
(float).12,(float)1.5, /* 19 */
(float).12,(float)1.5, /* 20 */
(float).12,(float)1.5, /* 21 */
(float).12,(float)1.5, /* 22 */
(float).11,(float)1.5, /* 23 */
(float).10,(float)1.4, /* 24 */
(float).09,(float)1.3, /* 25 */
(float).08,(float)1.3, /* 26 */
(float).04,(float)1.2, /* 27 */
(float).04,(float)1.2, /* 28 */
(float).02,(float)1.1, /* 29 */
(float).02,(float)1.1, /* 30 */
(float).01,(float)1, /* 31 */
(float).01,(float)1 /* 32 */
};
/* This shall be hopefully reasonably fast and portable
* We assume ix is <65536. If not, the letters will be smaller in
* horizontal dimension (because of overflow) but this will not cause
* a segfault. 65536 pixels wide bitmaps are not normal and will anyway
* exhaust the memory.
*/
static inline int compute_width(int ix, int iy, int required_height)
{
int width;
unsigned long reg;
reg=(unsigned long)aspect*(unsigned long)required_height;
if (reg>=0x1000000UL) {
/* It's big */
reg=(reg+32768)>>16;
width=(int)((reg*ix+(iy>>1))/iy);
}else{
/* It's small */
reg=(reg+128)>>8;
iy<<=8;
width=(int)((reg*ix+(iy>>1))/iy);
}
if (width<1) width=1;
return width;
}
static struct lru font_cache;
/* This is a cache for screen-ready colored bitmaps
* of lettrs and/or alpha channels for these (are the
* same size, only one byte per pixel and are used
* for letters with an image under them )
*/
#if defined(__i686__) || defined(__athlon__) || defined(__SSE2__) || defined(__x86_64__) || defined(__aarch64__) || defined(__alpha) || defined(__hppa)
/*
* On modern processors it is faster to do this in floating point.
* Do it only if we are sure that the coprocessor is present.
*/
typedef double scale_t;
#define USE_FP_SCALE
#elif defined(HAVE_LONG_LONG)
typedef unsigned long long scale_t;
#else
/*
* This may overflow, but systems without long long are very old
* and we will rather accept overflow on big images (65536 pixels)
* than slowing down the process unacceptably with possibly emulated FPU.
*/
typedef unsigned long scale_t;
#endif
/* Each input byte represents 1 byte (gray). The question whether 0 is
* black or 255 is black doesn't matter.
*/
static void add_col_gray(unsigned *my_restrict col_buf, unsigned char *my_restrict ptr, size_t line_skip, size_t n, unsigned weight)
{
for (;n;n--) {
*col_buf+=weight*(*ptr);
ptr+=line_skip;
col_buf++;
}
}
/* We assume unsigned short holds at least 16 bits. */
static void add_row_gray(unsigned *my_restrict row_buf, unsigned char *my_restrict ptr, size_t n, unsigned weight)
{
for (;n;n--) {
*row_buf+=weight**ptr;
ptr++;
row_buf++;
}
}
/* line_skip is in pixels. The column contains the whole pixels (R G B)
* We assume unsigned short holds at least 16 bits. */
#if defined(__ICC) && defined(USE_FP_SCALE)
/* ICC misoptimizes this function when inlining it */
ATTR_NOINLINE
#endif
static void add_col_color(scale_t *my_restrict col_buf,
#if defined(__GNUC__) && __GNUC__ == 3
volatile
#endif
unsigned short *my_restrict ptr,
size_t line_skip, size_t n, ulonglong weight)
{
if (!weight) return;
#ifndef USE_FP_SCALE
if (weight < 0x10000) {
unsigned short weight_16 = (unsigned short)weight;
for (;n;n--) {
col_buf[0]+=(scale_t)((unsigned)weight_16*(unsigned)ptr[0]);
col_buf[1]+=(scale_t)((unsigned)weight_16*(unsigned)ptr[1]);
col_buf[2]+=(scale_t)((unsigned)weight_16*(unsigned)ptr[2]);
ptr+=line_skip;
col_buf+=3;
}
} else
#endif
{
scale_t w = (scale_t)(longlong)weight;
for (;n;n--) {
col_buf[0]+=w*ptr[0];
col_buf[1]+=w*ptr[1];
col_buf[2]+=w*ptr[2];
ptr+=line_skip;
col_buf+=3;
}
}
}
/* n is in pixels. pixel is 3 unsigned shorts in series */
/* We assume unsigned short holds at least 16 bits. */
static void add_row_color(scale_t *my_restrict row_buf,
#if defined(__GNUC__) && __GNUC__ == 3
volatile
#endif
unsigned short *my_restrict ptr,
size_t n, ulonglong weight)
{
if (!weight) return;
#ifndef USE_FP_SCALE
if (weight < 0x10000) {
unsigned short weight_16 = (unsigned short)weight;
for (;n;n--) {
row_buf[0]+=(scale_t)((unsigned)weight_16*(unsigned)ptr[0]);
row_buf[1]+=(scale_t)((unsigned)weight_16*(unsigned)ptr[1]);
row_buf[2]+=(scale_t)((unsigned)weight_16*(unsigned)ptr[2]);
ptr+=3;
row_buf+=3;
}
} else
#endif
{
scale_t w = (scale_t)(longlong)weight;
for (;n;n--) {
row_buf[0]+=w*ptr[0];
row_buf[1]+=w*ptr[1];
row_buf[2]+=w*ptr[2];
ptr+=3;
row_buf+=3;
}
}
}
/* We assume unsigned holds at least 32 bits */
static void emit_and_bias_col_gray(unsigned *my_restrict col_buf, unsigned char *my_restrict out,
size_t line_skip, size_t n, unsigned weight)
{
unsigned half=weight>>1;
for (;n;n--) {
*out=*col_buf/weight;
out+=line_skip;
*col_buf++=half;
}
}
/* We assume unsigned holds at least 32 bits */
static void emit_and_bias_row_gray(unsigned *my_restrict row_buf, unsigned char *my_restrict out,
size_t n, unsigned weight)
{
unsigned half=weight>>1;
for (;n;n--) {
*out++=*row_buf/weight;
*row_buf++=half;
}
}
/* We assume unsigned holds at least 32 bits */
static void bias_buf_gray(unsigned *my_restrict col_buf, size_t n, unsigned half)
{
for (;n;n--) *col_buf++=half;
}
/* We assume unsigned holds at least 32 bits */
static void bias_buf_color(scale_t *my_restrict col_buf, size_t n, scale_t half)
{
for (;n;n--) {
col_buf[0]=half;
col_buf[1]=half;
col_buf[2]=half;
col_buf+=3;
}
/* System activated */
}
#ifdef USE_FP_SCALE
#define op(x) ((x) * inv_weight)
#else
#define op(x) (sizeof(unsigned long) < sizeof(scale_t) && (x) == (unsigned long)(x) ? (unsigned long)(x) / weight : (x) / weight)
#endif
/* line skip is in pixels. Pixel is 3*unsigned short */
/* We assume unsigned holds at least 32 bits */
/* We assume unsigned short holds at least 16 bits. */
static void emit_and_bias_col_color(scale_t *my_restrict col_buf,
unsigned short *my_restrict out, size_t line_skip, size_t n, unsigned weight)
{
scale_t half=(scale_t)weight / 2;
#ifdef USE_FP_SCALE
scale_t inv_weight = (scale_t)1 / weight;
#endif
for (;n;n--) {
out[0]=(unsigned short)op(col_buf[0]);
col_buf[0]=half;
out[1]=(unsigned short)op(col_buf[1]);
col_buf[1]=half;
/* The following line is an enemy of the State and will be
* prosecuted according to the Constitution of The United States
* Cap. 20/3 ix. Sel. Bill 12/1920
* Moses 12/20 Erizea farizea 2:2:1:14
*/
out[2]=(unsigned short)op(col_buf[2]);
col_buf[2]=half;
out+=line_skip;
col_buf+=3;
}
}
/* n is in pixels. pixel is 3 unsigned shorts in series. */
/* We assume unsigned holds at least 32 bits */
/* We assume unsigned short holds at least 16 bits. */
static void emit_and_bias_row_color(scale_t *my_restrict row_buf,
unsigned short *my_restrict out, size_t n, unsigned weight)
{
scale_t half=(scale_t)weight / 2;
#ifdef USE_FP_SCALE
scale_t inv_weight = (scale_t)1 / weight;
#endif
for (;n;n--) {
out[0]=(unsigned short)op(row_buf[0]);
row_buf[0]=half;
out[1]=(unsigned short)op(row_buf[1]);
row_buf[1]=half;
out[2]=(unsigned short)op(row_buf[2]);
row_buf[2]=half;
out+=3;
row_buf+=3;
}
}
#undef op
/* For enlargement only -- does linear filtering.
* Allocates output and frees input.
* We assume unsigned holds at least 32 bits */
static void enlarge_gray_horizontal(unsigned char *in, size_t ix, size_t y, unsigned char **out, size_t ox)
{
unsigned *col_buf;
size_t total;
size_t out_pos,in_pos,in_begin,in_end;
unsigned half = (unsigned)((ox - 1) >> 1);
unsigned char *outptr;
unsigned char *inptr;
if (ox && ox * y / ox != y) overalloc();
if (ox * y > MAX_SIZE_T) overalloc();
outptr=mem_alloc(ox * y);
inptr=in;
*out=outptr;
if (ix==1) {
/* Dull copying */
for (;y;y--) {
memset(outptr,*inptr,ox);
outptr+=ox;
inptr++;
}
mem_free(in);
}else{
total=(ix-1)*(ox-1);
if (y > MAX_SIZE_T / sizeof(*col_buf)) overalloc();
col_buf=mem_alloc(y * sizeof(*col_buf));
bias_buf_gray(col_buf, y, half);
out_pos=0;
in_pos=0;
again:
in_begin=in_pos;
in_end=in_pos+ox-1;
add_col_gray(col_buf, inptr, ix, y, (unsigned)(in_end - out_pos));
add_col_gray(col_buf, inptr + 1, ix, y, (unsigned)(out_pos - in_begin));
emit_and_bias_col_gray(col_buf, outptr, ox, y, (unsigned)(ox - 1));
outptr++;
out_pos+=ix-1;
if (out_pos>in_end) {
in_pos=in_end;
inptr++;
}
if (out_pos>total) {
mem_free(in);
mem_free(col_buf);
return;
}
goto again;
}
/* Rohan, oh Rohan... */
/* ztracena zeme :) */
}
static inline longlong multiply_int(size_t a, size_t b)
{
#ifndef HAVE_LONG_LONG
volatile
#endif
longlong result = (ulonglong)a * (ulonglong)b;
#if !defined(__TINYC__) && !defined(HAVE_LONG_LONG)
if (result / a != (longlong)b) {
/*fprintf(stderr, "%lld, %lld, %d, %d\n", result / a, result, a, b);*/
overflow();
}
#endif
#ifndef HAVE_LONG_LONG
if (result == result + 1 || result == result - 1)
overflow();
#endif
return result;
}
/* For enlargement only -- does linear filtering
* Frees input and allocates output.
* We assume unsigned holds at least 32 bits
*/
static void enlarge_color_horizontal(unsigned short *in, size_t ix, size_t y,
unsigned short **outa, size_t ox)
{
#ifdef HAVE_OPENMP
int use_omp;
#endif
int n_threads;
size_t alloc_size;
scale_t *col_buf;
size_t a;
size_t skip=3*ix;
size_t oskip=3*ox;
unsigned short *out;
if (!in) {
*outa = NULL;
return;
}
if (ix==ox) {
*outa=in;
return;
}
if (ox && ox * y / ox != y) overalloc();
if (ox * y > MAX_SIZE_T / 3 / sizeof(*out)) overalloc();
out = mem_alloc_mayfail(sizeof(*out) * 3 * ox * y);
*outa=out;
if (!out) {
mem_free(in);
return;
}
if (ix==1) {
unsigned short *inp = in;
for (;y;y--,inp+=3) for (a=ox;a;a--,out+=3) {
out[0]=inp[0];
out[1]=inp[1];
out[2]=inp[2];
}
mem_free(in);
return;
}
multiply_int(ix-1,ox-1);
n_threads = omp_start();
#ifdef HAVE_OPENMP
use_omp = !OPENMP_NONATOMIC & (n_threads > 1) & (ox >= 24);
if (!use_omp)
n_threads = 1;
#endif
if (y > (MAX_SIZE_T - SMP_ALIGN + 1) / 3 / sizeof(*col_buf)) overalloc();
alloc_size = y * 3 * sizeof(*col_buf);
alloc_size = (alloc_size + SMP_ALIGN - 1) & ~(SMP_ALIGN - 1);
if (alloc_size > MAX_SIZE_T / n_threads) overalloc();
col_buf = mem_alloc_mayfail(alloc_size * n_threads);
if (!col_buf) goto skip_omp;
#ifdef HAVE_OPENMP
#pragma omp parallel default(none) firstprivate(col_buf,alloc_size,in,out,ix,ox,y,skip,oskip) if (use_omp)
#endif
{
scale_t *thread_col_buf;
ssize_t out_idx;
thread_col_buf = (scale_t *)((char *)col_buf + alloc_size * omp_get_thread_num());
bias_buf_color(thread_col_buf, y, (scale_t)(ox - 1) / 2);
#ifdef HAVE_OPENMP
#pragma omp for nowait
#endif
for (out_idx = 0; out_idx <= (ssize_t)(ox - 1); out_idx++) {
ulonglong out_pos, in_pos, in_end;
size_t in_idx;
out_pos = (ulonglong)out_idx * (ix - 1);
if (out_idx)
in_idx = (out_pos - 1) / (ox - 1);
else
in_idx = 0;
in_pos = (ulonglong)in_idx * (ox - 1);
in_end = in_pos + (ox - 1);
add_col_color(thread_col_buf, in + in_idx * 3, skip, y,
in_end - out_pos);
add_col_color(thread_col_buf, in + (in_idx + 1) * 3, skip, y,
out_pos - in_pos);
emit_and_bias_col_color(thread_col_buf, out + out_idx * 3, oskip, y, (unsigned)(ox - 1));
}
}
skip_omp:
omp_end();
mem_free(in);
if (col_buf) mem_free(col_buf);
else {
mem_free(out);
*outa = NULL;
}
}
/* Works for both enlarging and diminishing. Linear resample, no low pass.
* Automatically mem_frees the "in" and allocates "out". */
/* We assume unsigned holds at least 32 bits */
static void scale_gray_horizontal(unsigned char *in, size_t ix, size_t y, unsigned char **out, size_t ox)
{
unsigned *col_buf;
size_t total=ix * ox;
size_t out_pos, in_pos, in_begin, in_end, out_end;
unsigned char *outptr;
unsigned char *inptr;
if (ix<ox) {
enlarge_gray_horizontal(in,ix,y,out,ox);
return;
}else if (ix==ox) {
*out=in;
return;
}
if (ox && ox * y / ox != y) overalloc();
if (ox * y > MAX_SIZE_T) overalloc();
outptr=mem_alloc(ox*y);
inptr=in;
*out=outptr;
if (y > MAX_SIZE_T / sizeof(*col_buf)) overalloc();
col_buf = mem_alloc(y * sizeof(*col_buf));
bias_buf_gray(col_buf, y, (unsigned)(ix >> 1));
out_pos=0;
in_pos=0;
again:
in_begin=in_pos;
in_end=in_pos+ox;
out_end=out_pos+ix;
if (in_begin<out_pos)in_begin=out_pos;
if (in_end>out_end)in_end=out_end;
add_col_gray(col_buf, inptr, ix, y, (unsigned)(in_end - in_begin));
in_end=in_pos+ox;
if (out_end>=in_end) {
in_pos=in_end;
inptr++;
}
if (out_end<=in_end) {
emit_and_bias_col_gray(col_buf, outptr, ox, y, (unsigned)ix);
out_pos=out_pos+ix;
outptr++;
}
if (out_pos==total) {
mem_free(in);
mem_free(col_buf);
return;
}
goto again;
}
/* Works for both enlarging and diminishing. Linear resample, no low pass.
* Does only one color component.
* Frees ina and allocates outa.
* If ox*3<=ix, and display_optimize, performs optimization for LCD.
*/
static void scale_color_horizontal(unsigned short *in, size_t ix, size_t y, unsigned short **outa, size_t ox)
{
#ifdef HAVE_OPENMP
int use_omp;
#endif
int n_threads;
size_t alloc_size;
scale_t *col_buf;
size_t skip = 3 * ix;
size_t oskip = 3 * ox;
unsigned short *out;
if (!in) {
*outa = NULL;
return;
}
if (ix==ox) {
*outa=in;
return;
}
if (ix<ox) {
enlarge_color_horizontal(in,ix,y,outa,ox);
return;
}
multiply_int(ix,ox);
if (ox && ox * y / ox != y) overalloc();
if (ox * y > MAX_SIZE_T / 3 / sizeof(*out)) overalloc();
out = mem_alloc_mayfail(sizeof(*out) * 3 * ox * y);
*outa=out;
if (!out) {
mem_free(in);
return;
}
n_threads = omp_start();
#ifdef HAVE_OPENMP
use_omp = !OPENMP_NONATOMIC & (n_threads > 1) & (ox >= 24);
if (!use_omp)
n_threads = 1;
#endif
if (y > (MAX_SIZE_T - SMP_ALIGN + 1) / 3 / sizeof(*col_buf)) overalloc();
alloc_size = y * 3 * sizeof(*col_buf);
alloc_size = (alloc_size + SMP_ALIGN - 1) & ~(SMP_ALIGN - 1);
if (alloc_size > MAX_SIZE_T / n_threads) overalloc();
col_buf = mem_alloc_mayfail(alloc_size * n_threads);
if (!col_buf) goto skip_omp;
#ifdef HAVE_OPENMP
#pragma omp parallel default(none) firstprivate(col_buf,alloc_size,in,out,ix,ox,y,skip,oskip) if (use_omp)
#endif
{
scale_t *thread_col_buf;
ssize_t out_idx;
thread_col_buf = (scale_t *)((char *)col_buf + alloc_size * omp_get_thread_num());
bias_buf_color(thread_col_buf, y, (scale_t)ix / 2);
#ifdef HAVE_OPENMP
#pragma omp for nowait
#endif
for (out_idx = 0; out_idx < (ssize_t)ox; out_idx++) {
ulonglong out_pos, out_end, in_pos;
size_t in_idx;
out_pos = (ulonglong)out_idx * ix;
out_end = out_pos + ix;
in_idx = out_pos / ox;
in_pos = (ulonglong)in_idx * ox;
do {
ulonglong in_begin, in_end;
in_begin = in_pos;
in_end = in_pos + ox;
if (in_begin < out_pos) in_begin = out_pos;
if (in_end > out_end) in_end = out_end;
add_col_color(thread_col_buf, in + in_idx * 3, skip, y, in_end - in_begin);
in_idx++;
in_pos += ox;
} while (in_pos < out_end);
emit_and_bias_col_color(thread_col_buf, out + out_idx * 3, oskip, y, (unsigned)ix);
}
}
skip_omp:
omp_end();
mem_free(in);
if (col_buf) mem_free(col_buf);
else {
mem_free(out);
*outa = NULL;
}
}
/* For magnification only. Does linear filtering. */
/* We assume unsigned holds at least 32 bits */
static void enlarge_gray_vertical(unsigned char *in, size_t x, size_t iy, unsigned char **out, size_t oy)
{
unsigned *row_buf;
size_t total;
size_t out_pos,in_pos,in_begin,in_end;
unsigned half = (unsigned)((oy - 1) >> 1);
unsigned char *outptr;
unsigned char *inptr;
if (iy==1) {
if (x && x * oy / x != oy) overalloc();
if (x * oy > MAX_SIZE_T) overalloc();
outptr = mem_alloc(oy * x);
*out=outptr;
for(;oy;oy--,outptr+=x)
memcpy(outptr,in,x);
mem_free(in);
}
else if (iy==oy) {
*out=in;
}else{
if (x && x * oy / x != oy) overalloc();
if (x * oy > MAX_SIZE_T) overalloc();
outptr = mem_alloc(oy * x);
inptr=in;
*out=outptr;
total=(iy-1)*(oy-1);
if (x > MAX_SIZE_T / sizeof(*row_buf)) overalloc();
row_buf=mem_alloc(x * sizeof(*row_buf));
bias_buf_gray(row_buf, x, half);
out_pos=0;
in_pos=0;
again:
in_begin=in_pos;
in_end=in_pos+oy-1;
add_row_gray(row_buf, inptr, x, (unsigned)(in_end - out_pos));
add_row_gray(row_buf, inptr + x, x, (unsigned)(out_pos - in_begin));
emit_and_bias_row_gray(row_buf, outptr, x, (unsigned)(oy - 1));
outptr+=x;
out_pos+=iy-1;
if (out_pos>in_end) {
in_pos=in_end;
inptr+=x;
}
if (out_pos>total) {
mem_free(in);
mem_free(row_buf);
return;
}
goto again;
}
}
/* For magnification only. Does linear filtering */
/* We assume unsigned holds at least 32 bits */
static void enlarge_color_vertical(unsigned short *in, size_t x, size_t iy, unsigned short **outa, size_t oy)
{
#ifdef HAVE_OPENMP
int use_omp;
#endif
int n_threads;
size_t alloc_size;
scale_t *row_buf;
unsigned short *out;
if (!in) {
*outa = NULL;
return;
}
if (iy==oy) {
*outa=in;
return;
}
/* Rivendell */
if (x && x * oy / x != oy) overalloc();
if (x * oy > MAX_SIZE_T / 3 / sizeof(*out)) overalloc();
out = mem_alloc_mayfail(sizeof(*out) * 3 * oy * x);
*outa=out;
if (!out) {
mem_free(in);
return;
}
if (iy==1) {
for (;oy;oy--) {
memcpy(out,in,3*x*sizeof(*out));
out+=3*x;
}
mem_free(in);
return;
}
multiply_int(iy-1,oy-1);
n_threads = omp_start();
#ifdef HAVE_OPENMP
use_omp = (!OPENMP_NONATOMIC | !(x & 3)) & (n_threads > 1) & (oy >= 24);
if (!use_omp)
n_threads = 1;
#endif
if (x > (MAX_SIZE_T - SMP_ALIGN + 1) / 3 / sizeof(*row_buf)) overalloc();
alloc_size = x * 3 * sizeof(*row_buf);
alloc_size = (alloc_size + SMP_ALIGN - 1) & ~(SMP_ALIGN - 1);
if (alloc_size > MAX_SIZE_T / n_threads) overalloc();
row_buf = mem_alloc_mayfail(alloc_size * n_threads);
if (!row_buf) goto skip_omp;
#ifdef HAVE_OPENMP
#pragma omp parallel default(none) firstprivate(row_buf,alloc_size,in,out,x,iy,oy) if (use_omp)
#endif
{
scale_t *thread_row_buf;
ssize_t out_idx;
thread_row_buf = (scale_t *)((char *)row_buf + alloc_size * omp_get_thread_num());
bias_buf_color(thread_row_buf,x,(scale_t)(oy-1) / 2);
#ifdef HAVE_OPENMP
#pragma omp for nowait
#endif
for (out_idx = 0; out_idx <= (ssize_t)(oy - 1); out_idx++) {
ulonglong out_pos, in_pos, in_end;
size_t in_idx;
out_pos = (ulonglong)out_idx * (iy - 1);
if (out_idx)
in_idx = (out_pos - 1) / (oy - 1);
else
in_idx = 0;
in_pos = (ulonglong)in_idx * (oy - 1);
in_end = in_pos + oy - 1;
add_row_color(thread_row_buf, in + in_idx * 3 * x, x,
in_end - out_pos);
add_row_color(thread_row_buf, in + (in_idx + 1) * 3 * x, x,
out_pos - in_pos);
emit_and_bias_row_color(thread_row_buf, out + out_idx * 3 * x, x, (unsigned)(oy - 1));
}
}
skip_omp:
omp_end();
mem_free(in);
if (row_buf) mem_free(row_buf);
else {
mem_free(out);
*outa = NULL;
}
}
/* Both enlarges and diminishes. Linear filtering.
* Automatically allocates output and frees input.
* We assume unsigned holds at least 32 bits */
static void scale_gray_vertical(unsigned char *in, size_t x, size_t iy, unsigned char **out, size_t oy)
{
unsigned *row_buf;
size_t total=iy*oy;
size_t out_pos, in_pos, in_begin, in_end, out_end;
unsigned char *outptr;
unsigned char *inptr;
/* Snow White, Snow White... */
if (iy<oy) {
enlarge_gray_vertical(in,x,iy,out,oy);
return;
}
if (iy==oy) {
*out=in;
return;
}
if (x && x * oy / x != oy) overalloc();
if (x * oy > MAX_SIZE_T) overalloc();
outptr=mem_alloc(x*oy);
inptr=in;
*out=outptr;
if (x > MAX_SIZE_T / sizeof(*row_buf)) overalloc();
row_buf = mem_calloc(x * sizeof(*row_buf));
bias_buf_gray(row_buf, x, (unsigned)(iy >> 1));
out_pos=0;
in_pos=0;
again:
in_begin=in_pos;
in_end=in_pos+oy;
out_end=out_pos+iy;
if (in_begin<out_pos)in_begin=out_pos;
if (in_end>out_end)in_end=out_end;
add_row_gray(row_buf, inptr, x, (unsigned)(in_end - in_begin));
in_end=in_pos+oy;
if (out_end>=in_end) {
in_pos=in_end;
inptr+=x;
}
if (out_end<=in_end) {
emit_and_bias_row_gray(row_buf, outptr, x, (unsigned)iy);
out_pos=out_pos+iy;
outptr+=x;
}
if (out_pos==total) {
mem_free(in);
mem_free(row_buf);
return;
}
goto again;
}
/* Both enlarges and diminishes. Linear filtering. Sizes are
in pixels. Sizes are not in bytes. 1 pixel=3 unsigned shorts.
We assume unsigned short can hold at least 16 bits.
We assume unsigned holds at least 32 bits.
*/
static void scale_color_vertical(unsigned short *in, size_t x, size_t iy, unsigned short **outa, size_t oy)
{
#ifdef HAVE_OPENMP
int use_omp;
#endif
int n_threads;
size_t alloc_size;
scale_t *row_buf;
unsigned short *out;
if (!in) {
*outa = NULL;
return;
}
if (iy==oy) {
*outa=in;
return;
}
if (iy<oy) {
enlarge_color_vertical(in,x,iy,outa,oy);
return;
}
multiply_int(iy,oy);
if (x && x * oy / x != oy) overalloc();
if (x * oy > MAX_SIZE_T / 3 / sizeof(*out)) overalloc();
out = mem_alloc_mayfail(sizeof(*out) * 3 * oy * x);
*outa=out;
if (!out) {
mem_free(in);
return;
}
n_threads = omp_start();
#ifdef HAVE_OPENMP
use_omp = (!OPENMP_NONATOMIC | !(x & 3)) & (n_threads > 1) & (oy >= 24);
if (!use_omp)
n_threads = 1;
#endif
if (x > (MAX_SIZE_T - SMP_ALIGN + 1) / 3 / sizeof(*row_buf)) overalloc();
alloc_size = x * 3 * sizeof(*row_buf);
alloc_size = (alloc_size + SMP_ALIGN - 1) & ~(SMP_ALIGN - 1);
if (alloc_size > MAX_SIZE_T / n_threads) overalloc();
row_buf = mem_alloc_mayfail(alloc_size * n_threads);
if (!row_buf) goto skip_omp;
#ifdef HAVE_OPENMP
#pragma omp parallel default(none) firstprivate(row_buf,alloc_size,in,out,x,iy,oy) if (use_omp)
#endif
{
scale_t *thread_row_buf;
ssize_t out_idx;
thread_row_buf = (scale_t *)((char *)row_buf + alloc_size * omp_get_thread_num());
bias_buf_color(thread_row_buf,x,(scale_t)iy / 2);
#ifdef HAVE_OPENMP
#pragma omp for nowait
#endif
for (out_idx = 0; out_idx < (ssize_t)oy; out_idx++) {
ulonglong out_pos, out_end, in_pos;
size_t in_idx;
out_pos = (ulonglong)out_idx * iy;
out_end = out_pos + iy;
in_idx = out_pos / oy;
in_pos = (ulonglong)in_idx * oy;
do {
ulonglong in_begin, in_end;
in_begin = in_pos;
in_end = in_pos + oy;
if (in_begin < out_pos) in_begin = out_pos;
if (in_end > out_end) in_end = out_end;
add_row_color(thread_row_buf, in + in_idx * 3 * x, x, in_end - in_begin);
in_idx++;
in_pos += oy;
} while (in_pos < out_end);
emit_and_bias_row_color(thread_row_buf, out + out_idx * 3 * x, x, (unsigned)iy);
}
}
skip_omp:
omp_end();
mem_free(in);
if (row_buf) mem_free(row_buf);
else {
mem_free(out);
*outa = NULL;
}
}
/* Scales grayscale 8-bit map. Both enlarges and diminishes. Uses either low
* pass or bilinear filtering. Automatically mem_frees the "in".
* Automatically allocates "out".
*/
static void scale_gray(unsigned char *in, size_t ix, size_t iy, unsigned char **out, size_t ox, size_t oy)
{
unsigned char *intermediate_buffer;
if (!ix||!iy) {
if (in) mem_free(in);
if (ox && ox * oy / ox != oy) overalloc();
if (ox * oy > MAX_SIZE_T) overalloc();
*out = mem_calloc(ox * oy);
return;
}
if (ix*oy<ox*iy) {
scale_gray_vertical(in,ix,iy,&intermediate_buffer,oy);
scale_gray_horizontal(intermediate_buffer,ix,oy,out,ox);
}else{
scale_gray_horizontal(in,ix,iy,&intermediate_buffer,ox);
scale_gray_vertical(intermediate_buffer,ox,iy,out,oy);
}
}
/* To be called only when global variable display_optimize is 1 or 2.
* Performs a decimation according to this variable. Data shrink to 1/3
* and x is the smaller width.
* There must be 9*x*y unsigned shorts of data.
* x must be >=1.
* Performs realloc onto the buffer after decimation to save memory.
*/
static void decimate_3(unsigned short **data0, size_t x, size_t y)
{
unsigned short *data=*data0;
unsigned short *ahead=data;
size_t i, futuresize;
if (!data)
return;
if (x && (size_t)x * (size_t)y / (size_t)x != (size_t)y) overalloc();
if ((size_t)x * (size_t)y > MAX_SIZE_T / 3 / sizeof(**data0)) overalloc();
futuresize = x * y * 3 * sizeof(**data0);
#ifdef DEBUG
if (!(x>0&&y>0)) internal_error("zero width or height in decimate_3");
#endif /* #Ifdef DEBUG */
if (display_optimize==1) {
if (x==1) {
for (;y;y--,ahead+=9,data+=3) {
data[0]=(ahead[0]+ahead[0]+ahead[3])/3;
data[1]=(ahead[1]+ahead[4]+ahead[7])/3;
data[2]=(ahead[5]+ahead[8]+ahead[8])/3;
}
}else{
for (;y;y--) {
data[0]=(ahead[0]+ahead[0]+ahead[3])/3;
data[1]=(ahead[1]+ahead[4]+ahead[7])/3;
data[2]=(ahead[5]+ahead[8]+ahead[11])/3;
for (ahead+=9,data+=3,i=x-2;i;i--,ahead+=9,data+=3) {
data[0]=(ahead[-3]+ahead[0]+ahead[3])/3;
data[1]=(ahead[1]+ahead[4]+ahead[7])/3;
data[2]=(ahead[5]+ahead[8]+ahead[11])/3;