fb2image 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .DS_Store
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in fb2image.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Kazuhiro Yamada
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,50 @@
1
+ # FB2Image
2
+
3
+ Convert Frame Buffer to Image (png, bmp..)
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'fb2image'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install fb2image
18
+
19
+ ## Usage
20
+
21
+ # require 'fb2image'
22
+
23
+ width = 100
24
+ height = 20
25
+
26
+ # make gradation fb
27
+ line = (0...width).map {|x| [x * 255 / width, 0, 0, 0] }
28
+ fb = ([line] * height).flatten
29
+
30
+ # initialize FB
31
+ fb = FB.new(width, height, fb, {
32
+ :bits_per_pixel => 32,
33
+ :red_shift => 0,
34
+ :green_shift => 8,
35
+ :blue_shift => 16
36
+ })
37
+
38
+ # create png
39
+ fb.write_png("tmp/gradation.png")
40
+
41
+ # create bmp
42
+ fb.write_bmp("tmp/gradation.bmp")
43
+
44
+ ## Contributing
45
+
46
+ 1. Fork it
47
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
48
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
49
+ 4. Push to the branch (`git push origin my-new-feature`)
50
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/fb2image ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'fb2image'
data/fb2image.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'fb2image/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "fb2image"
8
+ spec.version = FB2Image::VERSION
9
+ spec.authors = ["Kazuhiro Yamada"]
10
+ spec.email = ["yamadakazu45@gmail.com"]
11
+ spec.description = %q{Convert Frame Buffer to Image}
12
+ spec.summary = spec.description
13
+ spec.homepage = "https://github.com/k-yamada/fb2image"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency 'rspec'
24
+ end
data/lib/fb2image.rb ADDED
@@ -0,0 +1,84 @@
1
+ # -*- coding: utf-8 -*-
2
+ require "zlib"
3
+ require "fb2image/version"
4
+ require "fb2image/bmp"
5
+
6
+ module FB2Image
7
+ # Your code goes here...
8
+
9
+ class FB
10
+ attr_accessor :src_width, :src_height, :width, :height, :fb, :thinning_rate
11
+
12
+ def initialize(src_width, src_height, fb, opts={})
13
+ @src_width = src_width
14
+ @src_height = src_height
15
+ @fb = fb
16
+ @thinning_rate = opts[:thinning_rate] || 1
17
+ @width = @src_width / @thinning_rate
18
+ @height = @src_height / @thinning_rate
19
+ @r_shift = (opts[:red_shift] || 0) / 8
20
+ @g_shift = (opts[:green_shift] || 8) / 8
21
+ @b_shift = (opts[:blue_shift] || 16) / 8
22
+ @bits_per_pixel = opts[:bits_per_pixel] || 1
23
+ @bytes_per_pixel = @bits_per_pixel / 8
24
+ end
25
+
26
+ def write_png(dest_path)
27
+ depth = 8 #@bits_per_pixel
28
+ color_type = 2
29
+ img_data = ""
30
+
31
+ open(dest_path, "wb") do |f|
32
+ # ファイルシグニチャ
33
+ f.print "\x89PNG\r\n\x1a\n"
34
+
35
+ # ヘッダ
36
+ f.print chunk("IHDR", [@width, @height, depth, color_type, 0, 0, 0].pack("NNCCCCC"))
37
+
38
+ # 画像データ
39
+ @height.times do |y|
40
+ line = []
41
+ @width.times do |x|
42
+ idx = (y * @src_width * @thinning_rate) + (x * @thinning_rate)
43
+ pixel = @fb[idx * @bytes_per_pixel, @bytes_per_pixel]
44
+ r = pixel[@r_shift, 1]
45
+ g = pixel[@g_shift, 1]
46
+ b = pixel[@b_shift, 1]
47
+ line << [r, g, b]
48
+ end
49
+ img_data << ([0] + line.flatten).pack("C*")
50
+ end
51
+
52
+ f.print chunk("IDAT", Zlib::Deflate.deflate(img_data))
53
+
54
+ # 終端
55
+ f.print chunk("IEND", "")
56
+ end
57
+ end
58
+
59
+ def write_bmp(dest_path)
60
+ image = BitMap.new(@width, @height)
61
+ @height.times do |y|
62
+ @width.times do |x|
63
+ idx = (y * @src_width * @thinning_rate) + (x * @thinning_rate)
64
+ pixel = @fb[idx * @bytes_per_pixel, @bytes_per_pixel]
65
+ r = pixel[@r_shift]
66
+ g = pixel[@g_shift]
67
+ b = pixel[@b_shift]
68
+ image.pset(x, y, r, g, b)
69
+ end
70
+ end
71
+ image.write(dest_path)
72
+ end
73
+
74
+
75
+ private
76
+
77
+
78
+ # チャンクのバイト列生成関数
79
+ def chunk(type, data)
80
+ [data.bytesize, type, data, Zlib.crc32(type + data)].pack("NA4A*N")
81
+ end
82
+ end
83
+
84
+ end
@@ -0,0 +1,182 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ module FB2Image
4
+ class BitMap
5
+ def initialize(width, height, dpi = 96)
6
+ @width = width
7
+ @height = height
8
+ @line_size = width * 3 + (4 - (width * 3) % 4) % 4
9
+ @buf_size = @line_size * height
10
+ @buf = "\000" * @buf_size
11
+ @bit_count = 24
12
+ @compression = 0 # 圧縮無し
13
+ @size_image = 0
14
+ @x_pix_per_meter = (39.375 * dpi).round
15
+ @y_pix_per_meter = (39.375 * dpi).round
16
+ @clr_used = 0
17
+ @cir_important = 0
18
+ end
19
+
20
+ def clear(r = 255, g = 255, b = 255)
21
+ line = b.chr * @line_size
22
+ @width.times do |x|
23
+ line[x * 3 + 1] = g
24
+ line[x * 3 + 2] = r
25
+ end
26
+
27
+ @buf = line * @height
28
+ end
29
+
30
+ attr_writer :buf
31
+ attr_reader :width, :height
32
+
33
+ # BMPファイルを出力する
34
+ def write(filename)
35
+ file_size = 14 + 40 + @buf_size
36
+ data_offset = 14 + 40
37
+
38
+ open(filename, "wb") do |f|
39
+ f.print 'BM'
40
+ f.print [file_size, 0, data_offset].pack("l*")
41
+ f.print [40, @width, @height].pack("l*")
42
+ f.print [1, @bit_count].pack("S*")
43
+ f.print [@compression, @size_image,
44
+ @x_pix_per_meter, @y_pix_per_meter,
45
+ @clr_used, @cir_important].pack("l*")
46
+ f.print @buf
47
+ end
48
+ end
49
+
50
+
51
+ # BMPファイルを読み込む
52
+ def BitMap.read(filename)
53
+ buf = nil
54
+ open(filename, "rb") do |f|
55
+ buf = f.read
56
+ end
57
+
58
+ if buf[0] != ?B or buf[1] != ?M
59
+ raise('[Error] read: Invalid Header')
60
+ end
61
+ real_buf_size = buf.size
62
+ buf_size = (buf[2, 4].unpack("l*"))[0]
63
+ if buf_size > real_buf_size
64
+ raise('[Error] read: Invalid Buffer Size')
65
+ end
66
+ data_offset = (buf[10, 4].unpack("l*"))[0]
67
+ if data_offset != 54
68
+ raise('[Error] read: Invalid Data Offset')
69
+ end
70
+
71
+ width = (buf[18, 4].unpack("l*"))[0]
72
+ height = (buf[22, 4].unpack("l*"))[0]
73
+
74
+ bit_count = (buf[28, 2].unpack("s*"))[0]
75
+ if bit_count != 24
76
+ raise('[Error] read: Unsupported Color Depth')
77
+ end
78
+
79
+ compression = (buf[30, 4].unpack("l*"))[0]
80
+ if compression != 0
81
+ raise('[Error] read: Compression Not Supported')
82
+ end
83
+
84
+ pix_per_meter = (buf[38, 4].unpack("l*"))[0]
85
+ dpi = pix_per_meter / 39.375
86
+
87
+ image_buf = buf[54, buf_size]
88
+
89
+ image = BitMap.new(width, height, dpi)
90
+ image.buf = image_buf
91
+
92
+ return image
93
+ end
94
+
95
+
96
+ # (x1, y1) - (x2, y2)の部分画像を取り出す
97
+ def clip(x1, y1, x2, y2)
98
+ return if x1 > x2
99
+ return if y1 > y2
100
+ return if x2 < 0
101
+ return if y2 < 0
102
+ return if x1 >= @width
103
+ return if y1 >= @height
104
+ x1 = 0 if x1 < 0
105
+ y1 = 0 if y1 < 0
106
+ x2 = @width - 1 if x2 >= @width
107
+ y2 = @height - 1 if y2 >= @height
108
+
109
+ clip_width = x2 - x1 + 1
110
+ clip_height = y2 - y1 + 1
111
+
112
+ clip_image = BitMap.new(clip_width, clip_height, self.get_dpi)
113
+
114
+ for y in 0 .. (clip_height - 1)
115
+ for x in 0 .. (clip_width - 1)
116
+ color = self.pget(x1 + x, y1 + y)
117
+ clip_image.pset(x, y, color[0], color[1], color[2])
118
+ end
119
+ end
120
+
121
+ return clip_image
122
+ end
123
+
124
+
125
+ # x, y, r, g, b は整数であることを期待している
126
+ def pset(x, y, r, g, b)
127
+ return if x < 0 or @width <= x
128
+ return if y < 0 or @height <= y
129
+ r = 0 if r < 0
130
+ g = 0 if g < 0
131
+ b = 0 if b < 0
132
+ r = 255 if r > 255
133
+ g = 255 if g > 255
134
+ b = 255 if b > 255
135
+
136
+ @buf[(@height - 1 - y) * @line_size + x * 3 ] = b.chr
137
+ @buf[(@height - 1 - y) * @line_size + x * 3 + 1] = g.chr
138
+ @buf[(@height - 1 - y) * @line_size + x * 3 + 2] = r.chr
139
+ end
140
+
141
+ # x, yは整数であることを期待している
142
+ # 戻り値は[r, g, b]な配列
143
+ def pget(x, y)
144
+ x = 0 if x < 0
145
+ x = @width - 1 if x >= @width
146
+ y = 0 if y < 0
147
+ y = @height - 1 if y >= @height
148
+
149
+ addr = (@height - 1 - y) * @line_size + x * 3
150
+ b = @buf[addr ]
151
+ g = @buf[addr + 1]
152
+ r = @buf[addr + 2]
153
+
154
+ return [r, g, b]
155
+ end
156
+
157
+ def get_dpi()
158
+ return (@x_pix_per_meter / 39.375).round
159
+ end
160
+
161
+ def set_dpi(dpi)
162
+ @x_pix_per_meter = (39.375 * dpi).round
163
+ @y_pix_per_meter = @x_pix_per_meter
164
+ end
165
+
166
+ # x0, y0 は、貼り付ける始点(左上)の座標
167
+ def paste(image, x0 = 0, y0 = 0)
168
+ return if image == nil
169
+ image.height.times do |from_y|
170
+ y = y0 + from_y
171
+ next if y < 0 or @height <= y
172
+
173
+ image.width.times do |from_x|
174
+ x = x0 + from_x
175
+ next if x < 0 or @width <= x
176
+ color = image.pget(from_x, from_y)
177
+ self.pset(x, y, color[0], color[1], color[2])
178
+ end
179
+ end
180
+ end
181
+ end
182
+ end
@@ -0,0 +1,3 @@
1
+ module FB2Image
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,58 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'spec_helper'
3
+ module FB2Image
4
+ describe FB do
5
+ let(:create_fb) do
6
+ width = 100
7
+ height = 20
8
+
9
+ # make gradation fb
10
+ line = (0...width).map {|x| [x * 255 / width, 0, 0, 0] }
11
+ fb = ([line] * height).flatten
12
+
13
+ return FB.new(width, height, fb, {
14
+ :bits_per_pixel => 32,
15
+ :red_shift => 0,
16
+ :green_shift => 8,
17
+ :blue_shift => 16
18
+ })
19
+ end
20
+
21
+ describe "#write_png" do
22
+ it "should write png file" do
23
+ fb = create_fb
24
+ fb.write_png("tmp/gradation.png")
25
+ end
26
+ end
27
+
28
+ describe "#write_bmp" do
29
+ it "should write bmp file" do
30
+ fb = create_fb
31
+ fb.write_bmp("tmp/gradation.bmp")
32
+ end
33
+ end
34
+
35
+ describe "sample_code" do
36
+ width = 100
37
+ height = 20
38
+
39
+ # make gradation fb
40
+ line = (0...width).map {|x| [x * 255 / width, 0, 0, 0] }
41
+ fb = ([line] * height).flatten
42
+
43
+ # initialize FB
44
+ fb = FB.new(width, height, fb, {
45
+ :bits_per_pixel => 32,
46
+ :red_shift => 0,
47
+ :green_shift => 8,
48
+ :blue_shift => 16
49
+ })
50
+
51
+ # create png
52
+ fb.write_png("tmp/gradation.png")
53
+
54
+ # create bmp
55
+ fb.write_bmp("tmp/gradation.bmp")
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'pp'
4
+
5
+ require 'fb2image' # and any other gems you need
6
+
7
+ RSpec.configure do |config|
8
+ `mkdir -p tmp`
9
+ # some (optional) config here
10
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fb2image
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Kazuhiro Yamada
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-08-08 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Convert Frame Buffer to Image
63
+ email:
64
+ - yamadakazu45@gmail.com
65
+ executables:
66
+ - fb2image
67
+ extensions: []
68
+ extra_rdoc_files: []
69
+ files:
70
+ - .gitignore
71
+ - Gemfile
72
+ - LICENSE.txt
73
+ - README.md
74
+ - Rakefile
75
+ - bin/fb2image
76
+ - fb2image.gemspec
77
+ - lib/fb2image.rb
78
+ - lib/fb2image/bmp.rb
79
+ - lib/fb2image/version.rb
80
+ - spec/fb2image_spec.rb
81
+ - spec/spec_helper.rb
82
+ homepage: https://github.com/k-yamada/fb2image
83
+ licenses:
84
+ - MIT
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ! '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ! '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubyforge_project:
103
+ rubygems_version: 1.8.23
104
+ signing_key:
105
+ specification_version: 3
106
+ summary: Convert Frame Buffer to Image
107
+ test_files:
108
+ - spec/fb2image_spec.rb
109
+ - spec/spec_helper.rb