-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageX.rb
180 lines (162 loc) · 5.03 KB
/
ImageX.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
180
# encoding: ascii-8bit
require 'zlib'
class Image
RGB = 2
RGBA = 6
#-----------------------------------------------
# Header BMP
#-----------------------------------------------
def self.header_bmp(width, height)
# BMP header
[19778, (((24 * width + 31) >> 5) * height << 2) + 54, 54,
# DIB header
40, width, height, 1, 24].pack('slx4l4s2x24')
end
#-----------------------------------------------
# Load BMP
#-----------------------------------------------
def self.load_bmp(filename)
width, height, data = File.binread(filename).unpack('x18l2x28a*')
image = new(width, height)
padding = width & 3
w3 = width << 3
index = -3
index_image = (width * height.pred).pred << 2
height.times {
width.times {image.write(index_image += 4, 0, data.byteslice(index += 3, 3) << 255, 4)}
index_image -= w3
index += padding
}
image
end
#-----------------------------------------------
# Save BMP
#-----------------------------------------------
def save_bmp(filename)
File.open(filename,'wb') {|file|
file << Image.header_bmp(width, height)
size = width * height << 2
data = ' ' * size
read(0, 0, data, size)
padding = ' ' * (width & 3)
index = size - (width.succ << 2)
w = width << 3
height.times {
width.times {file << data.byteslice(index += 4, 3)}
file << padding
index -= w
}
}
end
#-----------------------------------------------
# Load PNG
#-----------------------------------------------
def self.load_png(filename)
image = nil
File.open(filename,'rb') {|file|
width = height = color_type = 0
# Signature
file.pos = 8
loop {
size, type = file.read(8).unpack('NA4')
case type
when 'IHDR'
width, height, color_type = file.read(13).unpack('N2xC')
when 'IDAT' # Support only single IDAT chunk
image = new(width, height)
data = Zlib::Inflate.inflate(file.read(size)).reverse!
index_image = -4
index = data.size
if color_type == RGB
height.times {
index -= 1
width.times {image.write(index_image += 4, 0, data.byteslice(index -= 3, 3) << 255, 4)}
}
else
height.times {
index -= 1
width.times {image.write(index_image += 4, 0, data.byteslice(index -= 3, 3) << data.getbyte(index -= 1), 4)}
}
end
when 'IEND'
break
else file.pos += size
end
# CRC
file.pos += 4
}
}
image
end
#-----------------------------------------------
# Save PNG
#-----------------------------------------------
def save_png(filename, color_type = RGB)
size = width * height << 2
data = ' ' * size
read(0, 0, data, size)
data.reverse!
img_data = ''
index = size.succ
if color_type == RGB
height.times {
img_data << 0
width.times {img_data << data.byteslice(index -= 4, 3)}
}
else
height.times {
img_data << 0
width.times {img_data << data.byteslice(index -= 4, 3) << data.getbyte(index.pred)}
}
end
Image.save_png_data(filename, img_data, width, height, color_type)
end
#-----------------------------------------------
# Save PNG data
#-----------------------------------------------
def self.save_png_data(filename, data, width, height, color_type)
File.binwrite(filename, "\x89PNG\r\n\x1a\n" <<
chunk('IHDR', [width, height, 8, color_type].pack('N2C2x3')) <<
chunk('IDAT', Zlib::Deflate.deflate(data, 9)) <<
"\0\0\0\0IEND\xAEB`\x82")
end
#-----------------------------------------------
# Chunk
#-----------------------------------------------
def self.chunk(type, data)
[data.size, type << data, Zlib.crc32(type)].pack('NA*N')
end
#-----------------------------------------------
# Save SVG
#-----------------------------------------------
def save_svg(filename, r = nil, g = nil, b = nil)
size = width * height << 2
data = ' ' * size
read(0, 0, data, size)
File.open(filename,'w') {|file|
file << "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"#{width}\" height=\"#{height}\""
if r and g and b
file.printf(' style="background:#%02x%02x%02x"', r, g, b)
background = [b, g, r].pack('C3')
end
file << '>'
paths = Hash.new {|h,k| h[k] = ''}
c = data.byteslice(index = 0, 3)
height.times {|y|
i = 0
color = c
width.times {|x|
if color != c
paths[color] << sprintf('M%d %dh%dv1H%d', i, y, x - i, i) if color != background
i = x
color = c
end
c = data.byteslice(index += 4, 3)
}
paths[color] << sprintf('M%d %dh%dv1H%d', i, y, width - i, i) if color != background
}
paths.each {|color,path| file.printf('<path d="%s" fill="#%02x%02x%02x"/>', path, *color.bytes.reverse!)}
file << '</svg>'
}
end
end