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.24 2000/12/22 04:18:00 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 ******************************************************************************/
31 F32Image::F32Image (int nx, int ny)
\r
32 : Array2dFile (nx, ny, sizeof(kfloat32), Array2dFile::PIXEL_FLOAT32)
\r
36 F32Image::F32Image (void)
\r
39 setPixelFormat (Array2dFile::PIXEL_FLOAT32);
\r
40 setPixelSize (sizeof(kfloat32));
\r
43 F64Image::F64Image (int nx, int ny)
\r
44 : Array2dFile (nx, ny, sizeof(kfloat64), Array2dFile::PIXEL_FLOAT64)
\r
48 F64Image::F64Image (void)
\r
51 setPixelFormat (PIXEL_FLOAT64);
\r
52 setPixelSize (sizeof(kfloat64));
\r
56 ImageFile::filterResponse (const char* const domainName, double bw, const char* const filterName, double filt_param)
58 int hx = (m_nx - 1) / 2;
59 int hy = (m_ny - 1) / 2;
60 ImageFileArray v = getArray();
61 SignalFilter filter (filterName, domainName, bw, filt_param);
63 for (int i = -hx; i <= hx; i++) {
64 for (int j = -hy; j <= hy; j++) {
65 double r = sqrt (i * i + j * j);
67 v[i+hx][j+hy] = filter.response (r);
73 ImageFile::display (void) const
77 getMinMax (pmin, pmax);
79 return (displayScaling (1, pmin, pmax));
83 ImageFile::displayScaling (const int scale, const ImageFileValue pmin, const ImageFileValue pmax) const
87 ImageFileArrayConst v = getArray();
88 if (v == NULL || nx == 0 || ny == 0)
92 int* pPens = new int [nx * ny * scale * scale ];
94 double view_scale = 255 / (pmax - pmin);
95 int id_X11 = g2_open_X11 (nx * scale, ny * scale);
97 for (int i = 0; i < 256; i++) {
98 double cval = i / 255.;
99 grayscale[i] = g2_ink (id_X11, cval, cval, cval);
102 for (int iy = ny - 1; iy >= 0; iy--) {
103 int iRowPos = ((ny - 1 - iy) * scale) * (nx * scale);
104 for (int ix = 0; ix < nx; ix++) {
105 int cval = static_cast<int>((v[ix][iy] - pmin) * view_scale);
110 for (int sy = 0; sy < scale; sy++)
111 for (int sx = 0; sx < scale; sx++)
112 pPens[iRowPos+(sy * nx * scale)+(sx + (ix * scale))] = grayscale[cval];
116 g2_image (id_X11, 0., 0., nx * scale, ny * scale, pPens);
127 // ImageFile::comparativeStatistics Calculate comparative stats
130 // d Normalized root mean squared distance measure
131 // r Normalized mean absolute distance measure
132 // e Worst case distance measure
135 // G.T. Herman, Image Reconstruction From Projections, 1980
138 ImageFile::comparativeStatistics (const ImageFile& imComp, double& d, double& r, double& e) const
140 if (imComp.nx() != m_nx && imComp.ny() != m_ny) {
141 sys_error (ERR_WARNING, "Image sizes differ [ImageFile::comparativeStatistics]");
144 ImageFileArrayConst v = getArray();
145 if (v == NULL || m_nx == 0 || m_ny == 0)
148 ImageFileArrayConst vComp = imComp.getArray();
151 for (unsigned int ix = 0; ix < m_nx; ix++) {
152 for (unsigned int iy = 0; iy < m_ny; iy++) {
156 myMean /= (m_nx * m_ny);
158 double sqErrorSum = 0.;
159 double absErrorSum = 0.;
160 double sqDiffFromMeanSum = 0.;
161 double absValueSum = 0.;
162 for (unsigned int ix2 = 0; ix2 < m_nx; ix2++) {
163 for (unsigned int iy = 0; iy < m_ny; iy++) {
164 double diff = v[ix2][iy] - vComp[ix2][iy];
165 sqErrorSum += diff * diff;
166 absErrorSum += fabs(diff);
167 double diffFromMean = v[ix2][iy] - myMean;
168 sqDiffFromMeanSum += diffFromMean * diffFromMean;
169 absValueSum += fabs(v[ix2][iy]);
173 d = sqrt (sqErrorSum / sqDiffFromMeanSum);
174 r = absErrorSum / absValueSum;
179 for (int ix3 = 0; ix3 < hx; ix3++) {
180 for (int iy = 0; iy < hy; iy++) {
181 double avgPixel = 0.25 * (v[2*ix3][2*iy] + v[2*ix3+1][2*iy] + v[2*ix3][2*iy+1] + v[2*ix3+1][2*iy+1]);
182 double avgPixelComp = 0.25 * (vComp[2*ix3][2*iy] + vComp[2*ix3+1][2*iy] + vComp[2*ix3][2*iy+1] + vComp[2*ix3+1][2*iy+1]);
183 double error = fabs (avgPixel - avgPixelComp);
196 ImageFile::printComparativeStatistics (const ImageFile& imComp, std::ostream& os) const
200 if (comparativeStatistics (imComp, d, r, e)) {
201 os << " Normalized root mean squared distance (d): " << d << std::endl;
202 os << " Normalized mean absolute distance (r): " << r << std::endl;
203 os << "Worst case distance (2x2 pixel average) (e): " << e << std::endl;
211 ImageFile::printStatistics (std::ostream& os) const
213 double min, max, mean, mode, median, stddev;
215 statistics (min, max, mean, mode, median, stddev);
217 os << " min: " << min << std::endl;
218 os << " max: " << max << std::endl;
219 os << " mean: " << mean << std::endl;
220 os << " mode: " << mode << std::endl;
221 os << "median: " << median << std::endl;
222 os << "stddev: " << stddev << std::endl;
227 ImageFile::statistics (double& min, double& max, double& mean, double& mode, double& median, double& stddev) const
231 ImageFileArrayConst v = getArray();
233 if (v == NULL || nx == 0 || ny == 0)
236 std::vector<double> vecImage;
\r
238 vecImage.resize (nx * ny);
\r
239 for (int ix = 0; ix < nx; ix++) {
\r
240 for (int iy = 0; iy < ny; iy++)
\r
241 vecImage[iVec++] = v[ix][iy];
\r
244 vectorNumericStatistics (vecImage, nx * ny, min, max, mean, mode, median, stddev);
\r
249 ImageFile::getMinMax (double& min, double& max) const
253 ImageFileArrayConst v = getArray();
255 if (v == NULL || nx == 0 || ny == 0)
260 for (int ix = 0; ix < nx; ix++) {
261 for (int iy = 0; iy < ny; iy++) {
271 ImageFile::subtractImages (const ImageFile& rRHS, ImageFile& result) const
\r
273 if (m_nx != rRHS.nx() || m_ny != rRHS.ny() || m_nx != result.nx() || m_ny != result.ny()) {
\r
274 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::subtractImage]");
\r
278 ImageFileArrayConst vLHS = getArray();
\r
279 ImageFileArrayConst vRHS = rRHS.getArray();
\r
280 ImageFileArray vResult = result.getArray();
\r
282 for (int ix = 0; ix < m_nx; ix++) {
\r
283 ImageFileColumnConst in1 = vLHS[ix];
\r
284 ImageFileColumnConst in2 = vRHS[ix];
\r
285 ImageFileColumn out = vResult[ix];
\r
286 for (int iy = 0; iy < m_ny; iy++)
\r
287 *out++ = *in1++ - *in2++;
\r
294 ImageFile::addImages (const ImageFile& rRHS, ImageFile& result) const
\r
296 if (m_nx != rRHS.nx() || m_ny != rRHS.ny() || m_nx != result.nx() || m_ny != result.ny()) {
\r
297 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::subtractImage]");
\r
301 ImageFileArrayConst vLHS = getArray();
\r
302 ImageFileArrayConst vRHS = rRHS.getArray();
\r
303 ImageFileArray vResult = result.getArray();
\r
305 for (int ix = 0; ix < m_nx; ix++) {
\r
306 ImageFileColumnConst in1 = vLHS[ix];
\r
307 ImageFileColumnConst in2 = vRHS[ix];
\r
308 ImageFileColumn out = vResult[ix];
\r
309 for (int iy = 0; iy < m_ny; iy++)
\r
310 *out++ = *in1++ + *in2++;
\r
317 ImageFile::multiplyImages (const ImageFile& rRHS, ImageFile& result) const
\r
319 if (m_nx != rRHS.nx() || m_ny != rRHS.ny() || m_nx != result.nx() || m_ny != result.ny()) {
\r
320 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::subtractImage]");
\r
324 ImageFileArrayConst vLHS = getArray();
\r
325 ImageFileArrayConst vRHS = rRHS.getArray();
\r
326 ImageFileArray vResult = result.getArray();
\r
328 for (int ix = 0; ix < m_nx; ix++) {
\r
329 ImageFileColumnConst in1 = vLHS[ix];
\r
330 ImageFileColumnConst in2 = vRHS[ix];
\r
331 ImageFileColumn out = vResult[ix];
\r
332 for (int iy = 0; iy < m_ny; iy++)
\r
333 *out++ = *in1++ * *in2++;
\r
340 ImageFile::divideImages (const ImageFile& rRHS, ImageFile& result) const
\r
342 if (m_nx != rRHS.nx() || m_ny != rRHS.ny() || m_nx != result.nx() || m_ny != result.ny()) {
\r
343 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::subtractImage]");
\r
347 ImageFileArrayConst vLHS = getArray();
\r
348 ImageFileArrayConst vRHS = rRHS.getArray();
\r
349 ImageFileArray vResult = result.getArray();
\r
351 for (int ix = 0; ix < m_nx; ix++) {
\r
352 ImageFileColumnConst in1 = vLHS[ix];
\r
353 ImageFileColumnConst in2 = vRHS[ix];
\r
354 ImageFileColumn out = vResult[ix];
\r
355 for (int iy = 0; iy < m_ny; iy++) {
\r
357 *out++ = *in1++ / *in2++;
\r
368 ImageFile::writeImagePGM (const char *outfile, int nxcell, int nycell, double densmin, double densmax)
373 ImageFileArray v = getArray();
375 unsigned char* rowp = new unsigned char [nx * nxcell];
377 if ((fp = fopen (outfile, "wb")) == NULL)
381 fprintf(fp, "%d %d\n", nx, ny);
382 fprintf(fp, "255\n");
384 for (int irow = ny - 1; irow >= 0; irow--) {
385 for (int icol = 0; icol < nx; icol++) {
386 int pos = icol * nxcell;
387 double dens = (v[icol][irow] - densmin) / (densmax - densmin);
388 dens = clamp (dens, 0., 1.);
389 for (int p = pos; p < pos + nxcell; p++) {
390 rowp[p] = static_cast<unsigned int> (dens * 255.);
393 for (int ir = 0; ir < nycell; ir++) {
394 for (int ic = 0; ic < nx * nxcell; ic++)
395 fputc( rowp[ic], fp );
404 ImageFile::writeImagePGMASCII (const char *outfile, int nxcell, int nycell, double densmin, double densmax)
409 ImageFileArray v = getArray();
411 unsigned char* rowp = new unsigned char [nx * nxcell];
413 if ((fp = fopen (outfile, "wb")) == NULL)
417 fprintf(fp, "%d %d\n", nx, ny);
418 fprintf(fp, "255\n");
420 for (int irow = ny - 1; irow >= 0; irow--) {
421 for (int icol = 0; icol < nx; icol++) {
422 int pos = icol * nxcell;
423 double dens = (v[icol][irow] - densmin) / (densmax - densmin);
424 dens = clamp (dens, 0., 1.);
425 for (int p = pos; p < pos + nxcell; p++) {
426 rowp[p] = static_cast<unsigned int> (dens * 255.);
429 for (int ir = 0; ir < nycell; ir++) {
430 for (int ic = 0; ic < nx * nxcell; ic++)
431 fprintf(fp, "%d ", rowp[ic]);
443 ImageFile::writeImagePNG (const char *outfile, int bitdepth, int nxcell, int nycell, double densmin, double densmax)
448 double max_out_level = (1 << bitdepth) - 1;
451 ImageFileArray v = getArray();
453 unsigned char* rowp = new unsigned char [nx * nxcell * (bitdepth / 8)];
455 if ((fp = fopen (outfile, "wb")) == NULL)
458 png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
462 info_ptr = png_create_info_struct(png_ptr);
464 png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
469 if (setjmp(png_ptr->jmpbuf)) {
470 png_destroy_write_struct(&png_ptr, &info_ptr);
475 png_init_io(png_ptr, fp);
477 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);
479 png_write_info(png_ptr, info_ptr);
480 for (int irow = ny - 1; irow >= 0; irow--) {
481 png_bytep row_pointer = rowp;
483 for (int icol = 0; icol < nx; icol++) {
484 int pos = icol * nxcell;
485 double dens = (v[icol][irow] - densmin) / (densmax - densmin);
486 dens = clamp (dens, 0., 1.);
487 unsigned int outval = static_cast<unsigned int> (dens * max_out_level);
489 for (int p = pos; p < pos + nxcell; p++) {
494 rowp[rowpos] = (outval >> 8) & 0xFF;
495 rowp[rowpos+1] = (outval & 0xFF);
499 for (int ir = 0; ir < nycell; ir++)
500 png_write_rows (png_ptr, &row_pointer, 1);
503 png_write_end(png_ptr, info_ptr);
504 png_destroy_write_struct(&png_ptr, &info_ptr);
513 static const int N_GRAYSCALE=256;
516 ImageFile::writeImageGIF (const char *outfile, int nxcell, int nycell, double densmin, double densmax)
520 int gs_indices[N_GRAYSCALE];
523 ImageFileArray v = getArray();
525 unsigned char rowp [nx * nxcell];
529 gif = gdImageCreate(nx * nxcell, ny * nycell);
530 for (int i = 0; i < N_GRAYSCALE; i++)
531 gs_indices[i] = gdImageColorAllocate(gif, i, i, i);
533 int lastrow = ny * nycell - 1;
534 for (int irow = 0; irow < ny; irow++) {
535 int rpos = irow * nycell;
536 for (int ir = rpos; ir < rpos + nycell; ir++) {
537 for (int icol = 0; icol < nx; icol++) {
538 int cpos = icol * nxcell;
539 double dens = (v[icol][irow] - densmin) / (densmax - densmin);
540 dens = clamp(dens, 0., 1.);
541 for (int ic = cpos; ic < cpos + nxcell; ic++) {
542 rowp[ic] = (unsigned int) (dens * (double) (N_GRAYSCALE - 1));
543 gdImageSetPixel(gif, ic, lastrow - ir, gs_indices[rowp[ic]]);
549 if ((out = fopen(outfile,"w")) == NULL) {
550 sys_error(ERR_FATAL, "Error opening output file %s for writing", outfile);