home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / historic / v941.tgz / icon.v941src.tar / icon.v941src / ipl / gprogs / giftoims.icn < prev    next >
Text File  |  2001-06-10  |  3KB  |  112 lines

  1. ############################################################################
  2. #
  3. #    File:     giftoims.icn
  4. #
  5. #    Subject:  Program to convert GIF files to image strings
  6. #
  7. #    Author:   Ralph E. Griswold
  8. #
  9. #    Date:     June 10, 2001
  10. #
  11. ############################################################################
  12. #
  13. #   This file is in the public domain.
  14. #
  15. ############################################################################
  16. #
  17. #  This program converts GIF images whose names are given on the command
  18. #  line to image strings as used by DrawImage().
  19. #
  20. #  The image strings are written to files with the basenames of the GIF
  21. #  files and the suffix "ims" or "iml" depending on the output option.
  22. #
  23. #  The following options are supported:
  24. #
  25. #    -l    write Icon literal instead of plain string; suffix is
  26. #          .iml (default .ims).
  27. #    -i i    make lines of literals at most i characters long
  28. #    -p s    palette to use; default c1.
  29. #
  30. #  For -l, the length refers to the number of characters represented.  If
  31. #  they require escapes, thea actual line length will be longer.  This is
  32. #  to prevent errors from trying to continue a string literal in the
  33. #  middle of an escape sequence.  In addition, three blanks are prepended
  34. #  to each line and the characters # and $ are escaped to prevent then
  35. #  from being misinterpreted by Icon's translator.
  36. #
  37. #  .iml files are suitable for inclusion in program text, either
  38. #  directly or by $include.
  39. #
  40. #  .ims files are suitable for reading.
  41. #
  42. ############################################################################
  43. #
  44. #  Requires:  Version 9 graphics
  45. #
  46. ############################################################################
  47. #
  48. #  Links:  basename, graphics, options, strings
  49. #
  50. ############################################################################
  51.  
  52. link basename
  53. link graphics
  54. link options
  55. link strings
  56.  
  57. procedure main(args)
  58.    local file, opts, name, output, literal, length, seg, palette, str
  59.    local suffix
  60.  
  61.    opts := options(args, "i+lp:")
  62.  
  63.    literal := opts["l"]
  64.    length := opts["i"]
  65.    palette := \opts["p"] | "c1"
  66.  
  67.    suffix := if \literal then ".iml" else ".ims"
  68.  
  69.    if not PaletteChars(palette) then
  70.       stop("*** invalid palette specification")
  71.  
  72.    every file := !args do {
  73.       name := basename(file, ".gif") || suffix
  74.       WOpen("canvas=hidden", "image=" || file) | {
  75.          write(&errout, "**** can't open ", file)
  76.          next
  77.          }
  78.       output := open(name, "w") | {
  79.          write(&errout, "*** can't write to ", name)
  80.          next
  81.          }
  82.       str := Capture(palette)
  83.       if /literal then writes(output, str)
  84.       else {
  85.          if  /length then str ? {
  86.          length := integer(tab(upto(',')))
  87.          }
  88.          str ? {
  89.             write(output, "   \"", tab(upto(',') + 1), tab(upto(',') + 1), "_")
  90.             while seg := move(length) do {
  91.                if pos(0) then write(output, "   ", esc(seg), "\"")
  92.                else write(output, "   ", esc(seg), "_")
  93.                }
  94.             if not pos(0) then write(output, "   ", esc(tab(0)), "\"")
  95.             }
  96.          }
  97.       close(output)
  98.       WClose()
  99.       }
  100.  
  101. end
  102.  
  103. procedure esc(s)
  104.  
  105.    s := image(s)
  106.    s := replace(s, "$", "\\x24")
  107.    s := replace(s, "#", "\\x23")
  108.  
  109.    return s[2:-1]
  110.  
  111. end
  112.