/*--------------------------------------------------------------------
 *	$Id: psconvert.c 19521 2017-12-27 22:38:24Z pwessel $
 *
 *	Copyright (c) 1991-2018 by P. Wessel, W. H. F. Smith, R. Scharroo, J. Luis and F. Wobbe
 *	See LICENSE.TXT file for copying and redistribution conditions.
 *
 *	This program is free software; you can redistribute it and/or modify
 *	it under the terms of the GNU Lesser General Public License as published by
 *	the Free Software Foundation; version 3 or any later version.
 *
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU Lesser General Public License for more details.
 *
 *	Contact info: gmt.soest.hawaii.edu
 *--------------------------------------------------------------------*/
/*
 *
 * Brief synopsis: psconvert converts one or several PostScript file(s) to other formats using GhostScript.
 * It works by modifying the page size in order that the image will have a size
 * which is specified by the BoundingBox.
 * As an option, a tight BoundingBox may be computed.
 * psconvert uses the ideas of the EPS2XXX.m from Primoz Cermelj published in MatLab Central
 * and of psbbox.sh of Remko Scharroo.
 *
 *
 *--------------------------------------------------------------------*/
/*
 * Authors:	Joaquim Luis and Remko Scharroo
 * Date:	1-JAN-2010
 * Version:	5 API
 */

#include "gmt_dev.h"
#define THIS_MODULE_NAME	"psconvert"
#define THIS_MODULE_LIB		"core"
#define THIS_MODULE_PURPOSE	"Convert [E]PS file(s) to other formats using GhostScript"
#define THIS_MODULE_KEYS	"<X{+,FI)"
#define THIS_MODULE_NEEDS	""
#define THIS_MODULE_OPTIONS "-V"

#ifdef _WIN32
#	define dup2 _dup2
	/* Various shared-library functions declared in gmt_sharedlibs.c */
	EXTERN_MSC void *dlopen (const char *module_name, int mode);
	EXTERN_MSC int dlclose (void *handle);
	EXTERN_MSC void *dlsym (void *handle, const char *name);
	EXTERN_MSC char *dlerror (void);
#	ifndef RTLD_LAZY
#		define RTLD_LAZY 1
#	endif
#else	/* Standard Unix things */
#	include <dlfcn.h>
#endif

EXTERN_MSC void gmt_str_toupper (char *string);
EXTERN_MSC void gmt_handle5_plussign (struct GMT_CTRL *GMT, char *in, char *mods, unsigned way);

#ifdef WIN32	/* Special for Windows */
#	include <windows.h>
#	include <process.h>
#	define getpid _getpid
#	define dup2 _dup2
#	define execv _execv
#	define pipe _pipe
	static char quote = '\"';
	static char *squote = "\"";
#else
	static char quote = '\'';
	static char *squote = "\'";
	struct popen2 {
		int fd[2];		/* The input [0] and output [1] file descriptors */
		int n_closed;	/* Number of times we have called gmt_pclose */
	    pid_t child_pid;	/* Pid of child */
	};
#endif

enum GMT_GS_Devices {
	GS_DEV_EPS = 0,
	GS_DEV_PDF,
	GS_DEV_SVG,
	GS_DEV_JPG,
	GS_DEV_PNG,
	GS_DEV_PPM,
	GS_DEV_TIF,
	GS_DEV_BMP,
	GS_DEV_TPNG,      /* PNG with transparency */
	GS_DEV_JPGG,      /* These are grayscale versions */
	GS_DEV_PNGG,
	GS_DEV_TIFG,
	GS_DEV_BMPG,
	N_GS_DEVICES};	/* Number of supported GS output devices */

enum GMT_KML_Elevation {
	KML_GROUND_ABS = 0,
	KML_GROUND_REL,
	KML_ABS,
	KML_SEAFLOOR_REL,
	KML_SEAFLOOR_ABS,
	N_KML_ELEVATIONS};

#define add_to_list(list,item) { if (list[0]) strcat (list, " "); strcat (list, item); }
#define add_to_qlist(list,item) { if (list[0]) strcat (list, " "); strcat (list, squote);  strcat (list, item); strcat (list, squote); }

struct PS2RASTER_CTRL {
	struct PS2R_In {	/* Input file info */
		unsigned int n_files;
	} In;
	struct PS2R_A {             /* -A[u][-] [Adjust boundingbox] */
		bool active;
		bool max;          /* Only scale if dim exceeds the given size */
		bool round;        /* Round HiRes BB instead of ceil */
		bool strip;        /* Remove the -U time-stamp */
		bool reset;        /* The -A- turns -A off, overriding any automode in effect */
		bool resize;       /* Resize to a user selected size */
		bool rescale;      /* Resize to a user selected scale factor */
		bool outline;      /* Draw frame around plot with selected pen [0.25p] */
		bool paint;        /* Paint box behind plot with selected fill */
		double scale;      /* Scale factor to go along with the 'rescale' option */
		double new_size[2];
		double margin[4];
		double new_dpi_x, new_dpi_y;
		struct GMT_PEN pen;
		struct GMT_FILL fill;
	} A;
	struct PS2R_C {	/* -C<option> */
		bool active;
		char arg[GMT_LEN256];
	} C;
	struct PS2R_D {	/* -D<dir> */
		bool active;
		char *dir;
	} D;
	struct PS2R_E {	/* -E<resolution> */
		bool active;
		unsigned int dpi;
	} E;
	struct PS2R_F {	/* -F<out_name> */
		bool active;
		char *file;
	} F;
	struct PS2R_G {	/* -G<GSpath> */
		bool active;
		char *file;
	} G;
	struct PS2R_I {	/* -I */
		bool active;
	} I;
	struct PS2R_L {	/* -L<listfile> */
		bool active;
		char *file;
	} L;
	struct PS2R_P {	/* -P */
		bool active;
	} P;
	struct PS2R_Q {	/* -Q[g|t]<bits> */
		bool active;
		bool on[2];	/* [0] for graphics, [1] for text antialiasing */
		unsigned int bits[2];
	} Q;
	struct PS2R_S {	/* -S */
		bool active;
	} S;
	struct PS2R_T {	/* -T */
		bool active;
		int eps;	/* 1 if we want to make EPS, -1 with setpagedevice (possibly in addition to another format) */
		int ps;		/* 1 if we want to save the final PS under "modern" setting */
		int device;	/* May be negative */
	} T;
	struct PS2R_W {	/* -W -- for world file production */
		bool active;
		bool folder;
		bool warp;
		bool kml;
		unsigned int mode;	/* 0 = clamp at ground, 1 is relative to ground, 2 is absolute 3 is relative to seafloor, 4 is clamp at seafloor */
		int min_lod, max_lod;	/* minLodPixels and maxLodPixels settings */
		int min_fade, max_fade;	/* minFadeExtent and maxFadeExtent settings */
		char *doctitle;		/* Name of KML document */
		char *overlayname;	/* Name of the image overlay */
		char *URL;		/* URL of remote site */
		char *foldername;	/* Name of KML folder */
		double altitude;
	} W;
	struct PS2R_Z { /* -Z */
		bool active;
	} Z;
};

#ifdef WIN32	/* Special for Windows */
	GMT_LOCAL int ghostbuster(struct GMTAPI_CTRL *API, struct PS2RASTER_CTRL *C);
#else
	/* Abstraction to get popen to do bidirectional read/write */
struct popen2 * gmt_popen2 (const char *cmdline) {
	struct popen2 *F = NULL;
	/* Must implement a bidirectional popen instead */
    pid_t p;
    int pipe_stdin[2] = {0, 0}, pipe_stdout[2] = {0, 0};

    if (pipe(pipe_stdin))  return NULL;
    if (pipe(pipe_stdout)) return NULL;

    printf("pipe_stdin[0] = %d,  pipe_stdin[1]  = %d\n", pipe_stdin[0], pipe_stdin[1]);
    printf("pipe_stdout[0] = %d, pipe_stdout[1] = %d\n", pipe_stdout[0], pipe_stdout[1]);

    if ((p = fork()) < 0) return NULL; /* Fork failed */

    if(p == 0) { /* child */
        close (pipe_stdin[1]);
        dup2 (pipe_stdin[0], 0);
        close (pipe_stdout[0]);
        dup2 (pipe_stdout[1], 1);
        execl ("/bin/sh", "sh", "-c", cmdline, NULL);
        perror ("execl"); exit (99);
    }
	/* Return the file handles back via structure */
	F = calloc (1, sizeof (struct popen2));
    F->child_pid = p;
    F->fd[1] = pipe_stdin[1];
    F->fd[0] = pipe_stdout[0];
    return F; 
}

#ifndef _WIN32
#include <signal.h>
#include <sys/wait.h>
#endif

void gmt_pclose2 (struct popen2 **Faddr, int dir) {
	struct popen2 *F = *Faddr;
	F->n_closed++;
	close (F->fd[dir]);	/* Close this pipe */
	if (F->n_closed == 2) {	/* Done so free object */
		/* Done, kill child */
#ifndef _WIN32
    	printf("kill(%d, 0) -> %d\n", F->child_pid, kill(F->child_pid, 0)); 
    	printf("waitpid() -> %d\n", waitpid(F->child_pid, NULL, 0));
    	printf("kill(%d, 0) -> %d\n", F->child_pid, kill(F->child_pid, 0));
#endif
		free (F);
		*Faddr = NULL;
	}
}
#endif

GMT_LOCAL int parse_A_settings (struct GMT_CTRL *GMT, char *arg, struct PS2RASTER_CTRL *Ctrl) {
	/* Syntax: -A[u][<margins>][-][+r][+s|S[m]<width>[u][/<height>[u]]] */

	bool error = false;
	unsigned int pos = 0;
	int j, k = 0, trim_j = -1;
	char txt[GMT_LEN128] = {""}, p[GMT_LEN128] = {""};
	char txt_a[GMT_LEN64] = {""}, txt_b[GMT_LEN64] = {""}, txt_c[GMT_LEN64] = {""}, txt_d[GMT_LEN64] = {""};

	Ctrl->A.active = true;

	if (arg[k] == 'u') {Ctrl->A.strip = true; k++;}
	/* If there are +modifiers later we need to temporarily disable them: */
	for (j = k; arg[j] && arg[j] != '+'; j++);
	if (arg[j] == '+') arg[j] = 0, trim_j = j;
	if (*arg != '\0' && arg[strlen(arg)-1] == '-') {
		Ctrl->A.reset = true;
		return (error);
	}
	if (arg[k] && arg[k] != '+') {	/* Also specified margin(s) */
		j = sscanf (&arg[k], "%[^/]/%[^/]/%[^/]/%s", txt_a, txt_b, txt_c, txt_d);
		switch (j) {
			case 1:	/* Got uniform margin */
				Ctrl->A.margin[XLO] = Ctrl->A.margin[XHI] = Ctrl->A.margin[YLO] = Ctrl->A.margin[YHI] = gmt_M_to_points (GMT, txt_a);
				break;
			case 2:	/* Got seprate x/y margins */
				Ctrl->A.margin[XLO] = Ctrl->A.margin[XHI] = gmt_M_to_points (GMT, txt_a);
				Ctrl->A.margin[YLO] = Ctrl->A.margin[YHI] = gmt_M_to_points (GMT, txt_b);
				break;
			case 4:	/* Got uniform margin */
				Ctrl->A.margin[XLO] = gmt_M_to_points (GMT, txt_a);
				Ctrl->A.margin[XHI] = gmt_M_to_points (GMT, txt_b);
				Ctrl->A.margin[YLO] = gmt_M_to_points (GMT, txt_c);
				Ctrl->A.margin[YHI] = gmt_M_to_points (GMT, txt_d);
				break;
			default:
				error++;
				GMT_Report (Ctrl, GMT_MSG_NORMAL, "-A: Give 1, 2, or 4 margins\n");
				break;
		}
	}
	if (trim_j >= 0) arg[trim_j] = '+';	/* Restore the chopped off section */
	strncpy (txt, arg, GMT_LEN128-1);
	while (!error && (gmt_strtok (txt, "+", &pos, p))) {
		switch (p[0]) {
			case 'p':	/* Draw outline */
				Ctrl->A.outline = true;
				if (!p[1])
					Ctrl->A.pen = GMT->current.setting.map_default_pen;
				else if (gmt_getpen (GMT, &p[1], &Ctrl->A.pen)) {
					gmt_pen_syntax (GMT, 'A', "sets background outline pen attributes", 0);
					error++;
				}
				break;
			case 'g':	/* Fill background */
				Ctrl->A.paint = true;
				if (!p[1]) {
					GMT_Report (Ctrl, GMT_MSG_NORMAL, "-A+g: Append the background fill\n");
					error++;
				}
				else if (gmt_getfill (GMT, &p[1], &Ctrl->A.fill)) {
					gmt_pen_syntax (GMT, 'A', "sets background fill attributes", 0);
					error++;
				}
				break;
			case 'r':	/* Round */
				Ctrl->A.round = true;
				break;
			case 'S':	/* New size via a scale factor */
				Ctrl->A.rescale = true;
				Ctrl->A.scale = atof(&p[1]);
				break;
			case 's':	/* New size +s[m]<width>[u][/<height>[u]] */
				Ctrl->A.resize = true;
				if (p[1] == 'm') { Ctrl->A.max = true, k = 2;} else k = 1;
				j = sscanf (&p[k], "%[^/]/%s", txt_a, txt_b);
				switch (j) {
					case 1:	/* Got width only. Height will be computed later */
						Ctrl->A.new_size[0] = gmt_M_to_points (GMT, txt_a);
						break;
					case 2:	/* Got separate width/height */
						Ctrl->A.new_size[0] = gmt_M_to_points (GMT, txt_a);
						Ctrl->A.new_size[1] = gmt_M_to_points (GMT, txt_b);
						break;
					default:
						GMT_Report (Ctrl, GMT_MSG_NORMAL, "Syntax error -A+s[m]<width[/height]>: Wrong size parameters\n");
						error++;
						break;
					}
				break;
		}
	}

	if (Ctrl->A.rescale && Ctrl->A.resize) {
		GMT_Report (Ctrl, GMT_MSG_NORMAL, "Syntax error -A+s|S: Cannot set both -A+s and -A+S\n");
		error++;
	}
	else if (Ctrl->A.rescale)    /* But we can. This makes the coding simpler later on */
		Ctrl->A.resize = true;

	return (error);
}

GMT_LOCAL int parse_GE_settings (struct GMT_CTRL *GMT, char *arg, struct PS2RASTER_CTRL *C) {
	/* Syntax: -W[+g][+k][+t<doctitle>][+n<layername>][+a<altmode>][+l<lodmin>/<lodmax>] */

	unsigned int pos = 0, error = 0;
	char p[GMT_LEN256] = {""};
	gmt_M_unused(GMT);

	C->W.active = true;
	while (gmt_getmodopt (GMT, 'W', arg, "afgklnotu", &pos, p, &error) && error == 0) {	/* Looking for +a, etc */
		switch (p[0]) {
			case 'a':	/* Altitude setting */
				switch (p[1]) {	/* Check which altitude mode we selected */
					case 'G':
						C->W.mode = KML_GROUND_ABS;
						break;
					case 'g':
						C->W.mode = KML_GROUND_REL;
						C->W.altitude = atof (&p[2]);
						break;
					case 'A':
						C->W.mode = KML_ABS;
						C->W.altitude = atof (&p[2]);
						break;
					case 's':
						C->W.mode = KML_SEAFLOOR_REL;
						C->W.altitude = atof (&p[2]);
						break;
					case 'S':
						C->W.mode = KML_SEAFLOOR_ABS;
						break;
					default:
						GMT_Report (C, GMT_MSG_NORMAL, "Syntax error -W+a<mode>[par]: Unrecognized altitude mode %c\n", p[1]);
						error++;
						break;
				}
				break;
			case 'f':	/* Set fading options in KML */
				sscanf (&p[1], "%d/%d", &C->W.min_fade, &C->W.max_fade);
				break;
			case 'g':	/* Use gdal to make geotiff */
				C->W.warp = true;
				break;
			case 'k':	/* Produce a KML file */
				C->W.kml = true;
				break;
			case 'l':	/* Set KML level of detail for image */
				sscanf (&p[1], "%d/%d", &C->W.min_lod, &C->W.max_lod);
				break;
			case 'n':	/* Set KML document layer name */
				gmt_M_str_free (C->W.overlayname);	/* Already set, free then reset */
				C->W.overlayname = gmt_assign_text (GMT, p);
				break;
			case 'o':	/* Produce a KML overlay as a folder subset */
				C->W.folder = true;
				gmt_M_str_free (C->W.foldername);	/* Already set, free then reset */
				C->W.foldername = gmt_assign_text (GMT, p);
				break;
			case 't':	/* Set KML document title */
				gmt_M_str_free (C->W.doctitle);	/* Already set, free then reset */
				C->W.doctitle = gmt_assign_text (GMT, p);
				break;
			case 'u':	/* Specify a remote address for image */
				gmt_M_str_free (C->W.URL);	/* Already set, free then reset */
				C->W.URL = gmt_assign_text (GMT, p);
				break;
			default:
				GMT_Report (C, GMT_MSG_NORMAL, "Syntax error -W+<opt>: Unrecognized option selection %c\n", p[1]);
				error++;
				break;
		}
	}
	return (error);
}

GMT_LOCAL void *New_Ctrl (struct GMT_CTRL *GMT) {	/* Allocate and initialize a new control structure */
	struct PS2RASTER_CTRL *C;

	C = gmt_M_memory (GMT, NULL, 1, struct PS2RASTER_CTRL);

	/* Initialize values whose defaults are not 0/false/NULL */
#ifdef WIN32
	if (ghostbuster(GMT->parent, C) != GMT_NOERROR)  /* Try first to find the gspath from registry */
		C->G.file = strdup ("gswin64c");     /* Fall back to this default and expect a miracle */
#else
	C->G.file = strdup ("gs");
#endif
	C->D.dir = strdup (".");

	C->W.doctitle = strdup ("GMT KML Document");
	C->W.overlayname = strdup ("GMT Image Overlay");
	C->W.foldername = strdup ("GMT Image Folder");

	return (C);
}

GMT_LOCAL void Free_Ctrl (struct GMT_CTRL *GMT, struct PS2RASTER_CTRL *C) {	/* Deallocate control structure */
	if (!C) return;
	gmt_M_str_free (C->D.dir);
	gmt_M_str_free (C->F.file);
	gmt_M_str_free (C->G.file);
	gmt_M_str_free (C->L.file);
	gmt_M_str_free (C->W.doctitle);
	gmt_M_str_free (C->W.overlayname);
	gmt_M_str_free (C->W.foldername);
	gmt_M_str_free (C->W.URL);
	gmt_M_free (GMT, C);
}

GMT_LOCAL int usage (struct GMTAPI_CTRL *API, int level) {
	gmt_show_name_and_purpose (API, THIS_MODULE_LIB, THIS_MODULE_NAME, THIS_MODULE_PURPOSE);
	if (level == GMT_MODULE_PURPOSE) return (GMT_NOERROR);
	GMT_Message (API, GMT_TIME_NONE, "usage: psconvert <psfile1> <psfile2> <...> -A[u][<margins>][-][+p[<pen>]][+g<fill>][+r][+s[m]|S<width[u]>[/<height>[u]]]\n");
	GMT_Message (API, GMT_TIME_NONE, "\t[-C<gs_command>] [-D<dir>] [-E<resolution>] [-F<out_name>] [-G<gs_path>] [-L<listfile>]\n");
	GMT_Message (API, GMT_TIME_NONE, "\t[-P] [-Q[g|t]1|2|4] [-S] [-Tb|e|E|f|F|g|G|j|m|s|t] [%s]\n", GMT_V_OPT);
	GMT_Message (API, GMT_TIME_NONE, "\t[-W[+a<mode>[<alt]][+f<minfade>/<maxfade>][+g][+k][+l<lodmin>/<lodmax>][+n<name>][+o<folder>][+t<title>][+u<URL>]]\n");
	if (API->GMT->current.setting.run_mode == GMT_CLASSIC)
		GMT_Message (API, GMT_TIME_NONE, "\t[-Z]\n");
	GMT_Message (API, GMT_TIME_NONE, "\n");

	if (level == GMT_SYNOPSIS) return (GMT_MODULE_SYNOPSIS);

	GMT_Message (API, GMT_TIME_NONE, "\tWorks by modifying the page size in order that the resulting\n");
	GMT_Message (API, GMT_TIME_NONE, "\timage will have the size specified by the BoundingBox.\n");
	GMT_Message (API, GMT_TIME_NONE, "\tAs an option, a tight BoundingBox may be computed.\n\n");
	GMT_Message (API, GMT_TIME_NONE, "\t<psfile(s)> PostScript file(s) to be converted.\n");
	if (API->external)
		GMT_Message (API, GMT_TIME_NONE, "\tTo access the current internal GMT plot, specify <psfile> as \"=\".\n");
	GMT_Message (API, GMT_TIME_NONE, "\n\tOPTIONS:\n");
	GMT_Message (API, GMT_TIME_NONE, "\t-A Adjust the BoundingBox to the minimum required by the image contents.\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   Append u to strip out time-stamps (produced by GMT -U options).\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   Append - to make sure -A is NOT activated by -W.\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   Optionally, append margin(s) to adjusted BoundingBox.\n");
	GMT_Message (API, GMT_TIME_NONE, "\t     -A<off>[u] sets uniform margin for all 4 sides.\n");
	GMT_Message (API, GMT_TIME_NONE, "\t     -A<xoff>[u]/<yoff>[u] set separate x- and y-margins.\n");
	GMT_Message (API, GMT_TIME_NONE, "\t     -A<woff>[u]/<eoff>[u]/<soff>[u]/<noff>[u] set separate w-,e-,s-,n-margins.\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   Use the -A+s[m]<width[u]>[/<height>[u]] option the select a new image size\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   but maintaining the DPI set by -E (ghostscript does the re-interpolation work).\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   Add +g<paint> to paint the BoundingBox [no paint].\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   Add +p[<pen>] to outline the BoundingBox [%s].\n",
	             gmt_putpen (API->GMT, &API->GMT->current.setting.map_default_pen));
	GMT_Message (API, GMT_TIME_NONE, "\t   Use +sm to only change size if figure size exceeds the new maximum size(s).\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   Append unit u (%s) [%c].\n",
	             GMT_DIM_UNITS_DISPLAY, &API->GMT->session.unit_name[API->GMT->current.setting.proj_length_unit][0]);
	GMT_Message (API, GMT_TIME_NONE, "\t   Alternatively use -A+S<scale> to scale the image by the <scale> factor.\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   Use -A+r to force rounding of HighRes BoundingBox instead of ceil.\n");
	GMT_Message (API, GMT_TIME_NONE, "\t-C Specify a single, custom option that will be passed on to GhostScript\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   as is. Repeat to add several options [none].\n");
	GMT_Message (API, GMT_TIME_NONE, "\t-D Set an alternative output directory (which must exist)\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   [Default is same directory as PS files].\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   Use -D. to place the output in the current directory.\n");
	GMT_Message (API, GMT_TIME_NONE, "\t-E Set raster resolution in dpi [default = 720 for PDF, 300 for others].\n");
	GMT_Message (API, GMT_TIME_NONE, "\t-F Force the output file name. By default output names are constructed\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   using the input names as base, which are appended with an appropriate\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   extension. Use this option to provide a different name, but WITHOUT\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   extension. Extension is still determined automatically.\n");
	GMT_Message (API, GMT_TIME_NONE, "\t-G Full path to your ghostscript executable.\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   NOTE: Under Unix systems this is generally not necessary.\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   Under Windows, ghostscript path is fished from the registry.\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   If this fails you can still add the GS path to system's path\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   or give the full path here.\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   (e.g., -Gc:\\programs\\gs\\gs9.02\\bin\\gswin64c).\n");
	GMT_Message (API, GMT_TIME_NONE, "\t-I Ghostscript versions >= 9.00 change gray-shades by using ICC profiles.\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   GS 9.05 and above provide the '-dUseFastColor=true' option to prevent that\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   and that is what psconvert does by default, unless option -I is set.\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   Note that for GS >= 9.00 and < 9.05 the gray-shade shifting is applied\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   to all but PDF format. We have no solution to offer other than ... upgrade GS\n");
	GMT_Message (API, GMT_TIME_NONE, "\t-L The <listfile> is an ASCII file with names of files to be converted.\n");
	GMT_Message (API, GMT_TIME_NONE, "\t-P Force Portrait mode. All Landscape mode plots will be rotated back\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   so that they show unrotated in Portrait mode.\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   This is practical when converting to image formats or preparing\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   EPS or PDF plots for inclusion in documents.\n");
	GMT_Message (API, GMT_TIME_NONE, "\t-Q Anti-aliasing setting for (g)raphics or (t)ext; append size (1,2,4)\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   of sub-sampling box.\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   Default is no anti-aliasing, which is the same as specifying size 1.\n");
	GMT_Message (API, GMT_TIME_NONE, "\t-S Apart from executing it, also writes the ghostscript command to\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   standard error and keeps all intermediate files.\n");
	GMT_Message (API, GMT_TIME_NONE, "\t-T Set output format [default is jpeg]:\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   b means BMP.\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   e means EPS.\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   E means EPS with setpagedevice command.\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   f means PDF.\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   F means multi-page PDF (requires -F).\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   g means PNG.\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   G means PNG (transparent where nothing is plotted).\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   j means JPEG.\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   m means PPM.\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   s means SVG [if supported by your ghostscript version].\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   t means TIF.\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   For b, g, j, t, append - to get a grayscale image [24-bit color].\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   The EPS format can be combined with any of the other formats.\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   For example, -Tef creates both an EPS and PDF file.\n");
	GMT_Message (API, GMT_TIME_NONE, "\t-V Provide progress report [default is silent] and shows the\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   gdal_translate command, in case you want to use this program\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   to create a geoTIFF file.\n");
	GMT_Message (API, GMT_TIME_NONE, "\t-W Write a ESRI type world file suitable to make (e.g.,) .tif files be\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   recognized as geotiff by software that know how to do it. Be aware,\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   however, that different results are obtained depending on the image\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   contents and if the -B option has been used or not. The trouble with\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   -B is that it creates a frame and very likely its annotations and\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   that introduces pixels outside the map data extent. As a consequence,\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   the map extents estimation will be wrong. To avoid this problem, use\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   the --MAP_FRAME_TYPE=inside option which plots all annotations related\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   stuff inside the image and does not compromise the coordinate.\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   computations. The world file naming follows the convention of jamming\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   a 'w' in the file extension. So, if the output is tif (-Tt) the world\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   file is a .tfw, for jpeg a .jgw, and so on.\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   Use -W+g to do a system call to gdal_translate and produce a true\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   geoTIFF image right away. The output file will have the extension\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   .tiff. See the man page for other 'gotchas'. Automatically sets -A -P.\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   Use -W+k to create a minimalist KML file that allows loading the\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   image in Google Earth. Note that for this option the image must be\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   in geographical coordinates. If not, a warning is issued but the\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   KML file is created anyway.\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   Several modifiers allow you to specify the content in the KML file:\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   +a<altmode>[<altitude>] sets the altitude mode of this layer, where\n");
	GMT_Message (API, GMT_TIME_NONE, "\t      <altmode> is one of 5 recognized by Google Earth:\n");
	GMT_Message (API, GMT_TIME_NONE, "\t      G clamped to the ground [Default].\n");
	GMT_Message (API, GMT_TIME_NONE, "\t      g Append altitude (in m) relative to ground.\n");
	GMT_Message (API, GMT_TIME_NONE, "\t      A Append absolute altitude (in m).\n");
	GMT_Message (API, GMT_TIME_NONE, "\t      s Append altitude (in m) relative to seafloor.\n");
	GMT_Message (API, GMT_TIME_NONE, "\t      S clamped to the seafloor.\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   +f<minfade>/<maxfade>] sets distances over which we fade from opaque\n");
	GMT_Message (API, GMT_TIME_NONE, "\t     to transparent [no fading].\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   +l<minLOD>/<maxLOD>] sets Level Of Detail when layer should be\n");
	GMT_Message (API, GMT_TIME_NONE, "\t     active [always active]. Image goes inactive when there are fewer\n");
	GMT_Message (API, GMT_TIME_NONE, "\t     than minLOD pixels or more than maxLOD pixels visible.\n");
	GMT_Message (API, GMT_TIME_NONE, "\t     -1 means never invisible.\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   +n<layername> sets the name of this particular layer\n");
	GMT_Message (API, GMT_TIME_NONE, "\t     [\"GMT Image Overlay\"].\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   +o<foldername> sets the name of this particular folder\n");
	GMT_Message (API, GMT_TIME_NONE, "\t     [\"GMT Image Folder\"].  This yields a KML snipped without header/trailer.\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   +t<doctitle> sets the document name [\"GMT KML Document\"].\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   +u<URL> prepands this URL to the name of the image referenced in the\n");
	GMT_Message (API, GMT_TIME_NONE, "\t     KML [local file].\n");
	GMT_Message (API, GMT_TIME_NONE, "\t   Escape any +? modifier inside strings with \\.\n");
	if (API->GMT->current.setting.run_mode == GMT_CLASSIC)
		GMT_Message (API, GMT_TIME_NONE, "\t-Z Remove input PostScript file(s) after successful conversion.\n");

	return (GMT_MODULE_USAGE);
}

GMT_LOCAL int parse (struct GMT_CTRL *GMT, struct PS2RASTER_CTRL *Ctrl, struct GMT_OPTION *options) {
	/* This parses the options provided to psconvert and sets parameters in CTRL.
	 * Any GMT common options will override values set previously by other commands.
	 * It also replaces any file names specified as input or output with the data ID
	 * returned when registering these sources/destinations with the API.
	 */

	unsigned int n_errors = 0, mode;
	int j;
	bool grayscale;
	struct GMT_OPTION *opt = NULL;

	for (opt = options; opt; opt = opt->next) {
		switch (opt->option) {

			case '<':	/* Input files [Allow for file "=" under API calls] */
				if (!(GMT->parent->external && !strncmp (opt->arg, "=", 1))) {	/* Can check if file is sane */
					if (!gmt_check_filearg (GMT, '<', opt->arg, GMT_IN, GMT_IS_TEXTSET)) n_errors++;
				}
				Ctrl->In.n_files++;
				break;

			/* Processes program-specific parameters */

			case 'A':	/* Adjust BoundingBox: -A[u][<margins>][-][+r][+s<width>[u][/<height>[u]]] or -A- */
				n_errors += parse_A_settings (GMT, opt->arg, Ctrl);
				break;
			case 'C':	/* Append extra custom GS options */
				strcat (Ctrl->C.arg, " ");
				strncat (Ctrl->C.arg, opt->arg, GMT_LEN256-1);	/* Append to list of extra GS options */
				break;
			case 'D':	/* Change output directory */
				if ((Ctrl->D.active = gmt_check_filearg (GMT, 'D', opt->arg, GMT_OUT, GMT_IS_TEXTSET)) != 0) {
					gmt_M_str_free (Ctrl->D.dir);
					Ctrl->D.dir = strdup (opt->arg);
				}
				else
					n_errors++;
				break;
			case 'E':	/* Set output dpi */
				Ctrl->E.active = true;
				Ctrl->E.dpi = atoi (opt->arg);
				break;
			case 'F':	/* Set explicitly the output file name */
				if ((Ctrl->F.active = gmt_check_filearg (GMT, 'F', opt->arg, GMT_OUT, GMT_IS_TEXTSET)) != 0) {
					Ctrl->F.file = strdup (opt->arg);
					if (!gmt_M_file_is_memory (Ctrl->F.file)) gmt_chop_ext (Ctrl->F.file);	/* Make sure file name has no extension */
				}
				else
					n_errors++;
				break;
			case 'G':	/* Set GS path */
				if ((Ctrl->G.active = gmt_check_filearg (GMT, 'G', opt->arg, GMT_IN, GMT_IS_TEXTSET)) != 0) {
					gmt_M_str_free (Ctrl->G.file);
					Ctrl->G.file = malloc (strlen (opt->arg)+3);	/* Add space for quotes */
					sprintf (Ctrl->G.file, "%c%s%c", quote, opt->arg, quote);
				}
				else
					n_errors++;
				break;
			case 'I':	/* Do not use the ICC profile when converting gray shades */
				Ctrl->I.active = true;
				break;
			case 'L':	/* Give list of files to convert */
				if ((Ctrl->L.active = gmt_check_filearg (GMT, 'L', opt->arg, GMT_IN, GMT_IS_TEXTSET)) != 0)
					Ctrl->L.file = strdup (opt->arg);
				else
					n_errors++;
				break;
			case 'P':	/* Force Portrait mode */
				Ctrl->P.active = true;
				break;
			case 'Q':	/* Anti-aliasing settings */
				Ctrl->Q.active = true;
				if (opt->arg[0] == 'g')
					mode = 0;
				else if (opt->arg[0] == 't')
					mode = 1;
				else {
					gmt_default_error (GMT, opt->option);
					n_errors++;
					continue;

				}
				Ctrl->Q.on[mode] = true;
				Ctrl->Q.bits[mode] = (opt->arg[1]) ? atoi (&opt->arg[1]) : 4;
				break;
			case 'S':	/* Write the GS command to STDERR */
				Ctrl->S.active = true;
				break;
			case 'T':	/* Select output format (optionally also request EPS) */
				Ctrl->T.active = true;
				grayscale = ((j = (int)strlen(opt->arg)) > 1 && opt->arg[j-1] == '-');
				for (j = 0; opt->arg[j]; j++) {
					switch (opt->arg[j]) {
						case 'e':	/* EPS */
							Ctrl->T.eps = 1;
							break;
						case 'E':	/* EPS with setpagedevice */
							Ctrl->T.eps = -1;
							break;
						case 'f':	/* PDF */
							Ctrl->T.device = GS_DEV_PDF;
							break;
						case 'F':	/* PDF (multipages) */
							Ctrl->T.device = -GS_DEV_PDF;
							break;
						case 'b':	/* BMP */
							Ctrl->T.device = (grayscale) ? GS_DEV_BMPG : GS_DEV_BMP;
							break;
						case 'j':	/* JPEG */
							Ctrl->T.device = (grayscale) ? GS_DEV_JPGG : GS_DEV_JPG;
							break;
						case 'g':	/* PNG */
							Ctrl->T.device = (grayscale) ? GS_DEV_PNGG : GS_DEV_PNG;
							break;
						case 'G':	/* PNG (transparent) */
							Ctrl->T.device = GS_DEV_TPNG;
							break;
						case 'm':	/* PPM */
							Ctrl->T.device = GS_DEV_PPM;
							break;
						case 'p':	/* PS */
							Ctrl->T.ps = 1;
							break;
						case 's':	/* SVG */
							Ctrl->T.device = GS_DEV_SVG;
							break;
						case 't':	/* TIFF */
							Ctrl->T.device = (grayscale) ? GS_DEV_TIFG : GS_DEV_TIF;
							break;
						case '-':	/* Just skip the trailing - for grayscale since it is handled separately */
							break;
						default:
							gmt_default_error (GMT, opt->option);
							n_errors++;
							break;
					}
				}
				break;
			case 'W':	/* Save world file */
				n_errors += parse_GE_settings (GMT, opt->arg, Ctrl);
				break;

			case 'Z':
				if (GMT->current.setting.run_mode == GMT_CLASSIC)
					Ctrl->Z.active = true;
				else {
					GMT_Report (GMT->parent, GMT_MSG_NORMAL, "Error: The -Z option is not available in MODERN mode!\n");
					n_errors++;
				}
				break;

			default:	/* Report bad options */
				n_errors += gmt_default_error (GMT, opt->option);
				break;
		}
	}

	if (!Ctrl->T.active) Ctrl->T.device = GS_DEV_JPG;	/* Default output device if none is specified */

	n_errors += gmt_M_check_condition (GMT, Ctrl->Q.on[0] && (Ctrl->Q.bits[0] < 1 || Ctrl->Q.bits[0] > 4),
	                                   "Syntax error: Anti-aliasing for graphics requires sub-sampling box of 1,2, or 4\n");

	n_errors += gmt_M_check_condition (GMT, Ctrl->Q.on[1] && (Ctrl->Q.bits[1] < 1 || Ctrl->Q.bits[1] > 4),
	                                   "Syntax error: Anti-aliasing for text requires sub-sampling box of 1,2, or 4\n");

	n_errors += gmt_M_check_condition (GMT, Ctrl->In.n_files > 1 && Ctrl->L.active,
	                                   "Syntax error: Cannot handle both a file list and multiple ps files in input\n");

	n_errors += gmt_M_check_condition (GMT, Ctrl->L.active && access (Ctrl->L.file, R_OK),
	                                   "Error: Cannot read list file %s\n", Ctrl->L.file);

	n_errors += gmt_M_check_condition (GMT, Ctrl->T.device == -GS_DEV_PDF && !Ctrl->F.active,
	                                   "Syntax error: Creation of Multipage PDF requires setting -F option\n");

	n_errors += gmt_M_check_condition (GMT, Ctrl->L.active && (GMT->current.setting.run_mode == GMT_MODERN),
	                                   "Error: Cannot use -L for list file under modern GMT mode\n");

	n_errors += gmt_M_check_condition (GMT, Ctrl->In.n_files > 1 && (GMT->current.setting.run_mode == GMT_MODERN),
	                                   "Syntax error: No listed input files allowed under modern GMT mode\n");

	n_errors += gmt_M_check_condition (GMT, !Ctrl->F.active && (GMT->current.setting.run_mode == GMT_MODERN),
	                                   "Syntax error: Modern GMT mode requires the -F option\n");

	return (n_errors ? GMT_PARSE_ERROR : GMT_NOERROR);
}

GMT_LOCAL int64_t file_line_reader (struct GMT_CTRL *GMT, char **L, size_t *size, FILE *fp, char *notused1, uint64_t *notused2) {
	/* file_line_reader gets the next logical record from filepointer fp and passes it back via L.
	 * We use this function since PS files may be shitty and contain a mixture of \r or \n
	 * to indicate end of a logical record.
	 * all_PS and pos are not used.
	 * Empty lines are suppressed.
	 * If the last end-of-line is missing, the last line is still produced with an end-of-line.
	 */
	int c;
	int64_t out = 0;
	char *line = *L;
	if (notused1 == NULL){};			/* Just to shut up a compiler warning of "unreferenced formal parameter" */
	if (notused2 == NULL){};			/* 		""		*/
	while ((c = fgetc (fp)) > 0) {	/* Keep reading until End-Of-File */
		if (c == '\r' || c == '\n') {	/* Got logical end of record */
			if (!out) continue; /* Nothing in buffer ... skip */
			line[out] = '\0';	/* Terminate output string */
			return out;	/* Return number of characters in L */
		}
		/* Got a valid character in current record */
		if ((size_t)out == (*size-1)) {	/* Need to extend our buffer; the -1 makes room for an \0 as needed to end a string */
			(*size) <<= 1;	/* Double the current buffer space */
			line = *L = gmt_M_memory (GMT, *L, *size, char);
		}
		line[out++] = (char)c;	/* Add this char to our buffer */
	}
	if (!out) return EOF;	/* Nothing in buffer ... exit with EOF */
	line[out] = '\0';	/* Terminate output string */
	return out;			/* Return number of characters in L. The next call to the routine will return EOF. */
}

void file_rewind (FILE *fp, uint64_t *notused) {	/* Rewinds to start of file */
	if (notused == NULL){};			/* Just to shut up a compiler warning of "unreferenced formal parameter" */
	rewind (fp);
}

#define bailout(code) {gmt_M_free_options (mode); return (code);}
#define Return(code) {Free_Ctrl (GMT, Ctrl); gmt_end_module (GMT, GMT_cpy); bailout (code);}

GMT_LOCAL inline char *alpha_bits (struct PS2RASTER_CTRL *Ctrl) {
	/* return alpha bits which are valid for the selected driver */
	static char alpha[48];
	char *c = alpha;
	unsigned int bits;
	*alpha = '\0'; /* reset string */
	if (Ctrl->Q.on[0]) {
		if (Ctrl->T.device == GS_DEV_PDF || Ctrl->T.device == -GS_DEV_PDF)
			/* Note: cannot set GraphicsAlphaBits > 1 with a vector device */
			bits = 1;
		else
			bits = Ctrl->Q.bits[0];
		sprintf (c, " -dGraphicsAlphaBits=%d", bits);
		c = strrchr(c, '\0'); /* advance to end of string */
	}
	if (Ctrl->Q.on[1]) {
		if (Ctrl->T.device == GS_DEV_PDF || Ctrl->T.device == -GS_DEV_PDF)
			/* Note: cannot set GraphicsAlphaBits > 1 with a vector device */
			bits = 1;
		else
			bits = Ctrl->Q.bits[1];
		sprintf (c, " -dTextAlphaBits=%d", bits);
	}
	return alpha;
}

GMT_LOCAL void possibly_fill_or_outline_BoundingBox (struct GMT_CTRL *GMT, struct PS2R_A *A, FILE *fp) {
	/* Check if user wanted to paint or outline the BoundingBox - otherwise do nothing */
	char *ptr = NULL;
	GMT->PSL->internal.dpp = PSL_DOTS_PER_INCH / 72.0;	/* Dots pr. point resolution of output device, set here since no PSL initialization */
	if (A->paint) {	/* Paint the background of the page */
		GMT_Report (GMT->parent, GMT_MSG_LONG_VERBOSE, "Paint background BoundingBox using paint %s\n", gmt_putrgb (GMT, A->fill.rgb));
		if (GMT->PSL->internal.comments) fprintf (fp, "%% Paint background BoundingBox using paint %s\n", gmt_putrgb (GMT, A->fill.rgb));
		ptr = PSL_makecolor (GMT->PSL, A->fill.rgb);
		fprintf (fp, "gsave clippath %s F N U\n", ptr);
	}
	if (A->outline) {	/* Draw the outline of the page */
		GMT_Report (GMT->parent, GMT_MSG_LONG_VERBOSE, "Outline background BoundingBox using pen %s\n", gmt_putpen (GMT, &A->pen));
		if (GMT->PSL->internal.comments) fprintf (fp, "%% Outline background BoundingBox using pen %s\n", gmt_putpen (GMT, &A->pen));
		/* Double pen thickness since half will be clipped by BoundingBox */
		ptr = PSL_makepen (GMT->PSL, 2.0*A->pen.width, A->pen.rgb, A->pen.style, A->pen.offset);
		fprintf (fp, "gsave %s clippath S U\n", ptr);
	}
}

/* ---------------------------------------------------------------------------------------------- */
GMT_LOCAL int pipe_HR_BB(struct GMTAPI_CTRL *API, struct PS2RASTER_CTRL *Ctrl, char *gs_BB, double margin, double *w, double *h) {
	/* Do what we do in the main code for the -A option but on a in-memory PS 'file' */
	char      cmd[GMT_LEN256] = {""}, buf[GMT_LEN128] = {""}, t[32] = {""}, *pch, c;
	int       fh, r, c_begin = 0;
	size_t    n;
	bool      landscape = false;
	double    x0, y0, x1, y1, xt, yt;
	struct GMT_POSTSCRIPT *PS = NULL;
#ifdef _WIN32
	int       fd[2] = { 0, 0 };
	FILE     *fp = NULL;
#else
	struct popen2 *H = NULL;
#endif

	sprintf (cmd, "%s %s %s -", Ctrl->G.file, gs_BB, Ctrl->C.arg);	/* Set up gs command */

#ifdef _WIN32
	if (_pipe(fd, 512, O_BINARY) == -1) {
		GMT_Report (API, GMT_MSG_NORMAL, "Error: failed to open the pipe.\n");
		return GMT_RUNTIME_ERROR;
	}
	if (dup2 (fd[1], fileno (stderr)) < 0) {
		GMT_Report (API, GMT_MSG_NORMAL, "Error: Failed to duplicate pipe.\n");
		return GMT_RUNTIME_ERROR;
	}
	if (close (fd[1]) == -1) { 		/* Close original write end of pipe */
		GMT_Report (API, GMT_MSG_NORMAL, "Error: failed to close write end of pipe.\n");
		return GMT_RUNTIME_ERROR;
	}
	if ((fp = popen (cmd, "w")) == NULL) {	/* Failed popen-job, exit */
		GMT_Report(API, GMT_MSG_NORMAL, "Cannot execute GhostScript command.\n");
		return GMT_RUNTIME_ERROR;
	}
#else
	if ((H = gmt_popen2 (cmd)) == NULL) {	/* Failed popen-job, exit */
		GMT_Report(API, GMT_MSG_NORMAL, "Cannot execute GhostScript command.\n");
		return GMT_RUNTIME_ERROR;
	}
#endif

	/* Allocate GMT_POSTSCRIPT struct to hold the string that lives inside GMT->PSL */
	PS = gmt_M_memory (API->GMT, NULL, 1, struct GMT_POSTSCRIPT);
	PS->data = PSL_getplot (API->GMT->PSL);		/* Get pointer to the internal plot buffer */
	PS->n_bytes = API->GMT->PSL->internal.n;	/* Length of plot buffer; note P->n_alloc = 0 since nothing was allocated here */

	/* Send the PS down ghostscripts throat */
#ifdef _WIN32
	fwrite (PS->data, sizeof(char), PS->n_bytes, fp);
	fflush (fp);
	if (pclose (fp) == -1)
		GMT_Report(API, GMT_MSG_NORMAL, "Error closing pipe used for GhostScript command.\n");
	fh = fd[0];	/* File handle for reading */
#else
 	write (H->fd[1], PS->data, PS->n_bytes);
	/* Now closed for writing */
    gmt_pclose2 (&H, 1);
	fh = H->fd[0];	/* File handle for reading */
#endif

	/* Now read the image from input pipe fd[0] */
	while (read(fh, t, 1U) && t[0] != '\n'); 	/* Consume first line that has the BoundingBox */
	n = 0;
	while (read(fh, t, 1U) && t[0] != '\n')		/* Read second line which has the HiResBoundingBox */
		buf[n++] = t[0];
#ifdef _WIN32
	if (close (fd[0]) == -1) { 		/* Close read end of pipe */
		GMT_Report (API, GMT_MSG_NORMAL, "Error: failed to close read end of pipe.\n");
		gmt_M_free (API->GMT, PS);
		return GMT_RUNTIME_ERROR;
	}
#else
	/* Now closed for reading */
 	gmt_pclose2 (&H, 0);
#endif

	sscanf (buf, "%s %lf %lf %lf %lf", t, &x0, &y0, &x1, &y1);
	c = PS->data[500];
	PS->data[500] = '\0';			/* Temporary cut the string to not search the whole file */
	pch = strstr(PS->data, "Landscape");
	if (pch != NULL) landscape = true;
	PS->data[500] = c;				/* Restore the deleted character */

	if (landscape)					/* We will need these in pipe_ghost() */
		xt = -x1, yt = -y0, *w = y1-y0, *h = x1-x0, r = -90;
	else
		xt = -x0, yt = -y0, *w = x1-x0, *h = y1-y0, r = 0;

	/* Add a margin if user requested it (otherwise margin = 0) */
	*w += 2 * margin;       *h += 2 * margin;
	xt += margin;           yt += margin;

	sprintf (buf, "BoundingBox: 0 0 %.0f %.0f", ceil(*w), ceil(*h));
	if ((pch = strstr (PS->data, "BoundingBox")) != NULL) {		/* Find where is the BB */
		for (n = 0; n < strlen(buf); n++)                       /* and update it */
			pch[n] = buf[n];
		while (pch[n] != '\n') pch[n++] = ' ';                  /* Make sure that there are only spaces till EOL */
	}
	else
		GMT_Report (API, GMT_MSG_NORMAL, "Warning: Something very odd the GMT PS does not have a %%BoundingBox\n");

	sprintf (buf, "HiResBoundingBox: 0 0 %.4f %.4f", *w, *h);
	if ((pch = strstr(PS->data, "HiResBoundingBox")) != NULL) {	/* Find where is the HiResBB */
		for (n = 0; n < strlen(buf); n++)                       /* and update it */
			pch[n] = buf[n];
	}
	else
		GMT_Report (API, GMT_MSG_NORMAL, "Warning: Something very odd the GMT PS does not have a %%HiResBoundingBox\n");

	/* Find where is the setpagedevice line */
	if ((pch = strstr(PS->data, "setpagedevice")) != NULL) {
		while (pch[c_begin] != '\n') c_begin--;		c_begin++;	/* It receded one too much */
		/* So now we know where the line starts. Put a 'translate' command in its place. */
		(r == 0) ? sprintf(buf, "%.3f %.3f translate", xt, yt) : sprintf(buf, "%d rotate\n%.3f %.3f translate", r, xt, yt);
		for (n = 0; n < strlen(buf); n++, c_begin++) pch[c_begin] = buf[n];
		while (pch[c_begin] != '\n')  pch[c_begin++] = ' ';     /* Make sure that there are only spaces till EOL */
	}
	else {
		if ((pch = strstr(PS->data, " translate")) != NULL) {		/* If user runs through this function twice 'setpagedevice' was changed to 'translate' */
			double old_xt, old_yt;
			while (pch[c_begin] != '\n') c_begin--;		c_begin++;	/* Goto line start but it receded one too much */
			sscanf (&pch[c_begin], "%lf %lf", &old_xt, &old_yt);
			(r == 0) ? sprintf(buf, "%.3f %.3f translate", xt + old_xt, yt + old_xt) : sprintf(buf, "%d rotate\n%.3f %.3f translate", r, xt + old_xt, yt + old_xt);
			for (n = 0; n < strlen(buf); n++, c_begin++) pch[c_begin] = buf[n];
			while (pch[c_begin] != '\n') pch[c_begin++] = ' ';
		}
		else
			GMT_Report(API, GMT_MSG_NORMAL, "Warning: Something very odd the GMT PS does not have the setpagedevice line\n");
	}

	gmt_M_free (API->GMT, PS);

	return GMT_NOERROR;
}
/* ---------------------------------------------------------------------------------------------- */

GMT_LOCAL int pipe_ghost (struct GMTAPI_CTRL *API, struct PS2RASTER_CTRL *Ctrl, char *gs_params, double w, double h, char *out_file) {
	/* Run the command that converts the PostScript into a raster format, but using a in-memory PS as source.
	   For that we use the popen function to run the GS command. The biggest problem, however, is that popen only
	   access one pipe and we need two. One for the PS input and the other for the raster output. There are popen
	   versions (popen2) that have that capacity, but not the one on Windows so we resort to a trick using both
	   popen() and pipe().

	   w,h are the raster Width and Height calculated by a previous call to pipe_HR_BB().
	   out_file is a string with two different meanings:
	      1. If it's empty than we save the raster in a GMT_IMAGE structure
	      2. If it holds a file name plus the settings for that driver, than we save the result in a file.
	*/
	char      cmd[1024] = {""}, buf[GMT_LEN128], t[16] = {""};
	int       fd[2] = {0, 0}, fh, n, k, pix_w, pix_h;
	uint64_t  dim[3], nXY, row, col, band, nCols, nRows, nBands;
	unsigned char *tmp;
	unsigned int nopad[4] = {0, 0, 0, 0};
	struct GMT_IMAGE *I = NULL;
	struct GMT_POSTSCRIPT *PS = NULL;
#ifdef _WIN32
	uint64_t  n_bytes;
#else
	struct popen2 *H = NULL;
#endif
	FILE     *fp = NULL;

	/* sprintf(cmd, "gswin64c -q -r300x300 -sDEVICE=ppmraw -sOutputFile=- -"); */
	pix_w = urint (ceil (w * Ctrl->E.dpi / 72.0));
	pix_h = urint (ceil (h * Ctrl->E.dpi / 72.0));
	sprintf (cmd, "%s -r%d -g%dx%d ", Ctrl->G.file, Ctrl->E.dpi, pix_w, pix_h);
	if (strlen(gs_params) < 450) strcat (cmd, gs_params);	/* We know it is but Coverity doesn't, and complains */

	if (out_file[0] == '\0') {		/* Need to open a pipe to capture the popen output, which will contain the raster */
		strcat (cmd, " -sDEVICE=ppmraw -sOutputFile=- -");
#ifdef _WIN32
		if (_pipe(fd, 145227600, O_BINARY) == -1) {
			GMT_Report (API, GMT_MSG_NORMAL, "Error: failed to open the pipe.\n");
			return GMT_RUNTIME_ERROR;
		}
		if (dup2 (fd[1], fileno (stdout)) < 0) {
			GMT_Report (API, GMT_MSG_NORMAL, "Error: Failed to duplicate pipe.\n");
			return GMT_RUNTIME_ERROR;
		}
		if (close (fd[1]) == -1) { 		/* Close original write end of pipe */
			GMT_Report (API, GMT_MSG_NORMAL, "Error: failed to close write end of pipe.\n");
			return GMT_RUNTIME_ERROR;
		}
		if ((fp = popen (cmd, "w")) == NULL) {
			GMT_Report (API, GMT_MSG_NORMAL, "Cannot execute GhostScript command.\n");
			gmt_M_free (API->GMT, PS);
			return GMT_RUNTIME_ERROR;
		}
#else
		if ((H = gmt_popen2 (cmd)) == NULL) {	/* Failed popen-job, exit */
			GMT_Report(API, GMT_MSG_NORMAL, "Cannot execute GhostScript command.\n");
			return GMT_RUNTIME_ERROR;
		}
#endif
	}
	else {	/* Only need a unidirectional pipe as supported by all since gs output is written to a file */
		strncat (cmd, out_file, 1023);
		if ((fp = popen (cmd, "w")) == NULL) {
			GMT_Report (API, GMT_MSG_NORMAL, "Cannot execute GhostScript command.\n");
			return GMT_RUNTIME_ERROR;
		}
	}
	
	PS = gmt_M_memory (API->GMT, NULL, 1, struct GMT_POSTSCRIPT);
	PS->data = PSL_getplot (API->GMT->PSL);		/* Get pointer to the plot buffer */
	PS->n_bytes = API->GMT->PSL->internal.n;	/* Length of plot buffer; note P->n_alloc = 0 since nothing was allocated here */

	/* Send the PS down ghostscript's throat */
#ifdef _WIN32
	if ((n_bytes = fwrite (PS->data, sizeof(char), PS->n_bytes, fp)) != PS->n_bytes)
		GMT_Report (API, GMT_MSG_NORMAL,
		            "Error writing PostScript buffer to GhostScript process. Bytes written = %ld, should have been %ld\n", n_bytes, PS->n_bytes);
	if (fflush (fp) == EOF)
		GMT_Report (API, GMT_MSG_NORMAL, "Error flushing GhostScript process.\n");
	if (pclose (fp) == -1)
		GMT_Report (API, GMT_MSG_NORMAL, "Error closing GhostScript process.\n");
	fh = fd[0];	/* File handle for reading */
#else
	if (fp) {	/* Did popen after all since we have an output filename */
		if (fwrite (PS->data, sizeof(char), PS->n_bytes, fp) != PS->n_bytes)
			GMT_Report (API, GMT_MSG_NORMAL, "Error writing PostScript buffer to GhostScript process.\n");
		if (fflush (fp) == EOF)
			GMT_Report (API, GMT_MSG_NORMAL, "Error flushing GhostScript process.\n");
		if (pclose (fp) == -1)
			GMT_Report (API, GMT_MSG_NORMAL, "Error closing GhostScript process.\n");
	}
	else {	/* On non-Windows and want a raster back */
 		write (H->fd[1], PS->data, PS->n_bytes);
		/* Now closed for writing */
    	gmt_pclose2 (&H, 1);
		fh = H->fd[0];	/* File handle for reading */
	}
#endif

	/* And now restore the original BB & HiResBB so that the PS data can be reused if wanted */
	gmt_M_free (API->GMT, PS);

	/* ----------- IF WE WROTE A FILE THEN WE ARE DONE AND WILL RETURN RIGHT NOW -------------- */
	if (out_file[0] != '\0')
		return GMT_NOERROR;
	/* ---------------------------------------------------------------------------------------- */

	if ((n = read (fh, buf, 3U)) != 3)				/* Consume first header line */
		GMT_Report (API, GMT_MSG_NORMAL, "pipe_ghost: failed read first line in popen store. Expect failures.\n");
	while (read (fh, buf, 1U) && buf[0] != '\n'); 	/* OK, by the end of this we are at the end of second header line */
	n = 0;
	while (read(fh, buf, 1U) && buf[0] != ' ') 		/* Get string with number of columns from 3rd header line */
		t[n++] = buf[0];
	dim[GMT_X] = atoi (t);
	n = 0;
	while (read(fh, buf, 1U) && buf[0] != '\n') 		/* Get string with number of rows from 3rd header line */
		t[n++] = buf[0];
	t[n] = '\0';						/* Make sure no character is left from previous usage */

	while (read(fh, buf, 1U) && buf[0] != '\n');		/* Consume fourth header line */

	dim[GMT_Y] = atoi (t);
	dim[GMT_Z] = 3;	/* This might change if we do monochrome at some point */
	GMT_Report (API, GMT_MSG_VERBOSE, "Image dimensions %d\t%d\n", dim[GMT_X], dim[GMT_Y]);

	if ((I = GMT_Create_Data (API, GMT_IS_IMAGE, GMT_IS_SURFACE, GMT_CONTAINER_AND_DATA, dim, NULL, NULL, 1, 0, NULL)) == NULL) {
		GMT_Report (API, GMT_MSG_NORMAL, "Could not create Image structure\n");
		return GMT_RUNTIME_ERROR;
	}

	nCols = dim[GMT_X];		nRows = dim[GMT_Y];		nBands = dim[2];	nXY = nRows * nCols;
	tmp   = gmt_M_memory(API->GMT, NULL, nCols * nBands, char);
	if (!strncmp(I->header->mem_layout, "TCP", 3)) {		/* Images.jl in Julia wants this */
		for (row = 0; row < nRows; row++) {
			if ((k = read (fd[0], tmp, (unsigned int)(nCols * nBands))) == 0) {	/* Read a row of nCols by nBands bytes of data */
				GMT_Report (API, GMT_MSG_NORMAL, "Could not read row from pipe into Image structure\n");
				gmt_M_free (API->GMT, tmp);
				return GMT_RUNTIME_ERROR;
			}
			for (col = n = 0; col < nCols; col++)
				for (band = 0; band < nBands; band++)
					I->data[row*nBands + col*nBands*nRows + band] = tmp[n++];
		}
	}
	else if (!strncmp(I->header->mem_layout, "TRP", 3)) {	/* Very cheap this one since is gs native order. */
		read (fd[0], I->data, (unsigned int)(nCols * nRows * nBands));		/* ... but may overflow */
	}
	else {
		for (row = 0; row < nRows; row++) {
			read (fd[0], tmp, (unsigned int)(nCols * nBands));	/* Read a row of nCols by nBands bytes of data */
			for (col = n = 0; col < nCols; col++)
				for (band = 0; band < nBands; band++)
					I->data[row + col*nRows + band*nXY] = tmp[n++];	/* Band interleaved, the best for MEX. */
		}
	}
	gmt_M_free (API->GMT, tmp);

#ifdef _WIN32
	if (close (fh) == -1) 		/* Close read end of pipe */
		GMT_Report (API, GMT_MSG_NORMAL, "Error: Failed to close read end of pipe.\n");
#else
	/* Now closed for reading */
 	gmt_pclose2 (&H, 0);
#endif

	I->type = GMT_CHAR;
	I->header->n_columns = (uint32_t)dim[GMT_X];	I->header->n_rows = (uint32_t)dim[GMT_Y];	I->header->n_bands = (uint32_t)dim[GMT_Z];
	I->header->registration = GMT_GRID_PIXEL_REG;
	gmt_M_grd_setpad (API->GMT, I->header, nopad);          /* Copy the no pad to the header */
	if (GMT_Write_Data (API, GMT_IS_IMAGE, GMT_IS_FILE, GMT_IS_SURFACE, GMT_CONTAINER_AND_DATA, NULL, Ctrl->F.file, I) != GMT_NOERROR)
		return GMT_RUNTIME_ERROR;

	return GMT_NOERROR;	/* Done here */
}

/* ---------------------------------------------------------------------------------------------- */
GMT_LOCAL int in_mem_PS_convert(struct GMTAPI_CTRL *API, struct PS2RASTER_CTRL *Ctrl, char **ps_names, char *gs_BB, char *gs_params, char *device[], char *device_options[], char *ext[]) {
	char out_file[GMT_LEN256] = {""};
	int    error = 0;
	double margin = 0, w = 0, h = 0;	/* Width and height in pixels of the final raster cropped of the outer white spaces */

	if (ps_names[0][1])		/* See if we have a margin request */
		margin = gmt_M_to_points (API->GMT, &ps_names[0][1]);

	if (API->GMT->PSL->internal.pmode != 3) {
		GMT_Report (API, GMT_MSG_NORMAL, "Error: Internal PSL PostScript is only half-baked [mode = %d]\n",
		            API->GMT->PSL->internal.pmode);
		error++;
	}
	if (error) 			/* Return in error state */
		return error;

	if (pipe_HR_BB (API, Ctrl, gs_BB, margin, &w, &h))		/* Apply the -A stuff to the in-memory PS */
		GMT_Report (API, GMT_MSG_NORMAL, "Failed to fish the HiResBoundingBox from PS-in-memory .\n");

	if (Ctrl->T.active) {		/* Then write the converted file into a file instead of storing it into a Image struct */
		char t[GMT_LEN256] = {""};
		sprintf (t, " -sDEVICE=%s %s -sOutputFile=", device[Ctrl->T.device], device_options[Ctrl->T.device]);
		strcat (out_file, t);
		if (API->external && Ctrl->F.active && !gmt_M_file_is_memory (Ctrl->F.file)) {
			strncpy (t, Ctrl->F.file, GMT_LEN256-1);
		}
		else {
			if (API->tmp_dir)	/* Use the established temp directory */
				sprintf (t, "%s/psconvert_tmp", API->tmp_dir);
			else	/* Must dump it in current directory */
				sprintf (t, "psconvert_tmp");
		}
		strncat (t, ext[Ctrl->T.device], 5);
		if (API->external) {		/* Apparently we cannot have the output file name inside quotes */
			strcat (out_file, t);		strcat (out_file, " -");
		}
		else {
			strncat (out_file, squote, 1);	strcat (out_file, t);	strncat (out_file, squote, 1);
			strcat (out_file, " -");
		}
	}
	if (pipe_ghost(API, Ctrl, gs_params, w, h, out_file)) {	/* Run ghostscript to convert the PS to the desired output format */
		GMT_Report (API, GMT_MSG_NORMAL, "Failed to wrap ghostscript in pipes.\n");
		error++;
	}
	return error;
}

EXTERN_MSC int gmt_copy (struct GMTAPI_CTRL *API, enum GMT_enum_family family, unsigned int direction, char *ifile, char *ofile);

int GMT_psconvert (void *V_API, int mode, void *args) {
	unsigned int i, j, k, pix_w = 0, pix_h = 0, got_BBatend;
	int sys_retval = 0, r, pos_file, pos_ext, error = 0;
	size_t len, line_size = 0U;
	uint64_t pos = 0;
	bool got_BB, got_HRBB, file_has_HRBB, got_end, landscape, landscape_orig, set_background = false;
	bool excessK, setup, found_proj = false, isGMT_PS = false, return_image = false, delete = false, file_processing = true;
	bool transparency = false, look_for_transparency, BeginPageSetup_here = false, add_grestore = false;

	double xt, yt, xt_bak, yt_bak, w, h, x0 = 0.0, x1 = 612.0, y0 = 0.0, y1 = 828.0;
	double west = 0.0, east = 0.0, south = 0.0, north = 0.0;
	double old_scale_x = 1, old_scale_y = 1;

	size_t n_alloc = GMT_SMALL_CHUNK;

	char **ps_names = NULL;
	char ps_file[PATH_MAX] = "", no_U_file[PATH_MAX] = "", clean_PS_file[PATH_MAX] = "", tmp_file[PATH_MAX] = "",
	     out_file[PATH_MAX] = "", BB_file[PATH_MAX] = "";
	char *line = NULL, c1[20] = {""}, c2[20] = {""}, c3[20] = {""}, c4[20] = {""},
	     cmd[GMT_BUFSIZ] = {""}, proj4_name[20] = {""}, *quiet = NULL;
	char *gs_params = NULL, *gs_BB = NULL, *proj4_cmd = NULL;
	char *device[N_GS_DEVICES] = {"", "pdfwrite", "svg", "jpeg", "png16m", "ppmraw", "tiff24nc", "bmp16m", "pngalpha",
	                              "jpeggray", "pnggray", "tiffgray", "bmpgray"};
	char *device_options[N_GS_DEVICES] = {
		/* extra options to pass to individual drivers */
		"",
		"", /* pdfwrite */
		"", /* svg */
		"-dJPEGQ=90", /* jpeg */
		"", /* png16m */
		"", /* ppmraw */
		"-sCompression=lzw", /* tiff24nc */
		"", /* bmp16m */
		"", /* pngalpha */
		"-dJPEGQ=90", /* jpeggray */
		"", /* pnggray */
		"-sCompression=lzw", /* tiffgray */
		""}; /* bmpgray */
	char *ext[N_GS_DEVICES] = {".eps", ".pdf", ".svg", ".jpg", ".png", ".ppm", ".tif", ".bmp", ".png", ".jpg", ".png", ".tif", ".bmp"};
	char *RefLevel[N_KML_ELEVATIONS] = {"clampToGround", "relativeToGround", "absolute", "relativeToSeaFloor", "clampToSeaFloor"};
#ifdef WIN32
	char at_sign[2] = "@";
#else
	char at_sign[2] = "";
#endif

#ifdef HAVE_GDAL
	struct GMT_GDALREAD_IN_CTRL  *to_gdalread = NULL;
	struct GMT_GDALREAD_OUT_CTRL *from_gdalread = NULL;
#endif

	FILE *fp = NULL, *fpo = NULL, *fpb = NULL, *fp2 = NULL, *fpw = NULL;

	struct GMT_OPTION *opt = NULL;
	struct PS2RASTER_CTRL *Ctrl = NULL;
	struct GMT_CTRL *GMT = NULL, *GMT_cpy = NULL;
	struct GMT_OPTION *options = NULL;
	struct GMTAPI_CTRL *API = gmt_get_api_ptr (V_API);	/* Cast from void to GMTAPI_CTRL pointer */
	struct { int major, minor; } gsVersion = {0, 0};
	struct GMT_POSTSCRIPT *PS = NULL;

	/*----------------------- Standard module initialization and parsing ----------------------*/

	if (API == NULL) return (GMT_NOT_A_SESSION);
	if (mode == GMT_MODULE_PURPOSE) return (usage (API, GMT_MODULE_PURPOSE));	/* Return the purpose of program */
	options = GMT_Create_Options (API, mode, args);	if (API->error) return (API->error);	/* Set or get option list */

	if (!options || options->option == GMT_OPT_USAGE) bailout (usage (API, GMT_USAGE));/* Return the usage message */
	if (options->option == GMT_OPT_SYNOPSIS) bailout (usage (API, GMT_SYNOPSIS));	/* Return the synopsis */

	/* Parse the command-line arguments */

	if ((GMT = gmt_init_module (API, THIS_MODULE_LIB, THIS_MODULE_NAME, THIS_MODULE_KEYS, THIS_MODULE_NEEDS, &options, &GMT_cpy)) == NULL) bailout (API->error); /* Save current state */
	if (GMT_Parse_Common (API, THIS_MODULE_OPTIONS, options)) Return (API->error);
	Ctrl = New_Ctrl (GMT);	/* Allocate and initialize a new control structure */
	if ((error = parse (GMT, Ctrl, options)) != 0) Return (error);

	/*---------------------------- This is the psconvert main code ----------------------------*/

	if (!Ctrl->L.active && (GMT->current.setting.run_mode == GMT_CLASSIC) && (Ctrl->In.n_files == 0)) {	/* No files given, bail */
		GMT_Report (API, GMT_MSG_NORMAL, "No PostScript files specified - exiting.\n");
		Return (GMT_NOERROR);
	}

	/* Test if GhostScript can be executed (version query) */
	sprintf(cmd, "%s --version", Ctrl->G.file);
	if ((fp = popen(cmd, "r")) != NULL) {
		int n;
		n = fscanf(fp, "%d.%d", &gsVersion.major, &gsVersion.minor);
		if (pclose(fp) == -1)
			GMT_Report (API, GMT_MSG_NORMAL, "Error closing GhostScript version query.\n");
		if (n != 2) {
			/* command execution failed or cannot parse response */
			GMT_Report (API, GMT_MSG_NORMAL, "Failed to parse response to GhostScript version query [n = %d %d %d].\n",
			            n, gsVersion.major, gsVersion.minor);
			Return (GMT_RUNTIME_ERROR);
		}
	}
	else { /* failed to open pipe */
		GMT_Report (API, GMT_MSG_NORMAL, "Cannot execute GhostScript (%s).\n", Ctrl->G.file);
		Return (GMT_RUNTIME_ERROR);
	}

	if (Ctrl->T.device == GS_DEV_SVG && (gsVersion.major > 9 || (gsVersion.major == 9 && gsVersion.minor >= 16))) {
		GMT_Report (API, GMT_MSG_NORMAL, "Error: Your ghostscript version (%d.%d) no longer supports the SVG device.\n",
		            gsVersion.major, gsVersion.minor);
		GMT_Report (API, GMT_MSG_NORMAL, "We recommend converting to PDF and then installing the pdf2svg package.\n");
		Return (GMT_RUNTIME_ERROR);
	}
	if (Ctrl->F.active && (Ctrl->L.active || Ctrl->D.active)) {
		GMT_Report (API, GMT_MSG_NORMAL, "Warning: Option -F and options -L OR -D are mutually exclusive. Ignoring option -F.\n");
		Ctrl->F.active = false;
	}

	if (Ctrl->F.active && Ctrl->In.n_files > 1 && Ctrl->T.device != -GS_DEV_PDF) {
		GMT_Report (API, GMT_MSG_NORMAL, "Warning: Option -F is incompatible with multiple inputs. Ignoring option -F.\n");
		Ctrl->F.active = false;
	}

	if (Ctrl->F.active && gmt_M_file_is_memory (Ctrl->F.file)) {
		if (Ctrl->T.device <= GS_DEV_SVG || Ctrl->In.n_files > 1) {
			GMT_Report (API, GMT_MSG_NORMAL, "Error: Can only return one (PS or PDF) raster image to calling program via memory array.\n");
			Return (GMT_RUNTIME_ERROR);
		}
		return_image = true;
#ifndef HAVE_GDAL
		GMT_Report (API, GMT_MSG_DEBUG, "Selecting ppmraw device since GDAL not available.\n");
		Ctrl->T.device = GS_DEV_PPM;
#endif
	}

	if (GMT->current.setting.run_mode == GMT_MODERN) {	/* Finalize hidden PS file first */
		if ((k = gmt_set_psfilename (GMT)) == 0) {	/* Get hidden file name for PS */
			GMT_Report (GMT->parent, GMT_MSG_NORMAL, "No hidden PS file %s found\n", GMT->current.ps.filename);
			Return (GMT_RUNTIME_ERROR);
		}
		if ((fp = PSL_fopen (GMT->PSL, GMT->current.ps.filename, "a")) == NULL) {	/* Must open inside PSL DLL */
			GMT_Report (GMT->parent, GMT_MSG_NORMAL, "Cannot append to file %s\n", GMT->current.ps.filename);
			Return (GMT_RUNTIME_ERROR);
		}
		PSL_endplot (GMT->PSL, 1);	/* Finalize the PS plot */
		if (PSL_fclose (GMT->PSL)) {
			GMT_Report (GMT->parent, GMT_MSG_NORMAL, "Error: Unable to close hidden PS file %s!\n", GMT->current.ps.filename);
			Return (GMT_RUNTIME_ERROR);
		}
	}
	
	/* Parameters for all the formats available */

	if (gsVersion.major >= 9 && gsVersion.minor >= 21)
		/* Artifex says that the -dSCANCONVERTERTYPE=2 new scan converter is faster (confirmed) and
		   will be the default in a future release. Since it was introduced in 9.21 we start using it
		   right now and remove this conditional once it becomes the default */
		gs_params = "-q -dSAFER -dNOPAUSE -dBATCH -dPDFSETTINGS=/prepress -dDownsampleColorImages=false -dDownsampleGrayImages=false -dDownsampleMonoImages=false -dUseFlateCompression=true -dEmbedAllFonts=true -dSubsetFonts=true -dMonoImageFilter=/FlateEncode -dAutoFilterGrayImages=false -dGrayImageFilter=/FlateEncode -dAutoFilterColorImages=false -dColorImageFilter=/FlateEncode -dSCANCONVERTERTYPE=2";
	else
		gs_params = "-q -dSAFER -dNOPAUSE -dBATCH -dPDFSETTINGS=/prepress -dDownsampleColorImages=false -dDownsampleGrayImages=false -dDownsampleMonoImages=false -dUseFlateCompression=true -dEmbedAllFonts=true -dSubsetFonts=true -dMonoImageFilter=/FlateEncode -dAutoFilterGrayImages=false -dGrayImageFilter=/FlateEncode -dAutoFilterColorImages=false -dColorImageFilter=/FlateEncode";

	gs_BB = "-q -dSAFER -dNOPAUSE -dBATCH -sDEVICE=bbox"; /* -r defaults to 4000, see http://pages.cs.wisc.edu/~ghost/doc/cvs/Devices.htm#Test */

	add_to_list (Ctrl->C.arg, "-dMaxBitmap=2147483647");	/* Add this as GS option to fix bug in GS */

	if (Ctrl->W.kml && !(Ctrl->T.device == GS_DEV_JPG ||
	    Ctrl->T.device == GS_DEV_JPGG || Ctrl->T.device == GS_DEV_TIF ||
	    Ctrl->T.device == GS_DEV_TIFG || Ctrl->T.device == GS_DEV_PNG ||
	    Ctrl->T.device == GS_DEV_TPNG || Ctrl->T.device == GS_DEV_PNGG) ) {
		GMT_Report (API, GMT_MSG_NORMAL, "Error: As far as we know selected raster type is unsupported by GE.\n");
	}

	if (Ctrl->W.active) {	/* Implies -P and -A (unless -A- is set ) */
		Ctrl->P.active = Ctrl->A.active = true;
		if (Ctrl->A.reset) Ctrl->A.active = false;
	}

	/* Use default DPI if not already set */
	if (Ctrl->E.dpi <= 0) Ctrl->E.dpi = (Ctrl->T.device == GS_DEV_PDF) ? 720 : 300;

	line_size = GMT_LEN128;
	line = gmt_M_memory (GMT, NULL, line_size, char);	/* Initial buffer size */

	/* Multiple files in a file with their names */
	if (Ctrl->L.active) {
		struct GMT_TEXTSET *T = NULL;
		if ((T = GMT_Read_Data (API, GMT_IS_TEXTSET, GMT_IS_FILE, GMT_IS_NONE, GMT_READ_NORMAL, NULL, Ctrl->L.file, NULL)) == NULL) {
			gmt_M_free (GMT, line);
			Return (GMT_RUNTIME_ERROR);
		}

		Ctrl->In.n_files = (unsigned int)T->n_records;
		ps_names = gmt_M_memory (GMT, NULL, T->n_records, char *);
		for (k = 0; k < T->table[0]->segment[0]->n_rows; k++)	/* Set pointers */
			ps_names[k] = T->table[0]->segment[0]->data[k];
	}
	else if (Ctrl->In.n_files) {	/* One or more files given on command line */
		ps_names = gmt_M_memory (GMT, NULL, Ctrl->In.n_files, char *);
		j = 0;
		for (opt = options; opt; opt = opt->next) {
			if (opt->option != '<') continue;
			ps_names[j++] = strdup (opt->arg);
		}
	}
	else if (GMT->current.setting.run_mode == GMT_MODERN) {	/* Just that one hidden PS file */
		ps_names = gmt_M_memory (GMT, NULL, 1, char *);
		ps_names[0] = strdup (GMT->current.ps.filename);
		Ctrl->In.n_files = 1;
	}

	/* -------------------- Special case of in-memory PS. Process it and return ------------------- */
	if (API->external && Ctrl->In.n_files == 1 && ps_names[0][0] == '=') {
		if (!return_image && !Ctrl->T.active) {
			GMT_Report (API, GMT_MSG_NORMAL, "Error: Internal PSL PostScript rip requires output file via -F\n");
			error++;
		}
		else
			error = in_mem_PS_convert(API, Ctrl, ps_names, gs_BB, gs_params, device, device_options, ext);

		if (!Ctrl->L.active) gmt_M_str_free (ps_names[0]);		/* Otherwise ps_names contents are the Garbageman territory */
		gmt_M_free (GMT, ps_names);
		gmt_M_free (GMT, line);
		Return (error ? GMT_RUNTIME_ERROR : GMT_NOERROR);		/* Done here */
	}
	/* --------------------------------------------------------------------------------------------- */

	/* Let gray 50 be rasterized as 50/50/50. See http://gmtrac.soest.hawaii.edu/issues/50 */
	if (!Ctrl->I.active && ((gsVersion.major == 9 && gsVersion.minor >= 5) || gsVersion.major > 9))
		add_to_list (Ctrl->C.arg, "-dUseFastColor=true");

	/* --------------------------------------------------------------------------------------------- */
	/* ------    If a multi-page PDF file creation is requested, do it and exit.   ------------------*/
	/* --------------------------------------------------------------------------------------------- */
	if (Ctrl->T.active && Ctrl->T.device == -GS_DEV_PDF) {
		char *all_names_in = NULL, *cmd2 = NULL;
		int   error = 0;

		n_alloc = 0;
		for (k = 0; k < Ctrl->In.n_files; k++)
			n_alloc += (strlen (ps_names[k]) + 3);	/* 3 = 2 quotes plus space */
		all_names_in = gmt_M_memory (GMT, NULL, n_alloc, char);
		for (k = 0; k < Ctrl->In.n_files; k++) {
			add_to_qlist (all_names_in, ps_names[k]);
			if (!Ctrl->L.active) gmt_M_str_free (ps_names[k]);		/* Otherwise ps_names contents are the Garbageman territory */
		}
		cmd2 = gmt_M_memory (GMT, NULL, n_alloc + PATH_MAX, char);
		sprintf (cmd2, "%s%s -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite %s%s -r%d -sOutputFile=%c%s.pdf%c %s",
			at_sign, Ctrl->G.file, Ctrl->C.arg, alpha_bits(Ctrl), Ctrl->E.dpi, quote, Ctrl->F.file, quote, all_names_in);

		GMT_Report (API, GMT_MSG_DEBUG, "Running: %s\n", cmd2);
		sys_retval = system (cmd2);		/* Execute the GhostScript command */
		if (sys_retval) {
			GMT_Report (API, GMT_MSG_NORMAL, "System call [%s] returned error %d.\n", cmd2, sys_retval);
			error++;
		}
		if (!error && Ctrl->S.active)
			GMT_Report (API, GMT_MSG_NORMAL, "%s\n", cmd2);

		gmt_M_free (GMT, all_names_in);
		gmt_M_free (GMT, cmd2);
		gmt_M_free (GMT, ps_names);
		gmt_M_free (GMT, line);
		Return (error ? GMT_RUNTIME_ERROR : GMT_NOERROR);		/* Done here */
	}
	/* ----------------------------------------------------------------------------------------------- */

	PS = gmt_M_memory (GMT, NULL, 1, struct GMT_POSTSCRIPT);	/* Only used if API passes = */

	/* Loop over all input files */

	for (k = 0; k < Ctrl->In.n_files; k++) {
		excessK = delete = false;
		*out_file = '\0'; /* truncate string */
		if (gmt_M_file_is_memory (ps_names[k])) {	/* For now we create temp file from PS given via memory so code below will work */
			sprintf (ps_file, "%s/psconvert_stream_%d.ps", API->tmp_dir, (int)getpid());
			if (gmt_copy (API, GMT_IS_POSTSCRIPT, GMT_OUT, ps_names[k], ps_file)) {
				GMT_Report (API, GMT_MSG_NORMAL, "Unable to make temp file %s from %s. Skipping.\n", ps_file, ps_names[k]);
				continue;
			}
			delete = true;	/* Must delete this temporary file when done */
		}
		else
			strncpy (ps_file, ps_names[k], PATH_MAX-1);

		if (file_processing && (fp = fopen (ps_file, "r")) == NULL) {
			GMT_Report (API, GMT_MSG_NORMAL, "Cannot open file %s\n", ps_file);
			continue;
		}

		GMT_Report (API, GMT_MSG_VERBOSE, "Processing %s...\n", ps_file);

		if (Ctrl->A.strip) {	/* Must strip off the GMT timestamp stuff, but pass any font encodings */
			int dump = true;
			GMT_Report (API, GMT_MSG_LONG_VERBOSE, "Strip GMT time-stamp...\n");
			sprintf (no_U_file, "%s/psconvert_%db.eps", Ctrl->D.dir, (int)getpid());
			if ((fp2 = fopen (no_U_file, "w+")) == NULL) {
				unsigned int kk;
				GMT_Report (API, GMT_MSG_NORMAL, "Unable to create a temporary file\n");
				if (file_processing) {fclose (fp);	fp = NULL;}	/* Close original PS file */
				if (delete && gmt_remove_file (GMT, ps_file))	/* Since we created a temporary file from the memdata */
					Return (GMT_RUNTIME_ERROR);
				if (!Ctrl->L.active)			/* Otherwise ps_names contents are the Garbageman territory */
					for (kk = 0; kk < Ctrl->In.n_files; kk++) gmt_M_str_free (ps_names[kk]);
				gmt_M_free (GMT, ps_names);
				gmt_M_free (GMT, PS);
				gmt_M_free (GMT, line);
				Return (GMT_RUNTIME_ERROR);
			}
			while (file_line_reader (GMT, &line, &line_size, fp, PS->data, &pos) != EOF) {
				if (dump && !strncmp (line, "% Begin GMT time-stamp", 22))
					dump = false;
				if (dump)
					fprintf (fp2, "%s\n", line);
				else if (!strncmp (line, "PSL_font_encode", 15))	/* Must always pass any font reencodings */
					fprintf (fp2, "%s\n", line);
				if (!dump && !strncmp (line, "% End GMT time-stamp", 20))
					dump = true;
			}
			if (file_processing) fclose (fp);	/* Close original PS file */
			rewind (fp2);	/* Rewind new file without timestamp */
			fp = fp2;	/* Set original file pointer to this file instead */
			file_processing = true;			/* Since we now are reading a temporary file */
		}

		got_BB = got_HRBB = file_has_HRBB = got_end = landscape = landscape_orig = setup = false;
		got_BBatend = 0;

		len = strlen (ps_file);
		j = (unsigned int)len - 1;
		pos_file = -1;
		pos_ext = -1;	/* In case file has no extension */
		for (i = 0; i < len; i++, j--) {
			if (pos_ext < 0 && ps_file[j] == '.') pos_ext = j;	/* Beginning of file extension */
			if (pos_file < 0 && (ps_file[j] == '/' || ps_file[j] == '\\')) pos_file = j + 1;	/* Beginning of file name */
		}
		if (pos_ext == -1) pos_ext = (unsigned int)len - 1;	/* File has no extension */
		if (!Ctrl->D.active || pos_file == -1) pos_file = 0;	/* File either has no leading directory or we want to use it */

		/* Adjust to a tight BoundingBox if user requested so */

		if (Ctrl->A.active) {
			char *psfile_to_use = NULL;
			GMT_Report (API, GMT_MSG_LONG_VERBOSE, "Find HiResBoundingBox ...\n");
			sprintf (BB_file, "%s/psconvert_%dc.bb", Ctrl->D.dir, (int)getpid());
			psfile_to_use = Ctrl->A.strip ? no_U_file : ((strlen (clean_PS_file) > 0) ? clean_PS_file : ps_file);
			sprintf (cmd, "%s%s %s %s %c%s%c 2> %c%s%c",
			         at_sign, Ctrl->G.file, gs_BB, Ctrl->C.arg, quote, psfile_to_use, quote, quote, BB_file, quote);
			GMT_Report (API, GMT_MSG_DEBUG, "Running: %s\n", cmd);
			sys_retval = system (cmd);		/* Execute the command that computes the tight BB */
			if (sys_retval) {
				GMT_Report (API, GMT_MSG_NORMAL, "System call [%s] returned error %d.\n", cmd, sys_retval);
				if (gmt_remove_file (GMT, BB_file))
					Return (GMT_RUNTIME_ERROR);
				if (delete && gmt_remove_file (GMT, ps_file))	/* Since we created a temporary file from the memdata */
					Return (GMT_RUNTIME_ERROR);
				gmt_M_free (GMT, PS);
				Return (GMT_RUNTIME_ERROR);
			}
			if ((fpb = fopen (BB_file, "r")) == NULL) {
				GMT_Report (API, GMT_MSG_NORMAL, "Unable to open file %s\n", BB_file);
				if (delete && gmt_remove_file (GMT, ps_file))	/* Since we created a temporary file from the memdata */
					Return (GMT_RUNTIME_ERROR);
				gmt_M_free (GMT, PS);
				Return (GMT_ERROR_ON_FOPEN);
			}
			while ((file_line_reader (GMT, &line, &line_size, fpb, NULL, NULL) != EOF) && !got_BB) {
				/* We only use the High resolution BB */
				if ((strstr (line,"%%HiResBoundingBox:"))) {
					sscanf (&line[19], "%s %s %s %s", c1, c2, c3, c4);
					x0 = atof (c1);		y0 = atof (c2);
					x1 = atof (c3);		y1 = atof (c4);
					x0 -= Ctrl->A.margin[XLO];	x1 += Ctrl->A.margin[XHI];	/* If not given, margin = 0/0/0/0 */
					y0 -= Ctrl->A.margin[YLO];	y1 += Ctrl->A.margin[YHI];
					if (x1 <= x0 || y1 <= y0) {
						GMT_Report (API, GMT_MSG_NORMAL, "Unable to decode BoundingBox file %s\n", BB_file);
						fclose (fpb);	fpb = NULL;            /* so we don't accidentally close twice */
						if (!Ctrl->S.active && gmt_remove_file (GMT, BB_file))	/* Remove the file */
							Return (GMT_RUNTIME_ERROR);
						if (Ctrl->D.active)
							sprintf (tmp_file, "%s/", Ctrl->D.dir);
						strncat (tmp_file, &ps_file[pos_file], (size_t)(pos_ext - pos_file));
						strcat (tmp_file, ext[Ctrl->T.device]);
						sprintf (cmd, "%s%s %s %s%s -sDEVICE=%s %s -g1x1 -r%d -sOutputFile=%c%s%c -f%c%s%c",
							at_sign, Ctrl->G.file, gs_params, Ctrl->C.arg, alpha_bits(Ctrl), device[Ctrl->T.device],
							device_options[Ctrl->T.device],
							Ctrl->E.dpi, quote, tmp_file, quote, quote, ps_file, quote);
						GMT_Report (API, GMT_MSG_DEBUG, "Running: %s\n", cmd);
						sys_retval = system (cmd);		/* Execute the GhostScript command */
						if (Ctrl->S.active)
							GMT_Report (API, GMT_MSG_NORMAL, "%s\n", cmd);
						if (sys_retval) {
							GMT_Report (API, GMT_MSG_NORMAL, "System call [%s] returned error %d.\n", cmd, sys_retval);
							if (gmt_remove_file (GMT, tmp_file))	/* Remove the file */
								Return (GMT_RUNTIME_ERROR);
							if (delete && gmt_remove_file (GMT, ps_file))	/* Since we created a temporary file from the memdata */
								Return (GMT_RUNTIME_ERROR);
							gmt_M_free (GMT, PS);
							Return (GMT_RUNTIME_ERROR);
						}
						/* must leave loop because fpb has been closed and file_line_reader would
						 * read from closed file: */
						break;
					}
					got_BB = got_HRBB = true;
					GMT_Report (API, GMT_MSG_VERBOSE, "Figure dimensions: Width: %g points [%g %s]  Height: %g points [%g %s]\n",
					            x1-x0, (x1-x0)*GMT->session.u2u[GMT_PT][GMT->current.setting.proj_length_unit],
					            API->GMT->session.unit_name[API->GMT->current.setting.proj_length_unit],
					            y1-y0, (y1-y0)*GMT->session.u2u[GMT_PT][GMT->current.setting.proj_length_unit],
					            API->GMT->session.unit_name[API->GMT->current.setting.proj_length_unit]);
				}
			}
			if (fpb != NULL) { /* don't close twice */
				fclose (fpb);	fpb = NULL;
			}
			if (!Ctrl->S.active && gmt_remove_file (GMT, BB_file))	/* Remove the file with BB info */
				Return (GMT_RUNTIME_ERROR);
			if (got_BB) GMT_Report (API, GMT_MSG_LONG_VERBOSE, "[%g %g %g %g]...\n", x0, y0, x1, y1);
		}

		/* Open temporary file to be processed by ghostscript. When -Te or -TE is used, tmp_file is for keeps */

		if (Ctrl->T.eps) {
			GMT_Report (API, GMT_MSG_LONG_VERBOSE, "Format EPS file...\n");
			if (Ctrl->D.active) sprintf (tmp_file, "%s/", Ctrl->D.dir);	/* Use specified output directory */
			if (!Ctrl->F.active || return_image)
				strncat (tmp_file, &ps_file[pos_file], (size_t)(pos_ext - pos_file));
			else
				strcat (tmp_file, Ctrl->F.file);
			if (strlen(tmp_file) < PATH_MAX-4)		/* To please Coverity */
				strcat (tmp_file, ext[GS_DEV_EPS]);
			if ((fpo = fopen (tmp_file, "w")) == NULL) {
				GMT_Report (API, GMT_MSG_NORMAL, "Unable to open file %s for writing\n", tmp_file);
				continue;
			}
		}
		else {
			sprintf (tmp_file, "%s/psconvert_%dd.eps", Ctrl->D.dir, (int)getpid());
			if ((fpo = fopen (tmp_file, "w+")) == NULL) {
				GMT_Report (API, GMT_MSG_NORMAL, "Unable to create a temporary file\n");
				continue;
			}
		}

		/* Scan first 20 lines of input file for [HiRes]BoundingBox and Orientation statements.
		 * Since we prefer the HiResBB over BB we must continue to read until both are found or 20 lines have past */

		i = 0;
		while ((file_line_reader (GMT, &line, &line_size, fp, PS->data, &pos) != EOF) && i < 20 && !(got_BB && got_HRBB && got_end)) {
			i++;
			if (!line[0] || line[0] != '%')
				{ /* Skip empty and non-comment lines */ }
			else if (!got_BB && strstr (line, "%%BoundingBox:")) {
				sscanf (&line[14], "%s %s %s %s",c1,c2,c3,c4);
				if (strncmp (c1, "(atend)", 7)) {	/* Got actual numbers */
					if (!got_HRBB) {	/* Only assign values if we haven't seen the high-res version yet */
						x0 = atoi (c1);		y0 = atoi (c2);
						x1 = atoi (c3);		y1 = atoi (c4);
					}
					got_BB = true;
				}
				else
					got_BBatend++;
			}
			else if ((strstr (line, "%%HiResBoundingBox:"))) {
				file_has_HRBB = true;
				if (!got_HRBB) {
					sscanf (&line[19], "%s %s %s %s",c1,c2,c3,c4);
					if (strncmp (c1, "(atend)", 7)) {	/* Got actual numbers */
						x0 = atof (c1);		y0 = atof (c2);
						x1 = atof (c3);		y1 = atof (c4);
						got_HRBB = got_BB = true;
					}
				}
			}
			else if ((strstr (line, "%%Creator:"))) {
				if (!strncmp (&line[11], "GMT", 3))
					isGMT_PS = true;
			}
			else if ((strstr (line, "%%Orientation:"))) {
				if (!strncmp (&line[15], "Landscape", 9))
					landscape = landscape_orig = true;
			}
			else if ((strstr (line, "%%EndComments")))
				got_end = true;
			if (got_BBatend == 1 && (got_end || i == 19)) {	/* Now is the time to look at the end of the file */
				got_BBatend++;			/* Avoid jumping more than once to the end */
				if (file_processing) {
					if (fseek (fp, (off_t)-256, SEEK_END))
						GMT_Report (API, GMT_MSG_NORMAL, "Error: Seeking to start of last 256 bytes failed\n");
				}
				else {	/* Get towards end of string */
					pos = (PS->n_bytes > 256) ? PS->n_bytes - 256 : 0;
				}
			}
		}

		/* Cannot proceed without knowing the BoundingBox */

		if (!got_BB) {
			GMT_Report (API, GMT_MSG_NORMAL,
			            "Error: The file %s has no BoundingBox in the first 20 lines or last 256 bytes. Use -A option.\n", ps_file);
			if (!Ctrl->T.eps && gmt_remove_file (GMT, tmp_file))	/* Remove the temporary EPS file */
				Return (GMT_RUNTIME_ERROR);
			continue;
		}

		/* Do the math on the BoundingBox and translation coordinates */

		if (Ctrl->P.active && landscape)
			xt = -x1, yt = -y0, w = y1-y0, h = x1-x0, r = -90;
		else
			xt = -x0, yt = -y0, w = x1-x0, h = y1-y0, r = 0;

		xt_bak = xt;	yt_bak = yt;		/* Needed when Ctrl->A.resize */

		/* Set new height when width was set and no height given */
		if (Ctrl->A.new_size[0] > 0.0 && Ctrl->A.new_size[1] == 0.0) Ctrl->A.new_size[1] = Ctrl->A.new_size[0] * h / w;

		/* ****************************************************************** */
		/*         Rewind the input file and start copying and replacing      */
		/* ****************************************************************** */

		file_rewind (fp, &pos);

		/* To produce non-PDF output from PS with transparency we must determine if transparency is requested in the PS */
		look_for_transparency = Ctrl->T.device != GS_DEV_PDF && Ctrl->T.device != -GS_DEV_PDF;
		transparency = add_grestore = false;
		set_background = (Ctrl->A.paint || Ctrl->A.outline);

		while (file_line_reader (GMT, &line, &line_size, fp, PS->data, &pos) != EOF) {
			if (line[0] != '%') {	/* Copy any non-comment line, except one containing setpagedevice in the Setup block */
				if (look_for_transparency && strstr (line, " PSL_transp")) {
					transparency = true;		/* Yes, found transparency */
					look_for_transparency = false;	/* No need to check anymore */
				}
				if (setup && strstr(line,"setpagedevice") != NULL)	/* This is a "setpagedevice" command that should be avoided */
					continue;
				fprintf (fpo, "%s\n", line);
				continue;
			}
			else if (Ctrl->W.active && !found_proj) {	/* Here, line[0] == '%' */
				if (!strncmp (&line[2], "PROJ", 4)) { /* Search for the PROJ tag in the ps file */
					char *ptmp = NULL, xx1[20], xx2[20], yy1[20], yy2[20];
					sscanf (&line[8], "%s %s %s %s %s %s %s %s %s",proj4_name,xx1,xx2,yy1,yy2,c1,c2,c3,c4);
					west = atof (c1);		east = atof (c2);
					south = atof (c3);		north = atof (c4);
					GMT->common.R.wesn[XLO] = atof (xx1);		GMT->common.R.wesn[XHI] = atof (xx2);
					if (GMT->common.R.wesn[XLO] > 180.0 && GMT->common.R.wesn[XHI] > 180.0) {
						GMT->common.R.wesn[XLO] -= 360.0;
						GMT->common.R.wesn[XHI] -= 360.0;
					}
					GMT->common.R.wesn[YLO] = atof (yy1);	GMT->common.R.wesn[YHI] = atof (yy2);
					found_proj = true;
					if ((ptmp = strstr (&line[2], "+proj")) != NULL) {  /* Search for the +proj in the comment line */
						proj4_cmd = strdup (&line[(int)(ptmp - &line[0])]);
						gmt_chop (proj4_cmd);		/* Remove the new line char */
					}
					if (!strcmp (proj4_name,"latlong") || !strcmp (proj4_name,"xy")) {		/* Linear case, use original coords */
						west  = atof(xx1);		east  = atof(xx2);
						south = atof(yy1);		north = atof(yy2);
						/* One further test. +xy was found, but have we geog coords? Check that */
						if (!strcmp (proj4_name,"xy") &&
								(west >= -180) && ((east <= 360) && ((east - west) <= 360)) &&
								(south >= -90) && (north <= 90) ) {
							proj4_cmd = strdup ("latlon");
							GMT_Report (API, GMT_MSG_NORMAL, "Warning: An unknown psconvert setting was found but since "
									"image coordinates seem to be geographical, a linear transformation "
									"will be used.\n");
						}
						else if (!strcmp (proj4_name,"xy") && Ctrl->W.warp) {	/* Do not operate on a twice unknown setting */
							GMT_Report (API, GMT_MSG_NORMAL, "Error: requested an automatic geotiff generation, but "
									"no recognized psconvert option was used for the PS creation.\n");
						}
					}
					else if (Ctrl->W.kml) {
						GMT_Report (API, GMT_MSG_NORMAL, "Error: To GE images must be in geographical coordinates. Very likely "
									"this won't work as you wish inside GE.\n");
					}
				}
			}

			if (!strncmp (line, "%%BoundingBox:", 14)) {
				double w_t, h_t;
				w_t = w;	h_t = h;
				if (Ctrl->A.resize) {		/* Here the BB is the new size itself */
					w_t = Ctrl->A.new_size[0];		h_t = Ctrl->A.new_size[1];
				}
				if (got_BB && !Ctrl->A.round)
					fprintf (fpo, "%%%%BoundingBox: 0 0 %ld %ld\n", lrint (ceil(w_t)), lrint (ceil(h_t)));
				else if (got_BB && Ctrl->A.round)		/* Go against Adobe Law and round HRBB instead of ceil */
					fprintf (fpo, "%%%%BoundingBox: 0 0 %ld %ld\n", lrint (w_t), lrint (h_t));

				got_BB = false;
				if (file_has_HRBB)
					continue;	/* High-res BB will be put elsewhere */
				if (got_HRBB)
					fprintf (fpo, "%%%%HiResBoundingBox: 0 0 %g %g\n", w_t, h_t);
				got_HRBB = false;
				continue;
			}
			else if (!strncmp (line, "%%HiResBoundingBox:", 19)) {
				double w_t, h_t;
				w_t = w;	h_t = h;
				if (Ctrl->A.resize) {		/* Here the BB is the new size itself */
					w_t = Ctrl->A.new_size[0];		h_t = Ctrl->A.new_size[1];
				}
				if (got_HRBB)
					fprintf (fpo, "%%%%HiResBoundingBox: 0 0 %g %g\n", w_t, h_t);
				got_HRBB = false;
				continue;
			}
			else if (Ctrl->P.active && landscape && !strncmp (line, "%%Orientation:", 14)) {
				fprintf (fpo, "%%%%Orientation: Portrait\n");
				landscape = false;
				continue;
			}
			else if (!strncmp (line, "%%BeginSetup", 12))
				setup = true;
			else if (!strncmp (line, "%%EndSetup", 10)) {
				setup = false;
				if (Ctrl->T.eps == -1)	/* Write out setpagedevice command */
					fprintf (fpo, "<< /PageSize [%g %g] >> setpagedevice\n", w, h);
				if (r != 0)
					fprintf (fpo, "%d rotate\n", r);
				if (!gmt_M_is_zero (xt) || !gmt_M_is_zero (yt))
					fprintf (fpo, "%g %g translate\n", xt, yt);
				xt = yt = 0.0;
				r = 0;
			}
			else if (Ctrl->A.resize) {
				/* We are going to trick ghostscript to do what -dEPSFitPage was supposed to do but doesn't
				   because it's bugged. For that we recompute a new scale, offsets and DPIs such that at the
				   end we will end up with an image with the imposed size and the current -E dpi setting.
				*/
				double new_scale_x, new_scale_y, new_off_x, new_off_y, r_x, r_y;
				char t1[8], t2[8];	/* To hold the translate part when landscape */
				char t3[128];		/* To hold a copy of the last commented (%%) line */
				if (!strncmp (line, "%%BeginPageSetup", 16)) {
					int n_scan = 0;
					size_t Lsize = 128U;
					char *line_ = gmt_M_memory (GMT, NULL, Lsize, char);
					BeginPageSetup_here = true;	/* Signal that on next line the job must be done */
					file_line_reader (GMT, &line_, &Lsize, fp, PS->data, &pos);   /* Read also next line which is to be overwritten (unless a comment) */
					while (line_[0] == '%') {	/* Skip all comments until we get the first actionable line */
						strncpy(t3, line_, 127);
						file_line_reader (GMT, &line_, &Lsize, fp, PS->data, &pos);
					}

					/* The trouble is that we can have things like "V 612 0 T 90 R 0.06 0.06 scale" or "V 0.06 0.06 scale" */
					if (landscape_orig)
						n_scan = sscanf (line_, "%s %s %s %*s %*s %*s %s %s", c1, t1, t2, c2, c3);
					else
						n_scan = sscanf (line_, "%s %s %s", c1, c2, c3);
					if ((strcmp (c1, "V") && strcmp (c1, "gsave")) || !(n_scan == 3 || n_scan == 5)) {
						int c;
						GMT_Report (API, GMT_MSG_NORMAL, "Error: Parsing of scale after %%%%BeginPageSetup failed\n"
						                                 "\tMaking some assumptions to try to save the situation.\n");
						old_scale_x = 1;		old_scale_y = 1;	/* Assume this */
						/* Receed two lines to position us again in a comment line (hopefully %%EndPageSetup) */
						fseek (fp, -(off_t)(strlen(line_)+strlen(t3)+2U), SEEK_CUR);
						/* But because the line termination issue we must test if we seeked back long enough */
						c = fgetc (fp);
						if (c == '%')
							fseek (fp, -(off_t)(1U), SEEK_CUR);		/* Just receed the read character (file is unix terminate) */
						else
							fseek (fp, -(off_t)(3U), SEEK_CUR);		/* Receed the character and 2 more because file is Win terminated */
					}
					else {
						old_scale_x = atof (c2);		old_scale_y = atof (c3);
					}
					gmt_M_free (GMT, line_);
				}
				else if (BeginPageSetup_here) {
					BeginPageSetup_here = false;
					Ctrl->A.resize = false;       /* Need to reset so it doesn't keep checking inside this branch */
					/* Now we must calculate the new scale */
					if (Ctrl->A.rescale)          /* except if it was set as an option */
						r_x = Ctrl->A.scale;
					else if (Ctrl->A.max) {	/* Check if plot size exceeds specified limits */
						r_x = (w > Ctrl->A.new_size[0]) ? Ctrl->A.new_size[0] / w : 1.0;
						if (Ctrl->A.new_size[1] / h < r_x) r_x = Ctrl->A.new_size[1] / h;	/* Pick the smallest scale */
					}
					else
						r_x = Ctrl->A.new_size[0] / w;

					new_scale_x = new_scale_y = old_scale_x * r_x;
					new_off_x = -xt_bak + xt_bak * r_x;     /* Need to recompute the new offsets as well */
					new_off_y = -yt_bak + yt_bak * r_x;
					Ctrl->A.new_dpi_x = Ctrl->A.new_dpi_y = Ctrl->E.dpi * r_x;
					if (!Ctrl->A.max && Ctrl->A.new_size[1]) {
						r_y = Ctrl->A.new_size[1] / h;
						new_scale_y = old_scale_y * r_y;
						new_off_y = -yt_bak + yt_bak * r_y;
						Ctrl->A.new_dpi_y = Ctrl->E.dpi * r_y;
					}
					else
						r_y = r_x;	/* Not sure of this. Added later to shut up a compiler warning of r_y potentially used uninitialized */

					fprintf (fpo, "%% Recalculate translation and scale to obtain a resized image\n");
					fprintf (fpo, "%g %g translate\n", new_off_x, new_off_y);
					if (landscape_orig) {
						fprintf (fpo, "gsave %g %g translate 90 rotate %g %g scale\n", atof(t1)*r_x, atof(t2)*r_y, new_scale_x, new_scale_y);
					}
					else
						fprintf (fpo, "gsave %g %g scale\n", new_scale_x, new_scale_y);
					set_background = false;
					add_grestore = true;
					possibly_fill_or_outline_BoundingBox (GMT, &(Ctrl->A), fpo);
				}
			}
			else if (set_background) {
				/* Paint or outline the background rectangle. */
				if (!strncmp (line, "%%EndPageSetup", 14)) {
					set_background = false;
					possibly_fill_or_outline_BoundingBox (GMT, &(Ctrl->A), fpo);
				}
			}
			else if (!strncmp (line, "%%Page:", 7)) {
				if (r != 0)
					fprintf (fpo, "%d rotate\n", r);
				if (!gmt_M_is_zero (xt) || !gmt_M_is_zero (yt))
					fprintf (fpo, "%g %g translate\n", xt, yt);
				xt = yt = 0.0;
				r = 0;
			}
#ifdef HAVE_GDAL
			else if (found_proj && !strncmp (line, "%%PageTrailer", 13)) {
				file_line_reader (GMT, &line, &line_size, fp, PS->data, &pos);
				fprintf (fpo, "%%%%PageTrailer\n");
				fprintf (fpo, "%s\n", line);

				/* Write a GeoPDF registration info */

				/* Allocate new control structures */
				to_gdalread = gmt_M_memory (GMT, NULL, 1, struct GMT_GDALREAD_IN_CTRL);
				from_gdalread = gmt_M_memory (GMT, NULL, 1, struct GMT_GDALREAD_OUT_CTRL);
				to_gdalread->W.active = true;
				from_gdalread->ProjRefPROJ4 = proj4_cmd;
				gmt_gdalread (GMT, NULL, to_gdalread, from_gdalread);
				if (from_gdalread->ProjRefWKT != NULL) {
					double x0, y0, x1;	/* Projected coordinates */
					double h0, v0, h1;	/* Correspnding point coordinates */
					double a, H, V;		/* a -> coeff of affine matrix; H,V -> origin shift in projected coords */
					double pX[4], pY[4], lptsX[4], lptsY[4];

					x0 = west;		y0 = south;		x1 = east;	y1 = north;
					h0 = v0 = 0;	/* because we used -A option so origin in points is at (0,0) */
					h1 = h0 + w;
					/* takes the projected coordinate system into the page coordinate system */
					a = (h1 - h0)/(x1 - x0);
					H = h0 - a*x0;
					V = v0 - a*y0;

					/* Use the above matrix to discover where the corners of the map will fall */
					pX[0] = a*x0 + H;	pX[1] = a*x0 + H;	pX[2] = a*x1 + H;	pX[3] = a*x1 + H;
					pY[0] = a*y0 + V;	pY[1] = a*y1 + V;	pY[2] = a*y1 + V;	pY[3] = a*y0 + V;

					/* Now compute the LPTS array */
					lptsX[0] = pX[0] / w;	lptsX[1] = pX[1] / w;	lptsX[2] = pX[2] / w;	lptsX[3] = pX[3] / w;
					lptsY[0] = pY[0] / h;	lptsY[1] = pY[1] / h;	lptsY[2] = pY[2] / h;	lptsY[3] = pY[3] / h;

					fprintf (fpo, "\n%% embed georegistation info\n");
					fprintf (fpo, "[ {ThisPage} <<\n");
					fprintf (fpo, "\t/VP [ <<\n");
					fprintf (fpo, "\t\t/Type /Viewport\n");
					fprintf (fpo, "\t\t/BBox[0 0 %.1f %.1f]\n", w, h);
					fprintf (fpo, "\t\t/Measure <<\n");
					fprintf (fpo, "\t\t\t/Type /Measure\n");
					fprintf (fpo, "\t\t\t/Subtype /GEO\n");
					fprintf (fpo, "\t\t\t/Bounds[0 0 0 1 1 1 1 0]\n");
					fprintf (fpo, "\t\t\t/GPTS[%f %f %f %f %f %f %f %f]\n",
					         south, west, north, west, north, east, south, east);
					fprintf (fpo, "\t\t\t/LPTS[%f %f %f %f %f %f %f %f]\n",
					         lptsX[0],lptsY[0], lptsX[1],lptsY[1], lptsX[2],lptsY[2], lptsX[3],lptsY[3]);
					fprintf (fpo, "\t\t\t/GCS <<\n");
					fprintf (fpo, "\t\t\t\t/Type /PROJCS\n");
					fprintf (fpo, "\t\t\t\t/WKT\n");
					fprintf (fpo, "\t\t\t\t(%s)\n", from_gdalread->ProjRefWKT);
					fprintf (fpo, "\t\t\t>>\n");
					fprintf (fpo, "\t\t>>\n");
					fprintf (fpo, "\t>>]\n");
					fprintf (fpo, ">> /PUT pdfmark\n\n");
				}
				gmt_M_free (GMT, to_gdalread);
				gmt_M_free (GMT, from_gdalread);
				continue;
			}
#endif
			fprintf (fpo, "%s\n", line);
		}
		if (add_grestore)
			fprintf (fpo, "grestore\n");	/* Since we started with gsave to change size */
		/* Recede a bit to test the contents of last line. -7 for when PS has CRLF endings */
		if (fseek (fp, (off_t)-7, SEEK_END))
			GMT_Report (API, GMT_MSG_NORMAL, "Error: Seeking to spot 7 bytes earlier failed\n");
		/* Read until last line is encountered */
		while (file_line_reader (GMT, &line, &line_size, fp, PS->data, &pos) != EOF);
		if (strncmp (line, "%%EOF", 5U))
			/* Possibly a non-closed GMT PS file. To be confirmed later */
			excessK = true;

		fclose (fpo);	fpo = NULL;
		fclose (fp);	fp  = NULL;
		if (delete && gmt_remove_file (GMT, ps_file))	/* Since we created a temporary file from the memdata */
			Return (GMT_RUNTIME_ERROR);

		/* Build the converting ghostscript command and execute it */

		if (Ctrl->T.device != GS_DEV_EPS) {
			char tag[16] = {""};
			int dest_device = Ctrl->T.device;	/* Keep copy in case of temp change below */

			strncpy (tag, &ext[Ctrl->T.device][1], 15U);
			gmt_str_toupper (tag);

			if (transparency) {	/* Get here when PDF is _NOT_ the final output format but an intermediate format */
				GMT_Report (API, GMT_MSG_LONG_VERBOSE, "PS file with transparency must be converted to PDF before creating %s\n", tag);
				/* Temporarily change output device to PDF to get the PDF tmp file */
				Ctrl->T.device = GS_DEV_PDF;
				/* After conversion, we convert the tmp PDF file to desired format via a 2nd gs call */
				GMT_Report (API, GMT_MSG_LONG_VERBOSE, "Convert to intermediate PDF...\n");
				strncat (out_file, &ps_file[pos_file], (size_t)(pos_ext - pos_file));
				strcat (out_file, "_intermediate");
			}
			else {	/* Output is the final result */
					GMT_Report (API, GMT_MSG_LONG_VERBOSE, "Convert to %s...\n", tag);

				if (!Ctrl->F.active || return_image) {
					if (Ctrl->D.active) sprintf (out_file, "%s/", Ctrl->D.dir);	/* Use specified output directory */
					strncat (out_file, &ps_file[pos_file], (size_t)(pos_ext - pos_file));
				}
				else
					strncpy (out_file, Ctrl->F.file, PATH_MAX-1);
			}
			strcat (out_file, ext[Ctrl->T.device]);

			if (Ctrl->A.new_dpi_x) {	/* We have a resize request (was Ctrl->A.resize = true;) */
				pix_w = urint (ceil (w * Ctrl->A.new_dpi_x / 72.0));
				pix_h = urint (ceil (h * Ctrl->A.new_dpi_y / 72.0));
			}
			else {
				pix_w = urint (ceil (w * Ctrl->E.dpi / 72.0));
				pix_h = urint (ceil (h * Ctrl->E.dpi / 72.0));
			}

			sprintf (cmd, "%s%s %s %s%s -sDEVICE=%s %s -g%dx%d -r%d -sOutputFile=%c%s%c -f%c%s%c",
				at_sign, Ctrl->G.file, gs_params, Ctrl->C.arg, alpha_bits(Ctrl), device[Ctrl->T.device],
				device_options[Ctrl->T.device], pix_w, pix_h, Ctrl->E.dpi, quote, out_file, quote, quote, tmp_file, quote);

			if (Ctrl->S.active)	/* Print GhostScript command */
				GMT_Report (API, GMT_MSG_NORMAL, "%s\n", cmd);

			/* Execute the GhostScript command */
			GMT_Report (API, GMT_MSG_DEBUG, "Running: %s\n", cmd);
			sys_retval = system (cmd);
			if (sys_retval) {
				GMT_Report (API, GMT_MSG_NORMAL, "System call [%s] returned error %d.\n", cmd, sys_retval);
				gmt_remove_file (GMT, tmp_file);	/* Since we created a temporary file from the memdata */
				Return (GMT_RUNTIME_ERROR);
			}

			/* Check output file */
			if (access (out_file, R_OK)) {		/* output file not created */
				if (isGMT_PS && excessK)
					/* non-closed GMT input PS file */
					GMT_Report (API, GMT_MSG_NORMAL,
					            "%s: GMT PS format detected but file is not finalized. Maybe a -K in excess? No output created.\n", ps_file);
				else
					/* Either a bad closed GMT PS file or one of unknown origin */
					GMT_Report (API, GMT_MSG_NORMAL,
					            "Could not create %s. Maybe input file does not fulfill PS specifications.\n", out_file);
			}
			else {								/* output file exists */
				if (isGMT_PS && excessK)
					/* non-closed GMT input PS file */
					GMT_Report (API, GMT_MSG_NORMAL,
					            "%s: GMT PS format detected but file is not finalized. Maybe a -K in excess? %s could be messed up.\n",
					            ps_file, out_file);
				/* else: Either a good closed GMT PS file or one of unknown origin */
			}
			if (transparency) {	/* Now convert temporary PDF to desired format */
				char pdf_file[PATH_MAX] = {""};
				GMT_Report (API, GMT_MSG_LONG_VERBOSE, "Convert PDF with transparency to %s...\n", tag);
				Ctrl->T.device = dest_device;	/* Reset output device type */
				strcpy (pdf_file, out_file);	/* Now the PDF is the infile */
				*out_file = '\0'; /* truncate string to build new output file */
				if (!Ctrl->F.active || return_image) {
					if (Ctrl->D.active) sprintf (out_file, "%s/", Ctrl->D.dir);	/* Use specified output directory */
					strncat (out_file, &ps_file[pos_file], (size_t)(pos_ext - pos_file));
				}
				else
					strncpy (out_file, Ctrl->F.file, PATH_MAX-1);
				strcat (out_file, ext[Ctrl->T.device]);
				/* After conversion, convert the tmp PDF file to desired format via a 2nd gs call */
				sprintf (cmd, "%s%s %s %s%s -sDEVICE=%s %s -r%d -sOutputFile=%c%s%c %c%s%c",
					at_sign, Ctrl->G.file, gs_params, Ctrl->C.arg, alpha_bits(Ctrl), device[Ctrl->T.device],
					device_options[Ctrl->T.device],
					Ctrl->E.dpi, quote, out_file, quote, quote, pdf_file, quote);
				if (Ctrl->S.active)	/* Print 2nd GhostScript command */
					GMT_Report (API, GMT_MSG_NORMAL, "%s\n", cmd);
				/* Execute the 2nd GhostScript command */
				GMT_Report (API, GMT_MSG_DEBUG, "Running: %s\n", cmd);
				sys_retval = system (cmd);
				if (sys_retval) {
					GMT_Report (API, GMT_MSG_NORMAL, "System call [%s] returned error %d.\n", cmd, sys_retval);
					Return (GMT_RUNTIME_ERROR);
				}
				if (!Ctrl->S.active && gmt_remove_file (GMT, pdf_file))	/* The temporary PDF file is no longer needed */
					Return (GMT_RUNTIME_ERROR);
			}
		}

		if (GMT->current.setting.run_mode == GMT_MODERN) {
			if (Ctrl->T.ps) {	/* Under modern mode we can also save the PS file by renaming it */
				strncpy (out_file, Ctrl->F.file, PATH_MAX-1);
				strcat (out_file, ".ps");
				GMT_Report (API, GMT_MSG_DEBUG, "Rename %s -> %s\n", GMT->current.ps.filename, out_file);
				if (gmt_rename_file (GMT, GMT->current.ps.filename, out_file))
					Return (GMT_RUNTIME_ERROR);
			}
			else {	/* Delete it, no -Z needed */
				if (gmt_remove_file (GMT, GMT->current.ps.filename))
					Return (GMT_RUNTIME_ERROR);
			}
		}
		else if (Ctrl->Z.active && !delete) {		/* Remove input file, if requested */
			if (gmt_remove_file (GMT, ps_file))
				Return (GMT_RUNTIME_ERROR);
		}

		if (return_image) {	/* Must read in the saved raster image and return via Ctrl->F.file pointer */
			struct GMT_IMAGE *I = NULL;
#ifdef HAVE_GDAL	/* Since GMT_Read_Data with GMT_IS_IMAGE, GMT_IS_FILE means a call to GDAL */
			gmt_set_pad (GMT, 0U);	/* Temporary turn off padding (and thus BC setting) since we will use image exactly as is */
			/* State how we wish to receive images from GDAL */
			if (GMT->current.gdal_read_in.O.mem_layout[0])		/* At one point this should never be allowed to be empty */
				GMT_Set_Default (API, "API_IMAGE_LAYOUT", GMT->current.gdal_read_in.O.mem_layout);
			else
				GMT_Set_Default (API, "API_IMAGE_LAYOUT", "TCBa");
	
			if ((I = GMT_Read_Data (API, GMT_IS_IMAGE, GMT_IS_FILE, GMT_IS_SURFACE, GMT_CONTAINER_AND_DATA, NULL, out_file, NULL)) == NULL) {
				Return (API->error);
			}
#else			/* Here we have already set device to PPM which we can read ourselves. */
			uint64_t dim[GMT_DIM_SIZE] = {0U, 0U, 3U, 0U}; 	/* 3 bands. This might change if we do monochrome at some point */
			uint64_t row, col, band, nCols, nRows, nBands, nXY;
			FILE *fp_raw = NULL;
			unsigned char *tmp;
			int ios;
			if ((fp_raw = fopen (out_file, "rb")) == NULL) {
				GMT_Report (API, GMT_MSG_NORMAL, "Unable to open image file %s\n", out_file);
				Return (GMT_ERROR_ON_FOPEN);
			}
			gmt_fgets (GMT, line, GMT_LEN128, fp_raw);	/* Skip 1st line */
			gmt_fgets (GMT, line, GMT_LEN128, fp_raw);	/* Skip 2nd line */
			gmt_fgets (GMT, line, GMT_LEN128, fp_raw);	/* Get 3rd line */
			if (sscanf (line, "%" PRIu64 " %" PRIu64, &dim[GMT_X], &dim[GMT_Y]) != 2) {
				GMT_Report (API, GMT_MSG_NORMAL, "Unable to decipher size of image in file %s\n", out_file);
				fclose (fp_raw);
				Return (GMT_PARSE_ERROR);
			}
			gmt_fgets (GMT, line, GMT_LEN128, fp_raw);	/* Skip 4th line */
			gmt_set_pad (GMT, 0U);	/* Temporary turn off padding (and thus BC setting) since we will use image exactly as is */
			if ((I = GMT_Create_Data (API, GMT_IS_IMAGE, GMT_IS_SURFACE, GMT_CONTAINER_AND_DATA, dim, NULL, NULL, 0, 0, NULL)) == NULL) {
				GMT_Report (API, GMT_MSG_NORMAL, "Unable to create image structure\n");
				fclose (fp_raw);
				Return (API->error);
			}

			nCols = dim[GMT_X];		nRows = dim[GMT_Y];		nBands = dim[2];	nXY = nRows * nCols;
			tmp   = gmt_M_memory(GMT, NULL, nCols * nBands, char);
			for (row = 0; row < nRows; row++) {
				ios = fread(tmp, sizeof (unsigned char), nCols * nBands, fp_raw);		/* Read a row of nCols by nBands bytes of data */
				for (col = 0; col < nCols; col++) {
					for (band = 0; band < nBands; band++) {
						I->data[row + col*nRows + band*nXY] = tmp[band + col*nBands];
					}
				}
			}
			gmt_M_free (GMT, tmp);
			fclose (fp_raw);	fp_raw = NULL;
#endif
			if (GMT_Write_Data (API, GMT_IS_IMAGE, GMT_IS_FILE, GMT_IS_SURFACE, GMT_CONTAINER_AND_DATA, NULL, Ctrl->F.file, I) != GMT_NOERROR)
				Return (API->error);
			gmt_set_pad (GMT, API->pad);	/* Reset padding to GMT default */
			if (Ctrl->Z.active) {	/* Remove the image since it is returned to a calling program and -Z was given */
				GMT_Report (API, GMT_MSG_LONG_VERBOSE, "Removing %s...\n", out_file);
				if (gmt_remove_file (GMT, out_file))
					Return (GMT_RUNTIME_ERROR);
			}
		}

		GMT_Report (API, GMT_MSG_VERBOSE, "Done.\n");

		if (!Ctrl->S.active) {
			if (!Ctrl->T.eps && gmt_remove_file (GMT, tmp_file))
				Return (GMT_RUNTIME_ERROR);
			if (strlen (no_U_file) > 0 && gmt_remove_file (GMT, no_U_file)) /* empty string == file was not created */
				Return (GMT_RUNTIME_ERROR);
			if (strlen (clean_PS_file) > 0 && gmt_remove_file (GMT, clean_PS_file)) /* empty string == file was not created */
				Return (GMT_RUNTIME_ERROR);
		}

		if (Ctrl->W.active && found_proj && !Ctrl->W.kml) {	/* Write a world file */
			double x_inc, y_inc;
			char world_file[PATH_MAX] = "", *wext = NULL, *s = NULL;

			x_inc = (east  - west)  / pix_w;
			y_inc = (north - south) / pix_h;
			GMT_Report (API, GMT_MSG_VERBOSE, "width = %d\theight = %d\tX res = %f\tY res = %f\n", pix_w, pix_h, x_inc, y_inc);

			/* West and North of the world file contain the coordinates of the
			 * center of the pixel
				 but our current values are of the NW corner of the pixel (pixel
				 registration). So we'll move halph pixel inward. */ west  += x_inc /
			2.0; north -= y_inc / 2.0;

			if (Ctrl->D.active) sprintf (world_file, "%s/", Ctrl->D.dir);	/* Use specified output directory */
			if (Ctrl->F.active) {		/* Must rip the raster file extension before adding the world one */
				for (i = (int)strlen(out_file) - 1; i > 0; i--) {
					if (out_file[i] == '.') { 	/* Beginning of file extension */
						pos_ext = i;
						break;
					}
				}
				out_file[pos_ext] = '\0';
				strcat(world_file, out_file);
			}
			else
				strncat (world_file, &ps_file[pos_file], (size_t)(pos_ext - pos_file));

			s = ext[Ctrl->T.device];
			wext = strdup(ext[Ctrl->T.device]);
			wext[1] = s[1];		wext[2] = s[3];		wext[3] = 'w';
			strcat (world_file, wext);

			if ((fpw = fopen (world_file, "w")) == NULL) {
				GMT_Report (API, GMT_MSG_NORMAL, "Unable to open file %s for writing\n", world_file);
			}
			else {
				fprintf (fpw, "%.12f\n0.0\n0.0\n%.12f\n%.12f\n%.12f", x_inc, -y_inc, west, north);
				fclose (fpw);	fpw = NULL;
				GMT_Report (API, GMT_MSG_LONG_VERBOSE, "Wrote world file %s\n", world_file);
				if (proj4_cmd)
					GMT_Report (API, GMT_MSG_LONG_VERBOSE, "Proj4 definition: %s\n", proj4_cmd);
			}

			gmt_M_str_free (wext);

			if (Ctrl->W.warp && proj4_cmd && proj4_cmd[1] == 'p') {	/* We got a usable Proj4 string. Run it (if gdal is around) */
				/* The true geotiff file will have the same base name plus a .tiff extension.
				   We will reuse the world_file variable because all it is need is to replace the extension */
				for (i = (int)strlen(world_file) - 1; i > 0; i--) {
					if (world_file[i] == '.') { 	/* Beginning of file extension */
						pos_ext = i;
						break;
					}
				}
				world_file[pos_ext] = '\0';
				strcat (world_file, ".tiff");

				if (GMT->current.setting.verbose < GMT_MSG_VERBOSE)	/* Shut up the gdal_translate (low level) verbosity */
					quiet = " -quiet";
				else
					quiet = "";

				sprintf (cmd, "gdal_translate -mo TIFFTAG_XRESOLUTION=%d -mo TIFFTAG_YRESOLUTION=%d -a_srs %c%s%c "
				              "-co COMPRESS=LZW -co TILED=YES %s %c%s%c %c%s%c",
					Ctrl->E.dpi, Ctrl->E.dpi, quote, proj4_cmd, quote, quiet, quote, out_file, quote, quote, world_file, quote);
				gmt_M_str_free (proj4_cmd);
				GMT_Report (API, GMT_MSG_DEBUG, "Running: %s\n", cmd);
				sys_retval = system (cmd);		/* Execute the gdal_translate command */
				GMT_Report (API, GMT_MSG_NORMAL, "The gdal_translate command: \n%s\n", cmd);
				if (sys_retval) {
					GMT_Report (API, GMT_MSG_NORMAL, "System call [%s] returned error %d.\n", cmd, sys_retval);
					Return (GMT_RUNTIME_ERROR);
				}
			}
			else if (Ctrl->W.warp && !proj4_cmd)
				GMT_Report (API, GMT_MSG_NORMAL, "Could not find the Proj4 command in the PS file. No conversion performed.\n");
		}

		else if (Ctrl->W.kml) {	/* Write a basic kml file */
			char kml_file[PATH_MAX] = "";
			if (Ctrl->D.active)
				sprintf (kml_file, "%s/", Ctrl->D.dir);	/* Use specified output directory */
			if (Ctrl->F.active) {		/* Must rip the raster file extension before adding the kml one */
				for (i = (int)strlen(out_file) - 1; i > 0; i--) {
					if (out_file[i] == '.') { 	/* Beginning of file extension */
						pos_ext = i;
						break;
					}
				}
				out_file[pos_ext] = '\0';
				strcat (kml_file, out_file);
				out_file[pos_ext] = '.';	/* Reset the extension */
			}
			else
				strncat (kml_file, &ps_file[pos_file], (size_t)(pos_ext - pos_file));

			strcat (kml_file, ".kml");

			if ((fpw = fopen (kml_file, "w")) == NULL) {
				GMT_Report (API, GMT_MSG_NORMAL, "Unable to open file %s for writing\n", kml_file);
			}
			else {
				if (Ctrl->W.folder) {	/* Control KML overlay vs full file */
					fprintf (fpw, "<Folder>\n\t<name>%s</name>\n", Ctrl->W.foldername);
				}
				else {
					fprintf (fpw, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
					fprintf (fpw, "<kml xmlns=\"http://earth.google.com/kml/2.1\">\n");
				}
				fprintf (fpw, "<Document>\n\t<name>%s</name>\n", Ctrl->W.doctitle);
				fprintf (fpw, "\t<GroundOverlay>\n\t\t<name>%s</name>\n", Ctrl->W.overlayname);
				fprintf (fpw, "\t\t<Icon>\n");
				fprintf (fpw, "\t\t\t<href>");
				if (Ctrl->W.URL) fprintf (fpw, "%s/", Ctrl->W.URL);
				if (Ctrl->D.active) {		/* Do not write the directory name */
					char *p;
					p = strrchr (out_file, '/');
					(p == NULL) ? fprintf (fpw, "%s</href>\n", out_file) : fprintf (fpw, "%s</href>\n", ++p);
				}
				else
					fprintf (fpw, "%s</href>\n", out_file);
				fprintf (fpw, "\t\t</Icon>\n");
				fprintf (fpw, "\t\t<altitudeMode>%s</altitudeMode>\n", RefLevel[Ctrl->W.mode]);
				if (Ctrl->W.mode > KML_GROUND_ABS && Ctrl->W.mode < KML_SEAFLOOR_ABS)
					fprintf (fpw, "\t\t<altitude>%g</altitude>\n", Ctrl->W.altitude);
				fprintf (fpw, "\t\t<LatLonBox>\n");
				fprintf (fpw, "\t\t\t<north>%f</north>\n", GMT->common.R.wesn[YHI]);
				fprintf (fpw, "\t\t\t<south>%f</south>\n", GMT->common.R.wesn[YLO]);
				fprintf (fpw, "\t\t\t<east>%f</east>\n", GMT->common.R.wesn[XHI]);
				fprintf (fpw, "\t\t\t<west>%f</west>\n", GMT->common.R.wesn[XLO]);
				fprintf (fpw, "\t\t</LatLonBox>\n");
				fprintf (fpw, "\t\t<Region>\n");
				fprintf (fpw, "\t\t<LatLonAltBox>\n");
				fprintf (fpw, "\t\t\t<north>%f</north>\n", north);
				fprintf (fpw, "\t\t\t<south>%f</south>\n", south);
				fprintf (fpw, "\t\t\t<east>%f</east>\n", east);
				fprintf (fpw, "\t\t\t<west>%f</west>\n", west);
				fprintf (fpw, "\t\t</LatLonAltBox>\n");
				if (Ctrl->W.min_lod != Ctrl->W.max_lod) {	/* Control layer visibility */
 					fprintf (fpw, "\t\t<Lod>\n");
 					fprintf (fpw, "\t\t\t<minLodPixels>%d</minLodPixels>\n", Ctrl->W.min_lod);
 					fprintf (fpw, "\t\t\t<maxLodPixels>%d</maxLodPixels>\n", Ctrl->W.max_lod);
					if (Ctrl->W.min_fade) fprintf (fpw, "\t\t\t<minFadeExtent>%d</minFadeExtent>\n", Ctrl->W.min_fade);
	 				if (Ctrl->W.max_fade) fprintf (fpw, "\t\t\t<maxFadeExtent>%d</maxFadeExtent>\n", Ctrl->W.max_fade);
 					fprintf (fpw, "\t\t</Lod>\n");
				}
				fprintf (fpw, "\t\t</Region>\n");
				fprintf (fpw, "\t</GroundOverlay>\n");
				if (Ctrl->W.folder)	/* Control KML overlay vs full file */
					fprintf (fpw, "</Folder>\n");
				else
					fprintf (fpw, "</Document>\n</kml>\n");
				fclose (fpw);	fpw = NULL;
				GMT_Report (API, GMT_MSG_LONG_VERBOSE, "Wrote KML file %s\n", kml_file);
			}
		}

		else if (Ctrl->W.active && !found_proj) {
			GMT_Report (API, GMT_MSG_NORMAL, "Could not find the 'PROJ' tag in the PS file. No world file created.\n");
			GMT_Report (API, GMT_MSG_NORMAL, "This situation occurs when one of the two next cases is true:\n");
			GMT_Report (API, GMT_MSG_NORMAL, "1) the PS file was created with a pre-GMT v4.5.0 version\n");
			GMT_Report (API, GMT_MSG_NORMAL, "2) the PS file was not created by GMT\n");
		}
	}

	if (!Ctrl->L.active)		/* Otherwise ps_names contents are the Garbageman territory */
		for (k = 0; k < Ctrl->In.n_files; k++) gmt_M_str_free (ps_names[k]);
	gmt_M_free (GMT, ps_names);
	gmt_M_free (GMT, line);
	gmt_M_free (GMT, PS);
	/* According to Coverity there are still paths that may reach here with the files not closed */
	if (fp != NULL) fclose (fp);
	if (fpo != NULL) fclose (fpo);	if (fpb != NULL) fclose (fpb);
	if (fp2 != NULL) fclose (fp2);	if (fpw != NULL) fclose (fpw);
	GMT_Report (API, GMT_MSG_DEBUG, "Final input buffer length was % "PRIuS "\n", line_size);

	Return (GMT_NOERROR);
}

#ifdef WIN32
GMT_LOCAL int ghostbuster(struct GMTAPI_CTRL *API, struct PS2RASTER_CTRL *C) {
	/* Search the Windows registry for the directory containing the gswinXXc.exe
	   We do this by finding the GS_DLL that is a value of the HKLM\SOFTWARE\GPL Ghostscript\X.XX\ key
	   Things are further complicated because Win64 has TWO registries: one 32 and the other 64 bits.
	   Add to this that the installed GS version may be 32 or 64 bits, so we have to check for the
	   four GS_32|64 + GMT_32|64 combinations.

		 Adapted from snipets at http://www.daniweb.com/software-development/c/code/217174
	   and http://juknull.wordpress.com/tag/regenumkeyex-example */

	HKEY hkey;              /* Handle to registry key */
	char data[GMT_LEN256] = {""}, ver[8] = {""}, *ptr;
	char key[32] = "SOFTWARE\\GPL Ghostscript\\";
	unsigned long datalen = GMT_LEN256;
	unsigned long datatype;
	long RegO, rc = 0;
	int n = 0;
	bool bits64 = true;
	float maxVersion = 0;		/* In case more than one GS, hold the number of the highest version */

#ifdef _WIN64
	RegO = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\GPL Ghostscript", 0, KEY_READ, &hkey);	/* Read 64 bits Reg */
	if (RegO != ERROR_SUCCESS) {		/* Try the 32 bits registry */
		RegO = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\GPL Ghostscript", 0, KEY_READ|KEY_WOW64_32KEY, &hkey);
		bits64 = false;
	}
#else
	RegO = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\GPL Ghostscript", 0, KEY_READ, &hkey);	/* Read 32 bits Reg */
	if (RegO != ERROR_SUCCESS)			/* Failed. Try the 64 bits registry */
		RegO = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\GPL Ghostscript", 0, KEY_READ|KEY_WOW64_64KEY, &hkey);
	else {
		bits64 = false;
	}
#endif

	if (RegO != ERROR_SUCCESS) {
		GMT_Report (API, GMT_MSG_LONG_VERBOSE, "Error opening HKLM key\n");
		return (GMT_RUNTIME_ERROR);
	}

	while (rc != ERROR_NO_MORE_ITEMS) {
		rc  = RegEnumKeyEx (hkey, n++, data, &datalen, 0, NULL, NULL, NULL);
		datalen = GMT_LEN256; /* reset to buffer length (including terminating \0) */
		if (rc == ERROR_SUCCESS)
			maxVersion = MAX(maxVersion, strtof(data, NULL));	/* If more than one GS, keep highest version number */
	}

	RegCloseKey(hkey);

	if (maxVersion == 0) {
		GMT_Report (API, GMT_MSG_LONG_VERBOSE, "Unknown version reported in registry\n");
		return (GMT_RUNTIME_ERROR);
	}

	sprintf(ver, "%.2f", maxVersion);
	strcat(key, ver);

	/* Open the HKLM key, key, from which we wish to get data.
	   But now we already know the registry bitage */
#ifdef _WIN64
	if (bits64)
		RegO = RegOpenKeyEx(HKEY_LOCAL_MACHINE, key, 0, KEY_QUERY_VALUE, &hkey);
	else
		RegO = RegOpenKeyEx(HKEY_LOCAL_MACHINE, key, 0, KEY_QUERY_VALUE|KEY_WOW64_32KEY, &hkey);
#else
	if (bits64)
		RegO = RegOpenKeyEx(HKEY_LOCAL_MACHINE, key, 0, KEY_QUERY_VALUE|KEY_WOW64_64KEY, &hkey);
	else
		RegO = RegOpenKeyEx(HKEY_LOCAL_MACHINE, key, 0, KEY_QUERY_VALUE, &hkey);
#endif
	if (RegO != ERROR_SUCCESS) {
		GMT_Report (API, GMT_MSG_LONG_VERBOSE, "Error opening HKLM key\n");
		return (GMT_RUNTIME_ERROR);
	}

	/* Read the value for "GS_DLL" via the handle 'hkey' */
	RegO = RegQueryValueEx(hkey, "GS_DLL", NULL, &datatype, (LPBYTE)data, &datalen);

	RegCloseKey(hkey);

	if (RegO != ERROR_SUCCESS) {
		GMT_Report (API, GMT_MSG_LONG_VERBOSE, "Error reading the GS_DLL value contents\n");
		return (GMT_RUNTIME_ERROR);
	}

	if ((ptr = strstr(data,"\\gsdll")) == NULL ) {
		GMT_Report (API, GMT_MSG_LONG_VERBOSE, "GS_DLL value is screwed.\n");
		return (GMT_RUNTIME_ERROR);
	}

	/* Truncate string and add affix gswinXXc.exe */
	*ptr = '\0';
	strcat(data, bits64 ? "\\gswin64c.exe" : "\\gswin32c.exe");

	/* Now finally check that the gswinXXc.exe exists */
	if (access (data, R_OK)) {
		GMT_Report (API, GMT_MSG_LONG_VERBOSE, "gswinXXc.exe does not exist.\n");
		return (GMT_RUNTIME_ERROR);
	}

	/* Wrap the path in double quotes to prevent troubles raised by dumb things like "Program Files" */
	C->G.file = malloc (strlen (data) + 3);	/* strlen + 2 * " + \0 */
	sprintf (C->G.file, "\"%s\"", data);

	return (GMT_NOERROR);
}

#endif		/* WIN32 */
