simple_bmp 0.0.3 → 0.1.0

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 +4 -4
  2. data/lib/simple_bmp.rb +33 -10
  3. metadata +5 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cb9f1ad83d93fa70a90dcd64b6443b3e4f55f05c
4
- data.tar.gz: 9ab7e0f122ade992d4824ceb61e40bdc2e2dbe04
3
+ metadata.gz: d84f668c6933430316c3ac409ecc577b7fb71924
4
+ data.tar.gz: cf46732f084471b98c9f106fedfa781451b6e432
5
5
  SHA512:
6
- metadata.gz: b4c5a14f65ec7c8e9f905708cf41076ab60fb0b454a35c6d101447a8f7f1368e24809c34b5b91dca3275c6d6c36d6321879918f883f9a67ef564cf3b1afe65a8
7
- data.tar.gz: e202d61ca18e776abcae7b87474f4f054116ce3582dcffa5d7d1b537ddeff82d7a4c36d58c00ef85a0edad6cc165e4aba430a53d171225f92b153b14e1969cf0
6
+ metadata.gz: 66d1d770f7bbd94fb8b36356f0c9c77d1871d2ca8ec1f720283126dca9a892257f2f41774d26d185fa5ee45714dfe63536b3c7c621f7b5a764a83feaa512d7ed
7
+ data.tar.gz: 97ecd52f84f73ef570d0dbdadfe5359b6ef5fc87875b6786c8b475724f68d9be75a922240eb395ca723b5d7e378fee722c95eeb3d973d2240a2edf8196313cf6
data/lib/simple_bmp.rb CHANGED
@@ -1,11 +1,26 @@
1
+ ##
2
+ # Currently there are no need of classes -- just a single public module method.
3
+ #
4
+
1
5
  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
6
+
7
+ ##
8
+ # Pixels is either two or three dimensional array:
9
+ # pixels: [height x width x 3] of 0..255 for RGB
10
+ # pixels: [height x width] of 0..255 for grayscale
11
+ # Two-dimensional will be processed as it's three, where R, G anb B are equal.
12
+ #
13
+ # Examples of exporting a single pixel image which is almost gray:
14
+ # export [[100]], "temp.bmp"
15
+ # export [[[90,100,110]]], "temp.bmp"
16
+ #--
17
+ # we use BITMAPINFOHEADER (40 bytes), because BITMAPCOREHEADER (12 bytes) can't 1 channel
18
+
5
19
  def self.export pixels, filename = "temp.bmp"
6
20
  bitmap_width = pixels.first.size
7
21
  bitmap_height = pixels.size
8
22
  =begin
23
+ --
9
24
  hsv2bgr = lambda do |(h,s,v)|
10
25
  a = (v - vmin = (100-s)*v/100)*(h % 60)/60.0
11
26
  vinc, vdec = vmin + a, v - a
@@ -20,18 +35,26 @@ module SimpleBMP
20
35
  end
21
36
  =end
22
37
  pixel_array = pixels.reverse.flat_map do |row|
23
- #row_bytes = row.map(&hsv2bgr).flatten
38
+ #--
39
+ # row_bytes = row.map(&hsv2bgr).flatten
24
40
  row_bytes = row.flat_map{ |pixel| pixel.is_a?(Array) ? pixel : [pixel]*3 }
25
41
  row_bytes.fill 0, row_bytes.size .. (row_bytes.size + 3) / 4 * 4 - 1
26
42
  end
27
- file_size = 14 + 40 + pixel_array.size
43
+
44
+ #--
45
+ # 12=>BITMAPCOREHEADER, 40=>BITMAPINFOHEADER
46
+ dib_header_size = 12
47
+ file_size = 14 + dib_header_size + pixel_array.size
28
48
 
29
49
  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"
50
+ #--
51
+ # Bitmap file header
52
+ file.write ["BM", file_size, 14 + dib_header_size].pack "A2qi" # "A2QL"
53
+ #--
54
+ # DIB header
55
+ # file.write [dib_header_size, bitmap_width, bitmap_height, 1, 24].pack "LllSS"
56
+ # file.write [0, 0, 1, 1, 0, 0].pack "LLllLL"
57
+ file.write [dib_header_size, bitmap_width, bitmap_height, 1, 24].pack "LSSSS"
35
58
  file.write pixel_array.pack "C*"
36
59
  end
37
60
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_bmp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Victor Maslov
@@ -10,7 +10,9 @@ bindir: bin
10
10
  cert_chain: []
11
11
  date: 2014-05-14 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: A simple BMP renderer
13
+ description: As simple as you really need. You get only 40 bytes type header, and
14
+ currently only either RGB or Grayscale. If it seems to work under <=1.8.7, let me
15
+ know -- I'll update gemspec.
14
16
  email: nakilon@gmail.com
15
17
  executables: []
16
18
  extensions: []
@@ -29,7 +31,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
29
31
  requirements:
30
32
  - - '>='
31
33
  - !ruby/object:Gem::Version
32
- version: 1.8.7
34
+ version: 2.0.0
33
35
  required_rubygems_version: !ruby/object:Gem::Requirement
34
36
  requirements:
35
37
  - - '>='