-
Notifications
You must be signed in to change notification settings - Fork 1
/
split-image.red
87 lines (81 loc) · 2.17 KB
/
split-image.red
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
Red [
Title: "split-image"
Author: "Galen Ivanov"
]
context [
split-side: func [
size [integer!]
step [integer!]
/local parts
][
either step > size [
reduce [size]
][
parts: make block! n: to-integer size / step
append/dup parts step n
total: sum parts
if positive? rem: size - total [append parts rem]
parts
]
]
fix-sizes: func [
size [integer!]
steps [block!]
/local parts
][
parts: make block! 10
total: 0
forall steps [
append parts steps/1
total: total + absolute steps/1
if total >= size [break]
]
either positive? rem: size - total [
append parts rem
][
change back tail parts rem + last parts
]
parts
]
init-side: func [
size [integer!]
parts [integer! block!]
][
either integer? parts [split-side size parts][fix-sizes size parts]
]
init-grid: func [
size [pair!]
x-sz [integer! block!]
y-sz [integer! block!]
][
xs: init-side size/x x-sz
ys: init-side size/y y-sz
reduce [xs ys]
]
set 'split-image function [
img [image!]
x-sz [integer! block!]
y-sz [integer! block!]
][
res: init-grid img/size x-sz y-sz
xs: res/1
ys: res/2
y-offs: 0
collect [
foreach y ys [
if positive? y [
x-offs: 0
foreach x xs [
if positive? x [
keep draw/transparent as-pair x y compose [
image (img) crop (as-pair x-offs y-offs) (as-pair x-offs + x y-offs + y)
]
]
x-offs: x-offs + absolute x
]
]
y-offs: y-offs + absolute y
]
]
]
]