TPCCLIB
Loading...
Searching...
No Matches
anabyteo.c
Go to the documentation of this file.
1
8/*****************************************************************************/
9#include "tpcclibConfig.h"
10/*****************************************************************************/
11#include <stdio.h>
12#include <stdlib.h>
13#include <unistd.h>
14#include <math.h>
15#include <string.h>
16#include <time.h>
17#include <sys/stat.h>
18#ifdef HAVE_DIRECT_H
19#include <direct.h>
20#endif
21/*****************************************************************************/
22#include "libtpcmisc.h"
23#include "libtpcimgio.h"
24/*****************************************************************************/
25
26/*****************************************************************************/
27static char *info[] = {
28 "Show or optionally convert the byte order in Analyze 7.5 images.",
29 "Without options the current byte order is shown but not changed.",
30 " ",
31 "Usage: @P [Options] database(s)>",
32 " ",
33 "Options:",
34 " -little",
35 " Change byte order to little endian (PC Intel).",
36 " -big",
37 " Change byte order to big endian (Sun Sparc, Motorola, PowerPC).",
38 " -o=<directory>",
39 " Converted files are placed in specified directory, and original",
40 " files are not overwritten.",
41 " -stdoptions", // List standard options like --help, -v, etc
42 " ",
43 "See also: ana2ecat, ecat2ana, ana_lhdr, ana_ehdr, convend",
44 " ",
45 "Keywords: image, format conversion, Analyze, byte order",
46 0};
47/*****************************************************************************/
48
49/*****************************************************************************/
50/* Turn on the globbing of the command line, since it is disabled by default in
51 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
52 In Unix&Linux wildcard command line processing is enabled by default. */
53/*
54#undef _CRT_glob
55#define _CRT_glob -1
56*/
57int _dowildcard = -1;
58/*****************************************************************************/
59
60/*****************************************************************************/
64int main(int argc, char **argv)
65{
66 int ai, help=0, version=0, verbose=1;
67 int errorNr=0;
68 unsigned int byteSize;
69 int ffi=0, fileNr=0;
70 int orderTo=-1; /* 1=big, 0=little */
71 char dbname[FILENAME_MAX], datfile[FILENAME_MAX], hdrfile[FILENAME_MAX];
72 char temp[FILENAME_MAX], outpath[FILENAME_MAX], *cptr;
73 char dbname2[FILENAME_MAX], datfile2[FILENAME_MAX], hdrfile2[FILENAME_MAX];
74 FILE *fpi, *fpo;
75 unsigned char buf[32];
76 ANALYZE_DSR dsr;
77
78
79 /*
80 * Get arguments
81 */
82 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
83 outpath[0]=dbname[0]=datfile[0]=hdrfile[0]=(char)0;
84 dbname2[0]=datfile2[0]=hdrfile2[0]=(char)0;
85 /* Options */
86 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') { /* options */
87 cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(cptr==NULL) continue;
88 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
89 if(strncasecmp(cptr, "LITTLE", 1)==0) {
90 orderTo=0; continue;
91 } else if(strncasecmp(cptr, "BIG", 1)==0) {
92 orderTo=1; continue;
93 } else if(strncasecmp(cptr, "O=", 2)==0) {
94 cptr+=2; if(strlen(cptr)>0) {strcpy(outpath, cptr); continue;}
95 }
96 fprintf(stderr, "Error: invalid option '%s'\n", argv[ai]);
97 return(1);
98 } else break;
99
100 /* Print help or version? */
101 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
102 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
103 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
104
105 /* Process other arguments, starting from the first non-option */
106 for(; ai<argc; ai++) {
107 if(ffi<1) ffi=ai;
108 /* Check that Analyze file (*.hdr and *.img) exists */
109 if(anaDatabaseExists(argv[ai], hdrfile, datfile, NULL) > 0) {fileNr++; continue;}
110 fprintf(stderr, "Error: Analyze file '%s' does not exist.\n", argv[ai]);
111 return(1);
112 }
113
114 /* Is something missing? */
115 if(fileNr<1) {
116 fprintf(stderr, "Error: no Analyze files were specified.\n");
117 return(1);
118 }
119
120 /* In verbose mode print arguments and options */
121 if(verbose>1) {
122 for(ai=0; ai<argc; ai++) printf(" %s", argv[ai]);
123 printf("\n");
124 printf("orderTo := %d\n", orderTo);
125 printf("outpath := %s\n", outpath);
126 printf("fileNr := %d\n", fileNr);
127 }
128 if(verbose>3) ANALYZE_TEST=verbose-3; else ANALYZE_TEST=0;
129
130
131 /*
132 * Process each Analyze image
133 */
134 if(verbose>1) printf("processing %d Analyze file(s)\n", fileNr);
135 for(ai=ffi; ai<argc; ai++) {
136
137 /* Check and process the given filename */
138 strlcpy(dbname, argv[ai], FILENAME_MAX);
139 /* If it has an Analyze extension, remove it */
141 if(verbose>0) fprintf(stdout, "%s : \n", dbname);
142
143 /* Make hdr and img filenames */
144 if(anaDatabaseExists(dbname, hdrfile, datfile, NULL) < 1) {
145 fprintf(stderr, " Error: Analyze file '%s' does not exist.\n", dbname);
146 errorNr++; continue;
147 }
148
149 /* Read analyze header */
150 if(anaReadHeader(hdrfile, &dsr)) {
151 fprintf(stderr, " Error: cannot read header file %s\n", hdrfile);
152 errorNr++; continue;
153 }
154
155 /* Show the current byte order */
156 if(verbose>0 || orderTo<0) {
157 if(dsr.little)
158 fprintf(stdout, " currently little endian (Intel) byte order.\n");
159 else
160 fprintf(stdout, " currently big endian (SUN Sparc) byte order.\n");
161 }
162
163 /* If no option was given, then do nothing else */
164 if(orderTo<0) continue;
165
166 /* If byte order is already the required one, then do nothing */
167 if((dsr.little==1 && orderTo==0) || (dsr.little==0 && orderTo==1)) {
168 if(verbose>0) fprintf(stdout, " no conversion was necessary.\n");
169 continue;
170 }
171
172 /* Make output Analyze database name */
173 if(!outpath[0]) {
174 strcpy(dbname2, dbname);
175 } else {
176 cptr=outpath+strlen(outpath)-1;
177 if(*cptr=='/' || *cptr=='\\') *cptr=(char)0;
178 cptr=strrchr(dbname, '/'); if(cptr==NULL) cptr=strrchr(dbname, '\\');
179 if(cptr==NULL) cptr=dbname; else cptr++;
180 snprintf(dbname2, FILENAME_MAX, "%s/%s", outpath, cptr);
181 /* Create output directory if necessary */
182 if(access(outpath, 0) == -1) {
183 if(verbose>0) fprintf(stdout, " creating subdirectory %s\n", outpath);
184 int ret=1;
185#ifdef HAVE_MKDIR2
186 ret=mkdir(outpath, 00775);
187#elif defined HAVE_MKDIR1
188 ret=mkdir(outpath);
189#elif defined HAVE__MKDIR
190 ret=_mkdir(outpath);
191#endif
192 if(ret!=0) {
193 fprintf(stderr, " Error: cannot create subdirectory.\n");
194 errorNr++;
195 return(5); /* there is no reason to try the next image */
196 }
197 }
198 }
199 if(verbose>1) fprintf(stdout, " Output dbname='%s'\n", dbname2);
200 strcpy(datfile2, dbname2); strcat(datfile2, ".img");
201 strcpy(hdrfile2, dbname2); strcat(hdrfile2, ".hdr");
202
203 /* Change byte order of the image data */
204 /* Get the nr of pixels and their size in bytes */
205 size_t pxlNr=dsr.dime.dim[1]*dsr.dime.dim[2];
206 if(dsr.dime.dim[0]>2) pxlNr*=dsr.dime.dim[3];
207 if(dsr.dime.dim[0]>3) pxlNr*=dsr.dime.dim[4];
208 byteSize=dsr.dime.bitpix/8;
209 if(verbose>1)
210 fprintf(stdout, " %zu pixels, %d byte(s)/pixel\n", pxlNr, byteSize);
211 if(pxlNr<1) {
212 fprintf(stderr, " Error: invalid image dimensions.\n");
213 errorNr++; continue;
214 }
215 if(byteSize>8) {
216 fprintf(stderr, " Error: unsupported bitpix.\n");
217 errorNr++; continue;
218 } else if(byteSize<2) {
219 if(verbose>0)
220 fprintf(stdout, " no conversion is needed for image file.\n");
221 } else {
222 /* Make input image datafile name */
223 if(verbose>0) {
224 if(!outpath[0]) fprintf(stdout, " converting %s\n", datfile);
225 else fprintf(stdout, " converting %s to %s\n", datfile, datfile2);
226 }
227 /* Open original datafile */
228 if((fpi=fopen(datfile, "rb")) == NULL) {
229 fprintf(stderr, " Error: cannot read image file %s\n", datfile);
230 errorNr++; continue;
231 }
232 /* Open temporary datafile */
233 strcpy(temp, datfile2); strcat(temp, "%%");
234 if(verbose>2) printf(" temp file: %s\n", temp);
235 if((fpo=fopen(temp, "wb")) == NULL) {
236 fprintf(stderr, " Error: cannot write temp file %s\n", temp);
237 errorNr++; fclose(fpi); continue;
238 }
239 /* Read, swap, and write each pixel value */
240 int ret=0;
241 for(size_t i=0; i<pxlNr; i++) {
242 if(fread(buf, 1, byteSize, fpi) < byteSize) {ret=1; break;}
243 swap(buf, buf, byteSize);
244 if(fwrite(buf, 1, byteSize, fpo)!=byteSize) {ret=2; break;}
245 } /* next pixel */
246 fclose(fpi); fclose(fpo);
247 if(ret) {
248 if(ret==1) fprintf(stderr, " Error: cannot read %s\n", datfile);
249 else fprintf(stderr, " Error: cannot write temp file %s\n", temp);
250 errorNr++; remove(temp); continue;
251 }
252 }
253
254 /* Write header with different byte order */
255 if(verbose>0) {
256 if(!outpath[0]) fprintf(stdout, " converting %s\n", hdrfile);
257 else fprintf(stdout, " converting %s to %s\n", hdrfile, hdrfile2);
258 }
259 if(dsr.little==0) dsr.little=1; else dsr.little=0;
260 if(anaWriteHeader(hdrfile2, &dsr)) {
261 fprintf(stderr, " Error: cannot write header file %s\n", hdrfile2);
262 if(byteSize>1) remove(temp);
263 errorNr++; continue;
264 }
265
266 /* If no errors, then replace the original image datafile by new */
267 /* or just rename the temp file */
268 if(byteSize>1) {
269 if(verbose>2)
270 printf(" remove(%s); rename(%s, %s);\n", datfile2, temp, datfile2);
271 remove(datfile2); rename(temp, datfile2);
272 }
273
274 } /* next image */
275
276 if(errorNr>0) return(errorNr+10);
277 return(0);
278}
279/*****************************************************************************/
280
281/*****************************************************************************/
int anaWriteHeader(char *filename, ANALYZE_DSR *h)
Definition analyze.c:283
int anaDatabaseExists(const char *dbname, char *hdrfile, char *imgfile, char *siffile)
Definition analyze.c:704
void anaRemoveFNameExtension(char *fname)
Definition analyze.c:687
int ANALYZE_TEST
Definition analyze.c:8
int anaReadHeader(char *filename, ANALYZE_DSR *h)
Definition analyze.c:129
Header file for libtpcimgio.
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:244
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
void swap(void *orig, void *new, int size)
Definition swap.c:31
ANALYZE_HEADER_IMGDIM dime