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
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[] =
79 const int ImageFile::s_iExportFormatCount = sizeof(s_aszExportFormatName) / sizeof(const char*);
82 const int ImageFile::IMPORT_FORMAT_INVALID = -1;
83 const int ImageFile::IMPORT_FORMAT_PPM = 0;
85 const int ImageFile::IMPORT_FORMAT_PNG = 1;
88 const int ImageFile::IMPORT_FORMAT_DICOM = 2;
92 const char* ImageFile::s_aszImportFormatName[] =
103 const char* ImageFile::s_aszImportFormatTitle[] =
109 #ifdef HAVE_CTN_DICOM
113 const int ImageFile::s_iImportFormatCount = sizeof(s_aszImportFormatName) / sizeof(const char*);
117 F32Image::F32Image (int nx, int ny, int dataType)
118 : Array2dFile (nx, ny, sizeof(kfloat32), Array2dFile::PIXEL_FLOAT32, dataType)
122 F32Image::F32Image (void)
125 setPixelFormat (Array2dFile::PIXEL_FLOAT32);
126 setPixelSize (sizeof(kfloat32));
127 setDataType (Array2dFile::DATA_TYPE_REAL);
130 F64Image::F64Image (int nx, int ny, int dataType)
131 : Array2dFile (nx, ny, sizeof(kfloat64), Array2dFile::PIXEL_FLOAT64, dataType)
135 F64Image::F64Image (void)
138 setPixelFormat (PIXEL_FLOAT64);
139 setPixelSize (sizeof(kfloat64));
140 setDataType (Array2dFile::DATA_TYPE_REAL);
144 ImageFile::getCenterCoordinates (unsigned int& iXCenter, unsigned int& iYCenter)
149 iXCenter = (m_nx - 1) / 2;
154 iYCenter = (m_ny - 1) / 2;
159 ImageFile::filterResponse (const char* const domainName, double bw, const char* const filterName,
160 double filt_param, double dInputScale, double dOutputScale)
162 ImageFileArray v = getArray();
163 SignalFilter filter (filterName, domainName, bw, filt_param);
165 unsigned int iXCenter, iYCenter;
166 getCenterCoordinates (iXCenter, iYCenter);
168 for (unsigned int ix = 0; ix < m_nx; ix++)
169 for (unsigned int iy = 0; iy < m_ny; iy++) {
170 long lD2 = ((ix - iXCenter) * (ix - iXCenter)) + ((iy - iYCenter) * (iy - iYCenter));
171 double r = ::sqrt (static_cast<double>(lD2)) * dInputScale;
172 v[ix][iy] = filter.response (r) * dOutputScale;
177 // ImageFile::comparativeStatistics Calculate comparative stats
180 // d Normalized root mean squared distance measure
181 // r Normalized mean absolute distance measure
182 // e Worst case distance measure
185 // G.T. Herman, Image Reconstruction From Projections, 1980
188 ImageFile::comparativeStatistics (const ImageFile& imComp, double& d, double& r, double& e) const
190 if (imComp.nx() != m_nx && imComp.ny() != m_ny) {
191 sys_error (ERR_WARNING, "Image sizes differ [ImageFile::comparativeStatistics]");
194 ImageFileArrayConst v = getArray();
195 if (v == NULL || m_nx == 0 || m_ny == 0)
198 ImageFileArrayConst vComp = imComp.getArray();
201 for (unsigned int ix = 0; ix < m_nx; ix++) {
202 for (unsigned int iy = 0; iy < m_ny; iy++) {
206 myMean /= (m_nx * m_ny);
208 double sqErrorSum = 0.;
209 double absErrorSum = 0.;
210 double sqDiffFromMeanSum = 0.;
211 double absValueSum = 0.;
212 for (unsigned int ix2 = 0; ix2 < m_nx; ix2++) {
213 for (unsigned int iy = 0; iy < m_ny; iy++) {
214 double diff = v[ix2][iy] - vComp[ix2][iy];
215 sqErrorSum += diff * diff;
216 absErrorSum += fabs(diff);
217 double diffFromMean = v[ix2][iy] - myMean;
218 sqDiffFromMeanSum += diffFromMean * diffFromMean;
219 absValueSum += fabs(v[ix2][iy]);
223 d = ::sqrt (sqErrorSum / sqDiffFromMeanSum);
224 r = absErrorSum / absValueSum;
229 for (int ix3 = 0; ix3 < hx; ix3++) {
230 for (int iy = 0; iy < hy; iy++) {
231 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]);
232 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]);
233 double error = fabs (avgPixel - avgPixelComp);
246 ImageFile::printComparativeStatistics (const ImageFile& imComp, std::ostream& os) const
250 if (comparativeStatistics (imComp, d, r, e)) {
251 os << " Normalized root mean squared distance (d): " << d << std::endl;
252 os << " Normalized mean absolute distance (r): " << r << std::endl;
253 os << "Worst case distance (2x2 pixel average) (e): " << e << std::endl;
261 ImageFile::printStatistics (std::ostream& os) const
263 double min, max, mean, mode, median, stddev;
265 statistics (min, max, mean, mode, median, stddev);
267 os << "Real Component Statistics" << std::endl;
269 os << " min: " << min << std::endl;
270 os << " max: " << max << std::endl;
271 os << " mean: " << mean << std::endl;
272 os << " mode: " << mode << std::endl;
273 os << "median: " << median << std::endl;
274 os << "stddev: " << stddev << std::endl;
277 statistics (getImaginaryArray(), min, max, mean, mode, median, stddev);
278 os << std::endl << "Imaginary Component Statistics" << std::endl;
279 os << " min: " << min << std::endl;
280 os << " max: " << max << std::endl;
281 os << " mean: " << mean << std::endl;
282 os << " mode: " << mode << std::endl;
283 os << "median: " << median << std::endl;
284 os << "stddev: " << stddev << std::endl;
290 ImageFile::statistics (double& min, double& max, double& mean, double& mode, double& median, double& stddev) const
292 ImageFileArrayConst v = getArray();
293 statistics (v, min, max, mean, mode, median, stddev);
298 ImageFile::statistics (ImageFileArrayConst v, double& min, double& max, double& mean, double& mode, double& median, double& stddev) const
303 if (v == NULL || nx == 0 || ny == 0)
306 std::vector<double> vecImage;
308 vecImage.resize (nx * ny);
309 for (int ix = 0; ix < nx; ix++) {
310 for (int iy = 0; iy < ny; iy++)
311 vecImage[iVec++] = v[ix][iy];
314 vectorNumericStatistics (vecImage, nx * ny, min, max, mean, mode, median, stddev);
318 ImageFile::getMinMax (double& min, double& max) const
322 ImageFileArrayConst v = getArray();
324 if (v == NULL || nx == 0 || ny == 0)
329 for (int ix = 0; ix < nx; ix++) {
330 for (int iy = 0; iy < ny; iy++) {
340 ImageFile::convertRealToComplex ()
342 if (dataType() != Array2dFile::DATA_TYPE_REAL)
345 if (! reallocRealToComplex())
348 ImageFileArray vImag = getImaginaryArray();
349 for (unsigned int ix = 0; ix < m_nx; ix++) {
350 ImageFileColumn vCol = vImag[ix];
351 for (unsigned int iy = 0; iy < m_ny; iy++)
359 ImageFile::convertComplexToReal ()
361 if (dataType() != Array2dFile::DATA_TYPE_COMPLEX)
364 ImageFileArray vReal = getArray();
365 ImageFileArray vImag = getImaginaryArray();
366 for (unsigned int ix = 0; ix < m_nx; ix++) {
367 ImageFileColumn vRealCol = vReal[ix];
368 ImageFileColumn vImagCol = vImag[ix];
369 for (unsigned int iy = 0; iy < m_ny; iy++) {
370 CTSimComplex c (*vRealCol, *vImagCol);
371 *vRealCol++ = std::abs (c);
376 return reallocComplexToReal();
380 ImageFile::subtractImages (const ImageFile& rRHS, ImageFile& result) const
382 if (m_nx != rRHS.nx() || m_ny != rRHS.ny() || m_nx != result.nx() || m_ny != result.ny()) {
383 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::subtractImage]");
387 if (isComplex() || (rRHS.isComplex() && ! result.isComplex()))
388 result.convertRealToComplex();
390 ImageFileArrayConst vLHS = getArray();
391 ImageFileArrayConst vLHSImag = getImaginaryArray();
392 ImageFileArrayConst vRHS = rRHS.getArray();
393 ImageFileArrayConst vRHSImag = rRHS.getImaginaryArray();
394 ImageFileArray vResult = result.getArray();
395 ImageFileArray vResultImag = result.getImaginaryArray();
397 for (unsigned int ix = 0; ix < m_nx; ix++) {
398 for (unsigned int iy = 0; iy < m_ny; iy++) {
399 vResult[ix][iy] = vLHS[ix][iy] - vRHS[ix][iy];
400 if (result.isComplex()) {
401 vResultImag[ix][iy] = 0;
403 vResultImag[ix][iy] += vLHSImag[ix][iy];
404 if (rRHS.isComplex())
405 vResultImag[ix][iy] -= vRHSImag[ix][iy];
414 ImageFile::addImages (const ImageFile& rRHS, ImageFile& result) const
416 if (m_nx != rRHS.nx() || m_ny != rRHS.ny() || m_nx != result.nx() || m_ny != result.ny()) {
417 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::subtractImage]");
421 if (isComplex() || (rRHS.isComplex() && ! result.isComplex()))
422 result.convertRealToComplex();
424 ImageFileArrayConst vLHS = getArray();
425 ImageFileArrayConst vLHSImag = getImaginaryArray();
426 ImageFileArrayConst vRHS = rRHS.getArray();
427 ImageFileArrayConst vRHSImag = rRHS.getImaginaryArray();
428 ImageFileArray vResult = result.getArray();
429 ImageFileArray vResultImag = result.getImaginaryArray();
431 for (unsigned int ix = 0; ix < m_nx; ix++) {
432 for (unsigned int iy = 0; iy < m_ny; iy++) {
433 vResult[ix][iy] = vLHS[ix][iy] + vRHS[ix][iy];
434 if (result.isComplex()) {
435 vResultImag[ix][iy] = 0;
437 vResultImag[ix][iy] += vLHSImag[ix][iy];
438 if (rRHS.isComplex())
439 vResultImag[ix][iy] += vRHSImag[ix][iy];
448 ImageFile::multiplyImages (const ImageFile& rRHS, ImageFile& result) const
450 if (m_nx != rRHS.nx() || m_ny != rRHS.ny() || m_nx != result.nx() || m_ny != result.ny()) {
451 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::subtractImage]");
455 if (isComplex() || (rRHS.isComplex() && ! result.isComplex()))
456 result.convertRealToComplex();
458 ImageFileArrayConst vLHS = getArray();
459 ImageFileArrayConst vLHSImag = getImaginaryArray();
460 ImageFileArrayConst vRHS = rRHS.getArray();
461 ImageFileArrayConst vRHSImag = rRHS.getImaginaryArray();
462 ImageFileArray vResult = result.getArray();
463 ImageFileArray vResultImag = result.getImaginaryArray();
465 for (unsigned int ix = 0; ix < m_nx; ix++) {
466 for (unsigned int iy = 0; iy < m_ny; iy++) {
467 if (result.isComplex()) {
470 dImag = vLHSImag[ix][iy];
471 std::complex<double> cLHS (vLHS[ix][iy], dImag);
473 if (rRHS.isComplex())
474 dImag = vRHSImag[ix][iy];
475 std::complex<double> cRHS (vRHS[ix][iy], dImag);
476 std::complex<double> cResult = cLHS * cRHS;
477 vResult[ix][iy] = cResult.real();
478 vResultImag[ix][iy] = cResult.imag();
480 vResult[ix][iy] = vLHS[ix][iy] * vRHS[ix][iy];
489 ImageFile::divideImages (const ImageFile& rRHS, ImageFile& result) const
491 if (m_nx != rRHS.nx() || m_ny != rRHS.ny() || m_nx != result.nx() || m_ny != result.ny()) {
492 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::subtractImage]");
496 if (isComplex() || (rRHS.isComplex() && ! result.isComplex()))
497 result.convertRealToComplex();
499 ImageFileArrayConst vLHS = getArray();
500 ImageFileArrayConst vLHSImag = getImaginaryArray();
501 ImageFileArrayConst vRHS = rRHS.getArray();
502 ImageFileArrayConst vRHSImag = rRHS.getImaginaryArray();
503 ImageFileArray vResult = result.getArray();
504 ImageFileArray vResultImag = result.getImaginaryArray();
506 for (unsigned int ix = 0; ix < m_nx; ix++) {
507 for (unsigned int iy = 0; iy < m_ny; iy++) {
508 if (result.isComplex()) {
511 dImag = vLHSImag[ix][iy];
512 std::complex<double> cLHS (vLHS[ix][iy], dImag);
514 if (rRHS.isComplex())
515 dImag = vRHSImag[ix][iy];
516 std::complex<double> cRHS (vRHS[ix][iy], dImag);
517 std::complex<double> cResult = cLHS / cRHS;
518 vResult[ix][iy] = cResult.real();
519 vResultImag[ix][iy] = cResult.imag();
522 vResult[ix][iy] = vLHS[ix][iy] / vRHS[ix][iy];
534 ImageFile::invertPixelValues (ImageFile& result) const
536 if (m_nx != result.nx() || m_ny != result.ny()) {
537 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
541 if (isComplex() && ! result.isComplex())
542 result.convertRealToComplex();
544 ImageFileArrayConst vLHS = getArray();
545 ImageFileArray vResult = result.getArray();
547 for (unsigned int ix = 0; ix < m_nx; ix++) {
548 ImageFileColumnConst in = vLHS[ix];
549 ImageFileColumn out = vResult[ix];
550 for (unsigned int iy = 0; iy < m_ny; iy++)
558 ImageFile::sqrt (ImageFile& result) const
560 if (m_nx != result.nx() || m_ny != result.ny()) {
561 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
565 if (isComplex() && ! result.isComplex())
566 result.convertRealToComplex();
568 bool bComplexOutput = result.isComplex();
569 ImageFileArrayConst vLHS = getArray();
570 if (! bComplexOutput) // check if should convert to complex output
571 for (unsigned int ix = 0; ix < m_nx; ix++)
572 for (unsigned int iy = 0; iy < m_ny; iy++)
573 if (! bComplexOutput && vLHS[ix][iy] < 0) {
574 result.convertRealToComplex();
575 bComplexOutput = true;
579 ImageFileArrayConst vLHSImag = getImaginaryArray();
580 ImageFileArray vResult = result.getArray();
581 ImageFileArray vResultImag = result.getImaginaryArray();
583 for (unsigned int ix = 0; ix < m_nx; ix++) {
584 for (unsigned int iy = 0; iy < m_ny; iy++) {
585 if (result.isComplex()) {
588 dImag = vLHSImag[ix][iy];
589 std::complex<double> cLHS (vLHS[ix][iy], dImag);
590 std::complex<double> cResult = std::sqrt(cLHS);
591 vResult[ix][iy] = cResult.real();
592 vResultImag[ix][iy] = cResult.imag();
594 vResult[ix][iy] = ::sqrt (vLHS[ix][iy]);
603 ImageFile::log (ImageFile& result) const
605 if (m_nx != result.nx() || m_ny != result.ny()) {
606 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::log]");
610 if (isComplex() && ! result.isComplex())
611 result.convertRealToComplex();
613 ImageFileArrayConst vLHS = getArray();
614 ImageFileArrayConst vLHSImag = getImaginaryArray();
615 ImageFileArray vResult = result.getArray();
616 ImageFileArray vResultImag = result.getImaginaryArray();
618 for (unsigned int ix = 0; ix < m_nx; ix++) {
619 for (unsigned int iy = 0; iy < m_ny; iy++) {
620 if (result.isComplex()) {
623 dImag = vLHSImag[ix][iy];
624 std::complex<double> cLHS (vLHS[ix][iy], dImag);
625 std::complex<double> cResult = std::log (cLHS);
626 vResult[ix][iy] = cResult.real();
627 vResultImag[ix][iy] = cResult.imag();
629 if (vLHS[ix][iy] > 0)
630 vResult[ix][iy] = ::log (vLHS[ix][iy]);
642 ImageFile::exp (ImageFile& result) const
644 if (m_nx != result.nx() || m_ny != result.ny()) {
645 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
649 if (isComplex() && ! result.isComplex())
650 result.convertRealToComplex();
652 ImageFileArrayConst vLHS = getArray();
653 ImageFileArrayConst vLHSImag = getImaginaryArray();
654 ImageFileArray vResult = result.getArray();
655 ImageFileArray vResultImag = result.getImaginaryArray();
657 for (unsigned int ix = 0; ix < m_nx; ix++) {
658 for (unsigned int iy = 0; iy < m_ny; iy++) {
659 if (result.isComplex()) {
662 dImag = vLHSImag[ix][iy];
663 std::complex<double> cLHS (vLHS[ix][iy], dImag);
664 std::complex<double> cResult = std::exp (cLHS);
665 vResult[ix][iy] = cResult.real();
666 vResultImag[ix][iy] = cResult.imag();
668 vResult[ix][iy] = ::exp (vLHS[ix][iy]);
677 ImageFile::scaleImage (ImageFile& result) const
679 unsigned int nx = m_nx;
680 unsigned int ny = m_ny;
681 unsigned int newNX = result.nx();
682 unsigned int newNY = result.ny();
684 double dXScale = static_cast<double>(newNX) / static_cast<double>(nx);
685 double dYScale = static_cast<double>(newNY) / static_cast<double>(ny);
687 if (isComplex() && ! result.isComplex())
688 result.convertRealToComplex();
690 ImageFileArrayConst vReal = getArray();
691 ImageFileArrayConst vImag = getImaginaryArray();
692 ImageFileArray vResult = result.getArray();
693 ImageFileArray vResultImag = result.getImaginaryArray();
695 BilinearInterpolator<ImageFileValue> realInterp (vReal, nx, ny);
696 BilinearInterpolator<ImageFileValue> imagInterp (vImag, nx, ny);
698 for (unsigned int ix = 0; ix < newNX; ix++) {
699 for (unsigned int iy = 0; iy < newNY; iy++) {
700 double dXPos = ix / dXScale;
701 double dYPos = iy / dYScale;
702 vResult[ix][iy] = realInterp.interpolate (dXPos, dYPos);
703 if (result.isComplex()) {
705 vResultImag[ix][iy] = imagInterp.interpolate (dXPos, dYPos);
707 vResultImag[ix][iy] = 0;
718 ImageFile::fft (ImageFile& result) const
720 if (m_nx != result.nx() || m_ny != result.ny()) {
721 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
725 if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {
726 if (! result.convertRealToComplex ())
730 fftw_complex* in = static_cast<fftw_complex*> (fftw_malloc (sizeof(fftw_complex) * m_nx * m_ny));
732 ImageFileArrayConst vReal = getArray();
733 ImageFileArrayConst vImag = getImaginaryArray();
736 unsigned int iArray = 0;
737 for (ix = 0; ix < m_nx; ix++) {
738 for (iy = 0; iy < m_ny; iy++) {
739 in[iArray][0] = vReal[ix][iy];
741 in[iArray][1] = vImag[ix][iy];
748 fftw_plan plan = fftw_plan_dft_2d (m_nx, m_ny, in, in, FFTW_FORWARD, FFTW_ESTIMATE);
751 ImageFileArray vRealResult = result.getArray();
752 ImageFileArray vImagResult = result.getImaginaryArray();
754 unsigned int iScale = m_nx * m_ny;
755 for (ix = 0; ix < m_nx; ix++) {
756 for (iy = 0; iy < m_ny; iy++) {
757 vRealResult[ix][iy] = in[iArray][0] / iScale;
758 vImagResult[ix][iy] = in[iArray][1] / iScale;
763 fftw_destroy_plan (plan);
765 Fourier::shuffleFourierToNaturalOrder (result);
772 ImageFile::ifft (ImageFile& result) const
774 if (m_nx != result.nx() || m_ny != result.ny()) {
775 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
779 if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {
780 if (! result.convertRealToComplex ())
784 ImageFileArrayConst vReal = getArray();
785 ImageFileArrayConst vImag = getImaginaryArray();
786 ImageFileArray vRealResult = result.getArray();
787 ImageFileArray vImagResult = result.getImaginaryArray();
789 for (ix = 0; ix < m_nx; ix++) {
790 for (iy = 0; iy < m_ny; iy++) {
791 vRealResult[ix][iy] = vReal[ix][iy];
793 vImagResult[ix][iy] = vImag[ix][iy];
795 vImagResult[ix][iy] = 0;
799 Fourier::shuffleNaturalToFourierOrder (result);
801 fftw_complex* in = static_cast<fftw_complex*>(fftw_malloc(sizeof(fftw_complex) * m_nx * m_ny));
803 unsigned int iArray = 0;
804 for (ix = 0; ix < m_nx; ix++) {
805 for (iy = 0; iy < m_ny; iy++) {
806 in[iArray][0] = vRealResult[ix][iy];
807 in[iArray][1] = vImagResult[ix][iy];
812 fftw_plan plan = fftw_plan_dft_2d (m_nx, m_ny, in, in, FFTW_BACKWARD, FFTW_ESTIMATE);
817 for (ix = 0; ix < m_nx; ix++) {
818 for (iy = 0; iy < m_ny; iy++) {
819 vRealResult[ix][iy] = in[iArray][0];
820 vImagResult[ix][iy] = in[iArray][1];
824 fftw_destroy_plan (plan);
831 ImageFile::fftRows (ImageFile& result) const
833 if (m_nx != result.nx() || m_ny != result.ny()) {
834 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::fftRows]");
838 if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {
839 if (! result.convertRealToComplex ())
843 ImageFileArrayConst vReal = getArray();
844 ImageFileArrayConst vImag = getImaginaryArray();
846 fftw_complex* in = static_cast<fftw_complex*>(fftw_malloc(sizeof(fftw_complex) * m_nx));
847 fftw_plan plan = fftw_plan_dft_1d (m_nx, in, in, FFTW_FORWARD, FFTW_ESTIMATE);
849 std::complex<double>* pcRow = new std::complex<double> [m_nx];
850 for (unsigned int iy = 0; iy < m_ny; iy++) {
852 for (ix = 0; ix < m_nx; ix++) {
853 in[ix][0] = vReal[ix][iy];
855 in[ix][1] = vImag[ix][iy];
862 for (ix = 0; ix < m_nx; ix++)
863 pcRow[ix] = std::complex<double>(in[ix][0], in[ix][1]);
865 Fourier::shuffleFourierToNaturalOrder (pcRow, m_nx);
866 for (ix = 0; ix < m_nx; ix++) {
867 vReal[ix][iy] = pcRow[ix].real() / m_nx;
868 vImag[ix][iy] = pcRow[ix].imag() / m_nx;
873 fftw_destroy_plan (plan);
880 ImageFile::ifftRows (ImageFile& result) const
882 if (m_nx != result.nx() || m_ny != result.ny()) {
883 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::fftRows]");
887 if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {
888 if (! result.convertRealToComplex ())
892 ImageFileArrayConst vReal = getArray();
893 ImageFileArrayConst vImag = getImaginaryArray();
895 fftw_complex* in = static_cast<fftw_complex*>(fftw_malloc(sizeof(fftw_complex) * m_nx));
896 fftw_plan plan = fftw_plan_dft_1d (m_nx, in, in, FFTW_BACKWARD, FFTW_ESTIMATE);
897 std::complex<double>* pcRow = new std::complex<double> [m_nx];
900 // unsigned int iArray = 0;
901 for (iy = 0; iy < m_ny; iy++) {
902 for (ix = 0; ix < m_nx; ix++) {
905 dImag = vImag[ix][iy];
906 pcRow[ix] = std::complex<double> (vReal[ix][iy], dImag);
909 Fourier::shuffleNaturalToFourierOrder (pcRow, m_nx);
911 for (ix = 0; ix < m_nx; ix++) {
912 in[ix][0] = pcRow[ix].real();
913 in[ix][1] = pcRow[ix].imag();
918 for (ix = 0; ix < m_nx; ix++) {
919 vReal[ix][iy] = in[ix][0];
920 vImag[ix][iy] = in[ix][1];
925 fftw_destroy_plan (plan);
932 ImageFile::fftCols (ImageFile& result) const
934 if (m_nx != result.nx() || m_ny != result.ny()) {
935 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::fftRows]");
939 if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {
940 if (! result.convertRealToComplex ())
944 ImageFileArrayConst vReal = getArray();
945 ImageFileArrayConst vImag = getImaginaryArray();
947 fftw_complex* in = static_cast<fftw_complex*>(fftw_malloc(sizeof(fftw_complex) * m_ny));
948 fftw_plan plan = fftw_plan_dft_1d (m_ny, in, in, FFTW_FORWARD, FFTW_ESTIMATE);
950 std::complex<double>* pcCol = new std::complex<double> [m_ny];
951 for (unsigned int ix = 0; ix < m_nx; ix++) {
953 for (iy = 0; iy < m_ny; iy++) {
954 in[iy][0] = vReal[ix][iy];
956 in[iy][1] = vImag[ix][iy];
963 for (iy = 0; iy < m_ny; iy++)
964 pcCol[iy] = std::complex<double>(in[iy][0], in[iy][1]);
966 Fourier::shuffleFourierToNaturalOrder (pcCol, m_ny);
967 for (iy = 0; iy < m_ny; iy++) {
968 vReal[ix][iy] = pcCol[iy].real() / m_ny;
969 vImag[ix][iy] = pcCol[iy].imag() / m_ny;
974 fftw_destroy_plan (plan);
981 ImageFile::ifftCols (ImageFile& result) const
983 if (m_nx != result.nx() || m_ny != result.ny()) {
984 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::fftRows]");
988 if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {
989 if (! result.convertRealToComplex ())
993 ImageFileArrayConst vReal = getArray();
994 ImageFileArrayConst vImag = getImaginaryArray();
996 fftw_complex* in = static_cast<fftw_complex*>(fftw_malloc(sizeof(fftw_complex) * m_ny));
997 fftw_plan plan = fftw_plan_dft_1d (m_ny, in, in, FFTW_BACKWARD, FFTW_ESTIMATE);
998 std::complex<double>* pcCol = new std::complex<double> [m_ny];
1000 unsigned int ix, iy;
1001 // unsigned int iArray = 0;
1002 for (ix = 0; ix < m_nx; ix++) {
1003 for (iy = 0; iy < m_ny; iy++) {
1006 dImag = vImag[ix][iy];
1007 pcCol[iy] = std::complex<double> (vReal[ix][iy], dImag);
1010 Fourier::shuffleNaturalToFourierOrder (pcCol, m_ny);
1012 for (iy = 0; iy < m_ny; iy++) {
1013 in[iy][0] = pcCol[iy].real();
1014 in[iy][1] = pcCol[iy].imag();
1017 fftw_execute (plan);
1019 for (iy = 0; iy < m_ny; iy++) {
1020 vReal[ix][iy] = in[iy][0];
1021 vImag[ix][iy] = in[iy][1];
1026 fftw_destroy_plan (plan);
1037 ImageFile::fourier (ImageFile& result) const
1039 if (m_nx != result.nx() || m_ny != result.ny()) {
1040 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
1044 if (! result.isComplex())
1045 if (! result.convertRealToComplex ())
1048 ImageFileArrayConst vLHS = getArray();
1049 ImageFileArrayConst vLHSImag = getImaginaryArray();
1050 ImageFileArray vRealResult = result.getArray();
1051 ImageFileArray vImagResult = result.getImaginaryArray();
1053 unsigned int ix, iy;
1055 // alloc output matrix
1056 CTSimComplex** complexOut = new CTSimComplex* [m_nx];
1057 for (ix = 0; ix < m_nx; ix++)
1058 complexOut[ix] = new CTSimComplex [m_ny];
1060 // fourier each x column
1061 CTSimComplex* pY = new CTSimComplex [m_ny];
1062 for (ix = 0; ix < m_nx; ix++) {
1063 for (iy = 0; iy < m_ny; iy++) {
1066 dImag = vLHSImag[ix][iy];
1067 pY[iy] = std::complex<double>(vLHS[ix][iy], dImag);
1069 ProcessSignal::finiteFourierTransform (pY, complexOut[ix], m_ny, ProcessSignal::FORWARD);
1073 // fourier each y row
1074 CTSimComplex* pX = new CTSimComplex [m_nx];
1075 CTSimComplex* complexOutRow = new CTSimComplex [m_nx];
1076 for (iy = 0; iy < m_ny; iy++) {
1077 for (ix = 0; ix < m_nx; ix++)
1078 pX[ix] = complexOut[ix][iy];
1079 ProcessSignal::finiteFourierTransform (pX, complexOutRow, m_nx, ProcessSignal::FORWARD);
1080 for (ix = 0; ix < m_nx; ix++)
1081 complexOut[ix][iy] = complexOutRow[ix];
1084 delete [] complexOutRow;
1086 for (ix = 0; ix < m_nx; ix++)
1087 for (iy = 0; iy < m_ny; iy++) {
1088 vRealResult[ix][iy] = complexOut[ix][iy].real();
1089 vImagResult[ix][iy] = complexOut[ix][iy].imag();
1092 Fourier::shuffleFourierToNaturalOrder (result);
1094 // delete complexOut matrix
1095 for (ix = 0; ix < m_nx; ix++)
1096 delete [] complexOut[ix];
1097 delete [] complexOut;
1103 ImageFile::inverseFourier (ImageFile& result) const
1105 if (m_nx != result.nx() || m_ny != result.ny()) {
1106 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
1110 if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {
1111 if (! result.convertRealToComplex ())
1115 ImageFileArrayConst vLHSReal = getArray();
1116 ImageFileArrayConst vLHSImag = getImaginaryArray();
1117 ImageFileArray vRealResult = result.getArray();
1118 ImageFileArray vImagResult = result.getImaginaryArray();
1120 unsigned int ix, iy;
1121 // alloc 2d complex output matrix
1122 CTSimComplex** complexOut = new CTSimComplex* [m_nx];
1123 for (ix = 0; ix < m_nx; ix++)
1124 complexOut[ix] = new CTSimComplex [m_ny];
1126 // put input image into result
1127 for (ix = 0; ix < m_nx; ix++)
1128 for (iy = 0; iy < m_ny; iy++) {
1129 vRealResult[ix][iy] = vLHSReal[ix][iy];
1131 vImagResult[ix][iy] = vLHSImag[ix][iy];
1133 vImagResult[ix][iy] = 0;
1136 Fourier::shuffleNaturalToFourierOrder (result);
1138 // ifourier each x column
1139 CTSimComplex* pCol = new CTSimComplex [m_ny];
1140 for (ix = 0; ix < m_nx; ix++) {
1141 for (iy = 0; iy < m_ny; iy++) {
1142 pCol[iy] = std::complex<double> (vRealResult[ix][iy], vImagResult[ix][iy]);
1144 ProcessSignal::finiteFourierTransform (pCol, complexOut[ix], m_ny, ProcessSignal::BACKWARD);
1148 // ifourier each y row
1149 CTSimComplex* complexInRow = new CTSimComplex [m_nx];
1150 CTSimComplex* complexOutRow = new CTSimComplex [m_nx];
1151 for (iy = 0; iy < m_ny; iy++) {
1152 for (ix = 0; ix < m_nx; ix++)
1153 complexInRow[ix] = complexOut[ix][iy];
1154 ProcessSignal::finiteFourierTransform (complexInRow, complexOutRow, m_nx, ProcessSignal::BACKWARD);
1155 for (ix = 0; ix < m_nx; ix++)
1156 complexOut[ix][iy] = complexOutRow[ix];
1158 delete [] complexInRow;
1159 delete [] complexOutRow;
1161 for (ix = 0; ix < m_nx; ix++)
1162 for (iy = 0; iy < m_ny; iy++) {
1163 vRealResult[ix][iy] = complexOut[ix][iy].real();
1164 vImagResult[ix][iy] = complexOut[ix][iy].imag();
1167 // delete complexOut matrix
1168 for (ix = 0; ix < m_nx; ix++)
1169 delete [] complexOut[ix];
1170 delete [] complexOut;
1177 ImageFile::magnitude (ImageFile& result) const
1179 if (m_nx != result.nx() || m_ny != result.ny()) {
1180 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
1184 ImageFileArray vReal = getArray();
1185 ImageFileArray vImag = getImaginaryArray();
1186 ImageFileArray vRealResult = result.getArray();
1188 for (unsigned int ix = 0; ix < m_nx; ix++)
1189 for (unsigned int iy = 0; iy < m_ny; iy++) {
1191 vRealResult[ix][iy] = ::sqrt (vReal[ix][iy] * vReal[ix][iy] + vImag[ix][iy] * vImag[ix][iy]);
1193 vRealResult[ix][iy] = ::fabs(vReal[ix][iy]);
1196 if (result.isComplex())
1197 result.reallocComplexToReal();
1203 ImageFile::phase (ImageFile& result) const
1205 if (m_nx != result.nx() || m_ny != result.ny()) {
1206 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
1210 ImageFileArray vReal = getArray();
1211 ImageFileArray vImag = getImaginaryArray();
1212 ImageFileArray vRealResult = result.getArray();
1214 for (unsigned int ix = 0; ix < m_nx; ix++) {
1215 for (unsigned int iy = 0; iy < m_ny; iy++) {
1217 vRealResult[ix][iy] = ::atan2 (vImag[ix][iy], vReal[ix][iy]);
1219 vRealResult[ix][iy] = 0;
1222 if (result.isComplex())
1223 result.reallocComplexToReal();
1229 ImageFile::real (ImageFile& result) const
1231 if (m_nx != result.nx() || m_ny != result.ny()) {
1232 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
1236 ImageFileArray vReal = getArray();
1237 ImageFileArray vRealResult = result.getArray();
1239 for (unsigned int ix = 0; ix < m_nx; ix++) {
1240 for (unsigned int iy = 0; iy < m_ny; iy++) {
1241 vRealResult[ix][iy] = vReal[ix][iy];
1245 if (result.isComplex())
1246 result.reallocComplexToReal();
1252 ImageFile::imaginary (ImageFile& result) const
1254 if (m_nx != result.nx() || m_ny != result.ny()) {
1255 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
1259 ImageFileArray vImag = getArray();
1260 ImageFileArray vRealResult = result.getArray();
1262 for (unsigned int ix = 0; ix < m_nx; ix++) {
1263 for (unsigned int iy = 0; iy < m_ny; iy++) {
1265 vRealResult[ix][iy] = vImag[ix][iy];
1267 vRealResult[ix][iy] = 0;
1271 if (result.isComplex())
1272 result.reallocComplexToReal();
1278 ImageFile::square (ImageFile& result) const
1280 if (m_nx != result.nx() || m_ny != result.ny()) {
1281 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
1285 if (isComplex() && ! result.isComplex())
1286 result.convertRealToComplex();
1288 ImageFileArrayConst vLHS = getArray();
1289 ImageFileArrayConst vLHSImag = getImaginaryArray();
1290 ImageFileArray vResult = result.getArray();
1291 ImageFileArray vResultImag = result.getImaginaryArray();
1293 for (unsigned int ix = 0; ix < m_nx; ix++) {
1294 for (unsigned int iy = 0; iy < m_ny; iy++) {
1295 if (result.isComplex()) {
1298 dImag = vLHSImag[ix][iy];
1299 std::complex<double> cLHS (vLHS[ix][iy], dImag);
1300 std::complex<double> cResult = cLHS * cLHS;
1301 vResult[ix][iy] = cResult.real();
1302 vResultImag[ix][iy] = cResult.imag();
1304 vResult[ix][iy] = vLHS[ix][iy] * vLHS[ix][iy];
1312 ImageFile::convertExportFormatNameToID (const char* const formatName)
1314 int formatID = EXPORT_FORMAT_INVALID;
1316 for (int i = 0; i < s_iExportFormatCount; i++)
1317 if (strcasecmp (formatName, s_aszExportFormatName[i]) == 0) {
1326 ImageFile::convertExportFormatIDToName (int formatID)
1328 static const char *formatName = "";
1330 if (formatID >= 0 && formatID < s_iExportFormatCount)
1331 return (s_aszExportFormatName[formatID]);
1333 return (formatName);
1337 ImageFile::convertExportFormatIDToTitle (const int formatID)
1339 static const char *formatTitle = "";
1341 if (formatID >= 0 && formatID < s_iExportFormatCount)
1342 return (s_aszExportFormatTitle[formatID]);
1344 return (formatTitle);
1348 ImageFile::convertImportFormatNameToID (const char* const formatName)
1350 int formatID = IMPORT_FORMAT_INVALID;
1352 for (int i = 0; i < s_iImportFormatCount; i++)
1353 if (strcasecmp (formatName, s_aszImportFormatName[i]) == 0) {
1362 ImageFile::convertImportFormatIDToName (int formatID)
1364 static const char *formatName = "";
1366 if (formatID >= 0 && formatID < s_iImportFormatCount)
1367 return (s_aszImportFormatName[formatID]);
1369 return (formatName);
1373 ImageFile::convertImportFormatIDToTitle (const int formatID)
1375 static const char *formatTitle = "";
1377 if (formatID >= 0 && formatID < s_iImportFormatCount)
1378 return (s_aszImportFormatTitle[formatID]);
1380 return (formatTitle);
1384 ImageFile::importImage (const char* const pszFormat, const char* const pszFilename)
1386 int iFormatID = convertImportFormatNameToID (pszFormat);
1388 if (iFormatID == IMPORT_FORMAT_PPM)
1389 return readImagePPM (pszFilename);
1391 else if (iFormatID == IMPORT_FORMAT_PNG)
1392 return readImagePNG (pszFilename);
1395 sys_error (ERR_SEVERE, "Invalid format %s [ImageFile::importImage]", pszFormat);
1400 ImageFile::skipSpacePPM (FILE* fp)
1403 while (isspace (c) || c == '#') {
1404 if (c == '#') { // comment until end of line
1406 while (c != 13 && c != 10)
1417 ImageFile::readImagePPM (const char* const pszFile)
1419 FILE* fp = fopen (pszFile, "r");
1420 if ((fp = fopen (pszFile, "r")) == NULL)
1422 char cSignature = toupper(fgetc(fp));
1423 if (cSignature != 'P') {
1427 cSignature = fgetc(fp);
1428 if (cSignature == '5' || cSignature == '6') { // binary modes
1430 fp = fopen(pszFile, "rb"); // reopen in binary mode
1433 } else if (cSignature != '2' && cSignature != '3') {
1438 int nRows, nCols, iMaxValue;
1440 if (fscanf (fp, "%d", &nCols) != 1) {
1445 if (fscanf (fp, "%d", &nRows) != 1) {
1450 if (fscanf (fp, "%d", &iMaxValue) != 1) {
1454 setArraySize (nRows, nCols);
1456 if (cSignature == '5' || cSignature == '6') { // binary modes
1460 if (c != 10) // read msdos 13-10 newline
1464 skipSpacePPM (fp); // ascii may have comments
1466 bool bMonochromeImage = false;
1467 double dMaxValue = iMaxValue;
1468 double dMaxValue3 = iMaxValue * 3;
1470 ImageFileArray v = getArray();
1471 for (int iy = nRows - 1; iy >= 0; iy--) {
1472 for (int ix = 0; ix < nCols; ix++) {
1473 int iGS, iR, iG, iB;
1475 switch (cSignature) {
1477 if (fscanf(fp, "%d ", &iGS) != 1) {
1481 v[ix][iy] = iGS / dMaxValue;
1489 v[ix][iy] = iGS / dMaxValue;
1492 if (fscanf (fp, "%d %d %d ", &iR, &iG, &iB) != 3) {
1496 if (ix == 0 && iy == 0 && (iR == iG && iG == iB))
1497 bMonochromeImage = true;
1498 if (bMonochromeImage)
1499 v[ix][iy] = (iR + iG + iB) / dMaxValue3;
1501 dR = iR / dMaxValue;
1502 dG = iG / dMaxValue;
1503 dB = iB / dMaxValue;
1504 v[ix][iy] = colorToGrayscale (dR, dG, dB);
1516 if (ix == 0 && iy == 0 && (iR == iG && iG == iB))
1517 bMonochromeImage = true;
1519 if (bMonochromeImage)
1520 v[ix][iy] = (iR + iG + iB) / dMaxValue3;
1522 dR = iR / dMaxValue;
1523 dG = iG / dMaxValue;
1524 dB = iB / dMaxValue;
1525 v[ix][iy] = colorToGrayscale (dR, dG, dB);
1538 ImageFile::readImagePNG (const char* const pszFile)
1540 FILE* fp = fopen(pszFile, "rb");
1543 unsigned char header[8];
1544 fread (header, 1, 8, fp);
1545 if (png_sig_cmp (header, 0, 8)) {
1550 png_structp png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
1556 png_infop info_ptr = png_create_info_struct(png_ptr);
1558 png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
1563 png_infop end_info = png_create_info_struct(png_ptr);
1565 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
1570 if (setjmp(png_ptr->jmpbuf)) {
1571 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
1576 png_init_io(png_ptr, fp);
1577 png_set_sig_bytes(png_ptr, 8);
1578 png_read_info(png_ptr, info_ptr);
1580 int width = png_get_image_width (png_ptr, info_ptr);
1581 int height = png_get_image_height (png_ptr, info_ptr);
1582 int bit_depth = png_get_bit_depth (png_ptr, info_ptr);
1583 int color_type = png_get_color_type (png_ptr, info_ptr);
1585 if (color_type == PNG_COLOR_TYPE_PALETTE && bit_depth <= 8)
1586 png_set_expand(png_ptr);
1588 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
1589 png_set_expand(png_ptr);
1592 png_set_packing(png_ptr);
1594 if (color_type & PNG_COLOR_MASK_ALPHA)
1595 png_set_strip_alpha(png_ptr);
1597 if (bit_depth == 16)
1598 png_set_swap(png_ptr); // convert to little-endian format
1600 png_read_update_info(png_ptr, info_ptr); // update with transformations
1601 int rowbytes = png_get_rowbytes (png_ptr, info_ptr);
1602 bit_depth = png_get_bit_depth (png_ptr, info_ptr);
1603 color_type = png_get_color_type (png_ptr, info_ptr);
1605 png_bytep* row_pointers = new png_bytep [height];
1607 for (i = 0; i < height; i++)
1608 row_pointers[i] = new unsigned char [rowbytes];
1610 png_read_image(png_ptr, row_pointers);
1612 setArraySize (width, height);
1613 ImageFileArray v = getArray();
1614 for (int iy = 0; iy < height; iy++) {
1615 for (int ix = 0; ix < width; ix++) {
1617 if (color_type == PNG_COLOR_TYPE_GRAY) {
1619 dV = row_pointers[iy][ix] / 255.;
1620 else if (bit_depth == 16) {
1622 dV = (row_pointers[iy][iBase] + (row_pointers[iy][iBase+1] << 8)) / 65536.;
1625 } else if (color_type == PNG_COLOR_TYPE_RGB) {
1626 if (bit_depth == 8) {
1628 double dR = row_pointers[iy][iBase] / 255.;
1629 double dG = row_pointers[iy][iBase+1] / 255.;
1630 double dB = row_pointers[iy][iBase+2] / 255.;
1631 dV = colorToGrayscale (dR, dG, dB);
1635 v[ix][height-iy-1] = dV;
1639 png_read_end(png_ptr, end_info);
1640 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
1642 for (i = 0; i < height; i++)
1643 delete row_pointers[i];
1644 delete row_pointers;
1652 ImageFile::exportImage (const char* const pszFormat, const char* const pszFilename, int nxcell, int nycell, double densmin, double densmax)
1654 int iFormatID = convertExportFormatNameToID (pszFormat);
1656 if (iFormatID == EXPORT_FORMAT_PGM)
1657 return writeImagePGM (pszFilename, nxcell, nycell, densmin, densmax);
1658 else if (iFormatID == EXPORT_FORMAT_PGMASCII)
1659 return writeImagePGMASCII (pszFilename, nxcell, nycell, densmin, densmax);
1660 else if (iFormatID == EXPORT_FORMAT_TEXT)
1661 return writeImageText (pszFilename);
1663 else if (iFormatID == EXPORT_FORMAT_PNG)
1664 return writeImagePNG (pszFilename, 8, nxcell, nycell, densmin, densmax);
1665 else if (iFormatID == EXPORT_FORMAT_PNG16)
1666 return writeImagePNG (pszFilename, 16, nxcell, nycell, densmin, densmax);
1668 #ifdef HAVE_CTN_DICOM
1669 else if (iFormatID == EXPORT_FORMAT_DICOM) {
1670 DicomExporter dicomExport (this);
1671 bool bSuccess = dicomExport.writeFile (pszFilename);
1673 sys_error (ERR_SEVERE, dicomExport.failMessage().c_str());
1677 else if (iFormatID == EXPORT_FORMAT_RAW)
1678 return writeImageRaw(pszFilename, nxcell, nycell);
1681 sys_error (ERR_SEVERE, "Invalid format %s [ImageFile::exportImage]", pszFormat);
1687 ImageFile::writeImagePGM (const char* const outfile, int nxcell, int nycell, double densmin, double densmax)
1692 ImageFileArray v = getArray();
1694 unsigned char* rowp = new unsigned char [nx * nxcell];
1696 if ((fp = fopen (outfile, "wb")) == NULL)
1699 fprintf(fp, "P5\n");
1700 fprintf(fp, "%d %d\n", nx, ny);
1701 fprintf(fp, "255\n");
1703 for (int irow = ny - 1; irow >= 0; irow--) {
1704 for (int icol = 0; icol < nx; icol++) {
1705 int pos = icol * nxcell;
1706 double dens = (v[icol][irow] - densmin) / (densmax - densmin);
1707 dens = clamp (dens, 0., 1.);
1708 for (int p = pos; p < pos + nxcell; p++) {
1709 rowp[p] = static_cast<unsigned int> (dens * 255.);
1712 for (int ir = 0; ir < nycell; ir++) {
1713 for (int ic = 0; ic < nx * nxcell; ic++)
1714 fputc( rowp[ic], fp );
1725 ImageFile::writeImagePGMASCII (const char* const outfile, int nxcell, int nycell, double densmin, double densmax)
1730 ImageFileArray v = getArray();
1732 unsigned char* rowp = new unsigned char [nx * nxcell];
1734 if ((fp = fopen (outfile, "wb")) == NULL)
1737 fprintf(fp, "P2\n");
1738 fprintf(fp, "%d %d\n", nx, ny);
1739 fprintf(fp, "255\n");
1741 for (int irow = ny - 1; irow >= 0; irow--) {
1742 for (int icol = 0; icol < nx; icol++) {
1743 int pos = icol * nxcell;
1744 double dens = (v[icol][irow] - densmin) / (densmax - densmin);
1745 dens = clamp (dens, 0., 1.);
1746 for (int p = pos; p < pos + nxcell; p++) {
1747 rowp[p] = static_cast<unsigned int> (dens * 255.);
1750 for (int ir = 0; ir < nycell; ir++) {
1751 for (int ic = 0; ic < nx * nxcell; ic++)
1752 fprintf(fp, "%d ", rowp[ic]);
1764 ImageFile::writeImageText (const char* const outfile)
1769 ImageFileArray v = getArray();
1770 ImageFileArray vImag = getImaginaryArray();
1772 if ((fp = fopen (outfile, "w")) == NULL)
1775 for (int irow = ny - 1; irow >= 0; irow--) {
1776 for (int icol = 0; icol < nx; icol++) {
1778 if (vImag[icol][irow] >= 0)
1779 fprintf (fp, "%.9g+%.9gi ", v[icol][irow], vImag[icol][irow]);
1781 fprintf (fp, "%.9g-%.9gi ", v[icol][irow], -vImag[icol][irow]);
1783 fprintf (fp, "%12.8g ", v[icol][irow]);
1796 ImageFile::writeImagePNG (const char* const outfile, int bitdepth, int nxcell, int nycell, double densmin, double densmax)
1798 double max_out_level = (1 << bitdepth) - 1;
1801 ImageFileArray v = getArray();
1803 unsigned char* rowp = new unsigned char [nx * nxcell * (bitdepth / 8)];
1805 FILE *fp = fopen (outfile, "wb");
1809 png_structp png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
1813 png_infop info_ptr = png_create_info_struct (png_ptr);
1815 png_destroy_write_struct (&png_ptr, (png_infopp) NULL);
1820 if (setjmp (png_ptr->jmpbuf)) {
1821 png_destroy_write_struct (&png_ptr, &info_ptr);
1826 png_init_io(png_ptr, fp);
1828 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);
1830 png_write_info(png_ptr, info_ptr);
1831 for (int irow = ny - 1; irow >= 0; irow--) {
1832 png_bytep row_pointer = rowp;
1834 for (int icol = 0; icol < nx; icol++) {
1835 int pos = icol * nxcell;
1836 double dens = (v[icol][irow] - densmin) / (densmax - densmin);
1837 dens = clamp (dens, 0., 1.);
1838 unsigned int outval = static_cast<unsigned int> (dens * max_out_level);
1840 for (int p = pos; p < pos + nxcell; p++) {
1845 rowp[rowpos+1] = (outval >> 8) & 0xFF;
1846 rowp[rowpos] = (outval & 0xFF);
1850 for (int ir = 0; ir < nycell; ir++)
1851 png_write_rows (png_ptr, &row_pointer, 1);
1854 png_write_end (png_ptr, info_ptr);
1855 png_destroy_write_struct (&png_ptr, &info_ptr);
1866 static const int N_GRAYSCALE=256;
1869 ImageFile::writeImageGIF (const char* const outfile, int nxcell, int nycell, double densmin, double densmax)
1871 int gs_indices[N_GRAYSCALE];
1874 ImageFileArray v = getArray();
1876 unsigned char* rowp = new unsigned char [nx * nxcell];
1878 gdImagePtr gif = gdImageCreate(nx * nxcell, ny * nycell);
1879 for (int i = 0; i < N_GRAYSCALE; i++)
1880 gs_indices[i] = gdImageColorAllocate(gif, i, i, i);
1882 int lastrow = ny * nycell - 1;
1883 for (int irow = 0; irow < ny; irow++) {
1884 int rpos = irow * nycell;
1885 for (int ir = rpos; ir < rpos + nycell; ir++) {
1886 for (int icol = 0; icol < nx; icol++) {
1887 int cpos = icol * nxcell;
1888 double dens = (v[icol][irow] - densmin) / (densmax - densmin);
1889 dens = clamp(dens, 0., 1.);
1890 for (int ic = cpos; ic < cpos + nxcell; ic++) {
1891 rowp[ic] = (unsigned int) (dens * (double) (N_GRAYSCALE - 1));
1892 gdImageSetPixel(gif, ic, lastrow - ir, gs_indices[rowp[ic]]);
1899 if ((out = fopen (outfile,"w")) == NULL) {
1900 sys_error(ERR_SEVERE, "Error opening output file %s for writing", outfile);
1903 gdImageGif(gif,out);
1905 gdImageDestroy(gif);
1914 ImageFile::writeImageRaw (const char* const outfile, int nxcell, int nycell)
1919 ImageFileArray v = getArray();
1921 if ((fp = fopen (outfile, "wb")) == NULL)
1924 for (int irow = ny - 1; irow >= 0; irow--) {
1925 for (int icol = 0; icol < nx; icol++) {
1926 float dens = v[icol][irow];
1927 fwrite(&dens, sizeof(float), 1, fp);