X-Git-Url: http://git.kpe.io/?p=ctsim.git;a=blobdiff_plain;f=libctsim%2Fimagefile.cpp;h=0cbd67826ae194750b64cb8df9a70f3dbbdaeeac;hp=df647ce930efad8de0b4a8288f14c99499fc71bb;hb=9b2bb510160bdb56f04847f5b55ab61dd8a47976;hpb=7ec2cd66921180a624813dff9f8bac76c6b268cc;ds=sidebyside diff --git a/libctsim/imagefile.cpp b/libctsim/imagefile.cpp index df647ce..0cbd678 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.28 2001/01/01 10:14:34 kevin Exp $ +** $Id: imagefile.cpp,v 1.29 2001/01/02 05:34:57 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 @@ -27,6 +27,35 @@ #include "ct.h" +const int ImageFile::FORMAT_INVALID = -1; +const int ImageFile::FORMAT_PGM = 0; +const int ImageFile::FORMAT_PGMASCII = 1; +#ifdef HAVE_PNG +const int ImageFile::FORMAT_PNG = 2; +const int ImageFile::FORMAT_PNG16 = 3; +#endif + +const char* ImageFile::s_aszFormatName[] = +{ + {"pgm"}, + {"pgmascii"}, +#ifdef HAVE_PNG + {"png"}, + {"png16"}, +#endif +}; + +const char* ImageFile::s_aszFormatTitle[] = +{ + {"PGM"}, + {"PGM ASCII"}, + {"PNG"}, + {"PNG 16-bit"}, +}; + +const int ImageFile::s_iFormatCount = sizeof(s_aszFormatName) / sizeof(const char*); + + F32Image::F32Image (int nx, int ny, int dataType) : Array2dFile (nx, ny, sizeof(kfloat32), Array2dFile::PIXEL_FLOAT32, dataType) @@ -55,7 +84,7 @@ F64Image::F64Image (void) } void -ImageFile::filterResponse (const char* const domainName, double bw, const char* const filterName, double filt_param) +ImageFile::filterResponse (const char* const domainName, double bw, const char* const filterName, double filt_param, double dInputScale, double dOutputScale) { ImageFileArray v = getArray(); SignalFilter filter (filterName, domainName, bw, filt_param); @@ -73,9 +102,9 @@ ImageFile::filterResponse (const char* const domainName, double bw, const char* for (int ix = 0; ix < m_nx; ix++) for (int iy = 0; iy < m_ny; iy++) { - long lD2 = (ix - iXCenter) * (ix - iXCenter) + (iy - iYCenter) * (iy - iYCenter); - double r = ::sqrt (static_cast(lD2)); - v[ix][iy] = filter.response (r); + long lD2 = ((ix - iXCenter) * (ix - iXCenter)) + ((iy - iYCenter) * (iy - iYCenter)); + double r = ::sqrt (static_cast(lD2)) * dInputScale; + v[ix][iy] = filter.response (r) * dOutputScale; } #else int hx = (m_nx - 1) / 2; @@ -356,18 +385,29 @@ ImageFile::subtractImages (const ImageFile& rRHS, ImageFile& result) const return false; } + if (isComplex() || rRHS.isComplex() && ! result.isComplex()) + result.convertRealToComplex(); + ImageFileArrayConst vLHS = getArray(); + ImageFileArrayConst vLHSImag = getImaginaryArray(); ImageFileArrayConst vRHS = rRHS.getArray(); + ImageFileArrayConst vRHSImag = rRHS.getImaginaryArray(); ImageFileArray vResult = result.getArray(); + ImageFileArray vResultImag = result.getImaginaryArray(); for (unsigned int ix = 0; ix < m_nx; ix++) { - ImageFileColumnConst in1 = vLHS[ix]; - ImageFileColumnConst in2 = vRHS[ix]; - ImageFileColumn out = vResult[ix]; - for (unsigned int iy = 0; iy < m_ny; iy++) - *out++ = *in1++ - *in2++; + for (unsigned int iy = 0; iy < m_ny; iy++) { + vResult[ix][iy] = vLHS[ix][iy] - vRHS[ix][iy]; + if (result.isComplex()) { + vResultImag[ix][iy] = 0; + if (isComplex()) + vResultImag[ix][iy] += vLHSImag[ix][iy]; + if (rRHS.isComplex()) + vResultImag[ix][iy] -= vRHSImag[ix][iy]; + } + } } - + return true; } @@ -379,16 +419,27 @@ ImageFile::addImages (const ImageFile& rRHS, ImageFile& result) const return false; } + if (isComplex() || rRHS.isComplex() && ! result.isComplex()) + result.convertRealToComplex(); + ImageFileArrayConst vLHS = getArray(); + ImageFileArrayConst vLHSImag = getImaginaryArray(); ImageFileArrayConst vRHS = rRHS.getArray(); + ImageFileArrayConst vRHSImag = rRHS.getImaginaryArray(); ImageFileArray vResult = result.getArray(); + ImageFileArray vResultImag = result.getImaginaryArray(); for (unsigned int ix = 0; ix < m_nx; ix++) { - ImageFileColumnConst in1 = vLHS[ix]; - ImageFileColumnConst in2 = vRHS[ix]; - ImageFileColumn out = vResult[ix]; - for (unsigned int iy = 0; iy < m_ny; iy++) - *out++ = *in1++ + *in2++; + for (unsigned int iy = 0; iy < m_ny; iy++) { + vResult[ix][iy] = vLHS[ix][iy] + vRHS[ix][iy]; + if (result.isComplex()) { + vResultImag[ix][iy] = 0; + if (isComplex()) + vResultImag[ix][iy] += vLHSImag[ix][iy]; + if (rRHS.isComplex()) + vResultImag[ix][iy] += vRHSImag[ix][iy]; + } + } } return true; @@ -402,18 +453,34 @@ ImageFile::multiplyImages (const ImageFile& rRHS, ImageFile& result) const return false; } + if (isComplex() || rRHS.isComplex() && ! result.isComplex()) + result.convertRealToComplex(); + ImageFileArrayConst vLHS = getArray(); + ImageFileArrayConst vLHSImag = getImaginaryArray(); ImageFileArrayConst vRHS = rRHS.getArray(); + ImageFileArrayConst vRHSImag = rRHS.getImaginaryArray(); ImageFileArray vResult = result.getArray(); + ImageFileArray vResultImag = result.getImaginaryArray(); for (unsigned int ix = 0; ix < m_nx; ix++) { - ImageFileColumnConst in1 = vLHS[ix]; - ImageFileColumnConst in2 = vRHS[ix]; - ImageFileColumn out = vResult[ix]; - for (unsigned int iy = 0; iy < m_ny; iy++) - *out++ = *in1++ * *in2++; + for (unsigned int iy = 0; iy < m_ny; iy++) { + if (result.isComplex()) { + std::complex cLHS (vLHS[ix][iy], 0); + if (isComplex()) + cLHS.imag (vLHSImag[ix][iy]); + std::complex cRHS (vRHS[ix][iy], 0); + if (rRHS.isComplex()) + cRHS.imag (vRHSImag[ix][iy]); + std::complex cResult = cLHS * cRHS; + vResult[ix][iy] = cResult.real(); + vResultImag[ix][iy] = cResult.imag(); + } else + vResult[ix][iy] = vLHS[ix][iy] * vRHS[ix][iy]; + } } + return true; } @@ -425,22 +492,38 @@ ImageFile::divideImages (const ImageFile& rRHS, ImageFile& result) const return false; } + if (isComplex() || rRHS.isComplex() && ! result.isComplex()) + result.convertRealToComplex(); + ImageFileArrayConst vLHS = getArray(); + ImageFileArrayConst vLHSImag = getImaginaryArray(); ImageFileArrayConst vRHS = rRHS.getArray(); + ImageFileArrayConst vRHSImag = rRHS.getImaginaryArray(); ImageFileArray vResult = result.getArray(); + ImageFileArray vResultImag = result.getImaginaryArray(); for (unsigned int ix = 0; ix < m_nx; ix++) { - ImageFileColumnConst in1 = vLHS[ix]; - ImageFileColumnConst in2 = vRHS[ix]; - ImageFileColumn out = vResult[ix]; for (unsigned int iy = 0; iy < m_ny; iy++) { - if (*in2 != 0.) - *out++ = *in1++ / *in2++; - else - *out++ = 0; + if (result.isComplex()) { + std::complex cLHS (vLHS[ix][iy], 0); + if (isComplex()) + cLHS.imag (vLHSImag[ix][iy]); + std::complex cRHS (vRHS[ix][iy], 0); + if (rRHS.isComplex()) + cRHS.imag (vRHSImag[ix][iy]); + std::complex cResult = cLHS / cRHS; + vResult[ix][iy] = cResult.real(); + vResultImag[ix][iy] = cResult.imag(); + } else { + if (vRHS != 0) + vResult[ix][iy] = vLHS[ix][iy] / vRHS[ix][iy]; + else + vResult[ix][iy] = 0; + } } } + return true; } @@ -453,6 +536,9 @@ ImageFile::invertPixelValues (ImageFile& result) const return false; } + if (isComplex() && ! result.isComplex()) + result.convertRealToComplex(); + ImageFileArrayConst vLHS = getArray(); ImageFileArray vResult = result.getArray(); @@ -473,20 +559,40 @@ ImageFile::sqrt (ImageFile& result) const sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]"); return false; } + + if (isComplex() && ! result.isComplex()) + result.convertRealToComplex(); + bool bComplexOutput = result.isComplex(); ImageFileArrayConst vLHS = getArray(); + if (! bComplexOutput) // check if should convert to complex output + for (unsigned int ix = 0; ix < m_nx; ix++) + for (unsigned int iy = 0; iy < m_ny; iy++) + if (! bComplexOutput && vLHS[ix][iy] < 0) { + result.convertRealToComplex(); + bComplexOutput = true; + break; + } + + ImageFileArrayConst vLHSImag = getImaginaryArray(); ImageFileArray vResult = result.getArray(); + ImageFileArray vResultImag = result.getImaginaryArray(); for (unsigned int ix = 0; ix < m_nx; ix++) { - ImageFileColumnConst in = vLHS[ix]; - ImageFileColumn out = vResult[ix]; - for (unsigned int iy = 0; iy < m_ny; iy++) - if (*in < 0) - *out++ = -::sqrt(-*in++); - else - *out++ = ::sqrt(*in++); + for (unsigned int iy = 0; iy < m_ny; iy++) { + if (result.isComplex()) { + std::complex cLHS (vLHS[ix][iy], 0); + if (isComplex()) + cLHS.imag (vLHSImag[ix][iy]); + std::complex cResult = std::sqrt(cLHS); + vResult[ix][iy] = cResult.real(); + vResultImag[ix][iy] = cResult.imag(); + } else + vResult[ix][iy] = ::sqrt (vLHS[ix][iy]); + } } + return true; } @@ -498,19 +604,29 @@ ImageFile::log (ImageFile& result) const return false; } + if (isComplex() && ! result.isComplex()) + result.convertRealToComplex(); + ImageFileArrayConst vLHS = getArray(); + ImageFileArrayConst vLHSImag = getImaginaryArray(); ImageFileArray vResult = result.getArray(); + ImageFileArray vResultImag = result.getImaginaryArray(); for (unsigned int ix = 0; ix < m_nx; ix++) { - ImageFileColumnConst in = vLHS[ix]; - ImageFileColumn out = vResult[ix]; - for (unsigned int iy = 0; iy < m_ny; iy++) - if (*in <= 0) - *out++ = 0; - else - *out++ = ::log(*in++); + for (unsigned int iy = 0; iy < m_ny; iy++) { + if (result.isComplex()) { + std::complex cLHS (vLHS[ix][iy], 0); + if (isComplex()) + cLHS.imag (vLHSImag[ix][iy]); + std::complex cResult = std::log (cLHS); + vResult[ix][iy] = cResult.real(); + vResultImag[ix][iy] = cResult.imag(); + } else + vResult[ix][iy] = ::log (vLHS[ix][iy]); + } } + return true; } @@ -521,95 +637,69 @@ ImageFile::exp (ImageFile& result) const sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]"); return false; } - + + if (isComplex() && ! result.isComplex()) + result.convertRealToComplex(); + ImageFileArrayConst vLHS = getArray(); + ImageFileArrayConst vLHSImag = getImaginaryArray(); ImageFileArray vResult = result.getArray(); + ImageFileArray vResultImag = result.getImaginaryArray(); for (unsigned int ix = 0; ix < m_nx; ix++) { - ImageFileColumnConst in = vLHS[ix]; - ImageFileColumn out = vResult[ix]; - for (unsigned int iy = 0; iy < m_ny; iy++) - *out++ = ::exp (*in++); + for (unsigned int iy = 0; iy < m_ny; iy++) { + if (result.isComplex()) { + std::complex cLHS (vLHS[ix][iy], 0); + if (isComplex()) + cLHS.imag (vLHSImag[ix][iy]); + std::complex cResult = std::exp (cLHS); + vResult[ix][iy] = cResult.real(); + vResultImag[ix][iy] = cResult.imag(); + } else + vResult[ix][iy] = ::exp (vLHS[ix][iy]); + } } + return true; } -namespace ProcessImage { - -void -shuffleFourierToNaturalOrder (ImageFile& im) +bool +ImageFile::scaleImage (ImageFile& result) const { - ImageFileArray vReal = im.getArray(); - ImageFileArray vImag = im.getImaginaryArray(); - unsigned int ix, iy; - unsigned int nx = im.nx(); - unsigned int ny = im.ny(); - - // shuffle each column - for (ix = 0; ix < nx; ix++) { - ProcessSignal::shuffleFourierToNaturalOrder (vReal[ix], ny); - if (im.isComplex()) - ProcessSignal::shuffleFourierToNaturalOrder (vImag[ix], ny); - } + unsigned int nx = m_nx; + unsigned int ny = m_ny; + unsigned int newNX = result.nx(); + unsigned int newNY = result.ny(); - // shuffle each row - float* pRow = new float [nx]; - for (iy = 0; iy < ny; iy++) { - for (ix = 0; ix < nx; ix++) - pRow[ix] = vReal[ix][iy]; - ProcessSignal::shuffleFourierToNaturalOrder (pRow, nx); - for (ix = 0; ix < nx; ix++) - vReal[ix][iy] = pRow[ix]; - if (im.isComplex()) { - for (ix = 0; ix < nx; ix++) - pRow[ix] = vImag[ix][iy]; - ProcessSignal::shuffleFourierToNaturalOrder (pRow, nx);; - for (ix = 0; ix < nx; ix++) - vImag[ix][iy] = pRow[ix]; - } - } - delete pRow; -} - -void -shuffleNaturalToFourierOrder (ImageFile& im) -{ - ImageFileArray vReal = im.getArray(); - ImageFileArray vImag = im.getImaginaryArray(); - unsigned int ix, iy; - unsigned int nx = im.nx(); - unsigned int ny = im.ny(); - - // shuffle each x column - for (ix = 0; ix < nx; ix++) { - ProcessSignal::shuffleNaturalToFourierOrder (vReal[ix], ny); - if (im.isComplex()) - ProcessSignal::shuffleNaturalToFourierOrder (vImag[ix], ny); - } + double dXScale = static_cast(newNX) / static_cast(nx); + double dYScale = static_cast(newNY) / static_cast(ny); + + if (isComplex() && ! result.isComplex()) + result.convertRealToComplex(); - // shuffle each y row - float* pRow = new float [nx]; - for (iy = 0; iy < ny; iy++) { - for (ix = 0; ix < nx; ix++) - pRow[ix] = vReal[ix][iy]; - ProcessSignal::shuffleNaturalToFourierOrder (pRow, nx); - for (ix = 0; ix < nx; ix++) - vReal[ix][iy] = pRow[ix]; - if (im.isComplex()) { - for (ix = 0; ix < nx; ix++) - pRow[ix] = vImag[ix][iy]; - ProcessSignal::shuffleNaturalToFourierOrder (pRow, nx); - for (ix = 0; ix < nx; ix++) - vImag[ix][iy] = pRow[ix]; + ImageFileArrayConst vReal = getArray(); + ImageFileArrayConst vImag = getImaginaryArray(); + ImageFileArray vResult = result.getArray(); + ImageFileArray vResultImag = result.getImaginaryArray(); + + for (unsigned int ix = 0; ix < newNX; ix++) { + for (unsigned int iy = 0; iy < newNY; iy++) { + unsigned int scaleNX = static_cast (ix / dXScale); + unsigned int scaleNY = static_cast (iy / dYScale); + vResult[ix][iy] = vReal[scaleNX][scaleNY]; + if (result.isComplex()) { + if (isComplex()) + vResultImag[ix][iy] = vImag[scaleNX][scaleNY]; + else + vResultImag[ix][iy] = 0; + } } } - delete [] pRow; + + return true; } - -}; // namespace ProcessIamge - #ifdef HAVE_FFTW bool ImageFile::fft (ImageFile& result) const @@ -620,7 +710,7 @@ ImageFile::fft (ImageFile& result) const } if (result.dataType() == Array2dFile::DATA_TYPE_REAL) { - if (! result.reallocRealToComplex ()) + if (! result.convertRealToComplex ()) return false; } @@ -660,7 +750,7 @@ ImageFile::fft (ImageFile& result) const delete in; - ProcessImage::shuffleFourierToNaturalOrder (result); + Fourier::shuffleFourierToNaturalOrder (result); return true; } @@ -675,7 +765,7 @@ ImageFile::ifft (ImageFile& result) const } if (result.dataType() == Array2dFile::DATA_TYPE_REAL) { - if (! result.reallocRealToComplex ()) + if (! result.convertRealToComplex ()) return false; } @@ -693,7 +783,7 @@ ImageFile::ifft (ImageFile& result) const vImagResult[ix][iy] = 0; } - ProcessImage::shuffleNaturalToFourierOrder (result); + Fourier::shuffleNaturalToFourierOrder (result); fftw_complex* in = new fftw_complex [m_nx * m_ny]; @@ -735,10 +825,9 @@ ImageFile::fourier (ImageFile& result) const return false; } - if (result.dataType() == Array2dFile::DATA_TYPE_REAL) { - if (! result.reallocRealToComplex ()) + if (! result.isComplex()) + if (! result.convertRealToComplex ()) return false; - } ImageFileArrayConst vLHS = getArray(); ImageFileArrayConst vLHSImag = getImaginaryArray(); @@ -787,33 +876,22 @@ ImageFile::fourier (ImageFile& result) const complexOut[ix][iy] = complexOutRow[ix]; } delete [] pX; - - // shuffle each column - for (ix = 0; ix < m_nx; ix++) - ProcessSignal::shuffleFourierToNaturalOrder (complexOut[ix], m_ny); - - // shuffle each row - for (iy = 0; iy < m_ny; iy++) { - for (ix = 0; ix < m_nx; ix++) - complexOutRow[ix] = complexOut[ix][iy]; - ProcessSignal::shuffleFourierToNaturalOrder (complexOutRow, m_nx);; - for (ix = 0; ix < m_nx; ix++) - complexOut[ix][iy] = complexOutRow[ix]; - - } delete [] complexOutRow; - + for (ix = 0; ix < m_nx; ix++) for (iy = 0; iy < m_ny; iy++) { vRealResult[ix][iy] = complexOut[ix][iy].real(); vImagResult[ix][iy] = complexOut[ix][iy].imag(); } - for (ix = 0; ix < m_nx; ix++) - delete [] complexOut[ix]; - delete [] complexOut; + Fourier::shuffleFourierToNaturalOrder (result); + + // delete complexOut matrix + for (ix = 0; ix < m_nx; ix++) + delete [] complexOut[ix]; + delete [] complexOut; - return true; + return true; } bool @@ -825,50 +903,40 @@ ImageFile::inverseFourier (ImageFile& result) const } if (result.dataType() == Array2dFile::DATA_TYPE_REAL) { - if (! result.reallocRealToComplex ()) + if (! result.convertRealToComplex ()) return false; } ImageFileArrayConst vLHSReal = getArray(); ImageFileArrayConst vLHSImag = getImaginaryArray(); + ImageFileArray vRealResult = result.getArray(); + ImageFileArray vImagResult = result.getImaginaryArray(); unsigned int ix, iy; - // alloc 2d complex output matrix CTSimComplex** complexOut = new CTSimComplex* [m_nx]; for (ix = 0; ix < m_nx; ix++) complexOut[ix] = new CTSimComplex [m_ny]; - // put input image into complexOut + // put input image into result for (ix = 0; ix < m_nx; ix++) for (iy = 0; iy < m_ny; iy++) { - complexOut[ix][iy].real (vLHSReal[ix][iy]); + vRealResult[ix][iy] = vLHSReal[ix][iy]; if (isComplex()) - complexOut[ix][iy].imag (vLHSImag[ix][iy]); + vImagResult[ix][iy] = vLHSImag[ix][iy]; else - complexOut[ix][iy].imag (0); + vImagResult[ix][iy] = 0; } - // shuffle each x column - for (ix = 0; ix < m_nx; ix++) - ProcessSignal::shuffleNaturalToFourierOrder (complexOut[ix], m_ny); - - // shuffle each y row - CTSimComplex* pComplexRow = new CTSimComplex [m_nx]; - for (iy = 0; iy < m_ny; iy++) { - for (ix = 0; ix < m_nx; ix++) - pComplexRow[ix] = complexOut[ix][iy]; - ProcessSignal::shuffleNaturalToFourierOrder (pComplexRow, m_nx); - for (ix = 0; ix < m_nx; ix++) - complexOut[ix][iy] = pComplexRow[ix]; - } - delete [] pComplexRow; + Fourier::shuffleNaturalToFourierOrder (result); // ifourier each x column CTSimComplex* pCol = new CTSimComplex [m_ny]; for (ix = 0; ix < m_nx; ix++) { - for (iy = 0; iy < m_ny; iy++) - pCol[iy] = complexOut[ix][iy]; + for (iy = 0; iy < m_ny; iy++) { + pCol[iy].real (vRealResult[ix][iy]); + pCol[iy].imag (vImagResult[ix][iy]); + } ProcessSignal::finiteFourierTransform (pCol, complexOut[ix], m_ny, ProcessSignal::BACKWARD); } delete [] pCol; @@ -886,8 +954,6 @@ ImageFile::inverseFourier (ImageFile& result) const delete [] complexInRow; delete [] complexOutRow; - ImageFileArray vRealResult = result.getArray(); - ImageFileArray vImagResult = result.getImaginaryArray(); for (ix = 0; ix < m_nx; ix++) for (iy = 0; iy < m_ny; iy++) { vRealResult[ix][iy] = complexOut[ix][iy].real(); @@ -913,8 +979,8 @@ ImageFile::magnitude (ImageFile& result) const ImageFileArray vReal = getArray(); ImageFileArray vImag = getImaginaryArray(); - ImageFileArray vRealResult = result.getArray(); + for (unsigned int ix = 0; ix < m_nx; ix++) for (unsigned int iy = 0; iy < m_ny; iy++) { if (isComplex()) @@ -924,7 +990,7 @@ ImageFile::magnitude (ImageFile& result) const } if (result.isComplex()) - result.reallocComplexToReal(); + result.convertComplexToReal(); return true; } @@ -939,8 +1005,8 @@ ImageFile::phase (ImageFile& result) const ImageFileArray vReal = getArray(); ImageFileArray vImag = getImaginaryArray(); - ImageFileArray vRealResult = result.getArray(); + for (unsigned int ix = 0; ix < m_nx; ix++) for (unsigned int iy = 0; iy < m_ny; iy++) { if (isComplex()) @@ -950,7 +1016,7 @@ ImageFile::phase (ImageFile& result) const } if (result.isComplex()) - result.reallocComplexToReal(); + result.convertComplexToReal(); return true; } @@ -963,24 +1029,92 @@ ImageFile::square (ImageFile& result) const return false; } + if (isComplex() && ! result.isComplex()) + result.convertRealToComplex(); + ImageFileArrayConst vLHS = getArray(); + ImageFileArrayConst vLHSImag = getImaginaryArray(); ImageFileArray vResult = result.getArray(); + ImageFileArray vResultImag = result.getImaginaryArray(); for (unsigned int ix = 0; ix < m_nx; ix++) { - ImageFileColumnConst in = vLHS[ix]; - ImageFileColumn out = vResult[ix]; for (unsigned int iy = 0; iy < m_ny; iy++) { - *out++ = *in * *in; - in++; + if (result.isComplex()) { + std::complex cLHS (vLHS[ix][iy], 0); + if (isComplex()) + cLHS.imag (vLHSImag[ix][iy]); + std::complex cResult = cLHS * cLHS; + vResult[ix][iy] = cResult.real(); + vResultImag[ix][iy] = cResult.imag(); + } else + vResult[ix][iy] = vLHS[ix][iy] * vLHS[ix][iy]; } } + return true; } +int +ImageFile::convertFormatNameToID (const char* const formatName) +{ + int formatID = FORMAT_INVALID; -void -ImageFile::writeImagePGM (const char *outfile, int nxcell, int nycell, double densmin, double densmax) + for (int i = 0; i < s_iFormatCount; i++) + if (strcasecmp (formatName, s_aszFormatName[i]) == 0) { + formatID = i; + break; + } + + return (formatID); +} + +const char* +ImageFile::convertFormatIDToName (int formatID) +{ + static const char *formatName = ""; + + if (formatID >= 0 && formatID < s_iFormatCount) + return (s_aszFormatName[formatID]); + + return (formatName); +} + +const char* +ImageFile::convertFormatIDToTitle (const int formatID) +{ + static const char *formatTitle = ""; + + if (formatID >= 0 && formatID < s_iFormatCount) + return (s_aszFormatTitle[formatID]); + + return (formatTitle); +} + +bool +ImageFile::exportImage (const char* const pszFormat, const char* const pszFilename, int nxcell, int nycell, double densmin, double densmax) +{ + int iFormatID = convertFormatNameToID (pszFormat); + if (iFormatID == FORMAT_INVALID) { + sys_error (ERR_SEVERE, "Invalid format %s [ImageFile::exportImage]", pszFormat); + return false; + } + + if (iFormatID == FORMAT_PGM) + return writeImagePGM (pszFilename, nxcell, nycell, densmin, densmax); + else if (iFormatID == FORMAT_PGMASCII) + return writeImagePGMASCII (pszFilename, nxcell, nycell, densmin, densmax); + else if (iFormatID == FORMAT_PNG) + return writeImagePNG (pszFilename, 8, nxcell, nycell, densmin, densmax); + else if (iFormatID == FORMAT_PNG16) + return writeImagePNG (pszFilename, 16, nxcell, nycell, densmin, densmax); + + sys_error (ERR_SEVERE, "Invalid format %s [ImageFile::exportImage]", pszFormat); + return false; +} + +bool +ImageFile::writeImagePGM (const char* const outfile, int nxcell, int nycell, double densmin, double densmax) { FILE *fp; int nx = m_nx; @@ -990,7 +1124,7 @@ ImageFile::writeImagePGM (const char *outfile, int nxcell, int nycell, double de unsigned char* rowp = new unsigned char [nx * nxcell]; if ((fp = fopen (outfile, "wb")) == NULL) - return; + return false; fprintf(fp, "P5\n"); fprintf(fp, "%d %d\n", nx, ny); @@ -1012,11 +1146,13 @@ ImageFile::writeImagePGM (const char *outfile, int nxcell, int nycell, double de } delete rowp; - fclose(fp); + fclose(fp); + + return true; } -void -ImageFile::writeImagePGMASCII (const char *outfile, int nxcell, int nycell, double densmin, double densmax) +bool +ImageFile::writeImagePGMASCII (const char* const outfile, int nxcell, int nycell, double densmin, double densmax) { FILE *fp; int nx = m_nx; @@ -1026,7 +1162,7 @@ ImageFile::writeImagePGMASCII (const char *outfile, int nxcell, int nycell, doub unsigned char* rowp = new unsigned char [nx * nxcell]; if ((fp = fopen (outfile, "wb")) == NULL) - return; + return false; fprintf(fp, "P2\n"); fprintf(fp, "%d %d\n", nx, ny); @@ -1049,17 +1185,16 @@ ImageFile::writeImagePGMASCII (const char *outfile, int nxcell, int nycell, doub } delete rowp; - fclose(fp); + fclose(fp); + + return true; } #ifdef HAVE_PNG -void -ImageFile::writeImagePNG (const char *outfile, int bitdepth, int nxcell, int nycell, double densmin, double densmax) +bool +ImageFile::writeImagePNG (const char* const 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; @@ -1067,29 +1202,30 @@ ImageFile::writeImagePNG (const char *outfile, int bitdepth, int nxcell, int nyc unsigned char* rowp = new unsigned char [nx * nxcell * (bitdepth / 8)]; - if ((fp = fopen (outfile, "wb")) == NULL) - return; + FILE *fp = fopen (outfile, "wb"); + if (! fp) + return false; - png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); + png_structp png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); if (! png_ptr) - return; + return false; - info_ptr = png_create_info_struct(png_ptr); + png_infop info_ptr = png_create_info_struct (png_ptr); if (! info_ptr) { - png_destroy_write_struct(&png_ptr, (png_infopp) NULL); - fclose(fp); - return; + png_destroy_write_struct (&png_ptr, (png_infopp) NULL); + fclose (fp); + return false; } - if (setjmp(png_ptr->jmpbuf)) { - png_destroy_write_struct(&png_ptr, &info_ptr); - fclose(fp); - return; + if (setjmp (png_ptr->jmpbuf)) { + png_destroy_write_struct (&png_ptr, &info_ptr); + fclose (fp); + return false; } 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_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--) { @@ -1115,11 +1251,13 @@ ImageFile::writeImagePNG (const char *outfile, int bitdepth, int nxcell, int nyc png_write_rows (png_ptr, &row_pointer, 1); } - png_write_end(png_ptr, info_ptr); - png_destroy_write_struct(&png_ptr, &info_ptr); + png_write_end (png_ptr, info_ptr); + png_destroy_write_struct (&png_ptr, &info_ptr); delete rowp; - fclose(fp); + fclose(fp); + + return true; } #endif @@ -1127,21 +1265,17 @@ ImageFile::writeImagePNG (const char *outfile, int bitdepth, int nxcell, int nyc #include "gd.h" static const int N_GRAYSCALE=256; -void -ImageFile::writeImageGIF (const char *outfile, int nxcell, int nycell, double densmin, double densmax) +bool +ImageFile::writeImageGIF (const char* const 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; + unsigned char* rowp = new unsigned char [nx * nxcell]; - gif = gdImageCreate(nx * nxcell, ny * nycell); + gdImagePtr gif = gdImageCreate(nx * nxcell, ny * nycell); for (int i = 0; i < N_GRAYSCALE; i++) gs_indices[i] = gdImageColorAllocate(gif, i, i, i); @@ -1161,13 +1295,18 @@ ImageFile::writeImageGIF (const char *outfile, int nxcell, int nycell, double de } } - if ((out = fopen(outfile,"w")) == NULL) { + FILE *out; + if ((out = fopen (outfile,"w")) == NULL) { sys_error(ERR_FATAL, "Error opening output file %s for writing", outfile); - return (1); + return false; } gdImageGif(gif,out); fclose(out); - gdImageDestroy(gif); + gdImageDestroy(gif); + + delete rowp; + + return true; } #endif