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 / flohisto.icn < prev    next >
Text File  |  2002-03-26  |  5KB  |  194 lines

  1. ############################################################################
  2. #
  3. #    File:     flohisto.icn
  4. #
  5. #    Subject:  Program to display float histograms
  6. #
  7. #    Author:   Ralph E. Griswold
  8. #
  9. #    Date:     March 26, 2002
  10. #
  11. ############################################################################
  12. #
  13. #  This file is in the public domain.
  14. #
  15. ############################################################################
  16. #
  17. #  This program analyzes the floats in a drawdown GIF whose name is given
  18. #  on the command line and an image file showing the floats.
  19. #
  20. #  The command-line option -n s allows a basename for the image file
  21. #  to be specified.  The default is "floats".
  22. #
  23. #  If the command-line option -v is given, the float window is displayed
  24. #  and held until there until it is dismissed by a quit event.
  25. #
  26. ############################################################################
  27. #
  28. #  Requires:  Version 9 graphics
  29. #
  30. ############################################################################
  31. #
  32. #  Links:  numbers, options, wopen
  33. #
  34. ############################################################################
  35.  
  36. link wopen
  37. link numbers
  38. link options
  39.  
  40. $define FloatMax 15
  41. $define Width 300
  42. $define Gutter 20
  43. $define Height 250
  44. $define Delta 9
  45. $define Gap 4
  46. $define Xoff 20
  47. $define Yoff 30
  48.  
  49. procedure main(args)
  50.    local front, back, black, white, opts, name, i, canvas
  51.    local warp_front, warp_back, weft_front, weft_back, win
  52.  
  53.    opts := options(args, "n:v")
  54.  
  55.    name := (\opts["n"] | "floats") || ".gif"
  56.  
  57.    canvas := if \opts["v"] then "canvas=normal" else "canvas=hidden"
  58.  
  59.    WOpen("image=" || args[1], "canvas=hidden") |
  60.       stop("*** cannot open drawdown GIF")
  61.  
  62.    front := win2rows(&window)
  63.    
  64.    back := copy(front)
  65.  
  66.    #  0 = black, 1 = white.
  67.  
  68.    every i := 1 to *back do
  69.       back[i] := map(back[i], "10", "01")
  70.  
  71.    weft_front := analyze(front, "1")
  72.  
  73.    front := rot(front)
  74.  
  75.    warp_front := analyze(front, "0")
  76.  
  77.    weft_back := analyze(back, "1")
  78.  
  79.    back := rot(back)
  80.  
  81.    warp_back := analyze(back, "0")
  82.  
  83.    win := WOpen("size=" || (2 * Width + 2 * Gutter) || "," ||
  84.       (2 * Height + 2 * Gutter), canvas) |
  85.          stop("*** cannot open main window")
  86.  
  87.    CopyArea(plot(warp_front, "warp front"), win, , , , , 0, 0)
  88.    CopyArea(plot(weft_front, "weft front"), win, , , , , Width + Gutter, 0)
  89.    CopyArea(plot(warp_back, "warp back"), win, , , , , 0, Height + Gutter)
  90.    CopyArea(plot(weft_back, "weft back"), win, , , , , Width + Gutter,
  91.       Height + Gutter)
  92.  
  93.    if \opts["v"] then WDone(win)
  94.  
  95.    WriteImage(win, name)
  96.  
  97. end
  98.  
  99. procedure analyze(rows, color)
  100.    local counts, length, row, k, count_list
  101.  
  102.    counts := table(0)
  103.  
  104.    every row := !rows do {
  105.       row ? {
  106.          while tab(upto(color)) do {
  107.             length := *tab(many(color))
  108.             if length > 1 then counts[length] +:= 1
  109.             }
  110.          }
  111.       }
  112.  
  113.    if *counts = 0 then fail    # no floats
  114.  
  115.    count_list := list(FloatMax, 0)    # list of counts
  116.  
  117.    every k := key(counts) do
  118.       if k > FloatMax then count_list[FloatMax] +:= counts[k]
  119.       else count_list[k - 1] := counts[k]
  120.  
  121.    return count_list
  122.  
  123. end
  124.  
  125. procedure plot(data, legend)
  126.    local i, j, scale, maximum, y, width, win
  127.  
  128.    win := WOpen("size=" || Width || "," || Height, "font=times,10", "canvas=hidden") |
  129.       stop("*** cannot open plotting window")
  130.  
  131.    WAttrib(win, "dx=" || Xoff)
  132.    WAttrib(win, "dy=" || (Yoff + Gap))
  133.  
  134.    DrawLine(win, 0, 0 - Gap, Width, 0 - Gap)
  135.    DrawLine(win, 0, 0 - Gap, 0, Height - Gap)
  136.  
  137.    DrawString(win, -2, -(18 + Gap), legend)
  138.  
  139.    if /data then return win
  140.  
  141.    maximum := max ! data
  142.    maximum := integer((maximum + 99.0) / 100) * 100    # get to next hundred
  143.  
  144.    width := real(Width - 2 * Xoff)
  145.    scale := width / maximum
  146.  
  147.    every i := 0 to 4 do
  148.       CenterString(win, (width / 4) * i, 18 - Yoff, (maximum / 4) * i)
  149.  
  150.    every j := 2 to FloatMax + 1 do {
  151.       y := (j - 2) * (Delta + Gap)
  152.       FillRectangle(win, 0, y, data[j - 1] * scale, Delta)
  153.       if j > FloatMax then j := ">"
  154.       RightString(win, 15 - Xoff, y + Gap, j)
  155.       }
  156.  
  157.    return win
  158.  
  159. end
  160.  
  161. procedure win2rows(win)
  162.    local width, height, row, rows, pixel, y
  163.  
  164.    width := WAttrib(win, "width")
  165.    height := WAttrib(win, "height")
  166.  
  167.    rows := []
  168.  
  169.    every y := 0 to height - 1 do {
  170.       row := ""
  171.       every pixel := Pixel(win, 0, y, width, 1) do
  172.          row ||:= if pixel == "0,0,0" then "0" else "1"
  173.       put(rows, row)
  174.       }
  175.  
  176.    return rows
  177.  
  178. end
  179.  
  180. procedure rot(rows)
  181.    local cols, row, grid, i
  182.  
  183.    cols := list(*rows[1], "")
  184.  
  185.    every row := !rows do {
  186.       i := 0
  187.       every grid := !row do
  188.          cols[i +:= 1] := grid || cols[i]
  189.       }
  190.  
  191.    return cols
  192.  
  193. end
  194.