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.11 2000/08/25 15:59:13 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
64 ImageFileArrayConst v = getArray();
65 if (v == NULL || nx == 0 || ny == 0)
69 int* pPens = new int [nx * ny * scale * scale ];
71 double view_scale = 255 / (pmax - pmin);
72 int id_X11 = g2_open_X11 (nx * scale, ny * scale);
74 for (int i = 0; i < 256; i++) {
75 double cval = i / 255.;
76 grayscale[i] = g2_ink (id_X11, cval, cval, cval);
79 for (int iy = ny - 1; iy >= 0; iy--) {
80 int iRowPos = ((ny - 1 - iy) * scale) * (nx * scale);
81 for (int ix = 0; ix < nx; ix++) {
82 int cval = static_cast<int>((v[ix][iy] - pmin) * view_scale);
87 for (int sy = 0; sy < scale; sy++)
88 for (int sx = 0; sx < scale; sx++)
89 pPens[iRowPos+(sy * nx * scale)+(sx + (ix * scale))] = grayscale[cval];
93 g2_image (id_X11, 0., 0., nx * scale, ny * scale, pPens);
103 // ImageFile::comparativeStatistics Calculate comparative stats
106 // d Normalized root mean squared distance measure
107 // r Normalized mean absolute distance measure
108 // e Worst case distance measure
111 // G.T. Herman, Image Reconstruction From Projections, 1980
114 ImageFile::comparativeStatistics (const ImageFile& imComp, double& d, double& r, double& e) const
116 if (imComp.nx() != m_nx && imComp.ny() != m_ny) {
117 sys_error (ERR_WARNING, "Image sizes differ [ImageFile::comparativeStatistics]");
120 ImageFileArrayConst v = getArray();
121 if (v == NULL || m_nx == 0 || m_ny == 0)
124 ImageFileArrayConst vComp = imComp.getArray();
127 for (unsigned int ix = 0; ix < m_nx; ix++) {
128 for (unsigned int iy = 0; iy < m_ny; iy++) {
132 myMean /= (m_nx * m_ny);
134 double sqErrorSum = 0.;
135 double absErrorSum = 0.;
136 double sqDiffFromMeanSum = 0.;
137 double absValueSum = 0.;
138 for (unsigned int ix = 0; ix < m_nx; ix++) {
139 for (unsigned int iy = 0; iy < m_ny; iy++) {
140 double diff = v[ix][iy] - vComp[ix][iy];
141 sqErrorSum += diff * diff;
142 absErrorSum += fabs(diff);
143 double diffFromMean = v[ix][iy] - myMean;
144 sqDiffFromMeanSum += diffFromMean * diffFromMean;
145 absValueSum += fabs(v[ix][iy]);
149 d = sqrt (sqErrorSum / sqDiffFromMeanSum);
150 r = absErrorSum / absValueSum;
155 for (int ix = 0; ix < hx; ix++) {
156 for (int iy = 0; iy < hy; iy++) {
157 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]);
158 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]);
159 double error = fabs (avgPixel - avgPixelComp);
172 ImageFile::printComparativeStatistics (const ImageFile& imComp, ostream& os) const
176 if (comparativeStatistics (imComp, d, r, e)) {
177 os << " Normalized root mean squared distance (d): " << d << endl;
178 os << " Normalized mean absolute distance (r): " << r << endl;
179 os << "Worst case distance (2x2 pixel average) (e): " << e << endl;
187 ImageFile::printStatistics (ostream& os) const
189 double min, max, mean, mode, median, stddev;
191 statistics (min, max, mean, mode, median, stddev);
193 os << " min: " << min << endl;
194 os << " max: " << max << endl;
195 os << " mean: " << mean << endl;
196 os << " mode: " << mode << endl;
197 os << "median: " << median << endl;
198 os << "stddev: " << stddev << endl;
203 ImageFile::statistics (double& min, double& max, double& mean, double& mode, double& median, double& stddev) const
207 ImageFileArrayConst v = getArray();
209 if (v == NULL || nx == 0 || ny == 0)
215 for (int ix = 0; ix < nx; ix++) {
216 for (int iy = 0; iy < ny; iy++) {
226 static const int nbin = 1024;
227 int hist[ nbin ] = {0};
228 double spread = max - min;
231 for (int ix = 0; ix < nx; ix++) {
232 for (int iy = 0; iy < ny; iy++) {
233 int b = static_cast<int>((((v[ix][iy] - min) / spread) * (nbin - 1)) + 0.5);
235 double diff = (v[ix][iy] - mean);
236 stddev += diff * diff;
239 stddev = sqrt(stddev / (nx * ny));
241 int max_binindex = 0;
243 for (int ibin = 0; ibin < nbin; ibin++) {
244 if (hist[ibin] > max_bin) {
245 max_bin = hist[ibin];
250 mode = (max_binindex * spread / (nbin - 1)) + min;
252 int nPixels = nx * ny;
253 slist<double> vecImage;
254 for (int ix = 0; ix < nx; ix++)
255 for (int iy = 0; iy < ny; iy++)
256 vecImage.push_front (v[ix][iy]);
258 slist<double>::const_iterator iter = vecImage.begin();
259 for (int i = 0; i < (nPixels / 2) - 1; i++)
260 iter++; // Advance iterator to (nPixels / 2) - 1;
262 if (nPixels % 2) { // Odd
266 median = (*iter++ + *iter) / 2;
271 ImageFile::getMinMax (double& min, double& max) const
275 ImageFileArrayConst v = getArray();
277 if (v == NULL || nx == 0 || ny == 0)
282 for (int ix = 0; ix < nx; ix++) {
283 for (int iy = 0; iy < ny; iy++) {
293 ImageFile::writeImagePGM (const char *outfile, int nxcell, int nycell, double densmin, double densmax)
298 ImageFileArray v = getArray();
300 unsigned char rowp [nx * nxcell];
302 if ((fp = fopen (outfile, "wb")) == NULL)
306 fprintf(fp, "%d %d\n", nx, ny);
307 fprintf(fp, "255\n");
309 for (int irow = ny - 1; irow >= 0; irow--) {
310 for (int icol = 0; icol < nx; icol++) {
311 int pos = icol * nxcell;
312 double dens = (v[icol][irow] - densmin) / (densmax - densmin);
313 dens = clamp (dens, 0., 1.);
314 for (int p = pos; p < pos + nxcell; p++) {
315 rowp[p] = static_cast<unsigned int> (dens * 255.);
318 for (int ir = 0; ir < nycell; ir++) {
319 for (int ic = 0; ic < nx * nxcell; ic++)
320 fprintf(fp, "%c ", rowp[ic]);
328 ImageFile::writeImagePGMASCII (const char *outfile, int nxcell, int nycell, double densmin, double densmax)
333 ImageFileArray v = getArray();
335 unsigned char rowp [nx * nxcell];
339 if ((fp = fopen (outfile, "wb")) == NULL)
343 fprintf(fp, "%d %d\n", nx, ny);
344 fprintf(fp, "255\n");
346 for (int irow = ny - 1; irow >= 0; irow--) {
347 for (int icol = 0; icol < nx; icol++) {
348 int pos = icol * nxcell;
349 double dens = (v[icol][irow] - densmin) / (densmax - densmin);
350 dens = clamp (dens, 0., 1.);
351 for (int p = pos; p < pos + nxcell; p++) {
352 rowp[p] = static_cast<unsigned int> (dens * 255.);
355 for (int ir = 0; ir < nycell; ir++) {
356 for (int ic = 0; ic < nx * nxcell; ic++)
357 fprintf(fp, "%d ", rowp[ic]);
368 ImageFile::writeImagePNG (const char *outfile, int bitdepth, int nxcell, int nycell, double densmin, double densmax)
373 double max_out_level = (1 << bitdepth) - 1;
376 ImageFileArray v = getArray();
378 unsigned char rowp [nx * nxcell * (bitdepth / 8)];
380 if ((fp = fopen (outfile, "wb")) == NULL)
383 png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
387 info_ptr = png_create_info_struct(png_ptr);
389 png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
394 if (setjmp(png_ptr->jmpbuf)) {
395 png_destroy_write_struct(&png_ptr, &info_ptr);
400 png_init_io(png_ptr, fp);
402 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);
404 png_write_info(png_ptr, info_ptr);
405 for (int irow = ny - 1; irow >= 0; irow--) {
406 png_bytep row_pointer = rowp;
408 for (int icol = 0; icol < nx; icol++) {
409 int pos = icol * nxcell;
410 double dens = (v[icol][irow] - densmin) / (densmax - densmin);
411 dens = clamp (dens, 0., 1.);
412 unsigned int outval = static_cast<unsigned int> (dens * max_out_level);
414 for (int p = pos; p < pos + nxcell; p++) {
419 rowp[rowpos] = (outval >> 8) & 0xFF;
420 rowp[rowpos+1] = (outval & 0xFF);
424 for (int ir = 0; ir < nycell; ir++)
425 png_write_rows (png_ptr, &row_pointer, 1);
428 png_write_end(png_ptr, info_ptr);
429 png_destroy_write_struct(&png_ptr, &info_ptr);
437 static const int N_GRAYSCALE=256;
440 ImageFile::writeImageGIF (const char *outfile, int nxcell, int nycell, double densmin, double densmax)
444 int gs_indices[N_GRAYSCALE];
447 ImageFileArray v = getArray();
449 unsigned char rowp [nx * nxcell];
453 gif = gdImageCreate(nx * nxcell, ny * nycell);
454 for (int i = 0; i < N_GRAYSCALE; i++)
455 gs_indices[i] = gdImageColorAllocate(gif, i, i, i);
457 int lastrow = ny * nycell - 1;
458 for (int irow = 0; irow < ny; irow++) {
459 int rpos = irow * nycell;
460 for (int ir = rpos; ir < rpos + nycell; ir++) {
461 for (int icol = 0; icol < nx; icol++) {
462 int cpos = icol * nxcell;
463 double dens = (v[icol][irow] - densmin) / (densmax - densmin);
464 dens = clamp(dens, 0., 1.);
465 for (int ic = cpos; ic < cpos + nxcell; ic++) {
466 rowp[ic] = (unsigned int) (dens * (double) (N_GRAYSCALE - 1));
467 gdImageSetPixel(gif, ic, lastrow - ir, gs_indices[rowp[ic]]);
473 if ((out = fopen(outfile,"w")) == NULL) {
474 sys_error(ERR_FATAL, "Error opening output file %s for writing", outfile);