Skip to content

Commit be69a5e

Browse files
committed
2 parents e622c01 + 2cf68af commit be69a5e

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
load_library :chooser
2+
attr_reader :img, :data, :skip, :poster
3+
4+
###########
5+
# Sophisticated example of file chooser.
6+
###########
7+
8+
KEYS = %w(0 4 5 6 7 8 9)
9+
10+
def settings
11+
size 500, 500
12+
end
13+
14+
def setup
15+
sketch_title 'Pixellator'
16+
color_mode(HSB, 360, 1.0, 1.0)
17+
resizable
18+
fill 0, 0, 200
19+
text('Click Window to Load Image', 10, 100)
20+
@skip = 20 # controls apparent resolution
21+
@data = []
22+
@poster = 0
23+
end
24+
25+
def draw
26+
image(img, 0, 0) unless img.nil?
27+
filter(POSTERIZE, poster) unless poster == 0
28+
end
29+
30+
def write_data(name, data)
31+
df = " %s [x %d y %d s %0.2f hue %d sat %0.3f brightness 1.0]\n"
32+
open(data_path('data.cfdg'), 'w') do |pw|
33+
pw.puts format("shape %s{\n", name)
34+
data.each do |row|
35+
pw.puts format(df, *row)
36+
end
37+
pw.puts "}\n"
38+
end
39+
end
40+
41+
def write_start(start, data)
42+
open(data_path(format('%s.cfdg', start)), 'w') do |pw|
43+
pw.puts 'CF::Background = [b -1]'
44+
pw.puts format("startshape %s\n", start)
45+
pw.puts "shape dot{CIRCLE[]}\n"
46+
pw.puts "import data.cfdg\n"
47+
end
48+
write_data start, data
49+
end
50+
51+
def file_selected(selection)
52+
if selection.nil?
53+
puts 'Nothing Chosen'
54+
else
55+
@img = load_image(selection.get_absolute_path)
56+
surface.set_size(img.width, img.height)
57+
end
58+
end
59+
60+
def mouse_clicked
61+
@img = nil
62+
# java_signature 'void selectInput(String, String)'
63+
select_input('Select Image File', 'file_selected')
64+
end
65+
66+
def key_pressed
67+
case key
68+
when 'p', 'P'
69+
export = Thread.new do
70+
pixellate
71+
end
72+
export.join
73+
puts 'done'
74+
when 's', 'S'
75+
save_frame(data_path('original.png'))
76+
when *KEYS
77+
@poster = key.to_i
78+
else
79+
puts format('key %s was pressed', key)
80+
end
81+
end
82+
83+
def pixellate
84+
load_pixels
85+
shp = 'dot'
86+
(skip...img.width).step(skip) do |x|
87+
(skip...img.height).step(skip) do |y|
88+
pix = pixels[x + y * width]
89+
sat = saturation(pix)
90+
hue = hue(pix)
91+
sz = brightness(pix) * skip
92+
data << [
93+
shp, -width / 2 + x, height / 2 - y, sz.round(2), hue, sat
94+
] if sz > 0.4
95+
end
96+
end
97+
write_start 'haddock', data
98+
end

0 commit comments

Comments
 (0)