ruby-imagespec 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.0
data/lib/parser.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  Dir[File.join(File.dirname(__FILE__), 'parser/*.rb')].each { |parser| require parser }
2
2
 
3
3
  class ImageSpec
4
+ Error = Class.new(StandardError)
4
5
 
5
6
  module Parser
6
7
 
@@ -12,7 +13,7 @@ class ImageSpec
12
13
  formats.each do |format|
13
14
  return format.attributes(stream) if format.detected?(stream)
14
15
  end
15
- raise "#{stream.inspect} is not a supported image format. Sorry bub :("
16
+ raise Error, "#{stream.inspect} is not a supported image format. Sorry bub :("
16
17
  end
17
18
 
18
19
  end
data/lib/parser/gif.rb CHANGED
@@ -1,7 +1,6 @@
1
+ # encoding: ascii-8bit
1
2
  class ImageSpec
2
-
3
3
  module Parser
4
-
5
4
  class GIF
6
5
 
7
6
  CONTENT_TYPE = 'image/gif'
@@ -22,7 +21,5 @@ class ImageSpec
22
21
  end
23
22
 
24
23
  end
25
-
26
24
  end
27
-
28
25
  end
data/lib/parser/jpeg.rb CHANGED
@@ -1,10 +1,11 @@
1
+ # encoding: ascii-8bit
1
2
  class ImageSpec
2
-
3
3
  module Parser
4
-
5
4
  class JPEG
6
5
 
7
6
  CONTENT_TYPE = 'image/jpeg'
7
+ TYPE_JFIF = Regexp.new '^\xff\xd8\xff\xe0\x00\x10JFIF', nil, 'n'
8
+ TYPE_EXIF = Regexp.new '^\xff\xd8\xff\xe1(.*){2}Exif', nil, 'n'
8
9
 
9
10
  def self.attributes(stream)
10
11
  width, height = dimensions(stream)
@@ -14,19 +15,18 @@ class ImageSpec
14
15
  def self.detected?(stream)
15
16
  stream.rewind
16
17
  case stream.read(10)
17
- when /^\xff\xd8\xff\xe0\x00\x10JFIF/ then true
18
- when /^\xff\xd8\xff\xe1(.*){2}Exif/ then true
19
- else false
18
+ when TYPE_JFIF, TYPE_EXIF then true
19
+ else false
20
20
  end
21
21
  end
22
22
 
23
23
  def self.dimensions(stream)
24
24
  stream.rewind
25
- raise 'malformed JPEG' unless stream.getc == 0xFF && stream.getc == 0xD8 # SOI
25
+ raise ImageSpec::Error, 'malformed JPEG' unless stream.readbyte.chr == "\xFF" && stream.readbyte.chr == "\xD8" # SOI
26
26
 
27
27
  class << stream
28
28
  def readint
29
- (readchar << 8) + readchar
29
+ (readbyte.ord << 8) + readbyte.ord
30
30
  end
31
31
 
32
32
  def readframe
@@ -34,28 +34,26 @@ class ImageSpec
34
34
  end
35
35
 
36
36
  def readsof
37
- [readint, readchar, readint, readint, readchar]
37
+ [readint, readbyte.chr, readint, readint, readbyte.chr]
38
38
  end
39
39
 
40
40
  def next
41
- c = readchar while c != 0xFF
42
- c = readchar while c == 0xFF
41
+ c = readbyte.chr while c != "\xFF"
42
+ c = readbyte.chr while c == "\xFF"
43
43
  c
44
44
  end
45
45
  end
46
46
 
47
47
  while marker = stream.next
48
48
  case marker
49
- when 0xC0..0xC3, 0xC5..0xC7, 0xC9..0xCB, 0xCD..0xCF
49
+ when "\xC0".."\xC3", "\xC5".."\xC7", "\xC9".."\xCB", "\xCD".."\xCF"
50
50
  length, bits, height, width, components = stream.readsof
51
- raise 'malformed JPEG' unless length == 8 + components * 3
51
+ raise ImageSpec::Error, 'malformed JPEG' unless length == 8 + components[0].ord * 3
52
52
  return [width, height]
53
- when 0xD9, 0xDA
53
+ when "\xD9", "\xDA"
54
54
  break
55
- when 0xFE
55
+ when "\xFE"
56
56
  @comment = stream.readframe
57
- when 0xE1
58
- stream.readframe
59
57
  else
60
58
  stream.readframe
61
59
  end
@@ -63,7 +61,5 @@ class ImageSpec
63
61
  end
64
62
 
65
63
  end
66
-
67
64
  end
68
-
69
65
  end
data/lib/parser/png.rb CHANGED
@@ -1,7 +1,6 @@
1
+ # encoding: ascii-8bit
1
2
  class ImageSpec
2
-
3
3
  module Parser
4
-
5
4
  class PNG
6
5
 
7
6
  CONTENT_TYPE = 'image/png'
@@ -22,7 +21,5 @@ class ImageSpec
22
21
  end
23
22
 
24
23
  end
25
-
26
24
  end
27
-
28
25
  end
data/lib/parser/swf.rb CHANGED
@@ -1,9 +1,8 @@
1
+ # encoding: ascii-8bit
1
2
  require 'zlib'
2
3
 
3
4
  class ImageSpec
4
-
5
5
  module Parser
6
-
7
6
  class SWF
8
7
 
9
8
  CONTENT_TYPE = 'application/x-shockwave-flash'
@@ -30,12 +29,12 @@ class ImageSpec
30
29
 
31
30
  # Determine the length of the uncompressed stream
32
31
  length = contents[4..7].unpack('V').join.to_i
33
-
32
+
34
33
  # If we do, in fact, have compression
35
34
  if signature == 'CWS'
36
35
  # Decompress the body of the SWF
37
36
  body = Zlib::Inflate.inflate( contents[8..length] )
38
-
37
+
39
38
  # And reconstruct the stream contents to the first 8 bytes (header)
40
39
  # Plus our decompressed body
41
40
  contents = contents[0..7] + body
@@ -71,7 +70,5 @@ class ImageSpec
71
70
  end
72
71
 
73
72
  end
74
-
75
73
  end
76
-
77
74
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{ruby-imagespec}
8
- s.version = "0.1.0"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Brandon Anderson", "Michael Sheakoski", "Mike Boone", "Dimitrij Denissenko"]
12
- s.date = %q{2010-08-16}
12
+ s.date = %q{2010-08-26}
13
13
  s.description = %q{A lightweight module designed to extract the width/height dimensions of various image types. Also supports SWFs.}
14
14
  s.email = %q{dimitrij@blacksquaremedia.com}
15
15
  s.extra_rdoc_files = [
@@ -31,6 +31,11 @@ Gem::Specification.new do |s|
31
31
  "lib/parser/swf.rb",
32
32
  "ruby-imagespec.gemspec",
33
33
  "tasks/image_spec_tasks.rake",
34
+ "test/fixtures/corrupt.jpg",
35
+ "test/fixtures/leaderboard.jpg",
36
+ "test/fixtures/leaderboard.swf",
37
+ "test/fixtures/skyscraper.gif",
38
+ "test/fixtures/skyscraper.png",
34
39
  "test/image_spec_test.rb"
35
40
  ]
36
41
  s.homepage = %q{http://github.com/dim/ruby-imagespec}
@@ -0,0 +1 @@
1
+ ABCDEFGHIJKLMNOPQRSTUVWXYZ
Binary file
Binary file
Binary file
Binary file
@@ -1,8 +1,37 @@
1
+ $: << File.join(File.dirname(__FILE__),'..', 'lib')
2
+
1
3
  require 'test/unit'
4
+ require 'image_spec'
2
5
 
3
6
  class ImageSpecTest < Test::Unit::TestCase
4
7
  # Replace this with your real tests.
5
- def test_this_plugin
6
- flunk
8
+
9
+ def fixture(name)
10
+ File.open File.expand_path("../fixtures/#{name}", __FILE__), 'rb'
11
+ end
12
+
13
+ def assert_spec(values, spec)
14
+ assert_equal values, [spec.width, spec.height, spec.content_type]
15
+ end
16
+
17
+ def test_identifying_jpeg
18
+ assert_spec [728, 90, "image/jpeg"], ImageSpec.new(fixture('leaderboard.jpg'))
19
+ end
20
+
21
+ def test_identifying_gif
22
+ assert_spec [120, 600, "image/gif"], ImageSpec.new(fixture('skyscraper.gif'))
7
23
  end
24
+
25
+ def test_identifying_png
26
+ assert_spec [120, 600, "image/png"], ImageSpec.new(fixture('skyscraper.png'))
27
+ end
28
+
29
+ def test_identifying_swf
30
+ assert_spec [728, 90, "application/x-shockwave-flash"], ImageSpec.new(fixture('leaderboard.swf'))
31
+ end
32
+
33
+ def test_corrupted_files
34
+ assert_raises(ImageSpec::Error) { ImageSpec.new(fixture('corrupt.jpg')) }
35
+ end
36
+
8
37
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-imagespec
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 1
8
+ - 2
9
9
  - 0
10
- version: 0.1.0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Brandon Anderson
@@ -18,7 +18,7 @@ autorequire:
18
18
  bindir: bin
19
19
  cert_chain: []
20
20
 
21
- date: 2010-08-16 00:00:00 +01:00
21
+ date: 2010-08-26 00:00:00 +01:00
22
22
  default_executable:
23
23
  dependencies: []
24
24
 
@@ -46,6 +46,11 @@ files:
46
46
  - lib/parser/swf.rb
47
47
  - ruby-imagespec.gemspec
48
48
  - tasks/image_spec_tasks.rake
49
+ - test/fixtures/corrupt.jpg
50
+ - test/fixtures/leaderboard.jpg
51
+ - test/fixtures/leaderboard.swf
52
+ - test/fixtures/skyscraper.gif
53
+ - test/fixtures/skyscraper.png
49
54
  - test/image_spec_test.rb
50
55
  has_rdoc: true
51
56
  homepage: http://github.com/dim/ruby-imagespec