MODULE TestWritePS EXPORTS Main; (* Reads a PGM texture and writes an Encapsulated Postscript version of the same *) IMPORT Texture2DGray AS Tx; IMPORT ParseParams, Process, Wr, Thread; IMPORT Math; FROM Stdio IMPORT stderr; TYPE LONG = LONGREAL; BOOL = BOOLEAN; NAT = CARDINAL; INT = INTEGER; Options = RECORD inName: TEXT; (* Input file name. *) outName: TEXT; (* Output file name. *) VX: NAT; (* Brick-row slant of texture. *) rot: LONG; (* Rotation angle, degrees. *) wd, ht: LONG; (* Width and height of plot region. *) pixelSize: LONG; (* Pixel size in mm. *) END; CONST Pi = 3.141592653589793238d0; TwoPi = 2.0d0 * Pi; PROCEDURE Main() = BEGIN WITH o = GetOptions(), a = Tx.LongRealPixel{0.0d0}, s = Tx.LongRealPixel{1.0d0}, t = Tx.Read(o.inName & ".pgm", o.VX, a, s), cos = Math.cos(TwoPi * o.rot/360.0d0), sin = Math.sin(TwoPi * o.rot/360.0d0) DO PROCEDURE PixelToGray(READONLY p: Tx.Pixel): Tx.Color = BEGIN WITH g = 0.5*(FLOAT(p) + 1.0) DO RETURN Tx.Color{g, g, g} END END PixelToGray; PROCEDURE GoodPixel(x, y: INT): BOOL = BEGIN RETURN (2*x+y) MOD 7 # 0 AND (2*y-x) MOD 7 # 0 END GoodPixel; BEGIN Tx.WritePS( name := o.outName & ".eps", t := t, xOrg := 0.5d0 - 0.25d0 * (cos + sin), yOrg := 0.5d0 - 0.25d0 * (cos - sin), wd := o.wd, ht := o.ht, cos := cos, sin := sin, pixelSize := o.pixelSize, pixelColor := PixelToGray, outline := TRUE, defined := GoodPixel ) END END END Main; PROCEDURE GetOptions (): Options = <* FATAL Thread.Alerted, Wr.Failure *> VAR o: Options; CONST MaxOffset = 4093; BEGIN WITH pp = NEW(ParseParams.T).init(stderr) DO TRY IF pp.keywordPresent("-inName") THEN o.inName := pp.getNext(); ELSE o.inName := ""; END; IF pp.keywordPresent("-outName") THEN o.outName := pp.getNext(); ELSE o.outName := ""; END; IF pp.keywordPresent("-offset") THEN o.VX := pp.getNextInt(0, MaxOffset) ELSE o.VX := 0 END; IF pp.keywordPresent("-window") THEN o.wd := pp.getNextLongReal(1.0d0, 1000.0d0); o.ht := pp.getNextLongReal(1.0d0, 1000.0d0); ELSE o.wd := 15.0d0; o.ht := 20.0d0 END; IF pp.keywordPresent("-pixelSize") THEN o.pixelSize := pp.getNextLongReal(0.0d0, 200.0d0) ELSE o.pixelSize := 5.0d0 END; IF pp.keywordPresent("-rot") THEN o.rot := pp.getNextLongReal(-360.0d0, +360.0d0) ELSE o.rot := 0.0d0 END; pp.finish(); EXCEPT | ParseParams.Error => Wr.PutText(stderr, "Usage: TestWritePS\\\n"); Wr.PutText(stderr, " [ -inName ] [ -offset ] \\\n"); Wr.PutText(stderr, " [ -outName ] \\\n"); Wr.PutText(stderr, " [ -rot ] \\\n"); Wr.PutText(stderr, " [ -window ] \\\n"); Wr.PutText(stderr, " [ -pixelSize ] \n"); Process.Exit (1); END; END; RETURN o END GetOptions; BEGIN Main(); END TestWritePS.