54f38a18550998ee53bfd6b0af12b55bfbeaa55e
[ctsim.git] / libctsim / imagefile.cpp
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **      Name:         imagefile.cpp
5 **      Purpose:      Imagefile classes
6 **      Programmer:   Kevin Rosenberg
7 **      Date Started: June 2000
8 **
9 **  This is part of the CTSim program
10 **  Copyright (C) 1983-2000 Kevin Rosenberg
11 **
12 **  $Id: imagefile.cpp,v 1.25 2000/12/29 15:45:06 kevin Exp $
13 **
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.
17 **
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.
22 **
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 ******************************************************************************/
27
28 #include "ct.h"
29
30
31 F32Image::F32Image (int nx, int ny)\r
32 : Array2dFile (nx, ny, sizeof(kfloat32), Array2dFile::PIXEL_FLOAT32)\r
33 {\r
34 }\r
35 \r
36 F32Image::F32Image (void)\r
37 : Array2dFile()\r
38 {\r
39         setPixelFormat (Array2dFile::PIXEL_FLOAT32);\r
40         setPixelSize (sizeof(kfloat32));\r
41 }\r
42 \r
43 F64Image::F64Image (int nx, int ny)\r
44 : Array2dFile (nx, ny, sizeof(kfloat64), Array2dFile::PIXEL_FLOAT64)\r
45 {\r
46 }\r
47 \r
48 F64Image::F64Image (void)\r
49 : Array2dFile ()\r
50 {\r
51         setPixelFormat (PIXEL_FLOAT64);\r
52         setPixelSize (sizeof(kfloat64));\r
53 }\r
54
55 void 
56 ImageFile::filterResponse (const char* const domainName, double bw, const char* const filterName, double filt_param)
57 {
58         int hx = (m_nx - 1) / 2;
59         int hy = (m_ny - 1) / 2;
60         ImageFileArray v = getArray();
61         SignalFilter filter (filterName, domainName, bw, filt_param);
62         
63         for (int i = -hx; i <= hx; i++) {
64                 for (int j = -hy; j <= hy; j++) {
65       double r = ::sqrt (i * i + j * j);
66                         
67                         v[i+hx][j+hy] = filter.response (r);
68                 }
69         }
70 }
71
72 int
73 ImageFile::display (void) const
74 {
75     double pmin, pmax;
76         
77     getMinMax (pmin, pmax);
78         
79     return (displayScaling (1, pmin, pmax));
80 }
81
82 int 
83 ImageFile::displayScaling (const int scale, const ImageFileValue pmin, const ImageFileValue pmax) const
84 {
85     int nx = m_nx;
86     int ny = m_ny;
87     ImageFileArrayConst v = getArray();
88     if (v == NULL || nx == 0 || ny == 0)
89                 return 0;
90         
91 #if HAVE_G2_H
92     int* pPens = new int [nx * ny * scale * scale ];
93         
94     double view_scale = 255 / (pmax - pmin);
95     int id_X11 = g2_open_X11 (nx * scale, ny * scale);
96     int grayscale[256];
97     for (int i = 0; i < 256; i++) {
98                 double cval = i / 255.;
99                 grayscale[i] = g2_ink (id_X11, cval, cval, cval);
100     }
101         
102     for (int iy = ny - 1; iy >= 0; iy--) {
103                 int iRowPos = ((ny - 1 - iy) * scale) * (nx * scale);
104                 for (int ix = 0; ix < nx; ix++) {
105                         int cval = static_cast<int>((v[ix][iy] - pmin) * view_scale);
106                         if (cval < 0)  
107                                 cval = 0;
108                         else if (cval > 255) 
109                                 cval = 255;
110                         for (int sy = 0; sy < scale; sy++)
111                                 for (int sx = 0; sx < scale; sx++)
112                                         pPens[iRowPos+(sy * nx * scale)+(sx + (ix * scale))] = grayscale[cval];
113                 }
114     }
115         
116     g2_image (id_X11, 0., 0., nx * scale, ny * scale, pPens);
117         
118     delete pPens;
119     return (id_X11);
120 #else
121     return 0;
122 #endif
123 }
124
125
126
127 // ImageFile::comparativeStatistics    Calculate comparative stats
128 //
129 // OUTPUT
130 //   d   Normalized root mean squared distance measure
131 //   r   Normalized mean absolute distance measure
132 //   e   Worst case distance measure
133 //
134 // REFERENCES
135 //  G.T. Herman, Image Reconstruction From Projections, 1980
136
137 bool
138 ImageFile::comparativeStatistics (const ImageFile& imComp, double& d, double& r, double& e) const
139 {
140     if (imComp.nx() != m_nx && imComp.ny() != m_ny) {
141                 sys_error (ERR_WARNING, "Image sizes differ [ImageFile::comparativeStatistics]");
142                 return false;
143     }
144     ImageFileArrayConst v = getArray();
145     if (v == NULL || m_nx == 0 || m_ny == 0)
146                 return false;
147         
148     ImageFileArrayConst vComp = imComp.getArray();
149         
150     double myMean = 0.;
151     for (unsigned int ix = 0; ix < m_nx; ix++) {
152                 for (unsigned int iy = 0; iy < m_ny; iy++) {
153                         myMean += v[ix][iy];
154                 }
155     }
156     myMean /= (m_nx * m_ny);
157         
158     double sqErrorSum = 0.;
159     double absErrorSum = 0.;
160     double sqDiffFromMeanSum = 0.;
161     double absValueSum = 0.;
162     for (unsigned int ix2 = 0; ix2 < m_nx; ix2++) {
163                 for (unsigned int iy = 0; iy < m_ny; iy++) {
164                         double diff = v[ix2][iy] - vComp[ix2][iy];
165                         sqErrorSum += diff * diff;
166                         absErrorSum += fabs(diff);
167                         double diffFromMean = v[ix2][iy] - myMean;
168                         sqDiffFromMeanSum += diffFromMean * diffFromMean;
169                         absValueSum += fabs(v[ix2][iy]);
170                 }
171     }
172         
173     d = ::sqrt (sqErrorSum / sqDiffFromMeanSum);
174     r = absErrorSum / absValueSum;
175         
176     int hx = m_nx / 2;
177     int hy = m_ny / 2;
178     double eMax = -1;
179     for (int ix3 = 0; ix3 < hx; ix3++) {
180                 for (int iy = 0; iy < hy; iy++) {
181                         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]);
182                         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]);
183                         double error = fabs (avgPixel - avgPixelComp);
184                         if (error > eMax)
185                                 eMax = error;
186                 }
187     }
188         
189     e = eMax;
190         
191     return true;
192 }
193
194
195 bool
196 ImageFile::printComparativeStatistics (const ImageFile& imComp, std::ostream& os) const
197 {
198         double d, r, e;
199         
200         if (comparativeStatistics (imComp, d, r, e)) {
201                 os << "  Normalized root mean squared distance (d): " << d << std::endl;
202                 os << "      Normalized mean absolute distance (r): " << r << std::endl;
203                 os << "Worst case distance (2x2 pixel average) (e): " << e << std::endl;
204                 return true;
205         }
206         return false;
207 }
208
209
210 void
211 ImageFile::printStatistics (std::ostream& os) const
212 {
213     double min, max, mean, mode, median, stddev;
214         
215     statistics (min, max, mean, mode, median, stddev);
216         
217     os << "   min: " << min << std::endl;
218     os << "   max: " << max << std::endl;
219     os << "  mean: " << mean << std::endl;
220     os << "  mode: " << mode << std::endl;
221     os << "median: " << median << std::endl;
222     os << "stddev: " << stddev << std::endl;
223 }
224
225
226 void
227 ImageFile::statistics (double& min, double& max, double& mean, double& mode, double& median, double& stddev) const
228 {
229     int nx = m_nx;
230     int ny = m_ny;
231     ImageFileArrayConst v = getArray();
232     
233     if (v == NULL || nx == 0 || ny == 0)
234                 return;
235 \r
236         std::vector<double> vecImage;\r
237         int iVec = 0;\r
238         vecImage.resize (nx * ny);\r
239     for (int ix = 0; ix < nx; ix++) {\r
240                 for (int iy = 0; iy < ny; iy++)\r
241                         vecImage[iVec++] = v[ix][iy];\r
242         }\r
243 \r
244         vectorNumericStatistics (vecImage, nx * ny, min, max, mean, mode, median, stddev);\r
245 }
246
247
248 void
249 ImageFile::getMinMax (double& min, double& max) const
250 {
251     int nx = m_nx;
252     int ny = m_ny;
253     ImageFileArrayConst v = getArray();
254     
255     if (v == NULL || nx == 0 || ny == 0)
256                 return;
257         
258     min = v[0][0];
259     max = v[0][0];
260     for (int ix = 0; ix < nx; ix++) {
261                 for (int iy = 0; iy < ny; iy++) {
262                         if (v[ix][iy] > max)
263                                 max = v[ix][iy];
264                         if (v[ix][iy] < min)
265                                 min = v[ix][iy];
266                 }
267     }
268 }
269 \r
270 bool\r
271 ImageFile::subtractImages (const ImageFile& rRHS, ImageFile& result) const\r
272 {\r
273   if (m_nx != rRHS.nx() || m_ny != rRHS.ny() || m_nx != result.nx() || m_ny != result.ny()) {\r
274     sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::subtractImage]");\r
275     return false;\r
276   }\r
277 \r
278   ImageFileArrayConst vLHS = getArray();\r
279   ImageFileArrayConst vRHS = rRHS.getArray();\r
280   ImageFileArray vResult = result.getArray();\r
281 \r
282   for (int ix = 0; ix < m_nx; ix++) {\r
283     ImageFileColumnConst in1 = vLHS[ix];\r
284     ImageFileColumnConst in2 = vRHS[ix];\r
285     ImageFileColumn out = vResult[ix];\r
286     for (int iy = 0; iy < m_ny; iy++)\r
287         *out++ = *in1++ - *in2++;\r
288   }\r
289 \r
290     return true;\r
291 }\r
292
293 bool\r
294 ImageFile::addImages (const ImageFile& rRHS, ImageFile& result) const\r
295 {\r
296   if (m_nx != rRHS.nx() || m_ny != rRHS.ny() || m_nx != result.nx() || m_ny != result.ny()) {\r
297     sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::subtractImage]");\r
298     return false;\r
299   }\r
300 \r
301   ImageFileArrayConst vLHS = getArray();\r
302   ImageFileArrayConst vRHS = rRHS.getArray();\r
303   ImageFileArray vResult = result.getArray();\r
304 \r
305   for (int ix = 0; ix < m_nx; ix++) {\r
306     ImageFileColumnConst in1 = vLHS[ix];\r
307     ImageFileColumnConst in2 = vRHS[ix];\r
308     ImageFileColumn out = vResult[ix];\r
309     for (int iy = 0; iy < m_ny; iy++)\r
310         *out++ = *in1++ + *in2++;\r
311   }\r
312 \r
313     return true;\r
314 }\r
315 \r
316 bool\r
317 ImageFile::multiplyImages (const ImageFile& rRHS, ImageFile& result) const\r
318 {\r
319   if (m_nx != rRHS.nx() || m_ny != rRHS.ny() || m_nx != result.nx() || m_ny != result.ny()) {\r
320     sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::subtractImage]");\r
321     return false;\r
322   }\r
323 \r
324   ImageFileArrayConst vLHS = getArray();\r
325   ImageFileArrayConst vRHS = rRHS.getArray();\r
326   ImageFileArray vResult = result.getArray();\r
327 \r
328   for (int ix = 0; ix < m_nx; ix++) {\r
329     ImageFileColumnConst in1 = vLHS[ix];\r
330     ImageFileColumnConst in2 = vRHS[ix];\r
331     ImageFileColumn out = vResult[ix];\r
332     for (int iy = 0; iy < m_ny; iy++)\r
333         *out++ = *in1++ * *in2++;\r
334   }\r
335 \r
336     return true;\r
337 }\r
338 \r
339 bool\r
340 ImageFile::divideImages (const ImageFile& rRHS, ImageFile& result) const\r
341 {\r
342   if (m_nx != rRHS.nx() || m_ny != rRHS.ny() || m_nx != result.nx() || m_ny != result.ny()) {\r
343     sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::subtractImage]");\r
344     return false;\r
345   }\r
346 \r
347   ImageFileArrayConst vLHS = getArray();\r
348   ImageFileArrayConst vRHS = rRHS.getArray();\r
349   ImageFileArray vResult = result.getArray();\r
350 \r
351   for (int ix = 0; ix < m_nx; ix++) {\r
352     ImageFileColumnConst in1 = vLHS[ix];\r
353     ImageFileColumnConst in2 = vRHS[ix];\r
354     ImageFileColumn out = vResult[ix];\r
355     for (int iy = 0; iy < m_ny; iy++) {\r
356       if (*in2 != 0.)\r
357         *out++ = *in1++ / *in2++;\r
358       else\r
359         *out++ = 0;\r
360     }\r
361   }\r
362 \r
363     return true;\r
364 }\r
365 \r
366 \r
367 bool\r
368 ImageFile::invertPixelValues (ImageFile& result) const\r
369 {\r
370   if (m_nx != result.nx() || m_ny != result.ny()) {\r
371     sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");\r
372     return false;\r
373   }\r
374 \r
375   ImageFileArrayConst vLHS = getArray();\r
376   ImageFileArray vResult = result.getArray();\r
377 \r
378   for (int ix = 0; ix < m_nx; ix++) {\r
379     ImageFileColumnConst in = vLHS[ix];\r
380     ImageFileColumn out = vResult[ix];\r
381     for (int iy = 0; iy < m_ny; iy++)\r
382         *out++ = - *in++;\r
383   }\r
384 \r
385   return true;\r
386 }\r
387 \r
388 bool\r
389 ImageFile::sqrt (ImageFile& result) const\r
390 {\r
391   if (m_nx != result.nx() || m_ny != result.ny()) {\r
392     sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");\r
393     return false;\r
394   }\r
395 \r
396   ImageFileArrayConst vLHS = getArray();\r
397   ImageFileArray vResult = result.getArray();\r
398 \r
399   for (int ix = 0; ix < m_nx; ix++) {\r
400     ImageFileColumnConst in = vLHS[ix];\r
401     ImageFileColumn out = vResult[ix];\r
402     for (int iy = 0; iy < m_ny; iy++)\r
403       if (*in < 0)\r
404         *out++ = -::sqrt(-*in++);\r
405       else\r
406         *out++ = ::sqrt(*in++);\r
407   }\r
408 \r
409   return true;\r
410 }\r
411 \r
412 bool\r
413 ImageFile::log (ImageFile& result) const\r
414 {\r
415   if (m_nx != result.nx() || m_ny != result.ny()) {\r
416     sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");\r
417     return false;\r
418   }\r
419 \r
420   ImageFileArrayConst vLHS = getArray();\r
421   ImageFileArray vResult = result.getArray();\r
422 \r
423   for (int ix = 0; ix < m_nx; ix++) {\r
424     ImageFileColumnConst in = vLHS[ix];\r
425     ImageFileColumn out = vResult[ix];\r
426     for (int iy = 0; iy < m_ny; iy++)\r
427       if (*in <= 0)\r
428         *out++ = 0;\r
429       else\r
430         *out++ = ::log(*in++);\r
431   }\r
432 \r
433   return true;\r
434 }\r
435 \r
436 bool\r
437 ImageFile::exp (ImageFile& result) const\r
438 {\r
439   if (m_nx != result.nx() || m_ny != result.ny()) {\r
440     sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");\r
441     return false;\r
442   }\r
443 \r
444   ImageFileArrayConst vLHS = getArray();\r
445   ImageFileArray vResult = result.getArray();\r
446 \r
447   for (int ix = 0; ix < m_nx; ix++) {\r
448     ImageFileColumnConst in = vLHS[ix];\r
449     ImageFileColumn out = vResult[ix];\r
450     for (int iy = 0; iy < m_ny; iy++)\r
451       *out++ = ::exp (*in++);\r
452   }\r
453 \r
454   return true;\r
455 }\r
456 \r
457 bool\r
458 ImageFile::FFTMagnitude (ImageFile& result) const\r
459 {\r
460   if (m_nx != result.nx() || m_ny != result.ny()) {\r
461     sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");\r
462     return false;\r
463   }\r
464 \r
465   ImageFileArrayConst vLHS = getArray();\r
466   ImageFileArray vResult = result.getArray();\r
467 \r
468   int ix, iy;\r
469   double* pY = new double [m_ny];\r
470   std::complex<double>** complexOut = new std::complex<double>* [m_nx];\r
471   for (ix = 0; ix < m_nx; ix++)\r
472     complexOut[ix] = new std::complex<double> [m_ny];\r
473 \r
474   for (ix = 0; ix < m_nx; ix++) {\r
475     for (iy = 0; iy < m_ny; iy++)\r
476       pY[iy] = vLHS[ix][iy];\r
477     ProcessSignal::finiteFourierTransform (pY, complexOut[ix], m_ny,  ProcessSignal::FORWARD);\r
478   }\r
479   delete pY;\r
480 \r
481   std::complex<double>* pX = new std::complex<double> [m_nx];\r
482   std::complex<double>* complexOutCol = new std::complex<double> [m_nx];\r
483   for (iy = 0; iy < m_ny; iy++) {\r
484     for (ix = 0; ix < m_nx; ix++)\r
485       pX[ix] = complexOut[ix][iy];\r
486     ProcessSignal::finiteFourierTransform (pX, complexOutCol, m_nx, ProcessSignal::FORWARD);\r
487     for (ix = 0; ix < m_nx; ix++)\r
488       complexOut[ix][iy] = complexOutCol[ix];\r
489   }\r
490   delete [] pX;\r
491 \r
492   for (ix = 0; ix < m_nx; ix++)\r
493     ProcessSignal::shuffleFourierToNaturalOrder (complexOut[ix], m_ny);\r
494 \r
495   \r
496   for (iy = 0; iy < m_ny; iy++) {\r
497     for (ix = 0; ix < m_nx; ix++)\r
498       complexOutCol[ix] = complexOut[ix][iy];\r
499     ProcessSignal::shuffleFourierToNaturalOrder (complexOutCol, m_nx);;\r
500     for (ix = 0; ix < m_nx; ix++)\r
501       complexOut[ix][iy] = complexOutCol[ix];\r
502 \r
503   }\r
504   delete [] complexOutCol;\r
505 \r
506   for (ix = 0; ix < m_nx; ix++)\r
507     for (iy = 0; iy < m_ny; iy++)\r
508       vResult[ix][iy] = std::abs (complexOut[ix][iy]);\r
509 \r
510   for (ix = 0; ix < m_nx; ix++)\r
511     delete [] complexOut[ix];\r
512 \r
513   delete [] complexOut;\r
514 \r
515   return true;\r
516 }\r
517 \r
518 bool\r
519 ImageFile::FFTPhase (ImageFile& result) const\r
520 {\r
521   if (m_nx != result.nx() || m_ny != result.ny()) {\r
522     sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");\r
523     return false;\r
524   }\r
525 \r
526   ImageFileArrayConst vLHS = getArray();\r
527   ImageFileArray vResult = result.getArray();\r
528 \r
529   int ix, iy;\r
530   double* pY = new double [m_ny];\r
531   std::complex<double>** complexOut = new std::complex<double>* [m_nx];\r
532   for (ix = 0; ix < m_nx; ix++)\r
533     complexOut[ix] = new std::complex<double> [m_ny];\r
534 \r
535   for (ix = 0; ix < m_nx; ix++) {\r
536     for (iy = 0; iy < m_ny; iy++)\r
537       pY[iy] = vLHS[ix][iy];\r
538     ProcessSignal::finiteFourierTransform (pY, complexOut[ix], m_ny,  ProcessSignal::FORWARD);\r
539   }\r
540   delete pY;\r
541 \r
542   std::complex<double>* pX = new std::complex<double> [m_nx];\r
543   std::complex<double>* complexOutCol = new std::complex<double> [m_nx];\r
544   for (iy = 0; iy < m_ny; iy++) {\r
545     for (ix = 0; ix < m_nx; ix++)\r
546       pX[ix] = complexOut[ix][iy];\r
547     ProcessSignal::finiteFourierTransform (pX, complexOutCol, m_nx, ProcessSignal::FORWARD);\r
548     for (ix = 0; ix < m_nx; ix++)\r
549       complexOut[ix][iy] = complexOutCol[ix];\r
550   }\r
551   delete [] pX;\r
552 \r
553   for (ix = 0; ix < m_nx; ix++)\r
554     ProcessSignal::shuffleFourierToNaturalOrder (complexOut[ix], m_ny);\r
555 \r
556   \r
557   for (iy = 0; iy < m_ny; iy++) {\r
558     for (ix = 0; ix < m_nx; ix++)\r
559       complexOutCol[ix] = complexOut[ix][iy];\r
560     ProcessSignal::shuffleFourierToNaturalOrder (complexOutCol, m_nx);;\r
561     for (ix = 0; ix < m_nx; ix++)\r
562       complexOut[ix][iy] = complexOutCol[ix];\r
563 \r
564   }\r
565   delete [] complexOutCol;\r
566 \r
567   for (ix = 0; ix < m_nx; ix++)\r
568     for (iy = 0; iy < m_ny; iy++)\r
569       vResult[ix][iy] = atan (complexOut[ix][iy].imag / complexOut[ix][iy].real);\r
570 \r
571   for (ix = 0; ix < m_nx; ix++)\r
572     delete [] complexOut[ix];\r
573 \r
574   delete [] complexOut;\r
575 \r
576   return true;\r
577 }\r
578 \r
579 bool\r
580 ImageFile::square (ImageFile& result) const\r
581 {\r
582   if (m_nx != result.nx() || m_ny != result.ny()) {\r
583     sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");\r
584     return false;\r
585   }\r
586 \r
587   ImageFileArrayConst vLHS = getArray();\r
588   ImageFileArray vResult = result.getArray();\r
589 \r
590   for (int ix = 0; ix < m_nx; ix++) {\r
591     ImageFileColumnConst in = vLHS[ix];\r
592     ImageFileColumn out = vResult[ix];\r
593     for (int iy = 0; iy < m_ny; iy++) {\r
594         *out++ = *in * *in;\r
595         in++;\r
596     }\r
597   }\r
598 \r
599   return true;\r
600 }\r
601 \r
602 \r
603 void 
604 ImageFile::writeImagePGM (const char *outfile, int nxcell, int nycell, double densmin, double densmax)
605 {
606         FILE *fp;
607         int nx = m_nx;
608         int ny = m_ny;
609         ImageFileArray v = getArray();
610         
611         unsigned char* rowp = new unsigned char [nx * nxcell];
612         
613         if ((fp = fopen (outfile, "wb")) == NULL)
614                 return;
615         
616         fprintf(fp, "P5\n");
617         fprintf(fp, "%d %d\n", nx, ny);
618         fprintf(fp, "255\n");
619         
620         for (int irow = ny - 1; irow >= 0; irow--) {
621                 for (int icol = 0; icol < nx; icol++) {
622                         int pos = icol * nxcell;
623                         double dens = (v[icol][irow] - densmin) / (densmax - densmin);
624                         dens = clamp (dens, 0., 1.);
625                         for (int p = pos; p < pos + nxcell; p++) {
626                                 rowp[p] = static_cast<unsigned int> (dens * 255.);
627                         }
628                 }
629                 for (int ir = 0; ir < nycell; ir++) {
630                         for (int ic = 0; ic < nx * nxcell; ic++) 
631                                 fputc( rowp[ic], fp );
632                 }
633         }
634         \r
635         delete rowp;
636         fclose(fp);
637 }
638
639 void 
640 ImageFile::writeImagePGMASCII (const char *outfile, int nxcell, int nycell, double densmin, double densmax)
641 {
642         FILE *fp;
643         int nx = m_nx;
644         int ny = m_ny;
645         ImageFileArray v = getArray();
646         
647         unsigned char* rowp = new unsigned char [nx * nxcell];
648         
649         if ((fp = fopen (outfile, "wb")) == NULL)
650                 return;
651         
652         fprintf(fp, "P2\n");
653         fprintf(fp, "%d %d\n", nx, ny);
654         fprintf(fp, "255\n");
655         
656         for (int irow = ny - 1; irow >= 0; irow--) {
657                 for (int icol = 0; icol < nx; icol++) {
658                         int pos = icol * nxcell;
659                         double dens = (v[icol][irow] - densmin) / (densmax - densmin);
660                         dens = clamp (dens, 0., 1.);
661                         for (int p = pos; p < pos + nxcell; p++) {
662                                 rowp[p] = static_cast<unsigned int> (dens * 255.);
663                         }
664                 }
665                 for (int ir = 0; ir < nycell; ir++) {
666                         for (int ic = 0; ic < nx * nxcell; ic++) 
667                                 fprintf(fp, "%d ", rowp[ic]);
668                         fprintf(fp, "\n");
669                 }
670         }
671         \r
672         delete rowp;
673         fclose(fp);
674 }
675
676
677 #ifdef HAVE_PNG
678 void 
679 ImageFile::writeImagePNG (const char *outfile, int bitdepth, int nxcell, int nycell, double densmin, double densmax)
680 {
681         FILE *fp;
682         png_structp png_ptr;
683         png_infop info_ptr;
684         double max_out_level = (1 << bitdepth) - 1;
685         int nx = m_nx;
686         int ny = m_ny;
687         ImageFileArray v = getArray();
688         
689         unsigned char* rowp = new unsigned char [nx * nxcell * (bitdepth / 8)];
690         
691         if ((fp = fopen (outfile, "wb")) == NULL)
692                 return;
693         
694         png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
695         if (! png_ptr)
696                 return;
697         
698         info_ptr = png_create_info_struct(png_ptr);
699         if (! info_ptr) {
700                 png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
701                 fclose(fp);
702                 return;
703         }
704         
705         if (setjmp(png_ptr->jmpbuf)) {
706                 png_destroy_write_struct(&png_ptr, &info_ptr);
707                 fclose(fp);
708                 return;
709         }
710         
711         png_init_io(png_ptr, fp);
712         
713         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);
714         
715         png_write_info(png_ptr, info_ptr);
716         for (int irow = ny - 1; irow >= 0; irow--) {
717                 png_bytep row_pointer = rowp;
718                 
719                 for (int icol = 0; icol < nx; icol++) {
720                         int pos = icol * nxcell;
721                         double dens = (v[icol][irow] - densmin) / (densmax - densmin);
722                         dens = clamp (dens, 0., 1.);
723                         unsigned int outval = static_cast<unsigned int> (dens * max_out_level);
724                         
725                         for (int p = pos; p < pos + nxcell; p++) {
726                                 if (bitdepth == 8)
727                                         rowp[p] = outval;
728                                 else {
729                                         int rowpos = p * 2;
730                                         rowp[rowpos] = (outval >> 8) & 0xFF;
731                                         rowp[rowpos+1] = (outval & 0xFF);
732                                 }
733                         }
734                 }
735                 for (int ir = 0; ir < nycell; ir++)
736                         png_write_rows (png_ptr, &row_pointer, 1);
737         }
738         
739         png_write_end(png_ptr, info_ptr);
740         png_destroy_write_struct(&png_ptr, &info_ptr);
741         delete rowp;\r
742         
743         fclose(fp);
744 }
745 #endif
746
747 #ifdef HAVE_GD
748 #include "gd.h"
749 static const int N_GRAYSCALE=256;
750
751 void
752 ImageFile::writeImageGIF (const char *outfile, int nxcell, int nycell, double densmin, double densmax)
753 {
754         gdImagePtr gif;
755         FILE *out;
756         int gs_indices[N_GRAYSCALE];
757         int nx = m_nx;
758         int ny = m_ny;
759         ImageFileArray v = getArray();
760         
761         unsigned char rowp [nx * nxcell];
762         if (rowp == NULL)
763                 return;
764         
765         gif = gdImageCreate(nx * nxcell, ny * nycell);
766         for (int i = 0; i < N_GRAYSCALE; i++)
767                 gs_indices[i] = gdImageColorAllocate(gif, i, i, i);
768         
769         int lastrow = ny * nycell - 1;
770         for (int irow = 0; irow < ny; irow++) {
771                 int rpos = irow * nycell;
772                 for (int ir = rpos; ir < rpos + nycell; ir++) {
773                         for (int icol = 0; icol < nx; icol++) {
774                                 int cpos = icol * nxcell;
775                                 double dens = (v[icol][irow] - densmin) / (densmax - densmin);
776                                 dens = clamp(dens, 0., 1.);
777                                 for (int ic = cpos; ic < cpos + nxcell; ic++) {
778                                         rowp[ic] = (unsigned int) (dens * (double) (N_GRAYSCALE - 1));
779                                         gdImageSetPixel(gif, ic, lastrow - ir, gs_indices[rowp[ic]]);
780                                 }
781                         }
782                 }
783         }
784         
785         if ((out = fopen(outfile,"w")) == NULL) {
786                 sys_error(ERR_FATAL, "Error opening output file %s for writing", outfile);
787                 return (1);
788         }
789         gdImageGif(gif,out);
790         fclose(out);
791         gdImageDestroy(gif);
792 }
793 #endif
794