-
Notifications
You must be signed in to change notification settings - Fork 9
/
iio.c
5712 lines (5109 loc) · 163 KB
/
iio.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
// IIO: a library for reading small images {{{1
//
// Goal: load an image (of unknown format) from a given file
//
// Technique: read the first few bytes of the file to identify the format, and
// call the appropriate image library (lipng, lipjpeg, libtiff, etc). For
// simple, uncompressed formats, write the loading code by hand. For formats
// having a library with a nice API, call the library functions. For other or
// unrecognized formats use an external program (convert, anytopnm,
// gm convert...) to convert them into a readable format. If anything else
// fails, assume that the image is in headings+raw format, and try to extract
// its dimensions and headings using some heuristics (file name containing
// "%dx%d", headings containing ascii numbers, etc.)
//
// Difficulties: most image libraries expect to be fed a whole file, not a
// beheaded file. Thus, some hackery is necessary.
//
// See file "iio.txt" for slightly more detailed documentation, and "iio.h" for
// the API
//
// #defines {{{1
//
// editable configuration
//
#define IIO_SHOW_DEBUG_MESSAGES
//#define IIO_ABORT_ON_ERROR
#define I_CAN_HAS_LIBPNG
#define I_CAN_HAS_LIBJPEG
#define I_CAN_HAS_LIBTIFF
//#define I_CAN_HAS_LIBHDF5
//#define I_CAN_HAS_LIBEXR
#define I_CAN_HAS_WGET
#define I_CAN_HAS_WHATEVER
//#define I_CAN_KEEP_TMP_FILES
#define IIO_USE_INCONSISTENT_NAMES
//
// portability macros to choose OS features
//
//
#define I_CAN_POSIX
#define I_CAN_LINUX
#define I_CAN_GETENV
//
// no need to edit below here
//
#ifdef IIO_SHOW_DEBUG_MESSAGES
# define IIO_DEBUG(...) do {\
if (xgetenv("IIO_DEBUG")){\
fprintf(stderr,"DEBUG(%s:%d:%s): ",__FILE__,__LINE__,__PRETTY_FUNCTION__);\
fprintf(stderr,__VA_ARGS__);}} while(0)
#else//IIO_SHOW_DEBUG_MESSAGES
# define IIO_DEBUG(...) do { do_nop(__VA_ARGS__); } while(0) /* nothing */
#endif//IIO_SHOW_DEBUG_MESSAGES
#ifdef IIO_DISABLE_LIBEXR
#undef I_CAN_HAS_LIBEXR
#endif
#ifdef IIO_DISABLE_LIBPNG
#undef I_CAN_HAS_LIBPNG
#endif
#ifdef IIO_DISABLE_LIBJPEG
#undef I_CAN_HAS_LIBJPEG
#endif
#ifdef IIO_DISABLE_LIBTIFF
#undef I_CAN_HAS_LIBTIFF
#endif
#ifdef IIO_DISABLE_LIBHDF5
#undef I_CAN_HAS_LIBHDF5
#endif
#ifdef IIO_DISABLE_IMGLIBS
#undef I_CAN_HAS_LIBPNG
#undef I_CAN_HAS_LIBJPEG
#undef I_CAN_HAS_LIBTIFF
#undef I_CAN_HAS_LIBEXR
#undef I_CAN_HAS_LIBHDF5
#endif
// internal shit
#define IIO_MAX_DIMENSION 20
//
// enum-like, only used internally
//
#define IIO_TYPE_INT8 1
#define IIO_TYPE_UINT8 2
#define IIO_TYPE_INT16 3
#define IIO_TYPE_UINT16 4
#define IIO_TYPE_INT32 5
#define IIO_TYPE_UINT32 6
#define IIO_TYPE_FLOAT 7
#define IIO_TYPE_DOUBLE 8
#define IIO_TYPE_LONGDOUBLE 9
#define IIO_TYPE_INT64 10
#define IIO_TYPE_UINT64 11
#define IIO_TYPE_HALF 12
#define IIO_TYPE_UINT1 13
#define IIO_TYPE_UINT2 14
#define IIO_TYPE_UINT4 15
#define IIO_TYPE_CHAR 16
#define IIO_TYPE_SHORT 17
#define IIO_TYPE_INT 18
#define IIO_TYPE_LONG 19
#define IIO_TYPE_LONGLONG 20
#define IIO_FORMAT_WHATEVER 0
#define IIO_FORMAT_QNM 1
#define IIO_FORMAT_PNG 2
#define IIO_FORMAT_JPEG 3
#define IIO_FORMAT_TIFF 4
#define IIO_FORMAT_RIM 5
#define IIO_FORMAT_BMP 6
#define IIO_FORMAT_EXR 7
#define IIO_FORMAT_JP2 8
#define IIO_FORMAT_VTK 9
#define IIO_FORMAT_CIMG 10
#define IIO_FORMAT_PAU 11
#define IIO_FORMAT_DICOM 12
#define IIO_FORMAT_PFM 13
#define IIO_FORMAT_NIFTI 14
#define IIO_FORMAT_PCX 15
#define IIO_FORMAT_GIF 16
#define IIO_FORMAT_XPM 17
#define IIO_FORMAT_RAFA 18
#define IIO_FORMAT_FLO 19
#define IIO_FORMAT_JUV 20
#define IIO_FORMAT_LUM 21
#define IIO_FORMAT_PCM 22
#define IIO_FORMAT_ASC 23
#define IIO_FORMAT_PDS 24
#define IIO_FORMAT_RAW 25
#define IIO_FORMAT_RWA 26
#define IIO_FORMAT_CSV 27
#define IIO_FORMAT_VRT 28
#define IIO_FORMAT_FFD 29
#define IIO_FORMAT_DLM 30
#define IIO_FORMAT_NPY 31
#define IIO_FORMAT_VIC 32
#define IIO_FORMAT_CCS 33
#define IIO_FORMAT_FIT 34
#define IIO_FORMAT_HDF5 35
#define IIO_FORMAT_UNRECOGNIZED (-1)
//
// sugar
//
#define FORI(n) for(int i=0;i<(int)(n);i++)
#define FORJ(n) for(int j=0;j<(int)(n);j++)
#define FORK(n) for(int k=0;k<(int)(n);k++)
#define FORL(n) for(int l=0;l<(int)(n);l++)
//
// hacks
//
#ifndef __attribute__
# ifndef __GNUC__
# define __attribute__(x) /*NOTHING*/
# endif
#endif
// #includes {{{1
#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <libgen.h> // needed for dirname() multi-platform
#ifdef __MINGW32__ // needed for tmpfile(), this flag is also set by MINGW64
#include <windows.h>
#endif
#ifdef I_CAN_HAS_LIBPNG
// ugly "feature" in png.h forces this header to be included here
# include <png.h>
#endif
// portabil
#if _POSIX_C_SOURCE >= 200809L
# define I_CAN_HAS_FMEMOPEN 1
#endif
#if _POSIX_C_SOURCE >= 200112L || __OpenBSD__ || __DragonFly__ || __FreeBSD__ || __APPLE__
# define I_CAN_HAS_MKSTEMP 1
#endif
// typedefs {{{1
typedef long long longlong;
typedef long double longdouble;
// utility functions {{{1
#ifndef IIO_ABORT_ON_ERROR
// NOTE: libpng has a nasty "feature" whereby you have to include libpng.h
// before setjmp.h if you want to use both. This induces the following
// hackery:
# ifndef I_CAN_HAS_LIBPNG
# include <setjmp.h>
# endif//I_CAN_HAS_LIBPNG
# if __STDC_VERSION__ >= 201112L
_Thread_local
# endif
jmp_buf global_jump_buffer;
#endif//IIO_ABORT_ON_ERROR
//#include <errno.h> // only for errno
#include <ctype.h> // for isspace
#include <math.h> // for floorf
#include <stdlib.h>
#ifdef I_CAN_LINUX
# include <unistd.h>
static const char *emptystring = "";
static const char *myname(void)
{
# define n 0x29a
static char buf[n];
long p = getpid();
snprintf(buf, n, "/proc/%ld/cmdline", p);
FILE *f = fopen(buf, "r");
if (!f) return emptystring;
int c, i = 0;
while ((c = fgetc(f)) != EOF && i < n) {
# undef n
buf[i] = c ? c : ' ';
i += 1;
}
if (i) buf[i-1] = '\0';
fclose(f);
return buf;
}
#else
static const char *myname(void) { return ""; }
#endif//I_CAN_LINUX
static char *xgetenv(const char *s)
{
#ifdef I_CAN_GETENV
return getenv(s);
#else//I_CAN_GETENV
return 0;
#endif// I_CAN_GETENV
}
static void fail(const char *fmt, ...) __attribute__((noreturn,format(printf,1,2)));
static void fail(const char *fmt, ...)
{
va_list argp;
fprintf(stderr, "\nIIO_ERROR(\"%s\"): ", myname());
va_start(argp, fmt);
vfprintf(stderr, fmt, argp);
va_end(argp);
fprintf(stderr, "\n\n");
fflush(NULL);
// if (global_hack_to_never_fail)
// {
// IIO_DEBUG("now wave a dead chicken and press enter\n");
// getchar();
// return;
// }
// exit(43);
#ifndef IIO_ABORT_ON_ERROR
longjmp(global_jump_buffer, 1);
//iio_single_jmpstuff(true, false);
#else//IIO_ABORT_ON_ERROR
exit(xgetenv("IIO_SEGFAULT_ON_ERROR")?*(int*)0:-1);
//# ifdef NDEBUG
// exit(-1);
//# else//NDEBUG
//print_trace(stderr);
// exit(*(int *)0x43);
//# endif//NDEBUG
#endif//IIO_ABORT_ON_ERROR
}
static void do_nop(void *p, ...)
{
va_list argp;
va_start(argp, p);
va_end(argp);
}
static void *xmalloc(size_t size)
{
if (size == 0)
fail("xmalloc: zero size");
void *p = malloc(size);
if (!p)
{
double sm = size / (0x100000 * 1.0);
fail("xmalloc: out of memory when requesting "
"%zu bytes (%gMB)",//:\"%s\"",
size, sm);//, strerror(errno));
}
return p;
}
static void *xrealloc(void *p, size_t s)
{
void *r = realloc(p, s);
if (!r) fail("realloc failed");
return r;
}
static void xfree(void *p)
{
if (!p)
fail("thou shalt not free a null pointer!");
free(p);
}
# if __STDC_VERSION__ >= 201112L
_Thread_local
# endif
static const
char *global_variable_containing_the_name_of_the_last_opened_file = NULL;
static FILE *xfopen(const char *s, const char *p)
{
global_variable_containing_the_name_of_the_last_opened_file = NULL;
if (!s) fail("trying to open a file with NULL name");
if (0 == strcmp("-", s))
{
if (*p == 'w')
return stdout;
else if (*p == 'r')
return stdin;
else
fail("unknown fopen mode \"%s\"", p);
}
if (0 == strcmp("--", s) && *p == 'w') return stderr;
// NOTE: the 'b' flag is required for I/O on Windows systems
// on unix, it is ignored
char pp[3] = { p[0], 'b', '\0' };
FILE *f = fopen(s, pp);
if (f == NULL)
fail("can not open file \"%s\" in mode \"%s\"",// (%s)",
s, pp);//, strerror(errno));
global_variable_containing_the_name_of_the_last_opened_file = s;
IIO_DEBUG("fopen (%s) = %p\n", s, (void*)f);
return f;
}
static void xfclose(FILE *f)
{
global_variable_containing_the_name_of_the_last_opened_file = NULL;
if (f != stdout && f != stdin && f != stderr) {
int r = fclose(f);
IIO_DEBUG("fclose (%p) = %d\n", (void*)f, r);
if (r) fail("fclose error");// \"%s\"", strerror(errno));
}
}
static int pick_char_for_sure(FILE *f)
{
int c = getc(f);
if (EOF == c) {
xfclose(f);
fail("input file ended before expected");
}
//IIO_DEBUG("pcs = '%c'\n", c);
return c;
}
static void eat_spaces(FILE *f)
{
//IIO_DEBUG("inside eat spaces\n");
int c;
do
c = pick_char_for_sure(f);
while (isspace(c));
ungetc(c, f);
}
static void eat_line(FILE *f)
{
//IIO_DEBUG("inside eat line\n");
while (pick_char_for_sure(f) != '\n')
;
}
static void eat_spaces_and_comments(FILE *f)
{
//IIO_DEBUG("inside eat spaces and comments\n");
int c, comment_char = '#';
eat_spaces(f);
uppsala:
c = pick_char_for_sure(f);
if (c == comment_char)
{
eat_line(f);
eat_spaces(f);
} else
ungetc(c, f);
if (c == comment_char) goto uppsala;
}
static void fill_temporary_filename(char *out)
{
#ifdef I_CAN_HAS_MKSTEMP
char tfn[] = "/tmp/iio_tmp_file_XXXXXX\0";
int r = mkstemp(tfn);
if (r == -1) {
perror("hola");
fail("could not create tmp filename");
}
#else
static char buf[L_tmpnam+1];
char *tfn = tmpnam(buf);
#endif//I_CAN_HAS_MKSTEMP
snprintf(out, FILENAME_MAX, "%s", tfn);
}
// struct iio_image { ... }; {{{1
// This struct is used for exchanging image information between internal
// functions. It could be safely eliminated, and this information be passed as
// five or six variables.
struct iio_image {
int dimension; // 1, 2, 3 or 4, typically
int sizes[IIO_MAX_DIMENSION];
int pixel_dimension;
int type; // IIO_TYPE_*
int meta; // IIO_META_*
int format; // IIO_FORMAT_*
bool contiguous_data;
bool caca[3];
void *data;
};
// struct iio_image management {{{1
static void iio_image_assert_struct_consistency(struct iio_image *x)
{
assert(x->dimension > 0);
assert(x->dimension <= IIO_MAX_DIMENSION);
FORI(x->dimension) assert(x->sizes[i] > 0);
assert(x->pixel_dimension > 0);
switch(x->type) {
case IIO_TYPE_INT8: case IIO_TYPE_UINT8: case IIO_TYPE_INT16:
case IIO_TYPE_UINT16: case IIO_TYPE_INT32: case IIO_TYPE_UINT32:
case IIO_TYPE_FLOAT: case IIO_TYPE_DOUBLE: case IIO_TYPE_LONGDOUBLE:
case IIO_TYPE_INT64: case IIO_TYPE_UINT64: case IIO_TYPE_HALF:
case IIO_TYPE_CHAR: case IIO_TYPE_SHORT: case IIO_TYPE_INT:
case IIO_TYPE_LONG: case IIO_TYPE_LONGLONG: break;
default: assert(false);
}
//if (x->contiguous_data)
// assert(x->data == (void*)(x+1));
}
// API
static size_t iio_type_size(int type)
{
switch(type) {
case IIO_TYPE_INT8: return 1;
case IIO_TYPE_UINT8: return 1;
case IIO_TYPE_INT16: return 2;
case IIO_TYPE_UINT16: return 2;
case IIO_TYPE_INT32: return 4;
case IIO_TYPE_UINT32: return 4;
case IIO_TYPE_INT64: return 8;
case IIO_TYPE_UINT64: return 8;
case IIO_TYPE_CHAR: return sizeof(char); // 1
case IIO_TYPE_SHORT: return sizeof(short);
case IIO_TYPE_INT: return sizeof(int);
case IIO_TYPE_LONG: return sizeof(long);
case IIO_TYPE_LONGLONG: return sizeof(long long);
case IIO_TYPE_FLOAT: return sizeof(float);
case IIO_TYPE_DOUBLE: return sizeof(double);
case IIO_TYPE_LONGDOUBLE: return sizeof(long double);
case IIO_TYPE_HALF: return sizeof(float)/2;
default: fail("unrecognized type %d", type);
}
}
// XXX TODO FIXME: this is architecture dependent!
// this function actually requires A LOT of magic to be portable
// the present solution works well for the typical 32 and 64 bit platforms
static int normalize_type(int type_in)
{
int type_out;
switch(type_in) {
case IIO_TYPE_CHAR: type_out = IIO_TYPE_UINT8; break;
case IIO_TYPE_SHORT: type_out = IIO_TYPE_INT16; break;
case IIO_TYPE_INT: type_out = IIO_TYPE_UINT32; break;
default: type_out = type_in; break;
}
if (type_out != type_in) {
// the following assertion fails on many architectures
assert(iio_type_size(type_in) == iio_type_size(type_out));
}
return type_out;
}
// internal API
static
int iio_type_id(size_t sample_size, bool ieeefp_sample, bool signed_sample)
{
if (ieeefp_sample) {
if (signed_sample) fail("signed floats are a no-no!");
switch(sample_size) {
case sizeof(float): return IIO_TYPE_FLOAT;
case sizeof(double): return IIO_TYPE_DOUBLE;
case sizeof(long double): return IIO_TYPE_LONGDOUBLE;
case sizeof(float)/2: return IIO_TYPE_HALF;
default: fail("bad float size %zu", sample_size);
}
} else {
switch(sample_size) {
case 1: return signed_sample ? IIO_TYPE_INT8 : IIO_TYPE_UINT8;
case 2: return signed_sample ? IIO_TYPE_INT16 : IIO_TYPE_UINT16;
case 4: return signed_sample ? IIO_TYPE_INT32 : IIO_TYPE_UINT32;
case 8: return signed_sample ? IIO_TYPE_INT64 : IIO_TYPE_UINT64;
default: fail("bad integral size %zu", sample_size);
}
}
}
// internal API
static void iio_type_unid(int *size, bool *ieefp, bool *signedness, int type)
{
int t = normalize_type(type);
*size = iio_type_size(t);
*ieefp = (t==IIO_TYPE_HALF || t==IIO_TYPE_FLOAT
|| t==IIO_TYPE_DOUBLE || t==IIO_TYPE_LONGDOUBLE);
*signedness = (t==IIO_TYPE_INT8 || t==IIO_TYPE_INT16
|| t==IIO_TYPE_INT32 || t==IIO_TYPE_INT64);
}
// internal API
static int iio_image_number_of_elements(struct iio_image *x)
{
iio_image_assert_struct_consistency(x);
int r = 1;
FORI(x->dimension) r *= x->sizes[i];
return r;
}
// internal API
static int iio_image_number_of_samples(struct iio_image *x)
{
return iio_image_number_of_elements(x) * x->pixel_dimension;
}
// internal API
static size_t iio_image_sample_size(struct iio_image *x)
{
return iio_type_size(x->type);
}
static const char *iio_strtyp(int type)
{
#define M(t) case IIO_TYPE_ ## t: return #t
switch(type) {
M(INT8); M(UINT8); M(INT16); M(UINT16); M(INT32); M(UINT32); M(INT64);
M(UINT64); M(FLOAT); M(DOUBLE); M(LONGDOUBLE); M(HALF); M(UINT1);
M(UINT2); M(UINT4); M(CHAR); M(SHORT); M(INT); M(LONG); M(LONGLONG);
default: return "unrecognized";
}
#undef M
}
static int iio_inttyp(const char *type_name)
{
int n = strlen(type_name);
char utyp[n+1]; utyp[n] = '\0';
for (int i = 0; i < n; i++)
utyp[i] = toupper(type_name[i]);
#define M(t) if(!strcmp(utyp,#t))return IIO_TYPE_ ## t
M(INT8); M(UINT8); M(INT16); M(UINT16); M(INT32); M(UINT32); M(INT64);
M(UINT64); M(FLOAT); M(DOUBLE); M(LONGDOUBLE); M(HALF); M(UINT1);
M(UINT2); M(UINT4); M(CHAR); M(SHORT); M(INT); M(LONG); M(LONGLONG);
#undef M
fail("unrecognized typename \"%s\"", type_name);
}
static const char *iio_strfmt(int format)
{
#define M(f) case IIO_FORMAT_ ## f: return #f
switch(format) {
M(WHATEVER); M(QNM); M(PNG); M(JPEG);
M(TIFF); M(RIM); M(BMP); M(EXR); M(JP2);
M(VTK); M(CIMG); M(PAU); M(DICOM); M(PFM); M(NIFTI);
M(PCX); M(GIF); M(XPM); M(RAFA); M(FLO); M(LUM); M(JUV);
M(PCM); M(ASC); M(RAW); M(RWA); M(PDS); M(CSV); M(VRT);
M(FFD); M(DLM); M(NPY); M(VIC); M(CCS); M(FIT); M(HDF5);
M(UNRECOGNIZED);
default: fail("caca de la grossa (%d)", format);
}
#undef M
}
//#ifdef IIO_SHOW_DEBUG_MESSAGES
//static void iio_print_image_info(FILE *f, struct iio_image *x)
//{
// fprintf(f, "iio_print_image_info %p\n", (void *)x);
// fprintf(f, "dimension = %d\n", x->dimension);
// int *s = x->sizes;
// switch(x->dimension) {
// case 1: fprintf(f, "size = %d\n", s[0]); break;
// case 2: fprintf(f, "sizes = %dx%d\n", s[0],s[1]); break;
// case 3: fprintf(f, "sizes = %dx%dx%d\n", s[0],s[1],s[2]); break;
// case 4: fprintf(f, "sizes = %dx%dx%dx%d\n", s[0],s[1],s[2],s[3]); break;
// default: fail("unsupported dimension %d", x->dimension);
// }
// fprintf(f, "pixel_dimension = %d\n", x->pixel_dimension);
// fprintf(f, "type = %s\n", iio_strtyp(x->type));
// fprintf(f, "data = %p\n", (void *)x->data);
//}
//#endif//IIO_SHOW_DEBUG_MESSAGES
static void iio_image_fill(struct iio_image *x,
int dimension, int *sizes,
int type, int pixel_dimension)
{
x->dimension = dimension;
FORI(dimension) x->sizes[i] = sizes[i];
x->type = type;
x->pixel_dimension = pixel_dimension;
x->data = NULL;
x->format = -1;
x->meta = -1;
x->contiguous_data = false;
}
static void iio_wrap_image_struct_around_data(struct iio_image *x,
int dimension, int *sizes, int pixel_dimension,
int type, void *data)
{
assert(dimension < IIO_MAX_DIMENSION);
x->dimension = dimension;
FORI(x->dimension) x->sizes[i] = sizes[i];
x->pixel_dimension = pixel_dimension;
x->type = type;
x->data = data;
x->contiguous_data = false;
x->meta = -42;
x->format = -42;
}
static void iio_image_build_independent(struct iio_image *x,
int dimension, int *sizes, int type, int pixel_dimension)
{
assert(dimension > 0);
assert(dimension <= 4);
assert(pixel_dimension > 0);
iio_image_fill(x, dimension, sizes, type, pixel_dimension);
size_t datalength = 1; FORI(dimension) datalength *= sizes[i];
size_t datasize = datalength * iio_type_size(type) * pixel_dimension;
x->data = xmalloc(datasize);
}
static void inplace_swap_pixels(struct iio_image *x, int i, int j, int a, int b)
{
if (x->dimension != 2)
fail("can only flip 2-dimensional images");
int w = x->sizes[0];
//int h = x->sizes[1]; // unused
int pixsize = x->pixel_dimension * iio_image_sample_size(x);
uint8_t *p = (i + j * w) * pixsize + (uint8_t*)x->data;
uint8_t *q = (a + b * w) * pixsize + (uint8_t*)x->data;
for (int k = 0; k < pixsize; k++)
{
uint8_t tmp = p[k];
p[k] = q[k];
q[k] = tmp;
}
}
static void inplace_flip_horizontal(struct iio_image *x)
{
int w = x->sizes[0];
int h = x->sizes[1];
for (int j = 0; j < h; j++)
for (int i = 0; i < w/2; i++)
inplace_swap_pixels(x, i, j, w - i - 1, j);
}
static void inplace_flip_vertical(struct iio_image *x)
{
int w = x->sizes[0];
int h = x->sizes[1];
for (int j = 0; j < h/2; j++)
for (int i = 0; i < w; i++)
inplace_swap_pixels(x, i, j, i, h - j - 1);
}
static void inplace_transpose(struct iio_image *x)
{
int w = x->sizes[0];
int h = x->sizes[1];
if (w != h)
{
void rectangular_not_inplace_transpose(struct iio_image*);
rectangular_not_inplace_transpose(x);
} else {
for (int i = 0; i < w; i++)
for (int j = 0; j < i; j++)
inplace_swap_pixels(x, i, j, j, i);
}
}
static void inplace_reorient(struct iio_image *x, int orientation)
{
int orx = orientation & 0xff;
int ory = (orientation & 0xff00)/0x100;
if (isupper(orx))
inplace_flip_horizontal(x);
if (isupper(ory))
inplace_flip_vertical(x);
if (toupper(orx) > toupper(ory))
inplace_transpose(x);
}
static int insideP(int w, int h, int i, int j)
{
return i>=0 && j>=0 && i<w && j<h;
}
static void inplace_trim(struct iio_image *x,
int trim_left, int trim_bottom, int trim_right, int trim_top)
{
IIO_DEBUG("inplace trim (%dx%d) le=%d bo=%d ri=%d to=%d\n",
x->sizes[0], x->sizes[1],
trim_left, trim_bottom, trim_right, trim_top);
assert(2 == x->dimension);
int pd = x->pixel_dimension;
int ss = iio_image_sample_size(x);
int ps = pd * ss;
int w = x->sizes[0];
int h = x->sizes[1];
int nw = w - trim_left - trim_right; // new width
int nh = h - trim_bottom - trim_top; // new height
assert(nw > 0 && nh > 0);
char *old_data = x->data;
char *new_data = xmalloc(nw * nh * ps);
if (ss == sizeof(float))
for (int i = 0; i < nw*nh*pd; i++)
((float*)new_data)[i] = NAN;
else if (ss == sizeof(double))
for (int i = 0; i < nw*nh*pd; i++)
((double*)new_data)[i] = NAN;
else
for (int i = 0; i < nw*nh*ps; i++)
((char*)new_data)[i] = 0;
for (int j = 0; j < nh; j++)
for (int i = 0; i < nw; i++)
if (insideP(w, h, i+trim_left, j+trim_top))
memcpy(
new_data + ps * (j*nw+i),
old_data + ps * ((j+trim_top)*w+(i+trim_left)),
ps
);
x->sizes[0] = nw;
x->sizes[1] = nh;
x->data = new_data;
xfree(old_data);
}
void rectangular_not_inplace_transpose(struct iio_image *x)
{
assert(2 == x->dimension);
int ps = x->pixel_dimension * iio_image_sample_size(x); // pixel_size
int w = x->sizes[0];
int h = x->sizes[1];
int nw = h;
int nh = w;
char *old_data = x->data;
char *new_data = xmalloc(nw * nh * ps);
for (int i = 0; i < nw*nh*ps; i++)
((char*)new_data)[i] = 1;
for (int j = 0; j < nh; j++)
for (int i = 0; i < nw; i++)
memcpy(
new_data + ps * (j*nw + i),
old_data + ps * (i*w + j),
ps
);
x->sizes[0] = nw;
x->sizes[1] = nh;
x->data = new_data;
xfree(old_data);
}
// data conversion {{{1
// macros to crop a numeric value
#define T0(x) ((x)>0?(x):0)
#define T8(x) ((x)>0?((x)<0xff?(x):0xff):0)
#define T6(x) ((x)>0?((x)<0xffff?(x):0xffff):0)
#define CC(a,b) (77*(a)+(b)) // hash number of a conversion pair
#define I8 IIO_TYPE_INT8
#define U8 IIO_TYPE_UINT8
#define I6 IIO_TYPE_INT16
#define U6 IIO_TYPE_UINT16
#define I2 IIO_TYPE_INT32
#define U2 IIO_TYPE_UINT32
#define I4 IIO_TYPE_INT64
#define U4 IIO_TYPE_UINT64
#define F4 IIO_TYPE_FLOAT
#define F8 IIO_TYPE_DOUBLE
#define F6 IIO_TYPE_LONGDOUBLE
static void convert_datum(void *dest, void *src, int dest_fmt, int src_fmt)
{
// NOTE: verify that this switch is optimized outside of the loop in
// which it is contained. Otherwise, produce some self-modifying code
// here.
switch(CC(dest_fmt,src_fmt)) {
// change of sign (lossless, but changes interpretation)
case CC(I8,U8): *( int8_t*)dest = *( uint8_t*)src; break;
case CC(I6,U6): *( int16_t*)dest = *(uint16_t*)src; break;
case CC(I2,U2): *( int32_t*)dest = *(uint32_t*)src; break;
case CC(U8,I8): *( uint8_t*)dest = *( int8_t*)src; break;
case CC(U6,I6): *(uint16_t*)dest = *( int16_t*)src; break;
case CC(U2,I2): *(uint32_t*)dest = *( int32_t*)src; break;
// different size signed integers (3 lossy, 3 lossless)
case CC(I8,I6): *( int8_t*)dest = *( int16_t*)src; break;//iw810
case CC(I8,I2): *( int8_t*)dest = *( int32_t*)src; break;//iw810
case CC(I6,I2): *( int16_t*)dest = *( int32_t*)src; break;//iw810
case CC(I6,I8): *( int16_t*)dest = *( int8_t*)src; break;
case CC(I2,I8): *( int32_t*)dest = *( int8_t*)src; break;
case CC(I2,I6): *( int32_t*)dest = *( int16_t*)src; break;
// different size unsigned integers (3 lossy, 3 lossless)
case CC(U8,U6): *( uint8_t*)dest = T8(*(uint16_t*)src); break;//iw810
case CC(U8,U2): *( uint8_t*)dest = *(uint32_t*)src; break;//iw810
case CC(U6,U2): *(uint16_t*)dest = *(uint32_t*)src; break;//iw810
case CC(U6,U8): *(uint16_t*)dest = *( uint8_t*)src; break;
case CC(U2,U8): *(uint32_t*)dest = *( uint8_t*)src; break;
case CC(U2,U6): *(uint32_t*)dest = *(uint16_t*)src; break;
// diferent size mixed integers, make signed (3 lossy, 3 lossless)
case CC(I8,U6): *( int8_t*)dest = *(uint16_t*)src; break;//iw810
case CC(I8,U2): *( int8_t*)dest = *(uint32_t*)src; break;//iw810
case CC(I6,U2): *( int16_t*)dest = *(uint32_t*)src; break;//iw810
case CC(I6,U8): *( int16_t*)dest = *( uint8_t*)src; break;
case CC(I2,U8): *( int32_t*)dest = *( uint8_t*)src; break;
case CC(I2,U6): *( int32_t*)dest = *(uint16_t*)src; break;
// diferent size mixed integers, make unsigned (3 lossy, 3 lossless)
case CC(U8,I6): *( uint8_t*)dest = *( int16_t*)src; break;//iw810
case CC(U8,I2): *( uint8_t*)dest = *( int32_t*)src; break;//iw810
case CC(U6,I2): *(uint16_t*)dest = *( int32_t*)src; break;//iw810
case CC(U6,I8): *(uint16_t*)dest = *( int8_t*)src; break;
case CC(U2,I8): *(uint32_t*)dest = *( int8_t*)src; break;
case CC(U2,I6): *(uint32_t*)dest = *( int16_t*)src; break;
// from float (typically lossy, except for small integers)
case CC(I8,F4): *( int8_t*)dest = *( float*)src; break;//iw810
case CC(I6,F4): *( int16_t*)dest = *( float*)src; break;//iw810
case CC(I2,F4): *( int32_t*)dest = *( float*)src; break;
case CC(I8,F8): *( int8_t*)dest = *(double*)src; break;//iw810
case CC(I6,F8): *( int16_t*)dest = *(double*)src; break;//iw810
case CC(I2,F8): *( int32_t*)dest = *(double*)src; break;//iw810
case CC(U8,F4): *( uint8_t*)dest = T8(0.5+*( float*)src); break;//iw810
case CC(U6,F4): *(uint16_t*)dest = T6(0.5+*( float*)src); break;//iw810
case CC(U2,F4): *(uint32_t*)dest = *( float*)src; break;
case CC(U8,F8): *( uint8_t*)dest = T8(0.5+*(double*)src); break;//iw810
case CC(U6,F8): *(uint16_t*)dest = T6(0.5+*(double*)src); break;//iw810
case CC(U2,F8): *(uint32_t*)dest = *(double*)src; break;//iw810
// to float (typically lossless, except for large integers)
case CC(F4,I8): *( float*)dest = *( int8_t*)src; break;
case CC(F4,I6): *( float*)dest = *( int16_t*)src; break;
case CC(F4,I2): *( float*)dest = *( int32_t*)src; break;//iw810
case CC(F8,I8): *(double*)dest = *( int8_t*)src; break;
case CC(F8,I6): *(double*)dest = *( int16_t*)src; break;
case CC(F8,I2): *(double*)dest = *( int32_t*)src; break;
case CC(F4,U8): *( float*)dest = *( uint8_t*)src; break;
case CC(F4,U6): *( float*)dest = *(uint16_t*)src; break;
case CC(F4,U2): *( float*)dest = *(uint32_t*)src; break;//iw810
case CC(F8,U8): *(double*)dest = *( uint8_t*)src; break;
case CC(F8,U6): *(double*)dest = *(uint16_t*)src; break;
case CC(F8,U2): *(double*)dest = *(uint32_t*)src; break;
case CC(F8,F4): *(double*)dest = *( float*)src; break;
case CC(F4,F8): *( float*)dest = *( double*)src; break;
#ifdef I_CAN_HAS_INT64
// to int64_t and uint64_t
case CC(I4,I8): *( int64_t*)dest = *( int8_t*)src; break;
case CC(I4,I6): *( int64_t*)dest = *( int16_t*)src; break;
case CC(I4,I2): *( int64_t*)dest = *( int32_t*)src; break;
case CC(I4,U8): *( int64_t*)dest = *( uint8_t*)src; break;
case CC(I4,U6): *( int64_t*)dest = *(uint16_t*)src; break;
case CC(I4,U2): *( int64_t*)dest = *(uint32_t*)src; break;
case CC(I4,F4): *( int64_t*)dest = *( float*)src; break;
case CC(I4,F8): *( int64_t*)dest = *( double*)src; break;
case CC(U4,I8): *(uint64_t*)dest = *( int8_t*)src; break;
case CC(U4,I6): *(uint64_t*)dest = *( int16_t*)src; break;
case CC(U4,I2): *(uint64_t*)dest = *( int32_t*)src; break;
case CC(U4,U8): *(uint64_t*)dest = *( uint8_t*)src; break;
case CC(U4,U6): *(uint64_t*)dest = *(uint16_t*)src; break;
case CC(U4,U2): *(uint64_t*)dest = *(uint32_t*)src; break;
case CC(U4,F4): *(uint64_t*)dest = *( float*)src; break;
case CC(U4,F8): *(uint64_t*)dest = *( double*)src; break;
// from int64_t and uint64_t
case CC(I8,I4): *( int8_t*)dest = *( int64_t*)src; break;
case CC(I6,I4): *( int16_t*)dest = *( int64_t*)src; break;
case CC(I2,I4): *( int32_t*)dest = *( int64_t*)src; break;
case CC(U8,I4): *( uint8_t*)dest = *( int64_t*)src; break;
case CC(U6,I4): *(uint16_t*)dest = *( int64_t*)src; break;
case CC(U2,I4): *(uint32_t*)dest = *( int64_t*)src; break;
case CC(F4,I4): *( float*)dest = *( int64_t*)src; break;
case CC(F8,I4): *( double*)dest = *( int64_t*)src; break;
case CC(I8,U4): *( int8_t*)dest = *(uint64_t*)src; break;
case CC(I6,U4): *( int16_t*)dest = *(uint64_t*)src; break;
case CC(I2,U4): *( int32_t*)dest = *(uint64_t*)src; break;
case CC(U8,U4): *( uint8_t*)dest = *(uint64_t*)src; break;
case CC(U6,U4): *(uint16_t*)dest = *(uint64_t*)src; break;
case CC(U2,U4): *(uint32_t*)dest = *(uint64_t*)src; break;
case CC(F4,U4): *( float*)dest = *(uint64_t*)src; break;
case CC(F8,U4): *( double*)dest = *(uint64_t*)src; break;
#endif//I_CAN_HAS_INT64
#ifdef I_CAN_HAS_LONGDOUBLE
// to longdouble
case CC(F6,I8): *(longdouble*)dest = *( int8_t*)src; break;
case CC(F6,I6): *(longdouble*)dest = *( int16_t*)src; break;
case CC(F6,I2): *(longdouble*)dest = *( int32_t*)src; break;
case CC(F6,U8): *(longdouble*)dest = *( uint8_t*)src; break;
case CC(F6,U6): *(longdouble*)dest = *(uint16_t*)src; break;
case CC(F6,U2): *(longdouble*)dest = *(uint32_t*)src; break;
case CC(F6,F4): *(longdouble*)dest = *( float*)src; break;
case CC(F6,F8): *(longdouble*)dest = *( double*)src; break;
// from longdouble
case CC(I8,F6): *( int8_t*)dest = *(longdouble*)src; break;
case CC(I6,F6): *( int16_t*)dest = *(longdouble*)src; break;
case CC(I2,F6): *( int32_t*)dest = *(longdouble*)src; break;
case CC(U8,F6): *( uint8_t*)dest = T8(0.5+*(longdouble*)src); break;
case CC(U6,F6): *(uint16_t*)dest = T6(0.5+*(longdouble*)src); break;
case CC(U2,F6): *(uint32_t*)dest = *(longdouble*)src; break;
case CC(F4,F6): *( float*)dest = *(longdouble*)src; break;
case CC(F8,F6): *( double*)dest = *(longdouble*)src; break;
#ifdef I_CAN_HAS_INT64 //(nested)
case CC(F6,I4): *(longdouble*)dest = *( int64_t*)src; break;
case CC(F6,U4): *(longdouble*)dest = *(uint64_t*)src; break;
case CC(I4,F6): *( int64_t*)dest = *(longdouble*)src; break;
case CC(U4,F6): *(uint64_t*)dest = *(longdouble*)src; break;
#endif//I_CAN_HAS_INT64 (nested)