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.29 2001/01/02 05:34:57 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 ******************************************************************************/
30 const int ImageFile::FORMAT_INVALID = -1;
\r
31 const int ImageFile::FORMAT_PGM = 0;
\r
32 const int ImageFile::FORMAT_PGMASCII = 1;
\r
34 const int ImageFile::FORMAT_PNG = 2;
\r
35 const int ImageFile::FORMAT_PNG16 = 3;
\r
38 const char* ImageFile::s_aszFormatName[] =
\r
48 const char* ImageFile::s_aszFormatTitle[] =
\r
56 const int ImageFile::s_iFormatCount = sizeof(s_aszFormatName) / sizeof(const char*);
\r
60 F32Image::F32Image (int nx, int ny, int dataType)
\r
61 : Array2dFile (nx, ny, sizeof(kfloat32), Array2dFile::PIXEL_FLOAT32, dataType)
\r
65 F32Image::F32Image (void)
\r
68 setPixelFormat (Array2dFile::PIXEL_FLOAT32);
\r
69 setPixelSize (sizeof(kfloat32));
\r
70 setDataType (Array2dFile::DATA_TYPE_REAL);
\r
73 F64Image::F64Image (int nx, int ny, int dataType)
\r
74 : Array2dFile (nx, ny, sizeof(kfloat64), Array2dFile::PIXEL_FLOAT64, dataType)
\r
78 F64Image::F64Image (void)
\r
81 setPixelFormat (PIXEL_FLOAT64);
\r
82 setPixelSize (sizeof(kfloat64));
\r
83 setDataType (Array2dFile::DATA_TYPE_REAL);
\r
87 ImageFile::filterResponse (const char* const domainName, double bw, const char* const filterName, double filt_param, double dInputScale, double dOutputScale)
89 ImageFileArray v = getArray();
\r
90 SignalFilter filter (filterName, domainName, bw, filt_param);
\r
93 int iXCenter, iYCenter;
\r
95 iXCenter = m_nx / 2;
\r
97 iXCenter = (m_nx - 1) / 2;
\r
99 iYCenter = m_ny / 2;
\r
101 iYCenter = (m_ny - 1) / 2;
\r
103 for (int ix = 0; ix < m_nx; ix++)
\r
104 for (int iy = 0; iy < m_ny; iy++) {
\r
105 long lD2 = ((ix - iXCenter) * (ix - iXCenter)) + ((iy - iYCenter) * (iy - iYCenter));
\r
106 double r = ::sqrt (static_cast<double>(lD2)) * dInputScale;
\r
107 v[ix][iy] = filter.response (r) * dOutputScale;
\r
110 int hx = (m_nx - 1) / 2;
111 int hy = (m_ny - 1) / 2;
113 for (int i = -hx; i <= hx; i++) {
114 for (int j = -hy; j <= hy; j++) {
115 double r = ::sqrt (i * i + j * j);
117 v[i+hx][j+hy] = filter.response (r);
124 ImageFile::display (void) const
128 getMinMax (pmin, pmax);
130 return (displayScaling (1, pmin, pmax));
134 ImageFile::displayScaling (const int scale, const ImageFileValue pmin, const ImageFileValue pmax) const
138 ImageFileArrayConst v = getArray();
139 if (v == NULL || nx == 0 || ny == 0)
143 int* pPens = new int [nx * ny * scale * scale ];
145 double view_scale = 255 / (pmax - pmin);
146 int id_X11 = g2_open_X11 (nx * scale, ny * scale);
148 for (int i = 0; i < 256; i++) {
149 double cval = i / 255.;
150 grayscale[i] = g2_ink (id_X11, cval, cval, cval);
153 for (int iy = ny - 1; iy >= 0; iy--) {
154 int iRowPos = ((ny - 1 - iy) * scale) * (nx * scale);
155 for (int ix = 0; ix < nx; ix++) {
156 int cval = static_cast<int>((v[ix][iy] - pmin) * view_scale);
161 for (int sy = 0; sy < scale; sy++)
162 for (int sx = 0; sx < scale; sx++)
163 pPens[iRowPos+(sy * nx * scale)+(sx + (ix * scale))] = grayscale[cval];
167 g2_image (id_X11, 0., 0., nx * scale, ny * scale, pPens);
178 // ImageFile::comparativeStatistics Calculate comparative stats
181 // d Normalized root mean squared distance measure
182 // r Normalized mean absolute distance measure
183 // e Worst case distance measure
186 // G.T. Herman, Image Reconstruction From Projections, 1980
189 ImageFile::comparativeStatistics (const ImageFile& imComp, double& d, double& r, double& e) const
191 if (imComp.nx() != m_nx && imComp.ny() != m_ny) {
192 sys_error (ERR_WARNING, "Image sizes differ [ImageFile::comparativeStatistics]");
195 ImageFileArrayConst v = getArray();
196 if (v == NULL || m_nx == 0 || m_ny == 0)
199 ImageFileArrayConst vComp = imComp.getArray();
202 for (unsigned int ix = 0; ix < m_nx; ix++) {
203 for (unsigned int iy = 0; iy < m_ny; iy++) {
207 myMean /= (m_nx * m_ny);
209 double sqErrorSum = 0.;
210 double absErrorSum = 0.;
211 double sqDiffFromMeanSum = 0.;
212 double absValueSum = 0.;
213 for (unsigned int ix2 = 0; ix2 < m_nx; ix2++) {
214 for (unsigned int iy = 0; iy < m_ny; iy++) {
215 double diff = v[ix2][iy] - vComp[ix2][iy];
216 sqErrorSum += diff * diff;
217 absErrorSum += fabs(diff);
218 double diffFromMean = v[ix2][iy] - myMean;
219 sqDiffFromMeanSum += diffFromMean * diffFromMean;
220 absValueSum += fabs(v[ix2][iy]);
224 d = ::sqrt (sqErrorSum / sqDiffFromMeanSum);
225 r = absErrorSum / absValueSum;
230 for (int ix3 = 0; ix3 < hx; ix3++) {
231 for (int iy = 0; iy < hy; iy++) {
232 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]);
233 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]);
234 double error = fabs (avgPixel - avgPixelComp);
247 ImageFile::printComparativeStatistics (const ImageFile& imComp, std::ostream& os) const
251 if (comparativeStatistics (imComp, d, r, e)) {
252 os << " Normalized root mean squared distance (d): " << d << std::endl;
253 os << " Normalized mean absolute distance (r): " << r << std::endl;
254 os << "Worst case distance (2x2 pixel average) (e): " << e << std::endl;
262 ImageFile::printStatistics (std::ostream& os) const
264 double min, max, mean, mode, median, stddev;
266 statistics (min, max, mean, mode, median, stddev);
\r
268 os << "Real Component Statistics" << std::endl;
\r
270 os << " min: " << min << std::endl;
271 os << " max: " << max << std::endl;
272 os << " mean: " << mean << std::endl;
273 os << " mode: " << mode << std::endl;
274 os << "median: " << median << std::endl;
275 os << "stddev: " << stddev << std::endl;
\r
278 statistics (getImaginaryArray(), min, max, mean, mode, median, stddev);
\r
279 os << std::endl << "Imaginary Component Statistics" << std::endl;
280 os << " min: " << min << std::endl;
\r
281 os << " max: " << max << std::endl;
\r
282 os << " mean: " << mean << std::endl;
\r
283 os << " mode: " << mode << std::endl;
\r
284 os << "median: " << median << std::endl;
\r
285 os << "stddev: " << stddev << std::endl;
\r
291 ImageFile::statistics (double& min, double& max, double& mean, double& mode, double& median, double& stddev) const
293 ImageFileArrayConst v = getArray();
\r
294 statistics (v, min, max, mean, mode, median, stddev);
\r
299 ImageFile::statistics (ImageFileArrayConst v, double& min, double& max, double& mean, double& mode, double& median, double& stddev) const
\r
304 if (v == NULL || nx == 0 || ny == 0)
\r
307 std::vector<double> vecImage;
\r
309 vecImage.resize (nx * ny);
\r
310 for (int ix = 0; ix < nx; ix++) {
\r
311 for (int iy = 0; iy < ny; iy++)
\r
312 vecImage[iVec++] = v[ix][iy];
\r
315 vectorNumericStatistics (vecImage, nx * ny, min, max, mean, mode, median, stddev);
\r
319 ImageFile::getMinMax (double& min, double& max) const
323 ImageFileArrayConst v = getArray();
325 if (v == NULL || nx == 0 || ny == 0)
330 for (int ix = 0; ix < nx; ix++) {
331 for (int iy = 0; iy < ny; iy++) {
341 ImageFile::convertRealToComplex ()
\r
343 if (dataType() != Array2dFile::DATA_TYPE_REAL)
\r
346 if (! reallocRealToComplex())
\r
349 ImageFileArray vImag = getImaginaryArray();
\r
350 for (unsigned int ix = 0; ix < m_nx; ix++) {
\r
351 ImageFileColumn vCol = vImag[ix];
\r
352 for (unsigned int iy = 0; iy < m_ny; iy++)
\r
360 ImageFile::convertComplexToReal ()
\r
362 if (dataType() != Array2dFile::DATA_TYPE_COMPLEX)
\r
365 ImageFileArray vReal = getArray();
\r
366 ImageFileArray vImag = getImaginaryArray();
\r
367 for (unsigned int ix = 0; ix < m_nx; ix++) {
\r
368 ImageFileColumn vRealCol = vReal[ix];
\r
369 ImageFileColumn vImagCol = vImag[ix];
\r
370 for (unsigned int iy = 0; iy < m_ny; iy++) {
\r
371 CTSimComplex c (*vRealCol, *vImagCol);
\r
372 *vRealCol++ = std::abs (c);
\r
377 return reallocComplexToReal();
\r
381 ImageFile::subtractImages (const ImageFile& rRHS, ImageFile& result) const
\r
383 if (m_nx != rRHS.nx() || m_ny != rRHS.ny() || m_nx != result.nx() || m_ny != result.ny()) {
\r
384 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::subtractImage]");
\r
388 if (isComplex() || rRHS.isComplex() && ! result.isComplex())
\r
389 result.convertRealToComplex();
\r
391 ImageFileArrayConst vLHS = getArray();
\r
392 ImageFileArrayConst vLHSImag = getImaginaryArray();
\r
393 ImageFileArrayConst vRHS = rRHS.getArray();
\r
394 ImageFileArrayConst vRHSImag = rRHS.getImaginaryArray();
\r
395 ImageFileArray vResult = result.getArray();
\r
396 ImageFileArray vResultImag = result.getImaginaryArray();
\r
398 for (unsigned int ix = 0; ix < m_nx; ix++) {
\r
399 for (unsigned int iy = 0; iy < m_ny; iy++) {
\r
400 vResult[ix][iy] = vLHS[ix][iy] - vRHS[ix][iy];
\r
401 if (result.isComplex()) {
\r
402 vResultImag[ix][iy] = 0;
\r
404 vResultImag[ix][iy] += vLHSImag[ix][iy];
\r
405 if (rRHS.isComplex())
\r
406 vResultImag[ix][iy] -= vRHSImag[ix][iy];
\r
415 ImageFile::addImages (const ImageFile& rRHS, ImageFile& result) const
\r
417 if (m_nx != rRHS.nx() || m_ny != rRHS.ny() || m_nx != result.nx() || m_ny != result.ny()) {
\r
418 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::subtractImage]");
\r
422 if (isComplex() || rRHS.isComplex() && ! result.isComplex())
\r
423 result.convertRealToComplex();
\r
425 ImageFileArrayConst vLHS = getArray();
\r
426 ImageFileArrayConst vLHSImag = getImaginaryArray();
\r
427 ImageFileArrayConst vRHS = rRHS.getArray();
\r
428 ImageFileArrayConst vRHSImag = rRHS.getImaginaryArray();
\r
429 ImageFileArray vResult = result.getArray();
\r
430 ImageFileArray vResultImag = result.getImaginaryArray();
\r
432 for (unsigned int ix = 0; ix < m_nx; ix++) {
\r
433 for (unsigned int iy = 0; iy < m_ny; iy++) {
\r
434 vResult[ix][iy] = vLHS[ix][iy] + vRHS[ix][iy];
\r
435 if (result.isComplex()) {
\r
436 vResultImag[ix][iy] = 0;
\r
438 vResultImag[ix][iy] += vLHSImag[ix][iy];
\r
439 if (rRHS.isComplex())
\r
440 vResultImag[ix][iy] += vRHSImag[ix][iy];
\r
449 ImageFile::multiplyImages (const ImageFile& rRHS, ImageFile& result) const
\r
451 if (m_nx != rRHS.nx() || m_ny != rRHS.ny() || m_nx != result.nx() || m_ny != result.ny()) {
\r
452 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::subtractImage]");
\r
456 if (isComplex() || rRHS.isComplex() && ! result.isComplex())
\r
457 result.convertRealToComplex();
\r
459 ImageFileArrayConst vLHS = getArray();
\r
460 ImageFileArrayConst vLHSImag = getImaginaryArray();
\r
461 ImageFileArrayConst vRHS = rRHS.getArray();
\r
462 ImageFileArrayConst vRHSImag = rRHS.getImaginaryArray();
\r
463 ImageFileArray vResult = result.getArray();
\r
464 ImageFileArray vResultImag = result.getImaginaryArray();
\r
466 for (unsigned int ix = 0; ix < m_nx; ix++) {
\r
467 for (unsigned int iy = 0; iy < m_ny; iy++) {
\r
468 if (result.isComplex()) {
\r
469 std::complex<double> cLHS (vLHS[ix][iy], 0);
\r
471 cLHS.imag (vLHSImag[ix][iy]);
\r
472 std::complex<double> cRHS (vRHS[ix][iy], 0);
\r
473 if (rRHS.isComplex())
\r
474 cRHS.imag (vRHSImag[ix][iy]);
\r
475 std::complex<double> cResult = cLHS * cRHS;
\r
476 vResult[ix][iy] = cResult.real();
\r
477 vResultImag[ix][iy] = cResult.imag();
\r
479 vResult[ix][iy] = vLHS[ix][iy] * vRHS[ix][iy];
\r
488 ImageFile::divideImages (const ImageFile& rRHS, ImageFile& result) const
\r
490 if (m_nx != rRHS.nx() || m_ny != rRHS.ny() || m_nx != result.nx() || m_ny != result.ny()) {
\r
491 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::subtractImage]");
\r
495 if (isComplex() || rRHS.isComplex() && ! result.isComplex())
\r
496 result.convertRealToComplex();
\r
498 ImageFileArrayConst vLHS = getArray();
\r
499 ImageFileArrayConst vLHSImag = getImaginaryArray();
\r
500 ImageFileArrayConst vRHS = rRHS.getArray();
\r
501 ImageFileArrayConst vRHSImag = rRHS.getImaginaryArray();
\r
502 ImageFileArray vResult = result.getArray();
\r
503 ImageFileArray vResultImag = result.getImaginaryArray();
\r
505 for (unsigned int ix = 0; ix < m_nx; ix++) {
\r
506 for (unsigned int iy = 0; iy < m_ny; iy++) {
\r
507 if (result.isComplex()) {
\r
508 std::complex<double> cLHS (vLHS[ix][iy], 0);
\r
510 cLHS.imag (vLHSImag[ix][iy]);
\r
511 std::complex<double> cRHS (vRHS[ix][iy], 0);
\r
512 if (rRHS.isComplex())
\r
513 cRHS.imag (vRHSImag[ix][iy]);
\r
514 std::complex<double> cResult = cLHS / cRHS;
\r
515 vResult[ix][iy] = cResult.real();
\r
516 vResultImag[ix][iy] = cResult.imag();
\r
519 vResult[ix][iy] = vLHS[ix][iy] / vRHS[ix][iy];
\r
521 vResult[ix][iy] = 0;
\r
532 ImageFile::invertPixelValues (ImageFile& result) const
\r
534 if (m_nx != result.nx() || m_ny != result.ny()) {
\r
535 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
\r
539 if (isComplex() && ! result.isComplex())
\r
540 result.convertRealToComplex();
\r
542 ImageFileArrayConst vLHS = getArray();
\r
543 ImageFileArray vResult = result.getArray();
\r
545 for (unsigned int ix = 0; ix < m_nx; ix++) {
\r
546 ImageFileColumnConst in = vLHS[ix];
\r
547 ImageFileColumn out = vResult[ix];
\r
548 for (unsigned int iy = 0; iy < m_ny; iy++)
\r
556 ImageFile::sqrt (ImageFile& result) const
\r
558 if (m_nx != result.nx() || m_ny != result.ny()) {
\r
559 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
\r
563 if (isComplex() && ! result.isComplex())
\r
564 result.convertRealToComplex();
\r
566 bool bComplexOutput = result.isComplex();
\r
567 ImageFileArrayConst vLHS = getArray();
\r
568 if (! bComplexOutput) // check if should convert to complex output
\r
569 for (unsigned int ix = 0; ix < m_nx; ix++)
\r
570 for (unsigned int iy = 0; iy < m_ny; iy++)
\r
571 if (! bComplexOutput && vLHS[ix][iy] < 0) {
\r
572 result.convertRealToComplex();
\r
573 bComplexOutput = true;
\r
577 ImageFileArrayConst vLHSImag = getImaginaryArray();
\r
578 ImageFileArray vResult = result.getArray();
\r
579 ImageFileArray vResultImag = result.getImaginaryArray();
\r
581 for (unsigned int ix = 0; ix < m_nx; ix++) {
\r
582 for (unsigned int iy = 0; iy < m_ny; iy++) {
\r
583 if (result.isComplex()) {
\r
584 std::complex<double> cLHS (vLHS[ix][iy], 0);
\r
586 cLHS.imag (vLHSImag[ix][iy]);
\r
587 std::complex<double> cResult = std::sqrt(cLHS);
\r
588 vResult[ix][iy] = cResult.real();
\r
589 vResultImag[ix][iy] = cResult.imag();
\r
591 vResult[ix][iy] = ::sqrt (vLHS[ix][iy]);
\r
600 ImageFile::log (ImageFile& result) const
\r
602 if (m_nx != result.nx() || m_ny != result.ny()) {
\r
603 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
\r
607 if (isComplex() && ! result.isComplex())
\r
608 result.convertRealToComplex();
\r
610 ImageFileArrayConst vLHS = getArray();
\r
611 ImageFileArrayConst vLHSImag = getImaginaryArray();
\r
612 ImageFileArray vResult = result.getArray();
\r
613 ImageFileArray vResultImag = result.getImaginaryArray();
\r
615 for (unsigned int ix = 0; ix < m_nx; ix++) {
\r
616 for (unsigned int iy = 0; iy < m_ny; iy++) {
\r
617 if (result.isComplex()) {
\r
618 std::complex<double> cLHS (vLHS[ix][iy], 0);
\r
620 cLHS.imag (vLHSImag[ix][iy]);
\r
621 std::complex<double> cResult = std::log (cLHS);
\r
622 vResult[ix][iy] = cResult.real();
\r
623 vResultImag[ix][iy] = cResult.imag();
\r
625 vResult[ix][iy] = ::log (vLHS[ix][iy]);
\r
634 ImageFile::exp (ImageFile& result) const
\r
636 if (m_nx != result.nx() || m_ny != result.ny()) {
\r
637 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
\r
641 if (isComplex() && ! result.isComplex())
\r
642 result.convertRealToComplex();
\r
644 ImageFileArrayConst vLHS = getArray();
\r
645 ImageFileArrayConst vLHSImag = getImaginaryArray();
\r
646 ImageFileArray vResult = result.getArray();
\r
647 ImageFileArray vResultImag = result.getImaginaryArray();
\r
649 for (unsigned int ix = 0; ix < m_nx; ix++) {
\r
650 for (unsigned int iy = 0; iy < m_ny; iy++) {
\r
651 if (result.isComplex()) {
\r
652 std::complex<double> cLHS (vLHS[ix][iy], 0);
\r
654 cLHS.imag (vLHSImag[ix][iy]);
\r
655 std::complex<double> cResult = std::exp (cLHS);
\r
656 vResult[ix][iy] = cResult.real();
\r
657 vResultImag[ix][iy] = cResult.imag();
\r
659 vResult[ix][iy] = ::exp (vLHS[ix][iy]);
\r
668 ImageFile::scaleImage (ImageFile& result) const
\r
670 unsigned int nx = m_nx;
\r
671 unsigned int ny = m_ny;
\r
672 unsigned int newNX = result.nx();
\r
673 unsigned int newNY = result.ny();
\r
675 double dXScale = static_cast<double>(newNX) / static_cast<double>(nx);
\r
676 double dYScale = static_cast<double>(newNY) / static_cast<double>(ny);
\r
678 if (isComplex() && ! result.isComplex())
\r
679 result.convertRealToComplex();
\r
681 ImageFileArrayConst vReal = getArray();
\r
682 ImageFileArrayConst vImag = getImaginaryArray();
\r
683 ImageFileArray vResult = result.getArray();
\r
684 ImageFileArray vResultImag = result.getImaginaryArray();
\r
686 for (unsigned int ix = 0; ix < newNX; ix++) {
\r
687 for (unsigned int iy = 0; iy < newNY; iy++) {
\r
688 unsigned int scaleNX = static_cast<unsigned int> (ix / dXScale);
\r
689 unsigned int scaleNY = static_cast<unsigned int> (iy / dYScale);
\r
690 vResult[ix][iy] = vReal[scaleNX][scaleNY];
\r
691 if (result.isComplex()) {
\r
693 vResultImag[ix][iy] = vImag[scaleNX][scaleNY];
\r
695 vResultImag[ix][iy] = 0;
\r
705 ImageFile::fft (ImageFile& result) const
\r
707 if (m_nx != result.nx() || m_ny != result.ny()) {
\r
708 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
\r
712 if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {
\r
713 if (! result.convertRealToComplex ())
\r
717 fftw_complex* in = new fftw_complex [m_nx * m_ny];
\r
719 ImageFileArrayConst vReal = getArray();
\r
720 ImageFileArrayConst vImag = getImaginaryArray();
\r
722 unsigned int ix, iy;
\r
723 unsigned int iArray = 0;
\r
724 for (ix = 0; ix < m_nx; ix++)
\r
725 for (iy = 0; iy < m_ny; iy++) {
\r
726 in[iArray].re = vReal[ix][iy];
\r
728 in[iArray].im = vImag[ix][iy];
\r
734 fftwnd_plan plan = fftw2d_create_plan (m_nx, m_ny, FFTW_FORWARD, FFTW_IN_PLACE);
\r
736 fftwnd_one (plan, in, NULL);
\r
738 ImageFileArray vRealResult = result.getArray();
\r
739 ImageFileArray vImagResult = result.getImaginaryArray();
\r
741 unsigned int iScale = m_nx * m_ny;
\r
742 for (ix = 0; ix < m_nx; ix++)
\r
743 for (iy = 0; iy < m_ny; iy++) {
\r
744 vRealResult[ix][iy] = in[iArray].re / iScale;
\r
745 vImagResult[ix][iy] = in[iArray].im / iScale;
\r
749 fftwnd_destroy_plan (plan);
\r
753 Fourier::shuffleFourierToNaturalOrder (result);
\r
760 ImageFile::ifft (ImageFile& result) const
\r
762 if (m_nx != result.nx() || m_ny != result.ny()) {
\r
763 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
\r
767 if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {
\r
768 if (! result.convertRealToComplex ())
\r
772 ImageFileArrayConst vReal = getArray();
\r
773 ImageFileArrayConst vImag = getImaginaryArray();
\r
774 ImageFileArray vRealResult = result.getArray();
\r
775 ImageFileArray vImagResult = result.getImaginaryArray();
\r
776 unsigned int ix, iy;
\r
777 for (ix = 0; ix < m_nx; ix++)
\r
778 for (iy = 0; iy < m_ny; iy++) {
\r
779 vRealResult[ix][iy] = vReal[ix][iy];
\r
781 vImagResult[ix][iy] = vImag[ix][iy];
\r
783 vImagResult[ix][iy] = 0;
\r
786 Fourier::shuffleNaturalToFourierOrder (result);
\r
788 fftw_complex* in = new fftw_complex [m_nx * m_ny];
\r
790 unsigned int iArray = 0;
\r
791 for (ix = 0; ix < m_nx; ix++)
\r
792 for (iy = 0; iy < m_ny; iy++) {
\r
793 in[iArray].re = vRealResult[ix][iy];
\r
794 in[iArray].im = vImagResult[ix][iy];
\r
798 fftwnd_plan plan = fftw2d_create_plan (m_nx, m_ny, FFTW_BACKWARD, FFTW_IN_PLACE);
\r
800 fftwnd_one (plan, in, NULL);
\r
803 for (ix = 0; ix < m_nx; ix++)
\r
804 for (iy = 0; iy < m_ny; iy++) {
\r
805 vRealResult[ix][iy] = in[iArray].re;
\r
806 vImagResult[ix][iy] = in[iArray].im;
\r
810 fftwnd_destroy_plan (plan);
\r
816 #endif // HAVE_FFTW
\r
821 ImageFile::fourier (ImageFile& result) const
\r
823 if (m_nx != result.nx() || m_ny != result.ny()) {
\r
824 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
\r
828 if (! result.isComplex())
\r
829 if (! result.convertRealToComplex ())
\r
832 ImageFileArrayConst vLHS = getArray();
\r
833 ImageFileArrayConst vLHSImag = getImaginaryArray();
\r
834 ImageFileArray vRealResult = result.getArray();
\r
835 ImageFileArray vImagResult = result.getImaginaryArray();
\r
837 unsigned int ix, iy;
\r
839 // alloc output matrix
\r
840 CTSimComplex** complexOut = new CTSimComplex* [m_nx];
\r
841 for (ix = 0; ix < m_nx; ix++)
\r
842 complexOut[ix] = new CTSimComplex [m_ny];
\r
844 // fourier each x column
\r
846 CTSimComplex* pY = new CTSimComplex [m_ny];
\r
847 for (ix = 0; ix < m_nx; ix++) {
\r
848 for (iy = 0; iy < m_ny; iy++) {
\r
849 pY[iy].real (vLHS[ix][iy]);
\r
851 pY[iy].imag (vLHSImag[ix][iy]);
\r
855 ProcessSignal::finiteFourierTransform (pY, complexOut[ix], m_ny, ProcessSignal::FORWARD);
\r
858 double* pY = new double [m_ny];
\r
859 for (ix = 0; ix < m_nx; ix++) {
\r
860 for (iy = 0; iy < m_ny; iy++) {
\r
861 pY[iy] = vLHS[ix][iy];
\r
863 ProcessSignal::finiteFourierTransform (pY, complexOut[ix], m_ny, ProcessSignal::FORWARD);
\r
868 // fourier each y row
\r
869 CTSimComplex* pX = new CTSimComplex [m_nx];
\r
870 CTSimComplex* complexOutRow = new CTSimComplex [m_nx];
\r
871 for (iy = 0; iy < m_ny; iy++) {
\r
872 for (ix = 0; ix < m_nx; ix++)
\r
873 pX[ix] = complexOut[ix][iy];
\r
874 ProcessSignal::finiteFourierTransform (pX, complexOutRow, m_nx, ProcessSignal::FORWARD);
\r
875 for (ix = 0; ix < m_nx; ix++)
\r
876 complexOut[ix][iy] = complexOutRow[ix];
\r
879 delete [] complexOutRow;
\r
881 for (ix = 0; ix < m_nx; ix++)
\r
882 for (iy = 0; iy < m_ny; iy++) {
\r
883 vRealResult[ix][iy] = complexOut[ix][iy].real();
\r
884 vImagResult[ix][iy] = complexOut[ix][iy].imag();
\r
887 Fourier::shuffleFourierToNaturalOrder (result);
\r
889 // delete complexOut matrix
\r
890 for (ix = 0; ix < m_nx; ix++)
\r
891 delete [] complexOut[ix];
\r
892 delete [] complexOut;
\r
898 ImageFile::inverseFourier (ImageFile& result) const
\r
900 if (m_nx != result.nx() || m_ny != result.ny()) {
\r
901 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
\r
905 if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {
\r
906 if (! result.convertRealToComplex ())
\r
910 ImageFileArrayConst vLHSReal = getArray();
\r
911 ImageFileArrayConst vLHSImag = getImaginaryArray();
\r
912 ImageFileArray vRealResult = result.getArray();
\r
913 ImageFileArray vImagResult = result.getImaginaryArray();
\r
915 unsigned int ix, iy;
\r
916 // alloc 2d complex output matrix
\r
917 CTSimComplex** complexOut = new CTSimComplex* [m_nx];
\r
918 for (ix = 0; ix < m_nx; ix++)
\r
919 complexOut[ix] = new CTSimComplex [m_ny];
\r
921 // put input image into result
\r
922 for (ix = 0; ix < m_nx; ix++)
\r
923 for (iy = 0; iy < m_ny; iy++) {
\r
924 vRealResult[ix][iy] = vLHSReal[ix][iy];
\r
926 vImagResult[ix][iy] = vLHSImag[ix][iy];
\r
928 vImagResult[ix][iy] = 0;
\r
931 Fourier::shuffleNaturalToFourierOrder (result);
\r
933 // ifourier each x column
\r
934 CTSimComplex* pCol = new CTSimComplex [m_ny];
\r
935 for (ix = 0; ix < m_nx; ix++) {
\r
936 for (iy = 0; iy < m_ny; iy++) {
\r
937 pCol[iy].real (vRealResult[ix][iy]);
\r
938 pCol[iy].imag (vImagResult[ix][iy]);
\r
940 ProcessSignal::finiteFourierTransform (pCol, complexOut[ix], m_ny, ProcessSignal::BACKWARD);
\r
944 // ifourier each y row
\r
945 CTSimComplex* complexInRow = new CTSimComplex [m_nx];
\r
946 CTSimComplex* complexOutRow = new CTSimComplex [m_nx];
\r
947 for (iy = 0; iy < m_ny; iy++) {
\r
948 for (ix = 0; ix < m_nx; ix++)
\r
949 complexInRow[ix] = complexOut[ix][iy];
\r
950 ProcessSignal::finiteFourierTransform (complexInRow, complexOutRow, m_nx, ProcessSignal::BACKWARD);
\r
951 for (ix = 0; ix < m_nx; ix++)
\r
952 complexOut[ix][iy] = complexOutRow[ix];
\r
954 delete [] complexInRow;
\r
955 delete [] complexOutRow;
\r
957 for (ix = 0; ix < m_nx; ix++)
\r
958 for (iy = 0; iy < m_ny; iy++) {
\r
959 vRealResult[ix][iy] = complexOut[ix][iy].real();
\r
960 vImagResult[ix][iy] = complexOut[ix][iy].imag();
\r
963 // delete complexOut matrix
\r
964 for (ix = 0; ix < m_nx; ix++)
\r
965 delete [] complexOut[ix];
\r
966 delete [] complexOut;
\r
973 ImageFile::magnitude (ImageFile& result) const
\r
975 if (m_nx != result.nx() || m_ny != result.ny()) {
\r
976 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
\r
980 ImageFileArray vReal = getArray();
\r
981 ImageFileArray vImag = getImaginaryArray();
\r
982 ImageFileArray vRealResult = result.getArray();
\r
984 for (unsigned int ix = 0; ix < m_nx; ix++)
\r
985 for (unsigned int iy = 0; iy < m_ny; iy++) {
\r
987 vRealResult[ix][iy] = ::sqrt (vReal[ix][iy] * vReal[ix][iy] + vImag[ix][iy] * vImag[ix][iy]);
\r
989 vRealResult[ix][iy] = vReal[ix][iy];
\r
992 if (result.isComplex())
\r
993 result.convertComplexToReal();
\r
999 ImageFile::phase (ImageFile& result) const
\r
1001 if (m_nx != result.nx() || m_ny != result.ny()) {
\r
1002 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
\r
1006 ImageFileArray vReal = getArray();
\r
1007 ImageFileArray vImag = getImaginaryArray();
\r
1008 ImageFileArray vRealResult = result.getArray();
\r
1010 for (unsigned int ix = 0; ix < m_nx; ix++)
\r
1011 for (unsigned int iy = 0; iy < m_ny; iy++) {
\r
1013 vRealResult[ix][iy] = ::atan2 (vImag[ix][iy], vReal[ix][iy]);
\r
1015 vRealResult[ix][iy] = 0;
\r
1018 if (result.isComplex())
\r
1019 result.convertComplexToReal();
\r
1025 ImageFile::square (ImageFile& result) const
\r
1027 if (m_nx != result.nx() || m_ny != result.ny()) {
\r
1028 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
\r
1032 if (isComplex() && ! result.isComplex())
\r
1033 result.convertRealToComplex();
\r
1035 ImageFileArrayConst vLHS = getArray();
\r
1036 ImageFileArrayConst vLHSImag = getImaginaryArray();
\r
1037 ImageFileArray vResult = result.getArray();
\r
1038 ImageFileArray vResultImag = result.getImaginaryArray();
\r
1040 for (unsigned int ix = 0; ix < m_nx; ix++) {
\r
1041 for (unsigned int iy = 0; iy < m_ny; iy++) {
\r
1042 if (result.isComplex()) {
\r
1043 std::complex<double> cLHS (vLHS[ix][iy], 0);
\r
1045 cLHS.imag (vLHSImag[ix][iy]);
\r
1046 std::complex<double> cResult = cLHS * cLHS;
\r
1047 vResult[ix][iy] = cResult.real();
\r
1048 vResultImag[ix][iy] = cResult.imag();
\r
1050 vResult[ix][iy] = vLHS[ix][iy] * vLHS[ix][iy];
\r
1059 ImageFile::convertFormatNameToID (const char* const formatName)
\r
1061 int formatID = FORMAT_INVALID;
\r
1063 for (int i = 0; i < s_iFormatCount; i++)
\r
1064 if (strcasecmp (formatName, s_aszFormatName[i]) == 0) {
\r
1069 return (formatID);
\r
1073 ImageFile::convertFormatIDToName (int formatID)
\r
1075 static const char *formatName = "";
\r
1077 if (formatID >= 0 && formatID < s_iFormatCount)
\r
1078 return (s_aszFormatName[formatID]);
\r
1080 return (formatName);
\r
1084 ImageFile::convertFormatIDToTitle (const int formatID)
\r
1086 static const char *formatTitle = "";
\r
1088 if (formatID >= 0 && formatID < s_iFormatCount)
\r
1089 return (s_aszFormatTitle[formatID]);
\r
1091 return (formatTitle);
\r
1095 ImageFile::exportImage (const char* const pszFormat, const char* const pszFilename, int nxcell, int nycell, double densmin, double densmax)
\r
1097 int iFormatID = convertFormatNameToID (pszFormat);
\r
1098 if (iFormatID == FORMAT_INVALID) {
\r
1099 sys_error (ERR_SEVERE, "Invalid format %s [ImageFile::exportImage]", pszFormat);
\r
1103 if (iFormatID == FORMAT_PGM)
\r
1104 return writeImagePGM (pszFilename, nxcell, nycell, densmin, densmax);
\r
1105 else if (iFormatID == FORMAT_PGMASCII)
\r
1106 return writeImagePGMASCII (pszFilename, nxcell, nycell, densmin, densmax);
\r
1107 else if (iFormatID == FORMAT_PNG)
\r
1108 return writeImagePNG (pszFilename, 8, nxcell, nycell, densmin, densmax);
\r
1109 else if (iFormatID == FORMAT_PNG16)
\r
1110 return writeImagePNG (pszFilename, 16, nxcell, nycell, densmin, densmax);
\r
1112 sys_error (ERR_SEVERE, "Invalid format %s [ImageFile::exportImage]", pszFormat);
\r
1117 ImageFile::writeImagePGM (const char* const outfile, int nxcell, int nycell, double densmin, double densmax)
1122 ImageFileArray v = getArray();
1124 unsigned char* rowp = new unsigned char [nx * nxcell];
1126 if ((fp = fopen (outfile, "wb")) == NULL)
1129 fprintf(fp, "P5\n");
1130 fprintf(fp, "%d %d\n", nx, ny);
1131 fprintf(fp, "255\n");
1133 for (int irow = ny - 1; irow >= 0; irow--) {
1134 for (int icol = 0; icol < nx; icol++) {
1135 int pos = icol * nxcell;
1136 double dens = (v[icol][irow] - densmin) / (densmax - densmin);
1137 dens = clamp (dens, 0., 1.);
1138 for (int p = pos; p < pos + nxcell; p++) {
1139 rowp[p] = static_cast<unsigned int> (dens * 255.);
1142 for (int ir = 0; ir < nycell; ir++) {
1143 for (int ic = 0; ic < nx * nxcell; ic++)
1144 fputc( rowp[ic], fp );
1155 ImageFile::writeImagePGMASCII (const char* const outfile, int nxcell, int nycell, double densmin, double densmax)
1160 ImageFileArray v = getArray();
1162 unsigned char* rowp = new unsigned char [nx * nxcell];
1164 if ((fp = fopen (outfile, "wb")) == NULL)
1167 fprintf(fp, "P2\n");
1168 fprintf(fp, "%d %d\n", nx, ny);
1169 fprintf(fp, "255\n");
1171 for (int irow = ny - 1; irow >= 0; irow--) {
1172 for (int icol = 0; icol < nx; icol++) {
1173 int pos = icol * nxcell;
1174 double dens = (v[icol][irow] - densmin) / (densmax - densmin);
1175 dens = clamp (dens, 0., 1.);
1176 for (int p = pos; p < pos + nxcell; p++) {
1177 rowp[p] = static_cast<unsigned int> (dens * 255.);
1180 for (int ir = 0; ir < nycell; ir++) {
1181 for (int ic = 0; ic < nx * nxcell; ic++)
1182 fprintf(fp, "%d ", rowp[ic]);
1196 ImageFile::writeImagePNG (const char* const outfile, int bitdepth, int nxcell, int nycell, double densmin, double densmax)
1198 double max_out_level = (1 << bitdepth) - 1;
1201 ImageFileArray v = getArray();
1203 unsigned char* rowp = new unsigned char [nx * nxcell * (bitdepth / 8)];
1205 FILE *fp = fopen (outfile, "wb");
\r
1209 png_structp png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
1213 png_infop info_ptr = png_create_info_struct (png_ptr);
1215 png_destroy_write_struct (&png_ptr, (png_infopp) NULL);
1220 if (setjmp (png_ptr->jmpbuf)) {
1221 png_destroy_write_struct (&png_ptr, &info_ptr);
1226 png_init_io(png_ptr, fp);
1228 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);
1230 png_write_info(png_ptr, info_ptr);
1231 for (int irow = ny - 1; irow >= 0; irow--) {
1232 png_bytep row_pointer = rowp;
1234 for (int icol = 0; icol < nx; icol++) {
1235 int pos = icol * nxcell;
1236 double dens = (v[icol][irow] - densmin) / (densmax - densmin);
1237 dens = clamp (dens, 0., 1.);
1238 unsigned int outval = static_cast<unsigned int> (dens * max_out_level);
1240 for (int p = pos; p < pos + nxcell; p++) {
1245 rowp[rowpos] = (outval >> 8) & 0xFF;
1246 rowp[rowpos+1] = (outval & 0xFF);
1250 for (int ir = 0; ir < nycell; ir++)
1251 png_write_rows (png_ptr, &row_pointer, 1);
1254 png_write_end (png_ptr, info_ptr);
1255 png_destroy_write_struct (&png_ptr, &info_ptr);
1266 static const int N_GRAYSCALE=256;
1269 ImageFile::writeImageGIF (const char* const outfile, int nxcell, int nycell, double densmin, double densmax)
1271 int gs_indices[N_GRAYSCALE];
1274 ImageFileArray v = getArray();
1276 unsigned char* rowp = new unsigned char [nx * nxcell];
1278 gdImagePtr gif = gdImageCreate(nx * nxcell, ny * nycell);
1279 for (int i = 0; i < N_GRAYSCALE; i++)
1280 gs_indices[i] = gdImageColorAllocate(gif, i, i, i);
1282 int lastrow = ny * nycell - 1;
1283 for (int irow = 0; irow < ny; irow++) {
1284 int rpos = irow * nycell;
1285 for (int ir = rpos; ir < rpos + nycell; ir++) {
1286 for (int icol = 0; icol < nx; icol++) {
1287 int cpos = icol * nxcell;
1288 double dens = (v[icol][irow] - densmin) / (densmax - densmin);
1289 dens = clamp(dens, 0., 1.);
1290 for (int ic = cpos; ic < cpos + nxcell; ic++) {
1291 rowp[ic] = (unsigned int) (dens * (double) (N_GRAYSCALE - 1));
1292 gdImageSetPixel(gif, ic, lastrow - ir, gs_indices[rowp[ic]]);
1299 if ((out = fopen (outfile,"w")) == NULL) {
1300 sys_error(ERR_FATAL, "Error opening output file %s for writing", outfile);
1303 gdImageGif(gif,out);
1305 gdImageDestroy(gif);
\r