2 ***********************************************************
3 $SNARK_Header: S N A R K 1 4 - A PICTURE RECONSTRUCTION PROGRAM $
4 $HeadURL: svn://dig.cs.gc.cuny.edu/snark/trunk/src/snark/halft.cpp $
5 $LastChangedRevision: 118 $
6 $Date: 2014-07-09 14:22:29 -0400 (Wed, 09 Jul 2014) $
8 ***********************************************************
10 POST-PROCESSING ROUTINE TO GENERATE PSEUDO-HALF-TONE DISPLAYS OF
11 RECONSTRUCTIONS ON THE LINE PRINTER.
30 void strtrim(char* out, const char* in)
32 unsigned int first = 0;
33 //find first non white from the left
34 while ((in[first] == ' ') || (in[first] == '\t'))
35 { // removed third term of the condition. lajos, Nov 18, 2004
39 unsigned int last = strlen(in) - 1;
40 //find first non white from the right
43 if ((in[last] != ' ') && (in[last] != '\t'))
52 for (i = 0; i < last - first + 1; i++)
54 out[i] = in[first + i];
60 int SavePGM(char* filename, unsigned char* array, int xdim, int ydim,
65 if ((fpout = fopen(filename, "w")) == NULL)
71 fprintf(fpout, "P5\n");
72 fprintf(fpout, "#%s\n", coment);
73 fprintf(fpout, "%d %d\n", xdim, ydim);
74 fprintf(fpout, "255\n");
75 fwrite(array, xdim * ydim, sizeof(unsigned char), fpout);
99 // allocate space for image buffer
100 unsigned char* array = new unsigned char[nrow * mcol];
101 // changed "CHAR" to "char". lajos Nov 17, 2004
105 fprintf(output, "\n **** memory allocation error");
106 fprintf(output, "\n **** program aborted\n");
110 // FORM INTENSITY VALUES MEAN AND DIFFERENCE ARRAYS
118 fprintf(output, "\n intensity levels printed\n");
123 fprintf(output, "\n amplitude levels printed\n");
126 range = (REAL) 1.0 / range;
128 fprintf(output, "\n columns %5i to %5i being printed\n", 1, mcol);
130 for (i = 0; i < nrow * mcol; i++)
133 // AMPLITUDE OR INTENSITY
135 ot = MAX0((REAL) 0.0, MIN0((REAL) 1.0, ((x[i] - amin) * range)));
138 ot = (REAL) sqrt(ot);
140 array[i] = (unsigned char) (255 * ot);
141 // changed "(CHAR)" to "(unsigned char)". lajos Nov 18, 2004
145 SavePGM(filename, array, mcol, nrow, coment);
147 delete[] array; // bug 92 - Lajos - 03/02/2005
154 // was in post but it is realy local
159 // was in geo but it is realy local
164 // bug 124 - added minimum and maximum - swr - 7/6/05
165 static const INTEGER half_codes[] =
167 CHAR2INT('p', 'h', 'a', 'n'), CHAR2INT('d', 'i', 'f', 'f'),
168 CHAR2INT('m', 'i', 'n', 'i'), CHAR2INT('m', 'a', 'x', 'i'),
169 CHAR2INT('a', 'm', 'p', 'l'), CHAR2INT('i', 'n', 't', 'e') };
184 unsigned INTEGER count;
185 unsigned INTEGER iter;
202 // bug 124 - rewrote input line processing - swr - 7/6/05
204 int length = sizeof(half_codes) / sizeof(half_codes[0]);
205 word = InFile.getwrd(FALSE, &eol, &half_codes[pos], length - pos);
210 case CHAR2INT('p', 'h', 'a', 'n'):
214 case CHAR2INT('d', 'i', 'f', 'f'):
218 case CHAR2INT('m', 'i', 'n', 'i'):
220 amin = InFile.getnum(FALSE, &eol);
223 case CHAR2INT('m', 'a', 'x', 'i'):
225 amax = InFile.getnum(FALSE, &eol);
228 case CHAR2INT('a', 'm', 'p', 'l'):
231 case CHAR2INT('i', 'n', 't', 'e'):
237 word = InFile.getwrd(FALSE, &eol, &half_codes[pos], length - pos);
240 if (RecFile.Open("recfil") != 0)
242 fprintf(output, "\n **** unable to open recfil");
243 fprintf(output, "\n **** SKUNK execution aborted\n");
247 if (RecFile.GetNelem(&nelem) != 0)
252 if (RecFile.GetProjName(prjnam) != 0)
257 area = nelem * nelem;
261 // if phantom or differential read phantom
262 if (phan || (type != 0))
265 Phantom = new REAL[area];
267 if (RecFile.ReadPhan(phnnam, Phantom) != 0)
269 fprintf(output, "\n **** phantom not present");
270 fprintf(output, "\n **** SKUNK execution aborted\n");
271 delete[] Phantom; // bug 92 - Lajos - 03/02/2005
278 fprintf(output, "\n skunk display of phantom\n");
280 fprintf(output, "\n phantom name: %s", phnnam);
282 // bug 124 - restored flag checks - swr - 7/5/05
287 for (i = 1; i < area; i++)
289 if (Phantom[i] < amin)
298 for (i = 1; i < area; i++)
300 if (Phantom[i] > amax)
308 "\n **** maximal density is less or equal to minimal density");
309 fprintf(output, "\n **** SKUNK execution aborted\n");
310 delete[] Phantom; // bug 92 - Lajos - 03/02/2005
315 "This is phantom was created by snark14: Phantom Name %s",
318 char PhantomName[41];
320 strncpy(PhantomName, phnnam, 40);
321 PhantomName[40] = 0; // terminate string
323 char PhantomName2[41];
326 strtrim(PhantomName2, PhantomName);
328 sprintf(FileName, "%s.pgm", PhantomName2);
330 // bug 169 - swr - 9/25/05
331 for (i = 0; i < 100; ++i)
333 if (FileName[i] == ' ')
337 SaveImage(Phantom, amax, amin, nelem, nelem, nran, mp, FileName,
341 Recon = new REAL[area];
343 while (RecFile.ReadRec(recnam, algn, &count, &iter, Recon) == 0)
346 // continue if reconstruction not selected
347 if (flag[iter - 1] == 0)
355 "\n skunk display of reconstruction using %s at iter %4i\n",
361 "\n skunk difference picture using %s at iter %4i\n",
364 for (i = 0; i < area; i++)
366 Recon[i] = (REAL) fabs(Phantom[i] - Recon[i]);
370 fprintf(output, "\n projection data: %s", prjnam);
372 fprintf(output, "\n execution name: %s", recnam);
374 // bug 124 - restored flag checks - swr - 7/5/05
379 for (i = 1; i < area; i++)
390 for (i = 1; i < area; i++)
400 "\n **** maximal density is less or equal to minimal density");
401 fprintf(output, "\n **** SKUNK execution aborted\n");
405 char ProjectionName[13];
407 strncpy(ProjectionName, prjnam, 12);
408 ProjectionName[12] = 0; // terminate string
410 char ProjectionName2[13];
413 strtrim(ProjectionName2, ProjectionName);
415 char ReconstructionName[13];
417 strncpy(ReconstructionName, recnam, 12);
418 ReconstructionName[12] = 0; // terminate string
420 char ReconstructionName2[13];
423 strtrim(ReconstructionName2, ReconstructionName);
425 sprintf(FileName, "%s_%s_%4s_%04u_%c_%c.pgm", ProjectionName2,
426 ReconstructionName2, algn, count, (type != 0) ? 'd' : 'r',
427 (mp == 2) ? 'i' : 'a');
429 "This is reconstruction was created by snark14: Algorithm Name %4s Iteration %d",
432 // bug 169 - swr - 9/25/05
433 for (i = 0; i < 100; ++i)
435 if (FileName[i] == ' ')
439 SaveImage(Recon, amax, amin, nelem, nelem, nran, mp, FileName, coment);
442 delete[] Recon; // bug 92 - Lajos - 03/02/2005
444 if (phan || (type != 0))
446 delete[] Phantom; // bug 92 - Lajos - 03/02/2005
451 fprintf(output, "\n");