-void
-sdf2d_to_pgm (ImageFile& im, char *outfile, int nxcell, int nycell, double densmin, double densmax)
-{
- FILE *fp;
- int nx = im.nx();
- int ny = im.ny();
- ImageFileArray v = im.getArray();
-
- unsigned char rowp [nx * nxcell];
-
- if ((fp = fopen (outfile, "wb")) == NULL)
- return;
-
- 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++)
- fprintf(fp, "%c ", rowp[ic]);
- }
- }
-
- fclose(fp);
-}
-
-void
-sdf2d_to_pgmasc (ImageFile& im, char *outfile, int nxcell, int nycell, double densmin, double densmax)
-{
- FILE *fp;
- int nx = im.nx();
- int ny = im.ny();
- ImageFileArray v = im.getArray();
-
- unsigned char rowp [nx * nxcell];
- if (rowp == NULL)
- return;
-
- if ((fp = fopen (outfile, "wb")) == NULL)
- return;
-
- 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");
- }
- }
-
- fclose(fp);
-}
-
-
-#ifdef HAVE_PNG
-void
-sdf2d_to_png (ImageFile& im, char *outfile, int bitdepth, int nxcell, int nycell, double densmin, double densmax)
-{
- FILE *fp;
- png_structp png_ptr;
- png_infop info_ptr;
- double max_out_level = (1 << bitdepth) - 1;
- int nx = im.nx();
- int ny = im.ny();
- ImageFileArray v = im.getArray();
-
- unsigned char rowp [nx * nxcell * (bitdepth / 8)];
-
- if ((fp = fopen (outfile, "wb")) == NULL)
- return;
-
- png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
- if (! png_ptr)
- return;
-
- info_ptr = png_create_info_struct(png_ptr);
- if (! info_ptr) {
- png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
- fclose(fp);
- return;
- }
-
- if (setjmp(png_ptr->jmpbuf)) {
- png_destroy_write_struct(&png_ptr, &info_ptr);
- fclose(fp);
- return;
- }
-
- 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] = (outval >> 8) & 0xFF;
- rowp[rowpos+1] = (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);
-
- fclose(fp);
-}
-#endif
-
-#ifdef HAVE_GD
-#include "gd.h"
-static const int N_GRAYSCALE=256;
-#endif
-
-void
-sdf2d_to_gif (ImageFile& im, char *outfile, int nxcell, int nycell, double densmin, double densmax)
-{
-#ifdef HAVE_GD
- gdImagePtr gif;
- FILE *out;
- int gs_indices[N_GRAYSCALE];
- int nx = im.nx();
- int ny = im.ny();
- ImageFileArray v = im.getArray();
-
- unsigned char rowp [nx * nxcell];
- if (rowp == NULL)
- return;
-
- 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]]);
- }
- }
- }
- }
-
- if ((out = fopen(outfile,"w")) == NULL) {
- sys_error(ERR_FATAL, "Error opening output file %s for writing", outfile);
- return (1);
- }
- gdImageGif(gif,out);
- fclose(out);
- gdImageDestroy(gif);
-#else
- cout << "This version does not support GIF" << endl;
-#endif
-}
-