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-2001 Kevin Rosenberg
12 ** $Id: imagefile.cpp,v 1.47 2003/03/23 18:37:42 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 ******************************************************************************/
32 #include "interpolator.h"
34 const double ImageFile::s_dRedGrayscaleFactor = 0.299;
35 const double ImageFile::s_dGreenGrayscaleFactor = 0.587;
36 const double ImageFile::s_dBlueGrayscaleFactor = 0.114;
39 const int ImageFile::EXPORT_FORMAT_INVALID = -1;
40 const int ImageFile::EXPORT_FORMAT_TEXT = 0;
41 const int ImageFile::EXPORT_FORMAT_PGM = 1;
42 const int ImageFile::EXPORT_FORMAT_PGMASCII = 2;
44 const int ImageFile::EXPORT_FORMAT_PNG = 3;
45 const int ImageFile::EXPORT_FORMAT_PNG16 = 4;
48 const int ImageFile::EXPORT_FORMAT_DICOM = 5;
50 const int ImageFile::EXPORT_FORMAT_RAW = 6;
52 const char* ImageFile::s_aszExportFormatName[] =
66 const char* ImageFile::s_aszExportFormatTitle[] =
77 const int ImageFile::s_iExportFormatCount = sizeof(s_aszExportFormatName) / sizeof(const char*);
80 const int ImageFile::IMPORT_FORMAT_INVALID = -1;
81 const int ImageFile::IMPORT_FORMAT_PPM = 0;
83 const int ImageFile::IMPORT_FORMAT_PNG = 1;
86 const int ImageFile::IMPORT_FORMAT_DICOM = 2;
90 const char* ImageFile::s_aszImportFormatName[] =
101 const char* ImageFile::s_aszImportFormatTitle[] =
105 #ifdef HAVE_CTN_DICOM
109 const int ImageFile::s_iImportFormatCount = sizeof(s_aszImportFormatName) / sizeof(const char*);
113 F32Image::F32Image (int nx, int ny, int dataType)
114 : Array2dFile (nx, ny, sizeof(kfloat32), Array2dFile::PIXEL_FLOAT32, dataType)
118 F32Image::F32Image (void)
121 setPixelFormat (Array2dFile::PIXEL_FLOAT32);
122 setPixelSize (sizeof(kfloat32));
123 setDataType (Array2dFile::DATA_TYPE_REAL);
126 F64Image::F64Image (int nx, int ny, int dataType)
127 : Array2dFile (nx, ny, sizeof(kfloat64), Array2dFile::PIXEL_FLOAT64, dataType)
131 F64Image::F64Image (void)
134 setPixelFormat (PIXEL_FLOAT64);
135 setPixelSize (sizeof(kfloat64));
136 setDataType (Array2dFile::DATA_TYPE_REAL);
140 ImageFile::getCenterCoordinates (unsigned int& iXCenter, unsigned int& iYCenter)
145 iXCenter = (m_nx - 1) / 2;
150 iYCenter = (m_ny - 1) / 2;
155 ImageFile::filterResponse (const char* const domainName, double bw, const char* const filterName,
156 double filt_param, double dInputScale, double dOutputScale)
158 ImageFileArray v = getArray();
159 SignalFilter filter (filterName, domainName, bw, filt_param);
161 unsigned int iXCenter, iYCenter;
162 getCenterCoordinates (iXCenter, iYCenter);
164 for (unsigned int ix = 0; ix < m_nx; ix++)
165 for (unsigned int iy = 0; iy < m_ny; iy++) {
166 long lD2 = ((ix - iXCenter) * (ix - iXCenter)) + ((iy - iYCenter) * (iy - iYCenter));
167 double r = ::sqrt (static_cast<double>(lD2)) * dInputScale;
168 v[ix][iy] = filter.response (r) * dOutputScale;
173 // ImageFile::comparativeStatistics Calculate comparative stats
176 // d Normalized root mean squared distance measure
177 // r Normalized mean absolute distance measure
178 // e Worst case distance measure
181 // G.T. Herman, Image Reconstruction From Projections, 1980
184 ImageFile::comparativeStatistics (const ImageFile& imComp, double& d, double& r, double& e) const
186 if (imComp.nx() != m_nx && imComp.ny() != m_ny) {
187 sys_error (ERR_WARNING, "Image sizes differ [ImageFile::comparativeStatistics]");
190 ImageFileArrayConst v = getArray();
191 if (v == NULL || m_nx == 0 || m_ny == 0)
194 ImageFileArrayConst vComp = imComp.getArray();
197 for (unsigned int ix = 0; ix < m_nx; ix++) {
198 for (unsigned int iy = 0; iy < m_ny; iy++) {
202 myMean /= (m_nx * m_ny);
204 double sqErrorSum = 0.;
205 double absErrorSum = 0.;
206 double sqDiffFromMeanSum = 0.;
207 double absValueSum = 0.;
208 for (unsigned int ix2 = 0; ix2 < m_nx; ix2++) {
209 for (unsigned int iy = 0; iy < m_ny; iy++) {
210 double diff = v[ix2][iy] - vComp[ix2][iy];
211 sqErrorSum += diff * diff;
212 absErrorSum += fabs(diff);
213 double diffFromMean = v[ix2][iy] - myMean;
214 sqDiffFromMeanSum += diffFromMean * diffFromMean;
215 absValueSum += fabs(v[ix2][iy]);
219 d = ::sqrt (sqErrorSum / sqDiffFromMeanSum);
220 r = absErrorSum / absValueSum;
225 for (int ix3 = 0; ix3 < hx; ix3++) {
226 for (int iy = 0; iy < hy; iy++) {
227 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]);
228 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]);
229 double error = fabs (avgPixel - avgPixelComp);
242 ImageFile::printComparativeStatistics (const ImageFile& imComp, std::ostream& os) const
246 if (comparativeStatistics (imComp, d, r, e)) {
247 os << " Normalized root mean squared distance (d): " << d << std::endl;
248 os << " Normalized mean absolute distance (r): " << r << std::endl;
249 os << "Worst case distance (2x2 pixel average) (e): " << e << std::endl;
257 ImageFile::printStatistics (std::ostream& os) const
259 double min, max, mean, mode, median, stddev;
261 statistics (min, max, mean, mode, median, stddev);
263 os << "Real Component Statistics" << std::endl;
265 os << " min: " << min << std::endl;
266 os << " max: " << max << std::endl;
267 os << " mean: " << mean << std::endl;
268 os << " mode: " << mode << std::endl;
269 os << "median: " << median << std::endl;
270 os << "stddev: " << stddev << std::endl;
273 statistics (getImaginaryArray(), min, max, mean, mode, median, stddev);
274 os << std::endl << "Imaginary Component Statistics" << std::endl;
275 os << " min: " << min << std::endl;
276 os << " max: " << max << std::endl;
277 os << " mean: " << mean << std::endl;
278 os << " mode: " << mode << std::endl;
279 os << "median: " << median << std::endl;
280 os << "stddev: " << stddev << std::endl;
286 ImageFile::statistics (double& min, double& max, double& mean, double& mode, double& median, double& stddev) const
288 ImageFileArrayConst v = getArray();
289 statistics (v, min, max, mean, mode, median, stddev);
294 ImageFile::statistics (ImageFileArrayConst v, double& min, double& max, double& mean, double& mode, double& median, double& stddev) const
299 if (v == NULL || nx == 0 || ny == 0)
302 std::vector<double> vecImage;
304 vecImage.resize (nx * ny);
305 for (int ix = 0; ix < nx; ix++) {
306 for (int iy = 0; iy < ny; iy++)
307 vecImage[iVec++] = v[ix][iy];
310 vectorNumericStatistics (vecImage, nx * ny, min, max, mean, mode, median, stddev);
314 ImageFile::getMinMax (double& min, double& max) const
318 ImageFileArrayConst v = getArray();
320 if (v == NULL || nx == 0 || ny == 0)
325 for (int ix = 0; ix < nx; ix++) {
326 for (int iy = 0; iy < ny; iy++) {
336 ImageFile::convertRealToComplex ()
338 if (dataType() != Array2dFile::DATA_TYPE_REAL)
341 if (! reallocRealToComplex())
344 ImageFileArray vImag = getImaginaryArray();
345 for (unsigned int ix = 0; ix < m_nx; ix++) {
346 ImageFileColumn vCol = vImag[ix];
347 for (unsigned int iy = 0; iy < m_ny; iy++)
355 ImageFile::convertComplexToReal ()
357 if (dataType() != Array2dFile::DATA_TYPE_COMPLEX)
360 ImageFileArray vReal = getArray();
361 ImageFileArray vImag = getImaginaryArray();
362 for (unsigned int ix = 0; ix < m_nx; ix++) {
363 ImageFileColumn vRealCol = vReal[ix];
364 ImageFileColumn vImagCol = vImag[ix];
365 for (unsigned int iy = 0; iy < m_ny; iy++) {
366 CTSimComplex c (*vRealCol, *vImagCol);
367 *vRealCol++ = std::abs (c);
372 return reallocComplexToReal();
376 ImageFile::subtractImages (const ImageFile& rRHS, ImageFile& result) const
378 if (m_nx != rRHS.nx() || m_ny != rRHS.ny() || m_nx != result.nx() || m_ny != result.ny()) {
379 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::subtractImage]");
383 if (isComplex() || rRHS.isComplex() && ! result.isComplex())
384 result.convertRealToComplex();
386 ImageFileArrayConst vLHS = getArray();
387 ImageFileArrayConst vLHSImag = getImaginaryArray();
388 ImageFileArrayConst vRHS = rRHS.getArray();
389 ImageFileArrayConst vRHSImag = rRHS.getImaginaryArray();
390 ImageFileArray vResult = result.getArray();
391 ImageFileArray vResultImag = result.getImaginaryArray();
393 for (unsigned int ix = 0; ix < m_nx; ix++) {
394 for (unsigned int iy = 0; iy < m_ny; iy++) {
395 vResult[ix][iy] = vLHS[ix][iy] - vRHS[ix][iy];
396 if (result.isComplex()) {
397 vResultImag[ix][iy] = 0;
399 vResultImag[ix][iy] += vLHSImag[ix][iy];
400 if (rRHS.isComplex())
401 vResultImag[ix][iy] -= vRHSImag[ix][iy];
410 ImageFile::addImages (const ImageFile& rRHS, ImageFile& result) const
412 if (m_nx != rRHS.nx() || m_ny != rRHS.ny() || m_nx != result.nx() || m_ny != result.ny()) {
413 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::subtractImage]");
417 if (isComplex() || rRHS.isComplex() && ! result.isComplex())
418 result.convertRealToComplex();
420 ImageFileArrayConst vLHS = getArray();
421 ImageFileArrayConst vLHSImag = getImaginaryArray();
422 ImageFileArrayConst vRHS = rRHS.getArray();
423 ImageFileArrayConst vRHSImag = rRHS.getImaginaryArray();
424 ImageFileArray vResult = result.getArray();
425 ImageFileArray vResultImag = result.getImaginaryArray();
427 for (unsigned int ix = 0; ix < m_nx; ix++) {
428 for (unsigned int iy = 0; iy < m_ny; iy++) {
429 vResult[ix][iy] = vLHS[ix][iy] + vRHS[ix][iy];
430 if (result.isComplex()) {
431 vResultImag[ix][iy] = 0;
433 vResultImag[ix][iy] += vLHSImag[ix][iy];
434 if (rRHS.isComplex())
435 vResultImag[ix][iy] += vRHSImag[ix][iy];
444 ImageFile::multiplyImages (const ImageFile& rRHS, ImageFile& result) const
446 if (m_nx != rRHS.nx() || m_ny != rRHS.ny() || m_nx != result.nx() || m_ny != result.ny()) {
447 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::subtractImage]");
451 if (isComplex() || rRHS.isComplex() && ! result.isComplex())
452 result.convertRealToComplex();
454 ImageFileArrayConst vLHS = getArray();
455 ImageFileArrayConst vLHSImag = getImaginaryArray();
456 ImageFileArrayConst vRHS = rRHS.getArray();
457 ImageFileArrayConst vRHSImag = rRHS.getImaginaryArray();
458 ImageFileArray vResult = result.getArray();
459 ImageFileArray vResultImag = result.getImaginaryArray();
461 for (unsigned int ix = 0; ix < m_nx; ix++) {
462 for (unsigned int iy = 0; iy < m_ny; iy++) {
463 if (result.isComplex()) {
466 dImag = vLHSImag[ix][iy];
467 std::complex<double> cLHS (vLHS[ix][iy], dImag);
469 if (rRHS.isComplex())
470 dImag = vRHSImag[ix][iy];
471 std::complex<double> cRHS (vRHS[ix][iy], dImag);
472 std::complex<double> cResult = cLHS * cRHS;
473 vResult[ix][iy] = cResult.real();
474 vResultImag[ix][iy] = cResult.imag();
476 vResult[ix][iy] = vLHS[ix][iy] * vRHS[ix][iy];
485 ImageFile::divideImages (const ImageFile& rRHS, ImageFile& result) const
487 if (m_nx != rRHS.nx() || m_ny != rRHS.ny() || m_nx != result.nx() || m_ny != result.ny()) {
488 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::subtractImage]");
492 if (isComplex() || rRHS.isComplex() && ! result.isComplex())
493 result.convertRealToComplex();
495 ImageFileArrayConst vLHS = getArray();
496 ImageFileArrayConst vLHSImag = getImaginaryArray();
497 ImageFileArrayConst vRHS = rRHS.getArray();
498 ImageFileArrayConst vRHSImag = rRHS.getImaginaryArray();
499 ImageFileArray vResult = result.getArray();
500 ImageFileArray vResultImag = result.getImaginaryArray();
502 for (unsigned int ix = 0; ix < m_nx; ix++) {
503 for (unsigned int iy = 0; iy < m_ny; iy++) {
504 if (result.isComplex()) {
507 dImag = vLHSImag[ix][iy];
508 std::complex<double> cLHS (vLHS[ix][iy], dImag);
510 if (rRHS.isComplex())
511 dImag = vRHSImag[ix][iy];
512 std::complex<double> cRHS (vRHS[ix][iy], dImag);
513 std::complex<double> cResult = cLHS / cRHS;
514 vResult[ix][iy] = cResult.real();
515 vResultImag[ix][iy] = cResult.imag();
518 vResult[ix][iy] = vLHS[ix][iy] / vRHS[ix][iy];
530 ImageFile::invertPixelValues (ImageFile& result) const
532 if (m_nx != result.nx() || m_ny != result.ny()) {
533 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
537 if (isComplex() && ! result.isComplex())
538 result.convertRealToComplex();
540 ImageFileArrayConst vLHS = getArray();
541 ImageFileArray vResult = result.getArray();
543 for (unsigned int ix = 0; ix < m_nx; ix++) {
544 ImageFileColumnConst in = vLHS[ix];
545 ImageFileColumn out = vResult[ix];
546 for (unsigned int iy = 0; iy < m_ny; iy++)
554 ImageFile::sqrt (ImageFile& result) const
556 if (m_nx != result.nx() || m_ny != result.ny()) {
557 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
561 if (isComplex() && ! result.isComplex())
562 result.convertRealToComplex();
564 bool bComplexOutput = result.isComplex();
565 ImageFileArrayConst vLHS = getArray();
566 if (! bComplexOutput) // check if should convert to complex output
567 for (unsigned int ix = 0; ix < m_nx; ix++)
568 for (unsigned int iy = 0; iy < m_ny; iy++)
569 if (! bComplexOutput && vLHS[ix][iy] < 0) {
570 result.convertRealToComplex();
571 bComplexOutput = true;
575 ImageFileArrayConst vLHSImag = getImaginaryArray();
576 ImageFileArray vResult = result.getArray();
577 ImageFileArray vResultImag = result.getImaginaryArray();
579 for (unsigned int ix = 0; ix < m_nx; ix++) {
580 for (unsigned int iy = 0; iy < m_ny; iy++) {
581 if (result.isComplex()) {
584 dImag = vLHSImag[ix][iy];
585 std::complex<double> cLHS (vLHS[ix][iy], dImag);
586 std::complex<double> cResult = std::sqrt(cLHS);
587 vResult[ix][iy] = cResult.real();
588 vResultImag[ix][iy] = cResult.imag();
590 vResult[ix][iy] = ::sqrt (vLHS[ix][iy]);
599 ImageFile::log (ImageFile& result) const
601 if (m_nx != result.nx() || m_ny != result.ny()) {
602 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::log]");
606 if (isComplex() && ! result.isComplex())
607 result.convertRealToComplex();
609 ImageFileArrayConst vLHS = getArray();
610 ImageFileArrayConst vLHSImag = getImaginaryArray();
611 ImageFileArray vResult = result.getArray();
612 ImageFileArray vResultImag = result.getImaginaryArray();
614 for (unsigned int ix = 0; ix < m_nx; ix++) {
615 for (unsigned int iy = 0; iy < m_ny; iy++) {
616 if (result.isComplex()) {
619 dImag = vLHSImag[ix][iy];
620 std::complex<double> cLHS (vLHS[ix][iy], dImag);
621 std::complex<double> cResult = std::log (cLHS);
622 vResult[ix][iy] = cResult.real();
623 vResultImag[ix][iy] = cResult.imag();
625 if (vLHS[ix][iy] > 0)
626 vResult[ix][iy] = ::log (vLHS[ix][iy]);
638 ImageFile::exp (ImageFile& result) const
640 if (m_nx != result.nx() || m_ny != result.ny()) {
641 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
645 if (isComplex() && ! result.isComplex())
646 result.convertRealToComplex();
648 ImageFileArrayConst vLHS = getArray();
649 ImageFileArrayConst vLHSImag = getImaginaryArray();
650 ImageFileArray vResult = result.getArray();
651 ImageFileArray vResultImag = result.getImaginaryArray();
653 for (unsigned int ix = 0; ix < m_nx; ix++) {
654 for (unsigned int iy = 0; iy < m_ny; iy++) {
655 if (result.isComplex()) {
658 dImag = vLHSImag[ix][iy];
659 std::complex<double> cLHS (vLHS[ix][iy], dImag);
660 std::complex<double> cResult = std::exp (cLHS);
661 vResult[ix][iy] = cResult.real();
662 vResultImag[ix][iy] = cResult.imag();
664 vResult[ix][iy] = ::exp (vLHS[ix][iy]);
673 ImageFile::scaleImage (ImageFile& result) const
675 unsigned int nx = m_nx;
676 unsigned int ny = m_ny;
677 unsigned int newNX = result.nx();
678 unsigned int newNY = result.ny();
680 double dXScale = static_cast<double>(newNX) / static_cast<double>(nx);
681 double dYScale = static_cast<double>(newNY) / static_cast<double>(ny);
683 if (isComplex() && ! result.isComplex())
684 result.convertRealToComplex();
686 ImageFileArrayConst vReal = getArray();
687 ImageFileArrayConst vImag = getImaginaryArray();
688 ImageFileArray vResult = result.getArray();
689 ImageFileArray vResultImag = result.getImaginaryArray();
691 BilinearInterpolator<ImageFileValue> realInterp (vReal, nx, ny);
692 BilinearInterpolator<ImageFileValue> imagInterp (vImag, nx, ny);
694 for (unsigned int ix = 0; ix < newNX; ix++) {
695 for (unsigned int iy = 0; iy < newNY; iy++) {
696 double dXPos = ix / dXScale;
697 double dYPos = iy / dYScale;
698 vResult[ix][iy] = realInterp.interpolate (dXPos, dYPos);
699 if (result.isComplex())
701 vResultImag[ix][iy] = imagInterp.interpolate (dXPos, dYPos);
703 vResultImag[ix][iy] = 0;
712 ImageFile::fft (ImageFile& result) const
714 if (m_nx != result.nx() || m_ny != result.ny()) {
715 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
719 if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {
720 if (! result.convertRealToComplex ())
724 fftw_complex* in = new fftw_complex [m_nx * m_ny];
726 ImageFileArrayConst vReal = getArray();
727 ImageFileArrayConst vImag = getImaginaryArray();
730 unsigned int iArray = 0;
731 for (ix = 0; ix < m_nx; ix++) {
732 for (iy = 0; iy < m_ny; iy++) {
733 in[iArray].re = vReal[ix][iy];
735 in[iArray].im = vImag[ix][iy];
742 fftwnd_plan plan = fftw2d_create_plan (m_nx, m_ny, FFTW_FORWARD, FFTW_IN_PLACE | FFTW_ESTIMATE | FFTW_USE_WISDOM);
743 fftwnd_one (plan, in, NULL);
745 ImageFileArray vRealResult = result.getArray();
746 ImageFileArray vImagResult = result.getImaginaryArray();
748 unsigned int iScale = m_nx * m_ny;
749 for (ix = 0; ix < m_nx; ix++) {
750 for (iy = 0; iy < m_ny; iy++) {
751 vRealResult[ix][iy] = in[iArray].re / iScale;
752 vImagResult[ix][iy] = in[iArray].im / iScale;
757 fftwnd_destroy_plan (plan);
759 Fourier::shuffleFourierToNaturalOrder (result);
766 ImageFile::ifft (ImageFile& result) const
768 if (m_nx != result.nx() || m_ny != result.ny()) {
769 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
773 if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {
774 if (! result.convertRealToComplex ())
778 ImageFileArrayConst vReal = getArray();
779 ImageFileArrayConst vImag = getImaginaryArray();
780 ImageFileArray vRealResult = result.getArray();
781 ImageFileArray vImagResult = result.getImaginaryArray();
783 for (ix = 0; ix < m_nx; ix++) {
784 for (iy = 0; iy < m_ny; iy++) {
785 vRealResult[ix][iy] = vReal[ix][iy];
787 vImagResult[ix][iy] = vImag[ix][iy];
789 vImagResult[ix][iy] = 0;
793 Fourier::shuffleNaturalToFourierOrder (result);
795 fftw_complex* in = new fftw_complex [m_nx * m_ny];
797 unsigned int iArray = 0;
798 for (ix = 0; ix < m_nx; ix++) {
799 for (iy = 0; iy < m_ny; iy++) {
800 in[iArray].re = vRealResult[ix][iy];
801 in[iArray].im = vImagResult[ix][iy];
806 fftwnd_plan plan = fftw2d_create_plan (m_nx, m_ny, FFTW_BACKWARD, FFTW_IN_PLACE | FFTW_ESTIMATE | FFTW_USE_WISDOM);
808 fftwnd_one (plan, in, NULL);
811 for (ix = 0; ix < m_nx; ix++) {
812 for (iy = 0; iy < m_ny; iy++) {
813 vRealResult[ix][iy] = in[iArray].re;
814 vImagResult[ix][iy] = in[iArray].im;
818 fftwnd_destroy_plan (plan);
826 ImageFile::fftRows (ImageFile& result) const
828 if (m_nx != result.nx() || m_ny != result.ny()) {
829 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::fftRows]");
833 if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {
834 if (! result.convertRealToComplex ())
838 ImageFileArrayConst vReal = getArray();
839 ImageFileArrayConst vImag = getImaginaryArray();
841 fftw_plan plan = fftw_create_plan (m_nx, FFTW_FORWARD, FFTW_IN_PLACE | FFTW_ESTIMATE | FFTW_USE_WISDOM);
843 fftw_complex* in = new fftw_complex [m_nx];
844 std::complex<double>* pcRow = new std::complex<double> [m_nx];
845 for (unsigned int iy = 0; iy < m_ny; iy++) {
847 for (ix = 0; ix < m_nx; ix++) {
848 in[ix].re = vReal[ix][iy];
850 in[ix].im = vImag[ix][iy];
855 fftw_one (plan, in, NULL);
857 for (ix = 0; ix < m_nx; ix++)
858 pcRow[ix] = std::complex<double>(in[ix].re, in[ix].im);
860 Fourier::shuffleFourierToNaturalOrder (pcRow, m_nx);
861 for (ix = 0; ix < m_nx; ix++) {
862 vReal[ix][iy] = pcRow[ix].real() / m_nx;
863 vImag[ix][iy] = pcRow[ix].imag() / m_nx;
868 fftw_destroy_plan (plan);
875 ImageFile::ifftRows (ImageFile& result) const
877 if (m_nx != result.nx() || m_ny != result.ny()) {
878 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::fftRows]");
882 if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {
883 if (! result.convertRealToComplex ())
887 fftw_complex* in = new fftw_complex [m_nx];
889 ImageFileArrayConst vReal = getArray();
890 ImageFileArrayConst vImag = getImaginaryArray();
892 fftw_plan plan = fftw_create_plan (m_nx, FFTW_BACKWARD, FFTW_IN_PLACE | FFTW_ESTIMATE | FFTW_USE_WISDOM);
893 std::complex<double>* pcRow = new std::complex<double> [m_nx];
896 // unsigned int iArray = 0;
897 for (iy = 0; iy < m_ny; iy++) {
898 for (ix = 0; ix < m_nx; ix++) {
901 dImag = vImag[ix][iy];
902 pcRow[ix] = std::complex<double> (vReal[ix][iy], dImag);
905 Fourier::shuffleNaturalToFourierOrder (pcRow, m_nx);
907 for (ix = 0; ix < m_nx; ix++) {
908 in[ix].re = pcRow[ix].real();
909 in[ix].im = pcRow[ix].imag();
912 fftw_one (plan, in, NULL);
914 for (ix = 0; ix < m_nx; ix++) {
915 vReal[ix][iy] = in[ix].re;
916 vImag[ix][iy] = in[ix].im;
921 fftw_destroy_plan (plan);
928 ImageFile::fftCols (ImageFile& result) const
930 if (m_nx != result.nx() || m_ny != result.ny()) {
931 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::fftRows]");
935 if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {
936 if (! result.convertRealToComplex ())
940 ImageFileArrayConst vReal = getArray();
941 ImageFileArrayConst vImag = getImaginaryArray();
943 fftw_plan plan = fftw_create_plan (m_ny, FFTW_FORWARD, FFTW_IN_PLACE | FFTW_ESTIMATE | FFTW_USE_WISDOM);
945 std::complex<double>* pcCol = new std::complex<double> [m_ny];
946 fftw_complex* in = new fftw_complex [m_ny];
947 for (unsigned int ix = 0; ix < m_nx; ix++) {
949 for (iy = 0; iy < m_ny; iy++) {
950 in[iy].re = vReal[ix][iy];
952 in[iy].im = vImag[ix][iy];
957 fftw_one (plan, in, NULL);
959 for (iy = 0; iy < m_ny; iy++)
960 pcCol[iy] = std::complex<double>(in[iy].re, in[iy].im);
962 Fourier::shuffleFourierToNaturalOrder (pcCol, m_ny);
963 for (iy = 0; iy < m_ny; iy++) {
964 vReal[ix][iy] = pcCol[iy].real() / m_ny;
965 vImag[ix][iy] = pcCol[iy].imag() / m_ny;
970 fftw_destroy_plan (plan);
977 ImageFile::ifftCols (ImageFile& result) const
979 if (m_nx != result.nx() || m_ny != result.ny()) {
980 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::fftRows]");
984 if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {
985 if (! result.convertRealToComplex ())
989 fftw_complex* in = new fftw_complex [m_ny];
991 ImageFileArrayConst vReal = getArray();
992 ImageFileArrayConst vImag = getImaginaryArray();
994 fftw_plan plan = fftw_create_plan (m_ny, FFTW_BACKWARD, FFTW_IN_PLACE | FFTW_ESTIMATE | FFTW_USE_WISDOM);
995 std::complex<double>* pcCol = new std::complex<double> [m_ny];
998 // unsigned int iArray = 0;
999 for (ix = 0; ix < m_nx; ix++) {
1000 for (iy = 0; iy < m_ny; iy++) {
1003 dImag = vImag[ix][iy];
1004 pcCol[iy] = std::complex<double> (vReal[ix][iy], dImag);
1007 Fourier::shuffleNaturalToFourierOrder (pcCol, m_ny);
1009 for (iy = 0; iy < m_ny; iy++) {
1010 in[iy].re = pcCol[iy].real();
1011 in[iy].im = pcCol[iy].imag();
1014 fftw_one (plan, in, NULL);
1016 for (iy = 0; iy < m_ny; iy++) {
1017 vReal[ix][iy] = in[iy].re;
1018 vImag[ix][iy] = in[iy].im;
1023 fftw_destroy_plan (plan);
1034 ImageFile::fourier (ImageFile& result) const
1036 if (m_nx != result.nx() || m_ny != result.ny()) {
1037 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
1041 if (! result.isComplex())
1042 if (! result.convertRealToComplex ())
1045 ImageFileArrayConst vLHS = getArray();
1046 ImageFileArrayConst vLHSImag = getImaginaryArray();
1047 ImageFileArray vRealResult = result.getArray();
1048 ImageFileArray vImagResult = result.getImaginaryArray();
1050 unsigned int ix, iy;
1052 // alloc output matrix
1053 CTSimComplex** complexOut = new CTSimComplex* [m_nx];
1054 for (ix = 0; ix < m_nx; ix++)
1055 complexOut[ix] = new CTSimComplex [m_ny];
1057 // fourier each x column
1058 CTSimComplex* pY = new CTSimComplex [m_ny];
1059 for (ix = 0; ix < m_nx; ix++) {
1060 for (iy = 0; iy < m_ny; iy++) {
1063 dImag = vLHSImag[ix][iy];
1064 pY[iy] = std::complex<double>(vLHS[ix][iy], dImag);
1066 ProcessSignal::finiteFourierTransform (pY, complexOut[ix], m_ny, ProcessSignal::FORWARD);
1070 // fourier each y row
1071 CTSimComplex* pX = new CTSimComplex [m_nx];
1072 CTSimComplex* complexOutRow = new CTSimComplex [m_nx];
1073 for (iy = 0; iy < m_ny; iy++) {
1074 for (ix = 0; ix < m_nx; ix++)
1075 pX[ix] = complexOut[ix][iy];
1076 ProcessSignal::finiteFourierTransform (pX, complexOutRow, m_nx, ProcessSignal::FORWARD);
1077 for (ix = 0; ix < m_nx; ix++)
1078 complexOut[ix][iy] = complexOutRow[ix];
1081 delete [] complexOutRow;
1083 for (ix = 0; ix < m_nx; ix++)
1084 for (iy = 0; iy < m_ny; iy++) {
1085 vRealResult[ix][iy] = complexOut[ix][iy].real();
1086 vImagResult[ix][iy] = complexOut[ix][iy].imag();
1089 Fourier::shuffleFourierToNaturalOrder (result);
1091 // delete complexOut matrix
1092 for (ix = 0; ix < m_nx; ix++)
1093 delete [] complexOut[ix];
1094 delete [] complexOut;
1100 ImageFile::inverseFourier (ImageFile& result) const
1102 if (m_nx != result.nx() || m_ny != result.ny()) {
1103 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
1107 if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {
1108 if (! result.convertRealToComplex ())
1112 ImageFileArrayConst vLHSReal = getArray();
1113 ImageFileArrayConst vLHSImag = getImaginaryArray();
1114 ImageFileArray vRealResult = result.getArray();
1115 ImageFileArray vImagResult = result.getImaginaryArray();
1117 unsigned int ix, iy;
1118 // alloc 2d complex output matrix
1119 CTSimComplex** complexOut = new CTSimComplex* [m_nx];
1120 for (ix = 0; ix < m_nx; ix++)
1121 complexOut[ix] = new CTSimComplex [m_ny];
1123 // put input image into result
1124 for (ix = 0; ix < m_nx; ix++)
1125 for (iy = 0; iy < m_ny; iy++) {
1126 vRealResult[ix][iy] = vLHSReal[ix][iy];
1128 vImagResult[ix][iy] = vLHSImag[ix][iy];
1130 vImagResult[ix][iy] = 0;
1133 Fourier::shuffleNaturalToFourierOrder (result);
1135 // ifourier each x column
1136 CTSimComplex* pCol = new CTSimComplex [m_ny];
1137 for (ix = 0; ix < m_nx; ix++) {
1138 for (iy = 0; iy < m_ny; iy++) {
1139 pCol[iy] = std::complex<double> (vRealResult[ix][iy], vImagResult[ix][iy]);
1141 ProcessSignal::finiteFourierTransform (pCol, complexOut[ix], m_ny, ProcessSignal::BACKWARD);
1145 // ifourier each y row
1146 CTSimComplex* complexInRow = new CTSimComplex [m_nx];
1147 CTSimComplex* complexOutRow = new CTSimComplex [m_nx];
1148 for (iy = 0; iy < m_ny; iy++) {
1149 for (ix = 0; ix < m_nx; ix++)
1150 complexInRow[ix] = complexOut[ix][iy];
1151 ProcessSignal::finiteFourierTransform (complexInRow, complexOutRow, m_nx, ProcessSignal::BACKWARD);
1152 for (ix = 0; ix < m_nx; ix++)
1153 complexOut[ix][iy] = complexOutRow[ix];
1155 delete [] complexInRow;
1156 delete [] complexOutRow;
1158 for (ix = 0; ix < m_nx; ix++)
1159 for (iy = 0; iy < m_ny; iy++) {
1160 vRealResult[ix][iy] = complexOut[ix][iy].real();
1161 vImagResult[ix][iy] = complexOut[ix][iy].imag();
1164 // delete complexOut matrix
1165 for (ix = 0; ix < m_nx; ix++)
1166 delete [] complexOut[ix];
1167 delete [] complexOut;
1174 ImageFile::magnitude (ImageFile& result) const
1176 if (m_nx != result.nx() || m_ny != result.ny()) {
1177 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
1181 ImageFileArray vReal = getArray();
1182 ImageFileArray vImag = getImaginaryArray();
1183 ImageFileArray vRealResult = result.getArray();
1185 for (unsigned int ix = 0; ix < m_nx; ix++)
1186 for (unsigned int iy = 0; iy < m_ny; iy++) {
1188 vRealResult[ix][iy] = ::sqrt (vReal[ix][iy] * vReal[ix][iy] + vImag[ix][iy] * vImag[ix][iy]);
1190 vRealResult[ix][iy] = ::fabs(vReal[ix][iy]);
1193 if (result.isComplex())
1194 result.reallocComplexToReal();
1200 ImageFile::phase (ImageFile& result) const
1202 if (m_nx != result.nx() || m_ny != result.ny()) {
1203 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
1207 ImageFileArray vReal = getArray();
1208 ImageFileArray vImag = getImaginaryArray();
1209 ImageFileArray vRealResult = result.getArray();
1211 for (unsigned int ix = 0; ix < m_nx; ix++) {
1212 for (unsigned int iy = 0; iy < m_ny; iy++) {
1214 vRealResult[ix][iy] = ::atan2 (vImag[ix][iy], vReal[ix][iy]);
1216 vRealResult[ix][iy] = 0;
1219 if (result.isComplex())
1220 result.reallocComplexToReal();
1226 ImageFile::real (ImageFile& result) const
1228 if (m_nx != result.nx() || m_ny != result.ny()) {
1229 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
1233 ImageFileArray vReal = getArray();
1234 ImageFileArray vRealResult = result.getArray();
1236 for (unsigned int ix = 0; ix < m_nx; ix++) {
1237 for (unsigned int iy = 0; iy < m_ny; iy++) {
1238 vRealResult[ix][iy] = vReal[ix][iy];
1242 if (result.isComplex())
1243 result.reallocComplexToReal();
1249 ImageFile::imaginary (ImageFile& result) const
1251 if (m_nx != result.nx() || m_ny != result.ny()) {
1252 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
1256 ImageFileArray vImag = getArray();
1257 ImageFileArray vRealResult = result.getArray();
1259 for (unsigned int ix = 0; ix < m_nx; ix++) {
1260 for (unsigned int iy = 0; iy < m_ny; iy++) {
1262 vRealResult[ix][iy] = vImag[ix][iy];
1264 vRealResult[ix][iy] = 0;
1268 if (result.isComplex())
1269 result.reallocComplexToReal();
1275 ImageFile::square (ImageFile& result) const
1277 if (m_nx != result.nx() || m_ny != result.ny()) {
1278 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
1282 if (isComplex() && ! result.isComplex())
1283 result.convertRealToComplex();
1285 ImageFileArrayConst vLHS = getArray();
1286 ImageFileArrayConst vLHSImag = getImaginaryArray();
1287 ImageFileArray vResult = result.getArray();
1288 ImageFileArray vResultImag = result.getImaginaryArray();
1290 for (unsigned int ix = 0; ix < m_nx; ix++) {
1291 for (unsigned int iy = 0; iy < m_ny; iy++) {
1292 if (result.isComplex()) {
1295 dImag = vLHSImag[ix][iy];
1296 std::complex<double> cLHS (vLHS[ix][iy], dImag);
1297 std::complex<double> cResult = cLHS * cLHS;
1298 vResult[ix][iy] = cResult.real();
1299 vResultImag[ix][iy] = cResult.imag();
1301 vResult[ix][iy] = vLHS[ix][iy] * vLHS[ix][iy];
1309 ImageFile::convertExportFormatNameToID (const char* const formatName)
1311 int formatID = EXPORT_FORMAT_INVALID;
1313 for (int i = 0; i < s_iExportFormatCount; i++)
1314 if (strcasecmp (formatName, s_aszExportFormatName[i]) == 0) {
1323 ImageFile::convertExportFormatIDToName (int formatID)
1325 static const char *formatName = "";
1327 if (formatID >= 0 && formatID < s_iExportFormatCount)
1328 return (s_aszExportFormatName[formatID]);
1330 return (formatName);
1334 ImageFile::convertExportFormatIDToTitle (const int formatID)
1336 static const char *formatTitle = "";
1338 if (formatID >= 0 && formatID < s_iExportFormatCount)
1339 return (s_aszExportFormatTitle[formatID]);
1341 return (formatTitle);
1345 ImageFile::convertImportFormatNameToID (const char* const formatName)
1347 int formatID = IMPORT_FORMAT_INVALID;
1349 for (int i = 0; i < s_iImportFormatCount; i++)
1350 if (strcasecmp (formatName, s_aszImportFormatName[i]) == 0) {
1359 ImageFile::convertImportFormatIDToName (int formatID)
1361 static const char *formatName = "";
1363 if (formatID >= 0 && formatID < s_iImportFormatCount)
1364 return (s_aszImportFormatName[formatID]);
1366 return (formatName);
1370 ImageFile::convertImportFormatIDToTitle (const int formatID)
1372 static const char *formatTitle = "";
1374 if (formatID >= 0 && formatID < s_iImportFormatCount)
1375 return (s_aszImportFormatTitle[formatID]);
1377 return (formatTitle);
1381 ImageFile::importImage (const char* const pszFormat, const char* const pszFilename)
1383 int iFormatID = convertImportFormatNameToID (pszFormat);
1385 if (iFormatID == IMPORT_FORMAT_PPM)
1386 return readImagePPM (pszFilename);
1388 else if (iFormatID == IMPORT_FORMAT_PNG)
1389 return readImagePNG (pszFilename);
1392 sys_error (ERR_SEVERE, "Invalid format %s [ImageFile::importImage]", pszFormat);
1397 ImageFile::skipSpacePPM (FILE* fp)
1400 while (isspace (c) || c == '#') {
1401 if (c == '#') { // comment until end of line
1403 while (c != 13 && c != 10)
1414 ImageFile::readImagePPM (const char* const pszFile)
1416 FILE* fp = fopen (pszFile, "r");
1417 if ((fp = fopen (pszFile, "r")) == NULL)
1419 char cSignature = toupper(fgetc(fp));
1420 if (cSignature != 'P') {
1424 cSignature = fgetc(fp);
1425 if (cSignature == '5' || cSignature == '6') { // binary modes
1427 fp = fopen(pszFile, "rb"); // reopen in binary mode
1430 } else if (cSignature != '2' && cSignature != '3') {
1435 int nRows, nCols, iMaxValue;
1437 if (fscanf (fp, "%d", &nCols) != 1) {
1442 if (fscanf (fp, "%d", &nRows) != 1) {
1447 if (fscanf (fp, "%d", &iMaxValue) != 1) {
1451 setArraySize (nRows, nCols);
1453 if (cSignature == '5' || cSignature == '6') { // binary modes
1457 if (c != 10) // read msdos 13-10 newline
1461 skipSpacePPM (fp); // ascii may have comments
1463 bool bMonochromeImage = false;
1464 double dMaxValue = iMaxValue;
1465 double dMaxValue3 = iMaxValue * 3;
1467 ImageFileArray v = getArray();
1468 for (int iy = nRows - 1; iy >= 0; iy--) {
1469 for (int ix = 0; ix < nCols; ix++) {
1470 int iGS, iR, iG, iB;
1472 switch (cSignature) {
1474 if (fscanf(fp, "%d ", &iGS) != 1) {
1478 v[ix][iy] = iGS / dMaxValue;
1486 v[ix][iy] = iGS / dMaxValue;
1489 if (fscanf (fp, "%d %d %d ", &iR, &iG, &iB) != 3) {
1493 if (ix == 0 && iy == 0 && (iR == iG && iG == iB))
1494 bMonochromeImage = true;
1495 if (bMonochromeImage)
1496 v[ix][iy] = (iR + iG + iB) / dMaxValue3;
1498 dR = iR / dMaxValue;
1499 dG = iG / dMaxValue;
1500 dB = iB / dMaxValue;
1501 v[ix][iy] = colorToGrayscale (dR, dG, dB);
1513 if (ix == 0 && iy == 0 && (iR == iG && iG == iB))
1514 bMonochromeImage = true;
1516 if (bMonochromeImage)
1517 v[ix][iy] = (iR + iG + iB) / dMaxValue3;
1519 dR = iR / dMaxValue;
1520 dG = iG / dMaxValue;
1521 dB = iB / dMaxValue;
1522 v[ix][iy] = colorToGrayscale (dR, dG, dB);
1535 ImageFile::readImagePNG (const char* const pszFile)
1537 FILE* fp = fopen(pszFile, "rb");
1540 unsigned char header[8];
1541 fread (header, 1, 8, fp);
1542 if (png_sig_cmp (header, 0, 8)) {
1547 png_structp png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
1553 png_infop info_ptr = png_create_info_struct(png_ptr);
1555 png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
1560 png_infop end_info = png_create_info_struct(png_ptr);
1562 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
1567 if (setjmp(png_ptr->jmpbuf)) {
1568 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
1573 png_init_io(png_ptr, fp);
1574 png_set_sig_bytes(png_ptr, 8);
1575 png_read_info(png_ptr, info_ptr);
1577 int width = png_get_image_width (png_ptr, info_ptr);
1578 int height = png_get_image_height (png_ptr, info_ptr);
1579 int bit_depth = png_get_bit_depth (png_ptr, info_ptr);
1580 int color_type = png_get_color_type (png_ptr, info_ptr);
1582 if (color_type == PNG_COLOR_TYPE_PALETTE && bit_depth <= 8)
1583 png_set_expand(png_ptr);
1585 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
1586 png_set_expand(png_ptr);
1589 png_set_packing(png_ptr);
1591 if (color_type & PNG_COLOR_MASK_ALPHA)
1592 png_set_strip_alpha(png_ptr);
1594 if (bit_depth == 16)
1595 png_set_swap(png_ptr); // convert to little-endian format
1597 png_read_update_info(png_ptr, info_ptr); // update with transformations
1598 int rowbytes = png_get_rowbytes (png_ptr, info_ptr);
1599 bit_depth = png_get_bit_depth (png_ptr, info_ptr);
1600 color_type = png_get_color_type (png_ptr, info_ptr);
1602 png_bytep* row_pointers = new png_bytep [height];
1604 for (i = 0; i < height; i++)
1605 row_pointers[i] = new unsigned char [rowbytes];
1607 png_read_image(png_ptr, row_pointers);
1609 setArraySize (width, height);
1610 ImageFileArray v = getArray();
1611 for (int iy = 0; iy < height; iy++) {
1612 for (int ix = 0; ix < width; ix++) {
1614 if (color_type == PNG_COLOR_TYPE_GRAY) {
1616 dV = row_pointers[iy][ix] / 255.;
1617 else if (bit_depth == 16) {
1619 dV = (row_pointers[iy][iBase] + (row_pointers[iy][iBase+1] << 8)) / 65536.;
1622 } else if (color_type == PNG_COLOR_TYPE_RGB) {
1623 if (bit_depth == 8) {
1625 double dR = row_pointers[iy][iBase] / 255.;
1626 double dG = row_pointers[iy][iBase+1] / 255.;
1627 double dB = row_pointers[iy][iBase+2] / 255.;
1628 dV = colorToGrayscale (dR, dG, dB);
1632 v[ix][height-iy-1] = dV;
1636 png_read_end(png_ptr, end_info);
1637 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
1639 for (i = 0; i < height; i++)
1640 delete row_pointers[i];
1641 delete row_pointers;
1649 ImageFile::exportImage (const char* const pszFormat, const char* const pszFilename, int nxcell, int nycell, double densmin, double densmax)
1651 int iFormatID = convertExportFormatNameToID (pszFormat);
1653 if (iFormatID == EXPORT_FORMAT_PGM)
1654 return writeImagePGM (pszFilename, nxcell, nycell, densmin, densmax);
1655 else if (iFormatID == EXPORT_FORMAT_PGMASCII)
1656 return writeImagePGMASCII (pszFilename, nxcell, nycell, densmin, densmax);
1657 else if (iFormatID == EXPORT_FORMAT_TEXT)
1658 return writeImageText (pszFilename);
1660 else if (iFormatID == EXPORT_FORMAT_PNG)
1661 return writeImagePNG (pszFilename, 8, nxcell, nycell, densmin, densmax);
1662 else if (iFormatID == EXPORT_FORMAT_PNG16)
1663 return writeImagePNG (pszFilename, 16, nxcell, nycell, densmin, densmax);
1665 #ifdef HAVE_CTN_DICOM
1666 else if (iFormatID == EXPORT_FORMAT_DICOM) {
1667 DicomExporter dicomExport (this);
1668 bool bSuccess = dicomExport.writeFile (pszFilename);
1670 sys_error (ERR_SEVERE, dicomExport.failMessage().c_str());
1674 else if (iFormatID == EXPORT_FORMAT_RAW)
1675 return writeImageRaw(pszFilename, nxcell, nycell);
1678 sys_error (ERR_SEVERE, "Invalid format %s [ImageFile::exportImage]", pszFormat);
1684 ImageFile::writeImagePGM (const char* const outfile, int nxcell, int nycell, double densmin, double densmax)
1689 ImageFileArray v = getArray();
1691 unsigned char* rowp = new unsigned char [nx * nxcell];
1693 if ((fp = fopen (outfile, "wb")) == NULL)
1696 fprintf(fp, "P5\n");
1697 fprintf(fp, "%d %d\n", nx, ny);
1698 fprintf(fp, "255\n");
1700 for (int irow = ny - 1; irow >= 0; irow--) {
1701 for (int icol = 0; icol < nx; icol++) {
1702 int pos = icol * nxcell;
1703 double dens = (v[icol][irow] - densmin) / (densmax - densmin);
1704 dens = clamp (dens, 0., 1.);
1705 for (int p = pos; p < pos + nxcell; p++) {
1706 rowp[p] = static_cast<unsigned int> (dens * 255.);
1709 for (int ir = 0; ir < nycell; ir++) {
1710 for (int ic = 0; ic < nx * nxcell; ic++)
1711 fputc( rowp[ic], fp );
1722 ImageFile::writeImagePGMASCII (const char* const outfile, int nxcell, int nycell, double densmin, double densmax)
1727 ImageFileArray v = getArray();
1729 unsigned char* rowp = new unsigned char [nx * nxcell];
1731 if ((fp = fopen (outfile, "wb")) == NULL)
1734 fprintf(fp, "P2\n");
1735 fprintf(fp, "%d %d\n", nx, ny);
1736 fprintf(fp, "255\n");
1738 for (int irow = ny - 1; irow >= 0; irow--) {
1739 for (int icol = 0; icol < nx; icol++) {
1740 int pos = icol * nxcell;
1741 double dens = (v[icol][irow] - densmin) / (densmax - densmin);
1742 dens = clamp (dens, 0., 1.);
1743 for (int p = pos; p < pos + nxcell; p++) {
1744 rowp[p] = static_cast<unsigned int> (dens * 255.);
1747 for (int ir = 0; ir < nycell; ir++) {
1748 for (int ic = 0; ic < nx * nxcell; ic++)
1749 fprintf(fp, "%d ", rowp[ic]);
1761 ImageFile::writeImageText (const char* const outfile)
1766 ImageFileArray v = getArray();
1767 ImageFileArray vImag = getImaginaryArray();
1769 if ((fp = fopen (outfile, "w")) == NULL)
1772 for (int irow = ny - 1; irow >= 0; irow--) {
1773 for (int icol = 0; icol < nx; icol++) {
1775 if (vImag[icol][irow] >= 0)
1776 fprintf (fp, "%.9g+%.9gi ", v[icol][irow], vImag[icol][irow]);
1778 fprintf (fp, "%.9g-%.9gi ", v[icol][irow], -vImag[icol][irow]);
1780 fprintf (fp, "%12.8g ", v[icol][irow]);
1793 ImageFile::writeImagePNG (const char* const outfile, int bitdepth, int nxcell, int nycell, double densmin, double densmax)
1795 double max_out_level = (1 << bitdepth) - 1;
1798 ImageFileArray v = getArray();
1800 unsigned char* rowp = new unsigned char [nx * nxcell * (bitdepth / 8)];
1802 FILE *fp = fopen (outfile, "wb");
1806 png_structp png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
1810 png_infop info_ptr = png_create_info_struct (png_ptr);
1812 png_destroy_write_struct (&png_ptr, (png_infopp) NULL);
1817 if (setjmp (png_ptr->jmpbuf)) {
1818 png_destroy_write_struct (&png_ptr, &info_ptr);
1823 png_init_io(png_ptr, fp);
1825 png_set_IHDR (png_ptr, info_ptr, nx * nxcell, ny * nycell, bitdepth, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
1827 png_write_info(png_ptr, info_ptr);
1828 for (int irow = ny - 1; irow >= 0; irow--) {
1829 png_bytep row_pointer = rowp;
1831 for (int icol = 0; icol < nx; icol++) {
1832 int pos = icol * nxcell;
1833 double dens = (v[icol][irow] - densmin) / (densmax - densmin);
1834 dens = clamp (dens, 0., 1.);
1835 unsigned int outval = static_cast<unsigned int> (dens * max_out_level);
1837 for (int p = pos; p < pos + nxcell; p++) {
1842 rowp[rowpos+1] = (outval >> 8) & 0xFF;
1843 rowp[rowpos] = (outval & 0xFF);
1847 for (int ir = 0; ir < nycell; ir++)
1848 png_write_rows (png_ptr, &row_pointer, 1);
1851 png_write_end (png_ptr, info_ptr);
1852 png_destroy_write_struct (&png_ptr, &info_ptr);
1863 static const int N_GRAYSCALE=256;
1866 ImageFile::writeImageGIF (const char* const outfile, int nxcell, int nycell, double densmin, double densmax)
1868 int gs_indices[N_GRAYSCALE];
1871 ImageFileArray v = getArray();
1873 unsigned char* rowp = new unsigned char [nx * nxcell];
1875 gdImagePtr gif = gdImageCreate(nx * nxcell, ny * nycell);
1876 for (int i = 0; i < N_GRAYSCALE; i++)
1877 gs_indices[i] = gdImageColorAllocate(gif, i, i, i);
1879 int lastrow = ny * nycell - 1;
1880 for (int irow = 0; irow < ny; irow++) {
1881 int rpos = irow * nycell;
1882 for (int ir = rpos; ir < rpos + nycell; ir++) {
1883 for (int icol = 0; icol < nx; icol++) {
1884 int cpos = icol * nxcell;
1885 double dens = (v[icol][irow] - densmin) / (densmax - densmin);
1886 dens = clamp(dens, 0., 1.);
1887 for (int ic = cpos; ic < cpos + nxcell; ic++) {
1888 rowp[ic] = (unsigned int) (dens * (double) (N_GRAYSCALE - 1));
1889 gdImageSetPixel(gif, ic, lastrow - ir, gs_indices[rowp[ic]]);
1896 if ((out = fopen (outfile,"w")) == NULL) {
1897 sys_error(ERR_SEVERE, "Error opening output file %s for writing", outfile);
1900 gdImageGif(gif,out);
1902 gdImageDestroy(gif);
1911 ImageFile::writeImageRaw (const char* const outfile, int nxcell, int nycell)
1916 ImageFileArray v = getArray();
1918 if ((fp = fopen (outfile, "wb")) == NULL)
1921 for (int irow = ny - 1; irow >= 0; irow--) {
1922 for (int icol = 0; icol < nx; icol++) {
1923 float dens = v[icol][irow];
1924 fwrite(&dens, sizeof(float), 1, fp);