cuterb 0.1.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 016aeb88be1f56f01e4083e2d395b72fcf5ecb8894c9f801eba918bbc8abd520
4
+ data.tar.gz: e0b596e08a8abece9c32b032ef90b651582cc696c5d9b37a4460e27126a185d7
5
+ SHA512:
6
+ metadata.gz: 2e03434376382e3c9a8b2f916845e46572222e2f15683e45215c83fd21740d18c29e1b2b8e15b975cd5c49924b0ad4c4bff4334e1f3a5f3cb84d3994a9c5851f
7
+ data.tar.gz: c91e3bf27d89a5f8d795382e84aa5dfda72d3e7e6c440cb539aa88a31de159fd45b7d9c57679c7bf1458defc0d1aa16c54ef658d9c9d7e3fe8f7e7488e613210
data/.gitignore ADDED
@@ -0,0 +1,58 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ # Used by dotenv library to load environment variables.
14
+ # .env
15
+
16
+ ## Specific to RubyMotion:
17
+ .dat*
18
+ .repl_history
19
+ build/
20
+ *.bridgesupport
21
+ build-iPhoneOS/
22
+ build-iPhoneSimulator/
23
+
24
+ ## Specific to RubyMotion (use of CocoaPods):
25
+ #
26
+ # We recommend against adding the Pods directory to your .gitignore. However
27
+ # you should judge for yourself, the pros and cons are mentioned at:
28
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
29
+ #
30
+ # vendor/Pods/
31
+
32
+ ## Documentation cache and generated files:
33
+ /.yardoc/
34
+ /_yardoc/
35
+ /doc/
36
+ /rdoc/
37
+
38
+ ## Environment normalization:
39
+ /.bundle/
40
+ /vendor/bundle
41
+ /lib/bundler/man/
42
+
43
+ # for a library or gem, you might want to ignore these files since the code is
44
+ # intended to run in multiple environments; otherwise, check them in:
45
+ # Gemfile.lock
46
+ # .ruby-version
47
+ # .ruby-gemset
48
+
49
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
50
+ .rvmrc
51
+
52
+ .ruby-version
53
+ /.bundle/
54
+ /.yardoc
55
+ /_yardoc/
56
+ /doc/
57
+
58
+ .DS_Store
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in cuterb.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,26 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ cuterb (0.1.0)
5
+ rmagick
6
+ rqrcode
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ chunky_png (1.3.11)
12
+ rake (10.5.0)
13
+ rmagick (2.16.0)
14
+ rqrcode (0.10.1)
15
+ chunky_png (~> 1.0)
16
+
17
+ PLATFORMS
18
+ ruby
19
+
20
+ DEPENDENCIES
21
+ bundler (~> 2.0)
22
+ cuterb!
23
+ rake (~> 10.0)
24
+
25
+ BUNDLED WITH
26
+ 2.0.1
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019 Yusuke Morikoshi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,12 @@
1
+ # CuteRB
2
+ ```
3
+ $ cuterb
4
+ cuterb TEXT INPUT_IMAGE_FILE OUTPUT_IMAGE_FILE
5
+
6
+ $ cuterb "hogehoge" sample/image.png output/qr.png
7
+ ```
8
+
9
+ ## Install
10
+ ```
11
+ $ gem instlal cuterb
12
+ ```
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "cuterb"
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(__FILE__)
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
data/cuterb.gemspec ADDED
@@ -0,0 +1,44 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "cuterb/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "cuterb"
8
+ spec.version = CuteRB::VERSION
9
+ spec.authors = ["lrks"]
10
+ spec.email = ["lrks@5v-gnd.net"]
11
+
12
+ spec.summary = %q{Inspired by cuteR and qart.js}
13
+ spec.description = %q{Generate an artistic QR codes.}
14
+ spec.homepage = "https://5v-gnd.net/"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ #spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
21
+
22
+ spec.metadata["homepage_uri"] = spec.homepage
23
+ spec.metadata["source_code_uri"] = "https://github.com/lrks/cuteRB"
24
+ #spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
25
+ else
26
+ raise "RubyGems 2.0 or newer is required to protect against " \
27
+ "public gem pushes."
28
+ end
29
+
30
+ # Specify which files should be added to the gem when it is released.
31
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
32
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
33
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
34
+ end
35
+ spec.bindir = "exe"
36
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
37
+ spec.require_paths = ["lib"]
38
+
39
+ spec.add_development_dependency "bundler", "~> 2.0"
40
+ spec.add_development_dependency "rake", "~> 10.0"
41
+
42
+ spec.add_runtime_dependency "rqrcode"
43
+ spec.add_runtime_dependency "rmagick"
44
+ end
data/exe/cuterb ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ require "cuterb"
3
+
4
+ if ARGV.length != 3
5
+ puts "#{$0} TEXT INPUT_IMAGE_FILE OUTPUT_IMAGE_FILE"
6
+ exit 1
7
+ end
8
+
9
+ c = CuteRB::CLI.new(ARGV[0], ARGV[1], ARGV[2])
10
+ exit c.run()
data/lib/cuterb.rb ADDED
@@ -0,0 +1,78 @@
1
+ require 'cuterb/version'
2
+ require 'rqrcode'
3
+ require_relative './utils.rb'
4
+ require 'rmagick'
5
+
6
+ module CuteRB
7
+ class Error < StandardError; end
8
+
9
+ class CLI
10
+ def initialize(text, image, output)
11
+ @qr = RQRCode::QRCode.new(text, :level => :h)
12
+
13
+ @image = Magick::ImageList.new(image).first
14
+ @image = @image.crop(0, 0, [@image.columns, @image.rows].min, [@image.columns, @image.rows].min)
15
+ raise "#{image} too small." if @image.columns < @qr.module_count
16
+ @image = @image.quantize(256, Magick::GRAYColorspace)
17
+ bg = Magick::Image.new(@image.columns, @image.columns) { self.background_color = "white" }
18
+ @image = bg.composite(@image, 0, 0, Magick::OverCompositeOp).normalize.contrast(true).contrast(true)
19
+
20
+ @output = output
21
+ end
22
+
23
+ def run()
24
+ pixel = qrpixel()
25
+ overlay(pixel)
26
+ @image.write(@output)
27
+ return 0
28
+ end
29
+
30
+
31
+ private
32
+ def qrpixel()
33
+ data = @qr.to_s(:dark => 'b', :light => 'w').delete("\n").chars
34
+ Utils.place_position_probe_pattern(data, 0, 0)
35
+ Utils.place_position_probe_pattern(data, @qr.module_count - 7, 0)
36
+ Utils.place_position_probe_pattern(data, 0, @qr.module_count - 7)
37
+ Utils.place_position_adjust_pattern(data, @qr.version)
38
+ return data
39
+ end
40
+
41
+ def overlay(pixel, size=0.2, black_threshold=75, white_threshold=185)
42
+ scale = @image.columns / @qr.module_count.to_f
43
+ offset = (1 - size) * 0.5 * scale
44
+ size = size * scale
45
+
46
+ for row in 0...@qr.module_count
47
+ for col in 0...@qr.module_count
48
+ cell = pixel[row * @qr.module_count + col]
49
+
50
+ x = col * scale
51
+ y = row * scale
52
+ if cell.downcase == cell
53
+ x1 = (x + offset).round
54
+ y1 = (y + offset).round
55
+ xy = size.ceil
56
+
57
+ mean = @image.export_pixels(x1, y1, xy, xy, 'R').map{ |px| px/257.0 }.sum(0.0) / (xy * xy)
58
+ next if ((cell == 'b' && mean < black_threshold) || (cell == 'w' && mean > white_threshold))
59
+ else
60
+ x1 = x.round
61
+ y1 = y.round
62
+ xy = scale.ceil
63
+ end
64
+
65
+ draw = Magick::Draw.new
66
+ draw.fill((cell.downcase == 'w') ? '#ffffff' : '#000000')
67
+ draw.rectangle(x1, y1, x1+xy, y1+xy)
68
+ draw.draw(@image)
69
+ end
70
+ end
71
+
72
+ quiet = 4 * scale
73
+ width = (@image.columns + quiet * 2).ceil
74
+ bg = Magick::Image.new(width, width) { self.background_color = "white" }
75
+ @image = bg.composite(@image, quiet.round, quiet.round, Magick::OverCompositeOp)
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,3 @@
1
+ module CuteRB
2
+ VERSION = "0.1.0"
3
+ end
data/lib/utils.rb ADDED
@@ -0,0 +1,131 @@
1
+ module CuteRB
2
+ class Utils
3
+ # rqrcode: /lib/rqrcode/qrcode/qr_util.rb L16 -- L58
4
+ #--
5
+ # Copyright 2004 by Duncan Robertson (duncan@whomwah.com).
6
+ # All rights reserved.
7
+
8
+ # Permission is granted for use, copying, modification, distribution,
9
+ # and distribution of modified versions of this work as long as the
10
+ # above copyright notice is included.
11
+ #++
12
+ PATTERN_POSITION_TABLE = [
13
+ [],
14
+ [6, 18],
15
+ [6, 22],
16
+ [6, 26],
17
+ [6, 30],
18
+ [6, 34],
19
+ [6, 22, 38],
20
+ [6, 24, 42],
21
+ [6, 26, 46],
22
+ [6, 28, 50],
23
+ [6, 30, 54],
24
+ [6, 32, 58],
25
+ [6, 34, 62],
26
+ [6, 26, 46, 66],
27
+ [6, 26, 48, 70],
28
+ [6, 26, 50, 74],
29
+ [6, 30, 54, 78],
30
+ [6, 30, 56, 82],
31
+ [6, 30, 58, 86],
32
+ [6, 34, 62, 90],
33
+ [6, 28, 50, 72, 94],
34
+ [6, 26, 50, 74, 98],
35
+ [6, 30, 54, 78, 102],
36
+ [6, 28, 54, 80, 106],
37
+ [6, 32, 58, 84, 110],
38
+ [6, 30, 58, 86, 114],
39
+ [6, 34, 62, 90, 118],
40
+ [6, 26, 50, 74, 98, 122],
41
+ [6, 30, 54, 78, 102, 126],
42
+ [6, 26, 52, 78, 104, 130],
43
+ [6, 30, 56, 82, 108, 134],
44
+ [6, 34, 60, 86, 112, 138],
45
+ [6, 30, 58, 86, 114, 142],
46
+ [6, 34, 62, 90, 118, 146],
47
+ [6, 30, 54, 78, 102, 126, 150],
48
+ [6, 24, 50, 76, 102, 128, 154],
49
+ [6, 28, 54, 80, 106, 132, 158],
50
+ [6, 32, 58, 84, 110, 136, 162],
51
+ [6, 26, 54, 82, 110, 138, 166],
52
+ [6, 30, 58, 86, 114, 142, 170]
53
+ ]
54
+
55
+ # rqrcode: lib/rqrcode/qrcode/qr_code.rb L346 -- L361
56
+ #--
57
+ # Copyright 2008 by Duncan Robertson (duncan@whomwah.com).
58
+ # All rights reserved.
59
+
60
+ # Permission is granted for use, copying, modification, distribution,
61
+ # and distribution of modified versions of this work as long as the
62
+ # above copyright notice is included.
63
+ #++
64
+ def self.place_position_probe_pattern(dst, row, col)
65
+ length = Math.sqrt(dst.length).to_i
66
+ (-1..7).each do |r|
67
+ next if !(row + r).between?(0, length - 1)
68
+
69
+ (-1..7).each do |c|
70
+ next if !(col + c).between?(0, length - 1)
71
+
72
+ is_vert_line = (r.between?(0, 6) && (c == 0 || c == 6))
73
+ is_horiz_line = (c.between?(0, 6) && (r == 0 || r == 6))
74
+ is_square = r.between?(2,4) && c.between?(2, 4)
75
+ y = row + r
76
+ x = col + c
77
+ dst[y * length + x] = (is_vert_line || is_horiz_line || is_square) ? 'B' : 'W'
78
+ end
79
+ end
80
+ end
81
+
82
+ # rqrcode: lib/rqrcode/qrcode/qr_code.rb L388 -- L403
83
+ #--
84
+ # Copyright 2008 by Duncan Robertson (duncan@whomwah.com).
85
+ # All rights reserved.
86
+
87
+ # Permission is granted for use, copying, modification, distribution,
88
+ # and distribution of modified versions of this work as long as the
89
+ # above copyright notice is included.
90
+ #++
91
+ def self.place_position_adjust_pattern(dst, version)
92
+ length = Math.sqrt(dst.length).to_i
93
+ positions = PATTERN_POSITION_TABLE[version - 1]
94
+
95
+ positions.each do |row|
96
+ positions.each do |col|
97
+ next if dst[row * length + col] == dst[row * length + col].upcase
98
+
99
+ ( -2..2 ).each do |r|
100
+ ( -2..2 ).each do |c|
101
+ y = row + r
102
+ x = col + c
103
+ dst[y * length + x] = (r.abs == 2 || c.abs == 2 || (r == 0 && c == 0)) ? 'B' : 'W'
104
+ end
105
+ end
106
+ end
107
+ end
108
+ end
109
+
110
+ def self.dump(data)
111
+ length = Math.sqrt(data.length).to_i
112
+ for row in 0...length
113
+ for col in 0...length
114
+ case data[row * length + col]
115
+ when 'b'
116
+ print "\e[47m \e[m"
117
+ when 'w'
118
+ print "\e[40m \e[m"
119
+ when 'B'
120
+ print "\e[30m\e[47m--\e[m\e[m"
121
+ when 'W'
122
+ print "\e[37m\e[40m--\e[m\e[m"
123
+ else
124
+ p data[row * length + col]
125
+ end
126
+ end
127
+ puts ""
128
+ end
129
+ end
130
+ end
131
+ end
data/sample/puhaa.png ADDED
Binary file
Binary file
Binary file
Binary file
data/sample/snnn.png ADDED
Binary file
Binary file
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cuterb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - lrks
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-01-27 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: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
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: rqrcode
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
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: rmagick
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Generate an artistic QR codes.
70
+ email:
71
+ - lrks@5v-gnd.net
72
+ executables:
73
+ - cuterb
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - Gemfile
79
+ - Gemfile.lock
80
+ - LICENSE
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - cuterb.gemspec
86
+ - exe/cuterb
87
+ - lib/cuterb.rb
88
+ - lib/cuterb/version.rb
89
+ - lib/utils.rb
90
+ - sample/puhaa.png
91
+ - sample/puhaa_qr.png
92
+ - sample/sakenomi.png
93
+ - sample/sakenomi_qr.png
94
+ - sample/snnn.png
95
+ - sample/snnn_qr.png
96
+ homepage: https://5v-gnd.net/
97
+ licenses:
98
+ - MIT
99
+ metadata:
100
+ homepage_uri: https://5v-gnd.net/
101
+ source_code_uri: https://github.com/lrks/cuteRB
102
+ post_install_message:
103
+ rdoc_options: []
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ requirements: []
117
+ rubyforge_project:
118
+ rubygems_version: 2.7.6
119
+ signing_key:
120
+ specification_version: 4
121
+ summary: Inspired by cuteR and qart.js
122
+ test_files: []