############################################################################
#
#	File:     repeater.icn
#
#	Subject:  Program to repeat image
#
#	Author:   Ralph E. Griswold
#
#	Date:     May 2, 2001
#
############################################################################
#
#   This file is in the public domain.
#
############################################################################
#
#  This program repeats images a specified number of times.  The image
#  names are given on the command line.
#
#  The supported options are:
#
#	-h i	repeat horizontally i times, default 1.
#	-v i	repeat vertically i times, default 1.
#	-a i	repeat i times perpendicular to smallest dimension;
#		default 10; and 1 time perpendicular to the largest dimension;
#		overrides -h and 0v.
#	-l i	limit size in repeat direction to i; default 256; only applies
#		if -a is in force.
#	-p s	prefix to prepend to image name, default "rep_".  Can
#		be empty string, in which case the input image is
#		overwritten.
#
############################################################################
#
#  Requires:  Version 9 graphics
#
############################################################################
#
#  Links:  options, tile, wopen
#
############################################################################

link options
link tile
link wopen

procedure main(args)
   local opts, prefix, h_rep, v_rep, win1, win2, name, width, height
   local auto, wdim, hdim, limit

   opts := options(args, "h+v+a+l+p:")

   h_rep := \opts["h"] | 1
   v_rep := \opts["v"] | 1
   prefix := \opts["p"] | "rep_"
   auto := \opts["a"]
   limit := \opts["l"] | 256

   every name := !args do {
      win1 := WOpen("canvas=hidden", "image=" || name) | {
         write(&errout, "*** cannot open ", name)
         next
         }
      width := WAttrib(win1, "width")
      height := WAttrib(win1, "height")
      if \auto then {
         if width > height then {
            hdim := height * auto
            hdim >:= limit
            wdim := width
            }
         else {
            hdim := height
            wdim := width * auto
            wdim >:= limit
            }
         }
      else {
         hdim := height * h_rep
         wdim := width * v_rep
         } 
      win2 := WOpen("canvas=hidden", "width=" || wdim, "height=" || hdim) | {
         write(&errout, "*** cannot open window for repeat")
         WClose(win1)
         next
         }
      tile(win1, win2)
      WriteImage(win2, prefix || name)
      WClose(win1)
      WClose(win2)
      }
end
