Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Moved parsers into a Parsers module

  • Loading branch information...
commit 2eae50f32b5c43b85d7ddb6e2849726f929b839c 1 parent 4b38f02
Michael Sheakoski authored
View
8 lib/image_spec.rb
@@ -18,10 +18,10 @@ def initialize(file)
@content_type ||= content_type_from_filename(@filename)
@width, @height = case CONTENT_TYPES[@content_type]
- when :gif then GIF.dimensions(@stream)
- when :jpeg then JPEG.dimensions(@stream)
- when :png then PNG.dimensions(@stream)
- when :swf then SWF.dimensions(@stream)
+ when :gif then Parsers::GIF.dimensions(@stream)
+ when :jpeg then Parsers::JPEG.dimensions(@stream)
+ when :png then Parsers::PNG.dimensions(@stream)
+ when :swf then Parsers::SWF.dimensions(@stream)
end
end
View
12 lib/parsers/gif.rb
@@ -1,10 +1,14 @@
class ImageSpec
- class GIF
+ module Parsers
+
+ class GIF
+
+ def self.dimensions(file)
+ file.seek(6)
+ file.read(4).unpack('SS')
+ end
- def self.dimensions(file)
- file.seek(6)
- file.read(4).unpack('SS')
end
end
View
62 lib/parsers/jpeg.rb
@@ -1,38 +1,42 @@
class ImageSpec
- class JPEG
-
- def self.dimensions(io)
- raise 'malformed JPEG' unless io.getc == 0xFF && io.getc == 0xD8 # SOI
-
- class << io
- def readint; (readchar << 8) + readchar; end
- def readframe; read(readint - 2); end
- def readsof; [readint, readchar, readint, readint, readchar]; end
- def next
- c = readchar while c != 0xFF
- c = readchar while c == 0xFF
- c
+ module Parsers
+
+ class JPEG
+
+ def self.dimensions(io)
+ raise 'malformed JPEG' unless io.getc == 0xFF && io.getc == 0xD8 # SOI
+
+ class << io
+ def readint; (readchar << 8) + readchar; end
+ def readframe; read(readint - 2); end
+ def readsof; [readint, readchar, readint, readint, readchar]; end
+ def next
+ c = readchar while c != 0xFF
+ c = readchar while c == 0xFF
+ c
+ end
end
- end
- while marker = io.next
- case marker
- when 0xC0..0xC3, 0xC5..0xC7, 0xC9..0xCB, 0xCD..0xCF
- length, bits, height, width, components = io.readsof
- raise 'malformed JPEG' unless length == 8 + components * 3
- return [width, height]
- when 0xD9, 0xDA:
- break
- when 0xFE:
- @comment = io.readframe
- when 0xE1:
- io.readframe
- else
- io.readframe
+ while marker = io.next
+ case marker
+ when 0xC0..0xC3, 0xC5..0xC7, 0xC9..0xCB, 0xCD..0xCF
+ length, bits, height, width, components = io.readsof
+ raise 'malformed JPEG' unless length == 8 + components * 3
+ return [width, height]
+ when 0xD9, 0xDA:
+ break
+ when 0xFE:
+ @comment = io.readframe
+ when 0xE1:
+ io.readframe
+ else
+ io.readframe
+ end
end
end
- end
+
+ end
end
View
12 lib/parsers/png.rb
@@ -1,10 +1,14 @@
class ImageSpec
- class PNG
+ module Parsers
+
+ class PNG
+
+ def self.dimensions(file)
+ file.seek(0x10)
+ file.read(8).unpack('NN')
+ end
- def self.dimensions(file)
- file.seek(0x10)
- file.read(8).unpack('NN')
end
end
View
82 lib/parsers/swf.rb
@@ -2,57 +2,61 @@
class ImageSpec
- class SWF
+ module Parsers
- def self.dimensions(file)
- # Read the entire file into memory because the
- # dimensions aren't stored in a standard location
- contents = file.read
+ class SWF
- # Our 'signature' is the first 3 bytes
- # Either FWS or CWS. CWS indicates compression
- signature = contents[0..2]
+ def self.dimensions(file)
+ # Read the entire file into memory because the
+ # dimensions aren't stored in a standard location
+ contents = file.read
- # Determine the length of the uncompressed file
- length = contents[4..7].unpack('V').join.to_i
+ # Our 'signature' is the first 3 bytes
+ # Either FWS or CWS. CWS indicates compression
+ signature = contents[0..2]
- # If we do, in fact, have compression
- if signature == 'CWS'
- # Decompress the body of the SWF
- body = Zlib::Inflate.inflate( contents[8..length] )
+ # Determine the length of the uncompressed file
+ length = contents[4..7].unpack('V').join.to_i
- # And reconstruct the file contents to the first 8 bytes (header)
- # Plus our decompressed body
- contents = contents[0..7] + body
- end
+ # If we do, in fact, have compression
+ if signature == 'CWS'
+ # Decompress the body of the SWF
+ body = Zlib::Inflate.inflate( contents[8..length] )
- # Determine the nbits of our dimensions rectangle
- nbits = contents[8] >> 3
+ # And reconstruct the file contents to the first 8 bytes (header)
+ # Plus our decompressed body
+ contents = contents[0..7] + body
+ end
- # Determine how many bits long this entire RECT structure is
- rectbits = 5 + nbits * 4 # 5 bits for nbits, as well as nbits * number of fields (4)
+ # Determine the nbits of our dimensions rectangle
+ nbits = contents[8] >> 3
- # Determine how many bytes rectbits composes (ceil(rectbits/8))
- rectbytes = (rectbits.to_f / 8).ceil
+ # Determine how many bits long this entire RECT structure is
+ rectbits = 5 + nbits * 4 # 5 bits for nbits, as well as nbits * number of fields (4)
- # Unpack the RECT structure from the file in little-endian bit order, then join it into a string
- rect = contents[8..(8 + rectbytes)].unpack("#{'B8' * rectbytes}").join()
+ # Determine how many bytes rectbits composes (ceil(rectbits/8))
+ rectbytes = (rectbits.to_f / 8).ceil
- # Read in nbits incremenets starting from 5
- dimensions = Array.new
- 4.times do |n|
- s = 5 + (n * nbits) # Calculate our start index
- e = s + (nbits - 1) # Calculate our end index
- dimensions[n] = rect[s..e].to_i(2) # Read that range (binary) and convert it to an integer
- end
+ # Unpack the RECT structure from the file in little-endian bit order, then join it into a string
+ rect = contents[8..(8 + rectbytes)].unpack("#{'B8' * rectbytes}").join()
+
+ # Read in nbits incremenets starting from 5
+ dimensions = Array.new
+ 4.times do |n|
+ s = 5 + (n * nbits) # Calculate our start index
+ e = s + (nbits - 1) # Calculate our end index
+ dimensions[n] = rect[s..e].to_i(2) # Read that range (binary) and convert it to an integer
+ end
- # The values we have here are in "twips"
- # 20 twips to a pixel (that's why SWFs are fuzzy sometimes!)
- width = (dimensions[1] - dimensions[0]) / 20
- height = (dimensions[3] - dimensions[2]) / 20
+ # The values we have here are in "twips"
+ # 20 twips to a pixel (that's why SWFs are fuzzy sometimes!)
+ width = (dimensions[1] - dimensions[0]) / 20
+ height = (dimensions[3] - dimensions[2]) / 20
- # If you can't figure this one out, you probably shouldn't have read this far
- return [width, height]
+ # If you can't figure this one out, you probably shouldn't have read this far
+ return [width, height]
+
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.