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-2009 Kevin Rosenberg
12 ** This program is free software; you can redistribute it and/or modify
13 ** it under the terms of the GNU General Public License (version 2) as
14 ** published by the Free Software Foundation.
16 ** This program is distributed in the hope that it will be useful,
17 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ** GNU General Public License for more details.
21 ** You should have received a copy of the GNU General Public License
22 ** along with this program; if not, write to the Free Software
23 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 ******************************************************************************/
30 #include "interpolator.h"
32 const double ImageFile::s_dRedGrayscaleFactor = 0.299;
33 const double ImageFile::s_dGreenGrayscaleFactor = 0.587;
34 const double ImageFile::s_dBlueGrayscaleFactor = 0.114;
37 const int ImageFile::EXPORT_FORMAT_INVALID = -1;
38 const int ImageFile::EXPORT_FORMAT_TEXT = 0;
39 const int ImageFile::EXPORT_FORMAT_PGM = 1;
40 const int ImageFile::EXPORT_FORMAT_PGMASCII = 2;
42 const int ImageFile::EXPORT_FORMAT_PNG = 3;
43 const int ImageFile::EXPORT_FORMAT_PNG16 = 4;
46 const int ImageFile::EXPORT_FORMAT_DICOM = 5;
48 const int ImageFile::EXPORT_FORMAT_RAW = 6;
50 const char* ImageFile::s_aszExportFormatName[] =
64 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[] =
107 #ifdef HAVE_CTN_DICOM
111 const int ImageFile::s_iImportFormatCount = sizeof(s_aszImportFormatName) / sizeof(const char*);
115 F32Image::F32Image (int nx, int ny, int dataType)
116 : Array2dFile (nx, ny, sizeof(kfloat32), Array2dFile::PIXEL_FLOAT32, dataType)
120 F32Image::F32Image (void)
123 setPixelFormat (Array2dFile::PIXEL_FLOAT32);
124 setPixelSize (sizeof(kfloat32));
125 setDataType (Array2dFile::DATA_TYPE_REAL);
128 F64Image::F64Image (int nx, int ny, int dataType)
129 : Array2dFile (nx, ny, sizeof(kfloat64), Array2dFile::PIXEL_FLOAT64, dataType)
133 F64Image::F64Image (void)
136 setPixelFormat (PIXEL_FLOAT64);
137 setPixelSize (sizeof(kfloat64));
138 setDataType (Array2dFile::DATA_TYPE_REAL);
142 ImageFile::getCenterCoordinates (unsigned int& iXCenter, unsigned int& iYCenter)
147 iXCenter = (m_nx - 1) / 2;
152 iYCenter = (m_ny - 1) / 2;
157 ImageFile::filterResponse (const char* const domainName, double bw, const char* const filterName,
158 double filt_param, double dInputScale, double dOutputScale)
160 ImageFileArray v = getArray();
161 SignalFilter filter (filterName, domainName, bw, filt_param);
163 unsigned int iXCenter, iYCenter;
164 getCenterCoordinates (iXCenter, iYCenter);
166 for (unsigned int ix = 0; ix < m_nx; ix++)
167 for (unsigned int iy = 0; iy < m_ny; iy++) {
168 long lD2 = ((ix - iXCenter) * (ix - iXCenter)) + ((iy - iYCenter) * (iy - iYCenter));
169 double r = ::sqrt (static_cast<double>(lD2)) * dInputScale;
170 v[ix][iy] = filter.response (r) * dOutputScale;
175 // ImageFile::comparativeStatistics Calculate comparative stats
178 // d Normalized root mean squared distance measure
179 // r Normalized mean absolute distance measure
180 // e Worst case distance measure
183 // G.T. Herman, Image Reconstruction From Projections, 1980
186 ImageFile::comparativeStatistics (const ImageFile& imComp, double& d, double& r, double& e) const
188 if (imComp.nx() != m_nx && imComp.ny() != m_ny) {
189 sys_error (ERR_WARNING, "Image sizes differ [ImageFile::comparativeStatistics]");
192 ImageFileArrayConst v = getArray();
193 if (v == NULL || m_nx == 0 || m_ny == 0)
196 ImageFileArrayConst vComp = imComp.getArray();
199 for (unsigned int ix = 0; ix < m_nx; ix++) {
200 for (unsigned int iy = 0; iy < m_ny; iy++) {
204 myMean /= (m_nx * m_ny);
206 double sqErrorSum = 0.;
207 double absErrorSum = 0.;
208 double sqDiffFromMeanSum = 0.;
209 double absValueSum = 0.;
210 for (unsigned int ix2 = 0; ix2 < m_nx; ix2++) {
211 for (unsigned int iy = 0; iy < m_ny; iy++) {
212 double diff = v[ix2][iy] - vComp[ix2][iy];
213 sqErrorSum += diff * diff;
214 absErrorSum += fabs(diff);
215 double diffFromMean = v[ix2][iy] - myMean;
216 sqDiffFromMeanSum += diffFromMean * diffFromMean;
217 absValueSum += fabs(v[ix2][iy]);
221 d = ::sqrt (sqErrorSum / sqDiffFromMeanSum);
222 r = absErrorSum / absValueSum;
227 for (int ix3 = 0; ix3 < hx; ix3++) {
228 for (int iy = 0; iy < hy; iy++) {
229 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]);
230 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]);
231 double error = fabs (avgPixel - avgPixelComp);
244 ImageFile::printComparativeStatistics (const ImageFile& imComp, std::ostream& os) const
248 if (comparativeStatistics (imComp, d, r, e)) {
249 os << " Normalized root mean squared distance (d): " << d << std::endl;
250 os << " Normalized mean absolute distance (r): " << r << std::endl;
251 os << "Worst case distance (2x2 pixel average) (e): " << e << std::endl;
259 ImageFile::printStatistics (std::ostream& os) const
261 double min, max, mean, mode, median, stddev;
263 statistics (min, max, mean, mode, median, stddev);
265 os << "Real Component Statistics" << std::endl;
267 os << " min: " << min << std::endl;
268 os << " max: " << max << std::endl;
269 os << " mean: " << mean << std::endl;
270 os << " mode: " << mode << std::endl;
271 os << "median: " << median << std::endl;
272 os << "stddev: " << stddev << std::endl;
275 statistics (getImaginaryArray(), min, max, mean, mode, median, stddev);
276 os << std::endl << "Imaginary Component Statistics" << std::endl;
277 os << " min: " << min << std::endl;
278 os << " max: " << max << std::endl;
279 os << " mean: " << mean << std::endl;
280 os << " mode: " << mode << std::endl;
281 os << "median: " << median << std::endl;
282 os << "stddev: " << stddev << std::endl;
288 ImageFile::statistics (double& min, double& max, double& mean, double& mode, double& median, double& stddev) const
290 ImageFileArrayConst v = getArray();
291 statistics (v, min, max, mean, mode, median, stddev);
296 ImageFile::statistics (ImageFileArrayConst v, double& min, double& max, double& mean, double& mode, double& median, double& stddev) const
301 if (v == NULL || nx == 0 || ny == 0)
304 std::vector<double> vecImage;
306 vecImage.resize (nx * ny);
307 for (int ix = 0; ix < nx; ix++) {
308 for (int iy = 0; iy < ny; iy++)
309 vecImage[iVec++] = v[ix][iy];
312 vectorNumericStatistics (vecImage, nx * ny, min, max, mean, mode, median, stddev);
316 ImageFile::getMinMax (double& min, double& max) const
320 ImageFileArrayConst v = getArray();
322 if (v == NULL || nx == 0 || ny == 0)
327 for (int ix = 0; ix < nx; ix++) {
328 for (int iy = 0; iy < ny; iy++) {
338 ImageFile::convertRealToComplex ()
340 if (dataType() != Array2dFile::DATA_TYPE_REAL)
343 if (! reallocRealToComplex())
346 ImageFileArray vImag = getImaginaryArray();
347 for (unsigned int ix = 0; ix < m_nx; ix++) {
348 ImageFileColumn vCol = vImag[ix];
349 for (unsigned int iy = 0; iy < m_ny; iy++)
357 ImageFile::convertComplexToReal ()
359 if (dataType() != Array2dFile::DATA_TYPE_COMPLEX)
362 ImageFileArray vReal = getArray();
363 ImageFileArray vImag = getImaginaryArray();
364 for (unsigned int ix = 0; ix < m_nx; ix++) {
365 ImageFileColumn vRealCol = vReal[ix];
366 ImageFileColumn vImagCol = vImag[ix];
367 for (unsigned int iy = 0; iy < m_ny; iy++) {
368 CTSimComplex c (*vRealCol, *vImagCol);
369 *vRealCol++ = std::abs (c);
374 return reallocComplexToReal();
378 ImageFile::subtractImages (const ImageFile& rRHS, ImageFile& result) const
380 if (m_nx != rRHS.nx() || m_ny != rRHS.ny() || m_nx != result.nx() || m_ny != result.ny()) {
381 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::subtractImage]");
385 if (isComplex() || (rRHS.isComplex() && ! result.isComplex()))
386 result.convertRealToComplex();
388 ImageFileArrayConst vLHS = getArray();
389 ImageFileArrayConst vLHSImag = getImaginaryArray();
390 ImageFileArrayConst vRHS = rRHS.getArray();
391 ImageFileArrayConst vRHSImag = rRHS.getImaginaryArray();
392 ImageFileArray vResult = result.getArray();
393 ImageFileArray vResultImag = result.getImaginaryArray();
395 for (unsigned int ix = 0; ix < m_nx; ix++) {
396 for (unsigned int iy = 0; iy < m_ny; iy++) {
397 vResult[ix][iy] = vLHS[ix][iy] - vRHS[ix][iy];
398 if (result.isComplex()) {
399 vResultImag[ix][iy] = 0;
401 vResultImag[ix][iy] += vLHSImag[ix][iy];
402 if (rRHS.isComplex())
403 vResultImag[ix][iy] -= vRHSImag[ix][iy];
412 ImageFile::addImages (const ImageFile& rRHS, ImageFile& result) const
414 if (m_nx != rRHS.nx() || m_ny != rRHS.ny() || m_nx != result.nx() || m_ny != result.ny()) {
415 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::subtractImage]");
419 if (isComplex() || (rRHS.isComplex() && ! result.isComplex()))
420 result.convertRealToComplex();
422 ImageFileArrayConst vLHS = getArray();
423 ImageFileArrayConst vLHSImag = getImaginaryArray();
424 ImageFileArrayConst vRHS = rRHS.getArray();
425 ImageFileArrayConst vRHSImag = rRHS.getImaginaryArray();
426 ImageFileArray vResult = result.getArray();
427 ImageFileArray vResultImag = result.getImaginaryArray();
429 for (unsigned int ix = 0; ix < m_nx; ix++) {
430 for (unsigned int iy = 0; iy < m_ny; iy++) {
431 vResult[ix][iy] = vLHS[ix][iy] + vRHS[ix][iy];
432 if (result.isComplex()) {
433 vResultImag[ix][iy] = 0;
435 vResultImag[ix][iy] += vLHSImag[ix][iy];
436 if (rRHS.isComplex())
437 vResultImag[ix][iy] += vRHSImag[ix][iy];
446 ImageFile::multiplyImages (const ImageFile& rRHS, ImageFile& result) const
448 if (m_nx != rRHS.nx() || m_ny != rRHS.ny() || m_nx != result.nx() || m_ny != result.ny()) {
449 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::subtractImage]");
453 if (isComplex() || (rRHS.isComplex() && ! result.isComplex()))
454 result.convertRealToComplex();
456 ImageFileArrayConst vLHS = getArray();
457 ImageFileArrayConst vLHSImag = getImaginaryArray();
458 ImageFileArrayConst vRHS = rRHS.getArray();
459 ImageFileArrayConst vRHSImag = rRHS.getImaginaryArray();
460 ImageFileArray vResult = result.getArray();
461 ImageFileArray vResultImag = result.getImaginaryArray();
463 for (unsigned int ix = 0; ix < m_nx; ix++) {
464 for (unsigned int iy = 0; iy < m_ny; iy++) {
465 if (result.isComplex()) {
468 dImag = vLHSImag[ix][iy];
469 std::complex<double> cLHS (vLHS[ix][iy], dImag);
471 if (rRHS.isComplex())
472 dImag = vRHSImag[ix][iy];
473 std::complex<double> cRHS (vRHS[ix][iy], dImag);
474 std::complex<double> cResult = cLHS * cRHS;
475 vResult[ix][iy] = cResult.real();
476 vResultImag[ix][iy] = cResult.imag();
478 vResult[ix][iy] = vLHS[ix][iy] * vRHS[ix][iy];
487 ImageFile::divideImages (const ImageFile& rRHS, ImageFile& result) const
489 if (m_nx != rRHS.nx() || m_ny != rRHS.ny() || m_nx != result.nx() || m_ny != result.ny()) {
490 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::subtractImage]");
494 if (isComplex() || (rRHS.isComplex() && ! result.isComplex()))
495 result.convertRealToComplex();
497 ImageFileArrayConst vLHS = getArray();
498 ImageFileArrayConst vLHSImag = getImaginaryArray();
499 ImageFileArrayConst vRHS = rRHS.getArray();
500 ImageFileArrayConst vRHSImag = rRHS.getImaginaryArray();
501 ImageFileArray vResult = result.getArray();
502 ImageFileArray vResultImag = result.getImaginaryArray();
504 for (unsigned int ix = 0; ix < m_nx; ix++) {
505 for (unsigned int iy = 0; iy < m_ny; iy++) {
506 if (result.isComplex()) {
509 dImag = vLHSImag[ix][iy];
510 std::complex<double> cLHS (vLHS[ix][iy], dImag);
512 if (rRHS.isComplex())
513 dImag = vRHSImag[ix][iy];
514 std::complex<double> cRHS (vRHS[ix][iy], dImag);
515 std::complex<double> cResult = cLHS / cRHS;
516 vResult[ix][iy] = cResult.real();
517 vResultImag[ix][iy] = cResult.imag();
520 vResult[ix][iy] = vLHS[ix][iy] / vRHS[ix][iy];
532 ImageFile::invertPixelValues (ImageFile& result) const
534 if (m_nx != result.nx() || m_ny != result.ny()) {
535 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
539 if (isComplex() && ! result.isComplex())
540 result.convertRealToComplex();
542 ImageFileArrayConst vLHS = getArray();
543 ImageFileArray vResult = result.getArray();
545 for (unsigned int ix = 0; ix < m_nx; ix++) {
546 ImageFileColumnConst in = vLHS[ix];
547 ImageFileColumn out = vResult[ix];
548 for (unsigned int iy = 0; iy < m_ny; iy++)
556 ImageFile::sqrt (ImageFile& result) const
558 if (m_nx != result.nx() || m_ny != result.ny()) {
559 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
563 if (isComplex() && ! result.isComplex())
564 result.convertRealToComplex();
566 bool bComplexOutput = result.isComplex();
567 ImageFileArrayConst vLHS = getArray();
568 if (! bComplexOutput) // check if should convert to complex output
569 for (unsigned int ix = 0; ix < m_nx; ix++)
570 for (unsigned int iy = 0; iy < m_ny; iy++)
571 if (! bComplexOutput && vLHS[ix][iy] < 0) {
572 result.convertRealToComplex();
573 bComplexOutput = true;
577 ImageFileArrayConst vLHSImag = getImaginaryArray();
578 ImageFileArray vResult = result.getArray();
579 ImageFileArray vResultImag = result.getImaginaryArray();
581 for (unsigned int ix = 0; ix < m_nx; ix++) {
582 for (unsigned int iy = 0; iy < m_ny; iy++) {
583 if (result.isComplex()) {
586 dImag = vLHSImag[ix][iy];
587 std::complex<double> cLHS (vLHS[ix][iy], dImag);
588 std::complex<double> cResult = std::sqrt(cLHS);
589 vResult[ix][iy] = cResult.real();
590 vResultImag[ix][iy] = cResult.imag();
592 vResult[ix][iy] = ::sqrt (vLHS[ix][iy]);
601 ImageFile::log (ImageFile& result) const
603 if (m_nx != result.nx() || m_ny != result.ny()) {
604 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::log]");
608 if (isComplex() && ! result.isComplex())
609 result.convertRealToComplex();
611 ImageFileArrayConst vLHS = getArray();
612 ImageFileArrayConst vLHSImag = getImaginaryArray();
613 ImageFileArray vResult = result.getArray();
614 ImageFileArray vResultImag = result.getImaginaryArray();
616 for (unsigned int ix = 0; ix < m_nx; ix++) {
617 for (unsigned int iy = 0; iy < m_ny; iy++) {
618 if (result.isComplex()) {
621 dImag = vLHSImag[ix][iy];
622 std::complex<double> cLHS (vLHS[ix][iy], dImag);
623 std::complex<double> cResult = std::log (cLHS);
624 vResult[ix][iy] = cResult.real();
625 vResultImag[ix][iy] = cResult.imag();
627 if (vLHS[ix][iy] > 0)
628 vResult[ix][iy] = ::log (vLHS[ix][iy]);
640 ImageFile::exp (ImageFile& result) const
642 if (m_nx != result.nx() || m_ny != result.ny()) {
643 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
647 if (isComplex() && ! result.isComplex())
648 result.convertRealToComplex();
650 ImageFileArrayConst vLHS = getArray();
651 ImageFileArrayConst vLHSImag = getImaginaryArray();
652 ImageFileArray vResult = result.getArray();
653 ImageFileArray vResultImag = result.getImaginaryArray();
655 for (unsigned int ix = 0; ix < m_nx; ix++) {
656 for (unsigned int iy = 0; iy < m_ny; iy++) {
657 if (result.isComplex()) {
660 dImag = vLHSImag[ix][iy];
661 std::complex<double> cLHS (vLHS[ix][iy], dImag);
662 std::complex<double> cResult = std::exp (cLHS);
663 vResult[ix][iy] = cResult.real();
664 vResultImag[ix][iy] = cResult.imag();
666 vResult[ix][iy] = ::exp (vLHS[ix][iy]);
675 ImageFile::scaleImage (ImageFile& result) const
677 unsigned int nx = m_nx;
678 unsigned int ny = m_ny;
679 unsigned int newNX = result.nx();
680 unsigned int newNY = result.ny();
682 double dXScale = static_cast<double>(newNX) / static_cast<double>(nx);
683 double dYScale = static_cast<double>(newNY) / static_cast<double>(ny);
685 if (isComplex() && ! result.isComplex())
686 result.convertRealToComplex();
688 ImageFileArrayConst vReal = getArray();
689 ImageFileArrayConst vImag = getImaginaryArray();
690 ImageFileArray vResult = result.getArray();
691 ImageFileArray vResultImag = result.getImaginaryArray();
693 BilinearInterpolator<ImageFileValue> realInterp (vReal, nx, ny);
694 BilinearInterpolator<ImageFileValue> imagInterp (vImag, nx, ny);
696 for (unsigned int ix = 0; ix < newNX; ix++) {
697 for (unsigned int iy = 0; iy < newNY; iy++) {
698 double dXPos = ix / dXScale;
699 double dYPos = iy / dYScale;
700 vResult[ix][iy] = realInterp.interpolate (dXPos, dYPos);
701 if (result.isComplex()) {
703 vResultImag[ix][iy] = imagInterp.interpolate (dXPos, dYPos);
705 vResultImag[ix][iy] = 0;
716 ImageFile::fft (ImageFile& result) const
718 if (m_nx != result.nx() || m_ny != result.ny()) {
719 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
723 if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {
724 if (! result.convertRealToComplex ())
728 fftw_complex* in = static_cast<fftw_complex*> (fftw_malloc (sizeof(fftw_complex) * m_nx * m_ny));
730 ImageFileArrayConst vReal = getArray();
731 ImageFileArrayConst vImag = getImaginaryArray();
734 unsigned int iArray = 0;
735 for (ix = 0; ix < m_nx; ix++) {
736 for (iy = 0; iy < m_ny; iy++) {
737 in[iArray][0] = vReal[ix][iy];
739 in[iArray][1] = vImag[ix][iy];
746 fftw_plan plan = fftw_plan_dft_2d (m_nx, m_ny, in, in, FFTW_FORWARD, FFTW_ESTIMATE);
749 ImageFileArray vRealResult = result.getArray();
750 ImageFileArray vImagResult = result.getImaginaryArray();
752 unsigned int iScale = m_nx * m_ny;
753 for (ix = 0; ix < m_nx; ix++) {
754 for (iy = 0; iy < m_ny; iy++) {
755 vRealResult[ix][iy] = in[iArray][0] / iScale;
756 vImagResult[ix][iy] = in[iArray][1] / iScale;
761 fftw_destroy_plan (plan);
763 Fourier::shuffleFourierToNaturalOrder (result);
770 ImageFile::ifft (ImageFile& result) const
772 if (m_nx != result.nx() || m_ny != result.ny()) {
773 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
777 if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {
778 if (! result.convertRealToComplex ())
782 ImageFileArrayConst vReal = getArray();
783 ImageFileArrayConst vImag = getImaginaryArray();
784 ImageFileArray vRealResult = result.getArray();
785 ImageFileArray vImagResult = result.getImaginaryArray();
787 for (ix = 0; ix < m_nx; ix++) {
788 for (iy = 0; iy < m_ny; iy++) {
789 vRealResult[ix][iy] = vReal[ix][iy];
791 vImagResult[ix][iy] = vImag[ix][iy];
793 vImagResult[ix][iy] = 0;
797 Fourier::shuffleNaturalToFourierOrder (result);
799 fftw_complex* in = static_cast<fftw_complex*>(fftw_malloc(sizeof(fftw_complex) * m_nx * m_ny));
801 unsigned int iArray = 0;
802 for (ix = 0; ix < m_nx; ix++) {
803 for (iy = 0; iy < m_ny; iy++) {
804 in[iArray][0] = vRealResult[ix][iy];
805 in[iArray][1] = vImagResult[ix][iy];
810 fftw_plan plan = fftw_plan_dft_2d (m_nx, m_ny, in, in, FFTW_BACKWARD, FFTW_ESTIMATE);
815 for (ix = 0; ix < m_nx; ix++) {
816 for (iy = 0; iy < m_ny; iy++) {
817 vRealResult[ix][iy] = in[iArray][0];
818 vImagResult[ix][iy] = in[iArray][1];
822 fftw_destroy_plan (plan);
829 ImageFile::fftRows (ImageFile& result) const
831 if (m_nx != result.nx() || m_ny != result.ny()) {
832 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::fftRows]");
836 if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {
837 if (! result.convertRealToComplex ())
841 ImageFileArrayConst vReal = getArray();
842 ImageFileArrayConst vImag = getImaginaryArray();
844 fftw_complex* in = static_cast<fftw_complex*>(fftw_malloc(sizeof(fftw_complex) * m_nx));
845 fftw_plan plan = fftw_plan_dft_1d (m_nx, in, in, FFTW_FORWARD, FFTW_ESTIMATE);
847 std::complex<double>* pcRow = new std::complex<double> [m_nx];
848 for (unsigned int iy = 0; iy < m_ny; iy++) {
850 for (ix = 0; ix < m_nx; ix++) {
851 in[ix][0] = vReal[ix][iy];
853 in[ix][1] = vImag[ix][iy];
860 for (ix = 0; ix < m_nx; ix++)
861 pcRow[ix] = std::complex<double>(in[ix][0], in[ix][1]);
863 Fourier::shuffleFourierToNaturalOrder (pcRow, m_nx);
864 for (ix = 0; ix < m_nx; ix++) {
865 vReal[ix][iy] = pcRow[ix].real() / m_nx;
866 vImag[ix][iy] = pcRow[ix].imag() / m_nx;
871 fftw_destroy_plan (plan);
878 ImageFile::ifftRows (ImageFile& result) const
880 if (m_nx != result.nx() || m_ny != result.ny()) {
881 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::fftRows]");
885 if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {
886 if (! result.convertRealToComplex ())
890 ImageFileArrayConst vReal = getArray();
891 ImageFileArrayConst vImag = getImaginaryArray();
893 fftw_complex* in = static_cast<fftw_complex*>(fftw_malloc(sizeof(fftw_complex) * m_nx));
894 fftw_plan plan = fftw_plan_dft_1d (m_nx, in, in, FFTW_BACKWARD, FFTW_ESTIMATE);
895 std::complex<double>* pcRow = new std::complex<double> [m_nx];
898 // unsigned int iArray = 0;
899 for (iy = 0; iy < m_ny; iy++) {
900 for (ix = 0; ix < m_nx; ix++) {
903 dImag = vImag[ix][iy];
904 pcRow[ix] = std::complex<double> (vReal[ix][iy], dImag);
907 Fourier::shuffleNaturalToFourierOrder (pcRow, m_nx);
909 for (ix = 0; ix < m_nx; ix++) {
910 in[ix][0] = pcRow[ix].real();
911 in[ix][1] = pcRow[ix].imag();
916 for (ix = 0; ix < m_nx; ix++) {
917 vReal[ix][iy] = in[ix][0];
918 vImag[ix][iy] = in[ix][1];
923 fftw_destroy_plan (plan);
930 ImageFile::fftCols (ImageFile& result) const
932 if (m_nx != result.nx() || m_ny != result.ny()) {
933 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::fftRows]");
937 if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {
938 if (! result.convertRealToComplex ())
942 ImageFileArrayConst vReal = getArray();
943 ImageFileArrayConst vImag = getImaginaryArray();
945 fftw_complex* in = static_cast<fftw_complex*>(fftw_malloc(sizeof(fftw_complex) * m_ny));
946 fftw_plan plan = fftw_plan_dft_1d (m_ny, in, in, FFTW_FORWARD, FFTW_ESTIMATE);
948 std::complex<double>* pcCol = new std::complex<double> [m_ny];
949 for (unsigned int ix = 0; ix < m_nx; ix++) {
951 for (iy = 0; iy < m_ny; iy++) {
952 in[iy][0] = vReal[ix][iy];
954 in[iy][1] = vImag[ix][iy];
961 for (iy = 0; iy < m_ny; iy++)
962 pcCol[iy] = std::complex<double>(in[iy][0], in[iy][1]);
964 Fourier::shuffleFourierToNaturalOrder (pcCol, m_ny);
965 for (iy = 0; iy < m_ny; iy++) {
966 vReal[ix][iy] = pcCol[iy].real() / m_ny;
967 vImag[ix][iy] = pcCol[iy].imag() / m_ny;
972 fftw_destroy_plan (plan);
979 ImageFile::ifftCols (ImageFile& result) const
981 if (m_nx != result.nx() || m_ny != result.ny()) {
982 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::fftRows]");
986 if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {
987 if (! result.convertRealToComplex ())
991 ImageFileArrayConst vReal = getArray();
992 ImageFileArrayConst vImag = getImaginaryArray();
994 fftw_complex* in = static_cast<fftw_complex*>(fftw_malloc(sizeof(fftw_complex) * m_ny));
995 fftw_plan plan = fftw_plan_dft_1d (m_ny, in, in, FFTW_BACKWARD, FFTW_ESTIMATE);
996 std::complex<double>* pcCol = new std::complex<double> [m_ny];
999 // unsigned int iArray = 0;
1000 for (ix = 0; ix < m_nx; ix++) {
1001 for (iy = 0; iy < m_ny; iy++) {
1004 dImag = vImag[ix][iy];
1005 pcCol[iy] = std::complex<double> (vReal[ix][iy], dImag);
1008 Fourier::shuffleNaturalToFourierOrder (pcCol, m_ny);
1010 for (iy = 0; iy < m_ny; iy++) {
1011 in[iy][0] = pcCol[iy].real();
1012 in[iy][1] = pcCol[iy].imag();
1015 fftw_execute (plan);
1017 for (iy = 0; iy < m_ny; iy++) {
1018 vReal[ix][iy] = in[iy][0];
1019 vImag[ix][iy] = in[iy][1];
1024 fftw_destroy_plan (plan);
1035 ImageFile::fourier (ImageFile& result) const
1037 if (m_nx != result.nx() || m_ny != result.ny()) {
1038 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
1042 if (! result.isComplex())
1043 if (! result.convertRealToComplex ())
1046 ImageFileArrayConst vLHS = getArray();
1047 ImageFileArrayConst vLHSImag = getImaginaryArray();
1048 ImageFileArray vRealResult = result.getArray();
1049 ImageFileArray vImagResult = result.getImaginaryArray();
1051 unsigned int ix, iy;
1053 // alloc output matrix
1054 CTSimComplex** complexOut = new CTSimComplex* [m_nx];
1055 for (ix = 0; ix < m_nx; ix++)
1056 complexOut[ix] = new CTSimComplex [m_ny];
1058 // fourier each x column
1059 CTSimComplex* pY = new CTSimComplex [m_ny];
1060 for (ix = 0; ix < m_nx; ix++) {
1061 for (iy = 0; iy < m_ny; iy++) {
1064 dImag = vLHSImag[ix][iy];
1065 pY[iy] = std::complex<double>(vLHS[ix][iy], dImag);
1067 ProcessSignal::finiteFourierTransform (pY, complexOut[ix], m_ny, ProcessSignal::FORWARD);
1071 // fourier each y row
1072 CTSimComplex* pX = new CTSimComplex [m_nx];
1073 CTSimComplex* complexOutRow = new CTSimComplex [m_nx];
1074 for (iy = 0; iy < m_ny; iy++) {
1075 for (ix = 0; ix < m_nx; ix++)
1076 pX[ix] = complexOut[ix][iy];
1077 ProcessSignal::finiteFourierTransform (pX, complexOutRow, m_nx, ProcessSignal::FORWARD);
1078 for (ix = 0; ix < m_nx; ix++)
1079 complexOut[ix][iy] = complexOutRow[ix];
1082 delete [] complexOutRow;
1084 for (ix = 0; ix < m_nx; ix++)
1085 for (iy = 0; iy < m_ny; iy++) {
1086 vRealResult[ix][iy] = complexOut[ix][iy].real();
1087 vImagResult[ix][iy] = complexOut[ix][iy].imag();
1090 Fourier::shuffleFourierToNaturalOrder (result);
1092 // delete complexOut matrix
1093 for (ix = 0; ix < m_nx; ix++)
1094 delete [] complexOut[ix];
1095 delete [] complexOut;
1101 ImageFile::inverseFourier (ImageFile& result) const
1103 if (m_nx != result.nx() || m_ny != result.ny()) {
1104 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
1108 if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {
1109 if (! result.convertRealToComplex ())
1113 ImageFileArrayConst vLHSReal = getArray();
1114 ImageFileArrayConst vLHSImag = getImaginaryArray();
1115 ImageFileArray vRealResult = result.getArray();
1116 ImageFileArray vImagResult = result.getImaginaryArray();
1118 unsigned int ix, iy;
1119 // alloc 2d complex output matrix
1120 CTSimComplex** complexOut = new CTSimComplex* [m_nx];
1121 for (ix = 0; ix < m_nx; ix++)
1122 complexOut[ix] = new CTSimComplex [m_ny];
1124 // put input image into result
1125 for (ix = 0; ix < m_nx; ix++)
1126 for (iy = 0; iy < m_ny; iy++) {
1127 vRealResult[ix][iy] = vLHSReal[ix][iy];
1129 vImagResult[ix][iy] = vLHSImag[ix][iy];
1131 vImagResult[ix][iy] = 0;
1134 Fourier::shuffleNaturalToFourierOrder (result);
1136 // ifourier each x column
1137 CTSimComplex* pCol = new CTSimComplex [m_ny];
1138 for (ix = 0; ix < m_nx; ix++) {
1139 for (iy = 0; iy < m_ny; iy++) {
1140 pCol[iy] = std::complex<double> (vRealResult[ix][iy], vImagResult[ix][iy]);
1142 ProcessSignal::finiteFourierTransform (pCol, complexOut[ix], m_ny, ProcessSignal::BACKWARD);
1146 // ifourier each y row
1147 CTSimComplex* complexInRow = new CTSimComplex [m_nx];
1148 CTSimComplex* complexOutRow = new CTSimComplex [m_nx];
1149 for (iy = 0; iy < m_ny; iy++) {
1150 for (ix = 0; ix < m_nx; ix++)
1151 complexInRow[ix] = complexOut[ix][iy];
1152 ProcessSignal::finiteFourierTransform (complexInRow, complexOutRow, m_nx, ProcessSignal::BACKWARD);
1153 for (ix = 0; ix < m_nx; ix++)
1154 complexOut[ix][iy] = complexOutRow[ix];
1156 delete [] complexInRow;
1157 delete [] complexOutRow;
1159 for (ix = 0; ix < m_nx; ix++)
1160 for (iy = 0; iy < m_ny; iy++) {
1161 vRealResult[ix][iy] = complexOut[ix][iy].real();
1162 vImagResult[ix][iy] = complexOut[ix][iy].imag();
1165 // delete complexOut matrix
1166 for (ix = 0; ix < m_nx; ix++)
1167 delete [] complexOut[ix];
1168 delete [] complexOut;
1175 ImageFile::magnitude (ImageFile& result) const
1177 if (m_nx != result.nx() || m_ny != result.ny()) {
1178 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
1182 ImageFileArray vReal = getArray();
1183 ImageFileArray vImag = getImaginaryArray();
1184 ImageFileArray vRealResult = result.getArray();
1186 for (unsigned int ix = 0; ix < m_nx; ix++)
1187 for (unsigned int iy = 0; iy < m_ny; iy++) {
1189 vRealResult[ix][iy] = ::sqrt (vReal[ix][iy] * vReal[ix][iy] + vImag[ix][iy] * vImag[ix][iy]);
1191 vRealResult[ix][iy] = ::fabs(vReal[ix][iy]);
1194 if (result.isComplex())
1195 result.reallocComplexToReal();
1201 ImageFile::phase (ImageFile& result) const
1203 if (m_nx != result.nx() || m_ny != result.ny()) {
1204 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
1208 ImageFileArray vReal = getArray();
1209 ImageFileArray vImag = getImaginaryArray();
1210 ImageFileArray vRealResult = result.getArray();
1212 for (unsigned int ix = 0; ix < m_nx; ix++) {
1213 for (unsigned int iy = 0; iy < m_ny; iy++) {
1215 vRealResult[ix][iy] = ::atan2 (vImag[ix][iy], vReal[ix][iy]);
1217 vRealResult[ix][iy] = 0;
1220 if (result.isComplex())
1221 result.reallocComplexToReal();
1227 ImageFile::real (ImageFile& result) const
1229 if (m_nx != result.nx() || m_ny != result.ny()) {
1230 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
1234 ImageFileArray vReal = getArray();
1235 ImageFileArray vRealResult = result.getArray();
1237 for (unsigned int ix = 0; ix < m_nx; ix++) {
1238 for (unsigned int iy = 0; iy < m_ny; iy++) {
1239 vRealResult[ix][iy] = vReal[ix][iy];
1243 if (result.isComplex())
1244 result.reallocComplexToReal();
1250 ImageFile::imaginary (ImageFile& result) const
1252 if (m_nx != result.nx() || m_ny != result.ny()) {
1253 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
1257 ImageFileArray vImag = getArray();
1258 ImageFileArray vRealResult = result.getArray();
1260 for (unsigned int ix = 0; ix < m_nx; ix++) {
1261 for (unsigned int iy = 0; iy < m_ny; iy++) {
1263 vRealResult[ix][iy] = vImag[ix][iy];
1265 vRealResult[ix][iy] = 0;
1269 if (result.isComplex())
1270 result.reallocComplexToReal();
1276 ImageFile::square (ImageFile& result) const
1278 if (m_nx != result.nx() || m_ny != result.ny()) {
1279 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
1283 if (isComplex() && ! result.isComplex())
1284 result.convertRealToComplex();
1286 ImageFileArrayConst vLHS = getArray();
1287 ImageFileArrayConst vLHSImag = getImaginaryArray();
1288 ImageFileArray vResult = result.getArray();
1289 ImageFileArray vResultImag = result.getImaginaryArray();
1291 for (unsigned int ix = 0; ix < m_nx; ix++) {
1292 for (unsigned int iy = 0; iy < m_ny; iy++) {
1293 if (result.isComplex()) {
1296 dImag = vLHSImag[ix][iy];
1297 std::complex<double> cLHS (vLHS[ix][iy], dImag);
1298 std::complex<double> cResult = cLHS * cLHS;
1299 vResult[ix][iy] = cResult.real();
1300 vResultImag[ix][iy] = cResult.imag();
1302 vResult[ix][iy] = vLHS[ix][iy] * vLHS[ix][iy];
1310 ImageFile::convertExportFormatNameToID (const char* const formatName)
1312 int formatID = EXPORT_FORMAT_INVALID;
1314 for (int i = 0; i < s_iExportFormatCount; i++)
1315 if (strcasecmp (formatName, s_aszExportFormatName[i]) == 0) {
1324 ImageFile::convertExportFormatIDToName (int formatID)
1326 static const char *formatName = "";
1328 if (formatID >= 0 && formatID < s_iExportFormatCount)
1329 return (s_aszExportFormatName[formatID]);
1331 return (formatName);
1335 ImageFile::convertExportFormatIDToTitle (const int formatID)
1337 static const char *formatTitle = "";
1339 if (formatID >= 0 && formatID < s_iExportFormatCount)
1340 return (s_aszExportFormatTitle[formatID]);
1342 return (formatTitle);
1346 ImageFile::convertImportFormatNameToID (const char* const formatName)
1348 int formatID = IMPORT_FORMAT_INVALID;
1350 for (int i = 0; i < s_iImportFormatCount; i++)
1351 if (strcasecmp (formatName, s_aszImportFormatName[i]) == 0) {
1360 ImageFile::convertImportFormatIDToName (int formatID)
1362 static const char *formatName = "";
1364 if (formatID >= 0 && formatID < s_iImportFormatCount)
1365 return (s_aszImportFormatName[formatID]);
1367 return (formatName);
1371 ImageFile::convertImportFormatIDToTitle (const int formatID)
1373 static const char *formatTitle = "";
1375 if (formatID >= 0 && formatID < s_iImportFormatCount)
1376 return (s_aszImportFormatTitle[formatID]);
1378 return (formatTitle);
1382 ImageFile::importImage (const char* const pszFormat, const char* const pszFilename)
1384 int iFormatID = convertImportFormatNameToID (pszFormat);
1386 if (iFormatID == IMPORT_FORMAT_PPM)
1387 return readImagePPM (pszFilename);
1389 else if (iFormatID == IMPORT_FORMAT_PNG)
1390 return readImagePNG (pszFilename);
1393 sys_error (ERR_SEVERE, "Invalid format %s [ImageFile::importImage]", pszFormat);
1398 ImageFile::skipSpacePPM (FILE* fp)
1401 while (isspace (c) || c == '#') {
1402 if (c == '#') { // comment until end of line
1404 while (c != 13 && c != 10)
1415 ImageFile::readImagePPM (const char* const pszFile)
1417 FILE* fp = fopen (pszFile, "r");
1418 if ((fp = fopen (pszFile, "r")) == NULL)
1420 char cSignature = toupper(fgetc(fp));
1421 if (cSignature != 'P') {
1425 cSignature = fgetc(fp);
1426 if (cSignature == '5' || cSignature == '6') { // binary modes
1428 fp = fopen(pszFile, "rb"); // reopen in binary mode
1431 } else if (cSignature != '2' && cSignature != '3') {
1436 int nRows, nCols, iMaxValue;
1438 if (fscanf (fp, "%d", &nCols) != 1) {
1443 if (fscanf (fp, "%d", &nRows) != 1) {
1448 if (fscanf (fp, "%d", &iMaxValue) != 1) {
1452 setArraySize (nRows, nCols);
1454 if (cSignature == '5' || cSignature == '6') { // binary modes
1458 if (c != 10) // read msdos 13-10 newline
1462 skipSpacePPM (fp); // ascii may have comments
1464 bool bMonochromeImage = false;
1465 double dMaxValue = iMaxValue;
1466 double dMaxValue3 = iMaxValue * 3;
1468 ImageFileArray v = getArray();
1469 for (int iy = nRows - 1; iy >= 0; iy--) {
1470 for (int ix = 0; ix < nCols; ix++) {
1471 int iGS, iR, iG, iB;
1473 switch (cSignature) {
1475 if (fscanf(fp, "%d ", &iGS) != 1) {
1479 v[ix][iy] = iGS / dMaxValue;
1487 v[ix][iy] = iGS / dMaxValue;
1490 if (fscanf (fp, "%d %d %d ", &iR, &iG, &iB) != 3) {
1494 if (ix == 0 && iy == 0 && (iR == iG && iG == iB))
1495 bMonochromeImage = true;
1496 if (bMonochromeImage)
1497 v[ix][iy] = (iR + iG + iB) / dMaxValue3;
1499 dR = iR / dMaxValue;
1500 dG = iG / dMaxValue;
1501 dB = iB / dMaxValue;
1502 v[ix][iy] = colorToGrayscale (dR, dG, dB);
1514 if (ix == 0 && iy == 0 && (iR == iG && iG == iB))
1515 bMonochromeImage = true;
1517 if (bMonochromeImage)
1518 v[ix][iy] = (iR + iG + iB) / dMaxValue3;
1520 dR = iR / dMaxValue;
1521 dG = iG / dMaxValue;
1522 dB = iB / dMaxValue;
1523 v[ix][iy] = colorToGrayscale (dR, dG, dB);
1536 ImageFile::readImagePNG (const char* const pszFile)
1538 FILE* fp = fopen(pszFile, "rb");
1541 unsigned char header[8];
1542 fread (header, 1, 8, fp);
1543 if (png_sig_cmp (header, 0, 8)) {
1548 png_structp png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
1554 png_infop info_ptr = png_create_info_struct(png_ptr);
1556 png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
1561 png_infop end_info = png_create_info_struct(png_ptr);
1563 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
1568 if (setjmp(png_ptr->jmpbuf)) {
1569 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
1574 png_init_io(png_ptr, fp);
1575 png_set_sig_bytes(png_ptr, 8);
1576 png_read_info(png_ptr, info_ptr);
1578 int width = png_get_image_width (png_ptr, info_ptr);
1579 int height = png_get_image_height (png_ptr, info_ptr);
1580 int bit_depth = png_get_bit_depth (png_ptr, info_ptr);
1581 int color_type = png_get_color_type (png_ptr, info_ptr);
1583 if (color_type == PNG_COLOR_TYPE_PALETTE && bit_depth <= 8)
1584 png_set_expand(png_ptr);
1586 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
1587 png_set_expand(png_ptr);
1590 png_set_packing(png_ptr);
1592 if (color_type & PNG_COLOR_MASK_ALPHA)
1593 png_set_strip_alpha(png_ptr);
1595 if (bit_depth == 16)
1596 png_set_swap(png_ptr); // convert to little-endian format
1598 png_read_update_info(png_ptr, info_ptr); // update with transformations
1599 int rowbytes = png_get_rowbytes (png_ptr, info_ptr);
1600 bit_depth = png_get_bit_depth (png_ptr, info_ptr);
1601 color_type = png_get_color_type (png_ptr, info_ptr);
1603 png_bytep* row_pointers = new png_bytep [height];
1605 for (i = 0; i < height; i++)
1606 row_pointers[i] = new unsigned char [rowbytes];
1608 png_read_image(png_ptr, row_pointers);
1610 setArraySize (width, height);
1611 ImageFileArray v = getArray();
1612 for (int iy = 0; iy < height; iy++) {
1613 for (int ix = 0; ix < width; ix++) {
1615 if (color_type == PNG_COLOR_TYPE_GRAY) {
1617 dV = row_pointers[iy][ix] / 255.;
1618 else if (bit_depth == 16) {
1620 dV = (row_pointers[iy][iBase] + (row_pointers[iy][iBase+1] << 8)) / 65536.;
1623 } else if (color_type == PNG_COLOR_TYPE_RGB) {
1624 if (bit_depth == 8) {
1626 double dR = row_pointers[iy][iBase] / 255.;
1627 double dG = row_pointers[iy][iBase+1] / 255.;
1628 double dB = row_pointers[iy][iBase+2] / 255.;
1629 dV = colorToGrayscale (dR, dG, dB);
1633 v[ix][height-iy-1] = dV;
1637 png_read_end(png_ptr, end_info);
1638 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
1640 for (i = 0; i < height; i++)
1641 delete row_pointers[i];
1642 delete row_pointers;
1650 ImageFile::exportImage (const char* const pszFormat, const char* const pszFilename, int nxcell, int nycell, double densmin, double densmax)
1652 int iFormatID = convertExportFormatNameToID (pszFormat);
1654 if (iFormatID == EXPORT_FORMAT_PGM)
1655 return writeImagePGM (pszFilename, nxcell, nycell, densmin, densmax);
1656 else if (iFormatID == EXPORT_FORMAT_PGMASCII)
1657 return writeImagePGMASCII (pszFilename, nxcell, nycell, densmin, densmax);
1658 else if (iFormatID == EXPORT_FORMAT_TEXT)
1659 return writeImageText (pszFilename);
1661 else if (iFormatID == EXPORT_FORMAT_PNG)
1662 return writeImagePNG (pszFilename, 8, nxcell, nycell, densmin, densmax);
1663 else if (iFormatID == EXPORT_FORMAT_PNG16)
1664 return writeImagePNG (pszFilename, 16, nxcell, nycell, densmin, densmax);
1666 #ifdef HAVE_CTN_DICOM
1667 else if (iFormatID == EXPORT_FORMAT_DICOM) {
1668 DicomExporter dicomExport (this);
1669 bool bSuccess = dicomExport.writeFile (pszFilename);
1671 sys_error (ERR_SEVERE, dicomExport.failMessage().c_str());
1675 else if (iFormatID == EXPORT_FORMAT_RAW)
1676 return writeImageRaw(pszFilename, nxcell, nycell);
1679 sys_error (ERR_SEVERE, "Invalid format %s [ImageFile::exportImage]", pszFormat);
1685 ImageFile::writeImagePGM (const char* const outfile, int nxcell, int nycell, double densmin, double densmax)
1690 ImageFileArray v = getArray();
1692 unsigned char* rowp = new unsigned char [nx * nxcell];
1694 if ((fp = fopen (outfile, "wb")) == NULL)
1697 fprintf(fp, "P5\n");
1698 fprintf(fp, "%d %d\n", nx, ny);
1699 fprintf(fp, "255\n");
1701 for (int irow = ny - 1; irow >= 0; irow--) {
1702 for (int icol = 0; icol < nx; icol++) {
1703 int pos = icol * nxcell;
1704 double dens = (v[icol][irow] - densmin) / (densmax - densmin);
1705 dens = clamp (dens, 0., 1.);
1706 for (int p = pos; p < pos + nxcell; p++) {
1707 rowp[p] = static_cast<unsigned int> (dens * 255.);
1710 for (int ir = 0; ir < nycell; ir++) {
1711 for (int ic = 0; ic < nx * nxcell; ic++)
1712 fputc( rowp[ic], fp );
1723 ImageFile::writeImagePGMASCII (const char* const outfile, int nxcell, int nycell, double densmin, double densmax)
1728 ImageFileArray v = getArray();
1730 unsigned char* rowp = new unsigned char [nx * nxcell];
1732 if ((fp = fopen (outfile, "wb")) == NULL)
1735 fprintf(fp, "P2\n");
1736 fprintf(fp, "%d %d\n", nx, ny);
1737 fprintf(fp, "255\n");
1739 for (int irow = ny - 1; irow >= 0; irow--) {
1740 for (int icol = 0; icol < nx; icol++) {
1741 int pos = icol * nxcell;
1742 double dens = (v[icol][irow] - densmin) / (densmax - densmin);
1743 dens = clamp (dens, 0., 1.);
1744 for (int p = pos; p < pos + nxcell; p++) {
1745 rowp[p] = static_cast<unsigned int> (dens * 255.);
1748 for (int ir = 0; ir < nycell; ir++) {
1749 for (int ic = 0; ic < nx * nxcell; ic++)
1750 fprintf(fp, "%d ", rowp[ic]);
1762 ImageFile::writeImageText (const char* const outfile)
1767 ImageFileArray v = getArray();
1768 ImageFileArray vImag = getImaginaryArray();
1770 if ((fp = fopen (outfile, "w")) == NULL)
1773 for (int irow = ny - 1; irow >= 0; irow--) {
1774 for (int icol = 0; icol < nx; icol++) {
1776 if (vImag[icol][irow] >= 0)
1777 fprintf (fp, "%.9g+%.9gi ", v[icol][irow], vImag[icol][irow]);
1779 fprintf (fp, "%.9g-%.9gi ", v[icol][irow], -vImag[icol][irow]);
1781 fprintf (fp, "%12.8g ", v[icol][irow]);
1794 ImageFile::writeImagePNG (const char* const outfile, int bitdepth, int nxcell, int nycell, double densmin, double densmax)
1796 double max_out_level = (1 << bitdepth) - 1;
1799 ImageFileArray v = getArray();
1801 unsigned char* rowp = new unsigned char [nx * nxcell * (bitdepth / 8)];
1803 FILE *fp = fopen (outfile, "wb");
1807 png_structp png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
1811 png_infop info_ptr = png_create_info_struct (png_ptr);
1813 png_destroy_write_struct (&png_ptr, (png_infopp) NULL);
1818 if (setjmp (png_ptr->jmpbuf)) {
1819 png_destroy_write_struct (&png_ptr, &info_ptr);
1824 png_init_io(png_ptr, fp);
1826 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);
1828 png_write_info(png_ptr, info_ptr);
1829 for (int irow = ny - 1; irow >= 0; irow--) {
1830 png_bytep row_pointer = rowp;
1832 for (int icol = 0; icol < nx; icol++) {
1833 int pos = icol * nxcell;
1834 double dens = (v[icol][irow] - densmin) / (densmax - densmin);
1835 dens = clamp (dens, 0., 1.);
1836 unsigned int outval = static_cast<unsigned int> (dens * max_out_level);
1838 for (int p = pos; p < pos + nxcell; p++) {
1843 rowp[rowpos+1] = (outval >> 8) & 0xFF;
1844 rowp[rowpos] = (outval & 0xFF);
1848 for (int ir = 0; ir < nycell; ir++)
1849 png_write_rows (png_ptr, &row_pointer, 1);
1852 png_write_end (png_ptr, info_ptr);
1853 png_destroy_write_struct (&png_ptr, &info_ptr);
1864 static const int N_GRAYSCALE=256;
1867 ImageFile::writeImageGIF (const char* const outfile, int nxcell, int nycell, double densmin, double densmax)
1869 int gs_indices[N_GRAYSCALE];
1872 ImageFileArray v = getArray();
1874 unsigned char* rowp = new unsigned char [nx * nxcell];
1876 gdImagePtr gif = gdImageCreate(nx * nxcell, ny * nycell);
1877 for (int i = 0; i < N_GRAYSCALE; i++)
1878 gs_indices[i] = gdImageColorAllocate(gif, i, i, i);
1880 int lastrow = ny * nycell - 1;
1881 for (int irow = 0; irow < ny; irow++) {
1882 int rpos = irow * nycell;
1883 for (int ir = rpos; ir < rpos + nycell; ir++) {
1884 for (int icol = 0; icol < nx; icol++) {
1885 int cpos = icol * nxcell;
1886 double dens = (v[icol][irow] - densmin) / (densmax - densmin);
1887 dens = clamp(dens, 0., 1.);
1888 for (int ic = cpos; ic < cpos + nxcell; ic++) {
1889 rowp[ic] = (unsigned int) (dens * (double) (N_GRAYSCALE - 1));
1890 gdImageSetPixel(gif, ic, lastrow - ir, gs_indices[rowp[ic]]);
1897 if ((out = fopen (outfile,"w")) == NULL) {
1898 sys_error(ERR_SEVERE, "Error opening output file %s for writing", outfile);
1901 gdImageGif(gif,out);
1903 gdImageDestroy(gif);
1912 ImageFile::writeImageRaw (const char* const outfile, int nxcell, int nycell)
1917 ImageFileArray v = getArray();
1919 if ((fp = fopen (outfile, "wb")) == NULL)
1922 for (int irow = ny - 1; irow >= 0; irow--) {
1923 for (int icol = 0; icol < nx; icol++) {
1924 float dens = v[icol][irow];
1925 fwrite(&dens, sizeof(float), 1, fp);