9#include "tpcclibConfig.h"
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.",
28 "Usage: @P [Options] database(s)>",
32 " Change byte order to little endian (PC Intel).",
34 " Change byte order to big endian (Sun Sparc, Motorola, PowerPC).",
36 " Converted files are placed in specified directory, and original",
37 " files are not overwritten.",
40 "See also: ana2ecat, ecat2ana, ana_lhdr, ana_ehdr, convend",
42 "Keywords: image, format conversion, Analyze, byte order",
61int main(
int argc,
char **argv)
63 int ai, help=0, version=0, verbose=1;
65 unsigned int byteSize;
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];
72 unsigned char buf[32];
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;
83 for(ai=1; ai<argc; ai++)
if(*argv[ai]==
'-') {
84 cptr=argv[ai]+1;
if(*cptr==
'-') cptr++;
if(cptr==NULL)
continue;
86 if(strncasecmp(cptr,
"LITTLE", 1)==0) {
88 }
else if(strncasecmp(cptr,
"BIG", 1)==0) {
90 }
else if(strncasecmp(cptr,
"O=", 2)==0) {
91 cptr+=2;
if(strlen(cptr)>0) {strcpy(outpath, cptr);
continue;}
93 fprintf(stderr,
"Error: invalid option '%s'\n", argv[ai]);
98 if(help==2) {
tpcHtmlUsage(argv[0], info,
"");
return(0);}
103 for(; ai<argc; ai++) {
106 if(
anaDatabaseExists(argv[ai], hdrfile, datfile, NULL) > 0) {fileNr++;
continue;}
107 fprintf(stderr,
"Error: Analyze file '%s' does not exist.\n", argv[ai]);
113 fprintf(stderr,
"Error: no Analyze files were specified.\n");
119 for(ai=0; ai<argc; ai++) printf(
" %s", argv[ai]);
121 printf(
"orderTo := %d\n", orderTo);
122 printf(
"outpath := %s\n", outpath);
123 printf(
"fileNr := %d\n", fileNr);
131 if(verbose>1) printf(
"processing %d Analyze file(s)\n", fileNr);
132 for(ai=ffi; ai<argc; ai++) {
135 strlcpy(dbname, argv[ai], FILENAME_MAX);
138 if(verbose>0) fprintf(stdout,
"%s : \n", dbname);
142 fprintf(stderr,
" Error: Analyze file '%s' does not exist.\n", dbname);
148 fprintf(stderr,
" Error: cannot read header file %s\n", hdrfile);
153 if(verbose>0 || orderTo<0) {
155 fprintf(stdout,
" currently little endian (Intel) byte order.\n");
157 fprintf(stdout,
" currently big endian (SUN Sparc) byte order.\n");
161 if(orderTo<0)
continue;
164 if((dsr.
little==1 && orderTo==0) || (dsr.
little==0 && orderTo==1)) {
165 if(verbose>0) fprintf(stdout,
" no conversion was necessary.\n");
171 strcpy(dbname2, dbname);
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);
179 if(access(outpath, 0) == -1) {
180 if(verbose>0) fprintf(stdout,
" creating subdirectory %s\n", outpath);
182 int ret=mkdir(outpath);
184 int ret=mkdir(outpath, 00775);
187 fprintf(stderr,
" Error: cannot create subdirectory.\n");
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");
204 fprintf(stdout,
" %zu pixels, %d byte(s)/pixel\n", pxlNr, byteSize);
206 fprintf(stderr,
" Error: invalid image dimensions.\n");
210 fprintf(stderr,
" Error: unsupported bitpix.\n");
212 }
else if(byteSize<2) {
214 fprintf(stdout,
" no conversion is needed for image file.\n");
218 if(!outpath[0]) fprintf(stdout,
" converting %s\n", datfile);
219 else fprintf(stdout,
" converting %s to %s\n", datfile, datfile2);
222 if((fpi=fopen(datfile,
"rb")) == NULL) {
223 fprintf(stderr,
" Error: cannot read image file %s\n", datfile);
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;
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;}
240 fclose(fpi); fclose(fpo);
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;
250 if(!outpath[0]) fprintf(stdout,
" converting %s\n", hdrfile);
251 else fprintf(stdout,
" converting %s to %s\n", hdrfile, hdrfile2);
255 fprintf(stderr,
" Error: cannot write header file %s\n", hdrfile2);
256 if(byteSize>1) remove(temp);
264 printf(
" remove(%s); rename(%s, %s);\n", datfile2, temp, datfile2);
265 remove(datfile2); rename(temp, datfile2);
270 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