Class: ImageMap

Inherits:
CArray show all
Defined in:
ext/imagemap/lib/graphics/imagemap.rb

Overview

CArray#index_linear(scale) CArray#index_log10(scale)

ImageMap.create_image_map_linear(idx, n, palette, mask_color) ImageMap.create_image_map_log10(idx, n, palette, mask_color)

Constant Summary

Constants inherited from CArray

CArray::HAVE_NARRAY

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from CArray

#<=>, __new__, __new_fixlen__, #address, #anomaly, #asign, #attribute, #attribute=, #bin, bind, #block_iterator, #by, #classes, #classify, #code, combine, #compact, #compacted, composite, #correlation, #count_masked, #count_not_masked, #covariance, #covariancep, #deg_180, #deg_180!, #deg_360, #deg_360!, #delete_block, #differentiate, #display_by_magick, dump, #duplicated_values, #escape_bytea, #extend_as_table, #fa, #false, #first, #flatten, #flattened, from_binary, #from_bit_string, from_bit_string, from_csv, from_fortran_format, #has_attribute?, #imag, #imag=, #index, #indices, #insert_block, #inspect, #integrate, #interp_nd_linear, #interpolate, #interpolate2, #is_close, #is_divisible, #is_equiv, #is_not_divisible, #is_real, join, #join, #last, load, load_binary, load_binary_io, load_by_magick, load_csv, load_from_file, load_pg, load_sqlite3, #map, #marshal_dump, #marshal_load, #maskout, #maskout!, #matchup, #matchup_nearest, #max_by, #median, merge, #min_by, #na, #nm, #normalize, #normalize!, #nv, #order, pack, #percentile, pickup, #pull, #pulled, #quantile, #quo, #rad_2pi, #rad_2pi!, #rad_pi, #rad_pi!, #random, #randomn, #randomn!, #range, #real, #real=, #real?, #recurrence, #recurrence!, #replace, #reshape, #resize, #reversed, #roll, #roll!, #rolled, #rotate, #rotate!, #rotated, save, #save_binary, #save_binary_io, #save_by_magick, #save_csv, #scale, #scale!, #section, #select, #shift, #shift!, #sign, #solve, #solve2, #sort_by, #sorted_by, span, #span, #span!, #split, #st, summation, #to_binary, #to_bit_string, #to_column, #to_csv, #to_fortran_format, #to_na, #to_na!, #to_nam, #to_nam!, #to_nm, #to_nm!, #to_nv, #to_nv!, #to_row, #to_sqlite3, #to_tabular, #transform, #transpose, #transpose!, #true, #unblob, #unescape_bytea, #uniq, #where_range, #window_iterator, #windows, #zimg

Constructor Details

#initialize(*argv) ⇒ ImageMap

Returns a new instance of ImageMap.



189
190
191
192
193
194
195
# File 'ext/imagemap/lib/graphics/imagemap.rb', line 189

def initialize (*argv)
  super
  @yscale  = 1
  @xscale  = 1
  @yoffset = 0
  @xoffset = 0
end

Instance Attribute Details

#xoffsetObject

Returns the value of attribute xoffset.



197
198
199
# File 'ext/imagemap/lib/graphics/imagemap.rb', line 197

def xoffset
  @xoffset
end

#xscaleObject

Returns the value of attribute xscale.



197
198
199
# File 'ext/imagemap/lib/graphics/imagemap.rb', line 197

def xscale
  @xscale
end

#yoffsetObject

Returns the value of attribute yoffset.



197
198
199
# File 'ext/imagemap/lib/graphics/imagemap.rb', line 197

def yoffset
  @yoffset
end

#yscaleObject

Returns the value of attribute yscale.



197
198
199
# File 'ext/imagemap/lib/graphics/imagemap.rb', line 197

def yscale
  @yscale
end

Instance Method Details

#line(x0, y0, x1, y1, z) ⇒ Object



25
26
27
28
29
30
31
# File 'ext/imagemap/lib/graphics/imagemap.rb', line 25

def line (x0, y0, x1, y1, z)
  x0 = (x0 - @xoffset)*@xscale
  y0 = (y0 - @yoffset)*@yscale
  x1 = (x1 - @xoffset)*@xscale
  y1 = (y1 - @yoffset)*@yscale
  draw_line(y0, x0, y1, x1, z)
end

#map_points(pw, pu, pv) {|pu, pv, gx, gy| ... } ⇒ Object

Yields:

  • (pu, pv, gx, gy)


39
40
41
42
43
44
45
46
47
# File 'ext/imagemap/lib/graphics/imagemap.rb', line 39

def map_points (pw, pu, pv)
  lines, pixels = pw.dim0, pw.dim1
  gx = CArray.float(lines, pixels)
  gy = CArray.float(lines, pixels)
  yield(pu,pv,gx,gy)
  gx.sub!(@xoffset).mul!(@xscale)
  gy.sub!(@yoffset).mul!(@yscale)
  draw_points(gy, gx, pw)
end

#polyline(x, y, z) ⇒ Object



33
34
35
36
37
# File 'ext/imagemap/lib/graphics/imagemap.rb', line 33

def polyline (x, y, z)
  x = (x - @xoffset).mul!(@xscale)
  y = (y - @xoffset).mul!(@xscale)
  draw_polyline(y, x, z)
end

#set_xrange(rng) ⇒ Object



199
200
201
202
203
204
205
206
207
208
# File 'ext/imagemap/lib/graphics/imagemap.rb', line 199

def set_xrange (rng)
  first = rng.begin
  last  = rng.end
  if rng.exclude_end?
    @xscale = (dim1-1).to_f/(last-first).to_f
  else
    @xscale = dim1.to_f/(last-first).to_f
  end
  @xoffset = first
end

#set_yrange(rng) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
# File 'ext/imagemap/lib/graphics/imagemap.rb', line 210

def set_yrange (rng)
  first = rng.begin
  last  = rng.end
  if rng.exclude_end?
    @yscale = (dim0-1).to_f/(first-last).to_f
    @yoffset = last - @yscale
  else
    @yscale = dim0.to_f/(first-last).to_f
    @yoffset = last
  end
end

#tfw(metric = 1.0) ⇒ Object



259
260
261
262
263
264
265
266
267
# File 'ext/imagemap/lib/graphics/imagemap.rb', line 259

def tfw (metric = 1.0)
  format([["%f"]*6].join("\n"), 
         metric/@xscale,
         0,
         0,
         metric/@yscale,
         metric*(@xoffset+0.5/@xscale),
         metric*(@yoffset+0.5/@yscale))
end

#warp(data, x, y, opt = {}, &block) ⇒ Object



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
# File 'ext/imagemap/lib/graphics/imagemap.rb', line 222

def warp (data, x, y, opt = {}, &block)
  option = {
    :grid => "point",
    :interpolation => nil,
    :gradation => false,
  }
  option.update(opt)
  case option[:grid] 
  when /^area([01]|)$/, "pixel"
    if option[:interpolation] 
      raise("can't interpolate in pixel mode")
    else
      if option[:gradation]
        warp_image_gradation(data, x, y, &block)
      else
        if option[:grid] == "area" or option[:grid] == "area0" 
          warp_image0(data, x, y, &block) ### inter(extra)polation before
        else
          warp_image1(data, x, y, &block) ### inter(extra)polation after
        end
      end
    end
  when "point"
    if option[:interpolation]
      warp_grid_interpolation(data, x, y, option[:interpolation], &block)
    else
      if option[:gradation]
        warp_grid_gradation(data, x, y, &block)
      else
        warp_grid(data, x, y, &block)
      end
    end
  else
    raise("unknown grid type")
  end
end

#warp_grid(pw, pu, pv) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
# File 'ext/imagemap/lib/graphics/imagemap.rb', line 97

def warp_grid (pw, pu, pv)
  lines, pixels = pw.dim0, pw.dim1
  gx = CArray.float(lines, pixels)
  gy = CArray.float(lines, pixels)
  CArray.attach(pu,pv,pw) {
    yield(pu,pv,gx,gy)
    gx.sub!(@xoffset).mul!(@xscale)
    gy.sub!(@yoffset).mul!(@yscale)
    fill_rectangle_grid(gy, gx, pw)
  }
end

#warp_grid_gradation(pw, pu, pv) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'ext/imagemap/lib/graphics/imagemap.rb', line 142

def warp_grid_gradation (pw, pu, pv)
  lines, pixels = pu.dim0, pv.dim1
  gx = CArray.float(lines, pixels)
  gy = CArray.float(lines, pixels)
  CArray.attach(pu,pv,pw) {
    yield(pu,pv,gx,gy)
    gx.sub!(@xoffset).mul!(@xscale)
    gy.sub!(@yoffset).mul!(@yscale)
#      bx = gx[[0,2],[0,2]]
#      by = gy[[0,2],[0,2]]
#      bw = pw[[0,2],[0,2]]
    draw_rectangle_gradation_grid(gy, gx, pw)
#      pw[0..-2,0..-2].each_index do |i,j|
#        draw_rectangle_gradation(by.move(i,j), bx.move(i,j), bw.move(i,j))
#      end
  }
end

#warp_grid_interpolation(pw, pu, pv, n, &block) ⇒ Object



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
# File 'ext/imagemap/lib/graphics/imagemap.rb', line 160

def warp_grid_interpolation (pw, pu, pv, n, &block)
  lines, pixels = pu.dim0, pv.dim1
  m = 2+n
  hu = CArray.float(m, m)
  hv = CArray.float(m, m)
  hw = CArray.float(m, m)
  bu = pu[[0,2],[0,2]]
  bv = pv[[0,2],[0,2]]
  bw = pw[[0,2],[0,2]]
  CArray.attach(pu,pv,pw) {
    pw[0..-2,0..-2].each_index do |i,j|
      bu.move(i,j)
      bv.move(i,j)
      bw.move(i,j)
      hu.grid_new([[0,m-1],[0,m-1]])[] = bu
      hv.grid_new([[0,m-1],[0,m-1]])[] = bv
      hw.grid_new([[0,m-1],[0,m-1]])[] = bw
      [hu, hv, hw].each do |h|
        h[nil, 0].span!(h[0,0]..h[m-1,0])
        h[nil, m-1].span!(h[0,m-1]..h[m-1,m-1])
        h.dim0.times do |i|
          h[i, nil].span!(h[i,0]..h[i,m-1])
        end
      end
      warp_grid_gradation(hw, hu, hv, &block)
    end
  }
end

#warp_image0(pw, pu, pv) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'ext/imagemap/lib/graphics/imagemap.rb', line 49

def warp_image0 (pw, pu, pv)
  lines, pixels = pw.dim0, pw.dim1
  mu = CArray.float(lines, pixels+1)
  mv = CArray.float(lines, pixels+1)
  gu = CArray.float(lines+1, pixels+1)
  gv = CArray.float(lines+1, pixels+1)
  gx = CArray.float(lines+1, pixels+1)
  gy = CArray.float(lines+1, pixels+1)
  CArray.attach(pu,pv,pw) {
    [[pu,mu,gu], [pv,mv,gv]].each do |p,m,g|
      m[nil,1..-2] =  0.5*p[nil,0..-2] + 0.5*p[nil,1..-1]
      m[nil,0]     =  1.5*p[nil,0]     - 0.5*p[nil,1]
      m[nil,-1]    = -0.5*p[nil,-2]    + 1.5*p[nil,-1]
      g[1..-2,nil] =  0.5*m[0..-2,nil] + 0.5*m[1..-1,nil]
      g[ 0,nil]    =  1.5*m[0,nil]     - 0.5*m[1,nil]
      g[-1,nil]    = -0.5*m[-2,nil]    + 1.5*m[-1,nil]
    end
    yield(gu,gv,gx,gy)
    gx.sub!(@xoffset).mul!(@xscale)
    gy.sub!(@yoffset).mul!(@yscale)
    fill_rectangle_image(gy, gx, pw)
  }
end

#warp_image1(pw, pu, pv) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'ext/imagemap/lib/graphics/imagemap.rb', line 73

def warp_image1 (pw, pu, pv)
  lines, pixels = pw.dim0, pw.dim1
  px = CArray.float(lines, pixels)
  py = CArray.float(lines, pixels)
  mx = CArray.float(lines, pixels+1)
  my = CArray.float(lines, pixels+1)
  gx = CArray.float(lines+1, pixels+1)
  gy = CArray.float(lines+1, pixels+1)
  CArray.attach(pu,pv,pw) {
    yield(pu,pv,px,py)
    px.sub!(@xoffset).mul!(@xscale)
    py.sub!(@yoffset).mul!(@yscale)
    [[px,mx,gx], [py,my,gy]].each do |p,m,g|
      m[nil,1..-2] =  0.5*p[nil,0..-2] + 0.5*p[nil,1..-1]
      m[nil,0]     =  1.5*p[nil,0]     - 0.5*p[nil,1]
      m[nil,-1]    = -0.5*p[nil,-2]    + 1.5*p[nil,-1]
      g[1..-2,nil] =  0.5*m[0..-2,nil] + 0.5*m[1..-1,nil]
      g[ 0,nil]    =  1.5*m[0,nil]     - 0.5*m[1,nil]
      g[-1,nil]    = -0.5*m[-2,nil]    + 1.5*m[-1,nil]
    end
    fill_rectangle_image(gy, gx, pw)
  }
end

#warp_image_gradation(pw, pu, pv) ⇒ Object



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
# File 'ext/imagemap/lib/graphics/imagemap.rb', line 109

def warp_image_gradation (pw, pu, pv)
  lines, pixels = pw.dim0, pw.dim1
  mu = CArray.float(lines, pixels+1)
  mv = CArray.float(lines, pixels+1)
  mw = CArray.float(lines, pixels+1)
  gu = CArray.float(lines+1, pixels+1)
  gv = CArray.float(lines+1, pixels+1)
  gw = CArray.float(lines+1, pixels+1)
  gx = CArray.float(lines+1, pixels+1)
  gy = CArray.float(lines+1, pixels+1)
#    bgx = gx[[0,2],[0,2]]
#    bgy = gy[[0,2],[0,2]]
#    bgw = gw[[0,2],[0,2]]
#    p("hello")
  CArray.attach(pu,pv,pw) {
    [[pu,mu,gu], [pv,mv,gv], [pw.float32,mw,gw]].each do |p,m,g|
      m[nil,1..-2] =  0.5*p[nil,0..-2] + 0.5*p[nil,1..-1]
      m[nil,0]     =  1.5*p[nil,0]     - 0.5*p[nil,1]
      m[nil,-1]    = -0.5*p[nil,-2]    + 1.5*p[nil,-1]
      g[1..-2,nil] =  0.5*m[0..-2,nil] + 0.5*m[1..-1,nil]
      g[ 0,nil]    =  1.5*m[0,nil]     - 0.5*m[1,nil]
      g[-1,nil]    = -0.5*m[-2,nil]    + 1.5*m[-1,nil]
    end
    yield(gu,gv,gx,gy)
    gx.sub!(@xoffset).mul!(@xscale)
    gy.sub!(@yoffset).mul!(@yscale)
    draw_rectangle_gradation_grid(gy, gx, gw)
#      pw.each_index do |i,j|
#        draw_rectangle_gradation(bgy.move(i,j), bgx.move(i,j), bgw.move(i,j))
#      end
  }
end