-
Notifications
You must be signed in to change notification settings - Fork 1
/
animstack.scm
2155 lines (1949 loc) · 83.3 KB
/
animstack.scm
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
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; GIMP Animation Tools
;;; by Timofei Shatrov
;;; v. 0.64
(define (display-to-string value)
"Prints anything to string using display function"
(let ((port (open-output-string)))
(display value port)
;; Fun fact: get-output-string is not mentioned once in tinyscheme docs...
(get-output-string port)))
(define (gimp-message* . args)
(let ((port (open-output-string)))
(for-each
(lambda (arg)
(display arg port)
(display " " port))
args)
(gimp-message (get-output-string port))))
(define (vector-for-each fn vec)
"Run fn on each element of vec"
(do ((len (vector-length vec))
(i 0 (+ i 1)))
((= i len))
(fn (vector-ref vec i))))
(define (vector-for-each-i fn vec)
"Run fn on each element of vec"
(do ((len (vector-length vec))
(i 0 (+ i 1)))
((= i len))
(fn (vector-ref vec i) i)))
(define (is-true? fn item)
;; does fn return '(TRUE) ?
(= (car (fn item)) TRUE))
(define (int-round x)
(inexact->exact (round x)))
(define *default-animstack-hash-size* 200)
(define (animstack-hash size hashfn)
;;based on http://www.math.grin.edu/~stone/events/scheme-workshop/hash-tables.html
(let* ((table (make-vector size '()))
(add (lambda (key . value)
(let ((index (hashfn key)))
(vector-set! table index
(cons (cons key value) (vector-ref table index))))))
(init (lambda (assoc-list)
(for-each (lambda (pair) (apply add pair)) assoc-list)))
(get-assoc (lambda (key)
(let ((index (hashfn key)))
(assoc key (vector-ref table index))))))
(lambda (op . args)
(case op
((add) (apply add args))
((init) (apply init args))
((assoc) (apply get-assoc args))
((contents) table)
((stat)
(let ((collisions 0)
(filled 0))
(vector-for-each
(lambda (cell)
(if (pair? cell)
(begin
(set! filled (+ filled 1))
(if (> (length cell) 1) (set! collisions (+ collisions 1))))))
table)
`(("Size:" ,size) ("Filled:" ,filled) ("Collisions:" ,collisions))))
(else (error (string-append "Unsupported hash operation" (symbol->string op))))))))
(define (animstack-hashfn size)
(let* ((seed (random size))
(sqsize (int-round (sqrt size)))
(hashfn
(lambda (obj)
"It's terrible, but quick..."
(cond
((integer? obj) obj)
((char? obj) (char->integer obj))
((string? obj)
(let ((sl (string-length obj)))
(+ sl
(* sqsize (if (> sl 0) (hashfn (string-ref obj 0)) seed))
(if (> sl 1) (hashfn (string-ref obj 1)) seed)
)))
((symbol? obj)
(hashfn (symbol->string obj)))
((else (hashfn (display-to-string obj))))))))
(lambda (obj) (modulo (hashfn obj) size))))
(define (make-animstack-hash assoc-list)
(let* ((size *default-animstack-hash-size*)
(ah (animstack-hash size (animstack-hashfn size))))
(ah 'init assoc-list)
ah))
(define (flatten-layer-group img layer)
"Flatten a single layer group"
;; ok... there is no function for that... gotta do it the hard way...
(let ((layers (cadr (gimp-image-get-layers img))))
(vector-for-each
(lambda (lr) (gimp-item-set-visible lr (if (= lr layer) TRUE FALSE)))
layers)
;; do we need to make every sublayer of layer visible at this point?
(gimp-image-merge-visible-layers img EXPAND-AS-NECESSARY)))
(define (flatten-layer-groups img)
"Flatten all layer groups in an image"
(gimp-image-undo-group-start img)
(let* ((get-layers (gimp-image-get-layers img))
(layers (cadr get-layers))
(visi-status (make-vector (car get-layers)))
(i 0)
)
;; remember visibility status
(vector-for-each
(lambda (layer)
(let ((visible (car (gimp-item-get-visible layer))))
(vector-set! visi-status i visible)
(set! i (+ i 1))))
layers)
;; flatten each layer group
(vector-for-each
(lambda (layer)
(if (is-true? gimp-item-is-group layer)
(flatten-layer-group img layer)))
layers)
;; restore visibility status (note that old layers list is useless now)
(set! i 0)
(vector-for-each
(lambda (layer)
(gimp-item-set-visible layer (vector-ref visi-status i))
(set! i (+ i 1)))
(cadr (gimp-image-get-layers img))))
(gimp-image-undo-group-end img)
(gimp-displays-flush))
(define script-fu-flatten-layer-groups flatten-layer-groups)
(script-fu-register
"script-fu-flatten-layer-groups"
"Flatten Layer Groups"
"Flattens all layer groups in an image"
"Timofei Shatrov"
"Copyright 2012"
"June 15, 2012"
"RGB RGBA GRAY GRAYA"
SF-IMAGE "Image to use" 0
)
(script-fu-menu-register "script-fu-flatten-layer-groups" "<Image>/Image")
;;; Animation stacker
;;; (gimp-image-reorder-item image item parent position)
;;; (gimp-image-insert-layer image layer parent position)
(define (reorder-or-insert-layer image item parent position)
"because you never know..."
(let ((args (list image item parent position)))
(catch ;; actually GIMP still displays the error. it doesn't die, but might scare the user
(catch #f (apply gimp-image-insert-layer args))
(apply gimp-image-reorder-item args))))
(define (groupify-layer img layer)
"If layer is not a layer group, make a layer group containing only layer"
(if (not (is-true? gimp-item-is-group layer))
(let ((group (car (gimp-layer-group-new img)))
(layer-name (car (gimp-item-get-name layer)))
(default-name-function ;; potentially customizable?
(lambda (str) (string-append "+ " str))))
(gimp-image-set-active-layer img layer)
(gimp-item-set-name group (default-name-function layer-name))
(gimp-image-insert-layer img group 0 -1)
(gimp-image-reorder-item img layer group 0)
(animstack-copy-layer-labels layer group)
group)
layer))
(define (put-layer-in-group img layer parent position . rest)
"Position might be a positive (counted from the top) or negative (from the bottom)"
(let* ((group (groupify-layer img parent))
(group-length (car (gimp-item-get-children group)))
(pos (if (< position 0)
(max 0 (+ group-length position 1))
(min group-length position)))
(fn (if (null? rest) reorder-or-insert-layer (car rest))))
(fn img layer group pos)
group))
(define (get-layer-in-group group position)
(if (is-true? gimp-item-is-group group)
(let* ((group-children (gimp-item-get-children group))
(group-length (car group-children))
(pos (if (< position 0)
(max 0 (+ group-length position))
(min (- group-length 1) position))))
(vector-ref (cadr group-children) pos))
group))
(define (string2number str . opt)
"Replacement for string->number, which throws an uncatchable
exception as of GIMP 2.8. Returns #f if not a number."
(let ((s2a (string->atom str))
(fn (if (pair? opt) (car opt) number?)))
(and (fn s2a) s2a)))
(define animstack-save-selection #f)
(define animstack-restore-selection #f)
(let ((sel #f))
(set! animstack-save-selection
(lambda (img)
(set! sel #f)
(if (is-true? gimp-selection-bounds img)
(set! sel (car (gimp-selection-save img))))))
(set! animstack-restore-selection
(lambda (img)
(if sel
(begin
(gimp-selection-load sel)
(gimp-image-remove-channel img sel))
(gimp-selection-none img)))))
;; Tag syntax is [tag] or [tag:parameter] or [tag:param1:param2:...]
;; All parameters should be integers
(define (string-split string char)
(let ((res (list)))
(do ((i (string-length string) (- i 1))
(chunk (list))
(new (lambda (chunk) (cons (list->string chunk) res))))
((<= i 0) (new chunk))
(let ((chr (string-ref string (- i 1))))
(if (char=? chr char)
(begin (set! res (new chunk)) (set! chunk (list)))
(set! chunk (cons chr chunk)))))))
(define (parse-tag-param-simple str)
(let ((valid-param (lambda (x) (or (number? x) (symbol? x)))))
(string2number str valid-param)))
(define (parse-tag-param* str)
(let ((valid-param (lambda (x) (or (number? x) (symbol? x)))))
(if (= (string-length str) 0)
#f
(or (string2number str valid-param) 'err))))
(define (parse-tag-param str)
(let* ((ari (parse-generator-defn str))
(rest (parse-tag-param* (caddr ari))))
(cond ((or (eqv? rest 'err)
(and (= (car ari) 0) (= (cadr ari) 1))) rest)
(else (list (car ari) (cadr ari) rest)))))
(define (parse-animstack-tag string)
"Parse string beginning with [tag]. Returns a pair ( tag . index )
where tag might be #f"
(let* ((split1 (string-split string #\]))
(tagstr (car split1)))
(if (> (length split1) 1)
;;this might be legitimate tag
(let* ((split2 (string-split tagstr #\:))
(tagname (substring (car split2) 1 (string-length (car split2))))
(params (map (if (= (string-length tagname) 0)
parse-tag-param-simple ;;don't do ari stuff on label tags
parse-tag-param)
(cdr split2)))
(pos (+ (string-length tagstr) 1)))
(if (memv 'err params)
(cons #f pos)
(cons (cons tagname params) pos)))
;;cant find closing bracket...
(cons #f (string-length string)))))
(define (extract-animstack-tags layer . params)
"Returns list of animstack tags in the form (tag . parameters)"
(let loop ((layer-name-list (string->list (car (gimp-item-get-name layer)))))
(let ((tagtail (memv #\[ layer-name-list)))
(if tagtail
(let* ((parsed (parse-animstack-tag (list->string tagtail)))
(tag (car parsed))
(next (list-tail tagtail (cdr parsed))))
(if (and tag (or (null? params) ((car params) tag)))
(cons tag (loop next))
(loop next)))
(list)))))
(define (strip-tags string . params)
"Removes all tags from string"
(list->string
(let loop ((str-list (string->list string)))
(cond ((null? str-list) (list))
((char=? (car str-list) #\[)
(let ((rest (memv #\] str-list)))
(if (and rest
(let ((tag (car (parse-animstack-tag (list->string str-list)))))
(and tag (or (null? params) ((car params) tag)))))
(loop (cdr rest))
(cons (car str-list) (loop (cdr str-list)))
)))
(else (cons (car str-list) (loop (cdr str-list))))))))
(define (is-untagged? layer)
(null? (extract-animstack-tags layer)))
(define (pop-params n params)
(let ((pv (make-vector n #f)))
(do ((i 0 (+ i 1))
(pl params (cdr pl)))
((or (null? pl) (>= i n)) pv)
(vector-set! pv i (car pl)))))
(define (process-param-list param-list)
(let ((count 0))
(map
(lambda (param-def)
(prog1
(if (pair? param-def)
`(,(car param-def) (or (vector-ref pv ,count) (begin ,@(cdr param-def))))
`(,param-def (vector-ref pv ,count)))
(set! count (+ count 1))))
param-list)))
;; (with-params (x y z) ....)
(macro (with-params form)
(let ((param-list (cadr form))
(body (cddr form)))
`(let* ((pv (pop-params ,(length param-list) params))
,@(process-param-list param-list))
,@body)))
;; fun fact: this is the single most important function in AnimStack
(define (layer-walk get-layer start next test action limit ignore-first terminate-label)
(let loop ((i start)
(left limit)
(terminate #f))
(let ((layer (get-layer i)))
(cond
((or (not layer) terminate (and left (<= left 0))) #f)
(else
(if (and terminate-label (animstack-layer-has-label layer terminate-label))
(set! terminate #t))
(cond ((and ignore-first (= i start)))
((not (test layer)))
(else
(action layer)
(if left (set! left (- left 1)))))
(loop (next i) left terminate))))))
(define (layer-getter layers)
(let ((maxlen (vector-length layers)))
(lambda (pos)
(and (< -1 pos maxlen) (vector-ref layers pos)))))
(define (default-copy-name item . prefixargs)
(let ((prefix (if (pair? prefixargs) (car prefixargs) "* ")))
(string-append prefix (strip-tags (car (gimp-item-get-name item))))))
(define (get-bindings bindings)
(map (lambda (g) (list (car g) ((cadr g)))) bindings))
(define (apply-effects-simple img effects . rest)
(lambda (layer target)
(for-each (lambda (effect) (apply effect img layer target rest)) effects)))
(define (copy-action img source pos opts)
"Copy source layer and put it into target group at position pos"
(let ((copy-name (default-copy-name source)))
(lambda (target)
(let* ((bindings (get-bindings (cadr opts)))
(apply-effects (apply-effects-simple img (list-ref opts 3)
bindings #f)))
(if (caar opts) (apply-effects source target))
(let ((new (car (gimp-layer-copy source FALSE))))
(gimp-item-set-name new copy-name)
(set! target (put-layer-in-group img new target pos gimp-image-insert-layer))
(process-dup-options* opts img bindings target (dup-getter img new))
(if (not (caar opts)) (apply-effects new target)))))))
(define (animstack-next-fn delta opts)
(let* ((reverse (cadar opts))
(delta (if reverse (- delta) delta)))
(lambda (i) (+ i delta))))
(define (animstack-common delta pos actionfn)
(lambda (img layer opts . params)
(let* ((limit (if (null? params) #f (car params)))
(start (get-start-position img layer opts))
(layers (cadr (gimp-image-get-layers img)))
(getter (layer-getter layers))
(next (animstack-next-fn delta opts))
(action (actionfn img layer pos opts))
(tl (list-ref (car opts) 2)))
(if (and limit (<= limit 0)) (set! limit #f))
(for-each (lambda (effect) (effect img layer #f (list) #f)) (list-ref opts 2))
(layer-walk getter start next is-untagged? action limit #t tl))
(gimp-image-remove-layer img layer)
))
(define animstack-bg (animstack-common -1 -1 copy-action))
(define animstack-fg (animstack-common 1 0 copy-action))
(define (animstack-copy img layer opts . params)
(with-params
((pos -1) limit)
(let* ((fn (animstack-common -1 pos copy-action)))
(fn img layer opts limit))))
(define (noop-action img source pos opts)
"do nothing with source layer, but do execute all the actions"
(lambda (target)
(let* ((bindings (get-bindings (cadr opts)))
(apply-effects (apply-effects-simple img (list-ref opts 3)
bindings #f)))
(apply-effects source target)
(process-dup-options* opts img bindings target (lambda (tgt) source)))))
(define animstack-noop (animstack-common -1 -1 noop-action))
(define (get-roll-layer-list layer)
(if (is-true? gimp-item-is-group layer)
(vector->list (cadr (gimp-item-get-children layer)))
(list layer)))
(define animstack-reset-count #f)
(define animstack-init-count #f)
(define animstack-inc-count #f)
(define animstack-get-count #f)
(define animstack-set-repeat-value #f)
(let* ((count 0)
(repeat-count 0)
(repeat-value 1))
(set! animstack-reset-count
(lambda () (set! repeat-value 1) (set! repeat-count 0)))
(set! animstack-init-count
(lambda (n) (set! count (int-round n)) (set! repeat-count 0)))
(set! animstack-set-repeat-value
(lambda (n) (if (= repeat-count 0) (set! repeat-value (int-round n)))))
(set! animstack-inc-count
(lambda ()
(cond ((> repeat-value 0)
(set! repeat-count (+ repeat-count 1))
(if (>= repeat-count repeat-value)
(begin (animstack-reset-count) (set! count (+ count 1)))))
((= repeat-value 0) (animstack-reset-count))
((< repeat-value 0) (set! count (+ count repeat-value)) (animstack-reset-count)))
count))
(set! animstack-get-count (lambda () count)))
(define (roll-action img source pos opts roll-offset)
(let* ((roll-list (get-roll-layer-list source))
(roll-length (length roll-list))
(count roll-offset))
(if (= roll-length 0) (error "Empty roll"))
(if (< count 0) (set! count (random roll-length)))
(animstack-init-count count)
(lambda (target)
(let* ((pick (- roll-length 1 (modulo (animstack-get-count) roll-length)))
(layer (list-ref roll-list pick))
(bindings (get-bindings (cadr opts)))
(apply-effects (lambda (layer src)
(for-each (lambda (effect)
(effect img layer target bindings src))
(list-ref opts 3)))))
(if (caar opts) (apply-effects layer source))
(let ((new (car (gimp-layer-copy layer FALSE))))
(gimp-item-set-name new (default-copy-name new))
(set! target (put-layer-in-group img new target pos gimp-image-insert-layer))
(process-dup-options* opts img bindings target (dup-getter img new))
(if (not (caar opts)) (apply-effects new #f))
(animstack-inc-count))))))
(define (animstack-roll img layer opts . params)
"Rolls a layer stack or a single layer up the layer list in a given position"
(with-params
((pos -1) limit (roll-offset 0))
(let* ((start (get-start-position img layer opts))
(layers (cadr (gimp-image-get-layers img)))
(getter (layer-getter layers))
(next (animstack-next-fn -1 opts))
(action (roll-action img layer pos opts roll-offset))
(tl (list-ref (car opts) 2)))
(if (and limit (<= limit 0)) (set! limit #f))
(animstack-reset-count)
(for-each (lambda (effect) (effect img layer #f (list) layer)) (list-ref opts 2))
(layer-walk getter start next is-untagged? action limit #t tl)))
(gimp-image-remove-layer img layer))
(define (animstack-splice img layer opts . params)
(with-params
((pos -1) (roll-offset 0))
(let* ((limit (length (get-roll-layer-list layer))))
(animstack-roll img layer opts pos limit roll-offset))))
(define (check-all lst test)
(let loop ((l lst))
(cond ((null? l) #t)
((not (test (car l))) #f)
(else (loop (cdr l))))))
;; matte action
(define (layer-matte-cutout img layer bg-layer threshold)
(animstack-save-selection img)
(if (is-true? gimp-selection-bounds img)
(set! sel (car (gimp-selection-save img))))
(gimp-item-set-visible bg-layer FALSE)
(gimp-image-select-item img CHANNEL-OP-REPLACE layer)
(let ((chl (car (gimp-selection-save img))))
(gimp-selection-none img)
(gimp-drawable-threshold chl HISTOGRAM-VALUE 0 (/ threshold 255))
(gimp-image-select-item img CHANNEL-OP-REPLACE chl)
(gimp-image-remove-channel img chl))
(gimp-layer-add-alpha bg-layer)
(gimp-edit-clear bg-layer)
(gimp-item-set-visible bg-layer TRUE)
(animstack-restore-selection img))
;;TODO: fix terrible copy pasting of copy-action
(define (matte-action threshold img source pos opts)
(let ((copy-name (default-copy-name source)))
(lambda (target)
(let* ((bindings (get-bindings (cadr opts)))
(apply-effects (apply-effects-simple img (list-ref opts 3)
bindings #f)))
(if (caar opts) (apply-effects source target))
(let ((new (car (gimp-layer-copy source FALSE))))
(gimp-item-set-name new copy-name)
(set! target (put-layer-in-group img new target pos gimp-image-insert-layer))
(process-dup-options* opts img bindings target (dup-getter img new))
(if (not (caar opts)) (apply-effects new target))
(layer-matte-cutout img target new threshold))))))
(define (animstack-matte img layer opts . params)
(with-params
((threshold 1) limit)
(let* ((threshold (max threshold 0))
(fn (animstack-common -1 -1
(lambda args (apply matte-action threshold args)))))
(fn img layer opts limit))))
;; delete
(define (delete-action-factory step width)
(lambda (img source pos opts)
(let ((count 0))
(lambda (target)
(let* ((bindings (get-bindings (cadr opts)))
(apply-effects (apply-effects-simple img (list-ref opts 3)
bindings #f)))
(apply-effects source target)
(process-dup-options* opts img bindings target (lambda (tgt) source))
(if (< (modulo count step) width)
(gimp-image-remove-layer img target))
(set! count (+ count 1)))))))
(define (animstack-delete img layer opts . params)
(let* ((pv (pop-params 3 params))
(step (max (or (vector-ref pv 0) 2) 1))
(width (max (or (vector-ref pv 1) (- step 1)) 0))
(limit (vector-ref pv 2))
(action (delete-action-factory step width))
(fn (animstack-common -1 -1 action)))
(fn img layer opts limit)))
(define (render-layer-group img group interval)
"Like flatten-layer-group, but returns a new layer instead of merging"
(let ((name (default-copy-name group "R "))
(layers (cadr (gimp-image-get-layers img)))
(result #f)
(changed '()))
(vector-for-each
(lambda (lr) (gimp-item-set-visible lr (if (= lr group) TRUE FALSE)))
layers)
(if interval
(vector-for-each-i
(lambda (lr i)
(if (not (<= (car interval) i (cdr interval)))
(begin
(set! changed (cons (cons lr (car (gimp-item-get-visible lr))) changed))
(gimp-item-set-visible lr FALSE))))
(cadr (gimp-item-get-children group))))
(set! result (car (gimp-layer-new-from-visible img img name)))
(if (pair? changed)
(for-each (lambda (lv) (gimp-item-set-visible (car lv) (cdr lv))) changed))
(vector-for-each
(lambda (lr) (gimp-item-set-visible lr TRUE))
layers)
result))
(define (animstack-get-interval group only interval)
(if (is-true? gimp-item-is-group group)
(let* ((nlayers (car (gimp-item-get-children group)))
(onlypos (if (< only 0)
(max 0 (+ nlayers only))
(min (- nlayers 1) only))))
(if (< interval 0)
(cons (max 0 (+ onlypos interval)) onlypos)
(cons onlypos (min (- nlayers 1) (+ onlypos interval)))))
#f))
(define (render-action replace under only interval img source pos opts)
(lambda (target)
(let* ((bindings (get-bindings (cadr opts)))
(apply-effects (apply-effects-simple img (list-ref opts 3)
bindings #f))
(interval (if only (animstack-get-interval target only interval) #f))
(realpos (if interval (car interval) 0))
(shift (if under 0 1))
(new (render-layer-group img target interval)))
(if under
(if interval
(set! realpos (+ (cdr interval) 1))
(set! realpos -1)))
(set! target (put-layer-in-group img new target realpos gimp-image-insert-layer))
(process-dup-options* opts img bindings target (dup-getter img new))
(if replace
(let ((test (if interval
(lambda (i) (<= (+ (car interval) shift) i (+ (cdr interval) shift)))
(lambda (i) (not (= i 0))))))
(vector-for-each-i
(lambda (layer i) (if (test i) (gimp-image-remove-layer img layer)))
(cadr (gimp-item-get-children target)))))
(apply-effects new target) ;; render is always non-cumulative
)))
;; [render:limit:replace:only:interval]
(define (animstack-render img layer opts . params)
(with-params
(limit replace only (interval 0))
(let* ((rep (and replace (> replace 0)))
(under (and replace (< replace 0)))
(action (lambda args (apply render-action rep under only interval args)))
(fn (animstack-common -1 0 action)))
(fn img layer opts limit))))
(define (animstack-sample-to-target img source target x y width height)
(animstack-save-selection img)
(gimp-selection-none img)
(gimp-edit-clear target)
(gimp-image-select-rectangle img CHANNEL-OP-REPLACE x y width height)
(if (is-true? gimp-edit-copy source)
(let ((fl (car (gimp-edit-paste target FALSE)))
(offsets (gimp-drawable-offsets target))
(new-width (car (gimp-drawable-width target)))
(new-height (car (gimp-drawable-height target))))
;;(gimp-context-set-interpolation INTERPOLATION-CUBIC)
(gimp-layer-scale fl new-width new-height TRUE)
(gimp-layer-set-offsets fl (car offsets) (cadr offsets))
(gimp-floating-sel-anchor fl)))
(animstack-restore-selection img))
(define (animstack-linear-transition count coords1 coords2)
(if (> count 0)
(let* ((avg (lambda (i n)
(let ((c1 (list-ref coords1 i))
(c2 (list-ref coords2 i)))
(+ c1 (* (/ n count) (- c2 c1))))))
(di (lambda (i)
(let ((c1 (list-ref coords1 i))
(c2 (list-ref coords2 i)))
(/ (- c2 c1) count))))
(dx (di 0))
(dy (di 1))
(dzx (di 2))
(dzy (di 3)))
(lambda (n)
(let* ((newcoords (if (= n count)
coords2
(let loop ((i 0))
(if (< i 4) (cons (avg i n) (loop (+ i 1)))))))
(motion (if (= n count)
(list 0 0 0 0 #t)
(let ((cw (list-ref newcoords 2))
(ch (list-ref newcoords 3)))
(list (/ (+ dx (/ dzx 2)) cw)
(/ (+ dy (/ dzy 2)) ch)
(/ dzx (- cw dzx))
(/ dzy (- ch dzy))
#f)))))
(list newcoords motion))))))
(define (animstack-linear-path count nodes)
(let* ((segment-map (make-vector count))
(m (length nodes))
(node-map (make-vector m))
(first-val (list (car nodes) (list 0 0 0 0 #t))))
(cond
((< count m) (error (string-append "Too many nodes: "
(number->string m)
", no more than count="
(number->string count)
" allowed.")))
((or (= m 1) (= count 1)) (lambda (n) first-val))
(else
(do ((i 0 (+ i 1))) ((>= i (- m 1)))
(vector-set! node-map i (round (* (/ (+ i 1) (- m 1)) (- count 1)))))
(vector-set! segment-map 0 first-val)
(let ((prev 0))
(vector-for-each-i
(lambda (next i)
(if (< i (- m 1))
(let* ((curcoords (list-ref nodes i))
(nextcoords (list-ref nodes (+ i 1)))
(fn (animstack-linear-transition (- next prev) curcoords nextcoords)))
(do ((j (+ prev 1) (+ j 1))) ((> j next))
(vector-set! segment-map j (fn (- j prev))))))
(set! prev next))
node-map))
(lambda (n) (vector-ref segment-map n))))))
(define (get-layer-coords layer)
(let ((offsets (gimp-drawable-offsets layer))
(width (car (gimp-drawable-width layer)))
(height (car (gimp-drawable-height layer))))
(list (car offsets) (cadr offsets) width height)))
(define (get-toplevel-parent layer)
(let ((parent (car (gimp-item-get-parent layer))))
(if (= parent -1)
layer
(get-toplevel-parent parent))))
(define (sampler-count-frames-above img layer opts)
(let* ((reverse (cadar opts))
(tl (list-ref (car opts) 2))
(count 0)
(toplevel (get-toplevel-parent layer))
(pos (car (gimp-image-get-item-position img toplevel)))
(layers (cadr (gimp-image-get-layers img)))
(last (- (vector-length layers) 1))
(terminate #f))
(do ((i pos (+ i (if reverse 1 -1))))
((or terminate (< i 0) (> i last)) count)
(let* ((layer (vector-ref layers i)))
(if (is-untagged? layer)
(set! count (+ count 1)))
(if (and tl (animstack-layer-has-label layer tl))
(set! terminate #t))))))
(define (make-temp-sampler-layer img group width height)
(let ((layer (car (gimp-layer-new img width height RGBA-IMAGE
"Sample layer"
100 NORMAL-MODE))))
(gimp-image-insert-layer img layer group 0)
(gimp-layer-set-offsets layer 0 0)
layer))
(define animstack-get-motion #f)
(define animstack-reset-motion #f)
(define animstack-set-motion #f)
(let ((motion (list 0 0 0 0 #f)))
(set! animstack-get-motion (lambda () motion))
(set! animstack-reset-motion (lambda () (set! motion (list 0 0 0 0 #f))))
(set! animstack-set-motion (lambda (new-motion) (set! motion new-motion))))
(define (sampler-action img temp-layer source path
pos opts roll-mode)
(let* ((roll-list (if (>= roll-mode 0)
(get-roll-layer-list source)
(list source)))
(roll-length (length roll-list))
(roll-count (if (>= roll-mode 0) roll-mode 0))
(sampler-count 0))
(if (= roll-length 0) (error "Empty roll"))
(animstack-init-count roll-count)
(lambda (target)
(let* ((pick (- roll-length 1 (modulo (animstack-get-count) roll-length)))
(src (list-ref roll-list pick))
(pt (path sampler-count))
(bindings (get-bindings (cadr opts)))
(apply-effects (apply-effects-simple img (list-ref opts 3)
bindings #f)))
(apply animstack-sample-to-target img src temp-layer (car pt))
(animstack-set-motion (cadr pt))
(if (caar opts) (apply-effects temp-layer target))
(let ((new (car (gimp-layer-copy temp-layer FALSE))))
(gimp-item-set-name new (default-copy-name new))
(set! target (put-layer-in-group img new target pos gimp-image-insert-layer))
(process-dup-options* opts img bindings target (dup-getter img new))
(if (not (caar opts)) (apply-effects new target))
(animstack-inc-count)
(set! sampler-count (+ sampler-count 1)))))))
(define animstack-remember-image-size #f)
(define animstack-restore-image-size #f)
(let ((width #f)
(height #f))
(set! animstack-remember-image-size
(lambda (img)
(set! width (car (gimp-image-width img)))
(set! height (car (gimp-image-height img)))))
(set! animstack-restore-image-size
(lambda (img)
(gimp-image-resize img width height 0 0))))
;; [sampler:pos:count:limit:roll-mode:width:height]
(define (animstack-sampler img layer opts . params)
(with-params
((pos -1) count limit (roll-mode -1)
(width (car (gimp-image-width img)))
(height (car (gimp-image-height img))))
(let* ((start (get-start-position img layer opts))
(layers (cadr (gimp-image-get-layers img)))
(getter (layer-getter layers))
(next (animstack-next-fn -1 opts))
)
(if (or (not count) (= count 0)) (set! count (max (sampler-count-frames-above img layer opts) 1)))
(let* ((group (groupify-layer img layer))
(temp-layer #f)
(srcs (gimp-item-get-children group))
(src-count (car srcs))
(srcs (cadr srcs))
(source #f)
(node-layers #f))
(if (= src-count 0) (error "Empty sampler"))
(set! source (vector-ref srcs (- src-count 1)))
(if (< count 0) (set! count (length (get-roll-layer-list source))))
(if (or (not limit) (<= limit 0) (> limit count)) (set! limit count))
(animstack-remember-image-size img)
;; need to resize the image so that it includes the whole source
(let ((source-offsets (gimp-drawable-offsets source))
(source-width (car (gimp-drawable-width source)))
(source-height (car (gimp-drawable-height source))))
(gimp-image-resize img (+ (car source-offsets) source-width)
(+ (cadr source-offsets) source-height) 0 0))
(set! node-layers
(if (= src-count 1) (list source)
(cdr (reverse (vector->list srcs)))))
(set! temp-layer (make-temp-sampler-layer img group width height))
(animstack-reset-count)
;; before effects are applied to temp layer
(for-each
(lambda (effect) (effect img temp-layer #f (list) #f))
(list-ref opts 2))
(let* ((nodes (map get-layer-coords node-layers))
(path (animstack-linear-path count nodes))
(action (sampler-action img temp-layer source path
pos opts roll-mode))
(tl (list-ref (car opts) 2)))
(layer-walk getter start next is-untagged? action limit #t tl))
(animstack-restore-image-size img)
(animstack-reset-motion)
(gimp-image-remove-layer img group)
))))
;; Duplicate trees
(define (dup-getter img layer)
(let ((pos (car (gimp-image-get-item-position img layer))))
(lambda (target) (get-layer-in-group target pos))))
(define (process-dup-options dup-options img bindings target getter)
(let* ((opts1 (car dup-options))
(cumulative (car opts1))
(dir (list-ref opts1 3))
;; range set to always true because it gets confusing otherwise
(apply-effects (lambda (layer tgt effects)
((apply-effects-simple img effects bindings #f (lambda (n) #t)) layer tgt)))
(pos (car (gimp-image-get-item-position img target)))
)
(letrec ((branch-out
(lambda (from opts old-be old-de)
(let* ((before-effects (append old-be (caar opts)))
(during-effects (append old-de (cadar opts)))
(branch-from from)
(cur-source from))
(for-each
(lambda (el)
(if (car el)
(branch-out branch-from el
(if (= branch-from from) before-effects '())
during-effects)
(let* ((incpos (if (<= dir 0) (set! pos (+ pos 1))))
(new (duplicate-target-frame img cur-source dir pos))
(layer (getter new)))
(apply-effects layer new (cadr el)) ;; before-effects local
(if (= cur-source from) ;; before-effects branch
(apply-effects layer new before-effects))
(apply-effects layer new during-effects) ;; runtime-effects branch
(apply-effects layer new (cddr el)) ;; runtime-effects local
(set! branch-from new)
(if cumulative (set! cur-source new)))))
(cdr opts))))))
(branch-out target (cdr dup-options) '() '()))))
(define (process-dup-options* opts . rest)
(let ((dup-options (get-dup-opts opts)))
(if dup-options (apply process-dup-options dup-options rest))))
(define (get-dup-opts opts)
(and (> (length opts) 4) (list-ref opts 4)))
(define (get-start-position img layer opts)
(let ((dup-opts (get-dup-opts opts)))
(if dup-opts
(cadar dup-opts)
(car (gimp-image-get-item-position img layer)))))
(define (build-dup-branch layers)
(map (lambda (layer)
(let* ((tags (sort-animstack-tags (extract-animstack-tags layer)))
(before-effects (process-effect-tags (list-ref tags 2) #f))
(during-effects (process-effect-tags (list-ref tags 3) #f)))
(if (is-true? gimp-item-is-group layer)
(cons (list before-effects during-effects)
(build-dup-branch (vector->list (cadr (gimp-item-get-children layer)))))
(cons #f (cons before-effects during-effects)))))
layers))
(define (build-dup-tree dupes opts position dir)
(let ((cumulative (caar opts))
(generator-alist (cadr opts))
(before-effects (list-ref opts 2))
(during-effects (list-ref opts 3)))
(cons (list cumulative position generator-alist dir)
(cons (list before-effects during-effects)
(build-dup-branch dupes)))))
;; [dt:direction]
(define (animstack-dup-tree img layer opts . params)
(with-params
((dir 1))
(let* ((group (groupify-layer img layer))
(contents* (gimp-item-get-children group))
(contents (vector->list (cadr contents*)))
(primary #f)
(dupes #f)
(position (car (gimp-image-get-item-position img group))))
(if (= (car contents*) 0) (error "Empty duplicate tree"))
(if (> dir 0) (set! contents (reverse contents)))
(set! primary (car contents))
(set! dupes (cdr contents))
;; nested dts don't do anything (it just hurts my brain thinking about it)
(if (or (<= (length opts) 4) (not (list-ref opts 5)))
(animstack-process-layer img primary (build-dup-tree dupes opts position dir)))
(gimp-image-remove-layer img group))))
;; Action tag processing
(define (check-tag-params params test)
(check-all params (lambda (x) (or (not x) (test x)))))
(define (animstack-parse-tagname str)
;; (name . (cumulative reverse terminate-label))
(let* ((cumulative #t)
(reverse #f)
(terminate-label #f)
(bake
(lambda (str)
(let* ((split (string-split str #\>)))
(if (= (length split) 2)
(begin
(set! terminate-label (parse-tag-param-simple (cadr split)))
(set! str (car split)))))
(list str cumulative reverse terminate-label))))
(let loop ((str str))
(cond ((= (string-length str) 0) (bake str))
((char=? (string-ref str 0) #\.)
(set! cumulative #f)
(loop (substring str 1 (string-length str))))
((char=? (string-ref str 0) #\~)
(set! reverse #t)
(loop (substring str 1 (string-length str))))
(else (bake str))))))
(define *animstack-action-tag-assocs*
(make-animstack-hash
`(("bg" ,animstack-bg)
("fg" ,animstack-fg)
("copy" ,animstack-copy)
("roll" ,animstack-roll)
("splice" ,animstack-splice)
("noop" ,animstack-noop)
("no" ,animstack-noop)
("matte" ,animstack-matte)
("delete" ,animstack-delete)
("render" ,animstack-render)
("sampler" ,animstack-sampler)
("dt" ,animstack-dup-tree)
)))
(define (animstack-process-tag img layer tag generator-alist before-effects during-effects extra-opts)
(let* ((tagpair (animstack-parse-tagname (car tag))) ;; (tagname . other opts)
(tagname (car tagpair))
(opts (apply list (cdr tagpair) generator-alist before-effects during-effects extra-opts))