summaryrefslogtreecommitdiff
path: root/ipl/gprogs/tilescan.icn
blob: 395d00076418e14292f4b914e83d8fe4a36b5268 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
############################################################################
#
#	File:     tilescan.icn
#
#	Subject:  Program to select tile from an image
#
#	Author:   Ralph E. Griswold
#
#	Date:     December 14, 1997
#
############################################################################
#
#   This file is in the public domain.
#
############################################################################
#
#  This program is designed to assist in locating areas within an image
#  that, when tiled, produce a desired effect.  For example, a background
#  may consist of a tiled image; this program can be used to find the
#  smallest tile for the repeat (by "eye-balling").  It's worth noting
#  that interesting images can be found for other settings.  For example,
#  another interesting use of this program is to produce striped patterns by
#  selecting a row or column of an image to get a tile that is one character
#  wide.  Sometimes a few rows or columns give an interesting "fabric"
#  effect.
#
#  There are three windows:
#
#	the VIB control window
#	the source image window
#	a repeat window, which shows the selection from the source
#	  image, tiled.
#
#  The selection from the source image is shown as a marquee in the
#  source image window.  When a source image is loaded, the marquee starts
#  with the entire image.  The marquee can be changed by buttons and
#  arrow-key events on the control window (not the source image window).
#
#  The arrow keys have two modes.  With no modifier, they nudge the
#  location of the marquee.  With the meta-key modifier, they nudge
#  the dimensions of the marquee.
#
#  The reset button resets the marquee to the entire image.
#
#  The current selection can be mirrored using the mirror button.
#
#  The following features are provided through keyboard shortcuts,
#  the File menu, and in some cases, on-board buttons:
#
#	@M	mirror selection
#	@M	mirror selection
#	@O	open new source image
#	@P	pick a source image from GIF files in the current directory
#	@Q	quit application
#	@S	save current selection as an image
#
#  The repeat window can be resized by the user, but it is not redrawn
#  until the marque is changed or the refresh button is pushed.
#
############################################################################
#
#  Requires:  Version 9 graphics, UNIX (for "pick" feature)
#
############################################################################
#
#  Links:   grecords, interact, mirror, tile
#
############################################################################
#
#  Includes:  keysyms.icn
#
############################################################################

link grecords
link interact
link mirror
link tile

$include "keysyms.icn"

#  Globals related to windows:

global controls		# VIB control window
global pattern		# repeat window
global screen		# source image window visible
global source		# source image window hidden
global symmetry		# mirroring window

global posx		# x position relative to interface window
global posy		# y position relative to repeat window

global sx		# marquee location information
global sy

#  Globals related to the selection:

global current		# current selection record
global hmax		# maximum height of source image
global wmax		# maximum width of source image
global previous		# previous selection record

global vidgets		# table of interface vidgets

procedure main()
   local atts, x1, y1

   atts := ui_atts()
   put(atts,  "posx=10", "posy=10")

   controls := (WOpen ! atts) | ExitNotice("Cannot open control window.")

   vidgets := ui()

   init()

   repeat {
      while *Pending(controls) > 0 do
         ProcessEvent(vidgets["root"], , shortcuts)
      while *Pending(\screen) > 0 do
         if Event(screen) === &lpress then draw_marquee()
      }

end

#  Callback that handles all the buttons that change x, y, w, and h.

procedure dimens_cb(vidget, value)

   if /source then fail

   # Cute code alert:  The selected reversible assignment is performed
   # and passed to check().  It checks the resulting selection rectangle
   # and fails if it's not valid.  That failure causes the reversible
   # assignment to be undone and the expression fails, leaving the
   # selection as it was.

   case value of {
      "w max":  current.w := (wmax - current.x)
      "h max":  current.h := (hmax - current.y)
      "w = 1":  current.w := 1
      "h = 1":  current.h := 1
      "full":   {
         current.h := hmax
         current.w := wmax
         current.x := 0
         current.y := 0
         }
      "w / 2":    check(current.w <- current.w / 2)
      "h / 2":    check(current.h <- current.h / 2)
      "w * 2":    check(current.w <- current.w * 2)
      "h * 2":    check(current.h <- current.h * 2)
      } | fail

   show()

   return

end

procedure draw_marquee()
   local x1, y1

   current.x := &x
   current.y := &y
   current.h := current.w := 0

   update()

   repeat {
      case Event(screen) of {
         &ldrag:  update_marquee()
         &lrelease:  {
            update_marquee()
            Raise(controls)
            return
            }
         }
      }

end

procedure update_marquee()

   if &x < 0 then &x := 0
   if &y < 0 then &y := 0
   if &x > wmax then &x := wmax
   if &y > hmax then &y := hmax
   current.w := &x - current.x
   current.h := &y - current.y

   show()

   return

end

procedure location_cb(vidget, value)

   if /source then fail

   # Cute code alert:  The selected reversible assignment is performed
   # and passed to check().  It checks the resulting selection rectangle
   # and fails if it's not valid.  That failure causes the reversible
   # assignment to be undone and the expression fails, leaving the
   # selection as it was.

   case value of {
      "nw":     current.x := current.y := 0
      "ne":     {
          current.x := wmax - current.w
          current.y := 0
          }
      "se":     {
          current.x := wmax - current.w
          current.y := hmax - current.h
          }
      "sw":     {
          current.x := 0
          current.y := hmax - current.h
          }
      "x max":  current.x := wmax - current.w
      "y max":  current.y := hmax - current.h
      "center": {
         current.x := (wmax - current.w) / 2
         current.y := (hmax - current.h) / 2
         }
      "home":   {
         current.x := 0
         current.y := 0
         }
      "x / 2":    current.x <- current.x / 2
      "y / 2":    current.y <- current.y / 2
      "x * 2":    check(current.x <- current.x * 2)
      "y * 2":    check(current.y <- current.y * 2)
         } | fail
   show()

   return

end

#  Check validity of selection.

procedure check()

   if
      (0 <= current.w <= (wmax - current.x)) &
      (0 <= current.h <= (hmax - current.y)) &
      (0 <= current.x <= hmax) &
      (0 <= current.y <= wmax)
   then return else {
      Alert()
      fail
      }

end

#  Copy hidden source window to a visible window.

procedure copy_source(label)

   screen := WOpen(
      "size=" || WAttrib(source, "width") || "," || WAttrib(source, "height"),
      "posx=" || posx,
      "posy=" || posy,
      "label=" || label,
      "drawop=reverse",
      "linestyle=onoff"
      ) | ExitNotice("Cannot open image window.")

   CopyArea(source, screen)

   Raise(controls)

   wmax := WAttrib(source, "width")
   hmax := WAttrib(source, "height")

   WAttrib(pattern, "width=" || (WAttrib(screen, "width")))
   WAttrib(pattern, "height=" || (WAttrib(screen, "height")))
   EraseArea(pattern)

   current := rect(0, 0, wmax, hmax)

   show()

   return

end

#  File menu callback.

procedure file_cb(vidget, value)

   case value[1] of {
      "open  @O":        get_image()
      "pick  @P":        pick()
      "quit  @Q":        exit()
      "save  @S":        save_cb()
      "save mirrored":   mirror_snap()
      }

   return

end

#  Utility procedure to get new source image.

procedure get_image()

   WClose(\source)
   WClose(\screen)
   WClose(\symmetry)
   EraseArea(pattern)

   repeat {
      (OpenDialog("Open image:") == "Okay") | fail
      source := WOpen("canvas=hidden", "image=" || dialog_value) | {
         Notice("Can't open " || dialog_value || ".")
         next
         }
      copy_source(dialog_value)
      wmax := WAttrib(source, "width")
      hmax := WAttrib(source, "height")
      break
      }

   return

end

#  These values are for Motif; they may need to be changed for other
#  window managers.

$define Offset1	32
$define Offset2	82

#  Initialize the program.

procedure init()
   local iheight

   posx := WAttrib(controls, "width") + Offset1

   iheight := WAttrib(controls, "height")

   pattern := WOpen("label=repeat", "resize=on", "size=" || iheight ||
      "," || iheight, "posx=" || posx, "posy=10") |
         ExitNotice("Cannot open pattern window.")

   posy := WAttrib(pattern, "height") + Offset2

   sx := vidgets["text"].ax
   sy := vidgets["text"].ay

   Raise(controls)

   return

end

procedure update()

   #  Update selection information on interface.

   WAttrib(controls, "drawop=reverse")

   DrawString(controls, sx, sy, "marquee:  x=" || (\previous).x  || "  y=" ||
      previous.y || "  w=" || previous.w || "  h=" || previous.h)
   DrawString(controls, sx, sy, "marquee:  x=" || current.x  || "  y=" ||
      current.y || "  w=" || current.w || "  h=" || current.h)

   WAttrib(controls, "drawop=copy")

   #  Update the selection rectangle.

   DrawRectangle(screen, (\previous).x, previous.y, previous.w, previous.h)
   DrawRectangle(screen, current.x, current.y, current.w, current.h)

   previous := copy(current)

   return

end

procedure mirror_cb()

   if /source then {
      Notice("No source window.")
      fail
      }

   if current.w < 0 then {
      current.w := -current.w
      current.x -:= current.w
      }

   if current.h < 0 then {
      current.h := -current.h
      current.y -:= current.h
      }

   WClose(\symmetry)

   symmetry := mirror(source, current.x, current.y, current.w, current.h) | {
      Notice("Cannot mirror tile.")
      fail
      }

   #  In case the window manager opens a window larger than requested ...

   tile(symmetry, pattern, 0, 0, current.w * 2, current.h * 2)

   #  Hide it but keep it in case the user wants to save it.

   WAttrib(symmetry, "canvas=hidden")

   Raise(controls)

   return

end

procedure mirror_snap()

   snapshot(\symmetry, 0, 0, current.w * 2, current.h * 2) | {
      Notice("No mirrored tile.")
      fail
      }

   return

end

#  Utility procedure to let user pick an image file in the current directory.

procedure pick()
   local plist, ls

   plist := filelist("*.gif *.GIF") | {
      Notice("Pick not supported on this platform.")
      fail
      }

   if *plist = 0 then {
      Notice("No files found.")
      fail
      }

   repeat {
      if SelectDialog("Select image file:", plist, plist[1]) == "Cancel"
         then fail
      WClose(\source)
      WClose(\screen)
      WClose(\symmetry)
      EraseArea(pattern)
      source := WOpen("canvas=hidden", "image=" || dialog_value) | {
         Notice("Cannot open " || dialog_value || ".")
         next
         }
      copy_source(dialog_value)
      break
      }

   return
   
end
   
#  Callback to terminate program execution.

procedure quit_cb()

   exit()

end

procedure refresh_cb()

   tile(source, pattern, current.x, current.y, current.w, current.h)

   return

end

#  Callback procedure to allow use of standard tile sizes.

procedure select_cb(vidget, value)

   if /source then fail

   check(current.w <- current.h <-
      case value of {
         "     4 x 4":    4
         "     8 x 8":    8
         "   16 x 16":   16
         "   32 x 32":   32
         "   64 x 64":   64
         "   72 x 72":   72
         "   96 x 96":   96
         " 100 x 100":  100
         " 128 x 128":  128
         " 200 x 200":  200
         " 256 x 256":  256
         " 400 x 400":  400
         " 512 x 512":  512
         }) | fail

   show()

   return

end

#  Callback to allow setting of specific selection rectangle values.

procedure set_cb()

   repeat {
      if TextDialog("Set values:",
         ["x", "y", "w", "h"],
         [current.x, current.y, current.w, current.h ]
         ) == "Cancel" then fail
      check(
         current.x <- integer(dialog_value[1]) &
         current.y <- integer(dialog_value[2]) &
         current.w <- integer(dialog_value[3]) &
         current.h <- integer(dialog_value[4])
         ) | {
            Notice("Invalid value.")
            next
            }
      show()
      return
      }

end

#  Keyboard shortcuts.

procedure shortcuts(e)

   case type(e) of {
      "string": {
         if &meta then case map(e) of {			# fold case
            "m":   mirror_cb()
            "o":   get_image()
            "p":   pick()
            "q":   exit()
            "s":   save_cb()
            }
         }
      "integer": {
         if &meta then {	# nudge dimensions
            if check(
               case e of {
                  Key_Left:  current.w <- current.w - 1
                  Key_Right: current.w <- current.w + 1
                  Key_Up:    current.h <- current.h - 1
                  Key_Down:  current.h <- current.h + 1
                  }
               ) then show() else fail
            }
         else {			# nudge location
            if check (
               case e of {
                  Key_Left:  current.x <- current.x - 1
                  Key_Right: current.x <- current.x + 1
                  Key_Up:    current.y <- current.y - 1
                  Key_Down:  current.y <- current.y + 1
                  }
               ) then show() else fail
            }
         }
      }

   return

end

#  Procedure to handle all that goes with a new selection.

procedure show()
   local x, y, w, h

   if /source then {
      Notice("No source image.")
      fail
      }

   x := current.x
   y := current.y
   w := current.w
   h := current.h

   if w < 0 then
      x -:= (w := -w)
   if h < 0 then
      y -:= (h := -h)

   tile(source, pattern, x, y, w, h)

   update()

   return

end

#  Utility procedure to save current selection.

procedure save_cb()

   return snapshot(\source, current.x, current.y, current.w, current.h) | {
     Notice("No source image.")
     fail
     }

end

#===<<vib:begin>>===	modify using vib; do not remove this marker line
procedure ui_atts()
   return ["size=445,373", "bg=gray-white"]
end

procedure ui(win, cbk)
return vsetup(win, cbk,
   [":Sizer:::0,0,445,373:",],
   ["dim:Label:::209,34,70,13:dimensions",],
   ["dimens:Choice::9:214,55,64,189:",dimens_cb,
      ["home","w max","h max","w * 2","h * 2",
      "w / 2","h / 2","w = 1","h = 1"]],
   ["file:Menu:pull::0,1,36,21:File",file_cb,
      ["open  @O","pick  @P","save  @S    ","save mirrored","quit  @Q"]],
   ["line1:Line:::0,22,326,22:",],
   ["loc:Label:::120,34,56,13:location",],
   ["location:Choice::11:113,55,71,231:",location_cb,
      ["nw","ne","se","sw","center",
      "x max","y max","x * 2","y * 2","x / 2",
      "y / 2"]],
   ["mirror:Button:regular::17,126,58,20:mirror",mirror_cb],
   ["refresh:Button:regular::17,88,58,20:refresh",refresh_cb],
   ["select:Choice::13:309,55,99,273:",select_cb,
      ["     4 x 4","     8 x 8","   16 x 16","   32 x 32","   64 x 64",
      "   72 x 72","   96 x 96"," 100 x 100"," 128 x 128"," 200 x 200",
      " 256 x 256"," 400 x 400"," 512 x 512"]],
   ["set:Button:regular::17,51,58,20:set",set_cb],
   ["size:Label:::341,34,28,13:size",],
   ["text:Button:regularno::17,347,10,20:",],
   )
end
#===<<vib:end>>===	end of section maintained by vib