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.38 2001/03/01 20:02:18 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::invertPixelValues]");
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 vResult[ix][iy] = ::log (vLHS[ix][iy]);
679 ImageFile::exp (ImageFile& result) const
681 if (m_nx != result.nx() || m_ny != result.ny()) {
682 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
686 if (isComplex() && ! result.isComplex())
687 result.convertRealToComplex();
689 ImageFileArrayConst vLHS = getArray();
690 ImageFileArrayConst vLHSImag = getImaginaryArray();
691 ImageFileArray vResult = result.getArray();
692 ImageFileArray vResultImag = result.getImaginaryArray();
694 for (unsigned int ix = 0; ix < m_nx; ix++) {
695 for (unsigned int iy = 0; iy < m_ny; iy++) {
696 if (result.isComplex()) {
699 dImag = vLHSImag[ix][iy];
700 std::complex<double> cLHS (vLHS[ix][iy], dImag);
701 std::complex<double> cResult = std::exp (cLHS);
702 vResult[ix][iy] = cResult.real();
703 vResultImag[ix][iy] = cResult.imag();
705 vResult[ix][iy] = ::exp (vLHS[ix][iy]);
714 ImageFile::scaleImage (ImageFile& result) const
716 unsigned int nx = m_nx;
717 unsigned int ny = m_ny;
718 unsigned int newNX = result.nx();
719 unsigned int newNY = result.ny();
721 double dXScale = static_cast<double>(newNX) / static_cast<double>(nx);
722 double dYScale = static_cast<double>(newNY) / static_cast<double>(ny);
724 if (isComplex() && ! result.isComplex())
725 result.convertRealToComplex();
727 ImageFileArrayConst vReal = getArray();
728 ImageFileArrayConst vImag = getImaginaryArray();
729 ImageFileArray vResult = result.getArray();
730 ImageFileArray vResultImag = result.getImaginaryArray();
732 for (unsigned int ix = 0; ix < newNX; ix++) {
733 for (unsigned int iy = 0; iy < newNY; iy++) {
734 double dXPos = ix / dXScale;
735 double dYPos = iy / dYScale;
736 unsigned int scaleNX = static_cast<unsigned int> (dXPos);
737 unsigned int scaleNY = static_cast<unsigned int> (dYPos);
738 double dXFrac = dXPos - scaleNX;
739 double dYFrac = dYPos - scaleNY;
740 if (scaleNX >= nx - 1 || scaleNY >= ny - 1) {
741 vResult[ix][iy] = vReal[scaleNX][scaleNY];
742 if (result.isComplex()) {
744 vResultImag[ix][iy] = vImag[scaleNX][scaleNY];
746 vResultImag[ix][iy] = 0;
749 vResult[ix][iy] = (1 - dXFrac) * (1 - dYFrac) * vReal[scaleNX][scaleNY] +
750 dXFrac * (1 - dYFrac) * vReal[scaleNX+1][scaleNY] +
751 dYFrac * (1 - dXFrac) * vReal[scaleNX][scaleNY+1] +
752 dXFrac * dYFrac * vReal[scaleNX+1][scaleNY+1];
753 if (result.isComplex()) {
755 vResultImag[ix][iy] = (1 - dXFrac) * (1 - dYFrac) * vImag[scaleNX][scaleNY] +
756 dXFrac * (1 - dYFrac) * vImag[scaleNX+1][scaleNY] +
757 dYFrac * (1 - dXFrac) * vImag[scaleNX][scaleNY+1] +
758 dXFrac * dYFrac * vImag[scaleNX+1][scaleNY+1];
760 vResultImag[ix][iy] = 0;
771 ImageFile::fft (ImageFile& result) const
773 if (m_nx != result.nx() || m_ny != result.ny()) {
774 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
778 if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {
779 if (! result.convertRealToComplex ())
783 fftw_complex* in = new fftw_complex [m_nx * m_ny];
785 ImageFileArrayConst vReal = getArray();
786 ImageFileArrayConst vImag = getImaginaryArray();
789 unsigned int iArray = 0;
790 for (ix = 0; ix < m_nx; ix++)
791 for (iy = 0; iy < m_ny; iy++) {
792 in[iArray].re = vReal[ix][iy];
794 in[iArray].im = vImag[ix][iy];
800 fftwnd_plan plan = fftw2d_create_plan (m_nx, m_ny, FFTW_FORWARD, FFTW_IN_PLACE);
802 fftwnd_one (plan, in, NULL);
804 ImageFileArray vRealResult = result.getArray();
805 ImageFileArray vImagResult = result.getImaginaryArray();
807 unsigned int iScale = m_nx * m_ny;
808 for (ix = 0; ix < m_nx; ix++)
809 for (iy = 0; iy < m_ny; iy++) {
810 vRealResult[ix][iy] = in[iArray].re / iScale;
811 vImagResult[ix][iy] = in[iArray].im / iScale;
815 fftwnd_destroy_plan (plan);
819 Fourier::shuffleFourierToNaturalOrder (result);
826 ImageFile::ifft (ImageFile& result) const
828 if (m_nx != result.nx() || m_ny != result.ny()) {
829 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
833 if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {
834 if (! result.convertRealToComplex ())
838 ImageFileArrayConst vReal = getArray();
839 ImageFileArrayConst vImag = getImaginaryArray();
840 ImageFileArray vRealResult = result.getArray();
841 ImageFileArray vImagResult = result.getImaginaryArray();
843 for (ix = 0; ix < m_nx; ix++)
844 for (iy = 0; iy < m_ny; iy++) {
845 vRealResult[ix][iy] = vReal[ix][iy];
847 vImagResult[ix][iy] = vImag[ix][iy];
849 vImagResult[ix][iy] = 0;
852 Fourier::shuffleNaturalToFourierOrder (result);
854 fftw_complex* in = new fftw_complex [m_nx * m_ny];
856 unsigned int iArray = 0;
857 for (ix = 0; ix < m_nx; ix++)
858 for (iy = 0; iy < m_ny; iy++) {
859 in[iArray].re = vRealResult[ix][iy];
860 in[iArray].im = vImagResult[ix][iy];
864 fftwnd_plan plan = fftw2d_create_plan (m_nx, m_ny, FFTW_BACKWARD, FFTW_IN_PLACE);
866 fftwnd_one (plan, in, NULL);
869 for (ix = 0; ix < m_nx; ix++)
870 for (iy = 0; iy < m_ny; iy++) {
871 vRealResult[ix][iy] = in[iArray].re;
872 vImagResult[ix][iy] = in[iArray].im;
876 fftwnd_destroy_plan (plan);
884 ImageFile::fftRows (ImageFile& result) const
886 if (m_nx != result.nx() || m_ny != result.ny()) {
887 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::fftRows]");
891 if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {
892 if (! result.convertRealToComplex ())
896 fftw_complex* in = new fftw_complex [m_nx];
898 ImageFileArrayConst vReal = getArray();
899 ImageFileArrayConst vImag = getImaginaryArray();
901 fftw_plan plan = fftw_create_plan (m_nx, FFTW_FORWARD, FFTW_IN_PLACE);
902 std::complex<double>* pcRow = new std::complex<double> [m_nx];
905 unsigned int iArray = 0;
906 for (iy = 0; iy < m_ny; iy++) {
907 for (ix = 0; ix < m_nx; ix++) {
908 in[ix].re = vReal[ix][iy];
910 in[ix].im = vImag[ix][iy];
915 fftw_one (plan, in, NULL);
917 for (ix = 0; ix < m_nx; ix++)
918 pcRow[ix] = std::complex<double>(in[ix].re, in[ix].im);
920 Fourier::shuffleFourierToNaturalOrder (pcRow, m_nx);
921 for (ix = 0; ix < m_nx; ix++) {
922 vReal[ix][iy] = pcRow[ix].real();
923 vImag[ix][iy] = pcRow[ix].imag();
928 fftw_destroy_plan (plan);
935 ImageFile::ifftRows (ImageFile& result) const
937 if (m_nx != result.nx() || m_ny != result.ny()) {
938 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::fftRows]");
942 if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {
943 if (! result.convertRealToComplex ())
947 fftw_complex* in = new fftw_complex [m_nx];
949 ImageFileArrayConst vReal = getArray();
950 ImageFileArrayConst vImag = getImaginaryArray();
952 fftw_plan plan = fftw_create_plan (m_nx, FFTW_BACKWARD, FFTW_IN_PLACE);
953 std::complex<double>* pcRow = new std::complex<double> [m_nx];
956 unsigned int iArray = 0;
957 for (iy = 0; iy < m_ny; iy++) {
958 for (ix = 0; ix < m_nx; ix++) {
961 dImag = vImag[ix][iy];
962 pcRow[ix] = std::complex<double> (vReal[ix][iy], dImag);
965 Fourier::shuffleNaturalToFourierOrder (pcRow, m_nx);
967 for (ix = 0; ix < m_nx; ix++) {
968 in[ix].re = pcRow[ix].real();
969 in[ix].im = pcRow[ix].imag();
972 fftw_one (plan, in, NULL);
974 for (ix = 0; ix < m_nx; ix++) {
975 vReal[ix][iy] = in[ix].re;
976 vImag[ix][iy] = in[ix].im;
981 fftw_destroy_plan (plan);
988 ImageFile::fftCols (ImageFile& result) const
994 ImageFile::ifftCols (ImageFile& result) const
1004 ImageFile::fourier (ImageFile& result) const
1006 if (m_nx != result.nx() || m_ny != result.ny()) {
1007 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
1011 if (! result.isComplex())
1012 if (! result.convertRealToComplex ())
1015 ImageFileArrayConst vLHS = getArray();
1016 ImageFileArrayConst vLHSImag = getImaginaryArray();
1017 ImageFileArray vRealResult = result.getArray();
1018 ImageFileArray vImagResult = result.getImaginaryArray();
1020 unsigned int ix, iy;
1022 // alloc output matrix
1023 CTSimComplex** complexOut = new CTSimComplex* [m_nx];
1024 for (ix = 0; ix < m_nx; ix++)
1025 complexOut[ix] = new CTSimComplex [m_ny];
1027 // fourier each x column
1028 CTSimComplex* pY = new CTSimComplex [m_ny];
1029 for (ix = 0; ix < m_nx; ix++) {
1030 for (iy = 0; iy < m_ny; iy++) {
1033 dImag = vLHSImag[ix][iy];
1034 pY[iy] = std::complex<double>(vLHS[ix][iy], dImag);
1036 ProcessSignal::finiteFourierTransform (pY, complexOut[ix], m_ny, ProcessSignal::FORWARD);
1040 // fourier each y row
1041 CTSimComplex* pX = new CTSimComplex [m_nx];
1042 CTSimComplex* complexOutRow = new CTSimComplex [m_nx];
1043 for (iy = 0; iy < m_ny; iy++) {
1044 for (ix = 0; ix < m_nx; ix++)
1045 pX[ix] = complexOut[ix][iy];
1046 ProcessSignal::finiteFourierTransform (pX, complexOutRow, m_nx, ProcessSignal::FORWARD);
1047 for (ix = 0; ix < m_nx; ix++)
1048 complexOut[ix][iy] = complexOutRow[ix];
1051 delete [] complexOutRow;
1053 for (ix = 0; ix < m_nx; ix++)
1054 for (iy = 0; iy < m_ny; iy++) {
1055 vRealResult[ix][iy] = complexOut[ix][iy].real();
1056 vImagResult[ix][iy] = complexOut[ix][iy].imag();
1059 Fourier::shuffleFourierToNaturalOrder (result);
1061 // delete complexOut matrix
1062 for (ix = 0; ix < m_nx; ix++)
1063 delete [] complexOut[ix];
1064 delete [] complexOut;
1070 ImageFile::inverseFourier (ImageFile& result) const
1072 if (m_nx != result.nx() || m_ny != result.ny()) {
1073 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
1077 if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {
1078 if (! result.convertRealToComplex ())
1082 ImageFileArrayConst vLHSReal = getArray();
1083 ImageFileArrayConst vLHSImag = getImaginaryArray();
1084 ImageFileArray vRealResult = result.getArray();
1085 ImageFileArray vImagResult = result.getImaginaryArray();
1087 unsigned int ix, iy;
1088 // alloc 2d complex output matrix
1089 CTSimComplex** complexOut = new CTSimComplex* [m_nx];
1090 for (ix = 0; ix < m_nx; ix++)
1091 complexOut[ix] = new CTSimComplex [m_ny];
1093 // put input image into result
1094 for (ix = 0; ix < m_nx; ix++)
1095 for (iy = 0; iy < m_ny; iy++) {
1096 vRealResult[ix][iy] = vLHSReal[ix][iy];
1098 vImagResult[ix][iy] = vLHSImag[ix][iy];
1100 vImagResult[ix][iy] = 0;
1103 Fourier::shuffleNaturalToFourierOrder (result);
1105 // ifourier each x column
1106 CTSimComplex* pCol = new CTSimComplex [m_ny];
1107 for (ix = 0; ix < m_nx; ix++) {
1108 for (iy = 0; iy < m_ny; iy++) {
1109 pCol[iy] = std::complex<double> (vRealResult[ix][iy], vImagResult[ix][iy]);
1111 ProcessSignal::finiteFourierTransform (pCol, complexOut[ix], m_ny, ProcessSignal::BACKWARD);
1115 // ifourier each y row
1116 CTSimComplex* complexInRow = new CTSimComplex [m_nx];
1117 CTSimComplex* complexOutRow = new CTSimComplex [m_nx];
1118 for (iy = 0; iy < m_ny; iy++) {
1119 for (ix = 0; ix < m_nx; ix++)
1120 complexInRow[ix] = complexOut[ix][iy];
1121 ProcessSignal::finiteFourierTransform (complexInRow, complexOutRow, m_nx, ProcessSignal::BACKWARD);
1122 for (ix = 0; ix < m_nx; ix++)
1123 complexOut[ix][iy] = complexOutRow[ix];
1125 delete [] complexInRow;
1126 delete [] complexOutRow;
1128 for (ix = 0; ix < m_nx; ix++)
1129 for (iy = 0; iy < m_ny; iy++) {
1130 vRealResult[ix][iy] = complexOut[ix][iy].real();
1131 vImagResult[ix][iy] = complexOut[ix][iy].imag();
1134 // delete complexOut matrix
1135 for (ix = 0; ix < m_nx; ix++)
1136 delete [] complexOut[ix];
1137 delete [] complexOut;
1144 ImageFile::magnitude (ImageFile& result) const
1146 if (m_nx != result.nx() || m_ny != result.ny()) {
1147 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
1151 ImageFileArray vReal = getArray();
1152 ImageFileArray vImag = getImaginaryArray();
1153 ImageFileArray vRealResult = result.getArray();
1155 for (unsigned int ix = 0; ix < m_nx; ix++)
1156 for (unsigned int iy = 0; iy < m_ny; iy++) {
1158 vRealResult[ix][iy] = ::sqrt (vReal[ix][iy] * vReal[ix][iy] + vImag[ix][iy] * vImag[ix][iy]);
1160 vRealResult[ix][iy] = vReal[ix][iy];
1163 if (result.isComplex())
1164 result.convertComplexToReal();
1170 ImageFile::phase (ImageFile& result) const
1172 if (m_nx != result.nx() || m_ny != result.ny()) {
1173 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
1177 ImageFileArray vReal = getArray();
1178 ImageFileArray vImag = getImaginaryArray();
1179 ImageFileArray vRealResult = result.getArray();
1181 for (unsigned int ix = 0; ix < m_nx; ix++)
1182 for (unsigned int iy = 0; iy < m_ny; iy++) {
1184 vRealResult[ix][iy] = ::atan2 (vImag[ix][iy], vReal[ix][iy]);
1186 vRealResult[ix][iy] = 0;
1189 if (result.isComplex())
1190 result.convertComplexToReal();
1196 ImageFile::square (ImageFile& result) const
1198 if (m_nx != result.nx() || m_ny != result.ny()) {
1199 sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");
1203 if (isComplex() && ! result.isComplex())
1204 result.convertRealToComplex();
1206 ImageFileArrayConst vLHS = getArray();
1207 ImageFileArrayConst vLHSImag = getImaginaryArray();
1208 ImageFileArray vResult = result.getArray();
1209 ImageFileArray vResultImag = result.getImaginaryArray();
1211 for (unsigned int ix = 0; ix < m_nx; ix++) {
1212 for (unsigned int iy = 0; iy < m_ny; iy++) {
1213 if (result.isComplex()) {
1216 dImag = vLHSImag[ix][iy];
1217 std::complex<double> cLHS (vLHS[ix][iy], dImag);
1218 std::complex<double> cResult = cLHS * cLHS;
1219 vResult[ix][iy] = cResult.real();
1220 vResultImag[ix][iy] = cResult.imag();
1222 vResult[ix][iy] = vLHS[ix][iy] * vLHS[ix][iy];
1231 ImageFile::convertExportFormatNameToID (const char* const formatName)
1233 int formatID = EXPORT_FORMAT_INVALID;
1235 for (int i = 0; i < s_iExportFormatCount; i++)
1236 if (strcasecmp (formatName, s_aszExportFormatName[i]) == 0) {
1245 ImageFile::convertExportFormatIDToName (int formatID)
1247 static const char *formatName = "";
1249 if (formatID >= 0 && formatID < s_iExportFormatCount)
1250 return (s_aszExportFormatName[formatID]);
1252 return (formatName);
1256 ImageFile::convertExportFormatIDToTitle (const int formatID)
1258 static const char *formatTitle = "";
1260 if (formatID >= 0 && formatID < s_iExportFormatCount)
1261 return (s_aszExportFormatTitle[formatID]);
1263 return (formatTitle);
1267 ImageFile::convertImportFormatNameToID (const char* const formatName)
1269 int formatID = IMPORT_FORMAT_INVALID;
1271 for (int i = 0; i < s_iImportFormatCount; i++)
1272 if (strcasecmp (formatName, s_aszImportFormatName[i]) == 0) {
1281 ImageFile::convertImportFormatIDToName (int formatID)
1283 static const char *formatName = "";
1285 if (formatID >= 0 && formatID < s_iImportFormatCount)
1286 return (s_aszImportFormatName[formatID]);
1288 return (formatName);
1292 ImageFile::convertImportFormatIDToTitle (const int formatID)
1294 static const char *formatTitle = "";
1296 if (formatID >= 0 && formatID < s_iImportFormatCount)
1297 return (s_aszImportFormatTitle[formatID]);
1299 return (formatTitle);
1303 ImageFile::importImage (const char* const pszFormat, const char* const pszFilename)
1305 int iFormatID = convertImportFormatNameToID (pszFormat);
1307 if (iFormatID == IMPORT_FORMAT_PPM)
1308 return readImagePPM (pszFilename);
1310 else if (iFormatID == IMPORT_FORMAT_PNG)
1311 return readImagePNG (pszFilename);
1313 #ifdef HAVE_CTN_DICOM
1314 else if (iFormatID == IMPORT_FORMAT_DICOM)
1315 return readImageDicom (pszFilename);
1318 sys_error (ERR_SEVERE, "Invalid format %s [ImageFile::importImage]", pszFormat);
1323 ImageFile::readImageDicom (const char* const pszFile)
1329 ImageFile::skipSpacePPM (FILE* fp)
1332 while (isspace (c) || c == '#') {
1333 if (c == '#') { // comment until end of line
1335 while (c != 13 && c != 10)
1346 ImageFile::readImagePPM (const char* const pszFile)
1348 FILE* fp = fopen (pszFile, "r");
1349 if ((fp = fopen (pszFile, "r")) == NULL)
1351 char cSignature = toupper(fgetc(fp));
1352 if (cSignature != 'P') {
1356 cSignature = fgetc(fp);
1357 if (cSignature == '5' || cSignature == '6') { // binary modes
1359 fp = fopen(pszFile, "rb"); // reopen in binary mode
1362 } else if (cSignature != '2' && cSignature != '3') {
1367 int nRows, nCols, iMaxValue;
1369 if (fscanf (fp, "%d", &nCols) != 1) {
1374 if (fscanf (fp, "%d", &nRows) != 1) {
1379 if (fscanf (fp, "%d", &iMaxValue) != 1) {
1383 setArraySize (nRows, nCols);
1385 if (cSignature == '5' || cSignature == '6') { // binary modes
1389 if (c != 10) // read msdos 13-10 newline
1393 skipSpacePPM (fp); // ascii may have comments
1395 double dMaxValue = iMaxValue;
1396 ImageFileArray v = getArray();
1397 for (int iy = nRows - 1; iy >= 0; iy--) {
1398 for (int ix = 0; ix < nCols; ix++) {
1399 int iGS, iR, iG, iB;
1401 switch (cSignature) {
1403 if (fscanf(fp, "%d ", &iGS) != 1) {
1407 v[ix][iy] = iGS / dMaxValue;
1415 v[ix][iy] = iGS / dMaxValue;
1418 if (fscanf (fp, "%d %d %d ", &iR, &iG, &iB) != 3) {
1422 dR = iR / dMaxValue;
1423 dG = iG / dMaxValue;
1424 dB = iB / dMaxValue;
1425 v[ix][iy] = colorToGrayscale (dR, dG, dB);
1435 dR = iR / dMaxValue;
1436 dG = iG / dMaxValue;
1437 dB = iB / dMaxValue;
1438 v[ix][iy] = colorToGrayscale (dR, dG, dB);
1450 ImageFile::readImagePNG (const char* const pszFile)
1452 FILE* fp = fopen(pszFile, "rb");
1455 unsigned char header[8];
1456 fread (header, 1, 8, fp);
1457 if (png_sig_cmp (header, 0, 8)) {
1462 png_structp png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
1468 png_infop info_ptr = png_create_info_struct(png_ptr);
1470 png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
1475 png_infop end_info = png_create_info_struct(png_ptr);
1477 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
1482 if (setjmp(png_ptr->jmpbuf)) {
1483 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
1488 png_init_io(png_ptr, fp);
1489 png_set_sig_bytes(png_ptr, 8);
1490 png_read_info(png_ptr, info_ptr);
1492 int width = png_get_image_width (png_ptr, info_ptr);
1493 int height = png_get_image_height (png_ptr, info_ptr);
1494 int bit_depth = png_get_bit_depth (png_ptr, info_ptr);
1495 int color_type = png_get_color_type (png_ptr, info_ptr);
1497 if (color_type == PNG_COLOR_TYPE_PALETTE && bit_depth <= 8)
1498 png_set_expand(png_ptr);
1500 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
1501 png_set_expand(png_ptr);
1504 png_set_packing(png_ptr);
1506 if (color_type & PNG_COLOR_MASK_ALPHA)
1507 png_set_strip_alpha(png_ptr);
1509 if (bit_depth == 16)
1510 png_set_swap(png_ptr); // convert to little-endian format
1512 png_read_update_info(png_ptr, info_ptr); // update with transformations
1513 int rowbytes = png_get_rowbytes (png_ptr, info_ptr);
1514 bit_depth = png_get_bit_depth (png_ptr, info_ptr);
1515 color_type = png_get_color_type (png_ptr, info_ptr);
1517 png_bytep* row_pointers = new png_bytep [height];
1519 for (i = 0; i < height; i++)
1520 row_pointers[i] = new unsigned char [rowbytes];
1522 png_read_image(png_ptr, row_pointers);
1524 setArraySize (width, height);
1525 ImageFileArray v = getArray();
1526 for (int iy = 0; iy < height; iy++) {
1527 for (int ix = 0; ix < width; ix++) {
1529 if (color_type == PNG_COLOR_TYPE_GRAY) {
1531 dV = row_pointers[iy][ix] / 255.;
1532 else if (bit_depth == 16) {
1534 dV = (row_pointers[iy][iBase] + (row_pointers[iy][iBase+1] << 8)) / 65536.;
1536 } else if (color_type == PNG_COLOR_TYPE_RGB) {
1537 if (bit_depth == 8) {
1539 double dR = row_pointers[iy][iBase] / 255.;
1540 double dG = row_pointers[iy][iBase+1] / 255.;
1541 double dB = row_pointers[iy][iBase+2] / 255.;
1542 dV = colorToGrayscale (dR, dG, dR);
1545 v[ix][height-iy-1] = dV;
1549 png_read_end(png_ptr, end_info);
1550 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
1552 for (i = 0; i < height; i++)
1553 delete row_pointers[i];
1554 delete row_pointers;
1562 ImageFile::exportImage (const char* const pszFormat, const char* const pszFilename, int nxcell, int nycell, double densmin, double densmax)
1564 int iFormatID = convertExportFormatNameToID (pszFormat);
1566 if (iFormatID == EXPORT_FORMAT_PGM)
1567 return writeImagePGM (pszFilename, nxcell, nycell, densmin, densmax);
1568 else if (iFormatID == EXPORT_FORMAT_PGMASCII)
1569 return writeImagePGMASCII (pszFilename, nxcell, nycell, densmin, densmax);
1570 else if (iFormatID == EXPORT_FORMAT_PNG)
1571 return writeImagePNG (pszFilename, 8, nxcell, nycell, densmin, densmax);
1572 else if (iFormatID == EXPORT_FORMAT_PNG16)
1573 return writeImagePNG (pszFilename, 16, nxcell, nycell, densmin, densmax);
1575 sys_error (ERR_SEVERE, "Invalid format %s [ImageFile::exportImage]", pszFormat);
1581 ImageFile::writeImagePGM (const char* const outfile, int nxcell, int nycell, double densmin, double densmax)
1586 ImageFileArray v = getArray();
1588 unsigned char* rowp = new unsigned char [nx * nxcell];
1590 if ((fp = fopen (outfile, "wb")) == NULL)
1593 fprintf(fp, "P5\n");
1594 fprintf(fp, "%d %d\n", nx, ny);
1595 fprintf(fp, "255\n");
1597 for (int irow = ny - 1; irow >= 0; irow--) {
1598 for (int icol = 0; icol < nx; icol++) {
1599 int pos = icol * nxcell;
1600 double dens = (v[icol][irow] - densmin) / (densmax - densmin);
1601 dens = clamp (dens, 0., 1.);
1602 for (int p = pos; p < pos + nxcell; p++) {
1603 rowp[p] = static_cast<unsigned int> (dens * 255.);
1606 for (int ir = 0; ir < nycell; ir++) {
1607 for (int ic = 0; ic < nx * nxcell; ic++)
1608 fputc( rowp[ic], fp );
1619 ImageFile::writeImagePGMASCII (const char* const outfile, int nxcell, int nycell, double densmin, double densmax)
1624 ImageFileArray v = getArray();
1626 unsigned char* rowp = new unsigned char [nx * nxcell];
1628 if ((fp = fopen (outfile, "wb")) == NULL)
1631 fprintf(fp, "P2\n");
1632 fprintf(fp, "%d %d\n", nx, ny);
1633 fprintf(fp, "255\n");
1635 for (int irow = ny - 1; irow >= 0; irow--) {
1636 for (int icol = 0; icol < nx; icol++) {
1637 int pos = icol * nxcell;
1638 double dens = (v[icol][irow] - densmin) / (densmax - densmin);
1639 dens = clamp (dens, 0., 1.);
1640 for (int p = pos; p < pos + nxcell; p++) {
1641 rowp[p] = static_cast<unsigned int> (dens * 255.);
1644 for (int ir = 0; ir < nycell; ir++) {
1645 for (int ic = 0; ic < nx * nxcell; ic++)
1646 fprintf(fp, "%d ", rowp[ic]);
1660 ImageFile::writeImagePNG (const char* const outfile, int bitdepth, int nxcell, int nycell, double densmin, double densmax)
1662 double max_out_level = (1 << bitdepth) - 1;
1665 ImageFileArray v = getArray();
1667 unsigned char* rowp = new unsigned char [nx * nxcell * (bitdepth / 8)];
1669 FILE *fp = fopen (outfile, "wb");
1673 png_structp png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
1677 png_infop info_ptr = png_create_info_struct (png_ptr);
1679 png_destroy_write_struct (&png_ptr, (png_infopp) NULL);
1684 if (setjmp (png_ptr->jmpbuf)) {
1685 png_destroy_write_struct (&png_ptr, &info_ptr);
1690 png_init_io(png_ptr, fp);
1692 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);
1694 png_write_info(png_ptr, info_ptr);
1695 for (int irow = ny - 1; irow >= 0; irow--) {
1696 png_bytep row_pointer = rowp;
1698 for (int icol = 0; icol < nx; icol++) {
1699 int pos = icol * nxcell;
1700 double dens = (v[icol][irow] - densmin) / (densmax - densmin);
1701 dens = clamp (dens, 0., 1.);
1702 unsigned int outval = static_cast<unsigned int> (dens * max_out_level);
1704 for (int p = pos; p < pos + nxcell; p++) {
1709 rowp[rowpos+1] = (outval >> 8) & 0xFF;
1710 rowp[rowpos] = (outval & 0xFF);
1714 for (int ir = 0; ir < nycell; ir++)
1715 png_write_rows (png_ptr, &row_pointer, 1);
1718 png_write_end (png_ptr, info_ptr);
1719 png_destroy_write_struct (&png_ptr, &info_ptr);
1730 static const int N_GRAYSCALE=256;
1733 ImageFile::writeImageGIF (const char* const outfile, int nxcell, int nycell, double densmin, double densmax)
1735 int gs_indices[N_GRAYSCALE];
1738 ImageFileArray v = getArray();
1740 unsigned char* rowp = new unsigned char [nx * nxcell];
1742 gdImagePtr gif = gdImageCreate(nx * nxcell, ny * nycell);
1743 for (int i = 0; i < N_GRAYSCALE; i++)
1744 gs_indices[i] = gdImageColorAllocate(gif, i, i, i);
1746 int lastrow = ny * nycell - 1;
1747 for (int irow = 0; irow < ny; irow++) {
1748 int rpos = irow * nycell;
1749 for (int ir = rpos; ir < rpos + nycell; ir++) {
1750 for (int icol = 0; icol < nx; icol++) {
1751 int cpos = icol * nxcell;
1752 double dens = (v[icol][irow] - densmin) / (densmax - densmin);
1753 dens = clamp(dens, 0., 1.);
1754 for (int ic = cpos; ic < cpos + nxcell; ic++) {
1755 rowp[ic] = (unsigned int) (dens * (double) (N_GRAYSCALE - 1));
1756 gdImageSetPixel(gif, ic, lastrow - ir, gs_indices[rowp[ic]]);
1763 if ((out = fopen (outfile,"w")) == NULL) {
1764 sys_error(ERR_SEVERE, "Error opening output file %s for writing", outfile);
1767 gdImageGif(gif,out);
1769 gdImageDestroy(gif);