animated_gif_detector 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6617cd4c8a65784f425fd843ddc2fdb4b412d301
4
+ data.tar.gz: 4382a87a00ffa9a519f527151c9d540f5561dc53
5
+ SHA512:
6
+ metadata.gz: 1fb8c50418b3e149e6c44c6cf3d4efd322b85150f528b8b757a0289d5abffab62eb4791c44ef6ca63b0cea648306d25b793ca6f36cbbcd0a15874c00483f32b9
7
+ data.tar.gz: d94c821f84c7462d8342c58df64f63f9ecfcd0e78583bee55d019f3a6da94b8261584e34ebba9fe923dcc8cf094f7da15218d1309e2621474a51428657ddd06d
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.3
5
+ before_install: gem install bundler -v 1.13.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in animated_gif_detector.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,48 @@
1
+ [![Build Status](https://travis-ci.org/bfolkens/ruby-animated-gif-detector.svg?branch=master)](https://travis-ci.org/bfolkens/ruby-animated-gif-detector)
2
+
3
+ # AnimatedGifDetector
4
+
5
+ A simple utility to determine if a GIF is animated, and how many frames it contains. Only 2 frames are needed to determine if the GIF is "animated", so the user has the option to specify an option to return immediately or keep counting frames. Designed for use on a stream to avoid unnecessary memory usage.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'animated_gif_detector'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install animated_gif_detector
22
+
23
+ ## Usage
24
+
25
+ One-liner:
26
+
27
+ ```ruby
28
+ AnimatedGifDetector.new(File.open('your_image.gif', 'rb')).animated?
29
+ ```
30
+
31
+ Customizable:
32
+
33
+ ```ruby
34
+ io = File.open('your_image.gif', 'rb')
35
+ detector = AnimatedGifDetector.new(io, terminate_after: false)
36
+ detector.animated? # true
37
+ detector.frames # 3
38
+ ```
39
+
40
+ ## Development
41
+
42
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
43
+
44
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
45
+
46
+ ## Contributing
47
+
48
+ Bug reports and pull requests are welcome on GitHub at https://github.com/bfolkens/animated_gif_detector.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'animated_gif_detector/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "animated_gif_detector"
8
+ spec.version = AnimatedGifDetector::VERSION
9
+ spec.authors = ["Bradford Folkens"]
10
+ spec.email = ["bfolkens@gmail.com"]
11
+
12
+ spec.summary = %q{Animated GIF detection in pure ruby}
13
+ spec.description = %q{Reads a stream and determines if it's an animated GIF}
14
+ spec.homepage = "https://github.com/bfolkens/ruby-animated-gif-detector"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.13"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec", "~> 3.0"
26
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "animated_gif_detector"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,55 @@
1
+ require "animated_gif_detector/version"
2
+
3
+ class AnimatedGifDetector
4
+ class UnrecognizedFileFormatException < Exception; end
5
+ class EOFWithoutFrameException < Exception; end
6
+
7
+ # http://www.matthewflickinger.com/lab/whatsinagif/animation_and_transparency.asp
8
+
9
+ attr_reader :frames
10
+
11
+ BLOCK_TERMINATOR = 0x00.chr
12
+ GRAPHIC_CONTROL_EXTENSION_MAGIC = 0x21.chr + 0xF9.chr
13
+ BLOCK_SIZE = (BLOCK_TERMINATOR + GRAPHIC_CONTROL_EXTENSION_MAGIC).size
14
+
15
+ def initialize(io, options = {})
16
+ @options = { buffer_size: 1024, terminate_after: true }.merge(options)
17
+ @stream = io
18
+ @frames = 0
19
+ end
20
+
21
+ def animated?
22
+ raise UnrecognizedFileFormatException unless @stream.read(3) == 'GIF'
23
+
24
+ lookback = ''
25
+ animated = false
26
+ while data = @stream.read(@options[:buffer_size])
27
+ @frames += count_substring_matches(lookback + data, BLOCK_TERMINATOR + GRAPHIC_CONTROL_EXTENSION_MAGIC)
28
+ animated = @frames > 1
29
+
30
+ return true if @options[:terminate_after] and animated
31
+
32
+ lookback = last_characters(data, BLOCK_SIZE - 1)
33
+ end
34
+
35
+ raise EOFWithoutFrameException if @frames == 0
36
+ return animated
37
+ end
38
+
39
+ private
40
+
41
+ def count_substring_matches(str, match)
42
+ last_pos = 0
43
+ count = 0
44
+ while pos = str.index(match, last_pos)
45
+ count += 1
46
+ last_pos = pos + match.length
47
+ end
48
+
49
+ count
50
+ end
51
+
52
+ def last_characters(str, n)
53
+ str[-n..-1] || ''
54
+ end
55
+ end
@@ -0,0 +1,3 @@
1
+ class AnimatedGifDetector
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: animated_gif_detector
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Bradford Folkens
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-12-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: Reads a stream and determines if it's an animated GIF
56
+ email:
57
+ - bfolkens@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - README.md
67
+ - Rakefile
68
+ - animated_gif_detector.gemspec
69
+ - bin/console
70
+ - bin/setup
71
+ - lib/animated_gif_detector.rb
72
+ - lib/animated_gif_detector/version.rb
73
+ homepage: https://github.com/bfolkens/ruby-animated-gif-detector
74
+ licenses: []
75
+ metadata: {}
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 2.4.5.1
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: Animated GIF detection in pure ruby
96
+ test_files: []