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 / picktile.icn < prev    next >
Text File  |  2001-05-02  |  5KB  |  165 lines

  1. ############################################################################
  2. #
  3. #    File:     picktile.icn
  4. #
  5. #    Subject:  Program to pick a tile out of an image
  6. #
  7. #    Author:   Ralph E. Griswold
  8. #
  9. #    Date:     May 2, 2001
  10. #
  11. ############################################################################
  12. #
  13. #   This file is in the public domain.
  14. #
  15. ############################################################################
  16. #
  17. #  This program provides an optionally magnified view of an image file.
  18. #  Clicking on a pixel produces a pattern specification for the tile
  19. #  with the selected upper-left corner.
  20. #
  21. #  Options are:
  22. #
  23. #    -z i    zoom factor, default 1 (no magnification)
  24. #    -f    use fixed size tiles rather than selection; default selection
  25. #    -w i    width of tile, default 32
  26. #    -h i    height of tile, default width
  27. #    -I    pick tiles to make icons; implies -z2, -f, -w38, -w38 (the
  28. #          larger size leaves room for error and trimming)
  29. #    -R i    specs for ResEdit files; i = 32 or 16
  30. #     -t    trim whitepace around tile
  31. #
  32. #  Typical usage is
  33. #
  34. #    picktile image.xbm >image.tle
  35. #
  36. #  The program terminates if "q" is pressed when in the image window.
  37. #
  38. ############################################################################
  39. #
  40. #  Requires:  Version 9 graphics
  41. #
  42. ############################################################################
  43. #
  44. #  Links:  options, patxform, win, xcompat
  45. #
  46. ############################################################################
  47.  
  48. link options
  49. link patxform
  50. link win
  51. link xcompat
  52.  
  53. procedure main(args)
  54.    local pixmap, wix, hix, c, x, y, event, opts, base, magnif, cols, rows
  55.    local arglist, state, x0, x1, y0, y1, pattern
  56.  
  57.    opts := options(args, "tz+w+h+If")
  58.    magnif := \opts["z"] | 1
  59.    cols := \opts["w"] | 32
  60.    rows := \opts["h"] | cols
  61.  
  62.    if \opts["I"] then {
  63.       magnif := 2
  64.       cols := rows := 38
  65.       opts["f"] := 1
  66.       }
  67.  
  68.    pixmap := XBind(, , ,"image=" || args[1]) |
  69.       stop("*** cannot open image file")
  70.  
  71.    wix := WAttrib(pixmap, "width")
  72.    hix := WAttrib(pixmap, "height")
  73.  
  74.    win(magnif * wix, magnif * hix)
  75.  
  76. #  Build the magnified image.
  77.  
  78. #  But if the magnification happens to be 1, don't do it the dumb way.
  79.  
  80.    if magnif = 1 then
  81.       CopyArea(pixmap, &window)
  82.  
  83.    else {
  84.       every y := 0 to hix - 1 do {
  85.          arglist := []
  86.  
  87.          every x := 0 to wix - 1 do {
  88.             c := Pixel(pixmap, x, y, 1, 1)
  89.             if c == "0,0,0" then {
  90.                every put(arglist, (magnif * x) | (magnif * y) | magnif | magnif)
  91.                }
  92.             x +:= 1
  93.             }
  94.  
  95.          if *arglist > 0 then FillRectangle ! arglist
  96.          }
  97.       }
  98.  
  99.    if \opts["f"] then {                # let user pick corners
  100.       while event := Event() do {
  101.          case event of {
  102.             "q":   exit()
  103.             &lpress | &mpress | &rpress: {
  104.                pattern := pix2pat(pixmap, &x / magnif, &y / magnif, cols, rows)
  105.                if \opts["t"] then pattern := rows2pat(ptrim(pat2rows(pattern)))
  106.                write(pattern)
  107.                }
  108.             }
  109.          }
  110.       }
  111.  
  112.  
  113.    else {                    # let user drag to select area
  114.       state := "pick"                # waiting for user to pick
  115.    
  116.       WAttrib("drawop=reverse")
  117.       WAttrib("linestyle=dashed")
  118.    
  119.       while event := Event() do {
  120.          if event === "q" then exit()
  121.          case state of {
  122.             "pick": {                # pick the upper-left corner
  123.                if event === &lpress then {
  124.                   x1 := x0 := &x        # initial coordinates
  125.                   y1 := y0 := &y
  126.                   DrawRectangle(x0, y0, 0, 0)    # start the selection rectangle
  127.                   state := "select"        # now select the rectangle
  128.                   }
  129.                }
  130.             "select": {                # select the rectangle
  131.                case event of {
  132.                   &ldrag: {            # searching ...
  133.                      DrawRectangle(x0, y0, x1 - x0, y1 - y0) # erase rectangle
  134.                      x1 := &x            # new lower-right
  135.                      y1 := &y
  136.                      DrawRectangle(x0, y0, x1 - x0, y1 - y0) # new rectangle
  137.                      }
  138.                   &lrelease: {            # got it!
  139.                      DrawRectangle(x0, y0, x1 - x0, y1 - y0) # erase rectangle
  140.                      x1 := &x            # new lower-right
  141.                      y1 := &y
  142.                      DrawRectangle(x0, y0, x1 - x0, y1 - y0) # new rectangle
  143.                      state := "decide"            # now decide
  144.                      }
  145.                   }
  146.                }
  147.             "decide": {                # is it wanted or not?
  148.                DrawRectangle(x0, y0, x1 - x0, y1 - y0) # erase rectangle
  149.                if event === &lpress then {
  150.                   if (x0 <= &x <= x1) & (y0 <= &y <= y1) then {
  151.                      pattern := pix2pat(pixmap, x0 / magnif, y0 / magnif,
  152.                         (x1 - x0) / magnif, (y1 - y0) / magnif)
  153.                      if \opts["t"] then
  154.                         pattern := rows2pat(ptrim(pat2rows(pattern)))
  155.                      write(pattern)
  156.                      }
  157.                   }
  158.                state := "pick"            # go for another
  159.                }
  160.             }
  161.          }
  162.       }
  163.  
  164. end
  165.