-
-
Notifications
You must be signed in to change notification settings - Fork 649
/
vips7compat.c
5916 lines (4929 loc) · 100 KB
/
vips7compat.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
/* compat stuff for vips7
*
* 4/3/11
* - hacked up
*/
/*
This file is part of VIPS.
VIPS 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; either version 2 of the License, or
(at your option) any later version.
This program 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 program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
/*
These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
*/
/*
#define VIPS_DEBUG
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /*HAVE_CONFIG_H*/
#include <glib/gi18n-lib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <vips/vips.h>
#include <vips/vips7compat.h>
#include <vips/internal.h>
#include <vips/debug.h>
#include <vips/transform.h>
/* Split filename into name / mode components. name and mode should both be
* FILENAME_MAX chars.
*
* We look for the ':' splitting the name and mode by searching for the
* rightmost occurrence of the regexp "\.[A-Za-z0-9]+:". The initial dot can
* be missing if there's a dirsep or start of string, meaning this is a
* filename without an extension.
*
* Examples:
*
* c:\silly:dir:name\fr:ed.tif:jpeg:95,,,,c:\icc\srgb.icc
* -> c:\silly:dir:name\fr:ed.tif jpeg:95,,,,c:\icc\srgb.icc
* I180:
* -> I180 ""
* c:\silly:
* -> c:\silly ""
* c:\silly
* -> c:\silly ""
* C:\fixtures\2569067123_aca715a2ee_o.jpg
* -> C:\fixtures\2569067123_aca715a2ee_o.jpg ""
*
* vips8 handles this in a much better way :(
*/
void
im_filename_split(const char *path, char *name, char *mode)
{
char *p;
size_t len;
vips_strncpy(name, path, FILENAME_MAX);
strcpy(mode, "");
if ((len = strlen(name)) == 0)
return;
/* Search backwards towards start, stopping at each ':' char.
*/
for (p = name + len - 1; p > name; p -= 1)
if (*p == ':') {
char *q;
/* We are skipping back over the file extension,
* isalnum() is probably sufficient.
*/
for (q = p - 1; isalnum(*q) && q > name; q -= 1)
;
if (*q == '.') {
break;
}
/* All the way back to the start? We probably have a
* filename with no extension, eg. "I180:"
*/
if (q == name)
break;
/* .. or we could hit a dirsep. Allow win or nix
* separators.
*/
if (*q == '/' ||
*q == '\\')
break;
}
/* Ignore a ':' in column 1, it's probably a drive letter on a
* Windows path.
*/
if (*p == ':' &&
p - name != 1) {
vips_strncpy(mode, p + 1, FILENAME_MAX);
*p = '\0';
}
}
/**
* vips_path_filename7:
* @path: path to split
*
* Return the filename part of a vips7 path. For testing only.
*/
char *
vips_path_filename7(const char *path)
{
char name[FILENAME_MAX];
char mode[FILENAME_MAX];
im_filename_split(path, name, mode);
return g_strdup(name);
}
/**
* vips_path_mode7:
* @path: path to split
*
* Return the mode part of a vips7 path. For testing only.
*/
char *
vips_path_mode7(const char *path)
{
char name[FILENAME_MAX];
char mode[FILENAME_MAX];
im_filename_split(path, name, mode);
return g_strdup(mode);
}
/* Skip any leading path stuff. Horrible: if this is a filename which came
* from win32 and we're a *nix machine, it'll have '\\' not '/' as the
* separator :-(
*
* Try to fudge this ... if the file doesn't contain any of our native
* separators, look for the opposite one as well. If there are none of those
* either, just return the filename.
*/
const char *
im_skip_dir(const char *path)
{
char name[FILENAME_MAX];
char mode[FILENAME_MAX];
const char *p;
const char *q;
const char native_dir_sep = G_DIR_SEPARATOR;
const char non_native_dir_sep = native_dir_sep == '/' ? '\\' : '/';
/* Remove any trailing save modifiers: we don't want '/' or '\' in the
* modifier confusing us.
*/
im_filename_split(path, name, mode);
/* The '\0' char at the end of the string.
*/
p = name + strlen(name);
/* Search back for the first native dir sep, or failing that, the first
* non-native dir sep.
*/
for (q = p; q > name && q[-1] != native_dir_sep; q--)
;
if (q == name)
for (q = p; q > name && q[-1] != non_native_dir_sep; q--)
;
return path + (q - name);
}
/* Extract suffix from filename, ignoring any mode string. Suffix should be
* FILENAME_MAX chars. Include the "." character, if any.
*/
void
im_filename_suffix(const char *path, char *suffix)
{
char name[FILENAME_MAX];
char mode[FILENAME_MAX];
char *p;
im_filename_split(path, name, mode);
if ((p = strrchr(name, '.')))
strcpy(suffix, p);
else
strcpy(suffix, "");
}
/* Does a filename have one of a set of suffixes. Ignore case.
*/
int
im_filename_suffix_match(const char *path, const char *suffixes[])
{
char suffix[FILENAME_MAX];
const char **p;
im_filename_suffix(path, suffix);
for (p = suffixes; *p; p++)
if (g_ascii_strcasecmp(suffix, *p) == 0)
return 1;
return 0;
}
/* p points to the start of a buffer ... move it on through the buffer (ready
* for the next call), and return the current option (or NULL for option
* missing). ',' characters inside options can be escaped with a '\'.
*/
char *
im_getnextoption(char **in)
{
char *p;
char *q;
p = *in;
q = p;
if (!p || !*p)
return NULL;
/* Find the next ',' not prefixed with a '\'. If the first character
* of p is ',', there can't be a previous escape character.
*/
for (;;) {
if (!(p = strchr(p, ',')))
break;
if (p == q)
break;
if (p[-1] != '\\')
break;
p += 1;
}
if (p) {
/* Another option follows this one .. set up to pick that out
* next time.
*/
*p = '\0';
*in = p + 1;
}
else {
/* This is the last one.
*/
*in = NULL;
}
if (strlen(q) > 0)
return q;
else
return NULL;
}
/* Get a suboption string, or NULL.
*/
char *
im_getsuboption(const char *buf)
{
char *p, *q, *r;
if (!(p = strchr(buf, ':')))
/* No suboption.
*/
return NULL;
/* Step over the ':'.
*/
p += 1;
/* Need to unescape any \, pairs. Shift stuff down one if we find one.
*/
for (q = p; *q; q++)
if (q[0] == '\\' && q[1] == ',')
for (r = q; *r; r++)
r[0] = r[1];
return p;
}
VipsImage *
im_open(const char *filename, const char *mode)
{
VipsImage *image;
vips_check_init();
/* We have to go via the old VipsFormat system so we can support the
* "filename:option" syntax.
*
* Use "rs" to turn on seq mode.
*/
if (strcmp(mode, "r") == 0 ||
strcmp(mode, "rd") == 0) {
if (!(image = vips__deprecated_open_read(filename, FALSE)))
return NULL;
}
else if (strcmp(mode, "rs") == 0) {
if (!(image = vips__deprecated_open_read(filename, TRUE)))
return NULL;
}
else if (strcmp(mode, "w") == 0) {
if (!(image = vips__deprecated_open_write(filename)))
return NULL;
}
else {
if (!(image = vips_image_new_mode(filename, mode)))
return NULL;
}
return image;
}
/* Just for compatibility. New code should use vips_object_local() directly.
*/
VipsImage *
im_open_local(VipsImage *parent,
const char *filename, const char *mode)
{
VipsImage *image;
if (!(image = im_open(filename, mode)))
return NULL;
vips_object_local(parent, image);
return image;
}
/* Just for compatibility. New code should use vips_object_local_array().
*/
int
im_open_local_array(VipsImage *parent,
VipsImage **images, int n,
const char *filename, const char *mode)
{
int i;
for (i = 0; i < n; i++)
if (!(images[i] = im_open_local(parent, filename, mode)))
return -1;
return 0;
}
typedef struct {
im_callback_fn fn;
void *a;
void *b;
} Callback;
static void
im_add_callback_cb(VipsImage *im, Callback *callback)
{
if (callback->fn(callback->a, callback->b))
vips_image_set_kill(im, TRUE);
}
int
im_add_callback(VipsImage *im,
const char *name, im_callback_fn fn, void *a, void *b)
{
Callback *callback;
callback = VIPS_NEW(VIPS_OBJECT(im), Callback);
callback->fn = fn;
callback->a = a;
callback->b = b;
g_signal_connect(im, name,
G_CALLBACK(im_add_callback_cb), callback);
return 0;
}
static void
im_add_callback_cb1(VipsImage *im, void *x, Callback *callback)
{
if (callback->fn(callback->a, callback->b))
vips_image_set_kill(im, TRUE);
}
int
im_add_callback1(VipsImage *im,
const char *name, im_callback_fn fn, void *a, void *b)
{
Callback *callback;
callback = VIPS_NEW(VIPS_OBJECT(im), Callback);
callback->fn = fn;
callback->a = a;
callback->b = b;
g_signal_connect(im, name,
G_CALLBACK(im_add_callback_cb1), callback);
return 0;
}
/* Make something local to an image descriptor ... pass in a constructor
* and a destructor, plus three args.
*/
void *
im_local(IMAGE *im,
im_construct_fn cons, im_callback_fn dest, void *a, void *b, void *c)
{
void *obj;
if (!im) {
im_error("im_local", "%s", _("NULL image descriptor"));
return NULL;
}
if (!(obj = cons(a, b, c)))
return NULL;
if (im_add_close_callback(im, (im_callback_fn) dest, obj, a)) {
dest(obj, a);
return NULL;
}
return obj;
}
/* Make an array of things local to a descriptor ... eg. make 6 local temp
* images.
*/
int
im_local_array(IMAGE *im, void **out, int n,
im_construct_fn cons, im_callback_fn dest, void *a, void *b, void *c)
{
int i;
for (i = 0; i < n; i++)
if (!(out[i] = im_local(im, cons, dest, a, b, c)))
return -1;
return 0;
}
int
im_close(VipsImage *im)
{
g_object_unref(im);
return 0;
}
/* edvips.c needs this
*/
VipsImage *
im_init(const char *filename)
{
VipsImage *image;
image = vips_image_new();
IM_SETSTR(image->filename, filename);
return image;
}
/* We can't do this with a rename macro since the C++ interface needs
* this entrypoint, see VImage.h.
*
* As a result our fancy ABI check will not work with the vips7 interface.
*/
int
im_init_world(const char *argv0)
{
return vips_init(argv0);
}
/* Prettyprint various header fields. Just for vips7 compat, use
* vips_enum_value() instead.
*/
const char *
im_Type2char(VipsInterpretation type)
{
return vips_enum_string(VIPS_TYPE_INTERPRETATION, type);
}
const char *
im_BandFmt2char(VipsBandFormat format)
{
return vips_enum_string(VIPS_TYPE_BAND_FORMAT, format);
}
const char *
im_Coding2char(VipsCoding coding)
{
return vips_enum_string(VIPS_TYPE_CODING, coding);
}
const char *
im_dtype2char(VipsImageType n)
{
return vips_enum_string(VIPS_TYPE_IMAGE_TYPE, n);
}
const char *
im_dhint2char(VipsDemandStyle style)
{
return vips_enum_string(VIPS_TYPE_DEMAND_STYLE, style);
}
/* Old names for enums, for compat.
*/
static const char *im_Type[] = {
"IM_TYPE_MULTIBAND", /* 0 */
"IM_TYPE_B_W", /* 1 */
"LUMINACE", /* 2 */
"XRAY", /* 3 */
"IR", /* 4 */
"YUV", /* 5 */
"RED_ONLY", /* 6 */
"GREEN_ONLY", /* 7 */
"BLUE_ONLY", /* 8 */
"POWER_SPECTRUM", /* 9 */
"IM_TYPE_HISTOGRAM", /* 10 */
"LUT", /* 11 */
"IM_TYPE_XYZ", /* 12 */
"IM_TYPE_LAB", /* 13 */
"CMC", /* 14 */
"IM_TYPE_CMYK", /* 15 */
"IM_TYPE_LABQ", /* 15 */
"IM_TYPE_RGB", /* 17 */
"IM_TYPE_UCS", /* 18 */
"IM_TYPE_LCH", /* 19 */
"IM_TYPE_LABS", /* 20 */
"<unknown>", /* 21 */
"IM_TYPE_sRGB", /* 22 */
"IM_TYPE_YXY", /* 23 */
"IM_TYPE_FOURIER", /* 24 */
"IM_TYPE_RGB16", /* 25 */
"IM_TYPE_GREY16", /* 26 */
NULL
};
static const char *im_BandFmt[] = {
"IM_BANDFMT_UCHAR",
"IM_BANDFMT_CHAR",
"IM_BANDFMT_USHORT",
"IM_BANDFMT_SHORT",
"IM_BANDFMT_UINT",
"IM_BANDFMT_INT",
"IM_BANDFMT_FLOAT",
"IM_BANDFMT_COMPLEX",
"IM_BANDFMT_DOUBLE",
"IM_BANDFMT_DPCOMPLEX",
NULL
};
static const char *im_Coding[] = {
"IM_CODING_NONE",
"COLQUANT8",
"IM_CODING_LABQ",
"IM_CODING_LABQ_COMPRESSED",
"RGB_COMPRESSED",
"LUM_COMPRESSED",
"IM_CODING_RAD",
NULL
};
static const char *im_dtype[] = {
"IM_NONE",
"IM_SETBUF",
"IM_SETBUF_FOREIGN",
"IM_OPENIN",
"IM_MMAPIN",
"IM_MMAPINRW",
"IM_OPENOUT",
"IM_PARTIAL",
NULL
};
static const char *im_dhint[] = {
"IM_SMALLTILE",
"IM_FATSTRIP",
"IM_THINSTRIP",
"IM_ANY",
NULL
};
/* enum string to int, try the GEnum first, then use a compat *char[] for old
* names.
*/
static int
lookup_enum(GType type, const char *names[], const char *name)
{
GEnumClass *class;
GEnumValue *value;
int i;
class = g_type_class_ref(type);
if ((value = g_enum_get_value_by_nick(class, name)))
return value->value;
if ((value = g_enum_get_value_by_name(class, name)))
return value->value;
for (i = 0; names[i]; i++)
if (g_ascii_strcasecmp(names[i], name) == 0)
return i;
return -1;
}
VipsInterpretation
im_char2Type(const char *str)
{
return lookup_enum(VIPS_TYPE_INTERPRETATION, im_Type, str);
}
VipsBandFormat
im_char2BandFmt(const char *str)
{
return lookup_enum(VIPS_TYPE_BAND_FORMAT, im_BandFmt, str);
}
VipsCoding
im_char2Coding(const char *str)
{
return lookup_enum(VIPS_TYPE_CODING, im_Coding, str);
}
VipsImageType
im_char2dtype(const char *str)
{
return lookup_enum(VIPS_TYPE_IMAGE_TYPE, im_dtype, str);
}
VipsDemandStyle
im_char2dhint(const char *str)
{
return lookup_enum(VIPS_TYPE_DEMAND_STYLE, im_dhint, str);
}
/* Totally useless now.
*/
const char *
im_Compression2char(int n)
{
return "NONE";
}
int
im_char2Compression(const char *str)
{
return -1;
}
/* Wrap one / many is being replaced by a class thing.
*/
typedef struct {
im_wrapmany_fn fn; /* Function we call */
void *a, *b; /* User values for function */
} Bundle;
/* Maximum number of input images -- why not?
*/
#define MAX_INPUT_IMAGES (64)
/* Convert a VipsRegion.
*/
static int
process_region(VipsRegion *out_region, void *seq, void *a, void *b)
{
VipsRegion **ir = (VipsRegion **) seq;
Bundle *bun = (Bundle *) b;
PEL *p[MAX_INPUT_IMAGES], *q;
int i, y;
/* Prepare all input regions and make buffer pointers.
*/
if (vips_reorder_prepare_many(out_region->im, ir, &out_region->valid))
return -1;
for (i = 0; ir[i]; i++)
p[i] = (PEL *) VIPS_REGION_ADDR(ir[i],
out_region->valid.left, out_region->valid.top);
p[i] = NULL;
q = (PEL *) VIPS_REGION_ADDR(out_region, out_region->valid.left, out_region->valid.top);
/* Convert linewise.
*/
for (y = 0; y < out_region->valid.height; y++) {
PEL *p1[MAX_INPUT_IMAGES];
/* Make a copy of p[] which the buffer function can mess up if
* it wants.
*/
for (i = 0; ir[i]; i++)
p1[i] = p[i];
/* Bizarre double-cast stops a bogus gcc 4.1 compiler warning.
*/
bun->fn((void **) ((void *) p1), q,
out_region->valid.width, bun->a, bun->b);
/* Move pointers on.
*/
for (i = 0; ir[i]; i++)
p[i] += VIPS_REGION_LSKIP(ir[i]);
q += VIPS_REGION_LSKIP(out_region);
}
return 0;
}
/* Make a copy of an array of input images.
*/
static IMAGE **
dupims(IMAGE *out, IMAGE **in)
{
IMAGE **new;
int i, n;
for (n = 0; in[n]; n++)
;
new = VIPS_ARRAY(VIPS_OBJECT(out), n + 1, IMAGE *);
for (i = 0; i < n; i++)
new[i] = in[i];
new[n] = NULL;
return new;
}
int
im_wrapmany(IMAGE **in, IMAGE *out, im_wrapmany_fn fn, void *a, void *b)
{
Bundle *bun;
int i, n;
/* Count input images.
*/
for (n = 0; in[n]; n++)
;
if (n >= MAX_INPUT_IMAGES - 1) {
vips_error("im_wrapmany", "%s", _("too many input images"));
return -1;
}
/* Save args.
*/
bun = VIPS_NEW(VIPS_OBJECT(out), Bundle);
if (!(in = dupims(out, in)))
return -1;
bun->fn = fn;
bun->a = a;
bun->b = b;
/* Check descriptors --- make sure that our caller has done this
* correctly.
*/
for (i = 0; i < n; i++) {
if (in[i]->Xsize != out->Xsize || in[i]->Ysize != out->Ysize) {
vips_error("im_wrapmany",
"%s", _("descriptors differ in size"));
return -1;
}
/* Check io style.
*/
if (vips_image_pio_input(in[i]))
return -1;
}
/* Don't call vips_image_pipeline_array(), we don't want to copy
* fields.
*/
vips__demand_hint_array(out, VIPS_DEMAND_STYLE_THINSTRIP, in);
if (vips__reorder_set_input(out, in))
return -1;
/* Generate!
*/
if (vips_image_generate(out,
vips_start_many, (VipsGenerateFn) process_region,
vips_stop_many, in, bun))
return -1;
return 0;
}
static void
wrapone_gen(void **ins, void *out, int width, Bundle *bun, void *dummy)
{
((im_wrapone_fn) (bun->fn))(ins[0], out, width, bun->a, bun->b);
}
int
im_wrapone(IMAGE *in, IMAGE *out, im_wrapone_fn fn, void *a, void *b)
{
Bundle *bun;
IMAGE *invec[2];
/* Heh, yuk. We cast back above.
*/
bun = VIPS_NEW(VIPS_OBJECT(out), Bundle);
bun->fn = (im_wrapmany_fn) fn;
bun->a = a;
bun->b = b;
invec[0] = in;
invec[1] = NULL;
return im_wrapmany(invec, out,
(im_wrapmany_fn) wrapone_gen, bun, NULL);
}
static void
wraptwo_gen(void **ins, void *out, int width, Bundle *bun, void *dummy)
{
((im_wraptwo_fn) (bun->fn))(ins[0], ins[1], out,
width, bun->a, bun->b);
}
int
im_wraptwo(IMAGE *in1, IMAGE *in2, IMAGE *out,
im_wraptwo_fn fn, void *a, void *b)
{
Bundle *bun;
IMAGE *invec[3];
bun = VIPS_NEW(VIPS_OBJECT(out), Bundle);
bun->fn = (im_wrapmany_fn) fn;
bun->a = a;
bun->b = b;
invec[0] = in1;
invec[1] = in2;
invec[2] = NULL;
return im_wrapmany(invec, out,
(im_wrapmany_fn) wraptwo_gen, bun, NULL);
}
/* Save a bit of typing.
*/
#define UC IM_BANDFMT_UCHAR
#define C IM_BANDFMT_CHAR
#define US IM_BANDFMT_USHORT
#define S IM_BANDFMT_SHORT
#define UI IM_BANDFMT_UINT
#define I IM_BANDFMT_INT
#define F IM_BANDFMT_FLOAT
#define X IM_BANDFMT_COMPLEX
#define D IM_BANDFMT_DOUBLE
#define DX IM_BANDFMT_DPCOMPLEX
/* For two integer types, the "largest", ie. one which can represent the
* full range of both.
*/
static int bandfmt_largest[6][6] = {
/* UC C US S UI I */
/* UC */ { UC, S, US, S, UI, I },
/* C */ { S, C, I, S, I, I },
/* US */ { US, I, US, I, UI, I },
/* S */ { S, S, I, S, I, I },
/* UI */ { UI, I, UI, I, UI, I },
/* I */ { I, I, I, I, I, I }
};
/* For two formats, find one which can represent the full range of both.
*/
static VipsBandFmt
im__format_common(VipsBandFmt in1, VipsBandFmt in2)
{
if (vips_band_format_iscomplex(in1) ||
vips_band_format_iscomplex(in2)) {
/* What kind of complex?
*/
if (in1 == IM_BANDFMT_DPCOMPLEX || in2 == IM_BANDFMT_DPCOMPLEX)
/* Output will be DPCOMPLEX.
*/
return IM_BANDFMT_DPCOMPLEX;
else
return IM_BANDFMT_COMPLEX;
}
else if (vips_bandfmt_isfloat(in1) ||
vips_bandfmt_isfloat(in2)) {
/* What kind of float?
*/
if (in1 == IM_BANDFMT_DOUBLE || in2 == IM_BANDFMT_DOUBLE)
return IM_BANDFMT_DOUBLE;
else
return IM_BANDFMT_FLOAT;
}
else
/* Must be int+int -> int.
*/
return bandfmt_largest[in1][in2];
}
int
im__formatalike_vec(IMAGE **in, IMAGE **out, int n)
{
int i;
VipsBandFmt fmt;
g_assert(n >= 1);
fmt = in[0]->BandFmt;
for (i = 1; i < n; i++)
fmt = im__format_common(fmt, in[i]->BandFmt);
for (i = 0; i < n; i++)
if (im_clip2fmt(in[i], out[i], fmt))
return -1;
return 0;
}
int
im__formatalike(IMAGE *in1, IMAGE *in2, IMAGE *out1, IMAGE *out2)
{
IMAGE *in[2];
IMAGE *out[2];
in[0] = in1;
in[1] = in2;
out[0] = out1;
out[1] = out2;
return im__formatalike_vec(in, out, 2);
}
/* Make an n-band image. Input 1 or n bands.
*/
int
im__bandup(const char *domain, IMAGE *in, IMAGE *out, int n)
{
IMAGE *bands[256];
int i;
if (in->Bands == n)
return vips_image_write(in, out);
if (in->Bands != 1) {
im_error(domain, _("not one band or %d bands"), n);
return -1;
}
if (n > 256 || n < 1) {
im_error(domain, "%s", _("bad bands"));
return -1;
}
for (i = 0; i < n; i++)
bands[i] = in;
return im_gbandjoin(bands, out, n);
}
int
im__bandalike_vec(const char *domain, IMAGE **in, IMAGE **out, int n)
{
int i;
int max_bands;
g_assert(n >= 1);
max_bands = in[0]->Bands;
for (i = 1; i < n; i++)
max_bands = IM_MAX(max_bands, in[i]->Bands);
for (i = 0; i < n; i++)
if (im__bandup(domain, in[i], out[i], max_bands))
return -1;
return 0;
}
int
im__bandalike(const char *domain,
IMAGE *in1, IMAGE *in2, IMAGE *out1, IMAGE *out2)
{
IMAGE *in[2];
IMAGE *out[2];
in[0] = in1;
in[1] = in2;
out[0] = out1;
out[1] = out2;
if (im__bandalike_vec(domain, in, out, 2))
return -1;
return 0;