simple_bmp 0.1.0 → 0.2.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/simple_bmp.rb +42 -31
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8d90d75b6ec1571cd0aa6d6d07e6ad2972564fa2
|
4
|
+
data.tar.gz: 39a0f5d31d5d4cca1365ab592d4d90a2ee7eab1b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6233ec72c8fc2677c092d531733d0d6edf07b221ba310318cf741985490426167f6de16dbd4be3f96a01cc6609b20bdba1ba0ff0aefa58fe0d8e592a25972211
|
7
|
+
data.tar.gz: c92b8b2d761bc83cb8670e7505e064b6ad1872b6b19b5d0cf84944b2849628e6740d63abd6e81e3eda695c670d576767ad8314ea436799aab49ffbb497b41c23
|
data/lib/simple_bmp.rb
CHANGED
@@ -1,61 +1,72 @@
|
|
1
|
-
|
2
|
-
#
|
3
|
-
#
|
1
|
+
# Currently there are no need of classes -- just a single public module method.
|
2
|
+
# We use only BITMAPCOREHEADER[http://en.wikipedia.org/wiki/BMP_file_format] (12 bytes), because it's enough.
|
4
3
|
|
5
4
|
module SimpleBMP
|
6
5
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
#
|
6
|
+
# http://stackoverflow.com/a/12925407/322020
|
7
|
+
@@align4 = ->_{ (_ + 3) / 4 * 4 }
|
8
|
+
|
9
|
+
# \+pixels+ is either two or three dimensional array:
|
10
|
+
# [height x width x 3] of 0..255 for RGB
|
11
|
+
# [height x width] of 0..255 for grayscale
|
11
12
|
# Two-dimensional will be processed as it's three, where R, G anb B are equal.
|
12
13
|
#
|
13
14
|
# Examples of exporting a single pixel image which is almost gray:
|
14
15
|
# export [[100]], "temp.bmp"
|
15
16
|
# export [[[90,100,110]]], "temp.bmp"
|
16
|
-
#--
|
17
|
-
# we use BITMAPINFOHEADER (40 bytes), because BITMAPCOREHEADER (12 bytes) can't 1 channel
|
18
17
|
|
19
18
|
def self.export pixels, filename = "temp.bmp"
|
20
19
|
bitmap_width = pixels.first.size
|
21
20
|
bitmap_height = pixels.size
|
22
|
-
=begin
|
23
|
-
--
|
24
|
-
hsv2bgr = lambda do |(h,s,v)|
|
25
|
-
a = (v - vmin = (100-s)*v/100)*(h % 60)/60.0
|
26
|
-
vinc, vdec = vmin + a, v - a
|
27
|
-
case h/60
|
28
|
-
when 0 ; [vmin, vinc, v]
|
29
|
-
when 1 ; [vmin, v, vdec]
|
30
|
-
when 2 ; [vinc, v, vmin]
|
31
|
-
when 3 ; [v, vdec, vmin]
|
32
|
-
when 4 ; [v, vmin, vinc]
|
33
|
-
when 5 ; [vdec, vmin, v]
|
34
|
-
end.map{ |i| (2.55 * i).floor }
|
35
|
-
end
|
36
|
-
=end
|
37
21
|
pixel_array = pixels.reverse.flat_map do |row|
|
38
|
-
#--
|
39
22
|
# row_bytes = row.map(&hsv2bgr).flatten
|
40
|
-
row_bytes = row.flat_map{ |pixel| pixel.is_a?(Array) ? pixel : [pixel]*3 }
|
41
|
-
row_bytes.fill 0, row_bytes.size ..
|
23
|
+
row_bytes = row.flat_map{ |pixel| pixel.is_a?(Array) ? pixel.reverse : [pixel]*3 }
|
24
|
+
row_bytes.fill 0, row_bytes.size .. @@align4[row_bytes.size] - 1
|
42
25
|
end
|
43
26
|
|
44
|
-
#--
|
45
27
|
# 12=>BITMAPCOREHEADER, 40=>BITMAPINFOHEADER
|
46
28
|
dib_header_size = 12
|
47
29
|
file_size = 14 + dib_header_size + pixel_array.size
|
48
30
|
|
49
31
|
open(filename, "wb") do |file|
|
50
|
-
#--
|
51
32
|
# Bitmap file header
|
52
33
|
file.write ["BM", file_size, 14 + dib_header_size].pack "A2qi" # "A2QL"
|
53
|
-
#--
|
54
34
|
# DIB header
|
55
35
|
# file.write [dib_header_size, bitmap_width, bitmap_height, 1, 24].pack "LllSS"
|
56
|
-
# file.write [0, 0, 1, 1, 0, 0].pack "LLllLL"
|
36
|
+
# file.write [0, 0, 1, 1, 0, 0].pack "LLllLL" # BITMAPINFOHEADER
|
57
37
|
file.write [dib_header_size, bitmap_width, bitmap_height, 1, 24].pack "LSSSS"
|
58
38
|
file.write pixel_array.pack "C*"
|
59
39
|
end
|
60
40
|
end
|
41
|
+
|
42
|
+
# This imports BITMAPCOREHEADER headed RGB bmp images returning a 3-dimentional array.
|
43
|
+
|
44
|
+
def self.import filename = "temp.bmp"
|
45
|
+
File.open(filename, "rb") do |file|
|
46
|
+
_, _, shift = file.read(14).unpack("A2qi")
|
47
|
+
dib_header_size, bitmap_width, bitmap_height = file.read(8).unpack("LSS")
|
48
|
+
fail "can import only BITMAPCOREHEADER (12 bytes)" unless dib_header_size == 12
|
49
|
+
file.pos = shift
|
50
|
+
(file.read(bitmap_height * @@align4[bitmap_width * 3]).unpack("C*")).each_slice(@@align4[bitmap_width * 3]).map do |row|
|
51
|
+
row.each_slice(3).take(bitmap_width).map(&:reverse)
|
52
|
+
end.reverse
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
61
56
|
end
|
57
|
+
|
58
|
+
=begin
|
59
|
+
--
|
60
|
+
hsv2bgr = lambda do |(h,s,v)|
|
61
|
+
a = (v - vmin = (100-s)*v/100)*(h % 60)/60.0
|
62
|
+
vinc, vdec = vmin + a, v - a
|
63
|
+
case h/60
|
64
|
+
when 0 ; [vmin, vinc, v]
|
65
|
+
when 1 ; [vmin, v, vdec]
|
66
|
+
when 2 ; [vinc, v, vmin]
|
67
|
+
when 3 ; [v, vdec, vmin]
|
68
|
+
when 4 ; [v, vmin, vinc]
|
69
|
+
when 5 ; [vdec, vmin, v]
|
70
|
+
end.map{ |i| (2.55 * i).floor }
|
71
|
+
end
|
72
|
+
=end
|
metadata
CHANGED
@@ -1,18 +1,18 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_bmp
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Victor Maslov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-05-
|
11
|
+
date: 2014-05-15 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
|
-
description: As simple as you really need.
|
14
|
-
|
15
|
-
know -- I'll update gemspec.
|
13
|
+
description: As simple as you really need. Only BITMAPCOREHEADER (12 bytes) header
|
14
|
+
is used yet, and only 8bpp with no indexing. If it seems to work under <=1.8.7,
|
15
|
+
let me know -- I'll update gemspec.
|
16
16
|
email: nakilon@gmail.com
|
17
17
|
executables: []
|
18
18
|
extensions: []
|