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.30 2001/01/02 07:18:07 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 (unsigned int ix = 0; ix < m_nx; ix++)
\r
104 for (unsigned 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
471 dImag = vLHSImag[ix][iy];
\r
472 std::complex<double> cLHS (vLHS[ix][iy], dImag);
\r
474 if (rRHS.isComplex())
\r
475 dImag = vRHSImag[ix][iy];
\r
476 std::complex<double> cRHS (vRHS[ix][iy], dImag);
\r
477 std::complex<double> cResult = cLHS * cRHS;
\r
478 vResult[ix][iy] = cResult.real();
\r
479 vResultImag[ix][iy] = cResult.imag();
\r
481 vResult[ix][iy] = vLHS[ix][iy] * vRHS[ix][iy];
\r
490 ImageFile::divideImages (const ImageFile& rRHS, ImageFile& result) const
\r
492 if (m_nx != rRHS.nx() || m_ny != rRHS.ny() || m_nx != result.nx() || m_ny != result.ny()) {
\r
493 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::subtractImage]");
\r
497 if (isComplex() || rRHS.isComplex() && ! result.isComplex())
\r
498 result.convertRealToComplex();
\r
500 ImageFileArrayConst vLHS = getArray();
\r
501 ImageFileArrayConst vLHSImag = getImaginaryArray();
\r
502 ImageFileArrayConst vRHS = rRHS.getArray();
\r
503 ImageFileArrayConst vRHSImag = rRHS.getImaginaryArray();
\r
504 ImageFileArray vResult = result.getArray();
\r
505 ImageFileArray vResultImag = result.getImaginaryArray();
\r
507 for (unsigned int ix = 0; ix < m_nx; ix++) {
\r
508 for (unsigned int iy = 0; iy < m_ny; iy++) {
\r
509 if (result.isComplex()) {
\r
512 dImag = vLHSImag[ix][iy];
\r
513 std::complex<double> cLHS (vLHS[ix][iy], dImag);
\r
515 if (rRHS.isComplex())
\r
516 dImag = vRHSImag[ix][iy];
\r
517 std::complex<double> cRHS (vRHS[ix][iy], dImag);
\r
518 std::complex<double> cResult = cLHS / cRHS;
\r
519 vResult[ix][iy] = cResult.real();
\r
520 vResultImag[ix][iy] = cResult.imag();
\r
523 vResult[ix][iy] = vLHS[ix][iy] / vRHS[ix][iy];
\r
525 vResult[ix][iy] = 0;
\r
535 ImageFile::invertPixelValues (ImageFile& result) const
\r
537 if (m_nx != result.nx() || m_ny != result.ny()) {
\r
538 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
\r
542 if (isComplex() && ! result.isComplex())
\r
543 result.convertRealToComplex();
\r
545 ImageFileArrayConst vLHS = getArray();
\r
546 ImageFileArray vResult = result.getArray();
\r
548 for (unsigned int ix = 0; ix < m_nx; ix++) {
\r
549 ImageFileColumnConst in = vLHS[ix];
\r
550 ImageFileColumn out = vResult[ix];
\r
551 for (unsigned int iy = 0; iy < m_ny; iy++)
\r
559 ImageFile::sqrt (ImageFile& result) const
\r
561 if (m_nx != result.nx() || m_ny != result.ny()) {
\r
562 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
\r
566 if (isComplex() && ! result.isComplex())
\r
567 result.convertRealToComplex();
\r
569 bool bComplexOutput = result.isComplex();
\r
570 ImageFileArrayConst vLHS = getArray();
\r
571 if (! bComplexOutput) // check if should convert to complex output
\r
572 for (unsigned int ix = 0; ix < m_nx; ix++)
\r
573 for (unsigned int iy = 0; iy < m_ny; iy++)
\r
574 if (! bComplexOutput && vLHS[ix][iy] < 0) {
\r
575 result.convertRealToComplex();
\r
576 bComplexOutput = true;
\r
580 ImageFileArrayConst vLHSImag = getImaginaryArray();
\r
581 ImageFileArray vResult = result.getArray();
\r
582 ImageFileArray vResultImag = result.getImaginaryArray();
\r
584 for (unsigned int ix = 0; ix < m_nx; ix++) {
\r
585 for (unsigned int iy = 0; iy < m_ny; iy++) {
\r
586 if (result.isComplex()) {
\r
589 dImag = vLHSImag[ix][iy];
\r
590 std::complex<double> cLHS (vLHS[ix][iy], dImag);
\r
591 std::complex<double> cResult = std::sqrt(cLHS);
\r
592 vResult[ix][iy] = cResult.real();
\r
593 vResultImag[ix][iy] = cResult.imag();
\r
595 vResult[ix][iy] = ::sqrt (vLHS[ix][iy]);
\r
604 ImageFile::log (ImageFile& result) const
\r
606 if (m_nx != result.nx() || m_ny != result.ny()) {
\r
607 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
\r
611 if (isComplex() && ! result.isComplex())
\r
612 result.convertRealToComplex();
\r
614 ImageFileArrayConst vLHS = getArray();
\r
615 ImageFileArrayConst vLHSImag = getImaginaryArray();
\r
616 ImageFileArray vResult = result.getArray();
\r
617 ImageFileArray vResultImag = result.getImaginaryArray();
\r
619 for (unsigned int ix = 0; ix < m_nx; ix++) {
\r
620 for (unsigned int iy = 0; iy < m_ny; iy++) {
\r
621 if (result.isComplex()) {
\r
624 dImag = vLHSImag[ix][iy];
\r
625 std::complex<double> cLHS (vLHS[ix][iy], dImag);
\r
626 std::complex<double> cResult = std::log (cLHS);
\r
627 vResult[ix][iy] = cResult.real();
\r
628 vResultImag[ix][iy] = cResult.imag();
\r
630 vResult[ix][iy] = ::log (vLHS[ix][iy]);
\r
639 ImageFile::exp (ImageFile& result) const
\r
641 if (m_nx != result.nx() || m_ny != result.ny()) {
\r
642 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
\r
646 if (isComplex() && ! result.isComplex())
\r
647 result.convertRealToComplex();
\r
649 ImageFileArrayConst vLHS = getArray();
\r
650 ImageFileArrayConst vLHSImag = getImaginaryArray();
\r
651 ImageFileArray vResult = result.getArray();
\r
652 ImageFileArray vResultImag = result.getImaginaryArray();
\r
654 for (unsigned int ix = 0; ix < m_nx; ix++) {
\r
655 for (unsigned int iy = 0; iy < m_ny; iy++) {
\r
656 if (result.isComplex()) {
\r
659 dImag = vLHSImag[ix][iy];
\r
660 std::complex<double> cLHS (vLHS[ix][iy], dImag);
\r
661 std::complex<double> cResult = std::exp (cLHS);
\r
662 vResult[ix][iy] = cResult.real();
\r
663 vResultImag[ix][iy] = cResult.imag();
\r
665 vResult[ix][iy] = ::exp (vLHS[ix][iy]);
\r
674 ImageFile::scaleImage (ImageFile& result) const
\r
676 unsigned int nx = m_nx;
\r
677 unsigned int ny = m_ny;
\r
678 unsigned int newNX = result.nx();
\r
679 unsigned int newNY = result.ny();
\r
681 double dXScale = static_cast<double>(newNX) / static_cast<double>(nx);
\r
682 double dYScale = static_cast<double>(newNY) / static_cast<double>(ny);
\r
684 if (isComplex() && ! result.isComplex())
\r
685 result.convertRealToComplex();
\r
687 ImageFileArrayConst vReal = getArray();
\r
688 ImageFileArrayConst vImag = getImaginaryArray();
\r
689 ImageFileArray vResult = result.getArray();
\r
690 ImageFileArray vResultImag = result.getImaginaryArray();
\r
692 for (unsigned int ix = 0; ix < newNX; ix++) {
\r
693 for (unsigned int iy = 0; iy < newNY; iy++) {
\r
694 double dXPos = ix / dXScale;
\r
695 double dYPos = iy / dYScale;
\r
696 unsigned int scaleNX = static_cast<unsigned int> (dXPos);
\r
697 unsigned int scaleNY = static_cast<unsigned int> (dYPos);
\r
698 double dXFrac = dXPos - scaleNX;
\r
699 double dYFrac = dYPos - scaleNY;
\r
700 if (scaleNX >= nx - 1 || scaleNY >= ny - 1) {
\r
701 vResult[ix][iy] = vReal[scaleNX][scaleNY];
\r
702 if (result.isComplex()) {
\r
704 vResultImag[ix][iy] = vImag[scaleNX][scaleNY];
\r
706 vResultImag[ix][iy] = 0;
\r
709 vResult[ix][iy] = vReal[scaleNX][scaleNY] +
\r
710 dXFrac * (vReal[scaleNX+1][scaleNY] - vReal[scaleNX][scaleNY]) +
\r
711 dYFrac * (vReal[scaleNX][scaleNY+1] - vReal[scaleNX][scaleNY]);
\r
712 if (result.isComplex()) {
\r
714 vResultImag[ix][iy] = vImag[scaleNX][scaleNY] +
\r
715 dXFrac * (vImag[scaleNX+1][scaleNY] - vImag[scaleNX][scaleNY]) +
\r
716 dYFrac * (vImag[scaleNX][scaleNY+1] - vImag[scaleNX][scaleNY]);
\r
718 vResultImag[ix][iy] = 0;
\r
729 ImageFile::fft (ImageFile& result) const
\r
731 if (m_nx != result.nx() || m_ny != result.ny()) {
\r
732 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
\r
736 if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {
\r
737 if (! result.convertRealToComplex ())
\r
741 fftw_complex* in = new fftw_complex [m_nx * m_ny];
\r
743 ImageFileArrayConst vReal = getArray();
\r
744 ImageFileArrayConst vImag = getImaginaryArray();
\r
746 unsigned int ix, iy;
\r
747 unsigned int iArray = 0;
\r
748 for (ix = 0; ix < m_nx; ix++)
\r
749 for (iy = 0; iy < m_ny; iy++) {
\r
750 in[iArray].re = vReal[ix][iy];
\r
752 in[iArray].im = vImag[ix][iy];
\r
758 fftwnd_plan plan = fftw2d_create_plan (m_nx, m_ny, FFTW_FORWARD, FFTW_IN_PLACE);
\r
760 fftwnd_one (plan, in, NULL);
\r
762 ImageFileArray vRealResult = result.getArray();
\r
763 ImageFileArray vImagResult = result.getImaginaryArray();
\r
765 unsigned int iScale = m_nx * m_ny;
\r
766 for (ix = 0; ix < m_nx; ix++)
\r
767 for (iy = 0; iy < m_ny; iy++) {
\r
768 vRealResult[ix][iy] = in[iArray].re / iScale;
\r
769 vImagResult[ix][iy] = in[iArray].im / iScale;
\r
773 fftwnd_destroy_plan (plan);
\r
777 Fourier::shuffleFourierToNaturalOrder (result);
\r
784 ImageFile::ifft (ImageFile& result) const
\r
786 if (m_nx != result.nx() || m_ny != result.ny()) {
\r
787 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
\r
791 if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {
\r
792 if (! result.convertRealToComplex ())
\r
796 ImageFileArrayConst vReal = getArray();
\r
797 ImageFileArrayConst vImag = getImaginaryArray();
\r
798 ImageFileArray vRealResult = result.getArray();
\r
799 ImageFileArray vImagResult = result.getImaginaryArray();
\r
800 unsigned int ix, iy;
\r
801 for (ix = 0; ix < m_nx; ix++)
\r
802 for (iy = 0; iy < m_ny; iy++) {
\r
803 vRealResult[ix][iy] = vReal[ix][iy];
\r
805 vImagResult[ix][iy] = vImag[ix][iy];
\r
807 vImagResult[ix][iy] = 0;
\r
810 Fourier::shuffleNaturalToFourierOrder (result);
\r
812 fftw_complex* in = new fftw_complex [m_nx * m_ny];
\r
814 unsigned int iArray = 0;
\r
815 for (ix = 0; ix < m_nx; ix++)
\r
816 for (iy = 0; iy < m_ny; iy++) {
\r
817 in[iArray].re = vRealResult[ix][iy];
\r
818 in[iArray].im = vImagResult[ix][iy];
\r
822 fftwnd_plan plan = fftw2d_create_plan (m_nx, m_ny, FFTW_BACKWARD, FFTW_IN_PLACE);
\r
824 fftwnd_one (plan, in, NULL);
\r
827 for (ix = 0; ix < m_nx; ix++)
\r
828 for (iy = 0; iy < m_ny; iy++) {
\r
829 vRealResult[ix][iy] = in[iArray].re;
\r
830 vImagResult[ix][iy] = in[iArray].im;
\r
834 fftwnd_destroy_plan (plan);
\r
840 #endif // HAVE_FFTW
\r
845 ImageFile::fourier (ImageFile& result) const
\r
847 if (m_nx != result.nx() || m_ny != result.ny()) {
\r
848 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
\r
852 if (! result.isComplex())
\r
853 if (! result.convertRealToComplex ())
\r
856 ImageFileArrayConst vLHS = getArray();
\r
857 ImageFileArrayConst vLHSImag = getImaginaryArray();
\r
858 ImageFileArray vRealResult = result.getArray();
\r
859 ImageFileArray vImagResult = result.getImaginaryArray();
\r
861 unsigned int ix, iy;
\r
863 // alloc output matrix
\r
864 CTSimComplex** complexOut = new CTSimComplex* [m_nx];
\r
865 for (ix = 0; ix < m_nx; ix++)
\r
866 complexOut[ix] = new CTSimComplex [m_ny];
\r
868 // fourier each x column
\r
870 CTSimComplex* pY = new CTSimComplex [m_ny];
\r
871 for (ix = 0; ix < m_nx; ix++) {
\r
872 for (iy = 0; iy < m_ny; iy++) {
\r
875 dImag = vLHSImag[ix][iy];
\r
876 pY[iy] = complex<double>(vLHS[ix][iy], dImag);
\r
878 ProcessSignal::finiteFourierTransform (pY, complexOut[ix], m_ny, ProcessSignal::FORWARD);
\r
881 double* pY = new double [m_ny];
\r
882 for (ix = 0; ix < m_nx; ix++) {
\r
883 for (iy = 0; iy < m_ny; iy++) {
\r
884 pY[iy] = vLHS[ix][iy];
\r
886 ProcessSignal::finiteFourierTransform (pY, complexOut[ix], m_ny, ProcessSignal::FORWARD);
\r
891 // fourier each y row
\r
892 CTSimComplex* pX = new CTSimComplex [m_nx];
\r
893 CTSimComplex* complexOutRow = new CTSimComplex [m_nx];
\r
894 for (iy = 0; iy < m_ny; iy++) {
\r
895 for (ix = 0; ix < m_nx; ix++)
\r
896 pX[ix] = complexOut[ix][iy];
\r
897 ProcessSignal::finiteFourierTransform (pX, complexOutRow, m_nx, ProcessSignal::FORWARD);
\r
898 for (ix = 0; ix < m_nx; ix++)
\r
899 complexOut[ix][iy] = complexOutRow[ix];
\r
902 delete [] complexOutRow;
\r
904 for (ix = 0; ix < m_nx; ix++)
\r
905 for (iy = 0; iy < m_ny; iy++) {
\r
906 vRealResult[ix][iy] = complexOut[ix][iy].real();
\r
907 vImagResult[ix][iy] = complexOut[ix][iy].imag();
\r
910 Fourier::shuffleFourierToNaturalOrder (result);
\r
912 // delete complexOut matrix
\r
913 for (ix = 0; ix < m_nx; ix++)
\r
914 delete [] complexOut[ix];
\r
915 delete [] complexOut;
\r
921 ImageFile::inverseFourier (ImageFile& result) const
\r
923 if (m_nx != result.nx() || m_ny != result.ny()) {
\r
924 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
\r
928 if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {
\r
929 if (! result.convertRealToComplex ())
\r
933 ImageFileArrayConst vLHSReal = getArray();
\r
934 ImageFileArrayConst vLHSImag = getImaginaryArray();
\r
935 ImageFileArray vRealResult = result.getArray();
\r
936 ImageFileArray vImagResult = result.getImaginaryArray();
\r
938 unsigned int ix, iy;
\r
939 // alloc 2d complex output matrix
\r
940 CTSimComplex** complexOut = new CTSimComplex* [m_nx];
\r
941 for (ix = 0; ix < m_nx; ix++)
\r
942 complexOut[ix] = new CTSimComplex [m_ny];
\r
944 // put input image into result
\r
945 for (ix = 0; ix < m_nx; ix++)
\r
946 for (iy = 0; iy < m_ny; iy++) {
\r
947 vRealResult[ix][iy] = vLHSReal[ix][iy];
\r
949 vImagResult[ix][iy] = vLHSImag[ix][iy];
\r
951 vImagResult[ix][iy] = 0;
\r
954 Fourier::shuffleNaturalToFourierOrder (result);
\r
956 // ifourier each x column
\r
957 CTSimComplex* pCol = new CTSimComplex [m_ny];
\r
958 for (ix = 0; ix < m_nx; ix++) {
\r
959 for (iy = 0; iy < m_ny; iy++) {
\r
960 pCol[iy] = std::complex<double> (vRealResult[ix][iy], vImagResult[ix][iy]);
\r
962 ProcessSignal::finiteFourierTransform (pCol, complexOut[ix], m_ny, ProcessSignal::BACKWARD);
\r
966 // ifourier each y row
\r
967 CTSimComplex* complexInRow = new CTSimComplex [m_nx];
\r
968 CTSimComplex* complexOutRow = new CTSimComplex [m_nx];
\r
969 for (iy = 0; iy < m_ny; iy++) {
\r
970 for (ix = 0; ix < m_nx; ix++)
\r
971 complexInRow[ix] = complexOut[ix][iy];
\r
972 ProcessSignal::finiteFourierTransform (complexInRow, complexOutRow, m_nx, ProcessSignal::BACKWARD);
\r
973 for (ix = 0; ix < m_nx; ix++)
\r
974 complexOut[ix][iy] = complexOutRow[ix];
\r
976 delete [] complexInRow;
\r
977 delete [] complexOutRow;
\r
979 for (ix = 0; ix < m_nx; ix++)
\r
980 for (iy = 0; iy < m_ny; iy++) {
\r
981 vRealResult[ix][iy] = complexOut[ix][iy].real();
\r
982 vImagResult[ix][iy] = complexOut[ix][iy].imag();
\r
985 // delete complexOut matrix
\r
986 for (ix = 0; ix < m_nx; ix++)
\r
987 delete [] complexOut[ix];
\r
988 delete [] complexOut;
\r
995 ImageFile::magnitude (ImageFile& result) const
\r
997 if (m_nx != result.nx() || m_ny != result.ny()) {
\r
998 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
\r
1002 ImageFileArray vReal = getArray();
\r
1003 ImageFileArray vImag = getImaginaryArray();
\r
1004 ImageFileArray vRealResult = result.getArray();
\r
1006 for (unsigned int ix = 0; ix < m_nx; ix++)
\r
1007 for (unsigned int iy = 0; iy < m_ny; iy++) {
\r
1009 vRealResult[ix][iy] = ::sqrt (vReal[ix][iy] * vReal[ix][iy] + vImag[ix][iy] * vImag[ix][iy]);
\r
1011 vRealResult[ix][iy] = vReal[ix][iy];
\r
1014 if (result.isComplex())
\r
1015 result.convertComplexToReal();
\r
1021 ImageFile::phase (ImageFile& result) const
\r
1023 if (m_nx != result.nx() || m_ny != result.ny()) {
\r
1024 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
\r
1028 ImageFileArray vReal = getArray();
\r
1029 ImageFileArray vImag = getImaginaryArray();
\r
1030 ImageFileArray vRealResult = result.getArray();
\r
1032 for (unsigned int ix = 0; ix < m_nx; ix++)
\r
1033 for (unsigned int iy = 0; iy < m_ny; iy++) {
\r
1035 vRealResult[ix][iy] = ::atan2 (vImag[ix][iy], vReal[ix][iy]);
\r
1037 vRealResult[ix][iy] = 0;
\r
1040 if (result.isComplex())
\r
1041 result.convertComplexToReal();
\r
1047 ImageFile::square (ImageFile& result) const
\r
1049 if (m_nx != result.nx() || m_ny != result.ny()) {
\r
1050 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
\r
1054 if (isComplex() && ! result.isComplex())
\r
1055 result.convertRealToComplex();
\r
1057 ImageFileArrayConst vLHS = getArray();
\r
1058 ImageFileArrayConst vLHSImag = getImaginaryArray();
\r
1059 ImageFileArray vResult = result.getArray();
\r
1060 ImageFileArray vResultImag = result.getImaginaryArray();
\r
1062 for (unsigned int ix = 0; ix < m_nx; ix++) {
\r
1063 for (unsigned int iy = 0; iy < m_ny; iy++) {
\r
1064 if (result.isComplex()) {
\r
1067 dImag = vLHSImag[ix][iy];
\r
1068 std::complex<double> cLHS (vLHS[ix][iy], dImag);
\r
1069 std::complex<double> cResult = cLHS * cLHS;
\r
1070 vResult[ix][iy] = cResult.real();
\r
1071 vResultImag[ix][iy] = cResult.imag();
\r
1073 vResult[ix][iy] = vLHS[ix][iy] * vLHS[ix][iy];
\r
1082 ImageFile::convertFormatNameToID (const char* const formatName)
\r
1084 int formatID = FORMAT_INVALID;
\r
1086 for (int i = 0; i < s_iFormatCount; i++)
\r
1087 if (strcasecmp (formatName, s_aszFormatName[i]) == 0) {
\r
1092 return (formatID);
\r
1096 ImageFile::convertFormatIDToName (int formatID)
\r
1098 static const char *formatName = "";
\r
1100 if (formatID >= 0 && formatID < s_iFormatCount)
\r
1101 return (s_aszFormatName[formatID]);
\r
1103 return (formatName);
\r
1107 ImageFile::convertFormatIDToTitle (const int formatID)
\r
1109 static const char *formatTitle = "";
\r
1111 if (formatID >= 0 && formatID < s_iFormatCount)
\r
1112 return (s_aszFormatTitle[formatID]);
\r
1114 return (formatTitle);
\r
1118 ImageFile::exportImage (const char* const pszFormat, const char* const pszFilename, int nxcell, int nycell, double densmin, double densmax)
\r
1120 int iFormatID = convertFormatNameToID (pszFormat);
\r
1121 if (iFormatID == FORMAT_INVALID) {
\r
1122 sys_error (ERR_SEVERE, "Invalid format %s [ImageFile::exportImage]", pszFormat);
\r
1126 if (iFormatID == FORMAT_PGM)
\r
1127 return writeImagePGM (pszFilename, nxcell, nycell, densmin, densmax);
\r
1128 else if (iFormatID == FORMAT_PGMASCII)
\r
1129 return writeImagePGMASCII (pszFilename, nxcell, nycell, densmin, densmax);
\r
1130 else if (iFormatID == FORMAT_PNG)
\r
1131 return writeImagePNG (pszFilename, 8, nxcell, nycell, densmin, densmax);
\r
1132 else if (iFormatID == FORMAT_PNG16)
\r
1133 return writeImagePNG (pszFilename, 16, nxcell, nycell, densmin, densmax);
\r
1135 sys_error (ERR_SEVERE, "Invalid format %s [ImageFile::exportImage]", pszFormat);
\r
1140 ImageFile::writeImagePGM (const char* const outfile, int nxcell, int nycell, double densmin, double densmax)
1145 ImageFileArray v = getArray();
1147 unsigned char* rowp = new unsigned char [nx * nxcell];
1149 if ((fp = fopen (outfile, "wb")) == NULL)
1152 fprintf(fp, "P5\n");
1153 fprintf(fp, "%d %d\n", nx, ny);
1154 fprintf(fp, "255\n");
1156 for (int irow = ny - 1; irow >= 0; irow--) {
1157 for (int icol = 0; icol < nx; icol++) {
1158 int pos = icol * nxcell;
1159 double dens = (v[icol][irow] - densmin) / (densmax - densmin);
1160 dens = clamp (dens, 0., 1.);
1161 for (int p = pos; p < pos + nxcell; p++) {
1162 rowp[p] = static_cast<unsigned int> (dens * 255.);
1165 for (int ir = 0; ir < nycell; ir++) {
1166 for (int ic = 0; ic < nx * nxcell; ic++)
1167 fputc( rowp[ic], fp );
1178 ImageFile::writeImagePGMASCII (const char* const outfile, int nxcell, int nycell, double densmin, double densmax)
1183 ImageFileArray v = getArray();
1185 unsigned char* rowp = new unsigned char [nx * nxcell];
1187 if ((fp = fopen (outfile, "wb")) == NULL)
1190 fprintf(fp, "P2\n");
1191 fprintf(fp, "%d %d\n", nx, ny);
1192 fprintf(fp, "255\n");
1194 for (int irow = ny - 1; irow >= 0; irow--) {
1195 for (int icol = 0; icol < nx; icol++) {
1196 int pos = icol * nxcell;
1197 double dens = (v[icol][irow] - densmin) / (densmax - densmin);
1198 dens = clamp (dens, 0., 1.);
1199 for (int p = pos; p < pos + nxcell; p++) {
1200 rowp[p] = static_cast<unsigned int> (dens * 255.);
1203 for (int ir = 0; ir < nycell; ir++) {
1204 for (int ic = 0; ic < nx * nxcell; ic++)
1205 fprintf(fp, "%d ", rowp[ic]);
1219 ImageFile::writeImagePNG (const char* const outfile, int bitdepth, int nxcell, int nycell, double densmin, double densmax)
1221 double max_out_level = (1 << bitdepth) - 1;
1224 ImageFileArray v = getArray();
1226 unsigned char* rowp = new unsigned char [nx * nxcell * (bitdepth / 8)];
1228 FILE *fp = fopen (outfile, "wb");
\r
1232 png_structp png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
1236 png_infop info_ptr = png_create_info_struct (png_ptr);
1238 png_destroy_write_struct (&png_ptr, (png_infopp) NULL);
1243 if (setjmp (png_ptr->jmpbuf)) {
1244 png_destroy_write_struct (&png_ptr, &info_ptr);
1249 png_init_io(png_ptr, fp);
1251 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);
1253 png_write_info(png_ptr, info_ptr);
1254 for (int irow = ny - 1; irow >= 0; irow--) {
1255 png_bytep row_pointer = rowp;
1257 for (int icol = 0; icol < nx; icol++) {
1258 int pos = icol * nxcell;
1259 double dens = (v[icol][irow] - densmin) / (densmax - densmin);
1260 dens = clamp (dens, 0., 1.);
1261 unsigned int outval = static_cast<unsigned int> (dens * max_out_level);
1263 for (int p = pos; p < pos + nxcell; p++) {
1268 rowp[rowpos] = (outval >> 8) & 0xFF;
1269 rowp[rowpos+1] = (outval & 0xFF);
1273 for (int ir = 0; ir < nycell; ir++)
1274 png_write_rows (png_ptr, &row_pointer, 1);
1277 png_write_end (png_ptr, info_ptr);
1278 png_destroy_write_struct (&png_ptr, &info_ptr);
1289 static const int N_GRAYSCALE=256;
1292 ImageFile::writeImageGIF (const char* const outfile, int nxcell, int nycell, double densmin, double densmax)
1294 int gs_indices[N_GRAYSCALE];
1297 ImageFileArray v = getArray();
1299 unsigned char* rowp = new unsigned char [nx * nxcell];
1301 gdImagePtr gif = gdImageCreate(nx * nxcell, ny * nycell);
1302 for (int i = 0; i < N_GRAYSCALE; i++)
1303 gs_indices[i] = gdImageColorAllocate(gif, i, i, i);
1305 int lastrow = ny * nycell - 1;
1306 for (int irow = 0; irow < ny; irow++) {
1307 int rpos = irow * nycell;
1308 for (int ir = rpos; ir < rpos + nycell; ir++) {
1309 for (int icol = 0; icol < nx; icol++) {
1310 int cpos = icol * nxcell;
1311 double dens = (v[icol][irow] - densmin) / (densmax - densmin);
1312 dens = clamp(dens, 0., 1.);
1313 for (int ic = cpos; ic < cpos + nxcell; ic++) {
1314 rowp[ic] = (unsigned int) (dens * (double) (N_GRAYSCALE - 1));
1315 gdImageSetPixel(gif, ic, lastrow - ir, gs_indices[rowp[ic]]);
1322 if ((out = fopen (outfile,"w")) == NULL) {
1323 sys_error(ERR_FATAL, "Error opening output file %s for writing", outfile);
1326 gdImageGif(gif,out);
1328 gdImageDestroy(gif);
\r