Skip to content

Commit e622c01

Browse files
committed
add readme for cfdg
1 parent 1571ee1 commit e622c01

File tree

2 files changed

+206
-0
lines changed

2 files changed

+206
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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 cfdg program.
6+
7+
### Black and White Demo
8+
9+
```ruby
10+
load_library :chooser
11+
attr_reader :img, :data, :skip, :invert
12+
13+
def settings
14+
size 500, 500
15+
end
16+
17+
def setup
18+
sketch_title 'Pixellator'
19+
color_mode(HSB, 360, 1.0, 1.0)
20+
resizable
21+
fill 0, 0, 200
22+
text('Click Window to Load Image', 10, 100)
23+
@skip = 5 # controls apparent resolution
24+
@data = []
25+
@invert = true
26+
end
27+
28+
def draw
29+
unless img.nil?
30+
img.filter(INVERT) if invert
31+
@invert = false
32+
image(img, 0, 0)
33+
end
34+
end
35+
36+
def write_data(name, data)
37+
df = " %s [x %d y %d s %0.2f hue 0 sat 0.7 brightness 0]\n"
38+
open(data_path('data.cfdg'), 'w') do |pw|
39+
pw.puts format("shape %s{\n", name)
40+
data.each do |row|
41+
pw.puts format(df, *row)
42+
end
43+
pw.puts "}\n"
44+
end
45+
end
46+
47+
def write_start(start, data)
48+
open(data_path(format('%s.cfdg', start)), 'w') do |pw|
49+
pw.puts 'CF::Background = [b 1]'
50+
pw.puts format("startshape %s\n", start)
51+
pw.puts "shape dot{CIRCLE[]}\n"
52+
pw.puts "import data.cfdg\n"
53+
end
54+
write_data start, data
55+
end
56+
57+
def file_selected(selection)
58+
if selection.nil?
59+
puts 'Nothing Chosen'
60+
else
61+
@img = load_image(selection.get_absolute_path)
62+
surface.set_size(img.width, img.height)
63+
end
64+
end
65+
66+
def mouse_clicked
67+
@img = nil
68+
# java_signature 'void selectInput(String, String)'
69+
select_input('Select Image File', 'file_selected')
70+
end
71+
72+
def key_pressed
73+
case key
74+
when 'p', 'P'
75+
export = Thread.new do
76+
pixellate
77+
end
78+
export.join
79+
puts 'done'
80+
when 's', 'S'
81+
save_frame(data_path('original.png'))
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+
sz = brightness(pix) * skip
94+
data << [
95+
shp, -width / 2 + x, height / 2 - y, sz.round(2)
96+
] if sz > 0.4
97+
end
98+
end
99+
write_start 'haddock', data
100+
end
101+
```
102+
103+
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.
104+
105+
```bash
106+
cfdg haddock.cfdg -w 1180 -h 1200 -o phil.png
107+
```
108+
109+
But there is much more you can do (look up che che for a much more sophisticated cfdg file).
110+
111+
112+
NB: You could add grayscale filter if you are strating with a coloured image.
113+
114+
115+
[download]:http://www.contextfreeart.org/mediawiki/index.php/Download_page
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
load_library :chooser
2+
attr_reader :img, :data, :skip, :invert
3+
4+
def settings
5+
size 500, 500
6+
end
7+
8+
def setup
9+
sketch_title 'Pixellator'
10+
color_mode(HSB, 360, 1.0, 1.0)
11+
resizable
12+
fill 0, 0, 200
13+
text('Click Window to Load Image', 10, 100)
14+
@skip = 5 # controls apparent resolution
15+
@data = []
16+
@invert = true
17+
end
18+
19+
def draw
20+
unless img.nil?
21+
img.filter(INVERT) if invert
22+
@invert = false
23+
image(img, 0, 0)
24+
end
25+
end
26+
27+
def write_data(name, data)
28+
df = " %s [x %d y %d s %0.2f hue 0 sat 0.7 brightness 0]\n"
29+
open(data_path('data.cfdg'), 'w') do |pw|
30+
pw.puts format("shape %s{\n", name)
31+
data.each do |row|
32+
pw.puts format(df, *row)
33+
end
34+
pw.puts "}\n"
35+
end
36+
end
37+
38+
def write_start(start, data)
39+
open(data_path(format('%s.cfdg', start)), 'w') do |pw|
40+
pw.puts 'CF::Background = [b 1]'
41+
pw.puts format("startshape %s\n", start)
42+
pw.puts "shape dot{CIRCLE[]}\n"
43+
pw.puts "import data.cfdg\n"
44+
end
45+
write_data start, data
46+
end
47+
48+
def file_selected(selection)
49+
if selection.nil?
50+
puts 'Nothing Chosen'
51+
else
52+
@img = load_image(selection.get_absolute_path)
53+
surface.set_size(img.width, img.height)
54+
end
55+
end
56+
57+
def mouse_clicked
58+
@img = nil
59+
# java_signature 'void selectInput(String, String)'
60+
select_input('Select Image File', 'file_selected')
61+
end
62+
63+
def key_pressed
64+
case key
65+
when 'p', 'P'
66+
export = Thread.new do
67+
pixellate
68+
end
69+
export.join
70+
puts 'done'
71+
when 's', 'S'
72+
save_frame(data_path('original.png'))
73+
else
74+
puts format('key %s was pressed', key)
75+
end
76+
end
77+
78+
def pixellate
79+
load_pixels
80+
shp = 'dot'
81+
(skip...img.width).step(skip) do |x|
82+
(skip...img.height).step(skip) do |y|
83+
pix = pixels[x + y * width]
84+
sz = brightness(pix) * skip
85+
data << [
86+
shp, -width / 2 + x, height / 2 - y, sz.round(2)
87+
] if sz > 0.4
88+
end
89+
end
90+
write_start 'haddock', data
91+
end

0 commit comments

Comments
 (0)