format_parser 0.8.0 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +3 -0
- data/lib/format_parser/version.rb +1 -1
- data/lib/parsers/bmp_parser.rb +41 -0
- data/spec/parsers/bmp_parser_spec.rb +39 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 91bcb409bce9e0576a83680ef91cd6e910e6df9b4bc4953cb23de8d887021b59
|
4
|
+
data.tar.gz: ef6f74cd026bb3dd26e16c3282f9ec71e5b63f8e957d4b818cdac0acbc058c74
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c118d28ecce997d45058d3b19de89d1c8ab6a88df390c58875f4ad22b36043f4562913c78c66cea380357184d48fddf6cf5736a8f7f7eb62a83fea6bddc85b13
|
7
|
+
data.tar.gz: 70ee5504c514fbed060ca62cf0cd0e429f1f01535e805de6a974dbcd5a9154b81c859a70a09ad39fc2ef8c341391f3f1f2a2ed3566a19976884dea03b9d541d6
|
data/CHANGELOG.md
CHANGED
@@ -0,0 +1,41 @@
|
|
1
|
+
# Based on https://en.wikipedia.org/wiki/BMP_file_format
|
2
|
+
|
3
|
+
class FormatParser::BMPParser
|
4
|
+
include FormatParser::IOUtils
|
5
|
+
|
6
|
+
VALID_BMP = 'BM'
|
7
|
+
PIXEL_ARRAY_OFFSET = 54
|
8
|
+
|
9
|
+
def call(io)
|
10
|
+
io = FormatParser::IOConstraint.new(io)
|
11
|
+
|
12
|
+
magic_number, _file_size, _reserved1, _reserved2, dib_header_location = safe_read(io, 14).unpack('A2Vv2V')
|
13
|
+
return unless VALID_BMP == magic_number
|
14
|
+
return unless dib_header_location == PIXEL_ARRAY_OFFSET
|
15
|
+
|
16
|
+
dib_header = safe_read(io, 40)
|
17
|
+
|
18
|
+
_header_size, width, height, _planes, _bits_per_pixel,
|
19
|
+
_compression_method, _image_size, horizontal_res,
|
20
|
+
vertical_res, _n_colors, _i_colors = dib_header.unpack('Vl<2v2V2l<2V2')
|
21
|
+
|
22
|
+
# There are cases where the height might by negative indicating the data
|
23
|
+
# is ordered from top to bottom instead of bottom to top
|
24
|
+
# http://www.dragonwins.com/domains/getteched/bmp/bmpfileformat.htm#The%20Image%20Header
|
25
|
+
data_order = height < 0 ? :inverse : :normal
|
26
|
+
|
27
|
+
FormatParser::Image.new(
|
28
|
+
format: :bmp,
|
29
|
+
width_px: width,
|
30
|
+
height_px: height.abs,
|
31
|
+
color_mode: :rgb,
|
32
|
+
intrinsics: {
|
33
|
+
vertical_resolution: vertical_res,
|
34
|
+
horizontal_resolution: horizontal_res,
|
35
|
+
data_order: data_order
|
36
|
+
}
|
37
|
+
)
|
38
|
+
end
|
39
|
+
|
40
|
+
FormatParser.register_parser self, natures: :image, formats: :bmp
|
41
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe FormatParser::BMPParser do
|
4
|
+
it 'parses a BMP file with positive height_px values' do
|
5
|
+
bmp_path = fixtures_dir + '/BMP/test.bmp'
|
6
|
+
parsed = subject.call(File.open(bmp_path, 'rb'))
|
7
|
+
|
8
|
+
expect(parsed).not_to be_nil
|
9
|
+
expect(parsed.nature).to eq(:image)
|
10
|
+
expect(parsed.format).to eq(:bmp)
|
11
|
+
expect(parsed.color_mode).to eq(:rgb)
|
12
|
+
|
13
|
+
expect(parsed.width_px).to eq(40)
|
14
|
+
expect(parsed.height_px).to eq(27)
|
15
|
+
|
16
|
+
expect(parsed.intrinsics).not_to be_nil
|
17
|
+
expect(parsed.intrinsics[:vertical_resolution]).to eq(2834)
|
18
|
+
expect(parsed.intrinsics[:horizontal_resolution]).to eq(2834)
|
19
|
+
expect(parsed.intrinsics[:data_order]).to eq(:normal)
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'parses a BMP file with negative height_px values (divergent scan order)' do
|
23
|
+
bmp_path = fixtures_dir + '/BMP/test2.bmp'
|
24
|
+
parsed = subject.call(File.open(bmp_path, 'rb'))
|
25
|
+
|
26
|
+
expect(parsed).not_to be_nil
|
27
|
+
expect(parsed.nature).to eq(:image)
|
28
|
+
expect(parsed.format).to eq(:bmp)
|
29
|
+
expect(parsed.color_mode).to eq(:rgb)
|
30
|
+
|
31
|
+
expect(parsed.width_px).to eq(1920)
|
32
|
+
expect(parsed.height_px).to eq(1080)
|
33
|
+
|
34
|
+
expect(parsed.intrinsics).not_to be_nil
|
35
|
+
expect(parsed.intrinsics[:vertical_resolution]).to eq(2835)
|
36
|
+
expect(parsed.intrinsics[:horizontal_resolution]).to eq(2835)
|
37
|
+
expect(parsed.intrinsics[:data_order]).to eq(:inverse)
|
38
|
+
end
|
39
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: format_parser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.9.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Noah Berman
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date: 2018-04-
|
12
|
+
date: 2018-04-18 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: ks
|
@@ -172,6 +172,7 @@ files:
|
|
172
172
|
- lib/io_utils.rb
|
173
173
|
- lib/measurometer.rb
|
174
174
|
- lib/parsers/aiff_parser.rb
|
175
|
+
- lib/parsers/bmp_parser.rb
|
175
176
|
- lib/parsers/cr2_parser.rb
|
176
177
|
- lib/parsers/dpx_parser.rb
|
177
178
|
- lib/parsers/exif_parser.rb
|
@@ -204,6 +205,7 @@ files:
|
|
204
205
|
- spec/io_utils_spec.rb
|
205
206
|
- spec/measurometer_spec.rb
|
206
207
|
- spec/parsers/aiff_parser_spec.rb
|
208
|
+
- spec/parsers/bmp_parser_spec.rb
|
207
209
|
- spec/parsers/cr2_parser_spec.rb
|
208
210
|
- spec/parsers/dpx_parser_spec.rb
|
209
211
|
- spec/parsers/exif_parser_spec.rb
|