+#ifdef HAVE_PNG
+bool
+ImageFile::readImagePNG (const char* const pszFile)
+{
+ FILE* fp = fopen(pszFile, "rb");
+ if (!fp)
+ return false;
+ unsigned char header[8];
+ fread (header, 1, 8, fp);
+ if (png_sig_cmp (header, 0, 8)) {
+ fclose (fp);
+ return false;
+ }
+
+ png_structp png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
+ if (!png_ptr) {
+ fclose(fp);
+ return false;
+ }
+
+ png_infop info_ptr = png_create_info_struct(png_ptr);
+ if (!info_ptr) {
+ png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
+ fclose(fp);
+ return false;
+ }
+
+ png_infop end_info = png_create_info_struct(png_ptr);
+ if (!end_info) {
+ png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
+ fclose(fp);
+ return false;
+ }
+
+ if (setjmp(png_ptr->jmpbuf)) {
+ png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
+ fclose(fp);
+ return false;
+ }
+
+ png_init_io(png_ptr, fp);
+ png_set_sig_bytes(png_ptr, 8);
+ png_read_info(png_ptr, info_ptr);
+
+ int width = png_get_image_width (png_ptr, info_ptr);
+ int height = png_get_image_height (png_ptr, info_ptr);
+ int bit_depth = png_get_bit_depth (png_ptr, info_ptr);
+ int color_type = png_get_color_type (png_ptr, info_ptr);
+
+ if (color_type == PNG_COLOR_TYPE_PALETTE && bit_depth <= 8)
+ png_set_expand(png_ptr);
+
+ if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
+ png_set_expand(png_ptr);
+
+ if (bit_depth < 8)
+ png_set_packing(png_ptr);
+
+ if (color_type & PNG_COLOR_MASK_ALPHA)
+ png_set_strip_alpha(png_ptr);
+
+ if (bit_depth == 16)
+ png_set_swap(png_ptr); // convert to little-endian format
+
+ png_read_update_info(png_ptr, info_ptr); // update with transformations
+ int rowbytes = png_get_rowbytes (png_ptr, info_ptr);
+ bit_depth = png_get_bit_depth (png_ptr, info_ptr);
+ color_type = png_get_color_type (png_ptr, info_ptr);
+
+ png_bytep* row_pointers = new png_bytep [height];
+ int i;
+ for (i = 0; i < height; i++)
+ row_pointers[i] = new unsigned char [rowbytes];
+
+ png_read_image(png_ptr, row_pointers);
+
+ setArraySize (width, height);
+ ImageFileArray v = getArray();
+ for (int iy = 0; iy < height; iy++) {
+ for (int ix = 0; ix < width; ix++) {
+ double dV;
+ if (color_type == PNG_COLOR_TYPE_GRAY) {
+ if (bit_depth == 8)
+ dV = row_pointers[iy][ix] / 255.;
+ else if (bit_depth == 16) {
+ int iBase = ix * 2;
+ dV = (row_pointers[iy][iBase] + (row_pointers[iy][iBase+1] << 8)) / 65536.;
+ }
+ } else if (color_type == PNG_COLOR_TYPE_RGB) {
+ if (bit_depth == 8) {
+ int iBase = ix * 3;
+ double dR = row_pointers[iy][iBase] / 255.;
+ double dG = row_pointers[iy][iBase+1] / 255.;
+ double dB = row_pointers[iy][iBase+2] / 255.;
+ dV = colorToGrayscale (dR, dG, dR);
+ }
+ }
+ v[ix][height-iy-1] = dV;
+ }
+ }
+
+ png_read_end(png_ptr, end_info);
+ png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
+
+ for (i = 0; i < height; i++)
+ delete row_pointers[i];
+ delete row_pointers;
+
+ fclose (fp);
+ return true;
+}
+#endif
+
+bool
+ImageFile::exportImage (const char* const pszFormat, const char* const pszFilename, int nxcell, int nycell, double densmin, double densmax)
+{
+ int iFormatID = convertExportFormatNameToID (pszFormat);
+
+ if (iFormatID == EXPORT_FORMAT_PGM)
+ return writeImagePGM (pszFilename, nxcell, nycell, densmin, densmax);
+ else if (iFormatID == EXPORT_FORMAT_PGMASCII)
+ return writeImagePGMASCII (pszFilename, nxcell, nycell, densmin, densmax);
+ else if (iFormatID == EXPORT_FORMAT_PNG)
+ return writeImagePNG (pszFilename, 8, nxcell, nycell, densmin, densmax);
+ else if (iFormatID == EXPORT_FORMAT_PNG16)
+ return writeImagePNG (pszFilename, 16, nxcell, nycell, densmin, densmax);
+
+ sys_error (ERR_SEVERE, "Invalid format %s [ImageFile::exportImage]", pszFormat);
+ return false;
+}
+
+
+bool
+ImageFile::writeImagePGM (const char* const outfile, int nxcell, int nycell, double densmin, double densmax)
+{
+ FILE *fp;
+ int nx = m_nx;
+ int ny = m_ny;
+ ImageFileArray v = getArray();
+
+ unsigned char* rowp = new unsigned char [nx * nxcell];
+
+ if ((fp = fopen (outfile, "wb")) == NULL)
+ return false;
+
+ fprintf(fp, "P5\n");
+ fprintf(fp, "%d %d\n", nx, ny);
+ fprintf(fp, "255\n");
+
+ for (int irow = ny - 1; irow >= 0; irow--) {
+ for (int icol = 0; icol < nx; icol++) {
+ int pos = icol * nxcell;
+ double dens = (v[icol][irow] - densmin) / (densmax - densmin);
+ dens = clamp (dens, 0., 1.);
+ for (int p = pos; p < pos + nxcell; p++) {
+ rowp[p] = static_cast<unsigned int> (dens * 255.);
+ }
+ }
+ for (int ir = 0; ir < nycell; ir++) {
+ for (int ic = 0; ic < nx * nxcell; ic++)
+ fputc( rowp[ic], fp );
+ }
+ }
+
+ delete rowp;
+ fclose(fp);
+
+ return true;
+}
+
+bool
+ImageFile::writeImagePGMASCII (const char* const outfile, int nxcell, int nycell, double densmin, double densmax)
+{
+ FILE *fp;
+ int nx = m_nx;
+ int ny = m_ny;
+ ImageFileArray v = getArray();
+
+ unsigned char* rowp = new unsigned char [nx * nxcell];
+
+ if ((fp = fopen (outfile, "wb")) == NULL)
+ return false;
+
+ fprintf(fp, "P2\n");
+ fprintf(fp, "%d %d\n", nx, ny);
+ fprintf(fp, "255\n");
+
+ for (int irow = ny - 1; irow >= 0; irow--) {
+ for (int icol = 0; icol < nx; icol++) {
+ int pos = icol * nxcell;
+ double dens = (v[icol][irow] - densmin) / (densmax - densmin);
+ dens = clamp (dens, 0., 1.);
+ for (int p = pos; p < pos + nxcell; p++) {
+ rowp[p] = static_cast<unsigned int> (dens * 255.);
+ }
+ }
+ for (int ir = 0; ir < nycell; ir++) {
+ for (int ic = 0; ic < nx * nxcell; ic++)
+ fprintf(fp, "%d ", rowp[ic]);
+ fprintf(fp, "\n");
+ }
+ }
+
+ delete rowp;
+ fclose(fp);
+
+ return true;
+}
+
+
+#ifdef HAVE_PNG
+bool
+ImageFile::writeImagePNG (const char* const outfile, int bitdepth, int nxcell, int nycell, double densmin, double densmax)
+{
+ double max_out_level = (1 << bitdepth) - 1;
+ int nx = m_nx;
+ int ny = m_ny;
+ ImageFileArray v = getArray();
+
+ unsigned char* rowp = new unsigned char [nx * nxcell * (bitdepth / 8)];
+
+ FILE *fp = fopen (outfile, "wb");
+ if (! fp)
+ return false;
+
+ png_structp png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
+ if (! png_ptr)
+ return false;
+
+ png_infop info_ptr = png_create_info_struct (png_ptr);
+ if (! info_ptr) {
+ png_destroy_write_struct (&png_ptr, (png_infopp) NULL);
+ fclose (fp);
+ return false;
+ }
+
+ if (setjmp (png_ptr->jmpbuf)) {
+ png_destroy_write_struct (&png_ptr, &info_ptr);
+ fclose (fp);
+ return false;
+ }
+
+ png_init_io(png_ptr, fp);
+
+ 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);
+
+ png_write_info(png_ptr, info_ptr);
+ for (int irow = ny - 1; irow >= 0; irow--) {
+ png_bytep row_pointer = rowp;
+
+ for (int icol = 0; icol < nx; icol++) {
+ int pos = icol * nxcell;
+ double dens = (v[icol][irow] - densmin) / (densmax - densmin);
+ dens = clamp (dens, 0., 1.);
+ unsigned int outval = static_cast<unsigned int> (dens * max_out_level);
+
+ for (int p = pos; p < pos + nxcell; p++) {
+ if (bitdepth == 8)
+ rowp[p] = outval;
+ else {
+ int rowpos = p * 2;
+ rowp[rowpos+1] = (outval >> 8) & 0xFF;
+ rowp[rowpos] = (outval & 0xFF);
+ }
+ }
+ }
+ for (int ir = 0; ir < nycell; ir++)
+ png_write_rows (png_ptr, &row_pointer, 1);
+ }
+
+ png_write_end (png_ptr, info_ptr);
+ png_destroy_write_struct (&png_ptr, &info_ptr);
+ delete rowp;
+
+ fclose(fp);
+
+ return true;
+}
+#endif
+
+#ifdef HAVE_GD
+#include "gd.h"
+static const int N_GRAYSCALE=256;
+
+bool
+ImageFile::writeImageGIF (const char* const outfile, int nxcell, int nycell, double densmin, double densmax)
+{
+ int gs_indices[N_GRAYSCALE];
+ int nx = m_nx;
+ int ny = m_ny;
+ ImageFileArray v = getArray();
+
+ unsigned char* rowp = new unsigned char [nx * nxcell];
+
+ gdImagePtr gif = gdImageCreate(nx * nxcell, ny * nycell);
+ for (int i = 0; i < N_GRAYSCALE; i++)
+ gs_indices[i] = gdImageColorAllocate(gif, i, i, i);
+
+ int lastrow = ny * nycell - 1;
+ for (int irow = 0; irow < ny; irow++) {
+ int rpos = irow * nycell;
+ for (int ir = rpos; ir < rpos + nycell; ir++) {
+ for (int icol = 0; icol < nx; icol++) {
+ int cpos = icol * nxcell;
+ double dens = (v[icol][irow] - densmin) / (densmax - densmin);
+ dens = clamp(dens, 0., 1.);
+ for (int ic = cpos; ic < cpos + nxcell; ic++) {
+ rowp[ic] = (unsigned int) (dens * (double) (N_GRAYSCALE - 1));
+ gdImageSetPixel(gif, ic, lastrow - ir, gs_indices[rowp[ic]]);
+ }
+ }
+ }
+ }
+
+ FILE *out;
+ if ((out = fopen (outfile,"w")) == NULL) {
+ sys_error(ERR_SEVERE, "Error opening output file %s for writing", outfile);
+ return false;
+ }
+ gdImageGif(gif,out);
+ fclose(out);
+ gdImageDestroy(gif);
+
+ delete rowp;
+
+ return true;
+}
+#endif