Class: FB2Image::FB

Inherits:
Object
  • Object
show all
Defined in:
lib/fb2image.rb

Overview

Your code goes here…

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(src_width, src_height, fb, opts = {}) ⇒ FB

Returns a new instance of FB.



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/fb2image.rb', line 12

def initialize(src_width, src_height, fb, opts={})
  @src_width       = src_width
  @src_height      = src_height
  @fb              = fb
  @thinning_rate   = opts[:thinning_rate] || 1
  @width           = @src_width  / @thinning_rate
  @height          = @src_height / @thinning_rate
  @r_shift         = (opts[:red_shift]   || 0)  / 8
  @g_shift         = (opts[:green_shift] || 8)  / 8
  @b_shift         = (opts[:blue_shift]  || 16) / 8
  @bits_per_pixel  = opts[:bits_per_pixel] || 1
  @bytes_per_pixel = @bits_per_pixel / 8
end

Instance Attribute Details

#fbObject

Returns the value of attribute fb.



10
11
12
# File 'lib/fb2image.rb', line 10

def fb
  @fb
end

#heightObject

Returns the value of attribute height.



10
11
12
# File 'lib/fb2image.rb', line 10

def height
  @height
end

#src_heightObject

Returns the value of attribute src_height.



10
11
12
# File 'lib/fb2image.rb', line 10

def src_height
  @src_height
end

#src_widthObject

Returns the value of attribute src_width.



10
11
12
# File 'lib/fb2image.rb', line 10

def src_width
  @src_width
end

#thinning_rateObject

Returns the value of attribute thinning_rate.



10
11
12
# File 'lib/fb2image.rb', line 10

def thinning_rate
  @thinning_rate
end

#widthObject

Returns the value of attribute width.



10
11
12
# File 'lib/fb2image.rb', line 10

def width
  @width
end

Instance Method Details

#write_bmp(dest_path) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/fb2image.rb', line 59

def write_bmp(dest_path)
  image = BitMap.new(@width, @height)
  @height.times do |y|
    @width.times do |x|
      idx = (y * @src_width * @thinning_rate) + (x * @thinning_rate)
      pixel = @fb[idx * @bytes_per_pixel, @bytes_per_pixel]
      r = pixel[@r_shift]
      g = pixel[@g_shift]
      b = pixel[@b_shift]
      image.pset(x, y, r, g, b)
    end
  end
  image.write(dest_path)
end

#write_png(dest_path) ⇒ Object



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
# File 'lib/fb2image.rb', line 26

def write_png(dest_path)
  depth      = 8 #@bits_per_pixel
  color_type = 2
  img_data   = ""

  open(dest_path, "wb") do |f|
    # ファイルシグニチャ
    f.print "\x89PNG\r\n\x1a\n"

    # ヘッダ
    f.print chunk("IHDR", [@width, @height, depth, color_type, 0, 0, 0].pack("NNCCCCC"))

    # 画像データ
    @height.times do |y|
      line = []
      @width.times do |x|
        idx = (y * @src_width * @thinning_rate) + (x * @thinning_rate)
        pixel = @fb[idx * @bytes_per_pixel, @bytes_per_pixel]
        r = pixel[@r_shift, 1]
        g = pixel[@g_shift, 1]
        b = pixel[@b_shift, 1]
        line << [r, g, b]
      end
      img_data << ([0] + line.flatten).pack("C*")
    end

    f.print chunk("IDAT", Zlib::Deflate.deflate(img_data))

    # 終端
    f.print chunk("IEND", "")
  end
end