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