TPCCLIB
Loading...
Searching...
No Matches
e63mdel.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 <unistd.h>
13#include <math.h>
14#include <string.h>
15#include <time.h>
16/*****************************************************************************/
17#include "libtpcmisc.h"
18#include "libtpcimgio.h"
19/*****************************************************************************/
20
21/*****************************************************************************/
22static char *info[] = {
23 "Delete specified image matrix (frames and plane) in ECAT 6.3 file.",
24 "Note! Missing matrices usually prevent the usage of the image in most software.",
25 " ",
26 "Usage: @P [Options] imgfile frame plane outputfile ",
27 " ",
28 "Options:",
29 " -stdoptions", // List standard options like --help, -v, etc
30 " ",
31 "Example:",
32 " @P a2345dy1.img 20 10 a2345dy1_partial.img ",
33 " ",
34 "See also: e63mreg, esplit, imgdelfr, lmlist, eframe, efixplnr, imgadd",
35 " ",
36 "Keywords: ECAT, matrixlist, cropping, software testing",
37 0};
38/*****************************************************************************/
39
40/*****************************************************************************/
41/* Turn on the globbing of the command line, since it is disabled by default in
42 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
43 In Unix&Linux wildcard command line processing is enabled by default. */
44/*
45#undef _CRT_glob
46#define _CRT_glob -1
47*/
48int _dowildcard = -1;
49/*****************************************************************************/
50
51/*****************************************************************************/
55int main(int argc, char **argv)
56{
57 int ai, help=0, version=0, verbose=1;
58 int ret;
59 int plane=0, frame=0;
60 char ecatfile[FILENAME_MAX], outfile[FILENAME_MAX];
61 ECAT63_mainheader mainheader63;
62 MATRIXLIST mlist63;
63
64
65 /*
66 * Get arguments
67 */
68 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
69 ecatfile[0]=outfile[0]=(char)0;
70 ecat63InitMatlist(&mlist63);
71 /* Options */
72 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') { /* options */
73 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
74 //char *cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(cptr==NULL) continue;
75 fprintf(stderr, "Error: invalid option '%s'.\n", argv[ai]);
76 return(1);
77 } else break;
78
79 /* Print help or version? */
80 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
81 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
82 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
83
84 /* Process other arguments, starting from the first non-option */
85 if(ai<argc) {strlcpy(ecatfile, argv[ai++], FILENAME_MAX);}
86 if(ai<argc) {
87 if(atoi_with_check(argv[ai++], &frame) || frame<1) {
88 fprintf(stderr, "Error: invalid frame number.\n"); return(1);}
89 }
90 if(ai<argc) {
91 if(atoi_with_check(argv[ai++], &plane) || plane<1) {
92 fprintf(stderr, "Error: invalid plane number.\n"); return(1);}
93 }
94 if(ai<argc) {strlcpy(outfile, argv[ai++], FILENAME_MAX);}
95 if(ai<argc) {
96 fprintf(stderr, "Error: extra argument '%s'.\n", argv[ai]);
97 return(1);
98 }
99
100 /* Is something missing? */
101 if(!outfile[0]) {
102 fprintf(stderr, "Error: missing command-line argument; try %s --help\n", argv[0]);
103 return(1);
104 }
105
106 /* Check output filename */
107 if(strcasecmp(ecatfile, outfile)==0) {
108 fprintf(stderr, "Error: same name for input and output file.\n");
109 return(1);
110 }
111
112 /* In verbose mode print arguments and options */
113 if(verbose>1) {
114 printf("ecatfile := %s\n", ecatfile);
115 printf("outputfile := %s\n", outfile);
116 printf("frame := %d\n", frame);
117 printf("plane := %d\n", plane);
118 }
119
120
121 /*
122 * Read main header and matrix list
123 */
124 if(verbose>1) {fprintf(stdout, "reading %s\n", ecatfile); fflush(stdout);}
125
126 /* Open file for read */
127 FILE *fp;
128 if((fp=fopen(ecatfile, "rb")) == NULL) {
129 fprintf(stderr, "Error: cannot read file %s\n", ecatfile);
130 return(2);
131 }
132
133 /* Try to read ECAT 6.3 main header */
134 if(verbose>2) {fprintf(stdout, "reading main header\n"); fflush(stdout);}
135 ret=ecat63ReadMainheader(fp, &mainheader63);
136 if(ret) {
137 fprintf(stderr, "Error: cannot read main header.\n");
138 if(verbose>1) printf(" ret := %d\n", ret);
139 fclose(fp); return(2);
140 }
141
142 /* Read matrix list */
143 ret=ecat63ReadMatlist(fp, &mlist63, verbose-1);
144 if(ret!=0) {
145 fprintf(stderr, "Error: cannot read matrix list.\n");
146 if(verbose>1) printf(" ret := %d\n", ret);
147 fclose(fp); return(2);
148 }
149 if(mlist63.matrixNr<1) {
150 fprintf(stderr, "Error: matrix list is empty.\n");
151 fclose(fp); return(2);
152 }
153 ret=ecat63CheckMatlist(&mlist63);
154 if(ret!=0) {
155 fprintf(stderr, "Warning: matrix list fails testing (%d).\n", ret);
156 if(verbose>2) ecat63PrintMatlist(&mlist63);
157 } else {
158 if(verbose>100) ecat63PrintMatlist(&mlist63);
159 }
160
161
162 /*
163 * Mark listed matrix as deleted
164 */
165 int delNr=0, planeNr=0, frameNr=0;
166 Matval matval63;
167 for(int m=0; m<mlist63.matrixNr; m++) {
168 mat_numdoc(mlist63.matdir[m].matnum, &matval63);
169 if(matval63.plane==plane && matval63.frame==frame) {
170 mlist63.matdir[m].matstat=-1;
171 delNr++;
172 continue;
173 }
174 if(matval63.plane>planeNr) planeNr=matval63.plane;
175 if(matval63.frame>frameNr) frameNr=matval63.frame;
176 }
177 if(delNr==0) {
178 fprintf(stderr, "Error: specified matrix was not found.\n");
179 ecat63EmptyMatlist(&mlist63);
180 fclose(fp); return(3);
181 }
182 if(delNr==mlist63.matrixNr) {
183 fprintf(stderr, "Error: no matrices left after deleting this matrix.\n");
184 ecat63EmptyMatlist(&mlist63);
185 fclose(fp); return(4);
186 }
187 /* Fix main header contents accordingly */
188 if(mainheader63.num_planes>planeNr) mainheader63.num_planes=planeNr;
189 if(mainheader63.num_frames>frameNr) mainheader63.num_frames=frameNr;
190
191
192 /*
193 * Write output file
194 */
195 /* Check if output file exists; rename to backup file, if necessary */
196 //backupExistingFile(outfile, NULL, tmp);
197
198 /* Open output file */
199 FILE *fp2;
200 fp2=ecat63Create(outfile, &mainheader63);
201 if(fp2==NULL) {
202 fprintf(stderr, "Error: cannot write ECAT file.\n");
203 ecat63EmptyMatlist(&mlist63); fclose(fp); return(11);
204 }
205 /* Copy the matrices */
206 int blkNr, nxtblk;
207 char buf[MatBLKSIZE];
208 for(int m=0; m<mlist63.matrixNr; m++) if(mlist63.matdir[m].matstat==1) {
209 blkNr=1+mlist63.matdir[m].endblk-mlist63.matdir[m].strtblk;
210 /* Get block number for matrix header and data */
211 nxtblk=ecat63Matenter(fp2, mlist63.matdir[m].matnum, blkNr-1);
212 if(nxtblk<1) {
213 fprintf(stderr, "Error: cannot write ECAT matrix.\n");
214 ecat63EmptyMatlist(&mlist63); fclose(fp); fclose(fp2); remove(outfile); return(13);
215 }
216 if(verbose>3) printf(" m=%d blkNr=%d nxtblk=%d\n", m, blkNr, nxtblk);
217 /* Copy each block */
218 for(long long bi=mlist63.matdir[m].strtblk; bi<=mlist63.matdir[m].endblk; bi++) {
219 /* Read block */
220 fseeko(fp, (bi-1)*MatBLKSIZE, SEEK_SET);
221 if(ftello(fp)!=(bi-1)*MatBLKSIZE) {
222 fprintf(stderr, "Error: cannot find matrix block %lld.\n", bi);
223 ecat63EmptyMatlist(&mlist63); fclose(fp); fclose(fp2); remove(outfile); return(8);
224 }
225 if(fread(buf, MatBLKSIZE, 1, fp)<1) {
226 fprintf(stderr, "Error: cannot read matrix block %lld.\n", bi);
227 ecat63EmptyMatlist(&mlist63); fclose(fp); fclose(fp2); remove(outfile); return(9);
228 }
229 /* Write block */
230 fseeko(fp2, (long long)(nxtblk-1)*MatBLKSIZE, SEEK_SET);
231 if(ftello(fp2)!=(long long)(nxtblk-1)*MatBLKSIZE) {
232 fprintf(stderr, "Error: cannot find matrix block %d for write.\n", nxtblk);
233 ecat63EmptyMatlist(&mlist63); fclose(fp); fclose(fp2); remove(outfile); return(15);
234 }
235 if(fwrite(buf, 1, MatBLKSIZE, fp2)<1) {
236 fprintf(stderr, "Error: cannot write matrix block %d.\n", nxtblk);
237 ecat63EmptyMatlist(&mlist63); fclose(fp); fclose(fp2); remove(outfile); return(16);
238 }
239 nxtblk++;
240 }
241 }
242
243 /* Close files */
244 fclose(fp); fclose(fp2);
245 ecat63EmptyMatlist(&mlist63);
246
247 if(verbose>0) fprintf(stdout, "done.\n");
248
249 return(0);
250}
251/*****************************************************************************/
252
253/*****************************************************************************/
int atoi_with_check(const char *int_as_string, int *result_value)
Definition decpoint.c:238
int ecat63Matenter(FILE *fp, int matnum, int blkNr)
Definition ecat63ml.c:159
int ecat63ReadMatlist(FILE *fp, MATRIXLIST *ml, int verbose)
Definition ecat63ml.c:46
void ecat63InitMatlist(MATRIXLIST *mlist)
Definition ecat63ml.c:20
void ecat63EmptyMatlist(MATRIXLIST *mlist)
Definition ecat63ml.c:31
void ecat63PrintMatlist(MATRIXLIST *ml)
Definition ecat63ml.c:130
int ecat63CheckMatlist(MATRIXLIST *ml)
Definition ecat63ml.c:324
void mat_numdoc(int matnum, Matval *matval)
Definition ecat63ml.c:254
int ecat63ReadMainheader(FILE *fp, ECAT63_mainheader *h)
Definition ecat63r.c:25
FILE * ecat63Create(const char *fname, ECAT63_mainheader *h)
Definition ecat63w.c:365
Header file for libtpcimgio.
#define MatBLKSIZE
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
MatDir * matdir
int matstat
int endblk
int matnum
int strtblk
int frame
int plane