ruby-blocky-image-viewer 0.0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4d17d43d37caf9bae678f673125af0f4acdca1c9
4
+ data.tar.gz: 421852c56b8eb8d435f2e3fad145b6a23931a2f3
5
+ SHA512:
6
+ metadata.gz: ba651941adb56651c9f5aac7b28f7ae599688551823e6148f3cdc4f643684e6b5bb2d3c3ec609f0b6b4bbe6ae069f6dfbd2e141352d15ba9e0ff1be36f9a9a52
7
+ data.tar.gz: ce54e7c2f1362b4a2c304dc8ca3cd3bc45e441aa06736202589dc4da8fd2e28e820ab13e5be8b8dac6019e43acafd5e08fa0bc4e6dd07386dd54b4ebc86fd8a7
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+ gem 'rubocop', require: false
3
+
4
+ # Specify your gem's dependencies in ruby-blocky-image-viewer.gemspec
5
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Joseph Hallett
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,73 @@
1
+ # The Ruby Blocky Image Viewer
2
+
3
+ **tl;dr: terminal image displayer that’s a shameless re-write of [icat](https://github.com/atextor/icat) for Ruby**
4
+
5
+ ![tldr](https://raw.githubusercontent.com/bogwonch/ruby-blocky-image-viewer/master/images/tldr.png)
6
+
7
+ ## Summary
8
+
9
+ So you want to see an image, but don’t wanna leave your precious command-line?
10
+
11
+ `jp2a` makes you sad...
12
+
13
+ ![jp2a](https://raw.githubusercontent.com/bogwonch/ruby-blocky-image-viewer/master/images/jp2a.png)
14
+
15
+ `img2txt` is just weird...
16
+
17
+ ![img2txt](https://raw.githubusercontent.com/bogwonch/ruby-blocky-image-viewer/master/images/img2txt.png)
18
+
19
+ There is a better way!
20
+
21
+ ![biv](https://raw.githubusercontent.com/bogwonch/ruby-blocky-image-viewer/master/images/biv-cat.png)
22
+
23
+ ![biv](https://raw.githubusercontent.com/bogwonch/ruby-blocky-image-viewer/master/images/biv.png)
24
+
25
+ ## Features
26
+
27
+ * True color support!
28
+ * Higher definition output (2 pixels per character!)
29
+ * Standard definition output (1 pixel per character!)
30
+ * Width and height controls!
31
+
32
+ ## Installation
33
+
34
+ Add this line to your application's Gemfile:
35
+
36
+ ```ruby
37
+ gem 'ruby-blocky-image-viewer'
38
+ ```
39
+
40
+ And then execute:
41
+
42
+ ```sh
43
+ $ bundle
44
+ ```
45
+
46
+ Or install it yourself as:
47
+
48
+ ```sh
49
+ $ gem install ruby-blocky-image-viewer
50
+ ```
51
+
52
+ ## Usage
53
+
54
+ If you just wanna view an image checkout:
55
+
56
+ ```sh
57
+ $ biv --help
58
+ ```
59
+
60
+ Otherwise have a look at the source code, especially `Biv::Viewer`:
61
+
62
+ ```ruby
63
+ puts Biv::Viewer.new(“my-image.png”).to_s
64
+ ```
65
+
66
+
67
+ ## Contributing
68
+
69
+ 1. Fork it ( https://github.com/bogwonch/ruby-blocky-image-viewer/fork )
70
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
71
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
72
+ 4. Push to the branch (`git push origin my-new-feature`)
73
+ 5. Create a new Pull Request
@@ -0,0 +1,4 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rubocop/rake_task'
3
+
4
+ RuboCop::RakeTask.new
data/bin/biv ADDED
@@ -0,0 +1,39 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ require 'biv'
4
+ require 'terminfo'
5
+ require 'trollop'
6
+
7
+ opts = Trollop::options do
8
+ version "biv #{Biv::VERSION}"
9
+ banner <<-EOS
10
+ Print images on the commandline.
11
+
12
+ Usage:
13
+ biv [options] <filenames>+
14
+ EOS
15
+
16
+ opt :width, 'Maximum image width', type: :integer, default: TermInfo.screen_size[1]
17
+ opt :height, 'Maximum image height', type: :integer
18
+ opt :fit, 'Fit entire image to terminal'
19
+ opt :true_color, 'True color'
20
+ opt :sd, 'Lower definition images'
21
+ opt :background, 'Background color of the image', type: :string, default: "FFFFFF"
22
+ end
23
+
24
+ bg = opts[:background].to_i(16)
25
+ bg = [(bg && 0xFF0000) >> 16,
26
+ (bg && 0x00FF00) >> 8,
27
+ (bg && 0x0000FF)]
28
+
29
+ opts[:height] = TermInfo.screen_size[0] if opts[:fit]
30
+
31
+ ARGV.each do |arg|
32
+ puts Biv::Image.new(arg,
33
+ width: opts[:width],
34
+ height: opts[:height],
35
+ true_color: opts[:true_color],
36
+ hd: ! opts[:sd],
37
+ bg: bg).to_s
38
+
39
+ end
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1,180 @@
1
+ require 'biv/version'
2
+ require 'RMagick'
3
+ require 'rainbow/ext/string'
4
+
5
+ # Implements a blocky image viewer
6
+ module Biv
7
+ # An image to be printed in a blockily
8
+ class Image
9
+ # Load a new image for viewing
10
+ def initialize(file, width: nil, height: nil, true_color: false, hd: true, bg: [0xFF, 0xFF, 0xFF])
11
+ fail ArgumentError, 'Missing image width' if width.nil?
12
+
13
+ begin
14
+ @original_image = Magick::Image.read(file).first
15
+ rescue
16
+ raise ArgumentError, "I don't know how to open '#{file}'"
17
+ end
18
+ @width = width
19
+ @height = height
20
+ @true_color = true_color
21
+ @hd = hd
22
+ @bg = bg
23
+
24
+ @height *= 2 if !@height.nil? && @hd
25
+ preprocess
26
+
27
+ fail ArgumentError, 'I failed to load the image' if @image.nil?
28
+ end
29
+
30
+ # Convert an image to an array of printable strings
31
+ def to_a
32
+ buffer = []
33
+
34
+ for_row_indexes.each do |y|
35
+ line = ''
36
+ 0.upto(@image.columns - 1).each do |x|
37
+ p = get_pixel(x, y)
38
+ block = make_block p
39
+ line << block
40
+ end
41
+ line << "\x1b[0m" # Reset the color at the end of the line
42
+ buffer << line
43
+ end
44
+
45
+ buffer
46
+ end
47
+
48
+ # Convert an image to a string
49
+ def to_s
50
+ to_a.join "\n"
51
+ end
52
+
53
+ private
54
+
55
+ # Get the image ready to display
56
+ def preprocess
57
+ @image = @original_image
58
+ @image.scale! @image.columns, @image.rows / 2 unless @hd
59
+
60
+ if @height.nil?
61
+ @image = @image.resize_to_fit(@width)
62
+ else
63
+ @height /= 2 unless @hd
64
+ @image = @image.resize_to_fit(@width, @height)
65
+ end
66
+ end
67
+
68
+ # Get a pixel at a coordinate
69
+ def get_pixel(x, y)
70
+ if @hd
71
+ get_pixel_hd(x, y)
72
+ else
73
+ get_pixel_at(x, y)
74
+ end
75
+ end
76
+
77
+ # Get two pixels from an HD coordinate
78
+ def get_pixel_hd(x, y)
79
+ p0 = get_pixel_at(x, y * 2)
80
+ p1 = get_pixel_at(x, y * 2 + 1)
81
+
82
+ [p0, p1]
83
+ end
84
+
85
+ # Get a pixel at a specific coordinate
86
+ def get_pixel_at(x, y)
87
+ begin
88
+ p = get_rgb(@image.get_pixels(x, y, 1, 1).first)
89
+ rescue RangeError
90
+ p = @bg
91
+ end
92
+ p
93
+ end
94
+
95
+ # From a pixel get the rgb channels, taking into account transparency
96
+ def get_rgb(p)
97
+ r = p.red
98
+ g = p.green
99
+ b = p.blue
100
+ a = p.opacity
101
+
102
+ if a > 0
103
+ r *= (Magick::QuantumRange - a) / Magick::QuantumRange
104
+ g *= (Magick::QuantumRange - a) / Magick::QuantumRange
105
+ b *= (Magick::QuantumRange - a) / Magick::QuantumRange
106
+ end
107
+
108
+ [r, g, b]
109
+ end
110
+
111
+ # Convert rgb channels into 8bit color
112
+ def to_8bit(rgb)
113
+ rgb = rgb.map do |c|
114
+ c >>= 8
115
+ if c < 0x00
116
+ 0
117
+ elsif c > 0xFF
118
+ 0xFF
119
+ else
120
+ c
121
+ end
122
+ end
123
+ rgb
124
+ end
125
+
126
+ # create a color string from a channel array
127
+ def color_str(str, p)
128
+ format(str, p[0], p[1], p[2])
129
+ end
130
+
131
+ # A hex color string (for rainbow)
132
+ def hex(p)
133
+ color_str('%02x%02x%02x', to_8bit(p))
134
+ end
135
+
136
+ # A truecolor background colorstring
137
+ def true_bg(p)
138
+ color_str("\x1b[48;2;%d;%d;%dm", to_8bit(p))
139
+ end
140
+
141
+ # A truecolor foreground colorstring
142
+ def true_fg(p)
143
+ color_str("\x1b[38;2;%d;%d;%dm", to_8bit(p))
144
+ end
145
+
146
+ # Get row indexes
147
+ def for_row_indexes
148
+ if @hd
149
+ 0.upto((@image.rows - 1) / 2)
150
+ else
151
+ 0.upto(@image.rows - 1)
152
+ end
153
+ end
154
+
155
+ def make_block(p)
156
+ if @hd
157
+ make_block_hd(p)
158
+ else
159
+ if @true_color
160
+ block = true_bg(p) + ' '
161
+ else
162
+ block = ' '.background Rainbow::Color.parse_hex_color(hex(p))
163
+ end
164
+ block
165
+ end
166
+ end
167
+
168
+ def make_block_hd(p)
169
+ p0, p1 = p
170
+ if @true_color
171
+ block = true_bg(p0) + true_fg(p1) + '▄'
172
+ else
173
+ block = '▄'
174
+ .background(Rainbow::Color.parse_hex_color(hex p0))
175
+ .foreground(Rainbow::Color.parse_hex_color(hex p1))
176
+ end
177
+ block
178
+ end
179
+ end
180
+ end
@@ -0,0 +1,4 @@
1
+ # Version number
2
+ module Biv
3
+ VERSION = '0.0.1'
4
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'biv/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'ruby-blocky-image-viewer'
8
+ spec.version = Biv::VERSION
9
+ spec.authors = ['Joseph Hallett']
10
+ spec.email = ['bogwonch@bogwonch.net']
11
+ spec.summary = 'Display images on the commandline. Badly.'
12
+ spec.description = 'Display images on the commandline using blocks of color with true color support'
13
+ spec.homepage = 'https://github.com/bogwonch/ruby-blocky-image-viewer'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.7'
22
+ spec.add_development_dependency 'rake', '~> 10.0'
23
+
24
+ spec.add_runtime_dependency 'rmagick', '~> 2.15', '>= 2.15.3'
25
+ spec.add_runtime_dependency 'ruby-terminfo', '~> 0.1.1'
26
+ spec.add_runtime_dependency 'trollop', '~> 2.1', '>= 2.1.2'
27
+ end
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-blocky-image-viewer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Joseph Hallett
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-08-10 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.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
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: rmagick
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.15'
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: 2.15.3
51
+ type: :runtime
52
+ prerelease: false
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - "~>"
56
+ - !ruby/object:Gem::Version
57
+ version: '2.15'
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: 2.15.3
61
+ - !ruby/object:Gem::Dependency
62
+ name: ruby-terminfo
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: 0.1.1
68
+ type: :runtime
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: 0.1.1
75
+ - !ruby/object:Gem::Dependency
76
+ name: trollop
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '2.1'
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: 2.1.2
85
+ type: :runtime
86
+ prerelease: false
87
+ version_requirements: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - "~>"
90
+ - !ruby/object:Gem::Version
91
+ version: '2.1'
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: 2.1.2
95
+ description: Display images on the commandline using blocks of color with true color
96
+ support
97
+ email:
98
+ - bogwonch@bogwonch.net
99
+ executables:
100
+ - biv
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - Gemfile
106
+ - LICENSE.txt
107
+ - README.md
108
+ - Rakefile
109
+ - bin/biv
110
+ - images/biv-cat.png
111
+ - images/biv.png
112
+ - images/img2txt.png
113
+ - images/jp2a.png
114
+ - images/tldr.png
115
+ - lib/biv.rb
116
+ - lib/biv/version.rb
117
+ - ruby-blocky-image-viewer.gemspec
118
+ homepage: https://github.com/bogwonch/ruby-blocky-image-viewer
119
+ licenses:
120
+ - MIT
121
+ metadata: {}
122
+ post_install_message:
123
+ rdoc_options: []
124
+ require_paths:
125
+ - lib
126
+ required_ruby_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ required_rubygems_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ requirements: []
137
+ rubyforge_project:
138
+ rubygems_version: 2.4.3
139
+ signing_key:
140
+ specification_version: 4
141
+ summary: Display images on the commandline. Badly.
142
+ test_files: []