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.39 2001/03/02 02:08:14 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 ******************************************************************************/
30 const double ImageFile::s_dRedGrayscaleFactor = 0.299;
31 const double ImageFile::s_dGreenGrayscaleFactor = 0.587;
32 const double ImageFile::s_dBlueGrayscaleFactor = 0.114;
35 const int ImageFile::EXPORT_FORMAT_INVALID = -1;
36 const int ImageFile::EXPORT_FORMAT_PGM = 0;
37 const int ImageFile::EXPORT_FORMAT_PGMASCII = 1;
39 const int ImageFile::EXPORT_FORMAT_PNG = 2;
40 const int ImageFile::EXPORT_FORMAT_PNG16 = 3;
43 const int ImageFile::EXPORT_FORMAT_DICOM = 4;
46 const char* ImageFile::s_aszExportFormatName[] =
59 const char* ImageFile::s_aszExportFormatTitle[] =
69 const int ImageFile::s_iExportFormatCount = sizeof(s_aszExportFormatName) / sizeof(const char*);
72 const int ImageFile::IMPORT_FORMAT_INVALID = -1;
73 const int ImageFile::IMPORT_FORMAT_PPM = 0;
75 const int ImageFile::IMPORT_FORMAT_PNG = 1;
78 const int ImageFile::IMPORT_FORMAT_DICOM = 2;
82 const char* ImageFile::s_aszImportFormatName[] =
93 const char* ImageFile::s_aszImportFormatTitle[] =
101 const int ImageFile::s_iImportFormatCount = sizeof(s_aszImportFormatName) / sizeof(const char*);
105 F32Image::F32Image (int nx, int ny, int dataType)
106 : Array2dFile (nx, ny, sizeof(kfloat32), Array2dFile::PIXEL_FLOAT32, dataType)
110 F32Image::F32Image (void)
113 setPixelFormat (Array2dFile::PIXEL_FLOAT32);
114 setPixelSize (sizeof(kfloat32));
115 setDataType (Array2dFile::DATA_TYPE_REAL);
118 F64Image::F64Image (int nx, int ny, int dataType)
119 : Array2dFile (nx, ny, sizeof(kfloat64), Array2dFile::PIXEL_FLOAT64, dataType)
123 F64Image::F64Image (void)
126 setPixelFormat (PIXEL_FLOAT64);
127 setPixelSize (sizeof(kfloat64));
128 setDataType (Array2dFile::DATA_TYPE_REAL);
132 ImageFile::getCenterCoordinates (unsigned int& iXCenter, unsigned int& iYCenter)
137 iXCenter = (m_nx - 1) / 2;
142 iYCenter = (m_ny - 1) / 2;
147 ImageFile::filterResponse (const char* const domainName, double bw, const char* const filterName, double filt_param, double dInputScale, double dOutputScale)
149 ImageFileArray v = getArray();
150 SignalFilter filter (filterName, domainName, bw, filt_param);
152 unsigned int iXCenter, iYCenter;
153 getCenterCoordinates (iXCenter, iYCenter);
155 for (unsigned int ix = 0; ix < m_nx; ix++)
156 for (unsigned int iy = 0; iy < m_ny; iy++) {
157 long lD2 = ((ix - iXCenter) * (ix - iXCenter)) + ((iy - iYCenter) * (iy - iYCenter));
158 double r = ::sqrt (static_cast<double>(lD2)) * dInputScale;
159 v[ix][iy] = filter.response (r) * dOutputScale;
164 ImageFile::display (void) const
168 getMinMax (pmin, pmax);
170 return (displayScaling (1, pmin, pmax));
174 ImageFile::displayScaling (const int scale, const ImageFileValue pmin, const ImageFileValue pmax) const
178 ImageFileArrayConst v = getArray();
179 if (v == NULL || nx == 0 || ny == 0)
183 int* pPens = new int [nx * ny * scale * scale ];
185 double view_scale = 255 / (pmax - pmin);
186 int id_X11 = g2_open_X11 (nx * scale, ny * scale);
188 for (int i = 0; i < 256; i++) {
189 double cval = i / 255.;
190 grayscale[i] = g2_ink (id_X11, cval, cval, cval);
193 for (int iy = ny - 1; iy >= 0; iy--) {
194 int iRowPos = ((ny - 1 - iy) * scale) * (nx * scale);
195 for (int ix = 0; ix < nx; ix++) {
196 int cval = static_cast<int>((v[ix][iy] - pmin) * view_scale);
201 for (int sy = 0; sy < scale; sy++)
202 for (int sx = 0; sx < scale; sx++)
203 pPens[iRowPos+(sy * nx * scale)+(sx + (ix * scale))] = grayscale[cval];
207 g2_image (id_X11, 0., 0., nx * scale, ny * scale, pPens);
218 // ImageFile::comparativeStatistics Calculate comparative stats
221 // d Normalized root mean squared distance measure
222 // r Normalized mean absolute distance measure
223 // e Worst case distance measure
226 // G.T. Herman, Image Reconstruction From Projections, 1980
229 ImageFile::comparativeStatistics (const ImageFile& imComp, double& d, double& r, double& e) const
231 if (imComp.nx() != m_nx && imComp.ny() != m_ny) {
232 sys_error (ERR_WARNING, "Image sizes differ [ImageFile::comparativeStatistics]");
235 ImageFileArrayConst v = getArray();
236 if (v == NULL || m_nx == 0 || m_ny == 0)
239 ImageFileArrayConst vComp = imComp.getArray();
242 for (unsigned int ix = 0; ix < m_nx; ix++) {
243 for (unsigned int iy = 0; iy < m_ny; iy++) {
247 myMean /= (m_nx * m_ny);
249 double sqErrorSum = 0.;
250 double absErrorSum = 0.;
251 double sqDiffFromMeanSum = 0.;
252 double absValueSum = 0.;
253 for (unsigned int ix2 = 0; ix2 < m_nx; ix2++) {
254 for (unsigned int iy = 0; iy < m_ny; iy++) {
255 double diff = v[ix2][iy] - vComp[ix2][iy];
256 sqErrorSum += diff * diff;
257 absErrorSum += fabs(diff);
258 double diffFromMean = v[ix2][iy] - myMean;
259 sqDiffFromMeanSum += diffFromMean * diffFromMean;
260 absValueSum += fabs(v[ix2][iy]);
264 d = ::sqrt (sqErrorSum / sqDiffFromMeanSum);
265 r = absErrorSum / absValueSum;
270 for (int ix3 = 0; ix3 < hx; ix3++) {
271 for (int iy = 0; iy < hy; iy++) {
272 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]);
273 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]);
274 double error = fabs (avgPixel - avgPixelComp);
287 ImageFile::printComparativeStatistics (const ImageFile& imComp, std::ostream& os) const
291 if (comparativeStatistics (imComp, d, r, e)) {
292 os << " Normalized root mean squared distance (d): " << d << std::endl;
293 os << " Normalized mean absolute distance (r): " << r << std::endl;
294 os << "Worst case distance (2x2 pixel average) (e): " << e << std::endl;
302 ImageFile::printStatistics (std::ostream& os) const
304 double min, max, mean, mode, median, stddev;
306 statistics (min, max, mean, mode, median, stddev);
308 os << "Real Component Statistics" << std::endl;
310 os << " min: " << min << std::endl;
311 os << " max: " << max << std::endl;
312 os << " mean: " << mean << std::endl;
313 os << " mode: " << mode << std::endl;
314 os << "median: " << median << std::endl;
315 os << "stddev: " << stddev << std::endl;
318 statistics (getImaginaryArray(), min, max, mean, mode, median, stddev);
319 os << std::endl << "Imaginary Component Statistics" << std::endl;
320 os << " min: " << min << std::endl;
321 os << " max: " << max << std::endl;
322 os << " mean: " << mean << std::endl;
323 os << " mode: " << mode << std::endl;
324 os << "median: " << median << std::endl;
325 os << "stddev: " << stddev << std::endl;
331 ImageFile::statistics (double& min, double& max, double& mean, double& mode, double& median, double& stddev) const
333 ImageFileArrayConst v = getArray();
334 statistics (v, min, max, mean, mode, median, stddev);
339 ImageFile::statistics (ImageFileArrayConst v, double& min, double& max, double& mean, double& mode, double& median, double& stddev) const
344 if (v == NULL || nx == 0 || ny == 0)
347 std::vector<double> vecImage;
349 vecImage.resize (nx * ny);
350 for (int ix = 0; ix < nx; ix++) {
351 for (int iy = 0; iy < ny; iy++)
352 vecImage[iVec++] = v[ix][iy];
355 vectorNumericStatistics (vecImage, nx * ny, min, max, mean, mode, median, stddev);
359 ImageFile::getMinMax (double& min, double& max) const
363 ImageFileArrayConst v = getArray();
365 if (v == NULL || nx == 0 || ny == 0)
370 for (int ix = 0; ix < nx; ix++) {
371 for (int iy = 0; iy < ny; iy++) {
381 ImageFile::convertRealToComplex ()
383 if (dataType() != Array2dFile::DATA_TYPE_REAL)
386 if (! reallocRealToComplex())
389 ImageFileArray vImag = getImaginaryArray();
390 for (unsigned int ix = 0; ix < m_nx; ix++) {
391 ImageFileColumn vCol = vImag[ix];
392 for (unsigned int iy = 0; iy < m_ny; iy++)
400 ImageFile::convertComplexToReal ()
402 if (dataType() != Array2dFile::DATA_TYPE_COMPLEX)
405 ImageFileArray vReal = getArray();
406 ImageFileArray vImag = getImaginaryArray();
407 for (unsigned int ix = 0; ix < m_nx; ix++) {
408 ImageFileColumn vRealCol = vReal[ix];
409 ImageFileColumn vImagCol = vImag[ix];
410 for (unsigned int iy = 0; iy < m_ny; iy++) {
411 CTSimComplex c (*vRealCol, *vImagCol);
412 *vRealCol++ = std::abs (c);
417 return reallocComplexToReal();
421 ImageFile::subtractImages (const ImageFile& rRHS, ImageFile& result) const
423 if (m_nx != rRHS.nx() || m_ny != rRHS.ny() || m_nx != result.nx() || m_ny != result.ny()) {
424 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::subtractImage]");
428 if (isComplex() || rRHS.isComplex() && ! result.isComplex())
429 result.convertRealToComplex();
431 ImageFileArrayConst vLHS = getArray();
432 ImageFileArrayConst vLHSImag = getImaginaryArray();
433 ImageFileArrayConst vRHS = rRHS.getArray();
434 ImageFileArrayConst vRHSImag = rRHS.getImaginaryArray();
435 ImageFileArray vResult = result.getArray();
436 ImageFileArray vResultImag = result.getImaginaryArray();
438 for (unsigned int ix = 0; ix < m_nx; ix++) {
439 for (unsigned int iy = 0; iy < m_ny; iy++) {
440 vResult[ix][iy] = vLHS[ix][iy] - vRHS[ix][iy];
441 if (result.isComplex()) {
442 vResultImag[ix][iy] = 0;
444 vResultImag[ix][iy] += vLHSImag[ix][iy];
445 if (rRHS.isComplex())
446 vResultImag[ix][iy] -= vRHSImag[ix][iy];
455 ImageFile::addImages (const ImageFile& rRHS, ImageFile& result) const
457 if (m_nx != rRHS.nx() || m_ny != rRHS.ny() || m_nx != result.nx() || m_ny != result.ny()) {
458 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::subtractImage]");
462 if (isComplex() || rRHS.isComplex() && ! result.isComplex())
463 result.convertRealToComplex();
465 ImageFileArrayConst vLHS = getArray();
466 ImageFileArrayConst vLHSImag = getImaginaryArray();
467 ImageFileArrayConst vRHS = rRHS.getArray();
468 ImageFileArrayConst vRHSImag = rRHS.getImaginaryArray();
469 ImageFileArray vResult = result.getArray();
470 ImageFileArray vResultImag = result.getImaginaryArray();
472 for (unsigned int ix = 0; ix < m_nx; ix++) {
473 for (unsigned int iy = 0; iy < m_ny; iy++) {
474 vResult[ix][iy] = vLHS[ix][iy] + vRHS[ix][iy];
475 if (result.isComplex()) {
476 vResultImag[ix][iy] = 0;
478 vResultImag[ix][iy] += vLHSImag[ix][iy];
479 if (rRHS.isComplex())
480 vResultImag[ix][iy] += vRHSImag[ix][iy];
489 ImageFile::multiplyImages (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();
521 vResult[ix][iy] = vLHS[ix][iy] * vRHS[ix][iy];
530 ImageFile::divideImages (const ImageFile& rRHS, ImageFile& result) const
532 if (m_nx != rRHS.nx() || m_ny != rRHS.ny() || m_nx != result.nx() || m_ny != result.ny()) {
533 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::subtractImage]");
537 if (isComplex() || rRHS.isComplex() && ! result.isComplex())
538 result.convertRealToComplex();
540 ImageFileArrayConst vLHS = getArray();
541 ImageFileArrayConst vLHSImag = getImaginaryArray();
542 ImageFileArrayConst vRHS = rRHS.getArray();
543 ImageFileArrayConst vRHSImag = rRHS.getImaginaryArray();
544 ImageFileArray vResult = result.getArray();
545 ImageFileArray vResultImag = result.getImaginaryArray();
547 for (unsigned int ix = 0; ix < m_nx; ix++) {
548 for (unsigned int iy = 0; iy < m_ny; iy++) {
549 if (result.isComplex()) {
552 dImag = vLHSImag[ix][iy];
553 std::complex<double> cLHS (vLHS[ix][iy], dImag);
555 if (rRHS.isComplex())
556 dImag = vRHSImag[ix][iy];
557 std::complex<double> cRHS (vRHS[ix][iy], dImag);
558 std::complex<double> cResult = cLHS / cRHS;
559 vResult[ix][iy] = cResult.real();
560 vResultImag[ix][iy] = cResult.imag();
563 vResult[ix][iy] = vLHS[ix][iy] / vRHS[ix][iy];
575 ImageFile::invertPixelValues (ImageFile& result) const
577 if (m_nx != result.nx() || m_ny != result.ny()) {
578 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
582 if (isComplex() && ! result.isComplex())
583 result.convertRealToComplex();
585 ImageFileArrayConst vLHS = getArray();
586 ImageFileArray vResult = result.getArray();
588 for (unsigned int ix = 0; ix < m_nx; ix++) {
589 ImageFileColumnConst in = vLHS[ix];
590 ImageFileColumn out = vResult[ix];
591 for (unsigned int iy = 0; iy < m_ny; iy++)
599 ImageFile::sqrt (ImageFile& result) const
601 if (m_nx != result.nx() || m_ny != result.ny()) {
602 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
606 if (isComplex() && ! result.isComplex())
607 result.convertRealToComplex();
609 bool bComplexOutput = result.isComplex();
610 ImageFileArrayConst vLHS = getArray();
611 if (! bComplexOutput) // check if should convert to complex output
612 for (unsigned int ix = 0; ix < m_nx; ix++)
613 for (unsigned int iy = 0; iy < m_ny; iy++)
614 if (! bComplexOutput && vLHS[ix][iy] < 0) {
615 result.convertRealToComplex();
616 bComplexOutput = true;
620 ImageFileArrayConst vLHSImag = getImaginaryArray();
621 ImageFileArray vResult = result.getArray();
622 ImageFileArray vResultImag = result.getImaginaryArray();
624 for (unsigned int ix = 0; ix < m_nx; ix++) {
625 for (unsigned int iy = 0; iy < m_ny; iy++) {
626 if (result.isComplex()) {
629 dImag = vLHSImag[ix][iy];
630 std::complex<double> cLHS (vLHS[ix][iy], dImag);
631 std::complex<double> cResult = std::sqrt(cLHS);
632 vResult[ix][iy] = cResult.real();
633 vResultImag[ix][iy] = cResult.imag();
635 vResult[ix][iy] = ::sqrt (vLHS[ix][iy]);
644 ImageFile::log (ImageFile& result) const
646 if (m_nx != result.nx() || m_ny != result.ny()) {
647 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::log]");
651 if (isComplex() && ! result.isComplex())
652 result.convertRealToComplex();
654 ImageFileArrayConst vLHS = getArray();
655 ImageFileArrayConst vLHSImag = getImaginaryArray();
656 ImageFileArray vResult = result.getArray();
657 ImageFileArray vResultImag = result.getImaginaryArray();
659 for (unsigned int ix = 0; ix < m_nx; ix++) {
660 for (unsigned int iy = 0; iy < m_ny; iy++) {
661 if (result.isComplex()) {
664 dImag = vLHSImag[ix][iy];
665 std::complex<double> cLHS (vLHS[ix][iy], dImag);
666 std::complex<double> cResult = std::log (cLHS);
667 vResult[ix][iy] = cResult.real();
668 vResultImag[ix][iy] = cResult.imag();
670 if (vLHS[ix][iy] > 0)
671 vResult[ix][iy] = ::log (vLHS[ix][iy]);
683 ImageFile::exp (ImageFile& result) const
685 if (m_nx != result.nx() || m_ny != result.ny()) {
686 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
690 if (isComplex() && ! result.isComplex())
691 result.convertRealToComplex();
693 ImageFileArrayConst vLHS = getArray();
694 ImageFileArrayConst vLHSImag = getImaginaryArray();
695 ImageFileArray vResult = result.getArray();
696 ImageFileArray vResultImag = result.getImaginaryArray();
698 for (unsigned int ix = 0; ix < m_nx; ix++) {
699 for (unsigned int iy = 0; iy < m_ny; iy++) {
700 if (result.isComplex()) {
703 dImag = vLHSImag[ix][iy];
704 std::complex<double> cLHS (vLHS[ix][iy], dImag);
705 std::complex<double> cResult = std::exp (cLHS);
706 vResult[ix][iy] = cResult.real();
707 vResultImag[ix][iy] = cResult.imag();
709 vResult[ix][iy] = ::exp (vLHS[ix][iy]);
718 ImageFile::scaleImage (ImageFile& result) const
720 unsigned int nx = m_nx;
721 unsigned int ny = m_ny;
722 unsigned int newNX = result.nx();
723 unsigned int newNY = result.ny();
725 double dXScale = static_cast<double>(newNX) / static_cast<double>(nx);
726 double dYScale = static_cast<double>(newNY) / static_cast<double>(ny);
728 if (isComplex() && ! result.isComplex())
729 result.convertRealToComplex();
731 ImageFileArrayConst vReal = getArray();
732 ImageFileArrayConst vImag = getImaginaryArray();
733 ImageFileArray vResult = result.getArray();
734 ImageFileArray vResultImag = result.getImaginaryArray();
736 for (unsigned int ix = 0; ix < newNX; ix++) {
737 for (unsigned int iy = 0; iy < newNY; iy++) {
738 double dXPos = ix / dXScale;
739 double dYPos = iy / dYScale;
740 unsigned int scaleNX = static_cast<unsigned int> (dXPos);
741 unsigned int scaleNY = static_cast<unsigned int> (dYPos);
742 double dXFrac = dXPos - scaleNX;
743 double dYFrac = dYPos - scaleNY;
744 if (scaleNX >= nx - 1 || scaleNY >= ny - 1) {
745 vResult[ix][iy] = vReal[scaleNX][scaleNY];
746 if (result.isComplex()) {
748 vResultImag[ix][iy] = vImag[scaleNX][scaleNY];
750 vResultImag[ix][iy] = 0;
753 vResult[ix][iy] = (1 - dXFrac) * (1 - dYFrac) * vReal[scaleNX][scaleNY] +
754 dXFrac * (1 - dYFrac) * vReal[scaleNX+1][scaleNY] +
755 dYFrac * (1 - dXFrac) * vReal[scaleNX][scaleNY+1] +
756 dXFrac * dYFrac * vReal[scaleNX+1][scaleNY+1];
757 if (result.isComplex()) {
759 vResultImag[ix][iy] = (1 - dXFrac) * (1 - dYFrac) * vImag[scaleNX][scaleNY] +
760 dXFrac * (1 - dYFrac) * vImag[scaleNX+1][scaleNY] +
761 dYFrac * (1 - dXFrac) * vImag[scaleNX][scaleNY+1] +
762 dXFrac * dYFrac * vImag[scaleNX+1][scaleNY+1];
764 vResultImag[ix][iy] = 0;
775 ImageFile::fft (ImageFile& result) const
777 if (m_nx != result.nx() || m_ny != result.ny()) {
778 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
782 if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {
783 if (! result.convertRealToComplex ())
787 fftw_complex* in = new fftw_complex [m_nx * m_ny];
789 ImageFileArrayConst vReal = getArray();
790 ImageFileArrayConst vImag = getImaginaryArray();
793 unsigned int iArray = 0;
794 for (ix = 0; ix < m_nx; ix++)
795 for (iy = 0; iy < m_ny; iy++) {
796 in[iArray].re = vReal[ix][iy];
798 in[iArray].im = vImag[ix][iy];
804 fftwnd_plan plan = fftw2d_create_plan (m_nx, m_ny, FFTW_FORWARD, FFTW_IN_PLACE);
806 fftwnd_one (plan, in, NULL);
808 ImageFileArray vRealResult = result.getArray();
809 ImageFileArray vImagResult = result.getImaginaryArray();
811 unsigned int iScale = m_nx * m_ny;
812 for (ix = 0; ix < m_nx; ix++)
813 for (iy = 0; iy < m_ny; iy++) {
814 vRealResult[ix][iy] = in[iArray].re / iScale;
815 vImagResult[ix][iy] = in[iArray].im / iScale;
819 fftwnd_destroy_plan (plan);
823 Fourier::shuffleFourierToNaturalOrder (result);
830 ImageFile::ifft (ImageFile& result) const
832 if (m_nx != result.nx() || m_ny != result.ny()) {
833 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
837 if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {
838 if (! result.convertRealToComplex ())
842 ImageFileArrayConst vReal = getArray();
843 ImageFileArrayConst vImag = getImaginaryArray();
844 ImageFileArray vRealResult = result.getArray();
845 ImageFileArray vImagResult = result.getImaginaryArray();
847 for (ix = 0; ix < m_nx; ix++)
848 for (iy = 0; iy < m_ny; iy++) {
849 vRealResult[ix][iy] = vReal[ix][iy];
851 vImagResult[ix][iy] = vImag[ix][iy];
853 vImagResult[ix][iy] = 0;
856 Fourier::shuffleNaturalToFourierOrder (result);
858 fftw_complex* in = new fftw_complex [m_nx * m_ny];
860 unsigned int iArray = 0;
861 for (ix = 0; ix < m_nx; ix++)
862 for (iy = 0; iy < m_ny; iy++) {
863 in[iArray].re = vRealResult[ix][iy];
864 in[iArray].im = vImagResult[ix][iy];
868 fftwnd_plan plan = fftw2d_create_plan (m_nx, m_ny, FFTW_BACKWARD, FFTW_IN_PLACE);
870 fftwnd_one (plan, in, NULL);
873 for (ix = 0; ix < m_nx; ix++)
874 for (iy = 0; iy < m_ny; iy++) {
875 vRealResult[ix][iy] = in[iArray].re;
876 vImagResult[ix][iy] = in[iArray].im;
880 fftwnd_destroy_plan (plan);
888 ImageFile::fftRows (ImageFile& result) const
890 if (m_nx != result.nx() || m_ny != result.ny()) {
891 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::fftRows]");
895 if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {
896 if (! result.convertRealToComplex ())
900 fftw_complex* in = new fftw_complex [m_nx];
902 ImageFileArrayConst vReal = getArray();
903 ImageFileArrayConst vImag = getImaginaryArray();
905 fftw_plan plan = fftw_create_plan (m_nx, FFTW_FORWARD, FFTW_IN_PLACE);
906 std::complex<double>* pcRow = new std::complex<double> [m_nx];
909 unsigned int iArray = 0;
910 for (iy = 0; iy < m_ny; iy++) {
911 for (ix = 0; ix < m_nx; ix++) {
912 in[ix].re = vReal[ix][iy];
914 in[ix].im = vImag[ix][iy];
919 fftw_one (plan, in, NULL);
921 for (ix = 0; ix < m_nx; ix++)
922 pcRow[ix] = std::complex<double>(in[ix].re, in[ix].im);
924 Fourier::shuffleFourierToNaturalOrder (pcRow, m_nx);
925 for (ix = 0; ix < m_nx; ix++) {
926 vReal[ix][iy] = pcRow[ix].real();
927 vImag[ix][iy] = pcRow[ix].imag();
932 fftw_destroy_plan (plan);
939 ImageFile::ifftRows (ImageFile& result) const
941 if (m_nx != result.nx() || m_ny != result.ny()) {
942 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::fftRows]");
946 if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {
947 if (! result.convertRealToComplex ())
951 fftw_complex* in = new fftw_complex [m_nx];
953 ImageFileArrayConst vReal = getArray();
954 ImageFileArrayConst vImag = getImaginaryArray();
956 fftw_plan plan = fftw_create_plan (m_nx, FFTW_BACKWARD, FFTW_IN_PLACE);
957 std::complex<double>* pcRow = new std::complex<double> [m_nx];
960 unsigned int iArray = 0;
961 for (iy = 0; iy < m_ny; iy++) {
962 for (ix = 0; ix < m_nx; ix++) {
965 dImag = vImag[ix][iy];
966 pcRow[ix] = std::complex<double> (vReal[ix][iy], dImag);
969 Fourier::shuffleNaturalToFourierOrder (pcRow, m_nx);
971 for (ix = 0; ix < m_nx; ix++) {
972 in[ix].re = pcRow[ix].real();
973 in[ix].im = pcRow[ix].imag();
976 fftw_one (plan, in, NULL);
978 for (ix = 0; ix < m_nx; ix++) {
979 vReal[ix][iy] = in[ix].re;
980 vImag[ix][iy] = in[ix].im;
985 fftw_destroy_plan (plan);
992 ImageFile::fftCols (ImageFile& result) const
998 ImageFile::ifftCols (ImageFile& result) const
1008 ImageFile::fourier (ImageFile& result) const
1010 if (m_nx != result.nx() || m_ny != result.ny()) {
1011 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
1015 if (! result.isComplex())
1016 if (! result.convertRealToComplex ())
1019 ImageFileArrayConst vLHS = getArray();
1020 ImageFileArrayConst vLHSImag = getImaginaryArray();
1021 ImageFileArray vRealResult = result.getArray();
1022 ImageFileArray vImagResult = result.getImaginaryArray();
1024 unsigned int ix, iy;
1026 // alloc output matrix
1027 CTSimComplex** complexOut = new CTSimComplex* [m_nx];
1028 for (ix = 0; ix < m_nx; ix++)
1029 complexOut[ix] = new CTSimComplex [m_ny];
1031 // fourier each x column
1032 CTSimComplex* pY = new CTSimComplex [m_ny];
1033 for (ix = 0; ix < m_nx; ix++) {
1034 for (iy = 0; iy < m_ny; iy++) {
1037 dImag = vLHSImag[ix][iy];
1038 pY[iy] = std::complex<double>(vLHS[ix][iy], dImag);
1040 ProcessSignal::finiteFourierTransform (pY, complexOut[ix], m_ny, ProcessSignal::FORWARD);
1044 // fourier each y row
1045 CTSimComplex* pX = new CTSimComplex [m_nx];
1046 CTSimComplex* complexOutRow = new CTSimComplex [m_nx];
1047 for (iy = 0; iy < m_ny; iy++) {
1048 for (ix = 0; ix < m_nx; ix++)
1049 pX[ix] = complexOut[ix][iy];
1050 ProcessSignal::finiteFourierTransform (pX, complexOutRow, m_nx, ProcessSignal::FORWARD);
1051 for (ix = 0; ix < m_nx; ix++)
1052 complexOut[ix][iy] = complexOutRow[ix];
1055 delete [] complexOutRow;
1057 for (ix = 0; ix < m_nx; ix++)
1058 for (iy = 0; iy < m_ny; iy++) {
1059 vRealResult[ix][iy] = complexOut[ix][iy].real();
1060 vImagResult[ix][iy] = complexOut[ix][iy].imag();
1063 Fourier::shuffleFourierToNaturalOrder (result);
1065 // delete complexOut matrix
1066 for (ix = 0; ix < m_nx; ix++)
1067 delete [] complexOut[ix];
1068 delete [] complexOut;
1074 ImageFile::inverseFourier (ImageFile& result) const
1076 if (m_nx != result.nx() || m_ny != result.ny()) {
1077 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
1081 if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {
1082 if (! result.convertRealToComplex ())
1086 ImageFileArrayConst vLHSReal = getArray();
1087 ImageFileArrayConst vLHSImag = getImaginaryArray();
1088 ImageFileArray vRealResult = result.getArray();
1089 ImageFileArray vImagResult = result.getImaginaryArray();
1091 unsigned int ix, iy;
1092 // alloc 2d complex output matrix
1093 CTSimComplex** complexOut = new CTSimComplex* [m_nx];
1094 for (ix = 0; ix < m_nx; ix++)
1095 complexOut[ix] = new CTSimComplex [m_ny];
1097 // put input image into result
1098 for (ix = 0; ix < m_nx; ix++)
1099 for (iy = 0; iy < m_ny; iy++) {
1100 vRealResult[ix][iy] = vLHSReal[ix][iy];
1102 vImagResult[ix][iy] = vLHSImag[ix][iy];
1104 vImagResult[ix][iy] = 0;
1107 Fourier::shuffleNaturalToFourierOrder (result);
1109 // ifourier each x column
1110 CTSimComplex* pCol = new CTSimComplex [m_ny];
1111 for (ix = 0; ix < m_nx; ix++) {
1112 for (iy = 0; iy < m_ny; iy++) {
1113 pCol[iy] = std::complex<double> (vRealResult[ix][iy], vImagResult[ix][iy]);
1115 ProcessSignal::finiteFourierTransform (pCol, complexOut[ix], m_ny, ProcessSignal::BACKWARD);
1119 // ifourier each y row
1120 CTSimComplex* complexInRow = new CTSimComplex [m_nx];
1121 CTSimComplex* complexOutRow = new CTSimComplex [m_nx];
1122 for (iy = 0; iy < m_ny; iy++) {
1123 for (ix = 0; ix < m_nx; ix++)
1124 complexInRow[ix] = complexOut[ix][iy];
1125 ProcessSignal::finiteFourierTransform (complexInRow, complexOutRow, m_nx, ProcessSignal::BACKWARD);
1126 for (ix = 0; ix < m_nx; ix++)
1127 complexOut[ix][iy] = complexOutRow[ix];
1129 delete [] complexInRow;
1130 delete [] complexOutRow;
1132 for (ix = 0; ix < m_nx; ix++)
1133 for (iy = 0; iy < m_ny; iy++) {
1134 vRealResult[ix][iy] = complexOut[ix][iy].real();
1135 vImagResult[ix][iy] = complexOut[ix][iy].imag();
1138 // delete complexOut matrix
1139 for (ix = 0; ix < m_nx; ix++)
1140 delete [] complexOut[ix];
1141 delete [] complexOut;
1148 ImageFile::magnitude (ImageFile& result) const
1150 if (m_nx != result.nx() || m_ny != result.ny()) {
1151 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
1155 ImageFileArray vReal = getArray();
1156 ImageFileArray vImag = getImaginaryArray();
1157 ImageFileArray vRealResult = result.getArray();
1159 for (unsigned int ix = 0; ix < m_nx; ix++)
1160 for (unsigned int iy = 0; iy < m_ny; iy++) {
1162 vRealResult[ix][iy] = ::sqrt (vReal[ix][iy] * vReal[ix][iy] + vImag[ix][iy] * vImag[ix][iy]);
1164 vRealResult[ix][iy] = vReal[ix][iy];
1167 if (result.isComplex())
1168 result.convertComplexToReal();
1174 ImageFile::phase (ImageFile& result) const
1176 if (m_nx != result.nx() || m_ny != result.ny()) {
1177 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
1181 ImageFileArray vReal = getArray();
1182 ImageFileArray vImag = getImaginaryArray();
1183 ImageFileArray vRealResult = result.getArray();
1185 for (unsigned int ix = 0; ix < m_nx; ix++)
1186 for (unsigned int iy = 0; iy < m_ny; iy++) {
1188 vRealResult[ix][iy] = ::atan2 (vImag[ix][iy], vReal[ix][iy]);
1190 vRealResult[ix][iy] = 0;
1193 if (result.isComplex())
1194 result.convertComplexToReal();
1200 ImageFile::square (ImageFile& result) const
1202 if (m_nx != result.nx() || m_ny != result.ny()) {
1203 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
1207 if (isComplex() && ! result.isComplex())
1208 result.convertRealToComplex();
1210 ImageFileArrayConst vLHS = getArray();
1211 ImageFileArrayConst vLHSImag = getImaginaryArray();
1212 ImageFileArray vResult = result.getArray();
1213 ImageFileArray vResultImag = result.getImaginaryArray();
1215 for (unsigned int ix = 0; ix < m_nx; ix++) {
1216 for (unsigned int iy = 0; iy < m_ny; iy++) {
1217 if (result.isComplex()) {
1220 dImag = vLHSImag[ix][iy];
1221 std::complex<double> cLHS (vLHS[ix][iy], dImag);
1222 std::complex<double> cResult = cLHS * cLHS;
1223 vResult[ix][iy] = cResult.real();
1224 vResultImag[ix][iy] = cResult.imag();
1226 vResult[ix][iy] = vLHS[ix][iy] * vLHS[ix][iy];
1235 ImageFile::convertExportFormatNameToID (const char* const formatName)
1237 int formatID = EXPORT_FORMAT_INVALID;
1239 for (int i = 0; i < s_iExportFormatCount; i++)
1240 if (strcasecmp (formatName, s_aszExportFormatName[i]) == 0) {
1249 ImageFile::convertExportFormatIDToName (int formatID)
1251 static const char *formatName = "";
1253 if (formatID >= 0 && formatID < s_iExportFormatCount)
1254 return (s_aszExportFormatName[formatID]);
1256 return (formatName);
1260 ImageFile::convertExportFormatIDToTitle (const int formatID)
1262 static const char *formatTitle = "";
1264 if (formatID >= 0 && formatID < s_iExportFormatCount)
1265 return (s_aszExportFormatTitle[formatID]);
1267 return (formatTitle);
1271 ImageFile::convertImportFormatNameToID (const char* const formatName)
1273 int formatID = IMPORT_FORMAT_INVALID;
1275 for (int i = 0; i < s_iImportFormatCount; i++)
1276 if (strcasecmp (formatName, s_aszImportFormatName[i]) == 0) {
1285 ImageFile::convertImportFormatIDToName (int formatID)
1287 static const char *formatName = "";
1289 if (formatID >= 0 && formatID < s_iImportFormatCount)
1290 return (s_aszImportFormatName[formatID]);
1292 return (formatName);
1296 ImageFile::convertImportFormatIDToTitle (const int formatID)
1298 static const char *formatTitle = "";
1300 if (formatID >= 0 && formatID < s_iImportFormatCount)
1301 return (s_aszImportFormatTitle[formatID]);
1303 return (formatTitle);
1307 ImageFile::importImage (const char* const pszFormat, const char* const pszFilename)
1309 int iFormatID = convertImportFormatNameToID (pszFormat);
1311 if (iFormatID == IMPORT_FORMAT_PPM)
1312 return readImagePPM (pszFilename);
1314 else if (iFormatID == IMPORT_FORMAT_PNG)
1315 return readImagePNG (pszFilename);
1318 sys_error (ERR_SEVERE, "Invalid format %s [ImageFile::importImage]", pszFormat);
1323 ImageFile::skipSpacePPM (FILE* fp)
1326 while (isspace (c) || c == '#') {
1327 if (c == '#') { // comment until end of line
1329 while (c != 13 && c != 10)
1340 ImageFile::readImagePPM (const char* const pszFile)
1342 FILE* fp = fopen (pszFile, "r");
1343 if ((fp = fopen (pszFile, "r")) == NULL)
1345 char cSignature = toupper(fgetc(fp));
1346 if (cSignature != 'P') {
1350 cSignature = fgetc(fp);
1351 if (cSignature == '5' || cSignature == '6') { // binary modes
1353 fp = fopen(pszFile, "rb"); // reopen in binary mode
1356 } else if (cSignature != '2' && cSignature != '3') {
1361 int nRows, nCols, iMaxValue;
1363 if (fscanf (fp, "%d", &nCols) != 1) {
1368 if (fscanf (fp, "%d", &nRows) != 1) {
1373 if (fscanf (fp, "%d", &iMaxValue) != 1) {
1377 setArraySize (nRows, nCols);
1379 if (cSignature == '5' || cSignature == '6') { // binary modes
1383 if (c != 10) // read msdos 13-10 newline
1387 skipSpacePPM (fp); // ascii may have comments
1389 double dMaxValue = iMaxValue;
1390 ImageFileArray v = getArray();
1391 for (int iy = nRows - 1; iy >= 0; iy--) {
1392 for (int ix = 0; ix < nCols; ix++) {
1393 int iGS, iR, iG, iB;
1395 switch (cSignature) {
1397 if (fscanf(fp, "%d ", &iGS) != 1) {
1401 v[ix][iy] = iGS / dMaxValue;
1409 v[ix][iy] = iGS / dMaxValue;
1412 if (fscanf (fp, "%d %d %d ", &iR, &iG, &iB) != 3) {
1416 dR = iR / dMaxValue;
1417 dG = iG / dMaxValue;
1418 dB = iB / dMaxValue;
1419 v[ix][iy] = colorToGrayscale (dR, dG, dB);
1429 dR = iR / dMaxValue;
1430 dG = iG / dMaxValue;
1431 dB = iB / dMaxValue;
1432 v[ix][iy] = colorToGrayscale (dR, dG, dB);
1444 ImageFile::readImagePNG (const char* const pszFile)
1446 FILE* fp = fopen(pszFile, "rb");
1449 unsigned char header[8];
1450 fread (header, 1, 8, fp);
1451 if (png_sig_cmp (header, 0, 8)) {
1456 png_structp png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
1462 png_infop info_ptr = png_create_info_struct(png_ptr);
1464 png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
1469 png_infop end_info = png_create_info_struct(png_ptr);
1471 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
1476 if (setjmp(png_ptr->jmpbuf)) {
1477 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
1482 png_init_io(png_ptr, fp);
1483 png_set_sig_bytes(png_ptr, 8);
1484 png_read_info(png_ptr, info_ptr);
1486 int width = png_get_image_width (png_ptr, info_ptr);
1487 int height = png_get_image_height (png_ptr, info_ptr);
1488 int bit_depth = png_get_bit_depth (png_ptr, info_ptr);
1489 int color_type = png_get_color_type (png_ptr, info_ptr);
1491 if (color_type == PNG_COLOR_TYPE_PALETTE && bit_depth <= 8)
1492 png_set_expand(png_ptr);
1494 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
1495 png_set_expand(png_ptr);
1498 png_set_packing(png_ptr);
1500 if (color_type & PNG_COLOR_MASK_ALPHA)
1501 png_set_strip_alpha(png_ptr);
1503 if (bit_depth == 16)
1504 png_set_swap(png_ptr); // convert to little-endian format
1506 png_read_update_info(png_ptr, info_ptr); // update with transformations
1507 int rowbytes = png_get_rowbytes (png_ptr, info_ptr);
1508 bit_depth = png_get_bit_depth (png_ptr, info_ptr);
1509 color_type = png_get_color_type (png_ptr, info_ptr);
1511 png_bytep* row_pointers = new png_bytep [height];
1513 for (i = 0; i < height; i++)
1514 row_pointers[i] = new unsigned char [rowbytes];
1516 png_read_image(png_ptr, row_pointers);
1518 setArraySize (width, height);
1519 ImageFileArray v = getArray();
1520 for (int iy = 0; iy < height; iy++) {
1521 for (int ix = 0; ix < width; ix++) {
1523 if (color_type == PNG_COLOR_TYPE_GRAY) {
1525 dV = row_pointers[iy][ix] / 255.;
1526 else if (bit_depth == 16) {
1528 dV = (row_pointers[iy][iBase] + (row_pointers[iy][iBase+1] << 8)) / 65536.;
1530 } else if (color_type == PNG_COLOR_TYPE_RGB) {
1531 if (bit_depth == 8) {
1533 double dR = row_pointers[iy][iBase] / 255.;
1534 double dG = row_pointers[iy][iBase+1] / 255.;
1535 double dB = row_pointers[iy][iBase+2] / 255.;
1536 dV = colorToGrayscale (dR, dG, dR);
1539 v[ix][height-iy-1] = dV;
1543 png_read_end(png_ptr, end_info);
1544 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
1546 for (i = 0; i < height; i++)
1547 delete row_pointers[i];
1548 delete row_pointers;
1556 ImageFile::exportImage (const char* const pszFormat, const char* const pszFilename, int nxcell, int nycell, double densmin, double densmax)
1558 int iFormatID = convertExportFormatNameToID (pszFormat);
1560 if (iFormatID == EXPORT_FORMAT_PGM)
1561 return writeImagePGM (pszFilename, nxcell, nycell, densmin, densmax);
1562 else if (iFormatID == EXPORT_FORMAT_PGMASCII)
1563 return writeImagePGMASCII (pszFilename, nxcell, nycell, densmin, densmax);
1564 else if (iFormatID == EXPORT_FORMAT_PNG)
1565 return writeImagePNG (pszFilename, 8, nxcell, nycell, densmin, densmax);
1566 else if (iFormatID == EXPORT_FORMAT_PNG16)
1567 return writeImagePNG (pszFilename, 16, nxcell, nycell, densmin, densmax);
1569 sys_error (ERR_SEVERE, "Invalid format %s [ImageFile::exportImage]", pszFormat);
1575 ImageFile::writeImagePGM (const char* const outfile, int nxcell, int nycell, double densmin, double densmax)
1580 ImageFileArray v = getArray();
1582 unsigned char* rowp = new unsigned char [nx * nxcell];
1584 if ((fp = fopen (outfile, "wb")) == NULL)
1587 fprintf(fp, "P5\n");
1588 fprintf(fp, "%d %d\n", nx, ny);
1589 fprintf(fp, "255\n");
1591 for (int irow = ny - 1; irow >= 0; irow--) {
1592 for (int icol = 0; icol < nx; icol++) {
1593 int pos = icol * nxcell;
1594 double dens = (v[icol][irow] - densmin) / (densmax - densmin);
1595 dens = clamp (dens, 0., 1.);
1596 for (int p = pos; p < pos + nxcell; p++) {
1597 rowp[p] = static_cast<unsigned int> (dens * 255.);
1600 for (int ir = 0; ir < nycell; ir++) {
1601 for (int ic = 0; ic < nx * nxcell; ic++)
1602 fputc( rowp[ic], fp );
1613 ImageFile::writeImagePGMASCII (const char* const outfile, int nxcell, int nycell, double densmin, double densmax)
1618 ImageFileArray v = getArray();
1620 unsigned char* rowp = new unsigned char [nx * nxcell];
1622 if ((fp = fopen (outfile, "wb")) == NULL)
1625 fprintf(fp, "P2\n");
1626 fprintf(fp, "%d %d\n", nx, ny);
1627 fprintf(fp, "255\n");
1629 for (int irow = ny - 1; irow >= 0; irow--) {
1630 for (int icol = 0; icol < nx; icol++) {
1631 int pos = icol * nxcell;
1632 double dens = (v[icol][irow] - densmin) / (densmax - densmin);
1633 dens = clamp (dens, 0., 1.);
1634 for (int p = pos; p < pos + nxcell; p++) {
1635 rowp[p] = static_cast<unsigned int> (dens * 255.);
1638 for (int ir = 0; ir < nycell; ir++) {
1639 for (int ic = 0; ic < nx * nxcell; ic++)
1640 fprintf(fp, "%d ", rowp[ic]);
1654 ImageFile::writeImagePNG (const char* const outfile, int bitdepth, int nxcell, int nycell, double densmin, double densmax)
1656 double max_out_level = (1 << bitdepth) - 1;
1659 ImageFileArray v = getArray();
1661 unsigned char* rowp = new unsigned char [nx * nxcell * (bitdepth / 8)];
1663 FILE *fp = fopen (outfile, "wb");
1667 png_structp png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
1671 png_infop info_ptr = png_create_info_struct (png_ptr);
1673 png_destroy_write_struct (&png_ptr, (png_infopp) NULL);
1678 if (setjmp (png_ptr->jmpbuf)) {
1679 png_destroy_write_struct (&png_ptr, &info_ptr);
1684 png_init_io(png_ptr, fp);
1686 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);
1688 png_write_info(png_ptr, info_ptr);
1689 for (int irow = ny - 1; irow >= 0; irow--) {
1690 png_bytep row_pointer = rowp;
1692 for (int icol = 0; icol < nx; icol++) {
1693 int pos = icol * nxcell;
1694 double dens = (v[icol][irow] - densmin) / (densmax - densmin);
1695 dens = clamp (dens, 0., 1.);
1696 unsigned int outval = static_cast<unsigned int> (dens * max_out_level);
1698 for (int p = pos; p < pos + nxcell; p++) {
1703 rowp[rowpos+1] = (outval >> 8) & 0xFF;
1704 rowp[rowpos] = (outval & 0xFF);
1708 for (int ir = 0; ir < nycell; ir++)
1709 png_write_rows (png_ptr, &row_pointer, 1);
1712 png_write_end (png_ptr, info_ptr);
1713 png_destroy_write_struct (&png_ptr, &info_ptr);
1724 static const int N_GRAYSCALE=256;
1727 ImageFile::writeImageGIF (const char* const outfile, int nxcell, int nycell, double densmin, double densmax)
1729 int gs_indices[N_GRAYSCALE];
1732 ImageFileArray v = getArray();
1734 unsigned char* rowp = new unsigned char [nx * nxcell];
1736 gdImagePtr gif = gdImageCreate(nx * nxcell, ny * nycell);
1737 for (int i = 0; i < N_GRAYSCALE; i++)
1738 gs_indices[i] = gdImageColorAllocate(gif, i, i, i);
1740 int lastrow = ny * nycell - 1;
1741 for (int irow = 0; irow < ny; irow++) {
1742 int rpos = irow * nycell;
1743 for (int ir = rpos; ir < rpos + nycell; ir++) {
1744 for (int icol = 0; icol < nx; icol++) {
1745 int cpos = icol * nxcell;
1746 double dens = (v[icol][irow] - densmin) / (densmax - densmin);
1747 dens = clamp(dens, 0., 1.);
1748 for (int ic = cpos; ic < cpos + nxcell; ic++) {
1749 rowp[ic] = (unsigned int) (dens * (double) (N_GRAYSCALE - 1));
1750 gdImageSetPixel(gif, ic, lastrow - ir, gs_indices[rowp[ic]]);
1757 if ((out = fopen (outfile,"w")) == NULL) {
1758 sys_error(ERR_SEVERE, "Error opening output file %s for writing", outfile);
1761 gdImageGif(gif,out);
1763 gdImageDestroy(gif);