magro 0.1.2 → 0.2.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
- SHA1:
3
- metadata.gz: dcc8f3016f64b8b1c8f99fc81bc0e25970b2a027
4
- data.tar.gz: 7a5b1842fd84fe2983b9524eb4dd32194a6ead6e
2
+ SHA256:
3
+ metadata.gz: 4e24a0d3e82248b27f8a652e84ebe382363234a082732af2b1c18a55ea0dc6c3
4
+ data.tar.gz: dc3bc91550338e59455d2ea51effcaf6934b0970c7169434a2cce7422c852252
5
5
  SHA512:
6
- metadata.gz: 673cf64674cf0d5b89141594ec8407b9057df9b8cf1c344b6bf3c0a1040d212a04773c70c3a552277c9b1f4ec5553c681a6724a7f3a68d2d25fa406d6ac98a2e
7
- data.tar.gz: 61fd9e68c424f24534814ce5e5cc48ccde3dc7333a2001b3b5cd14d1562371600ef581a6af1fd91cd5bd607b13d7962d320149deb7c00f5874050b029d45c122
6
+ metadata.gz: 000e7447d4f24a0dd6c97610c5c057ee96feb6eb0698a4e3564e7cec29ce6103a1ac3479f12a90fd1d1436ea89329418097197ad346879cc00018cca25e25aa0
7
+ data.tar.gz: f8a09aad3d5fcadc92e753a453213f8c33e66fbde0522a263afeb02b7a3ceaaf3245c6801f9d61bde41fe7015070391bd3cba8298dc2a1ca275e4ed4e110c60b
data/.travis.yml CHANGED
@@ -1,5 +1,4 @@
1
1
  ---
2
- sudo: true
3
2
  os: linux
4
3
  dist: bionic
5
4
  language: ruby
@@ -8,7 +7,13 @@ rvm:
8
7
  - '2.4'
9
8
  - '2.5'
10
9
  - '2.6'
10
+ - '2.7'
11
+
12
+ addons:
13
+ apt:
14
+ packages:
15
+ - libpng-dev
16
+ - libjpeg-dev
11
17
 
12
18
  before_install:
13
- - sudo apt-get install -y libpng-dev libjpeg-dev
14
- - gem install bundler -v 2.0.2
19
+ - gem install bundler -v 2.1.4
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ # 0.2.0
2
+ - Add [transform module](https://yoshoku.github.io/magro/doc/Magro/Transform.html) and resize method.
3
+ - Fix some configulation files.
4
+
1
5
  # 0.1.2
2
6
  - Fix bug that fails to read and save file with upper case file extension.
3
7
 
data/Gemfile CHANGED
@@ -1,4 +1,9 @@
1
- source "https://rubygems.org"
1
+ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in magro.gemspec
4
4
  gemspec
5
+
6
+ gem 'bundler', '~> 2.0'
7
+ gem 'rake', '~> 12.0'
8
+ gem 'rake-compiler', '~> 1.0'
9
+ gem 'rspec', '~> 3.0'
data/LICENSE.txt CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2019 Atsushi Tatsuma
1
+ Copyright (c) 2019-2020 Atsushi Tatsuma
2
2
  All rights reserved.
3
3
 
4
4
  Redistribution and use in source and binary forms, with or without
data/README.md CHANGED
@@ -10,13 +10,14 @@ Magro uses [Numo::NArray](https://github.com/ruby-numo/numo-narray) arrays as im
10
10
 
11
11
  ## Installation
12
12
 
13
- Magro dependents some external libraries to provides functions loading image file.
13
+ Magro dependents libpng and libjpeg to provides functions loading image file.
14
+ It is recommended that using libpng version 1.6 or later.
14
15
 
15
16
  macOS:
16
17
 
17
18
  $ brew install libpng libjpeg
18
19
 
19
- Ubuntu:
20
+ Ubuntu (bionic):
20
21
 
21
22
  $ sudo apt-get install libpng-dev libjpeg-dev
22
23
 
data/lib/magro.rb CHANGED
@@ -6,3 +6,4 @@ require 'magro/version'
6
6
  require 'magro/magro'
7
7
 
8
8
  require 'magro/io'
9
+ require 'magro/transform'
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Magro
4
+ # Transform module provide functions of image transfom.
5
+ module Transform
6
+ module_function
7
+
8
+ # Resizes an image with bilinear interpolation method.
9
+ #
10
+ # @example
11
+ # require 'numo/narray'
12
+ # require 'magro'
13
+ #
14
+ # image = Numo::UInt8.new(16, 16).seq
15
+ # resized = Magro::Transform.resize(image, height: 64, width: 64)
16
+ #
17
+ # @param image [Numo::UInt8] (shape: [height, width, n_channels] or [height, width]) Image data to be saved.
18
+ # @param height [Integer] Requested height in pixels.
19
+ # @param width [Integer] Requested width in pixels.
20
+ # @return [Numo::UInt8] (shape: [height, width, n_channels] or [height, width]) Resized image data.
21
+ def resize(image, height:, width:)
22
+ n_channels = image.shape[2]
23
+
24
+ if n_channels.nil?
25
+ bilinear_resize(image, height, width)
26
+ else
27
+ resized = image.class.zeros(height, width, n_channels)
28
+ n_channels.times { |c| resized[true, true, c] = bilinear_resize(image[true, true, c], height, width) }
29
+ resized
30
+ end
31
+ end
32
+
33
+ # private
34
+
35
+ def bilinear_resize(image, new_height, new_width)
36
+ height, width = image.shape
37
+
38
+ y_ratio = height.fdiv(new_height)
39
+ x_ratio = width.fdiv(new_width)
40
+
41
+ y, x = Numo::Int32.new(new_height * new_width).seq.divmod(new_width)
42
+
43
+ y_p = Numo::Int32.cast((y_ratio * (y + 0.5) - 0.5).floor).clip(0, height - 1)
44
+ x_p = Numo::Int32.cast((x_ratio * (x + 0.5) - 0.5).floor).clip(0, width - 1)
45
+ y_n = Numo::Int32.cast((y_ratio * (y + 0.5) - 0.5).ceil).clip(0, height - 1)
46
+ x_n = Numo::Int32.cast((x_ratio * (x + 0.5) - 0.5).ceil).clip(0, width - 1)
47
+
48
+ flt = image.flatten
49
+ a = flt[y_p * width + x_p]
50
+ b = flt[y_p * width + x_n]
51
+ c = flt[y_n * width + x_p]
52
+ d = flt[y_n * width + x_n]
53
+
54
+ y_d = y_ratio * (y + 0.5) - 0.5
55
+ x_d = x_ratio * (x + 0.5) - 0.5
56
+ y_d = y_d.class.maximum(0, y_d - y_d.floor)
57
+ x_d = x_d.class.maximum(0, x_d - x_d.floor)
58
+
59
+ resized = a * (1 - x_d) * (1 - y_d) + b * x_d * (1 - y_d) + c * (1 - x_d) * y_d + d * x_d * y_d
60
+
61
+ resized = resized.ceil.clip(image.class::MIN, image.class::MAX) if integer_narray?(image)
62
+ resized = image.class.cast(resized) unless resized.is_a?(image.class)
63
+ resized.reshape(new_height, new_width)
64
+ end
65
+
66
+ INTEGER_NARRAY = %w[Numo::Int8 Numo::Int16 Numo::Int32 Numo::Int64
67
+ Numo::UInt8 Numo::UInt16 Numo::UInt32 Numo::UInt64].freeze
68
+
69
+ private_constant :INTEGER_NARRAY
70
+
71
+ def integer_narray?(image)
72
+ INTEGER_NARRAY.include?(image.class.to_s)
73
+ end
74
+
75
+ private_class_method :bilinear_resize, :integer_narray?
76
+ end
77
+ end
data/lib/magro/version.rb CHANGED
@@ -3,5 +3,5 @@
3
3
  # Magro is an image processing library in Ruby.
4
4
  module Magro
5
5
  # The version of Magro you are using.
6
- VERSION = '0.1.2'
6
+ VERSION = '0.2.0'
7
7
  end
data/magro.gemspec CHANGED
@@ -26,9 +26,4 @@ Gem::Specification.new do |spec|
26
26
  spec.extensions = ['ext/magro/extconf.rb']
27
27
 
28
28
  spec.add_runtime_dependency 'numo-narray', '~> 0.9.1'
29
-
30
- spec.add_development_dependency 'bundler', '~> 2.0'
31
- spec.add_development_dependency 'rake', '~> 10.0'
32
- spec.add_development_dependency 'rake-compiler', '~> 1.0'
33
- spec.add_development_dependency 'rspec', '~> 3.0'
34
29
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: magro
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - yoshoku
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-09-08 00:00:00.000000000 Z
11
+ date: 2020-05-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: numo-narray
@@ -24,62 +24,6 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: 0.9.1
27
- - !ruby/object:Gem::Dependency
28
- name: bundler
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - "~>"
32
- - !ruby/object:Gem::Version
33
- version: '2.0'
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - "~>"
39
- - !ruby/object:Gem::Version
40
- version: '2.0'
41
- - !ruby/object:Gem::Dependency
42
- name: rake
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - "~>"
46
- - !ruby/object:Gem::Version
47
- version: '10.0'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - "~>"
53
- - !ruby/object:Gem::Version
54
- version: '10.0'
55
- - !ruby/object:Gem::Dependency
56
- name: rake-compiler
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - "~>"
60
- - !ruby/object:Gem::Version
61
- version: '1.0'
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - "~>"
67
- - !ruby/object:Gem::Version
68
- version: '1.0'
69
- - !ruby/object:Gem::Dependency
70
- name: rspec
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - "~>"
74
- - !ruby/object:Gem::Version
75
- version: '3.0'
76
- type: :development
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - "~>"
81
- - !ruby/object:Gem::Version
82
- version: '3.0'
83
27
  description: Magro is an image processing library for Ruby.
84
28
  email:
85
29
  - yoshoku@outlook.com
@@ -106,6 +50,7 @@ files:
106
50
  - ext/magro/magro.h
107
51
  - lib/magro.rb
108
52
  - lib/magro/io.rb
53
+ - lib/magro/transform.rb
109
54
  - lib/magro/version.rb
110
55
  - magro.gemspec
111
56
  homepage: https://github.com/yoshoku/magro
@@ -127,8 +72,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
127
72
  - !ruby/object:Gem::Version
128
73
  version: '0'
129
74
  requirements: []
130
- rubyforge_project:
131
- rubygems_version: 2.6.14.4
75
+ rubygems_version: 3.1.2
132
76
  signing_key:
133
77
  specification_version: 4
134
78
  summary: Magro is an image processing library for Ruby.