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.40 2001/03/07 16:34:47 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_PGM = 0;
40 const int ImageFile::EXPORT_FORMAT_PGMASCII = 1;
42 const int ImageFile::EXPORT_FORMAT_PNG = 2;
43 const int ImageFile::EXPORT_FORMAT_PNG16 = 3;
46 const int ImageFile::EXPORT_FORMAT_DICOM = 4;
49 const char* ImageFile::s_aszExportFormatName[] =
62 const char* ImageFile::s_aszExportFormatTitle[] =
72 const int ImageFile::s_iExportFormatCount = sizeof(s_aszExportFormatName) / sizeof(const char*);
75 const int ImageFile::IMPORT_FORMAT_INVALID = -1;
76 const int ImageFile::IMPORT_FORMAT_PPM = 0;
78 const int ImageFile::IMPORT_FORMAT_PNG = 1;
81 const int ImageFile::IMPORT_FORMAT_DICOM = 2;
85 const char* ImageFile::s_aszImportFormatName[] =
96 const char* ImageFile::s_aszImportFormatTitle[] =
100 #ifdef HAVE_CTN_DICOM
104 const int ImageFile::s_iImportFormatCount = sizeof(s_aszImportFormatName) / sizeof(const char*);
108 F32Image::F32Image (int nx, int ny, int dataType)
109 : Array2dFile (nx, ny, sizeof(kfloat32), Array2dFile::PIXEL_FLOAT32, dataType)
113 F32Image::F32Image (void)
116 setPixelFormat (Array2dFile::PIXEL_FLOAT32);
117 setPixelSize (sizeof(kfloat32));
118 setDataType (Array2dFile::DATA_TYPE_REAL);
121 F64Image::F64Image (int nx, int ny, int dataType)
122 : Array2dFile (nx, ny, sizeof(kfloat64), Array2dFile::PIXEL_FLOAT64, dataType)
126 F64Image::F64Image (void)
129 setPixelFormat (PIXEL_FLOAT64);
130 setPixelSize (sizeof(kfloat64));
131 setDataType (Array2dFile::DATA_TYPE_REAL);
135 ImageFile::getCenterCoordinates (unsigned int& iXCenter, unsigned int& iYCenter)
140 iXCenter = (m_nx - 1) / 2;
145 iYCenter = (m_ny - 1) / 2;
150 ImageFile::filterResponse (const char* const domainName, double bw, const char* const filterName, double filt_param, double dInputScale, double dOutputScale)
152 ImageFileArray v = getArray();
153 SignalFilter filter (filterName, domainName, bw, filt_param);
155 unsigned int iXCenter, iYCenter;
156 getCenterCoordinates (iXCenter, iYCenter);
158 for (unsigned int ix = 0; ix < m_nx; ix++)
159 for (unsigned int iy = 0; iy < m_ny; iy++) {
160 long lD2 = ((ix - iXCenter) * (ix - iXCenter)) + ((iy - iYCenter) * (iy - iYCenter));
161 double r = ::sqrt (static_cast<double>(lD2)) * dInputScale;
162 v[ix][iy] = filter.response (r) * dOutputScale;
167 ImageFile::display (void) const
171 getMinMax (pmin, pmax);
173 return (displayScaling (1, pmin, pmax));
177 ImageFile::displayScaling (const int scale, const ImageFileValue pmin, const ImageFileValue pmax) const
181 ImageFileArrayConst v = getArray();
182 if (v == NULL || nx == 0 || ny == 0)
186 int* pPens = new int [nx * ny * scale * scale ];
188 double view_scale = 255 / (pmax - pmin);
189 int id_X11 = g2_open_X11 (nx * scale, ny * scale);
191 for (int i = 0; i < 256; i++) {
192 double cval = i / 255.;
193 grayscale[i] = g2_ink (id_X11, cval, cval, cval);
196 for (int iy = ny - 1; iy >= 0; iy--) {
197 int iRowPos = ((ny - 1 - iy) * scale) * (nx * scale);
198 for (int ix = 0; ix < nx; ix++) {
199 int cval = static_cast<int>((v[ix][iy] - pmin) * view_scale);
204 for (int sy = 0; sy < scale; sy++)
205 for (int sx = 0; sx < scale; sx++)
206 pPens[iRowPos+(sy * nx * scale)+(sx + (ix * scale))] = grayscale[cval];
210 g2_image (id_X11, 0., 0., nx * scale, ny * scale, pPens);
221 // ImageFile::comparativeStatistics Calculate comparative stats
224 // d Normalized root mean squared distance measure
225 // r Normalized mean absolute distance measure
226 // e Worst case distance measure
229 // G.T. Herman, Image Reconstruction From Projections, 1980
232 ImageFile::comparativeStatistics (const ImageFile& imComp, double& d, double& r, double& e) const
234 if (imComp.nx() != m_nx && imComp.ny() != m_ny) {
235 sys_error (ERR_WARNING, "Image sizes differ [ImageFile::comparativeStatistics]");
238 ImageFileArrayConst v = getArray();
239 if (v == NULL || m_nx == 0 || m_ny == 0)
242 ImageFileArrayConst vComp = imComp.getArray();
245 for (unsigned int ix = 0; ix < m_nx; ix++) {
246 for (unsigned int iy = 0; iy < m_ny; iy++) {
250 myMean /= (m_nx * m_ny);
252 double sqErrorSum = 0.;
253 double absErrorSum = 0.;
254 double sqDiffFromMeanSum = 0.;
255 double absValueSum = 0.;
256 for (unsigned int ix2 = 0; ix2 < m_nx; ix2++) {
257 for (unsigned int iy = 0; iy < m_ny; iy++) {
258 double diff = v[ix2][iy] - vComp[ix2][iy];
259 sqErrorSum += diff * diff;
260 absErrorSum += fabs(diff);
261 double diffFromMean = v[ix2][iy] - myMean;
262 sqDiffFromMeanSum += diffFromMean * diffFromMean;
263 absValueSum += fabs(v[ix2][iy]);
267 d = ::sqrt (sqErrorSum / sqDiffFromMeanSum);
268 r = absErrorSum / absValueSum;
273 for (int ix3 = 0; ix3 < hx; ix3++) {
274 for (int iy = 0; iy < hy; iy++) {
275 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]);
276 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]);
277 double error = fabs (avgPixel - avgPixelComp);
290 ImageFile::printComparativeStatistics (const ImageFile& imComp, std::ostream& os) const
294 if (comparativeStatistics (imComp, d, r, e)) {
295 os << " Normalized root mean squared distance (d): " << d << std::endl;
296 os << " Normalized mean absolute distance (r): " << r << std::endl;
297 os << "Worst case distance (2x2 pixel average) (e): " << e << std::endl;
305 ImageFile::printStatistics (std::ostream& os) const
307 double min, max, mean, mode, median, stddev;
309 statistics (min, max, mean, mode, median, stddev);
311 os << "Real Component Statistics" << std::endl;
313 os << " min: " << min << std::endl;
314 os << " max: " << max << std::endl;
315 os << " mean: " << mean << std::endl;
316 os << " mode: " << mode << std::endl;
317 os << "median: " << median << std::endl;
318 os << "stddev: " << stddev << std::endl;
321 statistics (getImaginaryArray(), min, max, mean, mode, median, stddev);
322 os << std::endl << "Imaginary Component Statistics" << std::endl;
323 os << " min: " << min << std::endl;
324 os << " max: " << max << std::endl;
325 os << " mean: " << mean << std::endl;
326 os << " mode: " << mode << std::endl;
327 os << "median: " << median << std::endl;
328 os << "stddev: " << stddev << std::endl;
334 ImageFile::statistics (double& min, double& max, double& mean, double& mode, double& median, double& stddev) const
336 ImageFileArrayConst v = getArray();
337 statistics (v, min, max, mean, mode, median, stddev);
342 ImageFile::statistics (ImageFileArrayConst v, double& min, double& max, double& mean, double& mode, double& median, double& stddev) const
347 if (v == NULL || nx == 0 || ny == 0)
350 std::vector<double> vecImage;
352 vecImage.resize (nx * ny);
353 for (int ix = 0; ix < nx; ix++) {
354 for (int iy = 0; iy < ny; iy++)
355 vecImage[iVec++] = v[ix][iy];
358 vectorNumericStatistics (vecImage, nx * ny, min, max, mean, mode, median, stddev);
362 ImageFile::getMinMax (double& min, double& max) const
366 ImageFileArrayConst v = getArray();
368 if (v == NULL || nx == 0 || ny == 0)
373 for (int ix = 0; ix < nx; ix++) {
374 for (int iy = 0; iy < ny; iy++) {
384 ImageFile::convertRealToComplex ()
386 if (dataType() != Array2dFile::DATA_TYPE_REAL)
389 if (! reallocRealToComplex())
392 ImageFileArray vImag = getImaginaryArray();
393 for (unsigned int ix = 0; ix < m_nx; ix++) {
394 ImageFileColumn vCol = vImag[ix];
395 for (unsigned int iy = 0; iy < m_ny; iy++)
403 ImageFile::convertComplexToReal ()
405 if (dataType() != Array2dFile::DATA_TYPE_COMPLEX)
408 ImageFileArray vReal = getArray();
409 ImageFileArray vImag = getImaginaryArray();
410 for (unsigned int ix = 0; ix < m_nx; ix++) {
411 ImageFileColumn vRealCol = vReal[ix];
412 ImageFileColumn vImagCol = vImag[ix];
413 for (unsigned int iy = 0; iy < m_ny; iy++) {
414 CTSimComplex c (*vRealCol, *vImagCol);
415 *vRealCol++ = std::abs (c);
420 return reallocComplexToReal();
424 ImageFile::subtractImages (const ImageFile& rRHS, ImageFile& result) const
426 if (m_nx != rRHS.nx() || m_ny != rRHS.ny() || m_nx != result.nx() || m_ny != result.ny()) {
427 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::subtractImage]");
431 if (isComplex() || rRHS.isComplex() && ! result.isComplex())
432 result.convertRealToComplex();
434 ImageFileArrayConst vLHS = getArray();
435 ImageFileArrayConst vLHSImag = getImaginaryArray();
436 ImageFileArrayConst vRHS = rRHS.getArray();
437 ImageFileArrayConst vRHSImag = rRHS.getImaginaryArray();
438 ImageFileArray vResult = result.getArray();
439 ImageFileArray vResultImag = result.getImaginaryArray();
441 for (unsigned int ix = 0; ix < m_nx; ix++) {
442 for (unsigned int iy = 0; iy < m_ny; iy++) {
443 vResult[ix][iy] = vLHS[ix][iy] - vRHS[ix][iy];
444 if (result.isComplex()) {
445 vResultImag[ix][iy] = 0;
447 vResultImag[ix][iy] += vLHSImag[ix][iy];
448 if (rRHS.isComplex())
449 vResultImag[ix][iy] -= vRHSImag[ix][iy];
458 ImageFile::addImages (const ImageFile& rRHS, ImageFile& result) const
460 if (m_nx != rRHS.nx() || m_ny != rRHS.ny() || m_nx != result.nx() || m_ny != result.ny()) {
461 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::subtractImage]");
465 if (isComplex() || rRHS.isComplex() && ! result.isComplex())
466 result.convertRealToComplex();
468 ImageFileArrayConst vLHS = getArray();
469 ImageFileArrayConst vLHSImag = getImaginaryArray();
470 ImageFileArrayConst vRHS = rRHS.getArray();
471 ImageFileArrayConst vRHSImag = rRHS.getImaginaryArray();
472 ImageFileArray vResult = result.getArray();
473 ImageFileArray vResultImag = result.getImaginaryArray();
475 for (unsigned int ix = 0; ix < m_nx; ix++) {
476 for (unsigned int iy = 0; iy < m_ny; iy++) {
477 vResult[ix][iy] = vLHS[ix][iy] + vRHS[ix][iy];
478 if (result.isComplex()) {
479 vResultImag[ix][iy] = 0;
481 vResultImag[ix][iy] += vLHSImag[ix][iy];
482 if (rRHS.isComplex())
483 vResultImag[ix][iy] += vRHSImag[ix][iy];
492 ImageFile::multiplyImages (const ImageFile& rRHS, ImageFile& result) const
494 if (m_nx != rRHS.nx() || m_ny != rRHS.ny() || m_nx != result.nx() || m_ny != result.ny()) {
495 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::subtractImage]");
499 if (isComplex() || rRHS.isComplex() && ! result.isComplex())
500 result.convertRealToComplex();
502 ImageFileArrayConst vLHS = getArray();
503 ImageFileArrayConst vLHSImag = getImaginaryArray();
504 ImageFileArrayConst vRHS = rRHS.getArray();
505 ImageFileArrayConst vRHSImag = rRHS.getImaginaryArray();
506 ImageFileArray vResult = result.getArray();
507 ImageFileArray vResultImag = result.getImaginaryArray();
509 for (unsigned int ix = 0; ix < m_nx; ix++) {
510 for (unsigned int iy = 0; iy < m_ny; iy++) {
511 if (result.isComplex()) {
514 dImag = vLHSImag[ix][iy];
515 std::complex<double> cLHS (vLHS[ix][iy], dImag);
517 if (rRHS.isComplex())
518 dImag = vRHSImag[ix][iy];
519 std::complex<double> cRHS (vRHS[ix][iy], dImag);
520 std::complex<double> cResult = cLHS * cRHS;
521 vResult[ix][iy] = cResult.real();
522 vResultImag[ix][iy] = cResult.imag();
524 vResult[ix][iy] = vLHS[ix][iy] * vRHS[ix][iy];
533 ImageFile::divideImages (const ImageFile& rRHS, ImageFile& result) const
535 if (m_nx != rRHS.nx() || m_ny != rRHS.ny() || m_nx != result.nx() || m_ny != result.ny()) {
536 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::subtractImage]");
540 if (isComplex() || rRHS.isComplex() && ! result.isComplex())
541 result.convertRealToComplex();
543 ImageFileArrayConst vLHS = getArray();
544 ImageFileArrayConst vLHSImag = getImaginaryArray();
545 ImageFileArrayConst vRHS = rRHS.getArray();
546 ImageFileArrayConst vRHSImag = rRHS.getImaginaryArray();
547 ImageFileArray vResult = result.getArray();
548 ImageFileArray vResultImag = result.getImaginaryArray();
550 for (unsigned int ix = 0; ix < m_nx; ix++) {
551 for (unsigned int iy = 0; iy < m_ny; iy++) {
552 if (result.isComplex()) {
555 dImag = vLHSImag[ix][iy];
556 std::complex<double> cLHS (vLHS[ix][iy], dImag);
558 if (rRHS.isComplex())
559 dImag = vRHSImag[ix][iy];
560 std::complex<double> cRHS (vRHS[ix][iy], dImag);
561 std::complex<double> cResult = cLHS / cRHS;
562 vResult[ix][iy] = cResult.real();
563 vResultImag[ix][iy] = cResult.imag();
566 vResult[ix][iy] = vLHS[ix][iy] / vRHS[ix][iy];
578 ImageFile::invertPixelValues (ImageFile& result) const
580 if (m_nx != result.nx() || m_ny != result.ny()) {
581 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
585 if (isComplex() && ! result.isComplex())
586 result.convertRealToComplex();
588 ImageFileArrayConst vLHS = getArray();
589 ImageFileArray vResult = result.getArray();
591 for (unsigned int ix = 0; ix < m_nx; ix++) {
592 ImageFileColumnConst in = vLHS[ix];
593 ImageFileColumn out = vResult[ix];
594 for (unsigned int iy = 0; iy < m_ny; iy++)
602 ImageFile::sqrt (ImageFile& result) const
604 if (m_nx != result.nx() || m_ny != result.ny()) {
605 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
609 if (isComplex() && ! result.isComplex())
610 result.convertRealToComplex();
612 bool bComplexOutput = result.isComplex();
613 ImageFileArrayConst vLHS = getArray();
614 if (! bComplexOutput) // check if should convert to complex output
615 for (unsigned int ix = 0; ix < m_nx; ix++)
616 for (unsigned int iy = 0; iy < m_ny; iy++)
617 if (! bComplexOutput && vLHS[ix][iy] < 0) {
618 result.convertRealToComplex();
619 bComplexOutput = true;
623 ImageFileArrayConst vLHSImag = getImaginaryArray();
624 ImageFileArray vResult = result.getArray();
625 ImageFileArray vResultImag = result.getImaginaryArray();
627 for (unsigned int ix = 0; ix < m_nx; ix++) {
628 for (unsigned int iy = 0; iy < m_ny; iy++) {
629 if (result.isComplex()) {
632 dImag = vLHSImag[ix][iy];
633 std::complex<double> cLHS (vLHS[ix][iy], dImag);
634 std::complex<double> cResult = std::sqrt(cLHS);
635 vResult[ix][iy] = cResult.real();
636 vResultImag[ix][iy] = cResult.imag();
638 vResult[ix][iy] = ::sqrt (vLHS[ix][iy]);
647 ImageFile::log (ImageFile& result) const
649 if (m_nx != result.nx() || m_ny != result.ny()) {
650 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::log]");
654 if (isComplex() && ! result.isComplex())
655 result.convertRealToComplex();
657 ImageFileArrayConst vLHS = getArray();
658 ImageFileArrayConst vLHSImag = getImaginaryArray();
659 ImageFileArray vResult = result.getArray();
660 ImageFileArray vResultImag = result.getImaginaryArray();
662 for (unsigned int ix = 0; ix < m_nx; ix++) {
663 for (unsigned int iy = 0; iy < m_ny; iy++) {
664 if (result.isComplex()) {
667 dImag = vLHSImag[ix][iy];
668 std::complex<double> cLHS (vLHS[ix][iy], dImag);
669 std::complex<double> cResult = std::log (cLHS);
670 vResult[ix][iy] = cResult.real();
671 vResultImag[ix][iy] = cResult.imag();
673 if (vLHS[ix][iy] > 0)
674 vResult[ix][iy] = ::log (vLHS[ix][iy]);
686 ImageFile::exp (ImageFile& result) const
688 if (m_nx != result.nx() || m_ny != result.ny()) {
689 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
693 if (isComplex() && ! result.isComplex())
694 result.convertRealToComplex();
696 ImageFileArrayConst vLHS = getArray();
697 ImageFileArrayConst vLHSImag = getImaginaryArray();
698 ImageFileArray vResult = result.getArray();
699 ImageFileArray vResultImag = result.getImaginaryArray();
701 for (unsigned int ix = 0; ix < m_nx; ix++) {
702 for (unsigned int iy = 0; iy < m_ny; iy++) {
703 if (result.isComplex()) {
706 dImag = vLHSImag[ix][iy];
707 std::complex<double> cLHS (vLHS[ix][iy], dImag);
708 std::complex<double> cResult = std::exp (cLHS);
709 vResult[ix][iy] = cResult.real();
710 vResultImag[ix][iy] = cResult.imag();
712 vResult[ix][iy] = ::exp (vLHS[ix][iy]);
721 ImageFile::scaleImage (ImageFile& result) const
723 unsigned int nx = m_nx;
724 unsigned int ny = m_ny;
725 unsigned int newNX = result.nx();
726 unsigned int newNY = result.ny();
728 double dXScale = static_cast<double>(newNX) / static_cast<double>(nx);
729 double dYScale = static_cast<double>(newNY) / static_cast<double>(ny);
731 if (isComplex() && ! result.isComplex())
732 result.convertRealToComplex();
734 ImageFileArrayConst vReal = getArray();
735 ImageFileArrayConst vImag = getImaginaryArray();
736 ImageFileArray vResult = result.getArray();
737 ImageFileArray vResultImag = result.getImaginaryArray();
739 for (unsigned int ix = 0; ix < newNX; ix++) {
740 for (unsigned int iy = 0; iy < newNY; iy++) {
741 double dXPos = ix / dXScale;
742 double dYPos = iy / dYScale;
743 unsigned int scaleNX = static_cast<unsigned int> (dXPos);
744 unsigned int scaleNY = static_cast<unsigned int> (dYPos);
745 double dXFrac = dXPos - scaleNX;
746 double dYFrac = dYPos - scaleNY;
747 if (scaleNX >= nx - 1 || scaleNY >= ny - 1) {
748 vResult[ix][iy] = vReal[scaleNX][scaleNY];
749 if (result.isComplex()) {
751 vResultImag[ix][iy] = vImag[scaleNX][scaleNY];
753 vResultImag[ix][iy] = 0;
756 vResult[ix][iy] = (1 - dXFrac) * (1 - dYFrac) * vReal[scaleNX][scaleNY] +
757 dXFrac * (1 - dYFrac) * vReal[scaleNX+1][scaleNY] +
758 dYFrac * (1 - dXFrac) * vReal[scaleNX][scaleNY+1] +
759 dXFrac * dYFrac * vReal[scaleNX+1][scaleNY+1];
760 if (result.isComplex()) {
762 vResultImag[ix][iy] = (1 - dXFrac) * (1 - dYFrac) * vImag[scaleNX][scaleNY] +
763 dXFrac * (1 - dYFrac) * vImag[scaleNX+1][scaleNY] +
764 dYFrac * (1 - dXFrac) * vImag[scaleNX][scaleNY+1] +
765 dXFrac * dYFrac * vImag[scaleNX+1][scaleNY+1];
767 vResultImag[ix][iy] = 0;
778 ImageFile::fft (ImageFile& result) const
780 if (m_nx != result.nx() || m_ny != result.ny()) {
781 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
785 if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {
786 if (! result.convertRealToComplex ())
790 fftw_complex* in = new fftw_complex [m_nx * m_ny];
792 ImageFileArrayConst vReal = getArray();
793 ImageFileArrayConst vImag = getImaginaryArray();
796 unsigned int iArray = 0;
797 for (ix = 0; ix < m_nx; ix++)
798 for (iy = 0; iy < m_ny; iy++) {
799 in[iArray].re = vReal[ix][iy];
801 in[iArray].im = vImag[ix][iy];
807 fftwnd_plan plan = fftw2d_create_plan (m_nx, m_ny, FFTW_FORWARD, FFTW_IN_PLACE);
809 fftwnd_one (plan, in, NULL);
811 ImageFileArray vRealResult = result.getArray();
812 ImageFileArray vImagResult = result.getImaginaryArray();
814 unsigned int iScale = m_nx * m_ny;
815 for (ix = 0; ix < m_nx; ix++)
816 for (iy = 0; iy < m_ny; iy++) {
817 vRealResult[ix][iy] = in[iArray].re / iScale;
818 vImagResult[ix][iy] = in[iArray].im / iScale;
822 fftwnd_destroy_plan (plan);
826 Fourier::shuffleFourierToNaturalOrder (result);
833 ImageFile::ifft (ImageFile& result) const
835 if (m_nx != result.nx() || m_ny != result.ny()) {
836 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
840 if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {
841 if (! result.convertRealToComplex ())
845 ImageFileArrayConst vReal = getArray();
846 ImageFileArrayConst vImag = getImaginaryArray();
847 ImageFileArray vRealResult = result.getArray();
848 ImageFileArray vImagResult = result.getImaginaryArray();
850 for (ix = 0; ix < m_nx; ix++)
851 for (iy = 0; iy < m_ny; iy++) {
852 vRealResult[ix][iy] = vReal[ix][iy];
854 vImagResult[ix][iy] = vImag[ix][iy];
856 vImagResult[ix][iy] = 0;
859 Fourier::shuffleNaturalToFourierOrder (result);
861 fftw_complex* in = new fftw_complex [m_nx * m_ny];
863 unsigned int iArray = 0;
864 for (ix = 0; ix < m_nx; ix++)
865 for (iy = 0; iy < m_ny; iy++) {
866 in[iArray].re = vRealResult[ix][iy];
867 in[iArray].im = vImagResult[ix][iy];
871 fftwnd_plan plan = fftw2d_create_plan (m_nx, m_ny, FFTW_BACKWARD, FFTW_IN_PLACE);
873 fftwnd_one (plan, in, NULL);
876 for (ix = 0; ix < m_nx; ix++)
877 for (iy = 0; iy < m_ny; iy++) {
878 vRealResult[ix][iy] = in[iArray].re;
879 vImagResult[ix][iy] = in[iArray].im;
883 fftwnd_destroy_plan (plan);
891 ImageFile::fftRows (ImageFile& result) const
893 if (m_nx != result.nx() || m_ny != result.ny()) {
894 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::fftRows]");
898 if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {
899 if (! result.convertRealToComplex ())
903 fftw_complex* in = new fftw_complex [m_nx];
905 ImageFileArrayConst vReal = getArray();
906 ImageFileArrayConst vImag = getImaginaryArray();
908 fftw_plan plan = fftw_create_plan (m_nx, FFTW_FORWARD, FFTW_IN_PLACE);
909 std::complex<double>* pcRow = new std::complex<double> [m_nx];
912 unsigned int iArray = 0;
913 for (iy = 0; iy < m_ny; iy++) {
914 for (ix = 0; ix < m_nx; ix++) {
915 in[ix].re = vReal[ix][iy];
917 in[ix].im = vImag[ix][iy];
922 fftw_one (plan, in, NULL);
924 for (ix = 0; ix < m_nx; ix++)
925 pcRow[ix] = std::complex<double>(in[ix].re, in[ix].im);
927 Fourier::shuffleFourierToNaturalOrder (pcRow, m_nx);
928 for (ix = 0; ix < m_nx; ix++) {
929 vReal[ix][iy] = pcRow[ix].real();
930 vImag[ix][iy] = pcRow[ix].imag();
935 fftw_destroy_plan (plan);
942 ImageFile::ifftRows (ImageFile& result) const
944 if (m_nx != result.nx() || m_ny != result.ny()) {
945 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::fftRows]");
949 if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {
950 if (! result.convertRealToComplex ())
954 fftw_complex* in = new fftw_complex [m_nx];
956 ImageFileArrayConst vReal = getArray();
957 ImageFileArrayConst vImag = getImaginaryArray();
959 fftw_plan plan = fftw_create_plan (m_nx, FFTW_BACKWARD, FFTW_IN_PLACE);
960 std::complex<double>* pcRow = new std::complex<double> [m_nx];
963 unsigned int iArray = 0;
964 for (iy = 0; iy < m_ny; iy++) {
965 for (ix = 0; ix < m_nx; ix++) {
968 dImag = vImag[ix][iy];
969 pcRow[ix] = std::complex<double> (vReal[ix][iy], dImag);
972 Fourier::shuffleNaturalToFourierOrder (pcRow, m_nx);
974 for (ix = 0; ix < m_nx; ix++) {
975 in[ix].re = pcRow[ix].real();
976 in[ix].im = pcRow[ix].imag();
979 fftw_one (plan, in, NULL);
981 for (ix = 0; ix < m_nx; ix++) {
982 vReal[ix][iy] = in[ix].re;
983 vImag[ix][iy] = in[ix].im;
988 fftw_destroy_plan (plan);
995 ImageFile::fftCols (ImageFile& result) const
1001 ImageFile::ifftCols (ImageFile& result) const
1011 ImageFile::fourier (ImageFile& result) const
1013 if (m_nx != result.nx() || m_ny != result.ny()) {
1014 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
1018 if (! result.isComplex())
1019 if (! result.convertRealToComplex ())
1022 ImageFileArrayConst vLHS = getArray();
1023 ImageFileArrayConst vLHSImag = getImaginaryArray();
1024 ImageFileArray vRealResult = result.getArray();
1025 ImageFileArray vImagResult = result.getImaginaryArray();
1027 unsigned int ix, iy;
1029 // alloc output matrix
1030 CTSimComplex** complexOut = new CTSimComplex* [m_nx];
1031 for (ix = 0; ix < m_nx; ix++)
1032 complexOut[ix] = new CTSimComplex [m_ny];
1034 // fourier each x column
1035 CTSimComplex* pY = new CTSimComplex [m_ny];
1036 for (ix = 0; ix < m_nx; ix++) {
1037 for (iy = 0; iy < m_ny; iy++) {
1040 dImag = vLHSImag[ix][iy];
1041 pY[iy] = std::complex<double>(vLHS[ix][iy], dImag);
1043 ProcessSignal::finiteFourierTransform (pY, complexOut[ix], m_ny, ProcessSignal::FORWARD);
1047 // fourier each y row
1048 CTSimComplex* pX = new CTSimComplex [m_nx];
1049 CTSimComplex* complexOutRow = new CTSimComplex [m_nx];
1050 for (iy = 0; iy < m_ny; iy++) {
1051 for (ix = 0; ix < m_nx; ix++)
1052 pX[ix] = complexOut[ix][iy];
1053 ProcessSignal::finiteFourierTransform (pX, complexOutRow, m_nx, ProcessSignal::FORWARD);
1054 for (ix = 0; ix < m_nx; ix++)
1055 complexOut[ix][iy] = complexOutRow[ix];
1058 delete [] complexOutRow;
1060 for (ix = 0; ix < m_nx; ix++)
1061 for (iy = 0; iy < m_ny; iy++) {
1062 vRealResult[ix][iy] = complexOut[ix][iy].real();
1063 vImagResult[ix][iy] = complexOut[ix][iy].imag();
1066 Fourier::shuffleFourierToNaturalOrder (result);
1068 // delete complexOut matrix
1069 for (ix = 0; ix < m_nx; ix++)
1070 delete [] complexOut[ix];
1071 delete [] complexOut;
1077 ImageFile::inverseFourier (ImageFile& result) const
1079 if (m_nx != result.nx() || m_ny != result.ny()) {
1080 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
1084 if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {
1085 if (! result.convertRealToComplex ())
1089 ImageFileArrayConst vLHSReal = getArray();
1090 ImageFileArrayConst vLHSImag = getImaginaryArray();
1091 ImageFileArray vRealResult = result.getArray();
1092 ImageFileArray vImagResult = result.getImaginaryArray();
1094 unsigned int ix, iy;
1095 // alloc 2d complex output matrix
1096 CTSimComplex** complexOut = new CTSimComplex* [m_nx];
1097 for (ix = 0; ix < m_nx; ix++)
1098 complexOut[ix] = new CTSimComplex [m_ny];
1100 // put input image into result
1101 for (ix = 0; ix < m_nx; ix++)
1102 for (iy = 0; iy < m_ny; iy++) {
1103 vRealResult[ix][iy] = vLHSReal[ix][iy];
1105 vImagResult[ix][iy] = vLHSImag[ix][iy];
1107 vImagResult[ix][iy] = 0;
1110 Fourier::shuffleNaturalToFourierOrder (result);
1112 // ifourier each x column
1113 CTSimComplex* pCol = new CTSimComplex [m_ny];
1114 for (ix = 0; ix < m_nx; ix++) {
1115 for (iy = 0; iy < m_ny; iy++) {
1116 pCol[iy] = std::complex<double> (vRealResult[ix][iy], vImagResult[ix][iy]);
1118 ProcessSignal::finiteFourierTransform (pCol, complexOut[ix], m_ny, ProcessSignal::BACKWARD);
1122 // ifourier each y row
1123 CTSimComplex* complexInRow = new CTSimComplex [m_nx];
1124 CTSimComplex* complexOutRow = new CTSimComplex [m_nx];
1125 for (iy = 0; iy < m_ny; iy++) {
1126 for (ix = 0; ix < m_nx; ix++)
1127 complexInRow[ix] = complexOut[ix][iy];
1128 ProcessSignal::finiteFourierTransform (complexInRow, complexOutRow, m_nx, ProcessSignal::BACKWARD);
1129 for (ix = 0; ix < m_nx; ix++)
1130 complexOut[ix][iy] = complexOutRow[ix];
1132 delete [] complexInRow;
1133 delete [] complexOutRow;
1135 for (ix = 0; ix < m_nx; ix++)
1136 for (iy = 0; iy < m_ny; iy++) {
1137 vRealResult[ix][iy] = complexOut[ix][iy].real();
1138 vImagResult[ix][iy] = complexOut[ix][iy].imag();
1141 // delete complexOut matrix
1142 for (ix = 0; ix < m_nx; ix++)
1143 delete [] complexOut[ix];
1144 delete [] complexOut;
1151 ImageFile::magnitude (ImageFile& result) const
1153 if (m_nx != result.nx() || m_ny != result.ny()) {
1154 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
1158 ImageFileArray vReal = getArray();
1159 ImageFileArray vImag = getImaginaryArray();
1160 ImageFileArray vRealResult = result.getArray();
1162 for (unsigned int ix = 0; ix < m_nx; ix++)
1163 for (unsigned int iy = 0; iy < m_ny; iy++) {
1165 vRealResult[ix][iy] = ::sqrt (vReal[ix][iy] * vReal[ix][iy] + vImag[ix][iy] * vImag[ix][iy]);
1167 vRealResult[ix][iy] = vReal[ix][iy];
1170 if (result.isComplex())
1171 result.convertComplexToReal();
1177 ImageFile::phase (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] = ::atan2 (vImag[ix][iy], vReal[ix][iy]);
1193 vRealResult[ix][iy] = 0;
1196 if (result.isComplex())
1197 result.convertComplexToReal();
1203 ImageFile::square (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 if (isComplex() && ! result.isComplex())
1211 result.convertRealToComplex();
1213 ImageFileArrayConst vLHS = getArray();
1214 ImageFileArrayConst vLHSImag = getImaginaryArray();
1215 ImageFileArray vResult = result.getArray();
1216 ImageFileArray vResultImag = result.getImaginaryArray();
1218 for (unsigned int ix = 0; ix < m_nx; ix++) {
1219 for (unsigned int iy = 0; iy < m_ny; iy++) {
1220 if (result.isComplex()) {
1223 dImag = vLHSImag[ix][iy];
1224 std::complex<double> cLHS (vLHS[ix][iy], dImag);
1225 std::complex<double> cResult = cLHS * cLHS;
1226 vResult[ix][iy] = cResult.real();
1227 vResultImag[ix][iy] = cResult.imag();
1229 vResult[ix][iy] = vLHS[ix][iy] * vLHS[ix][iy];
1238 ImageFile::convertExportFormatNameToID (const char* const formatName)
1240 int formatID = EXPORT_FORMAT_INVALID;
1242 for (int i = 0; i < s_iExportFormatCount; i++)
1243 if (strcasecmp (formatName, s_aszExportFormatName[i]) == 0) {
1252 ImageFile::convertExportFormatIDToName (int formatID)
1254 static const char *formatName = "";
1256 if (formatID >= 0 && formatID < s_iExportFormatCount)
1257 return (s_aszExportFormatName[formatID]);
1259 return (formatName);
1263 ImageFile::convertExportFormatIDToTitle (const int formatID)
1265 static const char *formatTitle = "";
1267 if (formatID >= 0 && formatID < s_iExportFormatCount)
1268 return (s_aszExportFormatTitle[formatID]);
1270 return (formatTitle);
1274 ImageFile::convertImportFormatNameToID (const char* const formatName)
1276 int formatID = IMPORT_FORMAT_INVALID;
1278 for (int i = 0; i < s_iImportFormatCount; i++)
1279 if (strcasecmp (formatName, s_aszImportFormatName[i]) == 0) {
1288 ImageFile::convertImportFormatIDToName (int formatID)
1290 static const char *formatName = "";
1292 if (formatID >= 0 && formatID < s_iImportFormatCount)
1293 return (s_aszImportFormatName[formatID]);
1295 return (formatName);
1299 ImageFile::convertImportFormatIDToTitle (const int formatID)
1301 static const char *formatTitle = "";
1303 if (formatID >= 0 && formatID < s_iImportFormatCount)
1304 return (s_aszImportFormatTitle[formatID]);
1306 return (formatTitle);
1310 ImageFile::importImage (const char* const pszFormat, const char* const pszFilename)
1312 int iFormatID = convertImportFormatNameToID (pszFormat);
1314 if (iFormatID == IMPORT_FORMAT_PPM)
1315 return readImagePPM (pszFilename);
1317 else if (iFormatID == IMPORT_FORMAT_PNG)
1318 return readImagePNG (pszFilename);
1321 sys_error (ERR_SEVERE, "Invalid format %s [ImageFile::importImage]", pszFormat);
1326 ImageFile::skipSpacePPM (FILE* fp)
1329 while (isspace (c) || c == '#') {
1330 if (c == '#') { // comment until end of line
1332 while (c != 13 && c != 10)
1343 ImageFile::readImagePPM (const char* const pszFile)
1345 FILE* fp = fopen (pszFile, "r");
1346 if ((fp = fopen (pszFile, "r")) == NULL)
1348 char cSignature = toupper(fgetc(fp));
1349 if (cSignature != 'P') {
1353 cSignature = fgetc(fp);
1354 if (cSignature == '5' || cSignature == '6') { // binary modes
1356 fp = fopen(pszFile, "rb"); // reopen in binary mode
1359 } else if (cSignature != '2' && cSignature != '3') {
1364 int nRows, nCols, iMaxValue;
1366 if (fscanf (fp, "%d", &nCols) != 1) {
1371 if (fscanf (fp, "%d", &nRows) != 1) {
1376 if (fscanf (fp, "%d", &iMaxValue) != 1) {
1380 setArraySize (nRows, nCols);
1382 if (cSignature == '5' || cSignature == '6') { // binary modes
1386 if (c != 10) // read msdos 13-10 newline
1390 skipSpacePPM (fp); // ascii may have comments
1392 double dMaxValue = iMaxValue;
1393 ImageFileArray v = getArray();
1394 for (int iy = nRows - 1; iy >= 0; iy--) {
1395 for (int ix = 0; ix < nCols; ix++) {
1396 int iGS, iR, iG, iB;
1398 switch (cSignature) {
1400 if (fscanf(fp, "%d ", &iGS) != 1) {
1404 v[ix][iy] = iGS / dMaxValue;
1412 v[ix][iy] = iGS / dMaxValue;
1415 if (fscanf (fp, "%d %d %d ", &iR, &iG, &iB) != 3) {
1419 dR = iR / dMaxValue;
1420 dG = iG / dMaxValue;
1421 dB = iB / dMaxValue;
1422 v[ix][iy] = colorToGrayscale (dR, dG, dB);
1432 dR = iR / dMaxValue;
1433 dG = iG / dMaxValue;
1434 dB = iB / dMaxValue;
1435 v[ix][iy] = colorToGrayscale (dR, dG, dB);
1447 ImageFile::readImagePNG (const char* const pszFile)
1449 FILE* fp = fopen(pszFile, "rb");
1452 unsigned char header[8];
1453 fread (header, 1, 8, fp);
1454 if (png_sig_cmp (header, 0, 8)) {
1459 png_structp png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
1465 png_infop info_ptr = png_create_info_struct(png_ptr);
1467 png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
1472 png_infop end_info = png_create_info_struct(png_ptr);
1474 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
1479 if (setjmp(png_ptr->jmpbuf)) {
1480 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
1485 png_init_io(png_ptr, fp);
1486 png_set_sig_bytes(png_ptr, 8);
1487 png_read_info(png_ptr, info_ptr);
1489 int width = png_get_image_width (png_ptr, info_ptr);
1490 int height = png_get_image_height (png_ptr, info_ptr);
1491 int bit_depth = png_get_bit_depth (png_ptr, info_ptr);
1492 int color_type = png_get_color_type (png_ptr, info_ptr);
1494 if (color_type == PNG_COLOR_TYPE_PALETTE && bit_depth <= 8)
1495 png_set_expand(png_ptr);
1497 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
1498 png_set_expand(png_ptr);
1501 png_set_packing(png_ptr);
1503 if (color_type & PNG_COLOR_MASK_ALPHA)
1504 png_set_strip_alpha(png_ptr);
1506 if (bit_depth == 16)
1507 png_set_swap(png_ptr); // convert to little-endian format
1509 png_read_update_info(png_ptr, info_ptr); // update with transformations
1510 int rowbytes = png_get_rowbytes (png_ptr, info_ptr);
1511 bit_depth = png_get_bit_depth (png_ptr, info_ptr);
1512 color_type = png_get_color_type (png_ptr, info_ptr);
1514 png_bytep* row_pointers = new png_bytep [height];
1516 for (i = 0; i < height; i++)
1517 row_pointers[i] = new unsigned char [rowbytes];
1519 png_read_image(png_ptr, row_pointers);
1521 setArraySize (width, height);
1522 ImageFileArray v = getArray();
1523 for (int iy = 0; iy < height; iy++) {
1524 for (int ix = 0; ix < width; ix++) {
1526 if (color_type == PNG_COLOR_TYPE_GRAY) {
1528 dV = row_pointers[iy][ix] / 255.;
1529 else if (bit_depth == 16) {
1531 dV = (row_pointers[iy][iBase] + (row_pointers[iy][iBase+1] << 8)) / 65536.;
1533 } else if (color_type == PNG_COLOR_TYPE_RGB) {
1534 if (bit_depth == 8) {
1536 double dR = row_pointers[iy][iBase] / 255.;
1537 double dG = row_pointers[iy][iBase+1] / 255.;
1538 double dB = row_pointers[iy][iBase+2] / 255.;
1539 dV = colorToGrayscale (dR, dG, dR);
1542 v[ix][height-iy-1] = dV;
1546 png_read_end(png_ptr, end_info);
1547 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
1549 for (i = 0; i < height; i++)
1550 delete row_pointers[i];
1551 delete row_pointers;
1559 ImageFile::exportImage (const char* const pszFormat, const char* const pszFilename, int nxcell, int nycell, double densmin, double densmax)
1561 int iFormatID = convertExportFormatNameToID (pszFormat);
1563 if (iFormatID == EXPORT_FORMAT_PGM)
1564 return writeImagePGM (pszFilename, nxcell, nycell, densmin, densmax);
1565 else if (iFormatID == EXPORT_FORMAT_PGMASCII)
1566 return writeImagePGMASCII (pszFilename, nxcell, nycell, densmin, densmax);
1567 else if (iFormatID == EXPORT_FORMAT_PNG)
1568 return writeImagePNG (pszFilename, 8, nxcell, nycell, densmin, densmax);
1569 else if (iFormatID == EXPORT_FORMAT_PNG16)
1570 return writeImagePNG (pszFilename, 16, nxcell, nycell, densmin, densmax);
1571 #ifdef HAVE_CTN_DICOM
1572 else if (iFormatID == EXPORT_FORMAT_DICOM) {
1573 DicomExporter dicomExport (this);
1574 bool bSuccess = dicomExport.writeFile (pszFilename);
1576 sys_error (ERR_SEVERE, dicomExport.failMessage().c_str());
1581 sys_error (ERR_SEVERE, "Invalid format %s [ImageFile::exportImage]", pszFormat);
1587 ImageFile::writeImagePGM (const char* const outfile, int nxcell, int nycell, double densmin, double densmax)
1592 ImageFileArray v = getArray();
1594 unsigned char* rowp = new unsigned char [nx * nxcell];
1596 if ((fp = fopen (outfile, "wb")) == NULL)
1599 fprintf(fp, "P5\n");
1600 fprintf(fp, "%d %d\n", nx, ny);
1601 fprintf(fp, "255\n");
1603 for (int irow = ny - 1; irow >= 0; irow--) {
1604 for (int icol = 0; icol < nx; icol++) {
1605 int pos = icol * nxcell;
1606 double dens = (v[icol][irow] - densmin) / (densmax - densmin);
1607 dens = clamp (dens, 0., 1.);
1608 for (int p = pos; p < pos + nxcell; p++) {
1609 rowp[p] = static_cast<unsigned int> (dens * 255.);
1612 for (int ir = 0; ir < nycell; ir++) {
1613 for (int ic = 0; ic < nx * nxcell; ic++)
1614 fputc( rowp[ic], fp );
1625 ImageFile::writeImagePGMASCII (const char* const outfile, int nxcell, int nycell, double densmin, double densmax)
1630 ImageFileArray v = getArray();
1632 unsigned char* rowp = new unsigned char [nx * nxcell];
1634 if ((fp = fopen (outfile, "wb")) == NULL)
1637 fprintf(fp, "P2\n");
1638 fprintf(fp, "%d %d\n", nx, ny);
1639 fprintf(fp, "255\n");
1641 for (int irow = ny - 1; irow >= 0; irow--) {
1642 for (int icol = 0; icol < nx; icol++) {
1643 int pos = icol * nxcell;
1644 double dens = (v[icol][irow] - densmin) / (densmax - densmin);
1645 dens = clamp (dens, 0., 1.);
1646 for (int p = pos; p < pos + nxcell; p++) {
1647 rowp[p] = static_cast<unsigned int> (dens * 255.);
1650 for (int ir = 0; ir < nycell; ir++) {
1651 for (int ic = 0; ic < nx * nxcell; ic++)
1652 fprintf(fp, "%d ", rowp[ic]);
1666 ImageFile::writeImagePNG (const char* const outfile, int bitdepth, int nxcell, int nycell, double densmin, double densmax)
1668 double max_out_level = (1 << bitdepth) - 1;
1671 ImageFileArray v = getArray();
1673 unsigned char* rowp = new unsigned char [nx * nxcell * (bitdepth / 8)];
1675 FILE *fp = fopen (outfile, "wb");
1679 png_structp png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
1683 png_infop info_ptr = png_create_info_struct (png_ptr);
1685 png_destroy_write_struct (&png_ptr, (png_infopp) NULL);
1690 if (setjmp (png_ptr->jmpbuf)) {
1691 png_destroy_write_struct (&png_ptr, &info_ptr);
1696 png_init_io(png_ptr, fp);
1698 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);
1700 png_write_info(png_ptr, info_ptr);
1701 for (int irow = ny - 1; irow >= 0; irow--) {
1702 png_bytep row_pointer = rowp;
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 unsigned int outval = static_cast<unsigned int> (dens * max_out_level);
1710 for (int p = pos; p < pos + nxcell; p++) {
1715 rowp[rowpos+1] = (outval >> 8) & 0xFF;
1716 rowp[rowpos] = (outval & 0xFF);
1720 for (int ir = 0; ir < nycell; ir++)
1721 png_write_rows (png_ptr, &row_pointer, 1);
1724 png_write_end (png_ptr, info_ptr);
1725 png_destroy_write_struct (&png_ptr, &info_ptr);
1736 static const int N_GRAYSCALE=256;
1739 ImageFile::writeImageGIF (const char* const outfile, int nxcell, int nycell, double densmin, double densmax)
1741 int gs_indices[N_GRAYSCALE];
1744 ImageFileArray v = getArray();
1746 unsigned char* rowp = new unsigned char [nx * nxcell];
1748 gdImagePtr gif = gdImageCreate(nx * nxcell, ny * nycell);
1749 for (int i = 0; i < N_GRAYSCALE; i++)
1750 gs_indices[i] = gdImageColorAllocate(gif, i, i, i);
1752 int lastrow = ny * nycell - 1;
1753 for (int irow = 0; irow < ny; irow++) {
1754 int rpos = irow * nycell;
1755 for (int ir = rpos; ir < rpos + nycell; ir++) {
1756 for (int icol = 0; icol < nx; icol++) {
1757 int cpos = icol * nxcell;
1758 double dens = (v[icol][irow] - densmin) / (densmax - densmin);
1759 dens = clamp(dens, 0., 1.);
1760 for (int ic = cpos; ic < cpos + nxcell; ic++) {
1761 rowp[ic] = (unsigned int) (dens * (double) (N_GRAYSCALE - 1));
1762 gdImageSetPixel(gif, ic, lastrow - ir, gs_indices[rowp[ic]]);
1769 if ((out = fopen (outfile,"w")) == NULL) {
1770 sys_error(ERR_SEVERE, "Error opening output file %s for writing", outfile);
1773 gdImageGif(gif,out);
1775 gdImageDestroy(gif);