X-Git-Url: http://git.kpe.io/?a=blobdiff_plain;f=libctsim%2Fimagefile.cpp;h=9df4285a8a35904ee88939647bb6a5428f81a51b;hb=bfcc769cf8019eabc8c65c07257c8dbee4b4c977;hp=9ae326f73ed6de8e3bd1038ef736c645d7c7b5b5;hpb=30e455abcd8cac05ce7afe43216ec9e26342e1cf;p=ctsim.git diff --git a/libctsim/imagefile.cpp b/libctsim/imagefile.cpp index 9ae326f..9df4285 100644 --- a/libctsim/imagefile.cpp +++ b/libctsim/imagefile.cpp @@ -9,7 +9,7 @@ ** This is part of the CTSim program ** Copyright (C) 1983-2000 Kevin Rosenberg ** -** $Id: imagefile.cpp,v 1.7 2000/07/11 10:32:44 kevin Exp $ +** $Id: imagefile.cpp,v 1.11 2000/08/25 15:59:13 kevin Exp $ ** ** This program is free software; you can redistribute it and/or modify ** it under the terms of the GNU General Public License (version 2) as @@ -47,47 +47,52 @@ ImageFile::filterResponse (const char* const domainName, double bw, const char* } int -ImageFile::display (void) +ImageFile::display (void) const { double pmin, pmax; - // getPixelValueRange (pmin, pmax); + getMinMax (pmin, pmax); return (displayScaling (1, pmin, pmax)); } int -ImageFile::displayScaling (const int scale, const ImageFileValue pmin, const ImageFileValue pmax) +ImageFile::displayScaling (const int scale, const ImageFileValue pmin, const ImageFileValue pmax) const { - int grayscale[256]; int nx = m_nx; int ny = m_ny; - ImageFileArray v = getArray(); + ImageFileArrayConst v = getArray(); + if (v == NULL || nx == 0 || ny == 0) + return 0; #if HAVE_G2_H - int pens [nx * ny * scale * scale ]; + int* pPens = new int [nx * ny * scale * scale ]; double view_scale = 255 / (pmax - pmin); int id_X11 = g2_open_X11 (nx * scale, ny * scale); - + int grayscale[256]; for (int i = 0; i < 256; i++) { double cval = i / 255.; grayscale[i] = g2_ink (id_X11, cval, cval, cval); } - for (int i= 0, iy = ny - 1; iy >= 0; iy--) { - for (int ix = 0; ix < nx; ix++) { - int cval = static_cast((v[ix][iy] - pmin) * view_scale); - if (cval < 0) - cval = 0; - else if (cval > 255) - cval = 255; - pens[i++] = grayscale[cval]; + for (int iy = ny - 1; iy >= 0; iy--) { + int iRowPos = ((ny - 1 - iy) * scale) * (nx * scale); + for (int ix = 0; ix < nx; ix++) { + int cval = static_cast((v[ix][iy] - pmin) * view_scale); + if (cval < 0) + cval = 0; + else if (cval > 255) + cval = 255; + for (int sy = 0; sy < scale; sy++) + for (int sx = 0; sx < scale; sx++) + pPens[iRowPos+(sy * nx * scale)+(sx + (ix * scale))] = grayscale[cval]; } } - g2_image (id_X11, 0., 0., nx, ny, pens); + g2_image (id_X11, 0., 0., nx * scale, ny * scale, pPens); + delete pPens; return (id_X11); #endif @@ -113,11 +118,14 @@ ImageFile::comparativeStatistics (const ImageFile& imComp, double& d, double& r, return false; } ImageFileArrayConst v = getArray(); + if (v == NULL || m_nx == 0 || m_ny == 0) + return false; + ImageFileArrayConst vComp = imComp.getArray(); double myMean = 0.; - for (int ix = 0; ix < m_nx; ix++) { - for (int iy = 0; iy < m_ny; iy++) { + for (unsigned int ix = 0; ix < m_nx; ix++) { + for (unsigned int iy = 0; iy < m_ny; iy++) { myMean += v[ix][iy]; } } @@ -127,8 +135,8 @@ ImageFile::comparativeStatistics (const ImageFile& imComp, double& d, double& r, double absErrorSum = 0.; double sqDiffFromMeanSum = 0.; double absValueSum = 0.; - for (int ix = 0; ix < m_nx; ix++) { - for (int iy = 0; iy < m_ny; iy++) { + for (unsigned int ix = 0; ix < m_nx; ix++) { + for (unsigned int iy = 0; iy < m_ny; iy++) { double diff = v[ix][iy] - vComp[ix][iy]; sqErrorSum += diff * diff; absErrorSum += fabs(diff); @@ -169,7 +177,9 @@ ImageFile::printComparativeStatistics (const ImageFile& imComp, ostream& os) con os << " Normalized root mean squared distance (d): " << d << endl; os << " Normalized mean absolute distance (r): " << r << endl; os << "Worst case distance (2x2 pixel average) (e): " << e << endl; + return true; } + return false; } @@ -196,6 +206,9 @@ ImageFile::statistics (double& min, double& max, double& mean, double& mode, dou int ny = m_ny; ImageFileArrayConst v = getArray(); + if (v == NULL || nx == 0 || ny == 0) + return; + mean = 0; min = v[0][0]; max = v[0][0]; @@ -210,7 +223,7 @@ ImageFile::statistics (double& min, double& max, double& mean, double& mode, dou } mean /= (nx * ny); - static const int nbin = 256; + static const int nbin = 1024; int hist[ nbin ] = {0}; double spread = max - min; mode = 0; @@ -235,11 +248,47 @@ ImageFile::statistics (double& min, double& max, double& mean, double& mode, dou } mode = (max_binindex * spread / (nbin - 1)) + min; - - median = 0.; + + int nPixels = nx * ny; + slist vecImage; + for (int ix = 0; ix < nx; ix++) + for (int iy = 0; iy < ny; iy++) + vecImage.push_front (v[ix][iy]); + vecImage.sort(); + slist::const_iterator iter = vecImage.begin(); + for (int i = 0; i < (nPixels / 2) - 1; i++) + iter++; // Advance iterator to (nPixels / 2) - 1; + + if (nPixels % 2) { // Odd + iter++; + median = *iter; + } else // Even + median = (*iter++ + *iter) / 2; } +void +ImageFile::getMinMax (double& min, double& max) const +{ + int nx = m_nx; + int ny = m_ny; + ImageFileArrayConst v = getArray(); + + if (v == NULL || nx == 0 || ny == 0) + return; + + min = v[0][0]; + max = v[0][0]; + for (int ix = 0; ix < nx; ix++) { + for (int iy = 0; iy < ny; iy++) { + if (v[ix][iy] > max) + max = v[ix][iy]; + if (v[ix][iy] < min) + min = v[ix][iy]; + } + } +} + void ImageFile::writeImagePGM (const char *outfile, int nxcell, int nycell, double densmin, double densmax) {