r142: *** empty log message ***
[ctsim.git] / libctsim / imagefile.cpp
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **      Name:         imagefile.cpp
5 **      Purpose:      Imagefile classes
6 **      Programmer:   Kevin Rosenberg
7 **      Date Started: June 2000
8 **
9 **  This is part of the CTSim program
10 **  Copyright (C) 1983-2000 Kevin Rosenberg
11 **
12 **  $Id: imagefile.cpp,v 1.7 2000/07/11 10:32:44 kevin Exp $
13 **
14 **  This program is free software; you can redistribute it and/or modify
15 **  it under the terms of the GNU General Public License (version 2) as
16 **  published by the Free Software Foundation.
17 **
18 **  This program is distributed in the hope that it will be useful,
19 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
20 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 **  GNU General Public License for more details.
22 **
23 **  You should have received a copy of the GNU General Public License
24 **  along with this program; if not, write to the Free Software
25 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26 ******************************************************************************/
27
28 #include "ct.h"
29
30
31
32 void 
33 ImageFile::filterResponse (const char* const domainName, double bw, const char* const filterName, double filt_param)
34 {
35   int hx = (m_nx - 1) / 2;
36   int hy = (m_ny - 1) / 2;
37   ImageFileArray v = getArray();
38   SignalFilter filter (filterName, domainName, bw, filt_param);
39
40   for (int i = -hx; i <= hx; i++) {
41     for (int j = -hy; j <= hy; j++) {
42       double r = sqrt (i * i + j * j);
43       
44       v[i+hx][j+hy] = filter.response (r);
45     }
46   }
47 }
48
49 int
50 ImageFile::display (void)
51 {
52     double pmin, pmax;
53
54     //    getPixelValueRange (pmin, pmax);
55
56     return (displayScaling (1, pmin, pmax));
57 }
58
59 int 
60 ImageFile::displayScaling (const int scale, const ImageFileValue pmin, const ImageFileValue pmax)
61 {
62     int grayscale[256];
63     int nx = m_nx;
64     int ny = m_ny;
65     ImageFileArray v = getArray();
66
67 #if HAVE_G2_H
68     int pens [nx * ny * scale * scale ];
69
70     double view_scale = 255 / (pmax - pmin);
71     int id_X11 = g2_open_X11 (nx * scale, ny * scale);
72
73     for (int i = 0; i < 256; i++) {
74         double cval = i / 255.;
75         grayscale[i] = g2_ink (id_X11, cval, cval, cval);
76     }
77
78     for (int i= 0, iy = ny - 1; iy >= 0; iy--) {
79         for (int ix = 0; ix < nx; ix++) {
80             int cval = static_cast<int>((v[ix][iy] - pmin) * view_scale);
81             if (cval < 0)  
82               cval = 0;
83             else if (cval > 255) 
84               cval = 255;
85             pens[i++] = grayscale[cval];
86         }
87     }
88
89     g2_image (id_X11, 0., 0., nx, ny, pens);
90
91     return (id_X11);
92
93 #endif
94 }
95
96
97
98 // ImageFile::comparativeStatistics    Calculate comparative stats
99 //
100 // OUTPUT
101 //   d   Normalized root mean squared distance measure
102 //   r   Normalized mean absolute distance measure
103 //   e   Worst case distance measure
104 //
105 // REFERENCES
106 //  G.T. Herman, Image Reconstruction From Projections, 1980
107
108 bool
109 ImageFile::comparativeStatistics (const ImageFile& imComp, double& d, double& r, double& e) const
110 {
111     if (imComp.nx() != m_nx && imComp.ny() != m_ny) {
112         sys_error (ERR_WARNING, "Image sizes differ [ImageFile::comparativeStatistics]");
113         return false;
114     }
115     ImageFileArrayConst v = getArray();
116     ImageFileArrayConst vComp = imComp.getArray();
117
118     double myMean = 0.;
119     for (int ix = 0; ix < m_nx; ix++) {
120         for (int iy = 0; iy < m_ny; iy++) {
121             myMean += v[ix][iy];
122         }
123     }
124     myMean /= (m_nx * m_ny);
125
126     double sqErrorSum = 0.;
127     double absErrorSum = 0.;
128     double sqDiffFromMeanSum = 0.;
129     double absValueSum = 0.;
130     for (int ix = 0; ix < m_nx; ix++) {
131         for (int iy = 0; iy < m_ny; iy++) {
132             double diff = v[ix][iy] - vComp[ix][iy];
133             sqErrorSum += diff * diff;
134             absErrorSum += fabs(diff);
135             double diffFromMean = v[ix][iy] - myMean;
136             sqDiffFromMeanSum += diffFromMean * diffFromMean;
137             absValueSum += fabs(v[ix][iy]);
138         }
139     }
140
141     d = sqrt (sqErrorSum / sqDiffFromMeanSum);
142     r = absErrorSum / absValueSum;
143
144     int hx = m_nx / 2;
145     int hy = m_ny / 2;
146     double eMax = -1;
147     for (int ix = 0; ix < hx; ix++) {
148       for (int iy = 0; iy < hy; iy++) {
149         double avgPixel = 0.25 * (v[2*ix][2*iy] + v[2*ix+1][2*iy] + v[2*ix][2*iy+1] + v[2*ix+1][2*iy+1]);
150         double avgPixelComp = 0.25 * (vComp[2*ix][2*iy] + vComp[2*ix+1][2*iy] + vComp[2*ix][2*iy+1] + vComp[2*ix+1][2*iy+1]);
151         double error = fabs (avgPixel - avgPixelComp);
152         if (error > eMax)
153           eMax = error;
154       }
155     }
156
157     e = eMax;
158
159     return true;
160 }
161
162
163 bool
164 ImageFile::printComparativeStatistics (const ImageFile& imComp, ostream& os) const
165 {
166   double d, r, e;
167
168   if (comparativeStatistics (imComp, d, r, e)) {
169     os << "  Normalized root mean squared distance (d): " << d << endl;
170     os << "      Normalized mean absolute distance (r): " << r << endl;
171     os << "Worst case distance (2x2 pixel average) (e): " << e << endl;
172   }
173 }
174
175
176 void
177 ImageFile::printStatistics (ostream& os) const
178 {
179     double min, max, mean, mode, median, stddev;
180
181     statistics (min, max, mean, mode, median, stddev);
182
183     os << "   min: " << min << endl;
184     os << "   max: " << max << endl;
185     os << "  mean: " << mean << endl;
186     os << "  mode: " << mode << endl;
187     os << "median: " << median << endl;
188     os << "stddev: " << stddev << endl;
189 }
190
191
192 void
193 ImageFile::statistics (double& min, double& max, double& mean, double& mode, double& median, double& stddev) const
194 {
195     int nx = m_nx;
196     int ny = m_ny;
197     ImageFileArrayConst v = getArray();
198     
199     mean = 0;
200     min = v[0][0];
201     max = v[0][0];
202     for (int ix = 0; ix < nx; ix++) {
203       for (int iy = 0; iy < ny; iy++) {
204         if (v[ix][iy] > max)
205           max = v[ix][iy];
206         if (v[ix][iy] < min)
207           min = v[ix][iy];
208         mean += v[ix][iy];
209       }
210     }
211     mean /= (nx * ny);
212
213     static const int nbin = 256;
214     int hist[ nbin ] = {0};
215     double spread = max - min;
216     mode = 0;
217     stddev = 0;
218     for (int ix = 0; ix < nx; ix++) {
219       for (int iy = 0; iy < ny; iy++) {
220         int b = static_cast<int>((((v[ix][iy] - min) / spread) * (nbin - 1)) + 0.5);
221         hist[b]++;
222         double diff = (v[ix][iy] - mean);
223         stddev += diff * diff;
224         }
225     }
226     stddev = sqrt(stddev / (nx * ny));
227
228     int max_binindex = 0;
229     int max_bin = -1;
230     for (int ibin = 0; ibin < nbin; ibin++) {
231       if (hist[ibin] > max_bin) {
232         max_bin = hist[ibin];
233         max_binindex = ibin;
234       }
235     }
236
237     mode = (max_binindex * spread / (nbin - 1)) + min;
238       
239     median = 0.;
240 }
241
242
243 void 
244 ImageFile::writeImagePGM (const char *outfile, int nxcell, int nycell, double densmin, double densmax)
245 {
246   FILE *fp;
247   int nx = m_nx;
248   int ny = m_ny;
249   ImageFileArray v = getArray();
250
251   unsigned char rowp [nx * nxcell];
252
253   if ((fp = fopen (outfile, "wb")) == NULL)
254      return;
255
256   fprintf(fp, "P5\n");
257   fprintf(fp, "%d %d\n", nx, ny);
258   fprintf(fp, "255\n");
259
260   for (int irow = ny - 1; irow >= 0; irow--) {
261     for (int icol = 0; icol < nx; icol++) {
262       int pos = icol * nxcell;
263       double dens = (v[icol][irow] - densmin) / (densmax - densmin);
264       dens = clamp (dens, 0., 1.);
265       for (int p = pos; p < pos + nxcell; p++) {
266         rowp[p] = static_cast<unsigned int> (dens * 255.);
267       }
268     }
269     for (int ir = 0; ir < nycell; ir++) {
270       for (int ic = 0; ic < nx * nxcell; ic++) 
271         fprintf(fp, "%c ", rowp[ic]);
272     }
273   }
274
275   fclose(fp);
276 }
277
278 void 
279 ImageFile::writeImagePGMASCII (const char *outfile, int nxcell, int nycell, double densmin, double densmax)
280 {
281   FILE *fp;
282   int nx = m_nx;
283   int ny = m_ny;
284   ImageFileArray v = getArray();
285
286   unsigned char rowp [nx * nxcell];
287   if (rowp == NULL)
288     return;
289
290   if ((fp = fopen (outfile, "wb")) == NULL)
291      return;
292
293   fprintf(fp, "P2\n");
294   fprintf(fp, "%d %d\n", nx, ny);
295   fprintf(fp, "255\n");
296
297   for (int irow = ny - 1; irow >= 0; irow--) {
298     for (int icol = 0; icol < nx; icol++) {
299       int pos = icol * nxcell;
300       double dens = (v[icol][irow] - densmin) / (densmax - densmin);
301       dens = clamp (dens, 0., 1.);
302       for (int p = pos; p < pos + nxcell; p++) {
303         rowp[p] = static_cast<unsigned int> (dens * 255.);
304       }
305     }
306     for (int ir = 0; ir < nycell; ir++) {
307       for (int ic = 0; ic < nx * nxcell; ic++) 
308         fprintf(fp, "%d ", rowp[ic]);
309       fprintf(fp, "\n");
310     }
311   }
312
313   fclose(fp);
314 }
315
316
317 #ifdef HAVE_PNG
318 void 
319 ImageFile::writeImagePNG (const char *outfile, int bitdepth, int nxcell, int nycell, double densmin, double densmax)
320 {
321   FILE *fp;
322   png_structp png_ptr;
323   png_infop info_ptr;
324   double max_out_level = (1 << bitdepth) - 1;
325   int nx = m_nx;
326   int ny = m_ny;
327   ImageFileArray v = getArray();
328
329   unsigned char rowp [nx * nxcell * (bitdepth / 8)];
330
331   if ((fp = fopen (outfile, "wb")) == NULL)
332      return;
333
334   png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
335   if (! png_ptr)
336     return;
337
338   info_ptr = png_create_info_struct(png_ptr);
339   if (! info_ptr) {
340     png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
341     fclose(fp);
342     return;
343   }
344
345   if (setjmp(png_ptr->jmpbuf)) {
346     png_destroy_write_struct(&png_ptr, &info_ptr);
347     fclose(fp);
348     return;
349   }
350
351   png_init_io(png_ptr, fp);
352
353   png_set_IHDR(png_ptr, info_ptr, nx * nxcell, ny * nycell, bitdepth, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_DEFAULT);
354
355   png_write_info(png_ptr, info_ptr);
356   for (int irow = ny - 1; irow >= 0; irow--) {
357     png_bytep row_pointer = rowp;
358     
359     for (int icol = 0; icol < nx; icol++) {
360       int pos = icol * nxcell;
361       double dens = (v[icol][irow] - densmin) / (densmax - densmin);
362       dens = clamp (dens, 0., 1.);
363       unsigned int outval = static_cast<unsigned int> (dens * max_out_level);
364
365       for (int p = pos; p < pos + nxcell; p++) {
366         if (bitdepth == 8)
367           rowp[p] = outval;
368         else {
369           int rowpos = p * 2;
370           rowp[rowpos] = (outval >> 8) & 0xFF;
371           rowp[rowpos+1] = (outval & 0xFF);
372         }
373       }
374     }
375     for (int ir = 0; ir < nycell; ir++)
376       png_write_rows (png_ptr, &row_pointer, 1);
377   }
378
379   png_write_end(png_ptr, info_ptr);
380   png_destroy_write_struct(&png_ptr, &info_ptr);
381
382   fclose(fp);
383 }
384 #endif
385
386 #ifdef HAVE_GD
387 #include "gd.h"
388 static const int N_GRAYSCALE=256;
389
390 void
391 ImageFile::writeImageGIF (const char *outfile, int nxcell, int nycell, double densmin, double densmax)
392 {
393   gdImagePtr gif;
394   FILE *out;
395   int gs_indices[N_GRAYSCALE];
396   int nx = m_nx;
397   int ny = m_ny;
398   ImageFileArray v = getArray();
399
400   unsigned char rowp [nx * nxcell];
401   if (rowp == NULL)
402     return;
403
404   gif = gdImageCreate(nx * nxcell, ny * nycell);
405   for (int i = 0; i < N_GRAYSCALE; i++)
406     gs_indices[i] = gdImageColorAllocate(gif, i, i, i);
407
408   int lastrow = ny * nycell - 1;
409   for (int irow = 0; irow < ny; irow++) {
410     int rpos = irow * nycell;
411     for (int ir = rpos; ir < rpos + nycell; ir++) {
412       for (int icol = 0; icol < nx; icol++) {
413         int cpos = icol * nxcell;
414         double dens = (v[icol][irow] - densmin) / (densmax - densmin);
415         dens = clamp(dens, 0., 1.);
416         for (int ic = cpos; ic < cpos + nxcell; ic++) {
417           rowp[ic] = (unsigned int) (dens * (double) (N_GRAYSCALE - 1));
418           gdImageSetPixel(gif, ic, lastrow - ir, gs_indices[rowp[ic]]);
419         }
420       }
421     }
422   }
423
424   if ((out = fopen(outfile,"w")) == NULL) {
425     sys_error(ERR_FATAL, "Error opening output file %s for writing", outfile);
426     return (1);
427   }
428   gdImageGif(gif,out);
429   fclose(out);
430   gdImageDestroy(gif);
431 }
432 #endif
433