Skip to content
This repository has been archived by the owner on Jun 10, 2018. It is now read-only.

add support for windows bitmap files #9

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions lib/dimensions/reader.rb
Expand Up @@ -7,6 +7,7 @@ class Reader
JPEG_HEADER = [0xFF, 0xD8, 0xFF]
TIFF_HEADER_I = [0x49, 0x49, 0x2A, 0x00]
TIFF_HEADER_M = [0x4D, 0x4D, 0x00, 0x2A]
BMP_HEADER = [0x42, 0x4D]

attr_reader :type, :width, :height, :angle

Expand Down Expand Up @@ -45,12 +46,26 @@ def determine_type
@type = :jpeg
elsif match_header(TIFF_HEADER_I, bytes) || match_header(TIFF_HEADER_M, bytes)
@type = :tiff
elsif match_header(BMP_HEADER, bytes)
@type = :bmp
end

process @type ? :"extract_#{type}_dimensions" : nil
end
end

def extract_bmp_dimensions
if @size >= 26
dib_header_size = @data.unpack("x14C").first
@width, @height = if dib_header_size == 12
@data.unpack("x18v2")
else
@data.unpack("x18V2")
end
process nil
end
end

def extract_gif_dimensions
if @size >= 10
@width, @height = @data.unpack("x6v2")
Expand Down
Binary file added test/fixtures/os2.bmp
Binary file not shown.
Binary file added test/fixtures/windows.bmp
Binary file not shown.
8 changes: 8 additions & 0 deletions test/test_dimensions.rb
Expand Up @@ -30,6 +30,14 @@ def test_tiff_dimensions
assert_dimensions "short.tif", 50, 20
end

def test_windows_bmp_dimensions
assert_dimensions "windows.bmp", 127, 64
end

def test_os2_bmp_dimensions
assert_dimensions "os2.bmp", 127, 64
end

def assert_dimensions(filename, expected_width, expected_height)
actual_width, actual_height = Dimensions.dimensions(fixture_path(filename))
assert_equal "#{expected_width}x#{expected_height}", "#{actual_width}x#{actual_height}"
Expand Down