swdyh / quilt

a ruby library for generating identicon

This URL has Read+Write access

swdyh (author)
Fri Sep 19 05:28:47 -0700 2008
commit  504c3aee7df8f135bb0fad83f3c18eaf498b9966
tree    e415a53d71f7f7a18736839168b5cb292f967b21
parent  ce5cb6d7cc9a804df7bfbc56e68cc88ee1d0ea08
quilt / lib / quilt.rb
100644 267 lines (226 sloc) 6.247 kb
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# -*- coding: utf-8 -*-
require 'rubygems'
require 'digest/sha1'
 
module Quilt
  class ImageRmagick
    def initialize width, height
      require 'RMagick'
      @image = Magick::Image.new width, height
      @image.format = 'png'
    end
 
    def color r, g, b
      sprintf('#%02x%02x%02x', r, g, b)
    end
 
    def transparent color
      @image.transparent color
    end
 
    def fill_rect x, y, _x, _y, color
      dr = Magick::Draw.new
      dr.fill color
      dr.rectangle x, y, _x, _y
      dr.draw @image
    end
 
    def polygon points, color
      unless points.empty?
        dr = Magick::Draw.new
        dr.fill color
        dr.polygon *(points.flatten)
        dr.draw @image
      end
    end
 
    def write path
      open(path, 'w') {|f| f.puts @image.to_blob }
    end
 
    def to_blob
      @image.to_blob
    end
 
    def resize size
      @image.resize! size, size
    end
  end
 
  class ImageGD
    def initialize width, height
      require 'GD'
      @image = GD::Image.new width, height
    end
 
    def color r, g, b
      @image.colorAllocate r, g, b
    end
 
    def transparent color
      @image.transparent color
    end
 
    def fill_rect x, y, _x, _y, color
      @image.filledRectangle x, y, _x, _y, color
    end
 
    def polygon points, color
      poly = GD::Polygon.new
      points.each do |i|
        poly.addPt i[0], i[1]
      end
      @image.filledPolygon poly, color
    end
 
    def write path
      File.open(path, 'w') do |f|
        @image.png f
      end
    end
 
    def to_blob
      @image.pngStr
    end
 
    def resize size
      _image = GD::Image.new size, size
      # FIXME bug
      @image.copyResized _image, 0, 0, 0, 0, size, size, @image.width, @image.height
      @image = _image
    end
  end
 
  class Identicon
    PATCHES = [
               [0, 4, 24, 20, 0],
               [0, 4, 20, 0],
               [2, 24, 20, 2],
               [0, 2, 22, 20, 0],
               [2, 14, 22, 10, 2],
               [0, 14, 24, 22, 0],
               [2, 24, 22, 13, 11, 22, 20, 2],
               [0, 14, 22, 0],
               [6, 8, 18, 16, 6],
               [4, 20, 10, 12, 2, 4],
               [0, 2, 12, 10, 0],
               [10, 14, 22, 10],
               [20, 12, 24, 20],
               [10, 2, 12, 10],
               [0, 2, 10, 0],
               [],
              ]
    CENTER_PATCHES = [0, 4, 8, 15]
    PATCH_SIZE = 5
    @@image_lib = ImageRmagick
    @@salt = ''
    attr_reader :code
 
    def initialize str = '', opt = {}
      case opt[:type]
      when :code
        @code = str.to_i
      when :ip
        @code = Identicon.ip2code str
      else
        @code = Identicon.calc_code str.to_s
      end
      @decode = decode @code
 
      if opt[:size]
        @scale = (opt[:size].to_f / (PATCH_SIZE * 3)).ceil
        @resize_to = opt[:size]
      else
        @scale = opt[:scale] || 1
      end
 
      @patch_width = PATCH_SIZE * @scale
      @image = @@image_lib.new @patch_width * 3, @patch_width * 3
      @back_color = @image.color 255, 255, 255
      @fore_color = @image.color @decode[:red], @decode[:green], @decode[:blue]
      @image.transparent @back_color
      render
 
      if @resize_to
        @image.resize @resize_to
      end
    end
 
    def decode code
      {
        :center_type => (code & 0x3),
        :center_invert => (((code >> 2) & 0x01) != 0),
        :corner_type => ((code >> 3) & 0x0f),
        :corner_invert => (((code >> 7) & 0x01) != 0),
        :corner_turn => ((code >> 8) & 0x03),
        :side_type => ((code >> 10) & 0x0f),
        :side_invert => (((code >> 14) & 0x01) != 0),
        :side_turn => ((code >> 15) & 0x03),
        :red => (((code >> 16) & 0x01f) << 3),
        :green => (((code >> 21) & 0x01f) << 3),
        :blue => (((code >> 27) & 0x01f) << 3),
      }
    end
 
    def render
      center = [[1, 1]]
      side = [[1, 0], [2, 1], [1, 2], [0, 1]]
      corner = [[0, 0], [2, 0], [2, 2], [0, 2]]
 
      draw_patches(center, CENTER_PATCHES[@decode[:center_type]],
                   0, @decode[:center_invert])
      draw_patches(side, @decode[:side_type],
                   @decode[:side_turn], @decode[:side_invert])
      draw_patches(corner, @decode[:corner_type],
                   @decode[:corner_turn], @decode[:corner_invert])
    end
 
    def draw_patches list, patch, turn, invert
      list.each do |i|
        draw(:x => i[0], :y => i[1], :patch => patch,
             :turn => turn, :invert => invert)
        turn += 1
      end
    end
 
    def draw opt = {}
      x = opt[:x] * @patch_width
      y = opt[:y] * @patch_width
      patch = opt[:patch] % PATCHES.size
      turn = opt[:turn] % 4
 
      if opt[:invert]
        fore, back = @back_color, @fore_color
      else
        fore, back = @fore_color, @back_color
      end
      @image.fill_rect(x, y, x + @patch_width - 1, y + @patch_width - 1, back)
 
      points = []
      PATCHES[patch].each do |pt|
        dx = pt % PATCH_SIZE
        dy = pt / PATCH_SIZE
        len = @patch_width - 1
        px = dx.to_f / (PATCH_SIZE - 1) * len
        py = dy.to_f / (PATCH_SIZE - 1) * len
 
        case turn
        when 1
          px, py = len - py, px
        when 2
          px, py = len - px, len - py
        when 3
          px, py = py, len - px
        end
        points << [x + px, y + py]
      end
      @image.polygon points, fore
    end
 
    def write path = "#{@code}.png"
      @image.write path
    end
 
    def to_blob
      @image.to_blob
    end
 
    def self.calc_code str
      extract_code Identicon.digest(str)
    end
 
    def self.ip2code ip
      code_ip = extract_code(ip.split('.'))
      extract_code Identicon.digest(code_ip.to_s)
    end
 
    def self.digest str
      Digest::SHA1.digest(str + @@salt)
    end
 
    def self.extract_code list
      tmp = [list[0].to_i << 24, list[1].to_i << 16,
             list[2].to_i << 8, list[3].to_i]
      tmp.inject(0) do |r, i|
        r | ((i[31] == 1) ? -(i & 0x7fffffff) : i)
      end
    end
 
    def self.image_lib= image_lib
      @@image_lib = image_lib
    end
 
    def self.image_lib
      @@image_lib
    end
 
    def self.salt= salt
      @@salt = salt
    end
 
    def self.salt
      @@salt
    end
  end
end