-
Notifications
You must be signed in to change notification settings - Fork 1
/
triangle-pixels.rb
executable file
·180 lines (134 loc) · 4.11 KB
/
triangle-pixels.rb
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
#!/usr/bin/env ruby
# encoding: utf-8
require "rubygems"
require "RMagick"
require "haml"
include Magick
class Pixel
def tile
Tile.new(red, green, blue, 1.0)
end
end
class Tile
attr_accessor :red, :green, :blue, :norm
def initialize(red = 0, green = 0, blue = 0, norm = 0.0)
@red = red
@green = green
@blue = blue
@norm = norm
end
def +(rhs)
Tile.new(red + rhs.red, green + rhs.green, blue + rhs.blue, norm + rhs.norm)
end
def *(alpha)
Tile.new(alpha * red, alpha * green, alpha * blue, alpha * norm)
end
def diff(rhs)
# calculate the squared l2 norm of the difference
rhs = rhs.to_pixel.to_hsla
lhs = to_pixel.to_hsla
(lhs[0] + rhs[0]) ** 2 + (lhs[1] + rhs[1]) ** 2 + (lhs[2] + rhs[2]) ** 2
end
def color
throw "uargh" unless red == red
throw "uargh" unless green == green
throw "uargh" unless blue == blue
scale = 256 * norm
if scale > 0.0
sprintf "#%02x%02x%02x", red / scale, green / scale, blue / scale
else
"#000000"
end
end
def normalized
if norm == 0.0
self
else
Tile.new(red / norm, green / norm, blue / norm, 1.0)
end
end
def to_pixel
t = normalized
Pixel.new(t.red, t.green, t.blue, 0)
end
def to_s
"#{red},#{green},#{blue} * #{norm}"
end
end
class Float
def sign
s = self <=> 0.0
if s == 0 then return 1 else return s end
end
end
no_cropping = ARGV.delete("--keep")
if ARGV.size < 1
$stderr.puts "Usage: ./triangle-pixels.rb IMAGE_FILE NUMBER_OF_SQUARE_COLUMNS RESULTING_PIXELS_PER_SQUARE [--keep]"
$stderr.puts " Optional '--keep' will keep possibly not completely filled edges."
$stderr.puts " Resulting svg is written to stdout."
$stderr.puts
exit -1
end
image = ImageList.new(ARGV[0]).first
width = (ARGV[1] || 20).to_i
resulting_pixels_per_square = (ARGV[2] || 20).to_i
height = (width / (image.columns.to_f / image.rows)).to_i + 1
$stderr.puts "Image: '#{image.filename}'"
$stderr.puts "Columns: #{width}"
$stderr.puts "Rows: #{height}"
$stderr.puts "Pixels per square: #{resulting_pixels_per_square}x#{resulting_pixels_per_square}"
if no_cropping
width -= 1
height -= 1
end
pixels_per_square = image.columns.to_f / width
buckets = Array.new(width + 1) {
Array.new(height + 1) {
{
:left => Tile.new,
:right => Tile.new,
:top => Tile.new,
:bottom => Tile.new
}
}
}
sections = {1 => {-1 => :top, 1 => :right}, -1 => {1 => :bottom, -1 => :left}}
image.each_pixel do |pixel, column, row|
x = (column % pixels_per_square)
y = (row % pixels_per_square)
xalpha = [1, pixels_per_square - x].min
yalpha = [1, pixels_per_square - y].min
grid = [[column, xalpha], [column + 1, 1 - xalpha]].product [[row, yalpha], [row + 1, 1 - yalpha]]
grid.each do |things|
column = things[0][0]
xalpha = things[0][1]
row = things[1][0]
yalpha = things[1][1]
if xalpha == 0 || yalpha == 0
next
end
bucket_col = (column / pixels_per_square).to_i
bucket_row = (row / pixels_per_square).to_i
# 0.0 <= x,y <= 1.0
x = (column % pixels_per_square) / pixels_per_square
y = (row % pixels_per_square) / pixels_per_square
s = sections[(x - y).sign][(x + y - 1).sign]
alpha = xalpha * yalpha
buckets[bucket_col][bucket_row][s] += pixel.tile * alpha
end
end
buckets.each do |columns|
columns.each do |cell|
a = (cell[:right].diff cell[:top]) + (cell[:left].diff cell[:bottom])
b = (cell[:left].diff cell[:top]) + (cell[:right].diff cell[:bottom])
if a < b
cell[:right] = cell[:top] = (cell[:right].normalized + cell[:top].normalized)
cell[:left] = cell[:bottom] = (cell[:left].normalized + cell[:bottom].normalized)
else
cell[:left] = cell[:top] = (cell[:left].normalized + cell[:top].normalized)
cell[:right] = cell[:bottom] = (cell[:right].normalized + cell[:bottom].normalized)
end
end
end
engine = Haml::Engine.new(File.read("#{File.expand_path(File.dirname(__FILE__))}/triangle-pixels.haml"))
puts engine.render(Object.new, {:buckets => buckets, :scale => resulting_pixels_per_square, :no_cropping => no_cropping})