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.9 2000/07/15 08:36: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 pens [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 i= 0, iy = ny - 1; iy >= 0; iy--) {
80 for (int ix = 0; ix < nx; ix++) {
81 int cval = static_cast<int>((v[ix][iy] - pmin) * view_scale);
86 pens[i++] = grayscale[cval];
90 g2_image (id_X11, 0., 0., nx, ny, pens);
99 // ImageFile::comparativeStatistics Calculate comparative stats
102 // d Normalized root mean squared distance measure
103 // r Normalized mean absolute distance measure
104 // e Worst case distance measure
107 // G.T. Herman, Image Reconstruction From Projections, 1980
110 ImageFile::comparativeStatistics (const ImageFile& imComp, double& d, double& r, double& e) const
112 if (imComp.nx() != m_nx && imComp.ny() != m_ny) {
113 sys_error (ERR_WARNING, "Image sizes differ [ImageFile::comparativeStatistics]");
116 ImageFileArrayConst v = getArray();
117 if (v == NULL || m_nx == 0 || m_ny == 0)
120 ImageFileArrayConst vComp = imComp.getArray();
123 for (unsigned int ix = 0; ix < m_nx; ix++) {
124 for (unsigned int iy = 0; iy < m_ny; iy++) {
128 myMean /= (m_nx * m_ny);
130 double sqErrorSum = 0.;
131 double absErrorSum = 0.;
132 double sqDiffFromMeanSum = 0.;
133 double absValueSum = 0.;
134 for (unsigned int ix = 0; ix < m_nx; ix++) {
135 for (unsigned int iy = 0; iy < m_ny; iy++) {
136 double diff = v[ix][iy] - vComp[ix][iy];
137 sqErrorSum += diff * diff;
138 absErrorSum += fabs(diff);
139 double diffFromMean = v[ix][iy] - myMean;
140 sqDiffFromMeanSum += diffFromMean * diffFromMean;
141 absValueSum += fabs(v[ix][iy]);
145 d = sqrt (sqErrorSum / sqDiffFromMeanSum);
146 r = absErrorSum / absValueSum;
151 for (int ix = 0; ix < hx; ix++) {
152 for (int iy = 0; iy < hy; iy++) {
153 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]);
154 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]);
155 double error = fabs (avgPixel - avgPixelComp);
168 ImageFile::printComparativeStatistics (const ImageFile& imComp, ostream& os) const
172 if (comparativeStatistics (imComp, d, r, e)) {
173 os << " Normalized root mean squared distance (d): " << d << endl;
174 os << " Normalized mean absolute distance (r): " << r << endl;
175 os << "Worst case distance (2x2 pixel average) (e): " << e << endl;
183 ImageFile::printStatistics (ostream& os) const
185 double min, max, mean, mode, median, stddev;
187 statistics (min, max, mean, mode, median, stddev);
189 os << " min: " << min << endl;
190 os << " max: " << max << endl;
191 os << " mean: " << mean << endl;
192 os << " mode: " << mode << endl;
193 os << "median: " << median << endl;
194 os << "stddev: " << stddev << endl;
199 ImageFile::statistics (double& min, double& max, double& mean, double& mode, double& median, double& stddev) const
203 ImageFileArrayConst v = getArray();
205 if (v == NULL || nx == 0 || ny == 0)
211 for (int ix = 0; ix < nx; ix++) {
212 for (int iy = 0; iy < ny; iy++) {
222 static const int nbin = 256;
223 int hist[ nbin ] = {0};
224 double spread = max - min;
227 for (int ix = 0; ix < nx; ix++) {
228 for (int iy = 0; iy < ny; iy++) {
229 int b = static_cast<int>((((v[ix][iy] - min) / spread) * (nbin - 1)) + 0.5);
231 double diff = (v[ix][iy] - mean);
232 stddev += diff * diff;
235 stddev = sqrt(stddev / (nx * ny));
237 int max_binindex = 0;
239 for (int ibin = 0; ibin < nbin; ibin++) {
240 if (hist[ibin] > max_bin) {
241 max_bin = hist[ibin];
246 mode = (max_binindex * spread / (nbin - 1)) + min;
253 ImageFile::getMinMax (double& min, double& max) const
257 ImageFileArrayConst v = getArray();
259 if (v == NULL || nx == 0 || ny == 0)
264 for (int ix = 0; ix < nx; ix++) {
265 for (int iy = 0; iy < ny; iy++) {
275 ImageFile::writeImagePGM (const char *outfile, int nxcell, int nycell, double densmin, double densmax)
280 ImageFileArray v = getArray();
282 unsigned char rowp [nx * nxcell];
284 if ((fp = fopen (outfile, "wb")) == NULL)
288 fprintf(fp, "%d %d\n", nx, ny);
289 fprintf(fp, "255\n");
291 for (int irow = ny - 1; irow >= 0; irow--) {
292 for (int icol = 0; icol < nx; icol++) {
293 int pos = icol * nxcell;
294 double dens = (v[icol][irow] - densmin) / (densmax - densmin);
295 dens = clamp (dens, 0., 1.);
296 for (int p = pos; p < pos + nxcell; p++) {
297 rowp[p] = static_cast<unsigned int> (dens * 255.);
300 for (int ir = 0; ir < nycell; ir++) {
301 for (int ic = 0; ic < nx * nxcell; ic++)
302 fprintf(fp, "%c ", rowp[ic]);
310 ImageFile::writeImagePGMASCII (const char *outfile, int nxcell, int nycell, double densmin, double densmax)
315 ImageFileArray v = getArray();
317 unsigned char rowp [nx * nxcell];
321 if ((fp = fopen (outfile, "wb")) == NULL)
325 fprintf(fp, "%d %d\n", nx, ny);
326 fprintf(fp, "255\n");
328 for (int irow = ny - 1; irow >= 0; irow--) {
329 for (int icol = 0; icol < nx; icol++) {
330 int pos = icol * nxcell;
331 double dens = (v[icol][irow] - densmin) / (densmax - densmin);
332 dens = clamp (dens, 0., 1.);
333 for (int p = pos; p < pos + nxcell; p++) {
334 rowp[p] = static_cast<unsigned int> (dens * 255.);
337 for (int ir = 0; ir < nycell; ir++) {
338 for (int ic = 0; ic < nx * nxcell; ic++)
339 fprintf(fp, "%d ", rowp[ic]);
350 ImageFile::writeImagePNG (const char *outfile, int bitdepth, int nxcell, int nycell, double densmin, double densmax)
355 double max_out_level = (1 << bitdepth) - 1;
358 ImageFileArray v = getArray();
360 unsigned char rowp [nx * nxcell * (bitdepth / 8)];
362 if ((fp = fopen (outfile, "wb")) == NULL)
365 png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
369 info_ptr = png_create_info_struct(png_ptr);
371 png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
376 if (setjmp(png_ptr->jmpbuf)) {
377 png_destroy_write_struct(&png_ptr, &info_ptr);
382 png_init_io(png_ptr, fp);
384 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);
386 png_write_info(png_ptr, info_ptr);
387 for (int irow = ny - 1; irow >= 0; irow--) {
388 png_bytep row_pointer = rowp;
390 for (int icol = 0; icol < nx; icol++) {
391 int pos = icol * nxcell;
392 double dens = (v[icol][irow] - densmin) / (densmax - densmin);
393 dens = clamp (dens, 0., 1.);
394 unsigned int outval = static_cast<unsigned int> (dens * max_out_level);
396 for (int p = pos; p < pos + nxcell; p++) {
401 rowp[rowpos] = (outval >> 8) & 0xFF;
402 rowp[rowpos+1] = (outval & 0xFF);
406 for (int ir = 0; ir < nycell; ir++)
407 png_write_rows (png_ptr, &row_pointer, 1);
410 png_write_end(png_ptr, info_ptr);
411 png_destroy_write_struct(&png_ptr, &info_ptr);
419 static const int N_GRAYSCALE=256;
422 ImageFile::writeImageGIF (const char *outfile, int nxcell, int nycell, double densmin, double densmax)
426 int gs_indices[N_GRAYSCALE];
429 ImageFileArray v = getArray();
431 unsigned char rowp [nx * nxcell];
435 gif = gdImageCreate(nx * nxcell, ny * nycell);
436 for (int i = 0; i < N_GRAYSCALE; i++)
437 gs_indices[i] = gdImageColorAllocate(gif, i, i, i);
439 int lastrow = ny * nycell - 1;
440 for (int irow = 0; irow < ny; irow++) {
441 int rpos = irow * nycell;
442 for (int ir = rpos; ir < rpos + nycell; ir++) {
443 for (int icol = 0; icol < nx; icol++) {
444 int cpos = icol * nxcell;
445 double dens = (v[icol][irow] - densmin) / (densmax - densmin);
446 dens = clamp(dens, 0., 1.);
447 for (int ic = cpos; ic < cpos + nxcell; ic++) {
448 rowp[ic] = (unsigned int) (dens * (double) (N_GRAYSCALE - 1));
449 gdImageSetPixel(gif, ic, lastrow - ir, gs_indices[rowp[ic]]);
455 if ((out = fopen(outfile,"w")) == NULL) {
456 sys_error(ERR_FATAL, "Error opening output file %s for writing", outfile);