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.7 2000/07/11 10:32:44 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)
54 // getPixelValueRange (pmin, pmax);
56 return (displayScaling (1, pmin, pmax));
60 ImageFile::displayScaling (const int scale, const ImageFileValue pmin, const ImageFileValue pmax)
65 ImageFileArray v = getArray();
68 int pens [nx * ny * scale * scale ];
70 double view_scale = 255 / (pmax - pmin);
71 int id_X11 = g2_open_X11 (nx * scale, ny * scale);
73 for (int i = 0; i < 256; i++) {
74 double cval = i / 255.;
75 grayscale[i] = g2_ink (id_X11, cval, cval, cval);
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);
85 pens[i++] = grayscale[cval];
89 g2_image (id_X11, 0., 0., nx, ny, pens);
98 // ImageFile::comparativeStatistics Calculate comparative stats
101 // d Normalized root mean squared distance measure
102 // r Normalized mean absolute distance measure
103 // e Worst case distance measure
106 // G.T. Herman, Image Reconstruction From Projections, 1980
109 ImageFile::comparativeStatistics (const ImageFile& imComp, double& d, double& r, double& e) const
111 if (imComp.nx() != m_nx && imComp.ny() != m_ny) {
112 sys_error (ERR_WARNING, "Image sizes differ [ImageFile::comparativeStatistics]");
115 ImageFileArrayConst v = getArray();
116 ImageFileArrayConst vComp = imComp.getArray();
119 for (int ix = 0; ix < m_nx; ix++) {
120 for (int iy = 0; iy < m_ny; iy++) {
124 myMean /= (m_nx * m_ny);
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]);
141 d = sqrt (sqErrorSum / sqDiffFromMeanSum);
142 r = absErrorSum / absValueSum;
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);
164 ImageFile::printComparativeStatistics (const ImageFile& imComp, ostream& os) const
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;
177 ImageFile::printStatistics (ostream& os) const
179 double min, max, mean, mode, median, stddev;
181 statistics (min, max, mean, mode, median, stddev);
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;
193 ImageFile::statistics (double& min, double& max, double& mean, double& mode, double& median, double& stddev) const
197 ImageFileArrayConst v = getArray();
202 for (int ix = 0; ix < nx; ix++) {
203 for (int iy = 0; iy < ny; iy++) {
213 static const int nbin = 256;
214 int hist[ nbin ] = {0};
215 double spread = max - min;
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);
222 double diff = (v[ix][iy] - mean);
223 stddev += diff * diff;
226 stddev = sqrt(stddev / (nx * ny));
228 int max_binindex = 0;
230 for (int ibin = 0; ibin < nbin; ibin++) {
231 if (hist[ibin] > max_bin) {
232 max_bin = hist[ibin];
237 mode = (max_binindex * spread / (nbin - 1)) + min;
244 ImageFile::writeImagePGM (const char *outfile, int nxcell, int nycell, double densmin, double densmax)
249 ImageFileArray v = getArray();
251 unsigned char rowp [nx * nxcell];
253 if ((fp = fopen (outfile, "wb")) == NULL)
257 fprintf(fp, "%d %d\n", nx, ny);
258 fprintf(fp, "255\n");
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.);
269 for (int ir = 0; ir < nycell; ir++) {
270 for (int ic = 0; ic < nx * nxcell; ic++)
271 fprintf(fp, "%c ", rowp[ic]);
279 ImageFile::writeImagePGMASCII (const char *outfile, int nxcell, int nycell, double densmin, double densmax)
284 ImageFileArray v = getArray();
286 unsigned char rowp [nx * nxcell];
290 if ((fp = fopen (outfile, "wb")) == NULL)
294 fprintf(fp, "%d %d\n", nx, ny);
295 fprintf(fp, "255\n");
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.);
306 for (int ir = 0; ir < nycell; ir++) {
307 for (int ic = 0; ic < nx * nxcell; ic++)
308 fprintf(fp, "%d ", rowp[ic]);
319 ImageFile::writeImagePNG (const char *outfile, int bitdepth, int nxcell, int nycell, double densmin, double densmax)
324 double max_out_level = (1 << bitdepth) - 1;
327 ImageFileArray v = getArray();
329 unsigned char rowp [nx * nxcell * (bitdepth / 8)];
331 if ((fp = fopen (outfile, "wb")) == NULL)
334 png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
338 info_ptr = png_create_info_struct(png_ptr);
340 png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
345 if (setjmp(png_ptr->jmpbuf)) {
346 png_destroy_write_struct(&png_ptr, &info_ptr);
351 png_init_io(png_ptr, fp);
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);
355 png_write_info(png_ptr, info_ptr);
356 for (int irow = ny - 1; irow >= 0; irow--) {
357 png_bytep row_pointer = rowp;
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);
365 for (int p = pos; p < pos + nxcell; p++) {
370 rowp[rowpos] = (outval >> 8) & 0xFF;
371 rowp[rowpos+1] = (outval & 0xFF);
375 for (int ir = 0; ir < nycell; ir++)
376 png_write_rows (png_ptr, &row_pointer, 1);
379 png_write_end(png_ptr, info_ptr);
380 png_destroy_write_struct(&png_ptr, &info_ptr);
388 static const int N_GRAYSCALE=256;
391 ImageFile::writeImageGIF (const char *outfile, int nxcell, int nycell, double densmin, double densmax)
395 int gs_indices[N_GRAYSCALE];
398 ImageFileArray v = getArray();
400 unsigned char rowp [nx * nxcell];
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);
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]]);
424 if ((out = fopen(outfile,"w")) == NULL) {
425 sys_error(ERR_FATAL, "Error opening output file %s for writing", outfile);