TPCCLIB
Loading...
Searching...
No Matches
nii_ehdr.c
Go to the documentation of this file.
1
7/*****************************************************************************/
8#include "tpcclibConfig.h"
9/*****************************************************************************/
10#include <stdio.h>
11#include <stdlib.h>
12#include <math.h>
13#include <string.h>
14#include <time.h>
15/*****************************************************************************/
16#include "libtpcmisc.h"
17#include "libtpcimgio.h"
18/*****************************************************************************/
19#include "niftiift.h"
20/*****************************************************************************/
21
22/*****************************************************************************/
23static char *info[] = {
24 "Edits the information in NIfTI-1 image header.",
25 "New contents are given in an ASCII text file with one or more header fields",
26 "in the same format as is the output of nii_lhdr (IFT format).",
27 "Use this program only if you are familiar with NIfTI format; note that",
28 "no backup is created.",
29 "Note that frame times are not stored in NIfTI header but in SIF.",
30 " ",
31 "Usage: @P niftifile headerfile",
32 " ",
33 "Options:",
34 " -stdoptions", // List standard options like --help, -v, etc
35 " ",
36 "Example: copy header contents from image1.nii to image2.nii",
37 " nii_lhdr image1.nii > header.dat",
38 " @P image2.nii header.dat",
39 " ",
40 "See also: nii_lhdr, iftlist, iftdel, iftadd, ana_lhdr",
41 " ",
42 "Keywords: image, NIfTI, header, IFT",
43 0};
44/*****************************************************************************/
45
46/*****************************************************************************/
47/* Turn on the globbing of the command line, since it is disabled by default in
48 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
49 In Unix&Linux wildcard command line processing is enabled by default. */
50/*
51#undef _CRT_glob
52#define _CRT_glob -1
53*/
54int _dowildcard = -1;
55/*****************************************************************************/
56
57/*****************************************************************************/
61int main(int argc, char **argv)
62{
63 int ai, help=0, version=0, verbose=1;
64 int ret;
65 char hdrfile[FILENAME_MAX], iftfile[FILENAME_MAX], tmp[256];
66 NIFTI_DSR dsr;
67 IFT ift1, ift2;
68
69
70 /*
71 * Get arguments
72 */
73 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
74 hdrfile[0]=iftfile[0]=(char)0;
75 iftInit(&ift1); iftInit(&ift2);
76 /* Options */
77 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') { /* options */
78 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
79 fprintf(stderr, "Error: invalid option '%s'.\n", argv[ai]);
80 return(1);
81 } else break;
82
83 /* Print help or version? */
84 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
85 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
86 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
87
88 /* Process other arguments, starting from the first non-option */
89 for(; ai<argc; ai++) {
90 if(!hdrfile[0]) {strlcpy(hdrfile, argv[ai], FILENAME_MAX); continue;}
91 else if(!iftfile[0]) {strlcpy(iftfile, argv[ai], FILENAME_MAX); continue;}
92 fprintf(stderr, "Error: invalid argument '%s'.\n", argv[ai]);
93 return(1);
94 }
95
96 /* Is something missing? */
97 if(!iftfile[0]) {
98 fprintf(stderr, "Error: missing command-line argument.\n");
99 return(1);
100 }
101
102
103 /* In verbose mode print arguments and options */
104 if(verbose>1) {
105 printf("hdrfile := %s\n", hdrfile);
106 printf("iftfile := %s\n", iftfile);
107 }
108
109
110 /*
111 * Read original header file
112 */
113 if(verbose>1) printf("reading %s\n", hdrfile);
114 ret=niftiReadHeader(hdrfile, &dsr, verbose-2, tmp);
115 if(ret) {
116 fprintf(stderr, "Error: cannot read %s: %s.\n", hdrfile, tmp);
117 return(2);
118 }
119 /* Copy header contents to IFT struct */
120 ret=niftiHeaderToIFT(&dsr, &ift1, verbose-4);
121 if(ret!=STATUS_OK) {
122 fprintf(stderr, "Error: invalid header structure (%d).\n", ret);
123 return(5);
124 }
125 if(verbose>3) {
126 fprintf(stdout, "\nOriginal header:\n\n");
127 iftWrite(&ift1, "stdout", 0);
128 fprintf(stdout, "\n");
129 }
130
131
132 /*
133 * Read new header contents
134 */
135 if(verbose>1) printf("reading %s\n", iftfile);
136 ret=iftRead(&ift2, iftfile, 1, 0);
137 if(ret) {
138 fprintf(stderr, "Error in reading %s: %s.\n", iftfile, ift2.status);
139 iftEmpty(&ift1);
140 return(6);
141 }
142 if(verbose>4) {
143 fprintf(stdout, "\nNew header contents:\n\n");
144 iftWrite(&ift2, "stdout", 0);
145 fprintf(stdout, "\n");
146 }
147
148
149 /*
150 * Copy new contents into existing header struct
151 */
152 ret=niftiHeaderFromIFT(&dsr, &ift2, verbose-4);
153 if(ret!=STATUS_OK) {
154 fprintf(stderr, "Error: invalid header contents in %s.\n", iftfile);
155 iftEmpty(&ift1); iftEmpty(&ift2);
156 return(7);
157 }
158 /* Copy edited header contents to IFT struct */
159 iftEmpty(&ift2);
160 ret=niftiHeaderToIFT(&dsr, &ift2, verbose-4);
161 if(ret!=STATUS_OK) {
162 fprintf(stderr, "Error: invalid header structure (%d).\n", ret);
163 return(5);
164 }
165 if(verbose>3) {
166 fprintf(stdout, "\nNew header contents:\n\n");
167 iftWrite(&ift2, "stdout", 0);
168 fprintf(stdout, "\n");
169 }
170
171
172 /*
173 * Write edited header
174 */
175 if(verbose>1) printf("writing %s\n", hdrfile);
176 ret=niftiWriteHeader(hdrfile, &dsr, verbose-2, tmp);
177 if(ret!=STATUS_OK) {
178 fprintf(stderr, "Error: cannot write file: %s.\n", tmp);
179 return(11);
180 }
181
182 iftEmpty(&ift1);
183 iftEmpty(&ift2);
184
185 return(0);
186}
187/*****************************************************************************/
188
189/*****************************************************************************/
void iftEmpty(IFT *ift)
Definition ift.c:60
void iftInit(IFT *ift)
Definition ift.c:45
int iftRead(IFT *ift, char *filename, int is_key_required, int verbose)
Definition iftfile.c:24
int iftWrite(IFT *ift, char *filename, int verbose)
Definition iftfile.c:282
Header file for libtpcimgio.
int niftiWriteHeader(char *filename, NIFTI_DSR *dsr, int verbose, char *status)
Definition nifti.c:844
int niftiReadHeader(char *filename, NIFTI_DSR *h, int verbose, char *status)
Definition nifti.c:293
Header file for libtpcmisc.
int tpcProcessStdOptions(const char *s, int *print_usage, int *print_version, int *verbose_level)
Definition proginfo.c:40
size_t strlcpy(char *dst, const char *src, size_t dstsize)
Definition strext.c:245
int tpcHtmlUsage(const char *program, char *text[], const char *path)
Definition proginfo.c:213
void tpcPrintBuild(const char *program, FILE *fp)
Definition proginfo.c:383
void tpcPrintUsage(const char *program, char *text[], FILE *fp)
Definition proginfo.c:158
int niftiHeaderFromIFT(NIFTI_DSR *dsr, IFT *ift, int verbose)
Definition niftiift.c:25
int niftiHeaderToIFT(NIFTI_DSR *dsr, IFT *ift, int verbose)
Definition niftiift.c:256
Header file for niftiift.c.
const char * status
Definition libtpcmisc.h:277