forked from NIFTI-Imaging/nifti_clib
/
nifti1_io.c
7548 lines (6197 loc) · 266 KB
/
nifti1_io.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
#define _NIFTI1_IO_C_
#include "nifti1_io.h" /* typedefs, prototypes, macros, etc. */
/*****===================================================================*****/
/***** Sample functions to deal with NIFTI-1 and ANALYZE files *****/
/*****...................................................................*****/
/***** This code is released to the public domain. *****/
/*****...................................................................*****/
/***** Author: Robert W Cox, SSCC/DIRP/NIMH/NIH/DHHS/USA/EARTH *****/
/***** Date: August 2003 *****/
/*****...................................................................*****/
/***** Neither the National Institutes of Health (NIH), nor any of its *****/
/***** employees imply any warranty of usefulness of this software for *****/
/***** any purpose, and do not assume any liability for damages, *****/
/***** incidental or otherwise, caused by any use of this document. *****/
/*****===================================================================*****/
/** \file nifti1_io.c
\brief main collection of nifti1 i/o routines
- written by Bob Cox, SSCC NIMH
- revised by Mark Jenkinson, FMRIB
- revised by Rick Reynolds, SSCC, NIMH
- revised by Kate Fissell, University of Pittsburgh
The library history can be viewed via "nifti_tool -nifti_hist".
<br>The library version can be viewed via "nifti_tool -nifti_ver".
*/
/*! global history and version strings, for printing */
static char const * const gni_history[] =
{
"----------------------------------------------------------------------\n"
"history (of nifti library changes):\n"
"\n",
"0.0 August, 2003 [rwcox]\n"
" (Robert W Cox of the National Institutes of Health, SSCC/DIRP/NIMH)\n"
" - initial version\n"
"\n",
"0.1 July/August, 2004 [Mark Jenkinson]\n"
" (FMRIB Centre, University of Oxford, UK)\n"
" - Mainly adding low-level IO and changing things to allow gzipped\n"
" files to be read and written\n"
" - Full backwards compatability should have been maintained\n"
"\n",
"0.2 16 Nov 2004 [rickr]\n"
" (Rick Reynolds of the National Institutes of Health, SSCC/DIRP/NIMH)\n"
" - included Mark's changes in the AFNI distribution (including znzlib/)\n"
" (HAVE_ZLIB is commented out for the standard distribution)\n"
" - modified nifti_validfilename() and nifti_makebasename()\n"
" - added nifti_find_file_extension()\n"
"\n",
"0.3 3 Dec 2004 [rickr]\n"
" - note: header extensions are not yet checked for\n"
" - added formatted history as global string, for printing\n"
" - added nifti_disp_lib_hist(), to display the nifti library history\n"
" - added nifti_disp_lib_version(), to display the nifti library history\n",
" - re-wrote nifti_findhdrname()\n"
" o used nifti_find_file_extension()\n"
" o changed order of file tests (default is .nii, depends on input)\n"
" o free hdrname on failure\n"
" - made similar changes to nifti_findimgname()\n"
" - check for NULL return from nifti_findhdrname() calls\n",
" - removed most of ERREX() macros\n"
" - modified nifti_image_read()\n"
" o added debug info and error checking (on gni_debug > 0, only)\n"
" o fail if workingname is NULL\n"
" o check for failure to open header file\n"
" o free workingname on failure\n"
" o check for failure of nifti_image_load()\n"
" o check for failure of nifti_convert_nhdr2nim()\n",
" - changed nifti_image_load() to int, and check nifti_read_buffer return\n"
" - changed nifti_read_buffer() to fail on short read, and to count float\n"
" fixes (to print on debug)\n"
" - changed nifti_image_infodump to print to stderr\n"
" - updated function header comments, or moved comments above header\n"
" - removed const keyword\n"
" - added LNI_FERR() macro for error reporting on input files\n"
"\n",
"0.4 10 Dec 2004 [rickr] - added header extensions\n"
" - in nifti1_io.h:\n"
" o added num_ext and ext_list to the definition of nifti_image\n"
" o made many functions static (more to follow)\n"
" o added LNI_MAX_NIA_EXT_LEN, for max nifti_type 3 extension length\n",
" - added __DATE__ to version output in nifti_disp_lib_version()\n"
" - added nifti_disp_matrix_orient() to print orientation information\n"
" - added '.nia' as a valid file extension in nifti_find_file_extension()\n"
" - added much more debug output\n"
" - in nifti_image_read(), in the case of an ASCII header, check for\n"
" extensions after the end of the header\n",
" - added nifti_read_extensions() function\n"
" - added nifti_read_next_extension() function\n"
" - added nifti_add_exten_to_list() function\n"
" - added nifti_check_extension() function\n"
" - added nifti_write_extensions() function\n"
" - added nifti_extension_size() function\n"
" - in nifti_set_iname_offest():\n"
" o adjust offset by the extension size and the extender size\n",
" o fixed the 'ceiling modulo 16' computation\n"
" - in nifti_image_write_hdr_img2(): \n"
" o added extension writing\n"
" o check for NULL return from nifti_findimgname()\n"
" - include number of extensions in nifti_image_to_ascii() output\n"
" - in nifti_image_from_ascii():\n"
" o return bytes_read as a parameter, computed from the final spos\n"
" o extract num_ext from ASCII header\n"
"\n",
"0.5 14 Dec 2004 [rickr] - added sub-brick reading functions\n"
" - added nifti_brick_list type to nifti1_io.h, along with new prototypes\n"
" - added main nifti_image_read_bricks() function, with description\n"
" - added nifti_image_load_bricks() - library function (requires nim)\n"
" - added valid_nifti_brick_list() - library function\n"
" - added free_NBL() - library function\n",
" - added update_nifti_image_for_brick_list() for dimension update\n"
" - added nifti_load_NBL_bricks(), nifti_alloc_NBL_mem(),\n"
" nifti_copynsort() and force_positive() (static functions)\n"
" - in nifti_image_read(), check for failed load only if read_data is set\n"
" - broke most of nifti_image_load() into nifti_image_load_prep()\n"
"\n",
"0.6 15 Dec 2004 [rickr] - added sub-brick writing functionality\n"
" - in nifti1_io.h, removed znzlib directory from include - all nifti\n"
" library files are now under the nifti directory\n"
" - nifti_read_extensions(): print no offset warning for nifti_type 3\n"
" - nifti_write_all_data():\n"
" o pass nifti_brick_list * NBL, for optional writing\n"
" o if NBL, write each sub-brick, sequentially\n",
" - nifti_set_iname_offset(): case 1 must have sizeof() cast to int\n"
" - pass NBL to nifti_image_write_hdr_img2(), and allow NBL or data\n"
" - added nifti_image_write_bricks() wrapper for ...write_hdr_img2()\n"
" - included compression abilities\n"
"\n",
"0.7 16 Dec 2004 [rickr] - minor changes to extension reading\n"
"\n",
"0.8 21 Dec 2004 [rickr] - restrict extension reading, and minor changes\n"
" - in nifti_image_read(), compute bytes for extensions (see remaining)\n"
" - in nifti_read_extensions(), pass 'remain' as space for extensions,\n"
" pass it to nifti_read_next_ext(), and update for each one read \n"
" - in nifti_check_extension(), require (size <= remain)\n",
" - in update_nifti_image_brick_list(), update nvox\n"
" - in nifti_image_load_bricks(), make explicit check for nbricks <= 0\n"
" - in int_force_positive(), check for (!list)\n"
" - in swap_nifti_header(), swap sizeof_hdr, and reorder to struct order\n"
" - change get_filesize functions to signed ( < 0 is no file or error )\n",
" - in nifti_validfilename(), lose redundant (len < 0) check\n"
" - make print_hex_vals() static\n"
" - in disp_nifti_1_header, restrict string field widths\n"
"\n",
"0.9 23 Dec 2004 [rickr] - minor changes\n"
" - broke ASCII header reading out of nifti_image_read(), into new\n"
" functions has_ascii_header() and read_ascii_image()\n",
" - check image_read failure and znzseek failure\n"
" - altered some debug output\n"
" - nifti_write_all_data() now returns an int\n"
"\n",
"0.10 29 Dec 2004 [rickr]\n"
" - renamed nifti_valid_extension() to nifti_check_extension()\n"
" - added functions nifti_makehdrname() and nifti_makeimgname()\n"
" - added function valid_nifti_extensions()\n"
" - in nifti_write_extensions(), check for validity before writing\n",
" - rewrote nifti_image_write_hdr_img2():\n"
" o set write_data and leave_open flags from write_opts\n"
" o add debug print statements\n"
" o use nifti_write_ascii_image() for the ascii case\n"
" o rewrote the logic of all cases to be easier to follow\n",
" - broke out code as nifti_write_ascii_image() function\n"
" - added debug to top-level write functions, and free the znzFile\n"
" - removed unused internal function nifti_image_open()\n"
"\n",
"0.11 30 Dec 2004 [rickr] - small mods\n"
" - moved static function prototypes from header to C file\n"
" - free extensions in nifti_image_free()\n"
"\n",
"1.0 07 Jan 2005 [rickr] - INITIAL RELEASE VERSION\n"
" - added function nifti_set_filenames()\n"
" - added function nifti_read_header()\n"
" - added static function nhdr_looks_good()\n"
" - added static function need_nhdr_swap()\n"
" - exported nifti_add_exten_to_list symbol\n",
" - fixed #bytes written in nifti_write_extensions()\n"
" - only modify offset if it is too small (nifti_set_iname_offset)\n"
" - added nifti_type 3 to nifti_makehdrname and nifti_makeimgname\n"
" - added function nifti_set_filenames()\n"
"\n",
"1.1 07 Jan 2005 [rickr]\n"
" - in nifti_read_header(), swap if needed\n"
"\n",
"1.2 07 Feb 2005 [kate fissell c/o rickr] \n"
" - nifti1.h: added doxygen comments for main struct and #define groups\n"
" - nifti1_io.h: added doxygen comments for file and nifti_image struct\n"
" - nifti1_io.h: added doxygen comments for file and some functions\n"
" - nifti1_io.c: changed nifti_copy_nim_info to use memcpy\n"
"\n",
"1.3 09 Feb 2005 [rickr]\n"
" - nifti1.h: added doxygen comments for extension structs\n"
" - nifti1_io.h: put most #defines in #ifdef _NIFTI1_IO_C_ block\n"
" - added a doxygen-style description to every exported function\n"
" - added doxygen-style comments within some functions\n"
" - re-exported many znzFile functions that I had made static\n"
" - re-added nifti_image_open (sorry, Mark)\n"
" - every exported function now has 'nifti' in the name (19 functions)\n",
" - made sure every alloc() has a failure test\n"
" - added nifti_copy_extensions function, for use in nifti_copy_nim_info\n"
" - nifti_is_gzfile: added initial strlen test\n"
" - nifti_set_filenames: added set_byte_order parameter option\n"
" (it seems appropriate to set the BO when new files are associated)\n"
" - disp_nifti_1_header: prints to stdout (a.o.t. stderr), with fflush\n"
"\n",
"1.4 23 Feb 2005 [rickr] - sourceforge merge\n"
" - merged into the nifti_io CVS directory structure at sourceforge.net\n"
" - merged in 4 changes by Mark, and re-added his const keywords\n"
" - cast some pointers to (void *) for -pedantic compile option\n"
" - added nifti_free_extensions()\n"
"\n",
"1.5 02 Mar 2005 [rickr] - started nifti global options\n"
" - gni_debug is now g_opts.debug\n"
" - added validity check parameter to nifti_read_header\n"
" - need_nhdr_swap no longer does test swaps on the stack\n"
"\n",
"1.6 05 April 2005 [rickr] - validation and collapsed_image_read\n"
" - added nifti_read_collapsed_image(), an interface for reading partial\n"
" datasets, specifying a subset of array indices\n"
" - for read_collapsed_image, added static functions: rci_read_data(),\n"
" rci_alloc_mem(), and make_pivot_list()\n",
" - added nifti_nim_is_valid() to check for consistency (more to do)\n"
" - added nifti_nim_has_valid_dims() to do many dimensions tests\n"
"\n",
"1.7 08 April 2005 [rickr]\n"
" - added nifti_update_dims_from_array() - to update dimensions\n"
" - modified nifti_makehdrname() and nifti_makeimgname():\n"
" if prefix has a valid extension, use it (else make one up)\n"
" - added nifti_get_intlist - for making an array of ints\n"
" - fixed init of NBL->bsize in nifti_alloc_NBL_mem() {thanks, Bob}\n"
"\n",
"1.8 14 April 2005 [rickr]\n"
" - added nifti_set_type_from_names(), for nifti_set_filenames()\n"
" (only updates type if number of files does not match it)\n"
" - added is_valid_nifti_type(), just to be sure\n"
" - updated description of nifti_read_collapsed_image() for *data change\n"
" (if *data is already set, assume memory exists for results)\n"
" - modified rci_alloc_mem() to allocate only if *data is NULL\n"
"\n",
"1.9 19 April 2005 [rickr]\n"
" - added extension codes NIFTI_ECODE_COMMENT and NIFTI_ECODE_XCEDE\n"
" - added nifti_type codes NIFTI_MAX_ECODE and NIFTI_MAX_FTYPE\n"
" - added nifti_add_extension() {exported}\n"
" - added nifti_fill_extension() as a static function\n"
" - added nifti_is_valid_ecode() {exported}\n",
" - nifti_type values are now NIFTI_FTYPE_* file codes\n"
" - in nifti_read_extensions(), decrement 'remain' by extender size, 4\n"
" - in nifti_set_iname_offset(), case 1, update if offset differs\n"
" - only output '-d writing nifti file' if debug > 1\n"
"\n",
"1.10 10 May 2005 [rickr]\n"
" - files are read using ZLIB only if they end in '.gz'\n"
"\n",
"1.11 12 August 2005 [kate fissell]\n"
" - Kate's 0.2 release packaging, for sourceforge\n"
"\n",
"1.12 17 August 2005 [rickr] - comment (doxygen) updates\n"
" - updated comments for most functions (2 updates from Cinly Ooi)\n"
" - added nifti_type_and_names_match()\n"
"\n",
"1.12a 24 August 2005 [rickr] - remove all tabs from Clibs/*/*.[ch]\n",
"1.12b 25 August 2005 [rickr] - changes by Hans Johnson\n",
"1.13 25 August 2005 [rickr]\n",
" - finished changes by Hans for Insight\n"
" - added const in all appropraite parameter locations (30-40)\n"
" (any pointer referencing data that will not change)\n"
" - shortened all string constants below 509 character limit\n"
"1.14 28 October 2005 [HJohnson]\n",
" - use nifti_set_filenames() in nifti_convert_nhdr2nim()\n"
"1.15 02 November 2005 [rickr]\n",
" - added skip_blank_ext to nifti_global_options\n"
" - added nifti_set_skip_blank_ext(), to set option\n"
" - if skip_blank_ext and no extensions, do not read/write extender\n"
"1.16 18 November 2005 [rickr]\n",
" - removed any test or access of dim[i], i>dim[0]\n"
" - do not set pixdim for collapsed dims to 1.0, leave them as they are\n"
" - added magic and dim[i] tests in nifti_hdr_looks_good()\n"
" - added 2 size_t casts\n"
"1.17 22 November 2005 [rickr]\n",
" - in hdr->nim, for i > dim[0], pass 0 or 1, else set to 1\n"
"1.18 02 March 2006 [rickr]\n",
" - in nifti_alloc_NBL_mem(), fixed nt=0 case from 1.17 change\n"
"1.19 23 May 2006 [HJohnson,rickr]\n",
" - nifti_write_ascii_image(): free(hstr)\n"
" - nifti_copy_extensions(): clear num_ext and ext_list\n"
"1.20 27 Jun 2006 [rickr]\n",
" - nifti_findhdrname(): fixed assign of efirst to match stated logic\n"
" (problem found by Atle Bjørnerud)\n"
"1.21 05 Sep 2006 [rickr] update for nifticlib-0.4 release\n",
" - was reminded to actually add nifti_set_skip_blank_ext()\n"
" - init g_opts.skip_blank_ext to 0\n"
"1.22 01 Jun 2007 nifticlib-0.5 release\n",
"1.23 05 Jun 2007 nifti_add_exten_to_list: revert on failure, free old list\n"
"1.24 07 Jun 2007 nifti_copy_extensions: use esize-8 for data size\n"
"1.25 12 Jun 2007 [rickr] EMPTY_IMAGE creation\n",
" - added nifti_make_new_header() - to create from dims/dtype\n"
" - added nifti_make_new_nim() - to create from dims/dtype/fill\n"
" - added nifti_is_valid_datatype(), and more debug info\n",
"1.26 27 Jul 2007 [rickr] handle single volumes > 2^31 bytes (but < 2^32)\n",
"1.27 28 Jul 2007 [rickr] nim->nvox, NBL-bsize are now type size_t\n"
"1.28 30 Jul 2007 [rickr] size_t updates\n",
"1.29 08 Aug 2007 [rickr] for list, valid_nifti_brick_list requires 3 dims\n"
"1.30 08 Nov 2007 [Yaroslav/rickr]\n"
" - fix ARM struct alignment problem in byte-swapping routines\n",
"1.31 29 Nov 2007 [rickr] for nifticlib-1.0.0\n"
" - added nifti_datatype_to/from_string routines\n"
" - added DT_RGBA32/NIFTI_TYPE_RGBA32 datatype macros (2304)\n"
" - added NIFTI_ECODE_FREESURFER (14)\n",
"1.32 08 Dec 2007 [rickr]\n"
" - nifti_hdr_looks_good() allows ANALYZE headers (req. by V. Luccio)\n"
" - added nifti_datatype_is_valid()\n",
"1.33 05 Feb 2008 [hansj,rickr] - block nia.gz use\n"
"1.34 13 Jun 2008 [rickr] - added nifti_compiled_with_zlib()\n"
"1.35 03 Aug 2008 [rickr]\n",
" - deal with swapping, so that CPU type does not affect output\n"
" (motivated by C Burns)\n"
" - added nifti_analyze75 structure and nifti_swap_as_analyze()\n"
" - previous swap_nifti_header is saved as old_swap_nifti_header\n"
" - also swap UNUSED fields in nifti_1_header struct\n",
"1.36 07 Oct 2008 [rickr]\n",
" - added nifti_NBL_matches_nim() check for write_bricks()\n"
"1.37 10 Mar 2009 [rickr]\n",
" - H Johnson cast updates (06 Feb)\n"
" - added NIFTI_ECODE_PYPICKLE for PyNIfTI (06 Feb)\n"
" - added NIFTI_ECODEs 18-28 for the LONI MiND group\n"
"1.38 28 Apr 2009 [rickr]\n",
" - uppercase extensions are now valid (requested by M. Coursolle)\n"
" - nifti_set_allow_upper_fext controls this option (req by C. Ooi)\n"
"1.39 23 Jun 2009 [rickr]: added 4 checks of alloc() returns\n",
"1.40 16 Mar 2010 [rickr]: added NIFTI_ECODE_VOXBO for D. Kimberg\n",
"1.41 28 Apr 2010 [rickr]: added NIFTI_ECODE_CARET for J. Harwell\n",
"1.42 06 Jul 2010 [rickr]: trouble with large (gz) files\n",
" - noted/investigated by M Hanke and Y Halchenko\n"
" - fixed znzread/write, noting example by M Adler\n"
" - changed nifti_swap_* routines/calls to take size_t (6)\n"
"1.43 07 Jul 2010 [rickr]: fixed znzR/W to again return nmembers\n",
"1.44 19 Jul 2013 [rickr]: ITK compatibility updates from H Johnson\n",
"1.45 10 May 2019 [rickr]: added NIFTI_ECODE_QUANTIPHYSE\n",
"1.46 26 Sep 2019 [rickr]:\n"
" - nifti_read_ascii_image no longer closes fp or free's fname\n",
"----------------------------------------------------------------------\n"
};
static const char gni_version[] = "nifti library version 1.46 (26 Sep, 2019)";
/*! global nifti options structure - init with defaults */
static nifti_global_options g_opts = {
1, /* debug level */
0, /* skip_blank_ext - skip extender if no extensions */
1 /* allow_upper_fext - allow uppercase file extensions */
};
/*! global nifti types structure list (per type, ordered oldest to newest) */
static const nifti_type_ele nifti_type_list[] = {
/* type nbyper swapsize name */
{ 0, 0, 0, "DT_UNKNOWN" },
{ 0, 0, 0, "DT_NONE" },
{ 1, 0, 0, "DT_BINARY" }, /* not usable */
{ 2, 1, 0, "DT_UNSIGNED_CHAR" },
{ 2, 1, 0, "DT_UINT8" },
{ 2, 1, 0, "NIFTI_TYPE_UINT8" },
{ 4, 2, 2, "DT_SIGNED_SHORT" },
{ 4, 2, 2, "DT_INT16" },
{ 4, 2, 2, "NIFTI_TYPE_INT16" },
{ 8, 4, 4, "DT_SIGNED_INT" },
{ 8, 4, 4, "DT_INT32" },
{ 8, 4, 4, "NIFTI_TYPE_INT32" },
{ 16, 4, 4, "DT_FLOAT" },
{ 16, 4, 4, "DT_FLOAT32" },
{ 16, 4, 4, "NIFTI_TYPE_FLOAT32" },
{ 32, 8, 4, "DT_COMPLEX" },
{ 32, 8, 4, "DT_COMPLEX64" },
{ 32, 8, 4, "NIFTI_TYPE_COMPLEX64" },
{ 64, 8, 8, "DT_DOUBLE" },
{ 64, 8, 8, "DT_FLOAT64" },
{ 64, 8, 8, "NIFTI_TYPE_FLOAT64" },
{ 128, 3, 0, "DT_RGB" },
{ 128, 3, 0, "DT_RGB24" },
{ 128, 3, 0, "NIFTI_TYPE_RGB24" },
{ 255, 0, 0, "DT_ALL" },
{ 256, 1, 0, "DT_INT8" },
{ 256, 1, 0, "NIFTI_TYPE_INT8" },
{ 512, 2, 2, "DT_UINT16" },
{ 512, 2, 2, "NIFTI_TYPE_UINT16" },
{ 768, 4, 4, "DT_UINT32" },
{ 768, 4, 4, "NIFTI_TYPE_UINT32" },
{ 1024, 8, 8, "DT_INT64" },
{ 1024, 8, 8, "NIFTI_TYPE_INT64" },
{ 1280, 8, 8, "DT_UINT64" },
{ 1280, 8, 8, "NIFTI_TYPE_UINT64" },
{ 1536, 16, 16, "DT_FLOAT128" },
{ 1536, 16, 16, "NIFTI_TYPE_FLOAT128" },
{ 1792, 16, 8, "DT_COMPLEX128" },
{ 1792, 16, 8, "NIFTI_TYPE_COMPLEX128" },
{ 2048, 32, 16, "DT_COMPLEX256" },
{ 2048, 32, 16, "NIFTI_TYPE_COMPLEX256" },
{ 2304, 4, 0, "DT_RGBA32" },
{ 2304, 4, 0, "NIFTI_TYPE_RGBA32" },
};
/*---------------------------------------------------------------------------*/
/* prototypes for internal functions - not part of exported library */
/* extension routines */
static int nifti_read_extensions( nifti_image *nim, znzFile fp, int remain );
static int nifti_read_next_extension( nifti1_extension * nex, nifti_image *nim, int remain, znzFile fp );
static int nifti_check_extension(nifti_image *nim, int size,int code, int rem);
static void update_nifti_image_for_brick_list(nifti_image * nim , int nbricks);
static int nifti_add_exten_to_list(nifti1_extension * new_ext,
nifti1_extension ** list, int new_length);
static int nifti_fill_extension(nifti1_extension * ext, const char * data,
int len, int ecode);
/* NBL routines */
static int nifti_load_NBL_bricks(nifti_image * nim , const int * slist, const int * sindex, nifti_brick_list * NBL, znzFile fp );
static int nifti_alloc_NBL_mem( nifti_image * nim, int nbricks,
nifti_brick_list * nbl);
static int nifti_copynsort(int nbricks, const int *blist, int **slist,
int **sindex);
static int nifti_NBL_matches_nim(const nifti_image *nim,
const nifti_brick_list *NBL);
/* for nifti_read_collapsed_image: */
static int rci_read_data(nifti_image *nim, int *pivots, int *prods, int nprods,
const int dims[], char *data, znzFile fp, size_t base_offset);
static int rci_alloc_mem(void ** data, const int prods[8], int nprods, int nbyper );
static int make_pivot_list(nifti_image * nim, const int dims[], int pivots[],
int prods[], int * nprods );
/* misc */
static int compare_strlist (const char * str, char ** strlist, int len);
static int fileext_compare (const char * test_ext, const char * known_ext);
static int fileext_n_compare (const char * test_ext,
const char * known_ext, size_t maxlen);
static int is_mixedcase (const char * str);
static int is_uppercase (const char * str);
static int make_lowercase (char * str);
static int make_uppercase (char * str);
static int need_nhdr_swap (short dim0, int hdrsize);
static int print_hex_vals (const char * data, size_t nbytes, FILE * fp);
static int unescape_string (char *str); /* string utility functions */
static char *escapize_string (const char *str);
/* internal I/O routines */
static znzFile nifti_image_load_prep( nifti_image *nim );
static int has_ascii_header(znzFile fp);
/*---------------------------------------------------------------------------*/
/* for calling from some main program */
/*----------------------------------------------------------------------*/
/*! display the nifti library module history (via stdout)
*//*--------------------------------------------------------------------*/
void nifti_disp_lib_hist( void )
{
int c, len = sizeof(gni_history)/sizeof(char *);
for( c = 0; c < len; c++ )
fputs(gni_history[c], stdout);
}
/*----------------------------------------------------------------------*/
/*! display the nifti library version (via stdout)
*//*--------------------------------------------------------------------*/
void nifti_disp_lib_version( void )
{
printf("%s, compiled %s\n", gni_version, __DATE__);
}
/*----------------------------------------------------------------------*/
/*! nifti_image_read_bricks - read nifti data as array of bricks
*
* 13 Dec 2004 [rickr]
*
* \param hname - filename of dataset to read (must be valid)
* \param nbricks - number of sub-bricks to read
* (if blist is valid, nbricks must be > 0)
* \param blist - list of sub-bricks to read
* (can be NULL; if NULL, read complete dataset)
* \param NBL - pointer to empty nifti_brick_list struct
* (must be a valid pointer)
*
* \return
* <br> nim - same as nifti_image_read, but
* nim->nt = NBL->nbricks (or nt*nu*nv*nw)
* nim->nu,nv,nw = 1
* nim->data = NULL
* <br> NBL - filled with data volumes
*
* By default, this function will read the nifti dataset and break the data
* into a list of nt*nu*nv*nw sub-bricks, each having size nx*ny*nz elements.
* That is to say, instead of reading the entire dataset as a single array,
* break it up into sub-bricks (volumes), each of size nx*ny*nz elements.
*
* Note: in the returned nifti_image, nu, nv and nw will always be 1. The
* intention of this function is to collapse the dataset into a single
* array of volumes (of length nbricks or nt*nu*nv*nw).
*
* If 'blist' is valid, it is taken to be a list of sub-bricks, of length
* 'nbricks'. The data will still be separated into sub-bricks of size
* nx*ny*nz elements, but now 'nbricks' sub-bricks will be returned, of the
* caller's choosing via 'blist'.
*
* E.g. consider a dataset with 12 sub-bricks (numbered 0..11), and the
* following code:
*
* <pre>
* { nifti_brick_list NB_orig, NB_select;
* nifti_image * nim_orig, * nim_select;
* int blist[5] = { 7, 0, 5, 5, 9 };
*
* nim_orig = nifti_image_read_bricks("myfile.nii", 0, NULL, &NB_orig);
* nim_select = nifti_image_read_bricks("myfile.nii", 5, blist, &NB_select);
* }
* </pre>
*
* Here, nim_orig gets the entire dataset, where NB_orig.nbricks = 12. But
* nim_select has NB_select.nbricks = 5.
*
* Note that the first case is not quite the same as just calling the
* nifti_image_read function, as here the data is separated into sub-bricks.
*
* Note that valid blist elements are in [0..nt*nu*nv*nw-1],
* or written [ 0 .. (dim[4]*dim[5]*dim[6]*dim[7] - 1) ].
*
* Note that, as is the case with all of the reading functions, the
* data will be allocated, read in, and properly byte-swapped, if
* necessary.
*
* \sa nifti_image_load_bricks, nifti_free_NBL, valid_nifti_brick_list,
nifti_image_read
*//*----------------------------------------------------------------------*/
nifti_image *nifti_image_read_bricks(const char * hname, int nbricks,
const int * blist, nifti_brick_list * NBL)
{
nifti_image * nim;
if( !hname || !NBL ){
fprintf(stderr,"** nifti_image_read_bricks: bad params (%p,%p)\n",
hname, (void *)NBL);
return NULL;
}
if( blist && nbricks <= 0 ){
fprintf(stderr,"** nifti_image_read_bricks: bad nbricks, %d\n", nbricks);
return NULL;
}
nim = nifti_image_read(hname, 0); /* read header, but not data */
if( !nim ) return NULL; /* errors were already printed */
/* if we fail, free image and return */
if( nifti_image_load_bricks(nim, nbricks, blist, NBL) <= 0 ){
nifti_image_free(nim);
return NULL;
}
if( blist ) update_nifti_image_for_brick_list(nim, nbricks);
return nim;
}
/*----------------------------------------------------------------------
* update_nifti_image_for_brick_list - update nifti_image
*
* When loading a specific brick list, the distinction between
* nt, nu, nv and nw is lost. So put everything in t, and set
* dim[0] = 4.
*----------------------------------------------------------------------*/
static void update_nifti_image_for_brick_list( nifti_image * nim , int nbricks )
{
int ndim;
if( g_opts.debug > 2 ){
fprintf(stderr,"+d updating image dimensions for %d bricks in list\n",
nbricks);
fprintf(stderr," ndim = %d\n",nim->ndim);
fprintf(stderr," nx,ny,nz,nt,nu,nv,nw: (%d,%d,%d,%d,%d,%d,%d)\n",
nim->nx, nim->ny, nim->nz, nim->nt, nim->nu, nim->nv, nim->nw);
}
nim->nt = nbricks;
nim->nu = nim->nv = nim->nw = 1;
nim->dim[4] = nbricks;
nim->dim[5] = nim->dim[6] = nim->dim[7] = 1;
/* compute nvox */
/* do not rely on dimensions above dim[0] 16 Nov 2005 [rickr] */
for( nim->nvox = 1, ndim = 1; ndim <= nim->dim[0]; ndim++ )
nim->nvox *= nim->dim[ndim];
/* update the dimensions to 4 or lower */
for( ndim = 4; (ndim > 1) && (nim->dim[ndim] <= 1); ndim-- )
;
if( g_opts.debug > 2 ){
fprintf(stderr,"+d ndim = %d -> %d\n",nim->ndim, ndim);
fprintf(stderr," --> (%d,%d,%d,%d,%d,%d,%d)\n",
nim->nx, nim->ny, nim->nz, nim->nt, nim->nu, nim->nv, nim->nw);
}
nim->dim[0] = nim->ndim = ndim;
}
/*----------------------------------------------------------------------*/
/*! nifti_update_dims_from_array - update nx, ny, ... from nim->dim[]
Fix all the dimension information, based on a new nim->dim[].
Note: we assume that dim[0] will not increase.
Check for updates to pixdim[], dx,..., nx,..., nvox, ndim, dim[0].
*//*--------------------------------------------------------------------*/
int nifti_update_dims_from_array( nifti_image * nim )
{
int c, ndim;
if( !nim ){
fprintf(stderr,"** update_dims: missing nim\n");
return 1;
}
if( g_opts.debug > 2 ){
fprintf(stderr,"+d updating image dimensions given nim->dim:");
for( c = 0; c < 8; c++ ) fprintf(stderr," %d", nim->dim[c]);
fputc('\n',stderr);
}
/* verify dim[0] first */
if(nim->dim[0] < 1 || nim->dim[0] > 7){
fprintf(stderr,"** invalid dim[0], dim[] = ");
for( c = 0; c < 8; c++ ) fprintf(stderr," %d", nim->dim[c]);
fputc('\n',stderr);
return 1;
}
/* set nx, ny ..., dx, dy, ..., one by one */
/* less than 1, set to 1, else copy */
if(nim->dim[1] < 1) nim->nx = nim->dim[1] = 1;
else nim->nx = nim->dim[1];
nim->dx = nim->pixdim[1];
/* if undefined, or less than 1, set to 1 */
if(nim->dim[0] < 2 || (nim->dim[0] >= 2 && nim->dim[2] < 1))
nim->ny = nim->dim[2] = 1;
else
nim->ny = nim->dim[2];
/* copy delta values, in any case */
nim->dy = nim->pixdim[2];
if(nim->dim[0] < 3 || (nim->dim[0] >= 3 && nim->dim[3] < 1))
nim->nz = nim->dim[3] = 1;
else /* just copy vals from arrays */
nim->nz = nim->dim[3];
nim->dz = nim->pixdim[3];
if(nim->dim[0] < 4 || (nim->dim[0] >= 4 && nim->dim[4] < 1))
nim->nt = nim->dim[4] = 1;
else /* just copy vals from arrays */
nim->nt = nim->dim[4];
nim->dt = nim->pixdim[4];
if(nim->dim[0] < 5 || (nim->dim[0] >= 5 && nim->dim[5] < 1))
nim->nu = nim->dim[5] = 1;
else /* just copy vals from arrays */
nim->nu = nim->dim[5];
nim->du = nim->pixdim[5];
if(nim->dim[0] < 6 || (nim->dim[0] >= 6 && nim->dim[6] < 1))
nim->nv = nim->dim[6] = 1;
else /* just copy vals from arrays */
nim->nv = nim->dim[6];
nim->dv = nim->pixdim[6];
if(nim->dim[0] < 7 || (nim->dim[0] >= 7 && nim->dim[7] < 1))
nim->nw = nim->dim[7] = 1;
else /* just copy vals from arrays */
nim->nw = nim->dim[7];
nim->dw = nim->pixdim[7];
for( c = 1, nim->nvox = 1; c <= nim->dim[0]; c++ )
nim->nvox *= nim->dim[c];
/* compute ndim, assuming it can be no larger than the old one */
for( ndim = nim->dim[0]; (ndim > 1) && (nim->dim[ndim] <= 1); ndim-- )
;
if( g_opts.debug > 2 ){
fprintf(stderr,"+d ndim = %d -> %d\n",nim->ndim, ndim);
fprintf(stderr," --> (%d,%d,%d,%d,%d,%d,%d)\n",
nim->nx, nim->ny, nim->nz, nim->nt, nim->nu, nim->nv, nim->nw);
}
nim->dim[0] = nim->ndim = ndim;
return 0;
}
/*----------------------------------------------------------------------*/
/*! Load the image data from disk into an already-prepared image struct.
*
* \param nim - initialized nifti_image, without data
* \param nbricks - the length of blist (must be 0 if blist is NULL)
* \param blist - an array of xyz volume indices to read (can be NULL)
* \param NBL - pointer to struct where resulting data will be stored
*
* If blist is NULL, read all sub-bricks.
*
* \return the number of loaded bricks (NBL->nbricks),
* 0 on failure, < 0 on error
*
* NOTE: it is likely that another function will copy the data pointers
* out of NBL, in which case the only pointer the calling function
* will want to free is NBL->bricks (not each NBL->bricks[i]).
*//*--------------------------------------------------------------------*/
int nifti_image_load_bricks( nifti_image * nim , int nbricks,
const int * blist, nifti_brick_list * NBL )
{
int * slist = NULL, * sindex = NULL, rv;
znzFile fp;
/* we can have blist == NULL */
if( !nim || !NBL ){
fprintf(stderr,"** nifti_image_load_bricks, bad params (%p,%p)\n",
(void *)nim, (void *)NBL);
return -1;
}
if( blist && nbricks <= 0 ){
if( g_opts.debug > 1 )
fprintf(stderr,"-d load_bricks: received blist with nbricks = %d,"
"ignoring blist\n", nbricks);
blist = NULL; /* pretend nothing was passed */
}
if( blist && ! valid_nifti_brick_list(nim, nbricks, blist, g_opts.debug>0) )
return -1;
/* for efficiency, let's read the file in order */
if( blist && nifti_copynsort( nbricks, blist, &slist, &sindex ) != 0 )
return -1;
/* open the file and position the FILE pointer */
fp = nifti_image_load_prep( nim );
if( !fp ){
if( g_opts.debug > 0 )
fprintf(stderr,"** nifti_image_load_bricks, failed load_prep\n");
if( blist ){ free(slist); free(sindex); }
return -1;
}
/* this will flag to allocate defaults */
if( !blist ) nbricks = 0;
if( nifti_alloc_NBL_mem( nim, nbricks, NBL ) != 0 ){
if( blist ){ free(slist); free(sindex); }
znzclose(fp);
return -1;
}
rv = nifti_load_NBL_bricks(nim, slist, sindex, NBL, fp);
if( rv != 0 ){
nifti_free_NBL( NBL ); /* failure! */
NBL->nbricks = 0; /* repetative, but clear */
}
if( slist ){ free(slist); free(sindex); }
znzclose(fp);
return NBL->nbricks;
}
/*----------------------------------------------------------------------*/
/*! nifti_free_NBL - free all pointers and clear structure
*
* note: this does not presume to free the structure pointer
*//*--------------------------------------------------------------------*/
void nifti_free_NBL( nifti_brick_list * NBL )
{
int c;
if( NBL->bricks ){
for( c = 0; c < NBL->nbricks; c++ )
if( NBL->bricks[c] ) free(NBL->bricks[c]);
free(NBL->bricks);
NBL->bricks = NULL;
}
NBL->bsize = NBL->nbricks = 0;
}
/*----------------------------------------------------------------------
* nifti_load_NBL_bricks - read the file data into the NBL struct
*
* return 0 on success, -1 on failure
*----------------------------------------------------------------------*/
static int nifti_load_NBL_bricks( nifti_image * nim , const int * slist, const int * sindex,
nifti_brick_list * NBL, znzFile fp )
{
size_t oposn, fposn; /* orig and current file positions */
size_t rv;
long test;
int c;
int prev, isrc, idest; /* previous and current sub-brick, and new index */
test = znztell(fp); /* store current file position */
if( test < 0 ){
fprintf(stderr,"** load bricks: ztell failed??\n");
return -1;
}
fposn = oposn = test;
/* first, handle the default case, no passed blist */
if( !slist ){
for( c = 0; c < NBL->nbricks; c++ ) {
rv = nifti_read_buffer(fp, NBL->bricks[c], NBL->bsize, nim);
if( rv != NBL->bsize ){
fprintf(stderr,"** load bricks: cannot read brick %d from '%s'\n",
c, nim->iname ? nim->iname : nim->fname);
return -1;
}
}
if( g_opts.debug > 1 )
fprintf(stderr,"+d read %d default %u-byte bricks from file %s\n",
NBL->nbricks, (unsigned int)NBL->bsize,
nim->iname ? nim->iname:nim->fname );
return 0;
}
if( !sindex ){
fprintf(stderr,"** load_NBL_bricks: missing index list\n");
return -1;
}
prev = -1; /* use prev for previous sub-brick */
for( c = 0; c < NBL->nbricks; c++ ){
isrc = slist[c]; /* this is original brick index (c is new one) */
idest = sindex[c]; /* this is the destination index for this data */
/* if this sub-brick is not the previous, we must read from disk */
if( isrc != prev ){
/* if we are not looking at the correct sub-brick, scan forward */
if( fposn != (oposn + isrc*NBL->bsize) ){
fposn = oposn + isrc*NBL->bsize;
if( znzseek(fp, (long)fposn, SEEK_SET) < 0 ){
fprintf(stderr,"** failed to locate brick %d in file '%s'\n",
isrc, nim->iname ? nim->iname : nim->fname);
return -1;
}
}
/* only 10,000 lines later and we're actually reading something! */
rv = nifti_read_buffer(fp, NBL->bricks[idest], NBL->bsize, nim);
if( rv != NBL->bsize ){
fprintf(stderr,"** failed to read brick %d from file '%s'\n",
isrc, nim->iname ? nim->iname : nim->fname);
if( g_opts.debug > 1 )
fprintf(stderr," (read %u of %u bytes)\n",
(unsigned int)rv, (unsigned int)NBL->bsize);
return -1;
}
fposn += NBL->bsize;
} else {
/* we have already read this sub-brick, just copy the previous one */
/* note that this works because they are sorted */
memcpy(NBL->bricks[idest], NBL->bricks[sindex[c-1]], NBL->bsize);
}
prev = isrc; /* in any case, note the now previous sub-brick */
}
return 0;
}
/*----------------------------------------------------------------------
* nifti_alloc_NBL_mem - allocate memory for bricks
*
* return 0 on success, -1 on failure
*----------------------------------------------------------------------*/
static int nifti_alloc_NBL_mem(nifti_image * nim, int nbricks,
nifti_brick_list * nbl)
{
int c;
/* if nbricks is not specified, use the default */
if( nbricks > 0 ) nbl->nbricks = nbricks;
else { /* I missed this one with the 1.17 change 02 Mar 2006 [rickr] */
nbl->nbricks = 1;
for( c = 4; c <= nim->ndim; c++ )
nbl->nbricks *= nim->dim[c];
}
nbl->bsize = (size_t)nim->nx * nim->ny * nim->nz * nim->nbyper;/* bytes */
nbl->bricks = (void **)malloc(nbl->nbricks * sizeof(void *));
if( ! nbl->bricks ){
fprintf(stderr,"** NANM: failed to alloc %d void ptrs\n",nbricks);
return -1;
}
for( c = 0; c < nbl->nbricks; c++ ){
nbl->bricks[c] = (void *)malloc(nbl->bsize);
if( ! nbl->bricks[c] ){
fprintf(stderr,"** NANM: failed to alloc %u bytes for brick %d\n",
(unsigned int)nbl->bsize, c);
/* so free and clear everything before returning */
while( c > 0 ){
c--;
free(nbl->bricks[c]);
}
free(nbl->bricks);
nbl->bricks = NULL;
nbl->bsize = nbl->nbricks = 0;
return -1;
}
}
if( g_opts.debug > 2 )
fprintf(stderr,"+d NANM: alloc'd %d bricks of %u bytes for NBL\n",
nbl->nbricks, (unsigned int)nbl->bsize);
return 0;
}
/*----------------------------------------------------------------------
* nifti_copynsort - copy int list, and sort with indices
*
* 1. duplicate the incoming list
* 2. create an sindex list, and init with 0..nbricks-1
* 3. do a slow insertion sort on the small slist, along with sindex list
* 4. check results, just to be positive
*
* So slist is sorted, and sindex hold original positions.
*
* return 0 on success, -1 on failure
*----------------------------------------------------------------------*/
static int nifti_copynsort(int nbricks, const int * blist, int ** slist,
int ** sindex)
{
int * stmp, * itmp; /* for ease of typing/reading */
int c1, c2, spos, tmp;
*slist = (int *)malloc(nbricks * sizeof(int));
*sindex = (int *)malloc(nbricks * sizeof(int));
if( !*slist || !*sindex ){
fprintf(stderr,"** NCS: failed to alloc %d ints for sorting\n",nbricks);
if(*slist) free(*slist); /* maybe one succeeded */
if(*sindex) free(*sindex);
return -1;
}
/* init the lists */
memcpy(*slist, blist, nbricks*sizeof(int));
for( c1 = 0; c1 < nbricks; c1++ ) (*sindex)[c1] = c1;
/* now actually sort slist */
stmp = *slist;
itmp = *sindex;
for( c1 = 0; c1 < nbricks-1; c1++ ) {
/* find smallest value, init to current */
spos = c1;
for( c2 = c1+1; c2 < nbricks; c2++ )
if( stmp[c2] < stmp[spos] ) spos = c2;
if( spos != c1 ) /* swap: fine, don't maintain sub-order, see if I care */
{
tmp = stmp[c1]; /* first swap the sorting values */
stmp[c1] = stmp[spos];
stmp[spos] = tmp;
tmp = itmp[c1]; /* then swap the index values */
itmp[c1] = itmp[spos];
itmp[spos] = tmp;
}
}
if( g_opts.debug > 2 ){
fprintf(stderr, "+d sorted indexing list:\n");
fprintf(stderr, " orig : ");
for( c1 = 0; c1 < nbricks; c1++ ) fprintf(stderr," %d",blist[c1]);
fprintf(stderr,"\n new : ");
for( c1 = 0; c1 < nbricks; c1++ ) fprintf(stderr," %d",stmp[c1]);
fprintf(stderr,"\n indices: ");
for( c1 = 0; c1 < nbricks; c1++ ) fprintf(stderr," %d",itmp[c1]);
fputc('\n', stderr);
}