(* :Name: GSGraphics` *) (* :Title: Graphics output using GhostScript and GSView. *) (* :Author: Szabolcs Horvát *) (* :Package Version: 0.1 *) (* :Summary: This package makes it easy to preview graphics generated by Mathematica with GSView on Windows, and to export nice antialiased graphics to raster formats using convert.exe from ImageMagick. Get GSView from http://www.cs.wisc.edu/~ghost/gsview/ and ImageMagick from http://www.imagemagick.org/script/index.php Search for $GSview and $GSconvert below and set the paths. *) BeginPackage["GSGraphics`"] (* Symbols for export must be mentioned here, so that they become part of the GSGraphics` context, and not GSGraphics`Private`. *) ExportRaster::badformat = "Unsupported file format. The format is deduced from the file extension." ExportRaster::usage = "ExportRaster[\"file.ext\", expr] exports an expression to a raster image format corresponding to ext. See $RasterFormats for supported formats. It accepts the option ImageResolution." $RasterFormats = {"png", "jpg", "jpeg", "gif", "bmp", "pcx", "tga", "tiff", "xpm"} GhostDisplay::usage = "Display graphics in GSView. Use the $ShowGS and $ShowDefault symbols to turn on or off graphics display in GSView and the Front End." $ShowDefault = False (* Show graphics in the FE *) $ShowGS = True (* Show graphics in GSView *) Begin["`Private`"] $GSTempFile = ToFileName[$TemporaryPrefix, "mma_ghost.eps"] (* Set the path to GSView and convert.exe from ImageMagick here. The path must not contain any spaces! *) $GSview = "C:\\Progra~1\\Ghostgum\\gsview\\gsview32.exe" $GSconvert = "C:\\Progra~1\\ImageMagick\\convert.exe" GhostDisplay[gr_] := ( If[$ShowGS, Display[$GSTempFile, gr, "EPS"]; Run["start", $GSview, "-e", $GSTempFile]; ]; If[$ShowDefault, Display[$Display, gr], gr]) Options[ExportRaster] = {ImageResolution -> 96} ExportRaster[outfile_, gr_, opts___?OptionQ] := With[{tempfile = ToFileName[$TemporaryPrefix, "mma_gs_temp.eps"], res = ImageResolution /. {opts} /. Options[ExportRaster]}, If[ ! Or@@(StringMatchQ[outfile, "*."<> #]& /@ $RasterFormats), Message[ExportRaster::badformat];Null, Display[tempfile, gr, "EPS"]; Run[$GSconvert, "-density", res, tempfile, outfile]; DeleteFile[tempfile]; outfile ] ] (* Initialisation *) cleanTempFiles[] := (Off[DeleteFile::"nffil"]; DeleteFile[$GSTempFile]) If[FreeQ[OwnValues@$Epilog, cleanTempFiles], If[ValueQ[$Epilog], OwnValues[$Epilog] = OwnValues[$Epilog] /. (mm_ :> val_) :> (mm :> (cleanTempFiles[]; val));, $Epilog := cleanTempFiles[] ]] (* set $DisplayFunction -- first cache current value (if value hasn't already been cached) *) (* If[!ValueQ[System`Private`$OriginalDisplayFunction], System`Private`$OriginalDisplayFunction = $DisplayFunction ]; *) $DisplayFunction = GhostDisplay End[] EndPackage[]