1 /*****************************************************************************
2 ** This is part of the CTSim program
3 ** Copyright (C) 1983-2000 Kevin Rosenberg
5 ** $Id: phm2sdf.c,v 1.1 2000/04/28 13:02:44 kevin Exp $
7 ** Revision 1.1 2000/04/28 13:02:44 kevin
11 ** This program is free software; you can redistribute it and/or modify
12 ** it under the terms of the GNU General Public License (version 2) as
13 ** published by the Free Software Foundation.
15 ** This program is distributed in the hope that it will be useful,
16 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ** GNU General Public License for more details.
20 ** You should have received a copy of the GNU General Public License
21 ** along with this program; if not, write to the Free Software
22 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 ******************************************************************************/
25 * phm2sdf.c Generate a SDF image from a phantom
28 * 1984 - Final DOS verion
29 * 1999 - First UNIX version
42 #define O_FILTER_DOMAIN 9
43 #define O_FILTER_BW 10
44 #define O_FILTER_PARAM 11
48 static struct option my_options[] =
50 {"phantom", 1, 0, O_PHANTOM},
51 {"picfile", 1, 0, O_PICFILE},
52 {"desc", 1, 0, O_DESC},
53 {"nsample", 1, 0, O_NSAMPLE},
54 {"filter", 1, 0, O_FILTER},
55 {"filter-domain", 1, 0, O_FILTER_DOMAIN},
56 {"filter-bw", 1, 0, O_FILTER_BW},
57 {"filter-param", 1, 0, O_FILTER_PARAM},
58 {"trace", 1, 0, O_TRACE},
59 {"verbose", 0, 0, O_VERBOSE},
60 {"debug", 0, 0, O_DEBUG},
61 {"help", 0, 0, O_HELP},
62 {"version", 0, 0, O_VERSION},
67 usage (const char *program)
69 fprintf(stdout,"usage: %s outfile nx ny [--phantom phantom-name] [--picfile filename] [--filter filter-name] [OPTIONS]\n",kbasename(program));
70 fprintf(stdout,"Generate phantom image from a predefined --phantom or a --picfile\n");
72 fprintf(stdout," outfile Name of output file for image\n");
73 fprintf(stdout," nx Number of pixels X-axis\n");
74 fprintf(stdout," ny Number of pixels Y-axis\n");
75 fprintf(stdout," --phantom Phantom to use for projection\n");
76 fprintf(stdout," herman Herman head phantom\n");
77 fprintf(stdout," rowland Rowland head phantom\n");
78 fprintf(stdout," browland Bordered Rowland head phantom\n");
79 fprintf(stdout," unitpulse Unit pulse phantom\n");
80 fprintf(stdout," --picfile Generate Phantom from picture file\n");
81 fprintf(stdout," --filter Generate Phantom from a filter function\n");
82 fprintf(stdout," abs_bandlimit Abs * Bandlimiting\n");
83 fprintf(stdout," abs_sinc Abs * Sinc\n");
84 fprintf(stdout," abs_cos Abs * Cosine\n");
85 fprintf(stdout," abs_hamming Abs * Hamming\n");
86 fprintf(stdout," shepp Shepp-Logan\n");
87 fprintf(stdout," bandlimit Bandlimiting\n");
88 fprintf(stdout," sinc Sinc\n");
89 fprintf(stdout," cos Cosine\n");
90 fprintf(stdout," triangle Triangle\n");
91 fprintf(stdout," hamming Hamming\n");
92 fprintf(stdout," --filter-param Alpha level for Hamming filter\n");
93 fprintf(stdout," --filter-domain Set domain of filter\n");
94 fprintf(stdout," spatial Spatial domain (default)\n");
95 fprintf(stdout," freq Frequency domain\n");
96 fprintf(stdout," --filter-bw Filter bandwidth (default = 1)\n");
97 fprintf(stdout," --desc Description of raysum\n");
98 fprintf(stdout," --nsample Number of samples per axis per pixel (default = 1)\n");
99 fprintf(stdout," --trace Trace level to use\n");
100 fprintf(stdout," none No tracing (default)\n");
101 fprintf(stdout," text Trace text level\n");
102 fprintf(stdout," pic Trace picture\n");
103 fprintf(stdout," rays Trace rays\n");
104 fprintf(stdout," plot Trace plot\n");
105 fprintf(stdout," clipping Trace clipping\n");
106 fprintf(stdout," --debug Debug mode\n");
107 fprintf(stdout," --verbose Verbose mode\n");
108 fprintf(stdout," --version Print version\n");
109 fprintf(stdout," --help Print this help message\n");
114 void mpi_gather_image (IMAGE *im_global, IMAGE *im_local, const int opt_debug);
118 main (const int argc, char *const argv[])
120 IMAGE *im_global = NULL;
122 int opt_nx = 0, opt_ny = 0;
126 int opt_filter_domain = D_SPATIAL;
130 char opt_desc[256], opt_picfilename[256];
131 char *endstr, *endptr;
132 double opt_filter_param;
133 double opt_filter_bw = 1.;
134 int opt_trace = TRACE_NONE;
136 double time_start, time_end;
138 IMAGE *im_local = NULL;
140 char **mpi_argv = (char **) argv;
141 double mpi_t1, mpi_t2, mpi_t, mpi_t_g;
143 MPI_Init(&mpi_argc, &mpi_argv);
144 MPI_Comm_dup (MPI_COMM_WORLD, &mpi_ct.comm);
145 MPI_Comm_size(mpi_ct.comm, &mpi_ct.nproc);
146 MPI_Comm_rank(mpi_ct.comm, &mpi_ct.my_rank);
148 if (mpi_ct.nproc > MPI_MAX_PROCESS) {
149 sys_error(ERR_FATAL, "Number of mpi processes (%d) exceeds max processes (%d)",
150 mpi_ct.nproc, MPI_MAX_PROCESS);
156 time_start = MPI_Wtime();
158 time_start = td_current_sec();
162 if (mpi_ct.my_rank == 0) {
165 strcpy(opt_desc, "");
166 strcpy(opt_picfilename, "");
168 int c = getopt_long(argc, argv, "", my_options, NULL);
177 opt_picnum = opt_set_picture(optarg, argv[0]);
180 strncpy(opt_picfilename, optarg, sizeof(opt_picfilename));
181 pic = create_pic_from_file(opt_picfilename);
183 if (mpi_ct.my_rank == 0)
184 fprintf(stderr, "Can't use picture from file in MPI mode\n");
195 opt_trace = opt_set_trace(optarg, argv[0]);
198 opt_filter = opt_set_filter(optarg, argv[0]);
200 case O_FILTER_DOMAIN:
201 opt_filter_domain = opt_set_filter_domain(optarg, argv[0]);
204 strncpy(opt_desc, optarg, sizeof(opt_desc));
207 opt_filter_bw = strtod(optarg, &endptr);
208 endstr = optarg + strlen(optarg);
209 if (endptr != endstr) {
210 fprintf(stderr,"Error setting --filter-bw to %s\n", optarg);
216 opt_filter_param = strtod(optarg, &endptr);
217 endstr = optarg + strlen(optarg);
218 if (endptr != endstr) {
219 fprintf(stderr,"Error setting --filter-param to %s\n", optarg);
225 opt_nsample = strtol(optarg, &endptr, 10);
226 endstr = optarg + strlen(optarg);
227 if (endptr != endstr) {
228 fprintf(stderr,"Error setting --nsample to %s\n", optarg);
235 fprintf(stdout, "Version %s\n", VERSION);
237 fprintf(stderr, "Unknown version number");
249 if (pic == NULL && opt_picnum == -1 && opt_filter == -1) {
250 fprintf(stderr, "No phantom defined\n");
255 if (opt_trace >= TRACE_PIC)
258 if (optind + 3 != argc) {
262 opt_outfile = argv[optind];
263 opt_nx = strtol(argv[optind+1], &endptr, 10);
264 endstr = argv[optind+1] + strlen(argv[optind+1]);
265 if (endptr != endstr) {
266 fprintf(stderr,"Error setting nx to %s\n", argv[optind+1]);
270 opt_ny = strtol(argv[optind+2], &endptr, 10);
271 endstr = argv[optind+2] + strlen(argv[optind+2]);
272 if (endptr != endstr) {
273 fprintf(stderr,"Error setting ny to %s\n", argv[optind+2]);
278 snprintf(str, sizeof(str), "nx=%d, ny=%d, nsample=%d, ", opt_nx, opt_ny, opt_nsample);
279 if (opt_picfilename[0]) {
280 strncat(str, "phantom=", sizeof(str));
281 strncat(str, opt_picfilename, sizeof(str));
283 else if (opt_picnum != -1) {
284 strncat(str, "phantom=", sizeof(str));
285 strncat(str, name_of_picture(opt_picnum), sizeof(str));
287 else if (opt_filter != -1) {
288 strncat(str, "filter=", sizeof(str));
289 strncat(str, name_of_filter(opt_filter), sizeof(str));
290 strncat(str, " - ", sizeof(str));
291 strncat(str, name_of_filter_domain(opt_filter_domain), sizeof(str));
294 strncat(str, ": ", sizeof(str));
295 strncat(str, opt_desc, sizeof(str));
297 strncpy(opt_desc, str, sizeof(opt_desc));
300 printf("Compile Phantom to Image\n\n");
306 mpi_t1 = MPI_Wtime();
307 MPI_Bcast(&opt_verbose, 1, MPI_INT, 0, mpi_ct.comm);
308 MPI_Bcast(&opt_debug, 1, MPI_INT, 0, mpi_ct.comm);
309 MPI_Bcast(&opt_trace, 1, MPI_INT, 0, mpi_ct.comm);
310 MPI_Bcast(&opt_nx, 1, MPI_INT, 0, mpi_ct.comm);
311 MPI_Bcast(&opt_ny, 1, MPI_INT, 0, mpi_ct.comm);
312 MPI_Bcast(&opt_nsample, 1, MPI_INT, 0, mpi_ct.comm);
313 MPI_Bcast(&opt_picnum, 1, MPI_INT, 0, mpi_ct.comm);
314 MPI_Bcast(&opt_filter, 1, MPI_INT, 0, mpi_ct.comm);
315 MPI_Bcast(&opt_filter_domain, 1, MPI_INT, 0, mpi_ct.comm);
316 MPI_Bcast(&opt_filter_param, 1, MPI_DOUBLE, 0, mpi_ct.comm);
317 MPI_Bcast(&opt_filter_bw, 1, MPI_DOUBLE, 0, mpi_ct.comm);
320 mpi_t2 = MPI_Wtime();
321 mpi_t = mpi_t2 - mpi_t1;
322 MPI_Reduce(&mpi_t, &mpi_t_g, 1, MPI_DOUBLE, MPI_MAX, 0, mpi_ct.comm);
323 if (mpi_ct.my_rank == 0)
324 printf("Time to Bcast vars = %f secs, Max time = %f\n", mpi_t, mpi_t_g);
327 mpi_ct_calc_work_units(opt_nx);
329 if (mpi_ct.my_rank == 0)
330 im_global = image_create (opt_outfile, opt_nx, opt_ny);
332 im_local = image_create(NULL, opt_nx, opt_ny);
334 im_global = image_create (opt_outfile, opt_nx, opt_ny);
338 pic = create_pic(opt_picnum);
341 if (mpi_ct.my_rank == 0)
342 fprintf(stderr, "picnum < 0\n");
348 if (pic->type == P_UNIT_PULSE) {
349 if (mpi_ct.my_rank == 0) {
350 im_global->v[opt_nx/2][opt_ny/2] = 1.;
351 im_global->calctime = 0;
353 } else if (opt_filter != -1) {
354 if (mpi_ct.my_rank == 0) {
355 image_filter_init (im_global, opt_filter_domain, opt_filter_bw, opt_filter, opt_filter_param, opt_trace);
356 im_global->calctime = 0;
360 mpi_t1 = MPI_Wtime();
361 pic_to_image (pic, im_local, mpi_ct.start_work_unit[mpi_ct.my_rank],
362 mpi_ct.local_work_units[mpi_ct.my_rank], opt_nsample, opt_trace);
364 mpi_t2 = MPI_Wtime();
365 mpi_t = mpi_t2 - mpi_t1;
366 MPI_Reduce(&mpi_t, &mpi_t_g, 1, MPI_DOUBLE, MPI_MAX, 0, mpi_ct.comm);
367 if (mpi_ct.my_rank == 0)
368 printf("Time to compile pic = %f secs, Max time = %f\n", mpi_t, mpi_t_g);
370 mpi_gather_image(im_global, im_local, opt_debug);
373 if (pic->type == P_UNIT_PULSE) {
374 im_global->v[opt_nx/2][opt_ny/2] = 1.;
375 im_global->calctime = 0;
376 } else if (opt_filter != -1) {
377 image_filter_init (im_global, opt_filter_domain, opt_filter_bw, opt_filter, opt_filter_param, opt_trace);
378 im_global->calctime = 0;
380 pic_to_image (pic, im_global, 0, opt_nx, opt_nsample, opt_trace);
385 time_end = MPI_Wtime();
386 if (mpi_ct.my_rank == 0) {
387 im_global->calctime = time_end - time_start;
388 strncpy (im_global->remark, opt_desc, sizeof(im_global->remark));
391 if (mpi_ct.my_rank == 0) {
392 image_save (im_global);
394 fprintf (stdout, "Time to compile image = %.2f sec\n\n", im_global->calctime);
397 time_end = td_current_sec();
398 im_global->calctime = time_end - time_start;
399 strncpy (im_global->remark, opt_desc, sizeof(im_global->remark));
400 image_save (im_global);
402 if (opt_trace >= TRACE_PIC)
404 crt_set_mode (GM_TEXT, TRUE);
409 fprintf (stdout, "Time to compile image = %.2f sec\n\n", im_global->calctime);
412 if (opt_trace >= TRACE_PIC)
417 printf ("Enter size of cell (nx, ny): ");
418 scanf ("%d %d", &nx, &ny);
419 printf ("Enter minimum and maximum densities (min, max): ");
420 scanf ("%lf %lf", &dmin, &dmax);
421 crt_set_mode (GM_HIGHRES, TRUE);
422 image_paint (CRTDEV, im_global, 0, 0, nx, ny, dmin, dmax, 0);
425 crt_set_mode (GM_TEXT, TRUE);
426 crt_set_palette (0, crt_calc_color (0., 0., 2.));
427 crt_put_stra ("Finished", 8 + C_WHITE);
437 void mpi_gather_image (IMAGE *im_global, IMAGE *im_local, const int opt_debug)
443 end_work_unit = mpi_ct.local_work_units[mpi_ct.my_rank] - 1;
444 for (iw = 0; iw <= end_work_unit; iw++) {
445 MPI_Send(im_local->v[iw], im_local->ny, MPI_FLOAT, 0, 0, mpi_ct.comm);
448 if (mpi_ct.my_rank == 0) {
449 for (iproc = 0; iproc < mpi_ct.nproc; iproc++) {
450 end_work_unit = mpi_ct.start_work_unit[iproc]
451 + mpi_ct.local_work_units[iproc]
455 fprintf(stdout, "Recv rs data from process %d (%d-%d)\n", iproc,
456 mpi_ct.start_work_unit[iproc], end_work_unit);
461 for (iw = mpi_ct.start_work_unit[iproc]; iw <= end_work_unit; iw++) {
464 MPI_Recv(im_global->v[iw], im_global->ny, MPI_FLOAT, iproc, 0, mpi_ct.comm, &status);