############################################################################
#
#	File:     poller.icn
#
#	Subject:  Program to record image as pixel coordinates
#
#	Author:   Ralph E. Griswold
#
#	Date:     December 30, 1998
#
############################################################################
#
#  This file is in the public domain.
#
############################################################################
#
#  This program reads an image whose name is given on the command line and
#  writes it out as an Icon list of pixels in the form of an include file.
#  See the documentation below for details.
#
############################################################################
#
#  Requires:  Version 9 graphics
#
############################################################################
#
#  Links:  lists, wopen
#
############################################################################

link lists
link wopen

procedure main(args)
   local colors, width, height, x, y, c

   WOpen("image=" || args[1]) | stop("*** cannot open image")

   colors := table()

   width := WAttrib("width")
   height := WAttrib("height")

   x := y := 0

   #  Build table of argument lists for colors

   every c := Pixel() do {
      x +:= 1
      if x % width = 0 then {
         x := 0
         y +:= 1
         }
      /colors[c] := []			# new color
      put(colors[c], x, y)
      }

   #  Write Icon code for an include file.  A list of argument lists
   #  is assigned to "pixels".  Each argument list consists of the
   #  color followed by the pixel coordinates at which that color
   #  occurs
   #
   #  The last element of the list is a three-element list giving the
   #  width, height, and number of colors in the image.  Note that this
   #  is an easily accessible location and that it "solves" the problem
   #  that all previous lines are termianted by commas, so without it
   #  either there would be a trailing empty element in "pixels"
   #  or some painful code would be necessary to avoid it.

   write("pixels:=[")

   every c := key(colors) do {
      push(colors[c], c)
      write(limage(colors[c]), ",")
      }

   write("[", width, ",", height, ",", *colors,  "]")
   write("]")

end
