Skip to content

Commit c5fcab7

Browse files
committed
file chooser
1 parent 0e5fce1 commit c5fcab7

File tree

6 files changed

+352
-0
lines changed

6 files changed

+352
-0
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
### Obtaining and Using contextfreeart ###
2+
3+
For debian users it should be as simple as `sudo apt-get install cfdg` installs commandline version.
4+
5+
For others check [contextfreeart website][download], if you get the command line version you could easily automate running the cfdg program, once the files have been created.
6+
7+
### Black and White Demo
8+
9+
```ruby
10+
#!/usr/bin/env jruby -v -W2
11+
# frozen_string_literal: true
12+
require 'propane'
13+
# Creates files that you can use with context free art (Gray Scale)
14+
class GrayScale < Propane::App
15+
load_library :file_chooser
16+
attr_reader :img, :data, :skip, :invert
17+
18+
def settings
19+
size 500, 500
20+
end
21+
22+
def setup
23+
sketch_title 'Pixellator for CheChe'
24+
color_mode(HSB, 360, 1.0, 1.0)
25+
fill 0, 0, 200
26+
text('Click Window to Load Image', 10, 100)
27+
@skip = 5 # controls apparent resolution
28+
@data = []
29+
@invert = true
30+
end
31+
32+
def draw
33+
unless img.nil?
34+
img.filter(INVERT) if invert
35+
@invert = false
36+
image(img, 0, 0)
37+
end
38+
end
39+
40+
def write_data(name, data)
41+
df = " %s [x %d y %d s %0.2f hue 0 sat 0.7 brightness 0]\n"
42+
open(data_path('data.cfdg'), 'w') do |pw|
43+
pw.puts format("shape %s{\n", name)
44+
data.each do |row|
45+
pw.puts format(df, *row)
46+
end
47+
pw.puts "}\n"
48+
end
49+
end
50+
51+
def write_start(start, data)
52+
open(data_path(format('%s.cfdg', start)), 'w') do |pw|
53+
pw.puts 'CF::Background = [b 1]'
54+
pw.puts format("startshape %s\n", start)
55+
pw.puts "shape dot{CIRCLE[]}\n"
56+
pw.puts "import data.cfdg\n"
57+
end
58+
write_data start, data
59+
end
60+
61+
def file_selected(selection)
62+
if selection.nil?
63+
puts 'Nothing Chosen'
64+
else
65+
@img = load_image(selection.get_absolute_path)
66+
surface.set_size(img.width, img.height)
67+
end
68+
end
69+
70+
def mouse_clicked
71+
@img = nil
72+
# java_signature 'void selectInput(String, String)'
73+
select_input('Select Image File', 'file_selected')
74+
end
75+
76+
def key_pressed
77+
case key
78+
when 'p', 'P'
79+
export = Thread.new do
80+
pixellate
81+
end
82+
export.join
83+
puts 'done'
84+
when 's', 'S'
85+
save_frame(data_path('original.png'))
86+
else
87+
puts format('key %s was pressed', key)
88+
end
89+
end
90+
91+
def pixellate
92+
load_pixels
93+
shp = 'dot'
94+
(skip...img.width).step(skip) do |x|
95+
(skip...img.height).step(skip) do |y|
96+
pix = pixels[x + y * width]
97+
sz = brightness(pix) * skip
98+
data << [
99+
shp, -width / 2 + x, height / 2 - y, sz.round(2)
100+
] if sz > 0.4
101+
end
102+
end
103+
write_start 'cheche', data
104+
end
105+
end
106+
107+
GrayScale.new
108+
```
109+
110+
If for example the chosen sketch was 590 * 600 pixels you might use the following to generate the pixellated image using the `cli` assuming `haddock.cfdg` is in the local folder. Windows users might need to use different escapes for path, but then they've got a GUI to use if they want.
111+
112+
```bash
113+
cfdg haddock.cfdg -w 1180 -h 1200 -o phil.png
114+
```
115+
116+
But there is much more you can do (look up che che for a much more sophisticated cfdg file).
117+
118+
119+
NB: You could add a grayscale filter if you start with a coloured image. Here we set hue to 0 ie red colour 20 is yellow etc, read more from contextfreeart tutorial (plus have a go at creating a standalone cfdg sketch it is a lot of fun).
120+
121+
122+
[download]:http://www.contextfreeart.org/mediawiki/index.php/Download_page
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#!/usr/bin/env jruby -v -W2
2+
# frozen_string_literal: true
3+
require 'propane'
4+
# Creates files that you can use with context free art (Gray Scale)
5+
class GrayScale < Propane::App
6+
load_library :file_chooser
7+
attr_reader :img, :data, :skip, :invert
8+
9+
def settings
10+
size 500, 500
11+
end
12+
13+
def setup
14+
sketch_title 'Pixellator for CheChe'
15+
color_mode(HSB, 360, 1.0, 1.0)
16+
fill 0, 0, 200
17+
text('Click Window to Load Image', 10, 100)
18+
@skip = 5 # controls apparent resolution
19+
@data = []
20+
@invert = true
21+
end
22+
23+
def draw
24+
unless img.nil?
25+
img.filter(INVERT) if invert
26+
@invert = false
27+
image(img, 0, 0)
28+
end
29+
end
30+
31+
def write_data(name, data)
32+
df = " %s [x %d y %d s %0.2f hue 0 sat 0.7 brightness 0]\n"
33+
open(data_path('data.cfdg'), 'w') do |pw|
34+
pw.puts format("shape %s{\n", name)
35+
data.each do |row|
36+
pw.puts format(df, *row)
37+
end
38+
pw.puts "}\n"
39+
end
40+
end
41+
42+
def write_start(start, data)
43+
open(data_path(format('%s.cfdg', start)), 'w') do |pw|
44+
pw.puts 'CF::Background = [b 1]'
45+
pw.puts format("startshape %s\n", start)
46+
pw.puts "shape dot{CIRCLE[]}\n"
47+
pw.puts "import data.cfdg\n"
48+
end
49+
write_data start, data
50+
end
51+
52+
def file_selected(selection)
53+
if selection.nil?
54+
puts 'Nothing Chosen'
55+
else
56+
@img = load_image(selection.get_absolute_path)
57+
surface.set_size(img.width, img.height)
58+
end
59+
end
60+
61+
def mouse_clicked
62+
@img = nil
63+
# java_signature 'void selectInput(String, String)'
64+
select_input('Select Image File', 'file_selected')
65+
end
66+
67+
def key_pressed
68+
case key
69+
when 'p', 'P'
70+
export = Thread.new do
71+
pixellate
72+
end
73+
export.join
74+
puts 'done'
75+
when 's', 'S'
76+
save_frame(data_path('original.png'))
77+
else
78+
puts format('key %s was pressed', key)
79+
end
80+
end
81+
82+
def pixellate
83+
load_pixels
84+
shp = 'dot'
85+
(skip...img.width).step(skip) do |x|
86+
(skip...img.height).step(skip) do |y|
87+
pix = pixels[x + y * width]
88+
sz = brightness(pix) * skip
89+
data << [
90+
shp, -width / 2 + x, height / 2 - y, sz.round(2)
91+
] if sz > 0.4
92+
end
93+
end
94+
write_start 'cheche', data
95+
end
96+
end
97+
98+
GrayScale.new
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#!/usr/bin/env jruby -v -W2
2+
# frozen_string_literal: true
3+
require 'propane'
4+
# Creates files that you can use with context free art
5+
class Pixellator < Propane::App
6+
load_library :file_chooser
7+
attr_reader :img, :data, :skip, :poster
8+
9+
###########
10+
# Sophisticated example of file chooser.
11+
###########
12+
13+
KEYS = %w(0 4 5 6 7 8 9)
14+
15+
def settings
16+
size 500, 500
17+
end
18+
19+
def setup
20+
sketch_title 'Pixellator'
21+
color_mode(HSB, 360, 1.0, 1.0)
22+
fill 0, 0, 200
23+
text('Click Window to Load Image', 10, 100)
24+
@skip = 20 # controls apparent resolution
25+
@data = []
26+
@poster = 0
27+
end
28+
29+
def draw
30+
image(img, 0, 0) unless img.nil?
31+
filter(POSTERIZE, poster) unless poster == 0
32+
end
33+
34+
def write_data(name, data)
35+
df = " %s [x %d y %d s %0.2f hue %d sat %0.3f brightness 1.0]\n"
36+
open(data_path('data.cfdg'), 'w') do |pw|
37+
pw.puts format("shape %s{\n", name)
38+
data.each do |row|
39+
pw.puts format(df, *row)
40+
end
41+
pw.puts "}\n"
42+
end
43+
end
44+
45+
def write_start(start, data)
46+
open(data_path(format('%s.cfdg', start)), 'w') do |pw|
47+
pw.puts 'CF::Background = [b -1]'
48+
pw.puts format("startshape %s\n", start)
49+
pw.puts "shape dot{CIRCLE[]}\n"
50+
pw.puts "import data.cfdg\n"
51+
end
52+
write_data start, data
53+
end
54+
55+
def file_selected(selection)
56+
if selection.nil?
57+
puts 'Nothing Chosen'
58+
else
59+
@img = load_image(selection.get_absolute_path)
60+
surface.set_size(img.width, img.height)
61+
end
62+
end
63+
64+
def mouse_clicked
65+
@img = nil
66+
# java_signature 'void selectInput(String, String)'
67+
select_input('Select Image File', 'file_selected')
68+
end
69+
70+
def key_pressed
71+
case key
72+
when 'p', 'P'
73+
export = Thread.new do
74+
pixellate
75+
end
76+
export.join
77+
puts 'done'
78+
when 's', 'S'
79+
save_frame(data_path('original.png'))
80+
when *KEYS
81+
@poster = key.to_i
82+
else
83+
puts format('key %s was pressed', key)
84+
end
85+
end
86+
87+
def pixellate
88+
load_pixels
89+
shp = 'dot'
90+
(skip...img.width).step(skip) do |x|
91+
(skip...img.height).step(skip) do |y|
92+
pix = pixels[x + y * width]
93+
sat = saturation(pix)
94+
hue = hue(pix)
95+
sz = brightness(pix) * skip
96+
data << [
97+
shp, -width / 2 + x, height / 2 - y, sz.round(2), hue, sat
98+
] if sz > 0.4
99+
end
100+
end
101+
write_start 'haddock', data
102+
end
103+
end
104+
105+
Pixellator.new
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env jruby -v -W2
2+
# frozen_string_literal: true
3+
require 'propane'
4+
###########
5+
# Example Native File Chooser using vanilla processing
6+
# select_input, and file_selected
7+
###########
8+
class SelectFile < Propane::App
9+
load_library :file_chooser
10+
11+
def setup
12+
sketch_title 'SelectFile'
13+
# java_signature 'void selectInput(String, String)'
14+
select_input('Select a File', 'file_selected')
15+
end
16+
17+
def settings
18+
size 200, 100
19+
end
20+
21+
# signature 'void file_selected(java.io.File file)'
22+
def file_selected(file)
23+
puts file.get_absolute_path unless file.nil?
24+
end
25+
end
26+
27+
SelectFile.new

processing_app/topics/shaders/data/Univers45.vlw renamed to regular/data/Univers45.vlw

File renamed without changes.

processing_app/topics/shaders/kinetic_type.rb renamed to regular/kinetic_type.rb

File renamed without changes.

0 commit comments

Comments
 (0)