1 /*****************************************************************************
5 ** Purpose: Imagefile classes
6 ** Programmer: Kevin Rosenberg
7 ** Date Started: June 2000
9 ** This is part of the CTSim program
10 ** Copyright (c) 1983-2001 Kevin Rosenberg
12 ** $Id: imagefile.cpp,v 1.41 2001/03/18 18:08:25 kevin Exp $
14 ** This program is free software; you can redistribute it and/or modify
15 ** it under the terms of the GNU General Public License (version 2) as
16 ** published by the Free Software Foundation.
18 ** This program is distributed in the hope that it will be useful,
19 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ** GNU General Public License for more details.
23 ** You should have received a copy of the GNU General Public License
24 ** along with this program; if not, write to the Free Software
25 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 ******************************************************************************/
33 const double ImageFile::s_dRedGrayscaleFactor = 0.299;
34 const double ImageFile::s_dGreenGrayscaleFactor = 0.587;
35 const double ImageFile::s_dBlueGrayscaleFactor = 0.114;
38 const int ImageFile::EXPORT_FORMAT_INVALID = -1;
39 const int ImageFile::EXPORT_FORMAT_TEXT = 0;
40 const int ImageFile::EXPORT_FORMAT_PGM = 1;
41 const int ImageFile::EXPORT_FORMAT_PGMASCII = 2;
43 const int ImageFile::EXPORT_FORMAT_PNG = 3;
44 const int ImageFile::EXPORT_FORMAT_PNG16 = 4;
47 const int ImageFile::EXPORT_FORMAT_DICOM = 5;
50 const char* ImageFile::s_aszExportFormatName[] =
64 const char* ImageFile::s_aszExportFormatTitle[] =
75 const int ImageFile::s_iExportFormatCount = sizeof(s_aszExportFormatName) / sizeof(const char*);
78 const int ImageFile::IMPORT_FORMAT_INVALID = -1;
79 const int ImageFile::IMPORT_FORMAT_PPM = 0;
81 const int ImageFile::IMPORT_FORMAT_PNG = 1;
84 const int ImageFile::IMPORT_FORMAT_DICOM = 2;
88 const char* ImageFile::s_aszImportFormatName[] =
99 const char* ImageFile::s_aszImportFormatTitle[] =
103 #ifdef HAVE_CTN_DICOM
107 const int ImageFile::s_iImportFormatCount = sizeof(s_aszImportFormatName) / sizeof(const char*);
111 F32Image::F32Image (int nx, int ny, int dataType)
112 : Array2dFile (nx, ny, sizeof(kfloat32), Array2dFile::PIXEL_FLOAT32, dataType)
116 F32Image::F32Image (void)
119 setPixelFormat (Array2dFile::PIXEL_FLOAT32);
120 setPixelSize (sizeof(kfloat32));
121 setDataType (Array2dFile::DATA_TYPE_REAL);
124 F64Image::F64Image (int nx, int ny, int dataType)
125 : Array2dFile (nx, ny, sizeof(kfloat64), Array2dFile::PIXEL_FLOAT64, dataType)
129 F64Image::F64Image (void)
132 setPixelFormat (PIXEL_FLOAT64);
133 setPixelSize (sizeof(kfloat64));
134 setDataType (Array2dFile::DATA_TYPE_REAL);
138 ImageFile::getCenterCoordinates (unsigned int& iXCenter, unsigned int& iYCenter)
143 iXCenter = (m_nx - 1) / 2;
148 iYCenter = (m_ny - 1) / 2;
153 ImageFile::filterResponse (const char* const domainName, double bw, const char* const filterName, double filt_param, double dInputScale, double dOutputScale)
155 ImageFileArray v = getArray();
156 SignalFilter filter (filterName, domainName, bw, filt_param);
158 unsigned int iXCenter, iYCenter;
159 getCenterCoordinates (iXCenter, iYCenter);
161 for (unsigned int ix = 0; ix < m_nx; ix++)
162 for (unsigned int iy = 0; iy < m_ny; iy++) {
163 long lD2 = ((ix - iXCenter) * (ix - iXCenter)) + ((iy - iYCenter) * (iy - iYCenter));
164 double r = ::sqrt (static_cast<double>(lD2)) * dInputScale;
165 v[ix][iy] = filter.response (r) * dOutputScale;
170 ImageFile::display (void) const
174 getMinMax (pmin, pmax);
176 return (displayScaling (1, pmin, pmax));
180 ImageFile::displayScaling (const int scale, const ImageFileValue pmin, const ImageFileValue pmax) const
184 ImageFileArrayConst v = getArray();
185 if (v == NULL || nx == 0 || ny == 0)
189 int* pPens = new int [nx * ny * scale * scale ];
191 double view_scale = 255 / (pmax - pmin);
192 int id_X11 = g2_open_X11 (nx * scale, ny * scale);
194 for (int i = 0; i < 256; i++) {
195 double cval = i / 255.;
196 grayscale[i] = g2_ink (id_X11, cval, cval, cval);
199 for (int iy = ny - 1; iy >= 0; iy--) {
200 int iRowPos = ((ny - 1 - iy) * scale) * (nx * scale);
201 for (int ix = 0; ix < nx; ix++) {
202 int cval = static_cast<int>((v[ix][iy] - pmin) * view_scale);
207 for (int sy = 0; sy < scale; sy++)
208 for (int sx = 0; sx < scale; sx++)
209 pPens[iRowPos+(sy * nx * scale)+(sx + (ix * scale))] = grayscale[cval];
213 g2_image (id_X11, 0., 0., nx * scale, ny * scale, pPens);
224 // ImageFile::comparativeStatistics Calculate comparative stats
227 // d Normalized root mean squared distance measure
228 // r Normalized mean absolute distance measure
229 // e Worst case distance measure
232 // G.T. Herman, Image Reconstruction From Projections, 1980
235 ImageFile::comparativeStatistics (const ImageFile& imComp, double& d, double& r, double& e) const
237 if (imComp.nx() != m_nx && imComp.ny() != m_ny) {
238 sys_error (ERR_WARNING, "Image sizes differ [ImageFile::comparativeStatistics]");
241 ImageFileArrayConst v = getArray();
242 if (v == NULL || m_nx == 0 || m_ny == 0)
245 ImageFileArrayConst vComp = imComp.getArray();
248 for (unsigned int ix = 0; ix < m_nx; ix++) {
249 for (unsigned int iy = 0; iy < m_ny; iy++) {
253 myMean /= (m_nx * m_ny);
255 double sqErrorSum = 0.;
256 double absErrorSum = 0.;
257 double sqDiffFromMeanSum = 0.;
258 double absValueSum = 0.;
259 for (unsigned int ix2 = 0; ix2 < m_nx; ix2++) {
260 for (unsigned int iy = 0; iy < m_ny; iy++) {
261 double diff = v[ix2][iy] - vComp[ix2][iy];
262 sqErrorSum += diff * diff;
263 absErrorSum += fabs(diff);
264 double diffFromMean = v[ix2][iy] - myMean;
265 sqDiffFromMeanSum += diffFromMean * diffFromMean;
266 absValueSum += fabs(v[ix2][iy]);
270 d = ::sqrt (sqErrorSum / sqDiffFromMeanSum);
271 r = absErrorSum / absValueSum;
276 for (int ix3 = 0; ix3 < hx; ix3++) {
277 for (int iy = 0; iy < hy; iy++) {
278 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]);
279 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]);
280 double error = fabs (avgPixel - avgPixelComp);
293 ImageFile::printComparativeStatistics (const ImageFile& imComp, std::ostream& os) const
297 if (comparativeStatistics (imComp, d, r, e)) {
298 os << " Normalized root mean squared distance (d): " << d << std::endl;
299 os << " Normalized mean absolute distance (r): " << r << std::endl;
300 os << "Worst case distance (2x2 pixel average) (e): " << e << std::endl;
308 ImageFile::printStatistics (std::ostream& os) const
310 double min, max, mean, mode, median, stddev;
312 statistics (min, max, mean, mode, median, stddev);
314 os << "Real Component Statistics" << std::endl;
316 os << " min: " << min << std::endl;
317 os << " max: " << max << std::endl;
318 os << " mean: " << mean << std::endl;
319 os << " mode: " << mode << std::endl;
320 os << "median: " << median << std::endl;
321 os << "stddev: " << stddev << std::endl;
324 statistics (getImaginaryArray(), min, max, mean, mode, median, stddev);
325 os << std::endl << "Imaginary Component Statistics" << std::endl;
326 os << " min: " << min << std::endl;
327 os << " max: " << max << std::endl;
328 os << " mean: " << mean << std::endl;
329 os << " mode: " << mode << std::endl;
330 os << "median: " << median << std::endl;
331 os << "stddev: " << stddev << std::endl;
337 ImageFile::statistics (double& min, double& max, double& mean, double& mode, double& median, double& stddev) const
339 ImageFileArrayConst v = getArray();
340 statistics (v, min, max, mean, mode, median, stddev);
345 ImageFile::statistics (ImageFileArrayConst v, double& min, double& max, double& mean, double& mode, double& median, double& stddev) const
350 if (v == NULL || nx == 0 || ny == 0)
353 std::vector<double> vecImage;
355 vecImage.resize (nx * ny);
356 for (int ix = 0; ix < nx; ix++) {
357 for (int iy = 0; iy < ny; iy++)
358 vecImage[iVec++] = v[ix][iy];
361 vectorNumericStatistics (vecImage, nx * ny, min, max, mean, mode, median, stddev);
365 ImageFile::getMinMax (double& min, double& max) const
369 ImageFileArrayConst v = getArray();
371 if (v == NULL || nx == 0 || ny == 0)
376 for (int ix = 0; ix < nx; ix++) {
377 for (int iy = 0; iy < ny; iy++) {
387 ImageFile::convertRealToComplex ()
389 if (dataType() != Array2dFile::DATA_TYPE_REAL)
392 if (! reallocRealToComplex())
395 ImageFileArray vImag = getImaginaryArray();
396 for (unsigned int ix = 0; ix < m_nx; ix++) {
397 ImageFileColumn vCol = vImag[ix];
398 for (unsigned int iy = 0; iy < m_ny; iy++)
406 ImageFile::convertComplexToReal ()
408 if (dataType() != Array2dFile::DATA_TYPE_COMPLEX)
411 ImageFileArray vReal = getArray();
412 ImageFileArray vImag = getImaginaryArray();
413 for (unsigned int ix = 0; ix < m_nx; ix++) {
414 ImageFileColumn vRealCol = vReal[ix];
415 ImageFileColumn vImagCol = vImag[ix];
416 for (unsigned int iy = 0; iy < m_ny; iy++) {
417 CTSimComplex c (*vRealCol, *vImagCol);
418 *vRealCol++ = std::abs (c);
423 return reallocComplexToReal();
427 ImageFile::subtractImages (const ImageFile& rRHS, ImageFile& result) const
429 if (m_nx != rRHS.nx() || m_ny != rRHS.ny() || m_nx != result.nx() || m_ny != result.ny()) {
430 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::subtractImage]");
434 if (isComplex() || rRHS.isComplex() && ! result.isComplex())
435 result.convertRealToComplex();
437 ImageFileArrayConst vLHS = getArray();
438 ImageFileArrayConst vLHSImag = getImaginaryArray();
439 ImageFileArrayConst vRHS = rRHS.getArray();
440 ImageFileArrayConst vRHSImag = rRHS.getImaginaryArray();
441 ImageFileArray vResult = result.getArray();
442 ImageFileArray vResultImag = result.getImaginaryArray();
444 for (unsigned int ix = 0; ix < m_nx; ix++) {
445 for (unsigned int iy = 0; iy < m_ny; iy++) {
446 vResult[ix][iy] = vLHS[ix][iy] - vRHS[ix][iy];
447 if (result.isComplex()) {
448 vResultImag[ix][iy] = 0;
450 vResultImag[ix][iy] += vLHSImag[ix][iy];
451 if (rRHS.isComplex())
452 vResultImag[ix][iy] -= vRHSImag[ix][iy];
461 ImageFile::addImages (const ImageFile& rRHS, ImageFile& result) const
463 if (m_nx != rRHS.nx() || m_ny != rRHS.ny() || m_nx != result.nx() || m_ny != result.ny()) {
464 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::subtractImage]");
468 if (isComplex() || rRHS.isComplex() && ! result.isComplex())
469 result.convertRealToComplex();
471 ImageFileArrayConst vLHS = getArray();
472 ImageFileArrayConst vLHSImag = getImaginaryArray();
473 ImageFileArrayConst vRHS = rRHS.getArray();
474 ImageFileArrayConst vRHSImag = rRHS.getImaginaryArray();
475 ImageFileArray vResult = result.getArray();
476 ImageFileArray vResultImag = result.getImaginaryArray();
478 for (unsigned int ix = 0; ix < m_nx; ix++) {
479 for (unsigned int iy = 0; iy < m_ny; iy++) {
480 vResult[ix][iy] = vLHS[ix][iy] + vRHS[ix][iy];
481 if (result.isComplex()) {
482 vResultImag[ix][iy] = 0;
484 vResultImag[ix][iy] += vLHSImag[ix][iy];
485 if (rRHS.isComplex())
486 vResultImag[ix][iy] += vRHSImag[ix][iy];
495 ImageFile::multiplyImages (const ImageFile& rRHS, ImageFile& result) const
497 if (m_nx != rRHS.nx() || m_ny != rRHS.ny() || m_nx != result.nx() || m_ny != result.ny()) {
498 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::subtractImage]");
502 if (isComplex() || rRHS.isComplex() && ! result.isComplex())
503 result.convertRealToComplex();
505 ImageFileArrayConst vLHS = getArray();
506 ImageFileArrayConst vLHSImag = getImaginaryArray();
507 ImageFileArrayConst vRHS = rRHS.getArray();
508 ImageFileArrayConst vRHSImag = rRHS.getImaginaryArray();
509 ImageFileArray vResult = result.getArray();
510 ImageFileArray vResultImag = result.getImaginaryArray();
512 for (unsigned int ix = 0; ix < m_nx; ix++) {
513 for (unsigned int iy = 0; iy < m_ny; iy++) {
514 if (result.isComplex()) {
517 dImag = vLHSImag[ix][iy];
518 std::complex<double> cLHS (vLHS[ix][iy], dImag);
520 if (rRHS.isComplex())
521 dImag = vRHSImag[ix][iy];
522 std::complex<double> cRHS (vRHS[ix][iy], dImag);
523 std::complex<double> cResult = cLHS * cRHS;
524 vResult[ix][iy] = cResult.real();
525 vResultImag[ix][iy] = cResult.imag();
527 vResult[ix][iy] = vLHS[ix][iy] * vRHS[ix][iy];
536 ImageFile::divideImages (const ImageFile& rRHS, ImageFile& result) const
538 if (m_nx != rRHS.nx() || m_ny != rRHS.ny() || m_nx != result.nx() || m_ny != result.ny()) {
539 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::subtractImage]");
543 if (isComplex() || rRHS.isComplex() && ! result.isComplex())
544 result.convertRealToComplex();
546 ImageFileArrayConst vLHS = getArray();
547 ImageFileArrayConst vLHSImag = getImaginaryArray();
548 ImageFileArrayConst vRHS = rRHS.getArray();
549 ImageFileArrayConst vRHSImag = rRHS.getImaginaryArray();
550 ImageFileArray vResult = result.getArray();
551 ImageFileArray vResultImag = result.getImaginaryArray();
553 for (unsigned int ix = 0; ix < m_nx; ix++) {
554 for (unsigned int iy = 0; iy < m_ny; iy++) {
555 if (result.isComplex()) {
558 dImag = vLHSImag[ix][iy];
559 std::complex<double> cLHS (vLHS[ix][iy], dImag);
561 if (rRHS.isComplex())
562 dImag = vRHSImag[ix][iy];
563 std::complex<double> cRHS (vRHS[ix][iy], dImag);
564 std::complex<double> cResult = cLHS / cRHS;
565 vResult[ix][iy] = cResult.real();
566 vResultImag[ix][iy] = cResult.imag();
569 vResult[ix][iy] = vLHS[ix][iy] / vRHS[ix][iy];
581 ImageFile::invertPixelValues (ImageFile& result) const
583 if (m_nx != result.nx() || m_ny != result.ny()) {
584 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
588 if (isComplex() && ! result.isComplex())
589 result.convertRealToComplex();
591 ImageFileArrayConst vLHS = getArray();
592 ImageFileArray vResult = result.getArray();
594 for (unsigned int ix = 0; ix < m_nx; ix++) {
595 ImageFileColumnConst in = vLHS[ix];
596 ImageFileColumn out = vResult[ix];
597 for (unsigned int iy = 0; iy < m_ny; iy++)
605 ImageFile::sqrt (ImageFile& result) const
607 if (m_nx != result.nx() || m_ny != result.ny()) {
608 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
612 if (isComplex() && ! result.isComplex())
613 result.convertRealToComplex();
615 bool bComplexOutput = result.isComplex();
616 ImageFileArrayConst vLHS = getArray();
617 if (! bComplexOutput) // check if should convert to complex output
618 for (unsigned int ix = 0; ix < m_nx; ix++)
619 for (unsigned int iy = 0; iy < m_ny; iy++)
620 if (! bComplexOutput && vLHS[ix][iy] < 0) {
621 result.convertRealToComplex();
622 bComplexOutput = true;
626 ImageFileArrayConst vLHSImag = getImaginaryArray();
627 ImageFileArray vResult = result.getArray();
628 ImageFileArray vResultImag = result.getImaginaryArray();
630 for (unsigned int ix = 0; ix < m_nx; ix++) {
631 for (unsigned int iy = 0; iy < m_ny; iy++) {
632 if (result.isComplex()) {
635 dImag = vLHSImag[ix][iy];
636 std::complex<double> cLHS (vLHS[ix][iy], dImag);
637 std::complex<double> cResult = std::sqrt(cLHS);
638 vResult[ix][iy] = cResult.real();
639 vResultImag[ix][iy] = cResult.imag();
641 vResult[ix][iy] = ::sqrt (vLHS[ix][iy]);
650 ImageFile::log (ImageFile& result) const
652 if (m_nx != result.nx() || m_ny != result.ny()) {
653 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::log]");
657 if (isComplex() && ! result.isComplex())
658 result.convertRealToComplex();
660 ImageFileArrayConst vLHS = getArray();
661 ImageFileArrayConst vLHSImag = getImaginaryArray();
662 ImageFileArray vResult = result.getArray();
663 ImageFileArray vResultImag = result.getImaginaryArray();
665 for (unsigned int ix = 0; ix < m_nx; ix++) {
666 for (unsigned int iy = 0; iy < m_ny; iy++) {
667 if (result.isComplex()) {
670 dImag = vLHSImag[ix][iy];
671 std::complex<double> cLHS (vLHS[ix][iy], dImag);
672 std::complex<double> cResult = std::log (cLHS);
673 vResult[ix][iy] = cResult.real();
674 vResultImag[ix][iy] = cResult.imag();
676 if (vLHS[ix][iy] > 0)
677 vResult[ix][iy] = ::log (vLHS[ix][iy]);
689 ImageFile::exp (ImageFile& result) const
691 if (m_nx != result.nx() || m_ny != result.ny()) {
692 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
696 if (isComplex() && ! result.isComplex())
697 result.convertRealToComplex();
699 ImageFileArrayConst vLHS = getArray();
700 ImageFileArrayConst vLHSImag = getImaginaryArray();
701 ImageFileArray vResult = result.getArray();
702 ImageFileArray vResultImag = result.getImaginaryArray();
704 for (unsigned int ix = 0; ix < m_nx; ix++) {
705 for (unsigned int iy = 0; iy < m_ny; iy++) {
706 if (result.isComplex()) {
709 dImag = vLHSImag[ix][iy];
710 std::complex<double> cLHS (vLHS[ix][iy], dImag);
711 std::complex<double> cResult = std::exp (cLHS);
712 vResult[ix][iy] = cResult.real();
713 vResultImag[ix][iy] = cResult.imag();
715 vResult[ix][iy] = ::exp (vLHS[ix][iy]);
724 ImageFile::scaleImage (ImageFile& result) const
726 unsigned int nx = m_nx;
727 unsigned int ny = m_ny;
728 unsigned int newNX = result.nx();
729 unsigned int newNY = result.ny();
731 double dXScale = static_cast<double>(newNX) / static_cast<double>(nx);
732 double dYScale = static_cast<double>(newNY) / static_cast<double>(ny);
734 if (isComplex() && ! result.isComplex())
735 result.convertRealToComplex();
737 ImageFileArrayConst vReal = getArray();
738 ImageFileArrayConst vImag = getImaginaryArray();
739 ImageFileArray vResult = result.getArray();
740 ImageFileArray vResultImag = result.getImaginaryArray();
742 for (unsigned int ix = 0; ix < newNX; ix++) {
743 for (unsigned int iy = 0; iy < newNY; iy++) {
744 double dXPos = ix / dXScale;
745 double dYPos = iy / dYScale;
746 unsigned int scaleNX = static_cast<unsigned int> (dXPos);
747 unsigned int scaleNY = static_cast<unsigned int> (dYPos);
748 double dXFrac = dXPos - scaleNX;
749 double dYFrac = dYPos - scaleNY;
750 if (scaleNX >= nx - 1 || scaleNY >= ny - 1) {
751 vResult[ix][iy] = vReal[scaleNX][scaleNY];
752 if (result.isComplex()) {
754 vResultImag[ix][iy] = vImag[scaleNX][scaleNY];
756 vResultImag[ix][iy] = 0;
759 vResult[ix][iy] = (1 - dXFrac) * (1 - dYFrac) * vReal[scaleNX][scaleNY] +
760 dXFrac * (1 - dYFrac) * vReal[scaleNX+1][scaleNY] +
761 dYFrac * (1 - dXFrac) * vReal[scaleNX][scaleNY+1] +
762 dXFrac * dYFrac * vReal[scaleNX+1][scaleNY+1];
763 if (result.isComplex()) {
765 vResultImag[ix][iy] = (1 - dXFrac) * (1 - dYFrac) * vImag[scaleNX][scaleNY] +
766 dXFrac * (1 - dYFrac) * vImag[scaleNX+1][scaleNY] +
767 dYFrac * (1 - dXFrac) * vImag[scaleNX][scaleNY+1] +
768 dXFrac * dYFrac * vImag[scaleNX+1][scaleNY+1];
770 vResultImag[ix][iy] = 0;
781 ImageFile::fft (ImageFile& result) const
783 if (m_nx != result.nx() || m_ny != result.ny()) {
784 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
788 if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {
789 if (! result.convertRealToComplex ())
793 fftw_complex* in = new fftw_complex [m_nx * m_ny];
795 ImageFileArrayConst vReal = getArray();
796 ImageFileArrayConst vImag = getImaginaryArray();
799 unsigned int iArray = 0;
800 for (ix = 0; ix < m_nx; ix++)
801 for (iy = 0; iy < m_ny; iy++) {
802 in[iArray].re = vReal[ix][iy];
804 in[iArray].im = vImag[ix][iy];
810 fftwnd_plan plan = fftw2d_create_plan (m_nx, m_ny, FFTW_FORWARD, FFTW_IN_PLACE);
812 fftwnd_one (plan, in, NULL);
814 ImageFileArray vRealResult = result.getArray();
815 ImageFileArray vImagResult = result.getImaginaryArray();
817 unsigned int iScale = m_nx * m_ny;
818 for (ix = 0; ix < m_nx; ix++)
819 for (iy = 0; iy < m_ny; iy++) {
820 vRealResult[ix][iy] = in[iArray].re / iScale;
821 vImagResult[ix][iy] = in[iArray].im / iScale;
825 fftwnd_destroy_plan (plan);
829 Fourier::shuffleFourierToNaturalOrder (result);
836 ImageFile::ifft (ImageFile& result) const
838 if (m_nx != result.nx() || m_ny != result.ny()) {
839 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
843 if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {
844 if (! result.convertRealToComplex ())
848 ImageFileArrayConst vReal = getArray();
849 ImageFileArrayConst vImag = getImaginaryArray();
850 ImageFileArray vRealResult = result.getArray();
851 ImageFileArray vImagResult = result.getImaginaryArray();
853 for (ix = 0; ix < m_nx; ix++)
854 for (iy = 0; iy < m_ny; iy++) {
855 vRealResult[ix][iy] = vReal[ix][iy];
857 vImagResult[ix][iy] = vImag[ix][iy];
859 vImagResult[ix][iy] = 0;
862 Fourier::shuffleNaturalToFourierOrder (result);
864 fftw_complex* in = new fftw_complex [m_nx * m_ny];
866 unsigned int iArray = 0;
867 for (ix = 0; ix < m_nx; ix++)
868 for (iy = 0; iy < m_ny; iy++) {
869 in[iArray].re = vRealResult[ix][iy];
870 in[iArray].im = vImagResult[ix][iy];
874 fftwnd_plan plan = fftw2d_create_plan (m_nx, m_ny, FFTW_BACKWARD, FFTW_IN_PLACE);
876 fftwnd_one (plan, in, NULL);
879 for (ix = 0; ix < m_nx; ix++)
880 for (iy = 0; iy < m_ny; iy++) {
881 vRealResult[ix][iy] = in[iArray].re;
882 vImagResult[ix][iy] = in[iArray].im;
886 fftwnd_destroy_plan (plan);
894 ImageFile::fftRows (ImageFile& result) const
896 if (m_nx != result.nx() || m_ny != result.ny()) {
897 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::fftRows]");
901 if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {
902 if (! result.convertRealToComplex ())
906 fftw_complex* in = new fftw_complex [m_nx];
908 ImageFileArrayConst vReal = getArray();
909 ImageFileArrayConst vImag = getImaginaryArray();
911 fftw_plan plan = fftw_create_plan (m_nx, FFTW_FORWARD, FFTW_IN_PLACE);
912 std::complex<double>* pcRow = new std::complex<double> [m_nx];
915 unsigned int iArray = 0;
916 for (iy = 0; iy < m_ny; iy++) {
917 for (ix = 0; ix < m_nx; ix++) {
918 in[ix].re = vReal[ix][iy];
920 in[ix].im = vImag[ix][iy];
925 fftw_one (plan, in, NULL);
927 for (ix = 0; ix < m_nx; ix++)
928 pcRow[ix] = std::complex<double>(in[ix].re, in[ix].im);
930 Fourier::shuffleFourierToNaturalOrder (pcRow, m_nx);
931 for (ix = 0; ix < m_nx; ix++) {
932 vReal[ix][iy] = pcRow[ix].real() / m_nx;
933 vImag[ix][iy] = pcRow[ix].imag() / m_nx;
938 fftw_destroy_plan (plan);
945 ImageFile::ifftRows (ImageFile& result) const
947 if (m_nx != result.nx() || m_ny != result.ny()) {
948 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::fftRows]");
952 if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {
953 if (! result.convertRealToComplex ())
957 fftw_complex* in = new fftw_complex [m_nx];
959 ImageFileArrayConst vReal = getArray();
960 ImageFileArrayConst vImag = getImaginaryArray();
962 fftw_plan plan = fftw_create_plan (m_nx, FFTW_BACKWARD, FFTW_IN_PLACE);
963 std::complex<double>* pcRow = new std::complex<double> [m_nx];
966 unsigned int iArray = 0;
967 for (iy = 0; iy < m_ny; iy++) {
968 for (ix = 0; ix < m_nx; ix++) {
971 dImag = vImag[ix][iy];
972 pcRow[ix] = std::complex<double> (vReal[ix][iy], dImag);
975 Fourier::shuffleNaturalToFourierOrder (pcRow, m_nx);
977 for (ix = 0; ix < m_nx; ix++) {
978 in[ix].re = pcRow[ix].real();
979 in[ix].im = pcRow[ix].imag();
982 fftw_one (plan, in, NULL);
984 for (ix = 0; ix < m_nx; ix++) {
985 vReal[ix][iy] = in[ix].re;
986 vImag[ix][iy] = in[ix].im;
991 fftw_destroy_plan (plan);
998 ImageFile::fftCols (ImageFile& result) const
1000 if (m_nx != result.nx() || m_ny != result.ny()) {
1001 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::fftRows]");
1005 if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {
1006 if (! result.convertRealToComplex ())
1010 fftw_complex* in = new fftw_complex [m_ny];
1012 ImageFileArrayConst vReal = getArray();
1013 ImageFileArrayConst vImag = getImaginaryArray();
1015 fftw_plan plan = fftw_create_plan (m_ny, FFTW_FORWARD, FFTW_IN_PLACE);
1016 std::complex<double>* pcCol = new std::complex<double> [m_ny];
1018 unsigned int ix, iy;
1019 unsigned int iArray = 0;
1020 for (ix = 0; ix < m_nx; ix++) {
1021 for (iy = 0; iy < m_ny; iy++) {
1022 in[iy].re = vReal[ix][iy];
1024 in[iy].im = vImag[ix][iy];
1029 fftw_one (plan, in, NULL);
1031 for (iy = 0; iy < m_ny; iy++)
1032 pcCol[iy] = std::complex<double>(in[iy].re, in[iy].im);
1034 Fourier::shuffleFourierToNaturalOrder (pcCol, m_ny);
1035 for (iy = 0; iy < m_ny; iy++) {
1036 vReal[ix][iy] = pcCol[iy].real() / m_ny;
1037 vImag[ix][iy] = pcCol[iy].imag() / m_ny;
1042 fftw_destroy_plan (plan);
1049 ImageFile::ifftCols (ImageFile& result) const
1051 if (m_nx != result.nx() || m_ny != result.ny()) {
1052 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::fftRows]");
1056 if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {
1057 if (! result.convertRealToComplex ())
1061 fftw_complex* in = new fftw_complex [m_ny];
1063 ImageFileArrayConst vReal = getArray();
1064 ImageFileArrayConst vImag = getImaginaryArray();
1066 fftw_plan plan = fftw_create_plan (m_ny, FFTW_BACKWARD, FFTW_IN_PLACE);
1067 std::complex<double>* pcCol = new std::complex<double> [m_ny];
1069 unsigned int ix, iy;
1070 unsigned int iArray = 0;
1071 for (ix = 0; ix < m_nx; ix++) {
1072 for (iy = 0; iy < m_ny; iy++) {
1075 dImag = vImag[ix][iy];
1076 pcCol[iy] = std::complex<double> (vReal[ix][iy], dImag);
1079 Fourier::shuffleNaturalToFourierOrder (pcCol, m_ny);
1081 for (iy = 0; iy < m_ny; iy++) {
1082 in[iy].re = pcCol[iy].real();
1083 in[iy].im = pcCol[iy].imag();
1086 fftw_one (plan, in, NULL);
1088 for (iy = 0; iy < m_ny; iy++) {
1089 vReal[ix][iy] = in[iy].re;
1090 vImag[ix][iy] = in[iy].im;
1095 fftw_destroy_plan (plan);
1106 ImageFile::fourier (ImageFile& result) const
1108 if (m_nx != result.nx() || m_ny != result.ny()) {
1109 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
1113 if (! result.isComplex())
1114 if (! result.convertRealToComplex ())
1117 ImageFileArrayConst vLHS = getArray();
1118 ImageFileArrayConst vLHSImag = getImaginaryArray();
1119 ImageFileArray vRealResult = result.getArray();
1120 ImageFileArray vImagResult = result.getImaginaryArray();
1122 unsigned int ix, iy;
1124 // alloc output matrix
1125 CTSimComplex** complexOut = new CTSimComplex* [m_nx];
1126 for (ix = 0; ix < m_nx; ix++)
1127 complexOut[ix] = new CTSimComplex [m_ny];
1129 // fourier each x column
1130 CTSimComplex* pY = new CTSimComplex [m_ny];
1131 for (ix = 0; ix < m_nx; ix++) {
1132 for (iy = 0; iy < m_ny; iy++) {
1135 dImag = vLHSImag[ix][iy];
1136 pY[iy] = std::complex<double>(vLHS[ix][iy], dImag);
1138 ProcessSignal::finiteFourierTransform (pY, complexOut[ix], m_ny, ProcessSignal::FORWARD);
1142 // fourier each y row
1143 CTSimComplex* pX = new CTSimComplex [m_nx];
1144 CTSimComplex* complexOutRow = new CTSimComplex [m_nx];
1145 for (iy = 0; iy < m_ny; iy++) {
1146 for (ix = 0; ix < m_nx; ix++)
1147 pX[ix] = complexOut[ix][iy];
1148 ProcessSignal::finiteFourierTransform (pX, complexOutRow, m_nx, ProcessSignal::FORWARD);
1149 for (ix = 0; ix < m_nx; ix++)
1150 complexOut[ix][iy] = complexOutRow[ix];
1153 delete [] complexOutRow;
1155 for (ix = 0; ix < m_nx; ix++)
1156 for (iy = 0; iy < m_ny; iy++) {
1157 vRealResult[ix][iy] = complexOut[ix][iy].real();
1158 vImagResult[ix][iy] = complexOut[ix][iy].imag();
1161 Fourier::shuffleFourierToNaturalOrder (result);
1163 // delete complexOut matrix
1164 for (ix = 0; ix < m_nx; ix++)
1165 delete [] complexOut[ix];
1166 delete [] complexOut;
1172 ImageFile::inverseFourier (ImageFile& result) const
1174 if (m_nx != result.nx() || m_ny != result.ny()) {
1175 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
1179 if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {
1180 if (! result.convertRealToComplex ())
1184 ImageFileArrayConst vLHSReal = getArray();
1185 ImageFileArrayConst vLHSImag = getImaginaryArray();
1186 ImageFileArray vRealResult = result.getArray();
1187 ImageFileArray vImagResult = result.getImaginaryArray();
1189 unsigned int ix, iy;
1190 // alloc 2d complex output matrix
1191 CTSimComplex** complexOut = new CTSimComplex* [m_nx];
1192 for (ix = 0; ix < m_nx; ix++)
1193 complexOut[ix] = new CTSimComplex [m_ny];
1195 // put input image into result
1196 for (ix = 0; ix < m_nx; ix++)
1197 for (iy = 0; iy < m_ny; iy++) {
1198 vRealResult[ix][iy] = vLHSReal[ix][iy];
1200 vImagResult[ix][iy] = vLHSImag[ix][iy];
1202 vImagResult[ix][iy] = 0;
1205 Fourier::shuffleNaturalToFourierOrder (result);
1207 // ifourier each x column
1208 CTSimComplex* pCol = new CTSimComplex [m_ny];
1209 for (ix = 0; ix < m_nx; ix++) {
1210 for (iy = 0; iy < m_ny; iy++) {
1211 pCol[iy] = std::complex<double> (vRealResult[ix][iy], vImagResult[ix][iy]);
1213 ProcessSignal::finiteFourierTransform (pCol, complexOut[ix], m_ny, ProcessSignal::BACKWARD);
1217 // ifourier each y row
1218 CTSimComplex* complexInRow = new CTSimComplex [m_nx];
1219 CTSimComplex* complexOutRow = new CTSimComplex [m_nx];
1220 for (iy = 0; iy < m_ny; iy++) {
1221 for (ix = 0; ix < m_nx; ix++)
1222 complexInRow[ix] = complexOut[ix][iy];
1223 ProcessSignal::finiteFourierTransform (complexInRow, complexOutRow, m_nx, ProcessSignal::BACKWARD);
1224 for (ix = 0; ix < m_nx; ix++)
1225 complexOut[ix][iy] = complexOutRow[ix];
1227 delete [] complexInRow;
1228 delete [] complexOutRow;
1230 for (ix = 0; ix < m_nx; ix++)
1231 for (iy = 0; iy < m_ny; iy++) {
1232 vRealResult[ix][iy] = complexOut[ix][iy].real();
1233 vImagResult[ix][iy] = complexOut[ix][iy].imag();
1236 // delete complexOut matrix
1237 for (ix = 0; ix < m_nx; ix++)
1238 delete [] complexOut[ix];
1239 delete [] complexOut;
1246 ImageFile::magnitude (ImageFile& result) const
1248 if (m_nx != result.nx() || m_ny != result.ny()) {
1249 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
1253 ImageFileArray vReal = getArray();
1254 ImageFileArray vImag = getImaginaryArray();
1255 ImageFileArray vRealResult = result.getArray();
1257 for (unsigned int ix = 0; ix < m_nx; ix++)
1258 for (unsigned int iy = 0; iy < m_ny; iy++) {
1260 vRealResult[ix][iy] = ::sqrt (vReal[ix][iy] * vReal[ix][iy] + vImag[ix][iy] * vImag[ix][iy]);
1262 vRealResult[ix][iy] = vReal[ix][iy];
1265 if (result.isComplex())
1266 result.convertComplexToReal();
1272 ImageFile::phase (ImageFile& result) const
1274 if (m_nx != result.nx() || m_ny != result.ny()) {
1275 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
1279 ImageFileArray vReal = getArray();
1280 ImageFileArray vImag = getImaginaryArray();
1281 ImageFileArray vRealResult = result.getArray();
1283 for (unsigned int ix = 0; ix < m_nx; ix++)
1284 for (unsigned int iy = 0; iy < m_ny; iy++) {
1286 vRealResult[ix][iy] = ::atan2 (vImag[ix][iy], vReal[ix][iy]);
1288 vRealResult[ix][iy] = 0;
1291 if (result.isComplex())
1292 result.convertComplexToReal();
1298 ImageFile::square (ImageFile& result) const
1300 if (m_nx != result.nx() || m_ny != result.ny()) {
1301 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
1305 if (isComplex() && ! result.isComplex())
1306 result.convertRealToComplex();
1308 ImageFileArrayConst vLHS = getArray();
1309 ImageFileArrayConst vLHSImag = getImaginaryArray();
1310 ImageFileArray vResult = result.getArray();
1311 ImageFileArray vResultImag = result.getImaginaryArray();
1313 for (unsigned int ix = 0; ix < m_nx; ix++) {
1314 for (unsigned int iy = 0; iy < m_ny; iy++) {
1315 if (result.isComplex()) {
1318 dImag = vLHSImag[ix][iy];
1319 std::complex<double> cLHS (vLHS[ix][iy], dImag);
1320 std::complex<double> cResult = cLHS * cLHS;
1321 vResult[ix][iy] = cResult.real();
1322 vResultImag[ix][iy] = cResult.imag();
1324 vResult[ix][iy] = vLHS[ix][iy] * vLHS[ix][iy];
1333 ImageFile::convertExportFormatNameToID (const char* const formatName)
1335 int formatID = EXPORT_FORMAT_INVALID;
1337 for (int i = 0; i < s_iExportFormatCount; i++)
1338 if (strcasecmp (formatName, s_aszExportFormatName[i]) == 0) {
1347 ImageFile::convertExportFormatIDToName (int formatID)
1349 static const char *formatName = "";
1351 if (formatID >= 0 && formatID < s_iExportFormatCount)
1352 return (s_aszExportFormatName[formatID]);
1354 return (formatName);
1358 ImageFile::convertExportFormatIDToTitle (const int formatID)
1360 static const char *formatTitle = "";
1362 if (formatID >= 0 && formatID < s_iExportFormatCount)
1363 return (s_aszExportFormatTitle[formatID]);
1365 return (formatTitle);
1369 ImageFile::convertImportFormatNameToID (const char* const formatName)
1371 int formatID = IMPORT_FORMAT_INVALID;
1373 for (int i = 0; i < s_iImportFormatCount; i++)
1374 if (strcasecmp (formatName, s_aszImportFormatName[i]) == 0) {
1383 ImageFile::convertImportFormatIDToName (int formatID)
1385 static const char *formatName = "";
1387 if (formatID >= 0 && formatID < s_iImportFormatCount)
1388 return (s_aszImportFormatName[formatID]);
1390 return (formatName);
1394 ImageFile::convertImportFormatIDToTitle (const int formatID)
1396 static const char *formatTitle = "";
1398 if (formatID >= 0 && formatID < s_iImportFormatCount)
1399 return (s_aszImportFormatTitle[formatID]);
1401 return (formatTitle);
1405 ImageFile::importImage (const char* const pszFormat, const char* const pszFilename)
1407 int iFormatID = convertImportFormatNameToID (pszFormat);
1409 if (iFormatID == IMPORT_FORMAT_PPM)
1410 return readImagePPM (pszFilename);
1412 else if (iFormatID == IMPORT_FORMAT_PNG)
1413 return readImagePNG (pszFilename);
1416 sys_error (ERR_SEVERE, "Invalid format %s [ImageFile::importImage]", pszFormat);
1421 ImageFile::skipSpacePPM (FILE* fp)
1424 while (isspace (c) || c == '#') {
1425 if (c == '#') { // comment until end of line
1427 while (c != 13 && c != 10)
1438 ImageFile::readImagePPM (const char* const pszFile)
1440 FILE* fp = fopen (pszFile, "r");
1441 if ((fp = fopen (pszFile, "r")) == NULL)
1443 char cSignature = toupper(fgetc(fp));
1444 if (cSignature != 'P') {
1448 cSignature = fgetc(fp);
1449 if (cSignature == '5' || cSignature == '6') { // binary modes
1451 fp = fopen(pszFile, "rb"); // reopen in binary mode
1454 } else if (cSignature != '2' && cSignature != '3') {
1459 int nRows, nCols, iMaxValue;
1461 if (fscanf (fp, "%d", &nCols) != 1) {
1466 if (fscanf (fp, "%d", &nRows) != 1) {
1471 if (fscanf (fp, "%d", &iMaxValue) != 1) {
1475 setArraySize (nRows, nCols);
1477 if (cSignature == '5' || cSignature == '6') { // binary modes
1481 if (c != 10) // read msdos 13-10 newline
1485 skipSpacePPM (fp); // ascii may have comments
1487 double dMaxValue = iMaxValue;
1488 ImageFileArray v = getArray();
1489 for (int iy = nRows - 1; iy >= 0; iy--) {
1490 for (int ix = 0; ix < nCols; ix++) {
1491 int iGS, iR, iG, iB;
1493 switch (cSignature) {
1495 if (fscanf(fp, "%d ", &iGS) != 1) {
1499 v[ix][iy] = iGS / dMaxValue;
1507 v[ix][iy] = iGS / dMaxValue;
1510 if (fscanf (fp, "%d %d %d ", &iR, &iG, &iB) != 3) {
1514 dR = iR / dMaxValue;
1515 dG = iG / dMaxValue;
1516 dB = iB / dMaxValue;
1517 v[ix][iy] = colorToGrayscale (dR, dG, dB);
1527 dR = iR / dMaxValue;
1528 dG = iG / dMaxValue;
1529 dB = iB / dMaxValue;
1530 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 fread (header, 1, 8, fp);
1549 if (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_ptr->jmpbuf)) {
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.;
1628 } else if (color_type == PNG_COLOR_TYPE_RGB) {
1629 if (bit_depth == 8) {
1631 double dR = row_pointers[iy][iBase] / 255.;
1632 double dG = row_pointers[iy][iBase+1] / 255.;
1633 double dB = row_pointers[iy][iBase+2] / 255.;
1634 dV = colorToGrayscale (dR, dG, dR);
1637 v[ix][height-iy-1] = dV;
1641 png_read_end(png_ptr, end_info);
1642 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
1644 for (i = 0; i < height; i++)
1645 delete row_pointers[i];
1646 delete row_pointers;
1654 ImageFile::exportImage (const char* const pszFormat, const char* const pszFilename, int nxcell, int nycell, double densmin, double densmax)
1656 int iFormatID = convertExportFormatNameToID (pszFormat);
1658 if (iFormatID == EXPORT_FORMAT_PGM)
1659 return writeImagePGM (pszFilename, nxcell, nycell, densmin, densmax);
1660 else if (iFormatID == EXPORT_FORMAT_PGMASCII)
1661 return writeImagePGMASCII (pszFilename, nxcell, nycell, densmin, densmax);
1662 else if (iFormatID == EXPORT_FORMAT_TEXT)
1663 return writeImageText (pszFilename);
1665 else if (iFormatID == EXPORT_FORMAT_PNG)
1666 return writeImagePNG (pszFilename, 8, nxcell, nycell, densmin, densmax);
1667 else if (iFormatID == EXPORT_FORMAT_PNG16)
1668 return writeImagePNG (pszFilename, 16, nxcell, nycell, densmin, densmax);
1670 #ifdef HAVE_CTN_DICOM
1671 else if (iFormatID == EXPORT_FORMAT_DICOM) {
1672 DicomExporter dicomExport (this);
1673 bool bSuccess = dicomExport.writeFile (pszFilename);
1675 sys_error (ERR_SEVERE, dicomExport.failMessage().c_str());
1680 sys_error (ERR_SEVERE, "Invalid format %s [ImageFile::exportImage]", pszFormat);
1686 ImageFile::writeImagePGM (const char* const outfile, int nxcell, int nycell, double densmin, double densmax)
1691 ImageFileArray v = getArray();
1693 unsigned char* rowp = new unsigned char [nx * nxcell];
1695 if ((fp = fopen (outfile, "wb")) == NULL)
1698 fprintf(fp, "P5\n");
1699 fprintf(fp, "%d %d\n", nx, ny);
1700 fprintf(fp, "255\n");
1702 for (int irow = ny - 1; irow >= 0; irow--) {
1703 for (int icol = 0; icol < nx; icol++) {
1704 int pos = icol * nxcell;
1705 double dens = (v[icol][irow] - densmin) / (densmax - densmin);
1706 dens = clamp (dens, 0., 1.);
1707 for (int p = pos; p < pos + nxcell; p++) {
1708 rowp[p] = static_cast<unsigned int> (dens * 255.);
1711 for (int ir = 0; ir < nycell; ir++) {
1712 for (int ic = 0; ic < nx * nxcell; ic++)
1713 fputc( rowp[ic], fp );
1724 ImageFile::writeImagePGMASCII (const char* const outfile, int nxcell, int nycell, double densmin, double densmax)
1729 ImageFileArray v = getArray();
1731 unsigned char* rowp = new unsigned char [nx * nxcell];
1733 if ((fp = fopen (outfile, "wb")) == NULL)
1736 fprintf(fp, "P2\n");
1737 fprintf(fp, "%d %d\n", nx, ny);
1738 fprintf(fp, "255\n");
1740 for (int irow = ny - 1; irow >= 0; irow--) {
1741 for (int icol = 0; icol < nx; icol++) {
1742 int pos = icol * nxcell;
1743 double dens = (v[icol][irow] - densmin) / (densmax - densmin);
1744 dens = clamp (dens, 0., 1.);
1745 for (int p = pos; p < pos + nxcell; p++) {
1746 rowp[p] = static_cast<unsigned int> (dens * 255.);
1749 for (int ir = 0; ir < nycell; ir++) {
1750 for (int ic = 0; ic < nx * nxcell; ic++)
1751 fprintf(fp, "%d ", rowp[ic]);
1763 ImageFile::writeImageText (const char* const outfile)
1768 ImageFileArray v = getArray();
1769 ImageFileArray vImag = getImaginaryArray();
1771 if ((fp = fopen (outfile, "w")) == NULL)
1774 for (int irow = ny - 1; irow >= 0; irow--) {
1775 for (int icol = 0; icol < nx; icol++) {
1777 if (vImag[icol][irow] >= 0)
1778 fprintf (fp, "%.9g+%.9gi ", v[icol][irow], vImag[icol][irow]);
1780 fprintf (fp, "%.9g-%.9gi ", v[icol][irow], -vImag[icol][irow]);
1782 fprintf (fp, "%12.8g ", v[icol][irow]);
1795 ImageFile::writeImagePNG (const char* const outfile, int bitdepth, int nxcell, int nycell, double densmin, double densmax)
1797 double max_out_level = (1 << bitdepth) - 1;
1800 ImageFileArray v = getArray();
1802 unsigned char* rowp = new unsigned char [nx * nxcell * (bitdepth / 8)];
1804 FILE *fp = fopen (outfile, "wb");
1808 png_structp png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
1812 png_infop info_ptr = png_create_info_struct (png_ptr);
1814 png_destroy_write_struct (&png_ptr, (png_infopp) NULL);
1819 if (setjmp (png_ptr->jmpbuf)) {
1820 png_destroy_write_struct (&png_ptr, &info_ptr);
1825 png_init_io(png_ptr, fp);
1827 png_set_IHDR (png_ptr, info_ptr, nx * nxcell, ny * nycell, bitdepth, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_DEFAULT);
1829 png_write_info(png_ptr, info_ptr);
1830 for (int irow = ny - 1; irow >= 0; irow--) {
1831 png_bytep row_pointer = rowp;
1833 for (int icol = 0; icol < nx; icol++) {
1834 int pos = icol * nxcell;
1835 double dens = (v[icol][irow] - densmin) / (densmax - densmin);
1836 dens = clamp (dens, 0., 1.);
1837 unsigned int outval = static_cast<unsigned int> (dens * max_out_level);
1839 for (int p = pos; p < pos + nxcell; p++) {
1844 rowp[rowpos+1] = (outval >> 8) & 0xFF;
1845 rowp[rowpos] = (outval & 0xFF);
1849 for (int ir = 0; ir < nycell; ir++)
1850 png_write_rows (png_ptr, &row_pointer, 1);
1853 png_write_end (png_ptr, info_ptr);
1854 png_destroy_write_struct (&png_ptr, &info_ptr);
1865 static const int N_GRAYSCALE=256;
1868 ImageFile::writeImageGIF (const char* const outfile, int nxcell, int nycell, double densmin, double densmax)
1870 int gs_indices[N_GRAYSCALE];
1873 ImageFileArray v = getArray();
1875 unsigned char* rowp = new unsigned char [nx * nxcell];
1877 gdImagePtr gif = gdImageCreate(nx * nxcell, ny * nycell);
1878 for (int i = 0; i < N_GRAYSCALE; i++)
1879 gs_indices[i] = gdImageColorAllocate(gif, i, i, i);
1881 int lastrow = ny * nycell - 1;
1882 for (int irow = 0; irow < ny; irow++) {
1883 int rpos = irow * nycell;
1884 for (int ir = rpos; ir < rpos + nycell; ir++) {
1885 for (int icol = 0; icol < nx; icol++) {
1886 int cpos = icol * nxcell;
1887 double dens = (v[icol][irow] - densmin) / (densmax - densmin);
1888 dens = clamp(dens, 0., 1.);
1889 for (int ic = cpos; ic < cpos + nxcell; ic++) {
1890 rowp[ic] = (unsigned int) (dens * (double) (N_GRAYSCALE - 1));
1891 gdImageSetPixel(gif, ic, lastrow - ir, gs_indices[rowp[ic]]);
1898 if ((out = fopen (outfile,"w")) == NULL) {
1899 sys_error(ERR_SEVERE, "Error opening output file %s for writing", outfile);
1902 gdImageGif(gif,out);
1904 gdImageDestroy(gif);