Class: FB2Image::BitMap
- Inherits:
-
Object
- Object
- FB2Image::BitMap
- Defined in:
- lib/fb2image/bmp.rb
Instance Attribute Summary collapse
-
#buf ⇒ Object
writeonly
Sets the attribute buf.
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Class Method Summary collapse
-
.read(filename) ⇒ Object
BMPファイルを読み込む.
Instance Method Summary collapse
- #clear(r = 255, g = 255, b = 255) ⇒ Object
-
#clip(x1, y1, x2, y2) ⇒ Object
(x1, y1) - (x2, y2)の部分画像を取り出す.
- #get_dpi ⇒ Object
-
#initialize(width, height, dpi = 96) ⇒ BitMap
constructor
A new instance of BitMap.
-
#paste(image, x0 = 0, y0 = 0) ⇒ Object
x0, y0 は、貼り付ける始点(左上)の座標.
-
#pget(x, y) ⇒ Object
x, yは整数であることを期待している 戻り値は[r, g, b]な配列.
-
#pset(x, y, r, g, b) ⇒ Object
x, y, r, g, b は整数であることを期待している.
- #set_dpi(dpi) ⇒ Object
-
#write(filename) ⇒ Object
BMPファイルを出力する.
Constructor Details
#initialize(width, height, dpi = 96) ⇒ BitMap
Returns a new instance of BitMap.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/fb2image/bmp.rb', line 5 def initialize(width, height, dpi = 96) @width = width @height = height @line_size = width * 3 + (4 - (width * 3) % 4) % 4 @buf_size = @line_size * height @buf = "\000" * @buf_size @bit_count = 24 @compression = 0 # 圧縮無し @size_image = 0 @x_pix_per_meter = (39.375 * dpi).round @y_pix_per_meter = (39.375 * dpi).round @clr_used = 0 @cir_important = 0 end |
Instance Attribute Details
#buf=(value) ⇒ Object (writeonly)
Sets the attribute buf
30 31 32 |
# File 'lib/fb2image/bmp.rb', line 30 def buf=(value) @buf = value end |
#height ⇒ Object (readonly)
Returns the value of attribute height.
31 32 33 |
# File 'lib/fb2image/bmp.rb', line 31 def height @height end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
31 32 33 |
# File 'lib/fb2image/bmp.rb', line 31 def width @width end |
Class Method Details
.read(filename) ⇒ Object
BMPファイルを読み込む
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 |
# File 'lib/fb2image/bmp.rb', line 52 def BitMap.read(filename) buf = nil open(filename, "rb") do |f| buf = f.read end if buf[0] != ?B or buf[1] != ?M raise('[Error] read: Invalid Header') end real_buf_size = buf.size buf_size = (buf[2, 4].unpack("l*"))[0] if buf_size > real_buf_size raise('[Error] read: Invalid Buffer Size') end data_offset = (buf[10, 4].unpack("l*"))[0] if data_offset != 54 raise('[Error] read: Invalid Data Offset') end width = (buf[18, 4].unpack("l*"))[0] height = (buf[22, 4].unpack("l*"))[0] bit_count = (buf[28, 2].unpack("s*"))[0] if bit_count != 24 raise('[Error] read: Unsupported Color Depth') end compression = (buf[30, 4].unpack("l*"))[0] if compression != 0 raise('[Error] read: Compression Not Supported') end pix_per_meter = (buf[38, 4].unpack("l*"))[0] dpi = pix_per_meter / 39.375 image_buf = buf[54, buf_size] image = BitMap.new(width, height, dpi) image.buf = image_buf return image end |
Instance Method Details
#clear(r = 255, g = 255, b = 255) ⇒ Object
20 21 22 23 24 25 26 27 28 |
# File 'lib/fb2image/bmp.rb', line 20 def clear(r = 255, g = 255, b = 255) line = b.chr * @line_size @width.times do |x| line[x * 3 + 1] = g line[x * 3 + 2] = r end @buf = line * @height end |
#clip(x1, y1, x2, y2) ⇒ Object
(x1, y1) - (x2, y2)の部分画像を取り出す
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 |
# File 'lib/fb2image/bmp.rb', line 97 def clip(x1, y1, x2, y2) return if x1 > x2 return if y1 > y2 return if x2 < 0 return if y2 < 0 return if x1 >= @width return if y1 >= @height x1 = 0 if x1 < 0 y1 = 0 if y1 < 0 x2 = @width - 1 if x2 >= @width y2 = @height - 1 if y2 >= @height clip_width = x2 - x1 + 1 clip_height = y2 - y1 + 1 clip_image = BitMap.new(clip_width, clip_height, self.get_dpi) for y in 0 .. (clip_height - 1) for x in 0 .. (clip_width - 1) color = self.pget(x1 + x, y1 + y) clip_image.pset(x, y, color[0], color[1], color[2]) end end return clip_image end |
#get_dpi ⇒ Object
157 158 159 |
# File 'lib/fb2image/bmp.rb', line 157 def get_dpi() return (@x_pix_per_meter / 39.375).round end |
#paste(image, x0 = 0, y0 = 0) ⇒ Object
x0, y0 は、貼り付ける始点(左上)の座標
167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/fb2image/bmp.rb', line 167 def paste(image, x0 = 0, y0 = 0) return if image == nil image.height.times do |from_y| y = y0 + from_y next if y < 0 or @height <= y image.width.times do |from_x| x = x0 + from_x next if x < 0 or @width <= x color = image.pget(from_x, from_y) self.pset(x, y, color[0], color[1], color[2]) end end end |
#pget(x, y) ⇒ Object
x, yは整数であることを期待している戻り値は[r, g, b]な配列
143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/fb2image/bmp.rb', line 143 def pget(x, y) x = 0 if x < 0 x = @width - 1 if x >= @width y = 0 if y < 0 y = @height - 1 if y >= @height addr = (@height - 1 - y) * @line_size + x * 3 b = @buf[addr ] g = @buf[addr + 1] r = @buf[addr + 2] return [r, g, b] end |
#pset(x, y, r, g, b) ⇒ Object
x, y, r, g, b は整数であることを期待している
126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/fb2image/bmp.rb', line 126 def pset(x, y, r, g, b) return if x < 0 or @width <= x return if y < 0 or @height <= y r = 0 if r < 0 g = 0 if g < 0 b = 0 if b < 0 r = 255 if r > 255 g = 255 if g > 255 b = 255 if b > 255 @buf[(@height - 1 - y) * @line_size + x * 3 ] = b.chr @buf[(@height - 1 - y) * @line_size + x * 3 + 1] = g.chr @buf[(@height - 1 - y) * @line_size + x * 3 + 2] = r.chr end |
#set_dpi(dpi) ⇒ Object
161 162 163 164 |
# File 'lib/fb2image/bmp.rb', line 161 def set_dpi(dpi) @x_pix_per_meter = (39.375 * dpi).round @y_pix_per_meter = @x_pix_per_meter end |
#write(filename) ⇒ Object
BMPファイルを出力する
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/fb2image/bmp.rb', line 34 def write(filename) file_size = 14 + 40 + @buf_size data_offset = 14 + 40 open(filename, "wb") do |f| f.print 'BM' f.print [file_size, 0, data_offset].pack("l*") f.print [40, @width, @height].pack("l*") f.print [1, @bit_count].pack("S*") f.print [@compression, @size_image, @x_pix_per_meter, @y_pix_per_meter, @clr_used, @cir_important].pack("l*") f.print @buf end end |