simple_bmp 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/simple_bmp.rb +38 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: cb9f1ad83d93fa70a90dcd64b6443b3e4f55f05c
4
+ data.tar.gz: 9ab7e0f122ade992d4824ceb61e40bdc2e2dbe04
5
+ SHA512:
6
+ metadata.gz: b4c5a14f65ec7c8e9f905708cf41076ab60fb0b454a35c6d101447a8f7f1368e24809c34b5b91dca3275c6d6c36d6321879918f883f9a67ef564cf3b1afe65a8
7
+ data.tar.gz: e202d61ca18e776abcae7b87474f4f054116ce3582dcffa5d7d1b537ddeff82d7a4c36d58c00ef85a0edad6cc165e4aba430a53d171225f92b153b14e1969cf0
data/lib/simple_bmp.rb ADDED
@@ -0,0 +1,38 @@
1
+ module SimpleBMP
2
+ # export 2-dimensional array [height x width] of 0..255 for grayscale
3
+ # export 3-dimensional array [height x width x 3] of 0..255 for RGB
4
+ # we use BITMAPINFOHEADER (40 bytes), because BITMAPCOREHEADER (12 bytes) can't 8bpp
5
+ def self.export pixels, filename = "temp.bmp"
6
+ bitmap_width = pixels.first.size
7
+ bitmap_height = pixels.size
8
+ =begin
9
+ hsv2bgr = lambda do |(h,s,v)|
10
+ a = (v - vmin = (100-s)*v/100)*(h % 60)/60.0
11
+ vinc, vdec = vmin + a, v - a
12
+ case h/60
13
+ when 0 ; [vmin, vinc, v]
14
+ when 1 ; [vmin, v, vdec]
15
+ when 2 ; [vinc, v, vmin]
16
+ when 3 ; [v, vdec, vmin]
17
+ when 4 ; [v, vmin, vinc]
18
+ when 5 ; [vdec, vmin, v]
19
+ end.map{ |i| (2.55 * i).floor }
20
+ end
21
+ =end
22
+ pixel_array = pixels.reverse.flat_map do |row|
23
+ #row_bytes = row.map(&hsv2bgr).flatten
24
+ row_bytes = row.flat_map{ |pixel| pixel.is_a?(Array) ? pixel : [pixel]*3 }
25
+ row_bytes.fill 0, row_bytes.size .. (row_bytes.size + 3) / 4 * 4 - 1
26
+ end
27
+ file_size = 14 + 40 + pixel_array.size
28
+
29
+ open(filename, "wb") do |file|
30
+ # Bitmap file header (14 bytes)
31
+ file.write ["BM", file_size, 14 + 40].pack "A2qi" # "A2QL"
32
+ # DIB header (BITMAPINFOHEADER, 40 bytes)
33
+ file.write [40, bitmap_width, bitmap_height, 1, 24].pack "LllSS"
34
+ file.write [0, 0, 1, 1, 0, 0].pack "LLllLL"
35
+ file.write pixel_array.pack "C*"
36
+ end
37
+ end
38
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_bmp
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Victor Maslov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-14 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A simple BMP renderer
14
+ email: nakilon@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/simple_bmp.rb
20
+ homepage: http://rubygems.org/gems/simple_bmp
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - '>='
31
+ - !ruby/object:Gem::Version
32
+ version: 1.8.7
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.0.14
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: A simple BMP renderer
44
+ test_files: []