** This is part of the CTSim program
** Copyright (C) 1983-2000 Kevin Rosenberg
**
-** $Id: imagefile.cpp,v 1.6 2000/06/28 15:25:34 kevin Exp $
+** $Id: imagefile.cpp,v 1.25 2000/12/29 15:45:06 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
#include "ct.h"
+F32Image::F32Image (int nx, int ny)\r
+: Array2dFile (nx, ny, sizeof(kfloat32), Array2dFile::PIXEL_FLOAT32)\r
+{\r
+}\r
+\r
+F32Image::F32Image (void)\r
+: Array2dFile()\r
+{\r
+ setPixelFormat (Array2dFile::PIXEL_FLOAT32);\r
+ setPixelSize (sizeof(kfloat32));\r
+}\r
+\r
+F64Image::F64Image (int nx, int ny)\r
+: Array2dFile (nx, ny, sizeof(kfloat64), Array2dFile::PIXEL_FLOAT64)\r
+{\r
+}\r
+\r
+F64Image::F64Image (void)\r
+: Array2dFile ()\r
+{\r
+ setPixelFormat (PIXEL_FLOAT64);\r
+ setPixelSize (sizeof(kfloat64));\r
+}\r
void
ImageFile::filterResponse (const char* const domainName, double bw, const char* const filterName, double filt_param)
{
- int hx = (m_nx - 1) / 2;
- int hy = (m_ny - 1) / 2;
- ImageFileArray v = getArray();
- SignalFilter filter (filterName, domainName, bw, filt_param);
-
- for (int i = -hx; i <= hx; i++) {
- for (int j = -hy; j <= hy; j++) {
- double r = sqrt (i * i + j * j);
-
- v[i+hx][j+hy] = filter.response (r);
- }
- }
+ int hx = (m_nx - 1) / 2;
+ int hy = (m_ny - 1) / 2;
+ ImageFileArray v = getArray();
+ SignalFilter filter (filterName, domainName, bw, filt_param);
+
+ for (int i = -hx; i <= hx; i++) {
+ for (int j = -hy; j <= hy; j++) {
+ double r = ::sqrt (i * i + j * j);
+
+ v[i+hx][j+hy] = filter.response (r);
+ }
+ }
}
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);
+ 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<int>((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<int>((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);
-
+#else
+ return 0;
#endif
}
ImageFile::comparativeStatistics (const ImageFile& imComp, double& d, double& r, double& e) const
{
if (imComp.nx() != m_nx && imComp.ny() != m_ny) {
- sys_error (ERR_WARNING, "Image sizes differ [ImageFile::comparativeStatistics]");
- return false;
+ sys_error (ERR_WARNING, "Image sizes differ [ImageFile::comparativeStatistics]");
+ 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++) {
- myMean += v[ix][iy];
- }
+ for (unsigned int ix = 0; ix < m_nx; ix++) {
+ for (unsigned int iy = 0; iy < m_ny; iy++) {
+ myMean += v[ix][iy];
+ }
}
myMean /= (m_nx * m_ny);
-
+
double sqErrorSum = 0.;
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++) {
- double diff = v[ix][iy] - vComp[ix][iy];
- sqErrorSum += diff * diff;
- absErrorSum += fabs(diff);
- double diffFromMean = v[ix][iy] - myMean;
- sqDiffFromMeanSum += diffFromMean * diffFromMean;
- absValueSum += fabs(v[ix][iy]);
- }
+ for (unsigned int ix2 = 0; ix2 < m_nx; ix2++) {
+ for (unsigned int iy = 0; iy < m_ny; iy++) {
+ double diff = v[ix2][iy] - vComp[ix2][iy];
+ sqErrorSum += diff * diff;
+ absErrorSum += fabs(diff);
+ double diffFromMean = v[ix2][iy] - myMean;
+ sqDiffFromMeanSum += diffFromMean * diffFromMean;
+ absValueSum += fabs(v[ix2][iy]);
+ }
}
-
- d = sqrt (sqErrorSum / sqDiffFromMeanSum);
+
+ d = ::sqrt (sqErrorSum / sqDiffFromMeanSum);
r = absErrorSum / absValueSum;
-
+
int hx = m_nx / 2;
int hy = m_ny / 2;
double eMax = -1;
- for (int ix = 0; ix < hx; ix++) {
- for (int iy = 0; iy < hy; iy++) {
- 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]);
- 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]);
- double error = fabs (avgPixel - avgPixelComp);
- if (error > eMax)
- eMax = error;
- }
+ for (int ix3 = 0; ix3 < hx; ix3++) {
+ for (int iy = 0; iy < hy; iy++) {
+ 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]);
+ 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]);
+ double error = fabs (avgPixel - avgPixelComp);
+ if (error > eMax)
+ eMax = error;
+ }
}
-
+
e = eMax;
-
+
return true;
}
bool
-ImageFile::printComparativeStatistics (const ImageFile& imComp, ostream& os) const
+ImageFile::printComparativeStatistics (const ImageFile& imComp, std::ostream& os) const
{
- double d, r, e;
-
- if (comparativeStatistics (imComp, d, r, e)) {
- 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;
- }
+ double d, r, e;
+
+ if (comparativeStatistics (imComp, d, r, e)) {
+ os << " Normalized root mean squared distance (d): " << d << std::endl;
+ os << " Normalized mean absolute distance (r): " << r << std::endl;
+ os << "Worst case distance (2x2 pixel average) (e): " << e << std::endl;
+ return true;
+ }
+ return false;
}
void
-ImageFile::printStatistics (ostream& os) const
+ImageFile::printStatistics (std::ostream& os) const
{
double min, max, mean, mode, median, stddev;
-
+
statistics (min, max, mean, mode, median, stddev);
-
- os << " min: " << min << endl;
- os << " max: " << max << endl;
- os << " mean: " << mean << endl;
- os << " mode: " << mode << endl;
- os << "median: " << median << endl;
- os << "stddev: " << stddev << endl;
+
+ os << " min: " << min << std::endl;
+ os << " max: " << max << std::endl;
+ os << " mean: " << mean << std::endl;
+ os << " mode: " << mode << std::endl;
+ os << "median: " << median << std::endl;
+ os << "stddev: " << stddev << std::endl;
}
int ny = m_ny;
ImageFileArrayConst v = getArray();
- mean = 0;
+ if (v == NULL || nx == 0 || ny == 0)
+ return;
+\r
+ std::vector<double> vecImage;\r
+ int iVec = 0;\r
+ vecImage.resize (nx * ny);\r
+ for (int ix = 0; ix < nx; ix++) {\r
+ for (int iy = 0; iy < ny; iy++)\r
+ vecImage[iVec++] = v[ix][iy];\r
+ }\r
+\r
+ vectorNumericStatistics (vecImage, nx * ny, min, max, mean, mode, median, stddev);\r
+}
+
+
+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];
- mean += v[ix][iy];
- }
+ 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];
+ }
}
- mean /= (nx * ny);
+}
+\r
+bool\r
+ImageFile::subtractImages (const ImageFile& rRHS, ImageFile& result) const\r
+{\r
+ if (m_nx != rRHS.nx() || m_ny != rRHS.ny() || m_nx != result.nx() || m_ny != result.ny()) {\r
+ sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::subtractImage]");\r
+ return false;\r
+ }\r
+\r
+ ImageFileArrayConst vLHS = getArray();\r
+ ImageFileArrayConst vRHS = rRHS.getArray();\r
+ ImageFileArray vResult = result.getArray();\r
+\r
+ for (int ix = 0; ix < m_nx; ix++) {\r
+ ImageFileColumnConst in1 = vLHS[ix];\r
+ ImageFileColumnConst in2 = vRHS[ix];\r
+ ImageFileColumn out = vResult[ix];\r
+ for (int iy = 0; iy < m_ny; iy++)\r
+ *out++ = *in1++ - *in2++;\r
+ }\r
+\r
+ return true;\r
+}\r
+
+bool\r
+ImageFile::addImages (const ImageFile& rRHS, ImageFile& result) const\r
+{\r
+ if (m_nx != rRHS.nx() || m_ny != rRHS.ny() || m_nx != result.nx() || m_ny != result.ny()) {\r
+ sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::subtractImage]");\r
+ return false;\r
+ }\r
+\r
+ ImageFileArrayConst vLHS = getArray();\r
+ ImageFileArrayConst vRHS = rRHS.getArray();\r
+ ImageFileArray vResult = result.getArray();\r
+\r
+ for (int ix = 0; ix < m_nx; ix++) {\r
+ ImageFileColumnConst in1 = vLHS[ix];\r
+ ImageFileColumnConst in2 = vRHS[ix];\r
+ ImageFileColumn out = vResult[ix];\r
+ for (int iy = 0; iy < m_ny; iy++)\r
+ *out++ = *in1++ + *in2++;\r
+ }\r
+\r
+ return true;\r
+}\r
+\r
+bool\r
+ImageFile::multiplyImages (const ImageFile& rRHS, ImageFile& result) const\r
+{\r
+ if (m_nx != rRHS.nx() || m_ny != rRHS.ny() || m_nx != result.nx() || m_ny != result.ny()) {\r
+ sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::subtractImage]");\r
+ return false;\r
+ }\r
+\r
+ ImageFileArrayConst vLHS = getArray();\r
+ ImageFileArrayConst vRHS = rRHS.getArray();\r
+ ImageFileArray vResult = result.getArray();\r
+\r
+ for (int ix = 0; ix < m_nx; ix++) {\r
+ ImageFileColumnConst in1 = vLHS[ix];\r
+ ImageFileColumnConst in2 = vRHS[ix];\r
+ ImageFileColumn out = vResult[ix];\r
+ for (int iy = 0; iy < m_ny; iy++)\r
+ *out++ = *in1++ * *in2++;\r
+ }\r
+\r
+ return true;\r
+}\r
+\r
+bool\r
+ImageFile::divideImages (const ImageFile& rRHS, ImageFile& result) const\r
+{\r
+ if (m_nx != rRHS.nx() || m_ny != rRHS.ny() || m_nx != result.nx() || m_ny != result.ny()) {\r
+ sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::subtractImage]");\r
+ return false;\r
+ }\r
+\r
+ ImageFileArrayConst vLHS = getArray();\r
+ ImageFileArrayConst vRHS = rRHS.getArray();\r
+ ImageFileArray vResult = result.getArray();\r
+\r
+ for (int ix = 0; ix < m_nx; ix++) {\r
+ ImageFileColumnConst in1 = vLHS[ix];\r
+ ImageFileColumnConst in2 = vRHS[ix];\r
+ ImageFileColumn out = vResult[ix];\r
+ for (int iy = 0; iy < m_ny; iy++) {\r
+ if (*in2 != 0.)\r
+ *out++ = *in1++ / *in2++;\r
+ else\r
+ *out++ = 0;\r
+ }\r
+ }\r
+\r
+ return true;\r
+}\r
+\r
+\r
+bool\r
+ImageFile::invertPixelValues (ImageFile& result) const\r
+{\r
+ if (m_nx != result.nx() || m_ny != result.ny()) {\r
+ sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");\r
+ return false;\r
+ }\r
+\r
+ ImageFileArrayConst vLHS = getArray();\r
+ ImageFileArray vResult = result.getArray();\r
+\r
+ for (int ix = 0; ix < m_nx; ix++) {\r
+ ImageFileColumnConst in = vLHS[ix];\r
+ ImageFileColumn out = vResult[ix];\r
+ for (int iy = 0; iy < m_ny; iy++)\r
+ *out++ = - *in++;\r
+ }\r
+\r
+ return true;\r
+}\r
+\r
+bool\r
+ImageFile::sqrt (ImageFile& result) const\r
+{\r
+ if (m_nx != result.nx() || m_ny != result.ny()) {\r
+ sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");\r
+ return false;\r
+ }\r
+\r
+ ImageFileArrayConst vLHS = getArray();\r
+ ImageFileArray vResult = result.getArray();\r
+\r
+ for (int ix = 0; ix < m_nx; ix++) {\r
+ ImageFileColumnConst in = vLHS[ix];\r
+ ImageFileColumn out = vResult[ix];\r
+ for (int iy = 0; iy < m_ny; iy++)\r
+ if (*in < 0)\r
+ *out++ = -::sqrt(-*in++);\r
+ else\r
+ *out++ = ::sqrt(*in++);\r
+ }\r
+\r
+ return true;\r
+}\r
+\r
+bool\r
+ImageFile::log (ImageFile& result) const\r
+{\r
+ if (m_nx != result.nx() || m_ny != result.ny()) {\r
+ sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");\r
+ return false;\r
+ }\r
+\r
+ ImageFileArrayConst vLHS = getArray();\r
+ ImageFileArray vResult = result.getArray();\r
+\r
+ for (int ix = 0; ix < m_nx; ix++) {\r
+ ImageFileColumnConst in = vLHS[ix];\r
+ ImageFileColumn out = vResult[ix];\r
+ for (int iy = 0; iy < m_ny; iy++)\r
+ if (*in <= 0)\r
+ *out++ = 0;\r
+ else\r
+ *out++ = ::log(*in++);\r
+ }\r
+\r
+ return true;\r
+}\r
+\r
+bool\r
+ImageFile::exp (ImageFile& result) const\r
+{\r
+ if (m_nx != result.nx() || m_ny != result.ny()) {\r
+ sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");\r
+ return false;\r
+ }\r
+\r
+ ImageFileArrayConst vLHS = getArray();\r
+ ImageFileArray vResult = result.getArray();\r
+\r
+ for (int ix = 0; ix < m_nx; ix++) {\r
+ ImageFileColumnConst in = vLHS[ix];\r
+ ImageFileColumn out = vResult[ix];\r
+ for (int iy = 0; iy < m_ny; iy++)\r
+ *out++ = ::exp (*in++);\r
+ }\r
+\r
+ return true;\r
+}\r
+\r
+bool\r
+ImageFile::FFTMagnitude (ImageFile& result) const\r
+{\r
+ if (m_nx != result.nx() || m_ny != result.ny()) {\r
+ sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");\r
+ return false;\r
+ }\r
+\r
+ ImageFileArrayConst vLHS = getArray();\r
+ ImageFileArray vResult = result.getArray();\r
+\r
+ int ix, iy;\r
+ double* pY = new double [m_ny];\r
+ std::complex<double>** complexOut = new std::complex<double>* [m_nx];\r
+ for (ix = 0; ix < m_nx; ix++)\r
+ complexOut[ix] = new std::complex<double> [m_ny];\r
+\r
+ for (ix = 0; ix < m_nx; ix++) {\r
+ for (iy = 0; iy < m_ny; iy++)\r
+ pY[iy] = vLHS[ix][iy];\r
+ ProcessSignal::finiteFourierTransform (pY, complexOut[ix], m_ny, ProcessSignal::FORWARD);\r
+ }\r
+ delete pY;\r
+\r
+ std::complex<double>* pX = new std::complex<double> [m_nx];\r
+ std::complex<double>* complexOutCol = new std::complex<double> [m_nx];\r
+ for (iy = 0; iy < m_ny; iy++) {\r
+ for (ix = 0; ix < m_nx; ix++)\r
+ pX[ix] = complexOut[ix][iy];\r
+ ProcessSignal::finiteFourierTransform (pX, complexOutCol, m_nx, ProcessSignal::FORWARD);\r
+ for (ix = 0; ix < m_nx; ix++)\r
+ complexOut[ix][iy] = complexOutCol[ix];\r
+ }\r
+ delete [] pX;\r
+\r
+ for (ix = 0; ix < m_nx; ix++)\r
+ ProcessSignal::shuffleFourierToNaturalOrder (complexOut[ix], m_ny);\r
+\r
+ \r
+ for (iy = 0; iy < m_ny; iy++) {\r
+ for (ix = 0; ix < m_nx; ix++)\r
+ complexOutCol[ix] = complexOut[ix][iy];\r
+ ProcessSignal::shuffleFourierToNaturalOrder (complexOutCol, m_nx);;\r
+ for (ix = 0; ix < m_nx; ix++)\r
+ complexOut[ix][iy] = complexOutCol[ix];\r
+\r
+ }\r
+ delete [] complexOutCol;\r
+\r
+ for (ix = 0; ix < m_nx; ix++)\r
+ for (iy = 0; iy < m_ny; iy++)\r
+ vResult[ix][iy] = std::abs (complexOut[ix][iy]);\r
+\r
+ for (ix = 0; ix < m_nx; ix++)\r
+ delete [] complexOut[ix];\r
+\r
+ delete [] complexOut;\r
+\r
+ return true;\r
+}\r
+\r
+bool\r
+ImageFile::FFTPhase (ImageFile& result) const\r
+{\r
+ if (m_nx != result.nx() || m_ny != result.ny()) {\r
+ sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");\r
+ return false;\r
+ }\r
+\r
+ ImageFileArrayConst vLHS = getArray();\r
+ ImageFileArray vResult = result.getArray();\r
+\r
+ int ix, iy;\r
+ double* pY = new double [m_ny];\r
+ std::complex<double>** complexOut = new std::complex<double>* [m_nx];\r
+ for (ix = 0; ix < m_nx; ix++)\r
+ complexOut[ix] = new std::complex<double> [m_ny];\r
+\r
+ for (ix = 0; ix < m_nx; ix++) {\r
+ for (iy = 0; iy < m_ny; iy++)\r
+ pY[iy] = vLHS[ix][iy];\r
+ ProcessSignal::finiteFourierTransform (pY, complexOut[ix], m_ny, ProcessSignal::FORWARD);\r
+ }\r
+ delete pY;\r
+\r
+ std::complex<double>* pX = new std::complex<double> [m_nx];\r
+ std::complex<double>* complexOutCol = new std::complex<double> [m_nx];\r
+ for (iy = 0; iy < m_ny; iy++) {\r
+ for (ix = 0; ix < m_nx; ix++)\r
+ pX[ix] = complexOut[ix][iy];\r
+ ProcessSignal::finiteFourierTransform (pX, complexOutCol, m_nx, ProcessSignal::FORWARD);\r
+ for (ix = 0; ix < m_nx; ix++)\r
+ complexOut[ix][iy] = complexOutCol[ix];\r
+ }\r
+ delete [] pX;\r
+\r
+ for (ix = 0; ix < m_nx; ix++)\r
+ ProcessSignal::shuffleFourierToNaturalOrder (complexOut[ix], m_ny);\r
+\r
+ \r
+ for (iy = 0; iy < m_ny; iy++) {\r
+ for (ix = 0; ix < m_nx; ix++)\r
+ complexOutCol[ix] = complexOut[ix][iy];\r
+ ProcessSignal::shuffleFourierToNaturalOrder (complexOutCol, m_nx);;\r
+ for (ix = 0; ix < m_nx; ix++)\r
+ complexOut[ix][iy] = complexOutCol[ix];\r
+\r
+ }\r
+ delete [] complexOutCol;\r
+\r
+ for (ix = 0; ix < m_nx; ix++)\r
+ for (iy = 0; iy < m_ny; iy++)\r
+ vResult[ix][iy] = atan (complexOut[ix][iy].imag / complexOut[ix][iy].real);\r
+\r
+ for (ix = 0; ix < m_nx; ix++)\r
+ delete [] complexOut[ix];\r
+\r
+ delete [] complexOut;\r
+\r
+ return true;\r
+}\r
+\r
+bool\r
+ImageFile::square (ImageFile& result) const\r
+{\r
+ if (m_nx != result.nx() || m_ny != result.ny()) {\r
+ sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");\r
+ return false;\r
+ }\r
+\r
+ ImageFileArrayConst vLHS = getArray();\r
+ ImageFileArray vResult = result.getArray();\r
+\r
+ for (int ix = 0; ix < m_nx; ix++) {\r
+ ImageFileColumnConst in = vLHS[ix];\r
+ ImageFileColumn out = vResult[ix];\r
+ for (int iy = 0; iy < m_ny; iy++) {\r
+ *out++ = *in * *in;\r
+ in++;\r
+ }\r
+ }\r
+\r
+ return true;\r
+}\r
+\r
+\r
+void
+ImageFile::writeImagePGM (const char *outfile, int nxcell, int nycell, double densmin, double densmax)
+{
+ FILE *fp;
+ int nx = m_nx;
+ int ny = m_ny;
+ ImageFileArray v = getArray();
+
+ unsigned char* rowp = new unsigned char [nx * nxcell];
+
+ if ((fp = fopen (outfile, "wb")) == NULL)
+ return;
+
+ fprintf(fp, "P5\n");
+ fprintf(fp, "%d %d\n", nx, ny);
+ fprintf(fp, "255\n");
+
+ for (int irow = ny - 1; irow >= 0; irow--) {
+ for (int icol = 0; icol < nx; icol++) {
+ int pos = icol * nxcell;
+ double dens = (v[icol][irow] - densmin) / (densmax - densmin);
+ dens = clamp (dens, 0., 1.);
+ for (int p = pos; p < pos + nxcell; p++) {
+ rowp[p] = static_cast<unsigned int> (dens * 255.);
+ }
+ }
+ for (int ir = 0; ir < nycell; ir++) {
+ for (int ic = 0; ic < nx * nxcell; ic++)
+ fputc( rowp[ic], fp );
+ }
+ }
+ \r
+ delete rowp;
+ fclose(fp);
+}
- static const int nbin = 256;
- int hist[ nbin ] = {0};
- double spread = max - min;
- mode = 0;
- stddev = 0;
- for (int ix = 0; ix < nx; ix++) {
- for (int iy = 0; iy < ny; iy++) {
- int b = static_cast<int>((((v[ix][iy] - min) / spread) * (nbin - 1)) + 0.5);
- hist[b]++;
- double diff = (v[ix][iy] - mean);
- stddev += diff * diff;
+void
+ImageFile::writeImagePGMASCII (const char *outfile, int nxcell, int nycell, double densmin, double densmax)
+{
+ FILE *fp;
+ int nx = m_nx;
+ int ny = m_ny;
+ ImageFileArray v = getArray();
+
+ unsigned char* rowp = new unsigned char [nx * nxcell];
+
+ if ((fp = fopen (outfile, "wb")) == NULL)
+ return;
+
+ fprintf(fp, "P2\n");
+ fprintf(fp, "%d %d\n", nx, ny);
+ fprintf(fp, "255\n");
+
+ for (int irow = ny - 1; irow >= 0; irow--) {
+ for (int icol = 0; icol < nx; icol++) {
+ int pos = icol * nxcell;
+ double dens = (v[icol][irow] - densmin) / (densmax - densmin);
+ dens = clamp (dens, 0., 1.);
+ for (int p = pos; p < pos + nxcell; p++) {
+ rowp[p] = static_cast<unsigned int> (dens * 255.);
+ }
+ }
+ for (int ir = 0; ir < nycell; ir++) {
+ for (int ic = 0; ic < nx * nxcell; ic++)
+ fprintf(fp, "%d ", rowp[ic]);
+ fprintf(fp, "\n");
+ }
}
- }
- stddev = sqrt(stddev / (nx * ny));
-
- int max_binindex = 0;
- int max_bin = -1;
- for (int ibin = 0; ibin < nbin; ibin++) {
- if (hist[ibin] > max_bin) {
- max_bin = hist[ibin];
- max_binindex = ibin;
- }
- }
+ \r
+ delete rowp;
+ fclose(fp);
+}
+
- mode = (max_binindex * spread / (nbin - 1)) + min;
-
- median = 0.;
+#ifdef HAVE_PNG
+void
+ImageFile::writeImagePNG (const char *outfile, int bitdepth, int nxcell, int nycell, double densmin, double densmax)
+{
+ FILE *fp;
+ png_structp png_ptr;
+ png_infop info_ptr;
+ double max_out_level = (1 << bitdepth) - 1;
+ int nx = m_nx;
+ int ny = m_ny;
+ ImageFileArray v = getArray();
+
+ unsigned char* rowp = new unsigned char [nx * nxcell * (bitdepth / 8)];
+
+ if ((fp = fopen (outfile, "wb")) == NULL)
+ return;
+
+ png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
+ if (! png_ptr)
+ return;
+
+ info_ptr = png_create_info_struct(png_ptr);
+ if (! info_ptr) {
+ png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
+ fclose(fp);
+ return;
+ }
+
+ if (setjmp(png_ptr->jmpbuf)) {
+ png_destroy_write_struct(&png_ptr, &info_ptr);
+ fclose(fp);
+ return;
+ }
+
+ png_init_io(png_ptr, fp);
+
+ 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);
+
+ png_write_info(png_ptr, info_ptr);
+ for (int irow = ny - 1; irow >= 0; irow--) {
+ png_bytep row_pointer = rowp;
+
+ for (int icol = 0; icol < nx; icol++) {
+ int pos = icol * nxcell;
+ double dens = (v[icol][irow] - densmin) / (densmax - densmin);
+ dens = clamp (dens, 0., 1.);
+ unsigned int outval = static_cast<unsigned int> (dens * max_out_level);
+
+ for (int p = pos; p < pos + nxcell; p++) {
+ if (bitdepth == 8)
+ rowp[p] = outval;
+ else {
+ int rowpos = p * 2;
+ rowp[rowpos] = (outval >> 8) & 0xFF;
+ rowp[rowpos+1] = (outval & 0xFF);
+ }
+ }
+ }
+ for (int ir = 0; ir < nycell; ir++)
+ png_write_rows (png_ptr, &row_pointer, 1);
+ }
+
+ png_write_end(png_ptr, info_ptr);
+ png_destroy_write_struct(&png_ptr, &info_ptr);
+ delete rowp;\r
+
+ fclose(fp);
}
+#endif
+#ifdef HAVE_GD
+#include "gd.h"
+static const int N_GRAYSCALE=256;
+
+void
+ImageFile::writeImageGIF (const char *outfile, int nxcell, int nycell, double densmin, double densmax)
+{
+ gdImagePtr gif;
+ FILE *out;
+ int gs_indices[N_GRAYSCALE];
+ int nx = m_nx;
+ int ny = m_ny;
+ ImageFileArray v = getArray();
+
+ unsigned char rowp [nx * nxcell];
+ if (rowp == NULL)
+ return;
+
+ gif = gdImageCreate(nx * nxcell, ny * nycell);
+ for (int i = 0; i < N_GRAYSCALE; i++)
+ gs_indices[i] = gdImageColorAllocate(gif, i, i, i);
+
+ int lastrow = ny * nycell - 1;
+ for (int irow = 0; irow < ny; irow++) {
+ int rpos = irow * nycell;
+ for (int ir = rpos; ir < rpos + nycell; ir++) {
+ for (int icol = 0; icol < nx; icol++) {
+ int cpos = icol * nxcell;
+ double dens = (v[icol][irow] - densmin) / (densmax - densmin);
+ dens = clamp(dens, 0., 1.);
+ for (int ic = cpos; ic < cpos + nxcell; ic++) {
+ rowp[ic] = (unsigned int) (dens * (double) (N_GRAYSCALE - 1));
+ gdImageSetPixel(gif, ic, lastrow - ir, gs_indices[rowp[ic]]);
+ }
+ }
+ }
+ }
+
+ if ((out = fopen(outfile,"w")) == NULL) {
+ sys_error(ERR_FATAL, "Error opening output file %s for writing", outfile);
+ return (1);
+ }
+ gdImageGif(gif,out);
+ fclose(out);
+ gdImageDestroy(gif);
+}
+#endif