pix_scale 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6d73a45dd39fc7155e6de8aa14e07b11da3cffb1
4
- data.tar.gz: e7e4111dae8d43d96d7be3304e30b83e2f59c23d
3
+ metadata.gz: 3b969d5d5f7aa5de29c297dbd8e98c5d6a020d68
4
+ data.tar.gz: 9baf4d771fec21f8bab5d0a3e3b48c93fe2ca30e
5
5
  SHA512:
6
- metadata.gz: 76292d7776f8a3b388758369f6b76706a04a83825ac24b1ca557e0b50291f2a29d23c2e06d039fa2298da46ade6d521c44ef02059d9f5b8987baa33c60d3c3b7
7
- data.tar.gz: d3de833422624841bc88e3d69df3666f59ff7901de3fa95fa8dc420f34ccd81df15f20919aa123d9a70a1d0a0be4f8cd5d72ebb9a3fab9b993cd6d053552c08c
6
+ metadata.gz: 8f32fb05f7e477d24b491a57512c5461ac23d44a76f409b61cce943e740c87ce13c617d6a463a9b910826ad47e0347d817c694acbdb5c6fcae64f8d175d416cd
7
+ data.tar.gz: 7a0d320513997a7fec07e4e480ecff59ff9c4f3b29f33cbd167ababbe25ac86b2ffb5f5a627e39740b7a523e15a042eac2c9215cfa337e7efbc0bd1523184ec5
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ notifications:
6
+ recipients:
7
+ - myokoym@gmail.com
data/NEWS.md CHANGED
@@ -1,5 +1,15 @@
1
1
  # NEWS
2
2
 
3
+ ## 0.1.0: 2013-06-29
4
+
5
+ Supported absolute length release!
6
+
7
+ ### Changes
8
+
9
+ * Improvements
10
+ * Supported absolute length for width and height
11
+ * Supported upper case for extension name
12
+
3
13
  ## 0.0.2: 2013-06-27
4
14
 
5
15
  Fix a typo release!
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # PixScale
1
+ # pix\_scale
2
2
 
3
3
  A command line tool for scale of pix.
4
4
 
@@ -11,19 +11,48 @@ A command line tool for scale of pix.
11
11
 
12
12
  gem install pix_scale
13
13
 
14
+ ## Official supported format
15
+
16
+ * PNG
17
+ * JPEG
18
+
14
19
  ## Usage
15
20
 
21
+ ### rate
22
+
23
+ % ls -l
24
+ -rw-rw-r-- 1 you you 46643 2013-06-27 20:00 foo.png
25
+
26
+ % pix_scale foo.png 0.5
27
+
28
+ % ls -l
29
+ -rw-rw-r-- 1 you you 46643 2013-06-27 20:00 foo.png
30
+ -rw-rw-r-- 1 you you 18539 2013-06-27 20:01 foo-0.5.png
31
+
32
+ ### width,height
33
+
34
+ % ls -l
35
+ -rw-rw-r-- 1 you you 46643 2013-06-27 20:00 foo.png
36
+
37
+ % pix_scale foo.png 200,200
38
+
39
+ % ls -l
40
+ -rw-rw-r-- 1 you you 46643 2013-06-27 20:00 foo.png
41
+ -rw-rw-r-- 1 you you 18539 2013-06-27 20:01 foo-200_200.png
42
+
43
+ ### multiple files
44
+
16
45
  % ls -l
17
- -rw-rw-r-- 1 you you 46643 6 27 20:00 foo.png
18
- -rw-rw-r-- 1 you you 31438 6 27 20:00 bar.png
46
+ -rw-rw-r-- 1 you you 46643 2013-06-27 20:00 foo.png
47
+ -rw-rw-r-- 1 you you 31438 2013-06-27 20:00 bar.png
19
48
 
20
49
  % pix_scale *.png 0.5
21
50
 
22
51
  % ls -l
23
- -rw-rw-r-- 1 you you 46643 6 27 20:00 foo.png
24
- -rw-rw-r-- 1 you you 18539 6 27 20:01 foo-0.5.png
25
- -rw-rw-r-- 1 you you 31438 6 27 20:00 bar.png
26
- -rw-rw-r-- 1 you you 16462 6 27 20:01 bar-0.5.png
52
+ -rw-rw-r-- 1 you you 46643 2013-06-27 20:00 foo.png
53
+ -rw-rw-r-- 1 you you 18539 2013-06-27 20:01 foo-0.5.png
54
+ -rw-rw-r-- 1 you you 31438 2013-06-27 20:00 bar.png
55
+ -rw-rw-r-- 1 you you 16462 2013-06-27 20:01 bar-0.5.png
27
56
 
28
57
  ## Contributing
29
58
 
data/Rakefile CHANGED
@@ -1 +1,9 @@
1
1
  require "bundler/gem_tasks"
2
+
3
+ desc "Run test"
4
+ task :test do
5
+ Bundler::GemHelper.install_tasks
6
+ ruby("test/run-test.rb")
7
+ end
8
+
9
+ task :default => :test
@@ -1,4 +1,4 @@
1
- require "gdk_pixbuf2"
1
+ require "pix_scale/pic"
2
2
  require "pix_scale/version"
3
3
 
4
4
  module PixScale
@@ -15,19 +15,10 @@ Example: pix_scale foo.png 0.5
15
15
  exit(true)
16
16
  end
17
17
 
18
- scale = arguments.pop.to_f
18
+ scale = arguments.pop
19
19
 
20
20
  arguments.each do |pic_path|
21
- dirname = File.dirname(pic_path)
22
- basename = File.basename(pic_path, ".*")
23
- extname = File.extname(pic_path).sub(/^\./, "")
24
-
25
- pic = Gdk::Pixbuf.new(pic_path)
26
- scaled_pic = pic.scale(pic.width * scale, pic.height * scale)
27
-
28
- output_path = "#{dirname}/#{basename}-#{scale.to_s}.#{extname}"
29
- type = (/\Ajpg\z/ =~ extname) ? "jpeg" : extname
30
- scaled_pic.save(output_path, type)
21
+ Pic.scale_and_save(pic_path, scale)
31
22
  end
32
23
  end
33
24
  end
@@ -0,0 +1,68 @@
1
+ require "gdk_pixbuf2"
2
+
3
+ module PixScale
4
+ class Pic
5
+ def self.scale_and_save(path, scale)
6
+ new(path).scale_and_save(scale)
7
+ end
8
+
9
+ attr_reader :path, :type
10
+ def initialize(path)
11
+ @path = path
12
+ @pic = Gdk::Pixbuf.new(path)
13
+ @type = (/\A\.jpg\z/i =~ extname) ? "jpeg" : extname.sub(/^\./, "").downcase
14
+ end
15
+
16
+ def scale_and_save(scale)
17
+ scale_string = scale.to_s.sub(/[^\.0-9]/, "_")
18
+ output_path = "#{dirname}/#{basename}-#{scale_string}#{extname}"
19
+ scale(scale).save(output_path)
20
+ end
21
+
22
+ def scale(scale)
23
+ new_pic = self.dup
24
+ new_pic.scale!(scale)
25
+ new_pic
26
+ end
27
+
28
+ def scale!(scale)
29
+ if scale.is_a?(Float)
30
+ width = @pic.width * scale
31
+ height = @pic.height * scale
32
+ elsif scale.is_a?(Integer)
33
+ width = scale
34
+ height = @pic.height * (scale.to_f / @pic.width)
35
+ elsif /\A[0-9]+\.[0-9]+\z/ =~ scale
36
+ width = @pic.width * scale.to_f
37
+ height = @pic.height * scale.to_f
38
+ elsif /\A[0-9]+[^\.0-9][0-9]+\z/ =~ scale
39
+ width, height = scale.split(/[^\.0-9]/).map(&:to_i)
40
+ elsif /\A[0-9]+\z/ =~ scale
41
+ width = scale.to_i
42
+ height = @pic.height * (scale.to_f / @pic.width)
43
+ elsif /\A[^\.0-9]([0-9]+)\z/ =~ scale
44
+ width = @pic.width * ($1.to_f / @pic.height)
45
+ height = $1.to_i
46
+ end
47
+
48
+ @pic = @pic.scale(width, height)
49
+ end
50
+
51
+ def save(output_path)
52
+ @pic.save(output_path, @type)
53
+ end
54
+
55
+ private
56
+ def dirname
57
+ File.dirname(@path)
58
+ end
59
+
60
+ def basename
61
+ File.basename(@path, ".*")
62
+ end
63
+
64
+ def extname
65
+ File.extname(@path)
66
+ end
67
+ end
68
+ end
@@ -1,3 +1,3 @@
1
1
  module PixScale
2
- VERSION = "0.0.2"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/pix_scale.rb CHANGED
@@ -1 +1,2 @@
1
1
  require "pix_scale/version"
2
+ require "pix_scale/pic"
data/pix_scale.gemspec CHANGED
@@ -20,6 +20,9 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_runtime_dependency("gdk_pixbuf2")
22
22
 
23
+ spec.add_development_dependency("test-unit")
24
+ spec.add_development_dependency("test-unit-notify")
25
+ spec.add_development_dependency("test-unit-rr")
23
26
  spec.add_development_dependency("bundler", "~> 1.3")
24
27
  spec.add_development_dependency("rake")
25
28
  end
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
data/test/run-test.rb ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "test-unit"
4
+ require "test/unit/notify"
5
+ require "test/unit/rr"
6
+
7
+ base_dir = File.expand_path(File.join(File.dirname(__FILE__), ".."))
8
+ $LOAD_PATH.unshift(File.join(base_dir, "lib"))
9
+ $LOAD_PATH.unshift(File.join(base_dir, "test"))
10
+
11
+ exit Test::Unit::AutoRunner.run(true)
data/test/test-pic.rb ADDED
@@ -0,0 +1,127 @@
1
+ require "pix_scale/pic"
2
+
3
+ class PicTest < Test::Unit::TestCase
4
+ def test_static_scale_and_save
5
+ path = "test/fixtures/nijip.png"
6
+ scaled_path = "test/fixtures/nijip-0.5.png"
7
+ File.delete(scaled_path) if File.exist?(scaled_path)
8
+ PixScale::Pic.scale_and_save(path, 0.5)
9
+ assert_true(File.file?(scaled_path))
10
+ assert_true(File.size(path) > File.size(scaled_path))
11
+ assert_true(File.size(scaled_path) > 0)
12
+ File.delete(scaled_path)
13
+ end
14
+
15
+ class ScaleAndSaveTest < self
16
+ def test_scale_and_save_rate
17
+ path = "test/fixtures/nijip.png"
18
+ scale = 0.5
19
+ scaled_path = "test/fixtures/nijip-0.5.png"
20
+ assert_scale_and_save(path, scale, scaled_path)
21
+ end
22
+
23
+ def test_scale_and_save_width_and_height
24
+ path = "test/fixtures/nijip.png"
25
+ scale = "240,180"
26
+ scaled_path = "test/fixtures/nijip-240_180.png"
27
+ assert_scale_and_save(path, scale, scaled_path)
28
+ end
29
+
30
+ def test_scale_and_save_upcase_PNG
31
+ path = "test/fixtures/nyanko.PNG"
32
+ scale = 0.66
33
+ scaled_path = "test/fixtures/nyanko-0.66.PNG"
34
+ assert_scale_and_save(path, scale, scaled_path)
35
+ end
36
+
37
+ def test_scale_and_save_jpg
38
+ path = "test/fixtures/teabreak.jpg"
39
+ scale = 0.09
40
+ scaled_path = "test/fixtures/teabreak-0.09.jpg"
41
+ assert_scale_and_save(path, scale, scaled_path)
42
+ end
43
+
44
+ def test_scale_and_save_jpeg
45
+ path = "test/fixtures/teabreak.jpeg"
46
+ scale = 0.09
47
+ scaled_path = "test/fixtures/teabreak-0.09.jpeg"
48
+ assert_scale_and_save(path, scale, scaled_path)
49
+ end
50
+
51
+ def test_scale_and_save_upcase_JPG
52
+ path = "test/fixtures/teabreak.JPG"
53
+ scale = 0.09
54
+ scaled_path = "test/fixtures/teabreak-0.09.JPG"
55
+ assert_scale_and_save(path, scale, scaled_path)
56
+ end
57
+
58
+ def test_scale_and_save_upcase_JPEG
59
+ path = "test/fixtures/teabreak.JPEG"
60
+ scale = 0.09
61
+ scaled_path = "test/fixtures/teabreak-0.09.JPEG"
62
+ assert_scale_and_save(path, scale, scaled_path)
63
+ end
64
+
65
+ private
66
+ def assert_scale_and_save(path, scale, scaled_path)
67
+ File.delete(scaled_path) if File.exist?(scaled_path)
68
+ pic = PixScale::Pic.new(path)
69
+ pic.scale_and_save(scale)
70
+ assert_true(File.file?(scaled_path))
71
+ assert_true(File.size(path) > File.size(scaled_path))
72
+ assert_true(File.size(scaled_path) > 0)
73
+ File.delete(scaled_path)
74
+ end
75
+ end
76
+
77
+ class ScaleTest < self
78
+ def setup
79
+ @path = "test/fixtures/nijip.png"
80
+ @pic = PixScale::Pic.new(@path)
81
+ @before_pic = @pic.instance_variable_get(:@pic).dup
82
+ end
83
+
84
+ def test_float
85
+ scale = 0.5
86
+ scaled_pic = @pic.scale(scale).instance_variable_get(:@pic)
87
+ assert_equal(@before_pic.width * scale, scaled_pic.width)
88
+ assert_equal(@before_pic.height * scale, scaled_pic.height)
89
+ end
90
+
91
+ def test_string_like_float
92
+ scale = "0.5"
93
+ scaled_pic = @pic.scale(scale).instance_variable_get(:@pic)
94
+ assert_equal(@before_pic.width * scale.to_f, scaled_pic.width)
95
+ assert_equal(@before_pic.height * scale.to_f, scaled_pic.height)
96
+ end
97
+
98
+ def test_integer_as_width
99
+ width = 240
100
+ scaled_pic = @pic.scale(width).instance_variable_get(:@pic)
101
+ assert_equal(width, scaled_pic.width)
102
+ assert_equal(@before_pic.height * (width.to_f / @before_pic.width), scaled_pic.height)
103
+ end
104
+
105
+ def test_string_as_width
106
+ width = "240"
107
+ scaled_pic = @pic.scale(width).instance_variable_get(:@pic)
108
+ assert_equal(width.to_i, scaled_pic.width)
109
+ assert_equal(@before_pic.height * (width.to_f / @before_pic.width), scaled_pic.height)
110
+ end
111
+
112
+ def test_string_as_height
113
+ height = "180"
114
+ scaled_pic = @pic.scale(",#{height}").instance_variable_get(:@pic)
115
+ assert_equal(@before_pic.width * (height.to_f / @before_pic.height), scaled_pic.width)
116
+ assert_equal(height.to_i, scaled_pic.height)
117
+ end
118
+
119
+ def test_string_width_and_height
120
+ width = 240
121
+ height = 180
122
+ scaled_pic = @pic.scale("#{width},#{height}").instance_variable_get(:@pic)
123
+ assert_equal(width, scaled_pic.width)
124
+ assert_equal(height, scaled_pic.height)
125
+ end
126
+ end
127
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pix_scale
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Masafumi Yokoyama
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-06-27 00:00:00.000000000 Z
11
+ date: 2013-06-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: gdk_pixbuf2
@@ -24,6 +24,48 @@ dependencies:
24
24
  - - '>='
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: test-unit
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: test-unit-notify
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: test-unit-rr
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
27
69
  - !ruby/object:Gem::Dependency
28
70
  name: bundler
29
71
  requirement: !ruby/object:Gem::Requirement
@@ -61,6 +103,7 @@ extensions: []
61
103
  extra_rdoc_files: []
62
104
  files:
63
105
  - .gitignore
106
+ - .travis.yml
64
107
  - Gemfile
65
108
  - LICENSE.txt
66
109
  - NEWS.md
@@ -69,8 +112,17 @@ files:
69
112
  - bin/pix_scale
70
113
  - lib/pix_scale.rb
71
114
  - lib/pix_scale/command.rb
115
+ - lib/pix_scale/pic.rb
72
116
  - lib/pix_scale/version.rb
73
117
  - pix_scale.gemspec
118
+ - test/fixtures/nijip.png
119
+ - test/fixtures/nyanko.PNG
120
+ - test/fixtures/teabreak.JPEG
121
+ - test/fixtures/teabreak.JPG
122
+ - test/fixtures/teabreak.jpeg
123
+ - test/fixtures/teabreak.jpg
124
+ - test/run-test.rb
125
+ - test/test-pic.rb
74
126
  homepage: https://github.com/myokoym/pix_scale
75
127
  licenses:
76
128
  - MIT
@@ -95,5 +147,13 @@ rubygems_version: 2.0.3
95
147
  signing_key:
96
148
  specification_version: 4
97
149
  summary: A command line tool for scale of pix.
98
- test_files: []
150
+ test_files:
151
+ - test/fixtures/nijip.png
152
+ - test/fixtures/nyanko.PNG
153
+ - test/fixtures/teabreak.JPEG
154
+ - test/fixtures/teabreak.JPG
155
+ - test/fixtures/teabreak.jpeg
156
+ - test/fixtures/teabreak.jpg
157
+ - test/run-test.rb
158
+ - test/test-pic.rb
99
159
  has_rdoc: