9#include "tpcclibConfig.h"
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.",
31 "Usage: @P [Options] database(s)>",
35 " Change byte order to little endian (PC Intel).",
37 " Change byte order to big endian (Sun Sparc, Motorola, PowerPC).",
39 " Converted files are placed in specified directory, and original",
40 " files are not overwritten.",
43 "See also: ana2ecat, ecat2ana, ana_lhdr, ana_ehdr, convend",
45 "Keywords: image, format conversion, Analyze, byte order",
64int main(
int argc,
char **argv)
66 int ai, help=0, version=0, verbose=1;
68 unsigned int byteSize;
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];
75 unsigned char buf[32];
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;
86 for(ai=1; ai<argc; ai++)
if(*argv[ai]==
'-') {
87 cptr=argv[ai]+1;
if(*cptr==
'-') cptr++;
if(cptr==NULL)
continue;
89 if(strncasecmp(cptr,
"LITTLE", 1)==0) {
91 }
else if(strncasecmp(cptr,
"BIG", 1)==0) {
93 }
else if(strncasecmp(cptr,
"O=", 2)==0) {
94 cptr+=2;
if(strlen(cptr)>0) {strcpy(outpath, cptr);
continue;}
96 fprintf(stderr,
"Error: invalid option '%s'\n", argv[ai]);
101 if(help==2) {
tpcHtmlUsage(argv[0], info,
"");
return(0);}
106 for(; ai<argc; ai++) {
109 if(
anaDatabaseExists(argv[ai], hdrfile, datfile, NULL) > 0) {fileNr++;
continue;}
110 fprintf(stderr,
"Error: Analyze file '%s' does not exist.\n", argv[ai]);
116 fprintf(stderr,
"Error: no Analyze files were specified.\n");
122 for(ai=0; ai<argc; ai++) printf(
" %s", argv[ai]);
124 printf(
"orderTo := %d\n", orderTo);
125 printf(
"outpath := %s\n", outpath);
126 printf(
"fileNr := %d\n", fileNr);
134 if(verbose>1) printf(
"processing %d Analyze file(s)\n", fileNr);
135 for(ai=ffi; ai<argc; ai++) {
138 strlcpy(dbname, argv[ai], FILENAME_MAX);
141 if(verbose>0) fprintf(stdout,
"%s : \n", dbname);
145 fprintf(stderr,
" Error: Analyze file '%s' does not exist.\n", dbname);
151 fprintf(stderr,
" Error: cannot read header file %s\n", hdrfile);
156 if(verbose>0 || orderTo<0) {
158 fprintf(stdout,
" currently little endian (Intel) byte order.\n");
160 fprintf(stdout,
" currently big endian (SUN Sparc) byte order.\n");
164 if(orderTo<0)
continue;
167 if((dsr.
little==1 && orderTo==0) || (dsr.
little==0 && orderTo==1)) {
168 if(verbose>0) fprintf(stdout,
" no conversion was necessary.\n");
174 strcpy(dbname2, dbname);
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);
182 if(access(outpath, 0) == -1) {
183 if(verbose>0) fprintf(stdout,
" creating subdirectory %s\n", outpath);
186 ret=mkdir(outpath, 00775);
187#elif defined HAVE_MKDIR1
189#elif defined HAVE__MKDIR
193 fprintf(stderr,
" Error: cannot create subdirectory.\n");
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");
210 fprintf(stdout,
" %zu pixels, %d byte(s)/pixel\n", pxlNr, byteSize);
212 fprintf(stderr,
" Error: invalid image dimensions.\n");
216 fprintf(stderr,
" Error: unsupported bitpix.\n");
218 }
else if(byteSize<2) {
220 fprintf(stdout,
" no conversion is needed for image file.\n");
224 if(!outpath[0]) fprintf(stdout,
" converting %s\n", datfile);
225 else fprintf(stdout,
" converting %s to %s\n", datfile, datfile2);
228 if((fpi=fopen(datfile,
"rb")) == NULL) {
229 fprintf(stderr,
" Error: cannot read image file %s\n", datfile);
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;
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;}
246 fclose(fpi); fclose(fpo);
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;
256 if(!outpath[0]) fprintf(stdout,
" converting %s\n", hdrfile);
257 else fprintf(stdout,
" converting %s to %s\n", hdrfile, hdrfile2);
261 fprintf(stderr,
" Error: cannot write header file %s\n", hdrfile2);
262 if(byteSize>1) remove(temp);
270 printf(
" remove(%s); rename(%s, %s);\n", datfile2, temp, datfile2);
271 remove(datfile2); rename(temp, datfile2);
276 if(errorNr>0)
return(errorNr+10);
int anaWriteHeader(char *filename, ANALYZE_DSR *h)
int anaDatabaseExists(const char *dbname, char *hdrfile, char *imgfile, char *siffile)
void anaRemoveFNameExtension(char *fname)
int anaReadHeader(char *filename, ANALYZE_DSR *h)
Header file for libtpcimgio.
Header file for libtpcmisc.
int tpcProcessStdOptions(const char *s, int *print_usage, int *print_version, int *verbose_level)
size_t strlcpy(char *dst, const char *src, size_t dstsize)
int tpcHtmlUsage(const char *program, char *text[], const char *path)
void tpcPrintBuild(const char *program, FILE *fp)
void tpcPrintUsage(const char *program, char *text[], FILE *fp)
void swap(void *orig, void *new, int size)
ANALYZE_HEADER_IMGDIM dime