Class: Dimensions::Reader
- Inherits:
-
Object
- Object
- Dimensions::Reader
- Defined in:
- lib/dimensions/reader.rb
Constant Summary collapse
- GIF_HEADER =
[0x47, 0x49, 0x46, 0x38]
- PNG_HEADER =
[0x89, 0x50, 0x4E, 0x47]
- JPEG_HEADER =
[0xFF, 0xD8, 0xFF]
- TIFF_HEADER_I =
[0x49, 0x49, 0x2A, 0x00]
- TIFF_HEADER_M =
[0x4D, 0x4D, 0x00, 0x2A]
Instance Attribute Summary collapse
-
#angle ⇒ Object
readonly
Returns the value of attribute angle.
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Instance Method Summary collapse
- #<<(data) ⇒ Object
- #determine_type ⇒ Object
- #extract_gif_dimensions ⇒ Object
- #extract_jpeg_dimensions ⇒ Object
- #extract_png_dimensions ⇒ Object
- #extract_tiff_dimensions ⇒ Object
-
#initialize ⇒ Reader
constructor
A new instance of Reader.
- #match_header(header, bytes) ⇒ Object
- #process(process = @process) ⇒ Object
Constructor Details
#initialize ⇒ Reader
Returns a new instance of Reader.
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/dimensions/reader.rb', line 13 def initialize @process = :determine_type @type = nil @width = nil @height = nil @angle = nil @size = 0 @data = "" @data.force_encoding("BINARY") if @data.respond_to?(:force_encoding) end |
Instance Attribute Details
#angle ⇒ Object (readonly)
Returns the value of attribute angle.
11 12 13 |
# File 'lib/dimensions/reader.rb', line 11 def angle @angle end |
#height ⇒ Object (readonly)
Returns the value of attribute height.
11 12 13 |
# File 'lib/dimensions/reader.rb', line 11 def height @height end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
11 12 13 |
# File 'lib/dimensions/reader.rb', line 11 def type @type end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
11 12 13 |
# File 'lib/dimensions/reader.rb', line 11 def width @width end |
Instance Method Details
#<<(data) ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/dimensions/reader.rb', line 24 def <<(data) if @process @data << data @size = @data.length process end end |
#determine_type ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/dimensions/reader.rb', line 36 def determine_type if @size >= 4 bytes = @data.unpack("C4") if match_header(GIF_HEADER, bytes) @type = :gif elsif match_header(PNG_HEADER, bytes) @type = :png elsif match_header(JPEG_HEADER, bytes) @type = :jpeg elsif match_header(TIFF_HEADER_I, bytes) || match_header(TIFF_HEADER_M, bytes) @type = :tiff end process @type ? :"extract_#{type}_dimensions" : nil end end |
#extract_gif_dimensions ⇒ Object
54 55 56 57 58 59 |
# File 'lib/dimensions/reader.rb', line 54 def extract_gif_dimensions if @size >= 10 @width, @height = @data.unpack("x6v2") process nil end end |
#extract_jpeg_dimensions ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/dimensions/reader.rb', line 68 def extract_jpeg_dimensions scanner = JpegScanner.new(@data) if scanner.scan @width = scanner.width @height = scanner.height @angle = scanner.angle if @angle == 90 || @angle == 270 @width, @height = @height, @width end process nil end rescue JpegScanner::ScanError end |
#extract_png_dimensions ⇒ Object
61 62 63 64 65 66 |
# File 'lib/dimensions/reader.rb', line 61 def extract_png_dimensions if @size >= 24 @width, @height = @data.unpack("x16N2") process nil end end |
#extract_tiff_dimensions ⇒ Object
84 85 86 87 88 89 90 91 92 |
# File 'lib/dimensions/reader.rb', line 84 def extract_tiff_dimensions scanner = TiffScanner.new(@data) if scanner.scan @width = scanner.width @height = scanner.height process nil end rescue TiffScanner::ScanError end |
#match_header(header, bytes) ⇒ Object
94 95 96 |
# File 'lib/dimensions/reader.rb', line 94 def match_header(header, bytes) bytes[0, header.length] == header end |
#process(process = @process) ⇒ Object
32 33 34 |
# File 'lib/dimensions/reader.rb', line 32 def process(process = @process) send(@process) if @process = process end |