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;
580 ImageFileArrayConst vLHSImag = getImaginaryArray();
581 ImageFileArray vResult = result.getArray();
582 ImageFileArray vResultImag = result.getImaginaryArray();
584 for (unsigned int ix = 0; ix < m_nx; ix++) {
585 for (unsigned int iy = 0; iy < m_ny; iy++) {
586 if (result.isComplex()) {
589 dImag = vLHSImag[ix][iy];
590 std::complex<double> cLHS (vLHS[ix][iy], dImag);
591 std::complex<double> cResult = std::sqrt(cLHS);
592 vResult[ix][iy] = cResult.real();
593 vResultImag[ix][iy] = cResult.imag();
595 vResult[ix][iy] = ::sqrt (vLHS[ix][iy]);
604 ImageFile::log (ImageFile& result) const
606 if (m_nx != result.nx() || m_ny != result.ny()) {
607 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::log]");
611 if (isComplex() && ! result.isComplex())
612 result.convertRealToComplex();
614 ImageFileArrayConst vLHS = getArray();
615 ImageFileArrayConst vLHSImag = getImaginaryArray();
616 ImageFileArray vResult = result.getArray();
617 ImageFileArray vResultImag = result.getImaginaryArray();
619 for (unsigned int ix = 0; ix < m_nx; ix++) {
620 for (unsigned int iy = 0; iy < m_ny; iy++) {
621 if (result.isComplex()) {
624 dImag = vLHSImag[ix][iy];
625 std::complex<double> cLHS (vLHS[ix][iy], dImag);
626 std::complex<double> cResult = std::log (cLHS);
627 vResult[ix][iy] = cResult.real();
628 vResultImag[ix][iy] = cResult.imag();
630 if (vLHS[ix][iy] > 0)
631 vResult[ix][iy] = ::log (vLHS[ix][iy]);
643 ImageFile::exp (ImageFile& result) const
645 if (m_nx != result.nx() || m_ny != result.ny()) {
646 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
650 if (isComplex() && ! result.isComplex())
651 result.convertRealToComplex();
653 ImageFileArrayConst vLHS = getArray();
654 ImageFileArrayConst vLHSImag = getImaginaryArray();
655 ImageFileArray vResult = result.getArray();
656 ImageFileArray vResultImag = result.getImaginaryArray();
658 for (unsigned int ix = 0; ix < m_nx; ix++) {
659 for (unsigned int iy = 0; iy < m_ny; iy++) {
660 if (result.isComplex()) {
663 dImag = vLHSImag[ix][iy];
664 std::complex<double> cLHS (vLHS[ix][iy], dImag);
665 std::complex<double> cResult = std::exp (cLHS);
666 vResult[ix][iy] = cResult.real();
667 vResultImag[ix][iy] = cResult.imag();
669 vResult[ix][iy] = ::exp (vLHS[ix][iy]);
678 ImageFile::scaleImage (ImageFile& result) const
680 unsigned int nx = m_nx;
681 unsigned int ny = m_ny;
682 unsigned int newNX = result.nx();
683 unsigned int newNY = result.ny();
685 double dXScale = static_cast<double>(newNX) / static_cast<double>(nx);
686 double dYScale = static_cast<double>(newNY) / static_cast<double>(ny);
688 if (isComplex() && ! result.isComplex())
689 result.convertRealToComplex();
691 ImageFileArrayConst vReal = getArray();
692 ImageFileArrayConst vImag = getImaginaryArray();
693 ImageFileArray vResult = result.getArray();
694 ImageFileArray vResultImag = result.getImaginaryArray();
696 BilinearInterpolator<ImageFileValue> realInterp (vReal, nx, ny);
697 BilinearInterpolator<ImageFileValue> imagInterp (vImag, nx, ny);
699 for (unsigned int ix = 0; ix < newNX; ix++) {
700 for (unsigned int iy = 0; iy < newNY; iy++) {
701 double dXPos = ix / dXScale;
702 double dYPos = iy / dYScale;
703 vResult[ix][iy] = realInterp.interpolate (dXPos, dYPos);
704 if (result.isComplex()) {
706 vResultImag[ix][iy] = imagInterp.interpolate (dXPos, dYPos);
708 vResultImag[ix][iy] = 0;
719 ImageFile::fft (ImageFile& result) const
721 if (m_nx != result.nx() || m_ny != result.ny()) {
722 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
726 if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {
727 if (! result.convertRealToComplex ())
731 fftw_complex* in = static_cast<fftw_complex*> (fftw_malloc (sizeof(fftw_complex) * m_nx * m_ny));
733 ImageFileArrayConst vReal = getArray();
734 ImageFileArrayConst vImag = getImaginaryArray();
737 unsigned int iArray = 0;
738 for (ix = 0; ix < m_nx; ix++) {
739 for (iy = 0; iy < m_ny; iy++) {
740 in[iArray][0] = vReal[ix][iy];
742 in[iArray][1] = vImag[ix][iy];
749 fftw_plan plan = fftw_plan_dft_2d (m_nx, m_ny, in, in, FFTW_FORWARD, FFTW_ESTIMATE);
752 ImageFileArray vRealResult = result.getArray();
753 ImageFileArray vImagResult = result.getImaginaryArray();
755 unsigned int iScale = m_nx * m_ny;
756 for (ix = 0; ix < m_nx; ix++) {
757 for (iy = 0; iy < m_ny; iy++) {
758 vRealResult[ix][iy] = in[iArray][0] / iScale;
759 vImagResult[ix][iy] = in[iArray][1] / iScale;
764 fftw_destroy_plan (plan);
766 Fourier::shuffleFourierToNaturalOrder (result);
773 ImageFile::ifft (ImageFile& result) const
775 if (m_nx != result.nx() || m_ny != result.ny()) {
776 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
780 if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {
781 if (! result.convertRealToComplex ())
785 ImageFileArrayConst vReal = getArray();
786 ImageFileArrayConst vImag = getImaginaryArray();
787 ImageFileArray vRealResult = result.getArray();
788 ImageFileArray vImagResult = result.getImaginaryArray();
790 for (ix = 0; ix < m_nx; ix++) {
791 for (iy = 0; iy < m_ny; iy++) {
792 vRealResult[ix][iy] = vReal[ix][iy];
794 vImagResult[ix][iy] = vImag[ix][iy];
796 vImagResult[ix][iy] = 0;
800 Fourier::shuffleNaturalToFourierOrder (result);
802 fftw_complex* in = static_cast<fftw_complex*>(fftw_malloc(sizeof(fftw_complex) * m_nx * m_ny));
804 unsigned int iArray = 0;
805 for (ix = 0; ix < m_nx; ix++) {
806 for (iy = 0; iy < m_ny; iy++) {
807 in[iArray][0] = vRealResult[ix][iy];
808 in[iArray][1] = vImagResult[ix][iy];
813 fftw_plan plan = fftw_plan_dft_2d (m_nx, m_ny, in, in, FFTW_BACKWARD, FFTW_ESTIMATE);
818 for (ix = 0; ix < m_nx; ix++) {
819 for (iy = 0; iy < m_ny; iy++) {
820 vRealResult[ix][iy] = in[iArray][0];
821 vImagResult[ix][iy] = in[iArray][1];
825 fftw_destroy_plan (plan);
832 ImageFile::fftRows (ImageFile& result) const
834 if (m_nx != result.nx() || m_ny != result.ny()) {
835 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::fftRows]");
839 if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {
840 if (! result.convertRealToComplex ())
844 ImageFileArrayConst vReal = getArray();
845 ImageFileArrayConst vImag = getImaginaryArray();
847 fftw_complex* in = static_cast<fftw_complex*>(fftw_malloc(sizeof(fftw_complex) * m_nx));
848 fftw_plan plan = fftw_plan_dft_1d (m_nx, in, in, FFTW_FORWARD, FFTW_ESTIMATE);
850 std::complex<double>* pcRow = new std::complex<double> [m_nx];
851 for (unsigned int iy = 0; iy < m_ny; iy++) {
853 for (ix = 0; ix < m_nx; ix++) {
854 in[ix][0] = vReal[ix][iy];
856 in[ix][1] = vImag[ix][iy];
863 for (ix = 0; ix < m_nx; ix++)
864 pcRow[ix] = std::complex<double>(in[ix][0], in[ix][1]);
866 Fourier::shuffleFourierToNaturalOrder (pcRow, m_nx);
867 for (ix = 0; ix < m_nx; ix++) {
868 vReal[ix][iy] = pcRow[ix].real() / m_nx;
869 vImag[ix][iy] = pcRow[ix].imag() / m_nx;
874 fftw_destroy_plan (plan);
881 ImageFile::ifftRows (ImageFile& result) const
883 if (m_nx != result.nx() || m_ny != result.ny()) {
884 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::fftRows]");
888 if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {
889 if (! result.convertRealToComplex ())
893 ImageFileArrayConst vReal = getArray();
894 ImageFileArrayConst vImag = getImaginaryArray();
896 fftw_complex* in = static_cast<fftw_complex*>(fftw_malloc(sizeof(fftw_complex) * m_nx));
897 fftw_plan plan = fftw_plan_dft_1d (m_nx, in, in, FFTW_BACKWARD, FFTW_ESTIMATE);
898 std::complex<double>* pcRow = new std::complex<double> [m_nx];
901 // unsigned int iArray = 0;
902 for (iy = 0; iy < m_ny; iy++) {
903 for (ix = 0; ix < m_nx; ix++) {
906 dImag = vImag[ix][iy];
907 pcRow[ix] = std::complex<double> (vReal[ix][iy], dImag);
910 Fourier::shuffleNaturalToFourierOrder (pcRow, m_nx);
912 for (ix = 0; ix < m_nx; ix++) {
913 in[ix][0] = pcRow[ix].real();
914 in[ix][1] = pcRow[ix].imag();
919 for (ix = 0; ix < m_nx; ix++) {
920 vReal[ix][iy] = in[ix][0];
921 vImag[ix][iy] = in[ix][1];
926 fftw_destroy_plan (plan);
933 ImageFile::fftCols (ImageFile& result) const
935 if (m_nx != result.nx() || m_ny != result.ny()) {
936 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::fftRows]");
940 if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {
941 if (! result.convertRealToComplex ())
945 ImageFileArrayConst vReal = getArray();
946 ImageFileArrayConst vImag = getImaginaryArray();
948 fftw_complex* in = static_cast<fftw_complex*>(fftw_malloc(sizeof(fftw_complex) * m_ny));
949 fftw_plan plan = fftw_plan_dft_1d (m_ny, in, in, FFTW_FORWARD, FFTW_ESTIMATE);
951 std::complex<double>* pcCol = new std::complex<double> [m_ny];
952 for (unsigned int ix = 0; ix < m_nx; ix++) {
954 for (iy = 0; iy < m_ny; iy++) {
955 in[iy][0] = vReal[ix][iy];
957 in[iy][1] = vImag[ix][iy];
964 for (iy = 0; iy < m_ny; iy++)
965 pcCol[iy] = std::complex<double>(in[iy][0], in[iy][1]);
967 Fourier::shuffleFourierToNaturalOrder (pcCol, m_ny);
968 for (iy = 0; iy < m_ny; iy++) {
969 vReal[ix][iy] = pcCol[iy].real() / m_ny;
970 vImag[ix][iy] = pcCol[iy].imag() / m_ny;
975 fftw_destroy_plan (plan);
982 ImageFile::ifftCols (ImageFile& result) const
984 if (m_nx != result.nx() || m_ny != result.ny()) {
985 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::fftRows]");
989 if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {
990 if (! result.convertRealToComplex ())
994 ImageFileArrayConst vReal = getArray();
995 ImageFileArrayConst vImag = getImaginaryArray();
997 fftw_complex* in = static_cast<fftw_complex*>(fftw_malloc(sizeof(fftw_complex) * m_ny));
998 fftw_plan plan = fftw_plan_dft_1d (m_ny, in, in, FFTW_BACKWARD, FFTW_ESTIMATE);
999 std::complex<double>* pcCol = new std::complex<double> [m_ny];
1001 unsigned int ix, iy;
1002 // unsigned int iArray = 0;
1003 for (ix = 0; ix < m_nx; ix++) {
1004 for (iy = 0; iy < m_ny; iy++) {
1007 dImag = vImag[ix][iy];
1008 pcCol[iy] = std::complex<double> (vReal[ix][iy], dImag);
1011 Fourier::shuffleNaturalToFourierOrder (pcCol, m_ny);
1013 for (iy = 0; iy < m_ny; iy++) {
1014 in[iy][0] = pcCol[iy].real();
1015 in[iy][1] = pcCol[iy].imag();
1018 fftw_execute (plan);
1020 for (iy = 0; iy < m_ny; iy++) {
1021 vReal[ix][iy] = in[iy][0];
1022 vImag[ix][iy] = in[iy][1];
1027 fftw_destroy_plan (plan);
1038 ImageFile::fourier (ImageFile& result) const
1040 if (m_nx != result.nx() || m_ny != result.ny()) {
1041 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
1045 if (! result.isComplex()) {
1046 if (! result.convertRealToComplex ()) {
1050 ImageFileArrayConst vLHS = getArray();
1051 ImageFileArrayConst vLHSImag = getImaginaryArray();
1052 ImageFileArray vRealResult = result.getArray();
1053 ImageFileArray vImagResult = result.getImaginaryArray();
1055 unsigned int ix, iy;
1057 // alloc output matrix
1058 CTSimComplex** complexOut = new CTSimComplex* [m_nx];
1059 for (ix = 0; ix < m_nx; ix++)
1060 complexOut[ix] = new CTSimComplex [m_ny];
1062 // fourier each x column
1063 CTSimComplex* pY = new CTSimComplex [m_ny];
1064 for (ix = 0; ix < m_nx; ix++) {
1065 for (iy = 0; iy < m_ny; iy++) {
1068 dImag = vLHSImag[ix][iy];
1069 pY[iy] = std::complex<double>(vLHS[ix][iy], dImag);
1071 ProcessSignal::finiteFourierTransform (pY, complexOut[ix], m_ny, ProcessSignal::FORWARD);
1075 // fourier each y row
1076 CTSimComplex* pX = new CTSimComplex [m_nx];
1077 CTSimComplex* complexOutRow = new CTSimComplex [m_nx];
1078 for (iy = 0; iy < m_ny; iy++) {
1079 for (ix = 0; ix < m_nx; ix++)
1080 pX[ix] = complexOut[ix][iy];
1081 ProcessSignal::finiteFourierTransform (pX, complexOutRow, m_nx, ProcessSignal::FORWARD);
1082 for (ix = 0; ix < m_nx; ix++)
1083 complexOut[ix][iy] = complexOutRow[ix];
1086 delete [] complexOutRow;
1088 for (ix = 0; ix < m_nx; ix++)
1089 for (iy = 0; iy < m_ny; iy++) {
1090 vRealResult[ix][iy] = complexOut[ix][iy].real();
1091 vImagResult[ix][iy] = complexOut[ix][iy].imag();
1094 Fourier::shuffleFourierToNaturalOrder (result);
1096 // delete complexOut matrix
1097 for (ix = 0; ix < m_nx; ix++)
1098 delete [] complexOut[ix];
1099 delete [] complexOut;
1105 ImageFile::inverseFourier (ImageFile& result) const
1107 if (m_nx != result.nx() || m_ny != result.ny()) {
1108 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
1112 if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {
1113 if (! result.convertRealToComplex ())
1117 ImageFileArrayConst vLHSReal = getArray();
1118 ImageFileArrayConst vLHSImag = getImaginaryArray();
1119 ImageFileArray vRealResult = result.getArray();
1120 ImageFileArray vImagResult = result.getImaginaryArray();
1122 unsigned int ix, iy;
1123 // alloc 2d complex output matrix
1124 CTSimComplex** complexOut = new CTSimComplex* [m_nx];
1125 for (ix = 0; ix < m_nx; ix++)
1126 complexOut[ix] = new CTSimComplex [m_ny];
1128 // put input image into result
1129 for (ix = 0; ix < m_nx; ix++) {
1130 for (iy = 0; iy < m_ny; iy++) {
1131 vRealResult[ix][iy] = vLHSReal[ix][iy];
1133 vImagResult[ix][iy] = vLHSImag[ix][iy];
1135 vImagResult[ix][iy] = 0;
1139 Fourier::shuffleNaturalToFourierOrder (result);
1141 // ifourier each x column
1142 CTSimComplex* pCol = new CTSimComplex [m_ny];
1143 for (ix = 0; ix < m_nx; ix++) {
1144 for (iy = 0; iy < m_ny; iy++) {
1145 pCol[iy] = std::complex<double> (vRealResult[ix][iy], vImagResult[ix][iy]);
1147 ProcessSignal::finiteFourierTransform (pCol, complexOut[ix], m_ny, ProcessSignal::BACKWARD);
1151 // ifourier each y row
1152 CTSimComplex* complexInRow = new CTSimComplex [m_nx];
1153 CTSimComplex* complexOutRow = new CTSimComplex [m_nx];
1154 for (iy = 0; iy < m_ny; iy++) {
1155 for (ix = 0; ix < m_nx; ix++)
1156 complexInRow[ix] = complexOut[ix][iy];
1157 ProcessSignal::finiteFourierTransform (complexInRow, complexOutRow, m_nx, ProcessSignal::BACKWARD);
1158 for (ix = 0; ix < m_nx; ix++)
1159 complexOut[ix][iy] = complexOutRow[ix];
1161 delete [] complexInRow;
1162 delete [] complexOutRow;
1164 for (ix = 0; ix < m_nx; ix++)
1165 for (iy = 0; iy < m_ny; iy++) {
1166 vRealResult[ix][iy] = complexOut[ix][iy].real();
1167 vImagResult[ix][iy] = complexOut[ix][iy].imag();
1170 // delete complexOut matrix
1171 for (ix = 0; ix < m_nx; ix++)
1172 delete [] complexOut[ix];
1173 delete [] complexOut;
1180 ImageFile::magnitude (ImageFile& result) const
1182 if (m_nx != result.nx() || m_ny != result.ny()) {
1183 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
1187 ImageFileArray vReal = getArray();
1188 ImageFileArray vImag = getImaginaryArray();
1189 ImageFileArray vRealResult = result.getArray();
1191 for (unsigned int ix = 0; ix < m_nx; ix++) {
1192 for (unsigned int iy = 0; iy < m_ny; iy++) {
1194 vRealResult[ix][iy] = ::sqrt (vReal[ix][iy] * vReal[ix][iy] + vImag[ix][iy] * vImag[ix][iy]);
1196 vRealResult[ix][iy] = ::fabs(vReal[ix][iy]);
1199 if (result.isComplex())
1200 result.reallocComplexToReal();
1206 ImageFile::phase (ImageFile& result) const
1208 if (m_nx != result.nx() || m_ny != result.ny()) {
1209 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
1213 ImageFileArray vReal = getArray();
1214 ImageFileArray vImag = getImaginaryArray();
1215 ImageFileArray vRealResult = result.getArray();
1217 for (unsigned int ix = 0; ix < m_nx; ix++) {
1218 for (unsigned int iy = 0; iy < m_ny; iy++) {
1220 vRealResult[ix][iy] = ::atan2 (vImag[ix][iy], vReal[ix][iy]);
1222 vRealResult[ix][iy] = 0;
1225 if (result.isComplex())
1226 result.reallocComplexToReal();
1232 ImageFile::real (ImageFile& result) const
1234 if (m_nx != result.nx() || m_ny != result.ny()) {
1235 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
1239 ImageFileArray vReal = getArray();
1240 ImageFileArray vRealResult = result.getArray();
1242 for (unsigned int ix = 0; ix < m_nx; ix++) {
1243 for (unsigned int iy = 0; iy < m_ny; iy++) {
1244 vRealResult[ix][iy] = vReal[ix][iy];
1248 if (result.isComplex())
1249 result.reallocComplexToReal();
1255 ImageFile::imaginary (ImageFile& result) const
1257 if (m_nx != result.nx() || m_ny != result.ny()) {
1258 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
1262 ImageFileArray vImag = getArray();
1263 ImageFileArray vRealResult = result.getArray();
1265 for (unsigned int ix = 0; ix < m_nx; ix++) {
1266 for (unsigned int iy = 0; iy < m_ny; iy++) {
1268 vRealResult[ix][iy] = vImag[ix][iy];
1270 vRealResult[ix][iy] = 0;
1274 if (result.isComplex())
1275 result.reallocComplexToReal();
1281 ImageFile::square (ImageFile& result) const
1283 if (m_nx != result.nx() || m_ny != result.ny()) {
1284 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
1288 if (isComplex() && ! result.isComplex())
1289 result.convertRealToComplex();
1291 ImageFileArrayConst vLHS = getArray();
1292 ImageFileArrayConst vLHSImag = getImaginaryArray();
1293 ImageFileArray vResult = result.getArray();
1294 ImageFileArray vResultImag = result.getImaginaryArray();
1296 for (unsigned int ix = 0; ix < m_nx; ix++) {
1297 for (unsigned int iy = 0; iy < m_ny; iy++) {
1298 if (result.isComplex()) {
1301 dImag = vLHSImag[ix][iy];
1302 std::complex<double> cLHS (vLHS[ix][iy], dImag);
1303 std::complex<double> cResult = cLHS * cLHS;
1304 vResult[ix][iy] = cResult.real();
1305 vResultImag[ix][iy] = cResult.imag();
1307 vResult[ix][iy] = vLHS[ix][iy] * vLHS[ix][iy];
1315 ImageFile::convertExportFormatNameToID (const char* const formatName)
1317 int formatID = EXPORT_FORMAT_INVALID;
1319 for (int i = 0; i < s_iExportFormatCount; i++) {
1320 if (strcasecmp (formatName, s_aszExportFormatName[i]) == 0) {
1329 ImageFile::convertExportFormatIDToName (int formatID)
1331 static const char *formatName = "";
1333 if (formatID >= 0 && formatID < s_iExportFormatCount) {
1334 return (s_aszExportFormatName[formatID]);
1336 return (formatName);
1340 ImageFile::convertExportFormatIDToTitle (const int formatID)
1342 static const char *formatTitle = "";
1344 if (formatID >= 0 && formatID < s_iExportFormatCount)
1345 return (s_aszExportFormatTitle[formatID]);
1347 return (formatTitle);
1351 ImageFile::convertImportFormatNameToID (const char* const formatName)
1353 int formatID = IMPORT_FORMAT_INVALID;
1355 for (int i = 0; i < s_iImportFormatCount; i++) {
1356 if (strcasecmp (formatName, s_aszImportFormatName[i]) == 0) {
1366 ImageFile::convertImportFormatIDToName (int formatID)
1368 static const char *formatName = "";
1370 if (formatID >= 0 && formatID < s_iImportFormatCount)
1371 return (s_aszImportFormatName[formatID]);
1373 return (formatName);
1377 ImageFile::convertImportFormatIDToTitle (const int formatID)
1379 static const char *formatTitle = "";
1381 if (formatID >= 0 && formatID < s_iImportFormatCount)
1382 return (s_aszImportFormatTitle[formatID]);
1384 return (formatTitle);
1388 ImageFile::importImage (const char* const pszFormat, const char* const pszFilename)
1390 int iFormatID = convertImportFormatNameToID (pszFormat);
1392 if (iFormatID == IMPORT_FORMAT_PPM)
1393 return readImagePPM (pszFilename);
1395 else if (iFormatID == IMPORT_FORMAT_PNG)
1396 return readImagePNG (pszFilename);
1399 sys_error (ERR_SEVERE, "Invalid format %s [ImageFile::importImage]", pszFormat);
1404 ImageFile::skipSpacePPM (FILE* fp)
1407 while (isspace (c) || c == '#') {
1408 if (c == '#') { // comment until end of line
1410 while (c != 13 && c != 10)
1421 ImageFile::readImagePPM (const char* const pszFile)
1423 FILE* fp = fopen (pszFile, "r");
1424 if ((fp = fopen (pszFile, "r")) == NULL)
1426 char cSignature = toupper(fgetc(fp));
1427 if (cSignature != 'P') {
1431 cSignature = fgetc(fp);
1432 if (cSignature == '5' || cSignature == '6') { // binary modes
1434 fp = fopen(pszFile, "rb"); // reopen in binary mode
1437 } else if (cSignature != '2' && cSignature != '3') {
1442 int nRows, nCols, iMaxValue;
1444 if (fscanf (fp, "%d", &nCols) != 1) {
1449 if (fscanf (fp, "%d", &nRows) != 1) {
1454 if (fscanf (fp, "%d", &iMaxValue) != 1) {
1458 setArraySize (nRows, nCols);
1460 if (cSignature == '5' || cSignature == '6') { // binary modes
1464 if (c != 10) // read msdos 13-10 newline
1468 skipSpacePPM (fp); // ascii may have comments
1470 bool bMonochromeImage = false;
1471 double dMaxValue = iMaxValue;
1472 double dMaxValue3 = iMaxValue * 3;
1474 ImageFileArray v = getArray();
1475 for (int iy = nRows - 1; iy >= 0; iy--) {
1476 for (int ix = 0; ix < nCols; ix++) {
1477 int iGS, iR, iG, iB;
1479 switch (cSignature) {
1481 if (fscanf(fp, "%d ", &iGS) != 1) {
1485 v[ix][iy] = iGS / dMaxValue;
1493 v[ix][iy] = iGS / dMaxValue;
1496 if (fscanf (fp, "%d %d %d ", &iR, &iG, &iB) != 3) {
1500 if (ix == 0 && iy == 0 && (iR == iG && iG == iB))
1501 bMonochromeImage = true;
1502 if (bMonochromeImage)
1503 v[ix][iy] = (iR + iG + iB) / dMaxValue3;
1505 dR = iR / dMaxValue;
1506 dG = iG / dMaxValue;
1507 dB = iB / dMaxValue;
1508 v[ix][iy] = colorToGrayscale (dR, dG, dB);
1520 if (ix == 0 && iy == 0 && (iR == iG && iG == iB))
1521 bMonochromeImage = true;
1523 if (bMonochromeImage)
1524 v[ix][iy] = (iR + iG + iB) / dMaxValue3;
1526 dR = iR / dMaxValue;
1527 dG = iG / dMaxValue;
1528 dB = iB / dMaxValue;
1529 v[ix][iy] = colorToGrayscale (dR, dG, dB);
1542 ImageFile::readImagePNG (const char* const pszFile)
1544 FILE* fp = fopen(pszFile, "rb");
1547 unsigned char header[8];
1548 int n = fread (header, 1, 8, fp);
1549 if (n != 8 || png_sig_cmp (header, 0, 8)) {
1554 png_structp png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
1560 png_infop info_ptr = png_create_info_struct(png_ptr);
1562 png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
1567 png_infop end_info = png_create_info_struct(png_ptr);
1569 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
1574 if (setjmp(png_jmpbuf(png_ptr))) {
1575 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
1580 png_init_io(png_ptr, fp);
1581 png_set_sig_bytes(png_ptr, 8);
1582 png_read_info(png_ptr, info_ptr);
1584 int width = png_get_image_width (png_ptr, info_ptr);
1585 int height = png_get_image_height (png_ptr, info_ptr);
1586 int bit_depth = png_get_bit_depth (png_ptr, info_ptr);
1587 int color_type = png_get_color_type (png_ptr, info_ptr);
1589 if (color_type == PNG_COLOR_TYPE_PALETTE && bit_depth <= 8)
1590 png_set_expand(png_ptr);
1592 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
1593 png_set_expand(png_ptr);
1596 png_set_packing(png_ptr);
1598 if (color_type & PNG_COLOR_MASK_ALPHA)
1599 png_set_strip_alpha(png_ptr);
1601 if (bit_depth == 16)
1602 png_set_swap(png_ptr); // convert to little-endian format
1604 png_read_update_info(png_ptr, info_ptr); // update with transformations
1605 int rowbytes = png_get_rowbytes (png_ptr, info_ptr);
1606 bit_depth = png_get_bit_depth (png_ptr, info_ptr);
1607 color_type = png_get_color_type (png_ptr, info_ptr);
1609 png_bytep* row_pointers = new png_bytep [height];
1611 for (i = 0; i < height; i++)
1612 row_pointers[i] = new unsigned char [rowbytes];
1614 png_read_image(png_ptr, row_pointers);
1616 setArraySize (width, height);
1617 ImageFileArray v = getArray();
1618 for (int iy = 0; iy < height; iy++) {
1619 for (int ix = 0; ix < width; ix++) {
1621 if (color_type == PNG_COLOR_TYPE_GRAY) {
1623 dV = row_pointers[iy][ix] / 255.;
1624 else if (bit_depth == 16) {
1626 dV = (row_pointers[iy][iBase] + (row_pointers[iy][iBase+1] << 8)) / 65536.;
1629 } else if (color_type == PNG_COLOR_TYPE_RGB) {
1630 if (bit_depth == 8) {
1632 double dR = row_pointers[iy][iBase] / 255.;
1633 double dG = row_pointers[iy][iBase+1] / 255.;
1634 double dB = row_pointers[iy][iBase+2] / 255.;
1635 dV = colorToGrayscale (dR, dG, dB);
1639 v[ix][height-iy-1] = dV;
1643 png_read_end(png_ptr, end_info);
1644 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
1646 for (i = 0; i < height; i++)
1647 delete row_pointers[i];
1648 delete row_pointers;
1656 ImageFile::exportImage (const char* const pszFormat, const char* const pszFilename, int nxcell, int nycell, double densmin, double densmax)
1658 int iFormatID = convertExportFormatNameToID (pszFormat);
1660 if (iFormatID == EXPORT_FORMAT_PGM)
1661 return writeImagePGM (pszFilename, nxcell, nycell, densmin, densmax);
1662 else if (iFormatID == EXPORT_FORMAT_PGMASCII)
1663 return writeImagePGMASCII (pszFilename, nxcell, nycell, densmin, densmax);
1664 else if (iFormatID == EXPORT_FORMAT_TEXT)
1665 return writeImageText (pszFilename);
1667 else if (iFormatID == EXPORT_FORMAT_PNG)
1668 return writeImagePNG (pszFilename, 8, nxcell, nycell, densmin, densmax);
1669 else if (iFormatID == EXPORT_FORMAT_PNG16)
1670 return writeImagePNG (pszFilename, 16, nxcell, nycell, densmin, densmax);
1672 #ifdef HAVE_CTN_DICOM
1673 else if (iFormatID == EXPORT_FORMAT_DICOM) {
1674 DicomExporter dicomExport (this);
1675 bool bSuccess = dicomExport.writeFile (pszFilename);
1677 sys_error (ERR_SEVERE, dicomExport.failMessage().c_str());
1681 else if (iFormatID == EXPORT_FORMAT_RAW)
1682 return writeImageRaw(pszFilename, nxcell, nycell);
1685 sys_error (ERR_SEVERE, "Invalid format %s [ImageFile::exportImage]", pszFormat);
1691 ImageFile::writeImagePGM (const char* const outfile, int nxcell, int nycell, double densmin, double densmax)
1696 ImageFileArray v = getArray();
1698 unsigned char* rowp = new unsigned char [nx * nxcell];
1700 if ((fp = fopen (outfile, "wb")) == NULL)
1703 fprintf(fp, "P5\n");
1704 fprintf(fp, "%d %d\n", nx, ny);
1705 fprintf(fp, "255\n");
1707 for (int irow = ny - 1; irow >= 0; irow--) {
1708 for (int icol = 0; icol < nx; icol++) {
1709 int pos = icol * nxcell;
1710 double dens = (v[icol][irow] - densmin) / (densmax - densmin);
1711 dens = clamp (dens, 0., 1.);
1712 for (int p = pos; p < pos + nxcell; p++) {
1713 rowp[p] = static_cast<unsigned int> (dens * 255.);
1716 for (int ir = 0; ir < nycell; ir++) {
1717 for (int ic = 0; ic < nx * nxcell; ic++)
1718 fputc( rowp[ic], fp );
1729 ImageFile::writeImagePGMASCII (const char* const outfile, int nxcell, int nycell, double densmin, double densmax)
1734 ImageFileArray v = getArray();
1736 unsigned char* rowp = new unsigned char [nx * nxcell];
1738 if ((fp = fopen (outfile, "wb")) == NULL)
1741 fprintf(fp, "P2\n");
1742 fprintf(fp, "%d %d\n", nx, ny);
1743 fprintf(fp, "255\n");
1745 for (int irow = ny - 1; irow >= 0; irow--) {
1746 for (int icol = 0; icol < nx; icol++) {
1747 int pos = icol * nxcell;
1748 double dens = (v[icol][irow] - densmin) / (densmax - densmin);
1749 dens = clamp (dens, 0., 1.);
1750 for (int p = pos; p < pos + nxcell; p++) {
1751 rowp[p] = static_cast<unsigned int> (dens * 255.);
1754 for (int ir = 0; ir < nycell; ir++) {
1755 for (int ic = 0; ic < nx * nxcell; ic++)
1756 fprintf(fp, "%d ", rowp[ic]);
1768 ImageFile::writeImageText (const char* const outfile)
1773 ImageFileArray v = getArray();
1774 ImageFileArray vImag = getImaginaryArray();
1776 if ((fp = fopen (outfile, "w")) == NULL)
1779 for (int irow = ny - 1; irow >= 0; irow--) {
1780 for (int icol = 0; icol < nx; icol++) {
1782 if (vImag[icol][irow] >= 0)
1783 fprintf (fp, "%.9g+%.9gi ", v[icol][irow], vImag[icol][irow]);
1785 fprintf (fp, "%.9g-%.9gi ", v[icol][irow], -vImag[icol][irow]);
1787 fprintf (fp, "%12.8g ", v[icol][irow]);
1800 ImageFile::writeImagePNG (const char* const outfile, int bitdepth, int nxcell, int nycell, double densmin, double densmax)
1802 double max_out_level = (1 << bitdepth) - 1;
1805 ImageFileArray v = getArray();
1807 unsigned char* rowp = new unsigned char [nx * nxcell * (bitdepth / 8)];
1809 FILE *fp = fopen (outfile, "wb");
1813 png_structp png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
1817 png_infop info_ptr = png_create_info_struct (png_ptr);
1819 png_destroy_write_struct (&png_ptr, (png_infopp) NULL);
1824 if (setjmp(png_jmpbuf(png_ptr))) {
1825 png_destroy_write_struct (&png_ptr, &info_ptr);
1830 png_init_io(png_ptr, fp);
1832 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);
1834 png_write_info(png_ptr, info_ptr);
1835 for (int irow = ny - 1; irow >= 0; irow--) {
1836 png_bytep row_pointer = rowp;
1838 for (int icol = 0; icol < nx; icol++) {
1839 int pos = icol * nxcell;
1840 double dens = (v[icol][irow] - densmin) / (densmax - densmin);
1841 dens = clamp (dens, 0., 1.);
1842 unsigned int outval = static_cast<unsigned int> (dens * max_out_level);
1844 for (int p = pos; p < pos + nxcell; p++) {
1849 rowp[rowpos+1] = (outval >> 8) & 0xFF;
1850 rowp[rowpos] = (outval & 0xFF);
1854 for (int ir = 0; ir < nycell; ir++)
1855 png_write_rows (png_ptr, &row_pointer, 1);
1858 png_write_end (png_ptr, info_ptr);
1859 png_destroy_write_struct (&png_ptr, &info_ptr);
1870 static const int N_GRAYSCALE=256;
1873 ImageFile::writeImageGIF (const char* const outfile, int nxcell, int nycell, double densmin, double densmax)
1875 int gs_indices[N_GRAYSCALE];
1878 ImageFileArray v = getArray();
1880 unsigned char* rowp = new unsigned char [nx * nxcell];
1882 gdImagePtr gif = gdImageCreate(nx * nxcell, ny * nycell);
1883 for (int i = 0; i < N_GRAYSCALE; i++)
1884 gs_indices[i] = gdImageColorAllocate(gif, i, i, i);
1886 int lastrow = ny * nycell - 1;
1887 for (int irow = 0; irow < ny; irow++) {
1888 int rpos = irow * nycell;
1889 for (int ir = rpos; ir < rpos + nycell; ir++) {
1890 for (int icol = 0; icol < nx; icol++) {
1891 int cpos = icol * nxcell;
1892 double dens = (v[icol][irow] - densmin) / (densmax - densmin);
1893 dens = clamp(dens, 0., 1.);
1894 for (int ic = cpos; ic < cpos + nxcell; ic++) {
1895 rowp[ic] = (unsigned int) (dens * (double) (N_GRAYSCALE - 1));
1896 gdImageSetPixel(gif, ic, lastrow - ir, gs_indices[rowp[ic]]);
1903 if ((out = fopen (outfile,"w")) == NULL) {
1904 sys_error(ERR_SEVERE, "Error opening output file %s for writing", outfile);
1907 gdImageGif(gif,out);
1909 gdImageDestroy(gif);
1918 ImageFile::writeImageRaw (const char* const outfile, int nxcell, int nycell)
1923 ImageFileArray v = getArray();
1925 if ((fp = fopen (outfile, "wb")) == NULL)
1928 for (int irow = ny - 1; irow >= 0; irow--) {
1929 for (int icol = 0; icol < nx; icol++) {
1930 float dens = v[icol][irow];
1931 fwrite(&dens, sizeof(float), 1, fp);