rocrad 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm --create use 1.9.2@rocrad
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ gem "rake", "0.8.7"
7
+
8
+ # Add dependencies to develop your gem here.
9
+ # Include everything needed to run rake, tests, features, etc.
10
+ group :development do
11
+ gem "bundler", "~> 1.0.0"
12
+ gem "jeweler", "~> 1.6.0"
13
+ gem "rcov", ">= 0"
14
+ gem "test-unit", "2.3.0"
15
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,21 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ git (1.2.5)
5
+ jeweler (1.6.0)
6
+ bundler (~> 1.0.0)
7
+ git (>= 1.2.5)
8
+ rake
9
+ rake (0.8.7)
10
+ rcov (0.9.9)
11
+ test-unit (2.3.0)
12
+
13
+ PLATFORMS
14
+ ruby
15
+
16
+ DEPENDENCIES
17
+ bundler (~> 1.0.0)
18
+ jeweler (~> 1.6.0)
19
+ rake (= 0.8.7)
20
+ rcov
21
+ test-unit (= 2.3.0)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Ludwig Bratke
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = rocrad
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to rocrad
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
+ * Fork the project
10
+ * Start a feature/bugfix branch
11
+ * Commit and push until you are happy with your contribution
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2011 Ludwig Bratke. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "rocrad"
18
+ gem.homepage = "http://github.com/EeCnee/rocrad"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Ruby library for working with Ocrad - The GNU OCR}
21
+ gem.description = %Q{Ruby library for working with Ocrad - The GNU OCR}
22
+ gem.email = "EeCnee1@netscape.net"
23
+ gem.authors = ["Ludwig Bratke"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rake/testtask'
29
+ Rake::TestTask.new(:test) do |test|
30
+ test.libs << 'lib' << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+
35
+ require 'rcov/rcovtask'
36
+ Rcov::RcovTask.new do |test|
37
+ test.libs << 'test'
38
+ test.pattern = 'test/**/test_*.rb'
39
+ test.verbose = true
40
+ test.rcov_opts << '--exclude "gems/*"'
41
+ end
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "rocrad #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,6 @@
1
+ class Rocrad
2
+ class ConversionError < StandardError ;end
3
+ class ImageNotSelectedError < StandardError ;end
4
+ class TempFilesNotRemovedError < StandardError ;end
5
+ class UnsupportedFileTypeError < StandardError ;end
6
+ end
data/lib/rocrad.rb ADDED
@@ -0,0 +1,99 @@
1
+ require "pathname"
2
+ require "tempfile"
3
+
4
+ require "rocrad/errors"
5
+
6
+ class Rocrad
7
+
8
+ attr_accessor :options
9
+
10
+ def initialize(src="", options={})
11
+ @uid = options.delete(:uid) || nil
12
+ @source = Pathname.new src
13
+ @clear_console_output = options.delete(:clear_console_output)
14
+ @clear_console_output = true if @clear_console_output.nil?
15
+ @value = ""
16
+ end
17
+
18
+ def source= src
19
+ @value = ""
20
+ @source = Pathname.new src
21
+ end
22
+
23
+ #Remove files
24
+ def remove_file(files=[])
25
+ files.each do |file|
26
+ begin
27
+ File.unlink(file) if File.exist?(file)
28
+ rescue
29
+ system "rm -f #{file}"
30
+ end
31
+ end
32
+ true
33
+ rescue
34
+ raise Rocrad::TempFilesNotRemovedError
35
+ end
36
+
37
+ def generate_uid
38
+ @uid = rand.to_s[2, 10] if @uid.nil?
39
+ @uid
40
+ end
41
+
42
+ #Convert image to pnm
43
+ def image_to_pnm
44
+ generate_uid
45
+ tmp_file = Pathname.new(Dir::tmpdir).join("#{@uid}_#{@source.sub_ext(".pnm").basename}")
46
+ redirection = "#{@source} > #{tmp_file} #{clear_console_output}"
47
+ case @source.extname
48
+ when ".jpg" then
49
+ `djpeg -greyscale -pnm #{redirection}`
50
+ when ".tif" then
51
+ `tifftopnm #{redirection}`
52
+ when ".png" then
53
+ `pngtopnm #{redirection}`
54
+ when ".bmp" then
55
+ `bmptopnm #{redirection}`
56
+ else
57
+ raise UnsupportedFileTypeError
58
+ end
59
+ tmp_file
60
+ end
61
+
62
+ #Convert image to string
63
+ def convert
64
+ generate_uid
65
+ tmp_file = Pathname.new(Dir::tmpdir).join("#{@uid}_#{@source.sub_ext(".txt").basename}")
66
+ tmp_image = image_to_pnm
67
+ begin
68
+ `gocr #{tmp_image} -o #{tmp_file} #{clear_console_output}`
69
+ @value = File.read(tmp_file)
70
+ @uid = nil
71
+ remove_file([tmp_image, tmp_file])
72
+ rescue
73
+ raise ConversionError
74
+ end
75
+ end
76
+
77
+ #TODO: Clear console for MacOS or Windows
78
+ def clear_console_output
79
+ return "" unless @clear_console_output
80
+ "2>/dev/null" if File.exist?("/dev/null") #Linux console clear
81
+ end
82
+
83
+ #Output value
84
+ def to_s
85
+ return @value if @value != ""
86
+ if @source.file?
87
+ convert
88
+ @value
89
+ else
90
+ raise ImageNotSelectedError
91
+ end
92
+ end
93
+
94
+ #Remove spaces and break-lines
95
+ def to_s_without_spaces
96
+ to_s.gsub(" ", "").gsub("\n", "").gsub("\r", "").encode("US-ASCII")
97
+ end
98
+
99
+ end
data/rocrad.gemspec ADDED
@@ -0,0 +1,71 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{rocrad}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Ludwig Bratke"]
12
+ s.date = %q{2011-05-25}
13
+ s.description = %q{Ruby library for working with Ocrad - The GNU OCR}
14
+ s.email = %q{EeCnee1@netscape.net}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rvmrc",
22
+ "Gemfile",
23
+ "Gemfile.lock",
24
+ "LICENSE.txt",
25
+ "README.rdoc",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "lib/rocrad.rb",
29
+ "lib/rocrad/errors.rb",
30
+ "rocrad.gemspec",
31
+ "test/helper.rb",
32
+ "test/images/mixed.tif",
33
+ "test/images/test.bmp",
34
+ "test/images/test.foo",
35
+ "test/images/test.jpg",
36
+ "test/images/test.png",
37
+ "test/images/test.tif",
38
+ "test/images/test1.tif",
39
+ "test/test_rocrad.rb"
40
+ ]
41
+ s.homepage = %q{http://github.com/EeCnee/rocrad}
42
+ s.licenses = ["MIT"]
43
+ s.require_paths = ["lib"]
44
+ s.rubygems_version = %q{1.7.2}
45
+ s.summary = %q{Ruby library for working with Ocrad - The GNU OCR}
46
+
47
+ if s.respond_to? :specification_version then
48
+ s.specification_version = 3
49
+
50
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
51
+ s.add_runtime_dependency(%q<rake>, ["= 0.8.7"])
52
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
53
+ s.add_development_dependency(%q<jeweler>, ["~> 1.6.0"])
54
+ s.add_development_dependency(%q<rcov>, [">= 0"])
55
+ s.add_development_dependency(%q<test-unit>, ["= 2.3.0"])
56
+ else
57
+ s.add_dependency(%q<rake>, ["= 0.8.7"])
58
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
59
+ s.add_dependency(%q<jeweler>, ["~> 1.6.0"])
60
+ s.add_dependency(%q<rcov>, [">= 0"])
61
+ s.add_dependency(%q<test-unit>, ["= 2.3.0"])
62
+ end
63
+ else
64
+ s.add_dependency(%q<rake>, ["= 0.8.7"])
65
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
66
+ s.add_dependency(%q<jeweler>, ["~> 1.6.0"])
67
+ s.add_dependency(%q<rcov>, [">= 0"])
68
+ s.add_dependency(%q<test-unit>, ["= 2.3.0"])
69
+ end
70
+ end
71
+
data/test/helper.rb ADDED
@@ -0,0 +1,17 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+
12
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ require 'rocrad'
15
+
16
+ class Test::Unit::TestCase
17
+ end
Binary file
Binary file
@@ -0,0 +1 @@
1
+ file with invalid extension
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1,73 @@
1
+ begin
2
+ require "helper"
3
+ rescue LoadError
4
+ require File.dirname(__FILE__) + '/helper'
5
+ end
6
+
7
+ class TestRocrad < Test::Unit::TestCase
8
+
9
+ def setup
10
+ @path = Pathname.new(__FILE__.gsub("test_rocrad.rb", "")).expand_path
11
+ @image_jpg = @path.join("images", "test.jpg").to_s
12
+ end
13
+
14
+ def test_be_instantiable
15
+ assert_equal Rocrad, Rocrad.new.class
16
+ assert_equal Rocrad, Rocrad.new("").class
17
+ assert_equal Rocrad, Rocrad.new(@image_jpg).class
18
+ end
19
+
20
+ def test_translate_image_to_text
21
+ assert_equal "3R8Z", Rocrad.new(@image_jpg).to_s_without_spaces
22
+ assert_equal "43ZZ", Rocrad.new(@path.join("images", "test.tif").to_s).to_s_without_spaces
23
+ end
24
+
25
+ def test_unsupported_file_type_error
26
+ assert_raise Rocrad::UnsupportedFileTypeError do
27
+ Rocrad.new(@path.join("images", "test.foo").to_s).to_s_without_spaces
28
+ end
29
+ end
30
+
31
+ def test_image_not_selected_error
32
+ assert_raise Rocrad::ImageNotSelectedError do
33
+ Rocrad.new(@path.join("images", "test.noo").to_s).to_s_without_spaces
34
+ end
35
+ end
36
+
37
+ def test_translate_images_png_jpg
38
+ assert_equal "HW9W", Rocrad.new(@path.join("images", "test.png").to_s).to_s_without_spaces
39
+ assert_equal "3R8Z", Rocrad.new(@path.join("images", "test.jpg").to_s).to_s_without_spaces
40
+ end
41
+
42
+ def test_translate_images_bmp
43
+ assert_equal "ZLA6", Rocrad.new(@path.join("images", "test.bmp").to_s).to_s_without_spaces
44
+ end
45
+
46
+ def test_translate_mixed_tif
47
+ assert_equal "43ZZ", Rocrad.new(@path.join("images", "mixed.tif").to_s).to_s_without_spaces
48
+ end
49
+
50
+ def test_translate_test1_tif
51
+ assert_equal "V2V4", Rocrad.new(@path.join("images", "test1.tif").to_s).to_s_without_spaces
52
+ end
53
+
54
+ def test_change_the_image
55
+ image = Rocrad.new(@image_jpg)
56
+ assert_equal "3R8Z", image.to_s_without_spaces
57
+ image.source = @path.join("images", "test.tif").to_s
58
+ assert_equal "43ZZ", image.to_s_without_spaces
59
+ end
60
+
61
+ def test_unique_uid
62
+ assert_not_equal Rocrad.new(@image_jpg).generate_uid, Rocrad.new(@image_jpg).generate_uid
63
+ end
64
+
65
+ def test_generate_a_unique id
66
+ reg = Rocrad.new(@image_jpg)
67
+ assert_equal reg.generate_uid, reg.generate_uid
68
+ value = reg.generate_uid
69
+ reg.convert
70
+ assert_not_equal value, reg.generate_uid
71
+ end
72
+
73
+ end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rocrad
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - Ludwig Bratke
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-05-25 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rake
17
+ requirement: &id001 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - "="
21
+ - !ruby/object:Gem::Version
22
+ version: 0.8.7
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: bundler
28
+ requirement: &id002 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 1.0.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: *id002
37
+ - !ruby/object:Gem::Dependency
38
+ name: jeweler
39
+ requirement: &id003 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ version: 1.6.0
45
+ type: :development
46
+ prerelease: false
47
+ version_requirements: *id003
48
+ - !ruby/object:Gem::Dependency
49
+ name: rcov
50
+ requirement: &id004 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ type: :development
57
+ prerelease: false
58
+ version_requirements: *id004
59
+ - !ruby/object:Gem::Dependency
60
+ name: test-unit
61
+ requirement: &id005 !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - "="
65
+ - !ruby/object:Gem::Version
66
+ version: 2.3.0
67
+ type: :development
68
+ prerelease: false
69
+ version_requirements: *id005
70
+ description: Ruby library for working with Ocrad - The GNU OCR
71
+ email: EeCnee1@netscape.net
72
+ executables: []
73
+
74
+ extensions: []
75
+
76
+ extra_rdoc_files:
77
+ - LICENSE.txt
78
+ - README.rdoc
79
+ files:
80
+ - .document
81
+ - .rvmrc
82
+ - Gemfile
83
+ - Gemfile.lock
84
+ - LICENSE.txt
85
+ - README.rdoc
86
+ - Rakefile
87
+ - VERSION
88
+ - lib/rocrad.rb
89
+ - lib/rocrad/errors.rb
90
+ - rocrad.gemspec
91
+ - test/helper.rb
92
+ - test/images/mixed.tif
93
+ - test/images/test.bmp
94
+ - test/images/test.foo
95
+ - test/images/test.jpg
96
+ - test/images/test.png
97
+ - test/images/test.tif
98
+ - test/images/test1.tif
99
+ - test/test_rocrad.rb
100
+ homepage: http://github.com/EeCnee/rocrad
101
+ licenses:
102
+ - MIT
103
+ post_install_message:
104
+ rdoc_options: []
105
+
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ none: false
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ hash: 2868053043622808966
114
+ segments:
115
+ - 0
116
+ version: "0"
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ none: false
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: "0"
123
+ requirements: []
124
+
125
+ rubyforge_project:
126
+ rubygems_version: 1.7.2
127
+ signing_key:
128
+ specification_version: 3
129
+ summary: Ruby library for working with Ocrad - The GNU OCR
130
+ test_files: []
131
+