1 /*****************************************************************************
5 ** Purpose: Imagefile classes
6 ** Programmer: Kevin Rosenberg
7 ** Date Started: June 2000
9 ** This is part of the CTSim program
10 ** Copyright (C) 1983-2000 Kevin Rosenberg
12 ** $Id: imagefile.cpp,v 1.8 2000/07/13 07:03:21 kevin Exp $
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.
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.
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 ******************************************************************************/
33 ImageFile::filterResponse (const char* const domainName, double bw, const char* const filterName, double filt_param)
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);
40 for (int i = -hx; i <= hx; i++) {
41 for (int j = -hy; j <= hy; j++) {
42 double r = sqrt (i * i + j * j);
44 v[i+hx][j+hy] = filter.response (r);
50 ImageFile::display (void) const
54 getMinMax (pmin, pmax);
56 return (displayScaling (1, pmin, pmax));
60 ImageFile::displayScaling (const int scale, const ImageFileValue pmin, const ImageFileValue pmax) const
65 ImageFileArrayConst v = getArray();
66 if (v == NULL || nx == 0 || ny == 0)
70 int pens [nx * ny * scale * scale ];
72 double view_scale = 255 / (pmax - pmin);
73 int id_X11 = g2_open_X11 (nx * scale, ny * scale);
75 for (int i = 0; i < 256; i++) {
76 double cval = i / 255.;
77 grayscale[i] = g2_ink (id_X11, cval, cval, cval);
80 for (int i= 0, iy = ny - 1; iy >= 0; iy--) {
81 for (int ix = 0; ix < nx; ix++) {
82 int cval = static_cast<int>((v[ix][iy] - pmin) * view_scale);
87 pens[i++] = grayscale[cval];
91 g2_image (id_X11, 0., 0., nx, ny, pens);
100 // ImageFile::comparativeStatistics Calculate comparative stats
103 // d Normalized root mean squared distance measure
104 // r Normalized mean absolute distance measure
105 // e Worst case distance measure
108 // G.T. Herman, Image Reconstruction From Projections, 1980
111 ImageFile::comparativeStatistics (const ImageFile& imComp, double& d, double& r, double& e) const
113 if (imComp.nx() != m_nx && imComp.ny() != m_ny) {
114 sys_error (ERR_WARNING, "Image sizes differ [ImageFile::comparativeStatistics]");
117 ImageFileArrayConst v = getArray();
118 if (v == NULL || m_nx == 0 || m_ny == 0)
121 ImageFileArrayConst vComp = imComp.getArray();
124 for (unsigned int ix = 0; ix < m_nx; ix++) {
125 for (unsigned int iy = 0; iy < m_ny; iy++) {
129 myMean /= (m_nx * m_ny);
131 double sqErrorSum = 0.;
132 double absErrorSum = 0.;
133 double sqDiffFromMeanSum = 0.;
134 double absValueSum = 0.;
135 for (unsigned int ix = 0; ix < m_nx; ix++) {
136 for (unsigned int iy = 0; iy < m_ny; iy++) {
137 double diff = v[ix][iy] - vComp[ix][iy];
138 sqErrorSum += diff * diff;
139 absErrorSum += fabs(diff);
140 double diffFromMean = v[ix][iy] - myMean;
141 sqDiffFromMeanSum += diffFromMean * diffFromMean;
142 absValueSum += fabs(v[ix][iy]);
146 d = sqrt (sqErrorSum / sqDiffFromMeanSum);
147 r = absErrorSum / absValueSum;
152 for (int ix = 0; ix < hx; ix++) {
153 for (int iy = 0; iy < hy; iy++) {
154 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]);
155 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]);
156 double error = fabs (avgPixel - avgPixelComp);
169 ImageFile::printComparativeStatistics (const ImageFile& imComp, ostream& os) const
173 if (comparativeStatistics (imComp, d, r, e)) {
174 os << " Normalized root mean squared distance (d): " << d << endl;
175 os << " Normalized mean absolute distance (r): " << r << endl;
176 os << "Worst case distance (2x2 pixel average) (e): " << e << endl;
184 ImageFile::printStatistics (ostream& os) const
186 double min, max, mean, mode, median, stddev;
188 statistics (min, max, mean, mode, median, stddev);
190 os << " min: " << min << endl;
191 os << " max: " << max << endl;
192 os << " mean: " << mean << endl;
193 os << " mode: " << mode << endl;
194 os << "median: " << median << endl;
195 os << "stddev: " << stddev << endl;
200 ImageFile::statistics (double& min, double& max, double& mean, double& mode, double& median, double& stddev) const
204 ImageFileArrayConst v = getArray();
206 if (v == NULL || nx == 0 || ny == 0)
212 for (int ix = 0; ix < nx; ix++) {
213 for (int iy = 0; iy < ny; iy++) {
223 static const int nbin = 256;
224 int hist[ nbin ] = {0};
225 double spread = max - min;
228 for (int ix = 0; ix < nx; ix++) {
229 for (int iy = 0; iy < ny; iy++) {
230 int b = static_cast<int>((((v[ix][iy] - min) / spread) * (nbin - 1)) + 0.5);
232 double diff = (v[ix][iy] - mean);
233 stddev += diff * diff;
236 stddev = sqrt(stddev / (nx * ny));
238 int max_binindex = 0;
240 for (int ibin = 0; ibin < nbin; ibin++) {
241 if (hist[ibin] > max_bin) {
242 max_bin = hist[ibin];
247 mode = (max_binindex * spread / (nbin - 1)) + min;
254 ImageFile::getMinMax (double& min, double& max) const
258 ImageFileArrayConst v = getArray();
260 if (v == NULL || nx == 0 || ny == 0)
265 for (int ix = 0; ix < nx; ix++) {
266 for (int iy = 0; iy < ny; iy++) {
276 ImageFile::writeImagePGM (const char *outfile, int nxcell, int nycell, double densmin, double densmax)
281 ImageFileArray v = getArray();
283 unsigned char rowp [nx * nxcell];
285 if ((fp = fopen (outfile, "wb")) == NULL)
289 fprintf(fp, "%d %d\n", nx, ny);
290 fprintf(fp, "255\n");
292 for (int irow = ny - 1; irow >= 0; irow--) {
293 for (int icol = 0; icol < nx; icol++) {
294 int pos = icol * nxcell;
295 double dens = (v[icol][irow] - densmin) / (densmax - densmin);
296 dens = clamp (dens, 0., 1.);
297 for (int p = pos; p < pos + nxcell; p++) {
298 rowp[p] = static_cast<unsigned int> (dens * 255.);
301 for (int ir = 0; ir < nycell; ir++) {
302 for (int ic = 0; ic < nx * nxcell; ic++)
303 fprintf(fp, "%c ", rowp[ic]);
311 ImageFile::writeImagePGMASCII (const char *outfile, int nxcell, int nycell, double densmin, double densmax)
316 ImageFileArray v = getArray();
318 unsigned char rowp [nx * nxcell];
322 if ((fp = fopen (outfile, "wb")) == NULL)
326 fprintf(fp, "%d %d\n", nx, ny);
327 fprintf(fp, "255\n");
329 for (int irow = ny - 1; irow >= 0; irow--) {
330 for (int icol = 0; icol < nx; icol++) {
331 int pos = icol * nxcell;
332 double dens = (v[icol][irow] - densmin) / (densmax - densmin);
333 dens = clamp (dens, 0., 1.);
334 for (int p = pos; p < pos + nxcell; p++) {
335 rowp[p] = static_cast<unsigned int> (dens * 255.);
338 for (int ir = 0; ir < nycell; ir++) {
339 for (int ic = 0; ic < nx * nxcell; ic++)
340 fprintf(fp, "%d ", rowp[ic]);
351 ImageFile::writeImagePNG (const char *outfile, int bitdepth, int nxcell, int nycell, double densmin, double densmax)
356 double max_out_level = (1 << bitdepth) - 1;
359 ImageFileArray v = getArray();
361 unsigned char rowp [nx * nxcell * (bitdepth / 8)];
363 if ((fp = fopen (outfile, "wb")) == NULL)
366 png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
370 info_ptr = png_create_info_struct(png_ptr);
372 png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
377 if (setjmp(png_ptr->jmpbuf)) {
378 png_destroy_write_struct(&png_ptr, &info_ptr);
383 png_init_io(png_ptr, fp);
385 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);
387 png_write_info(png_ptr, info_ptr);
388 for (int irow = ny - 1; irow >= 0; irow--) {
389 png_bytep row_pointer = rowp;
391 for (int icol = 0; icol < nx; icol++) {
392 int pos = icol * nxcell;
393 double dens = (v[icol][irow] - densmin) / (densmax - densmin);
394 dens = clamp (dens, 0., 1.);
395 unsigned int outval = static_cast<unsigned int> (dens * max_out_level);
397 for (int p = pos; p < pos + nxcell; p++) {
402 rowp[rowpos] = (outval >> 8) & 0xFF;
403 rowp[rowpos+1] = (outval & 0xFF);
407 for (int ir = 0; ir < nycell; ir++)
408 png_write_rows (png_ptr, &row_pointer, 1);
411 png_write_end(png_ptr, info_ptr);
412 png_destroy_write_struct(&png_ptr, &info_ptr);
420 static const int N_GRAYSCALE=256;
423 ImageFile::writeImageGIF (const char *outfile, int nxcell, int nycell, double densmin, double densmax)
427 int gs_indices[N_GRAYSCALE];
430 ImageFileArray v = getArray();
432 unsigned char rowp [nx * nxcell];
436 gif = gdImageCreate(nx * nxcell, ny * nycell);
437 for (int i = 0; i < N_GRAYSCALE; i++)
438 gs_indices[i] = gdImageColorAllocate(gif, i, i, i);
440 int lastrow = ny * nycell - 1;
441 for (int irow = 0; irow < ny; irow++) {
442 int rpos = irow * nycell;
443 for (int ir = rpos; ir < rpos + nycell; ir++) {
444 for (int icol = 0; icol < nx; icol++) {
445 int cpos = icol * nxcell;
446 double dens = (v[icol][irow] - densmin) / (densmax - densmin);
447 dens = clamp(dens, 0., 1.);
448 for (int ic = cpos; ic < cpos + nxcell; ic++) {
449 rowp[ic] = (unsigned int) (dens * (double) (N_GRAYSCALE - 1));
450 gdImageSetPixel(gif, ic, lastrow - ir, gs_indices[rowp[ic]]);
456 if ((out = fopen(outfile,"w")) == NULL) {
457 sys_error(ERR_FATAL, "Error opening output file %s for writing", outfile);