colorpress 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4029097c4dd27546952ad3d3a951379fbb393ff5666f80948b28737fd7c43eaf
4
+ data.tar.gz: 2fcb97f0274c7e65a425a34faf123ab8e7a53151f097141f132c2734febc9892
5
+ SHA512:
6
+ metadata.gz: 0b22f563cec3b89010a9b1d5dd09d0b77b97486f852d03dc3dae8a330445d6318e7f54d4df216ba4423f50aaf97d61fe5d2b8b5f8b4488de5779a824670f4249
7
+ data.tar.gz: f8c3016b99fd5759542a55d31707536ef6b744d33327a4f9a9c05d0a8d1e40428970c59b80482f79ae1fff4ae0aaaf073b5a413578b6d170da58aaab3dbbff61
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /.idea
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in colorpress.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 Luc Street
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.
@@ -0,0 +1,30 @@
1
+ # colorpress
2
+
3
+ ## Usage
4
+
5
+ Encode bytes of a file to PNG format:
6
+ ```ruby
7
+ Colorpress::encode('file.txt') # creates file.txt.png, minimum width 50 px
8
+ Colorpress::encode('file.txt', 'out.png', min_width=100) # creates out.png, minimum width 100 px
9
+ ```
10
+ Decode bytes of a PNG back into character data:
11
+ ```ruby
12
+ Colorpress::decode('file.txt.png') # creates file.txt, deletes file.txt.png
13
+ Colorpress::decode('file.txt.png', cleanup=false) # creates file.txt, keeps file.txt.png
14
+ Colorpress::decode('out.png', 'original.txt') # creates original.txt, deletes out.png
15
+ ```
16
+ Documentation for all methods can be generated by running `yard doc` in a development environment.
17
+
18
+ ## Development
19
+
20
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
21
+
22
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
23
+
24
+ ## Contributing
25
+
26
+ Bug reports and pull requests are welcome on GitHub at https://github.com/lucis-fluxum/colorpress. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org/) code of conduct.
27
+
28
+ ## License
29
+
30
+ This gem is available as open source under the terms of the MIT License.
@@ -0,0 +1,9 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.test_files = FileList['test/**/*_test.rb']
6
+ end
7
+ desc 'Run tests'
8
+
9
+ task default: :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'colorpress'
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
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'colorpress/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'colorpress'
8
+ spec.version = Colorpress::VERSION
9
+ spec.authors = ['Luc Street']
10
+
11
+ spec.summary = 'Visualize arbitrary files as PNG images'
12
+ spec.homepage = 'https://github.com/lucis-fluxum/colorpress'
13
+ spec.license = 'MIT'
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
+ spec.bindir = 'bin'
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.add_development_dependency 'bundler', '~> 2.1.2'
21
+ spec.add_development_dependency 'rake', '~> 13.0.1'
22
+ spec.add_development_dependency 'yard', '~> 0.9.24'
23
+ spec.add_development_dependency 'minitest', '~> 5.14.0'
24
+
25
+ spec.add_dependency 'chunky_png', '~> 1.3.11'
26
+ end
@@ -0,0 +1,97 @@
1
+ require 'chunky_png'
2
+ require 'colorpress/version'
3
+
4
+ module Colorpress
5
+ # Encode a file to PNG format.
6
+ #
7
+ # @param file_name [String] the name of the file to encode
8
+ # @param output_name [String, nil] the optional name of the output file
9
+ # @param min_width [Integer] the minimum width of the image, in pixels
10
+ def self.encode(file_name, output_name=nil, min_width=50)
11
+ file_size = File.stat(file_name).size
12
+ # Increase width until it divides number of bytes evenly
13
+ while file_size % min_width != 0
14
+ min_width += 1
15
+ end
16
+ image = ChunkyPNG::Image.new(min_width, file_size/min_width)
17
+
18
+ y = 0
19
+ read_bytes(file_name, min_width) do |buffer|
20
+ unpacked_data = buffer.unpack('C*')
21
+ unpacked_data.each_with_index do |value, index|
22
+ # Randomly pick between red/green/blue
23
+ rgb = [0, 0, 0]
24
+ rgb[(rand * 3).to_i] = value
25
+ image[index, y] = ChunkyPNG::Color.rgb(*rgb)
26
+ end
27
+ y += 1
28
+ end
29
+
30
+ image.save(output_name.nil? ? file_name + '.png' : output_name)
31
+ end
32
+
33
+ # Decode a file from PNG format.
34
+ #
35
+ # @param image_name [String] the name of the image to decode, ex: 'filename.txt.png'
36
+ # @param output_name [String, nil] the name of the output file (no extension needed), ex: 'my_file'
37
+ # @param cleanup [Boolean] whether to delete PNG file after decoding
38
+ def self.decode(image_name, output_name=nil, cleanup=true)
39
+ output_name = image_name[0...-4] unless output_name # Remove .png extension
40
+ image = ChunkyPNG::Image.from_file(image_name)
41
+
42
+ write_data(output_name, '', true) # Make sure the output file is empty
43
+
44
+ bytes = []
45
+ (0..image.dimension.height-1).each do |y|
46
+ (0..image.dimension.width-1).each do |x|
47
+ # It's either red, green, or blue, so add them all
48
+ bytes << ChunkyPNG::Color.r(image[x, y]) +
49
+ ChunkyPNG::Color.g(image[x, y]) +
50
+ ChunkyPNG::Color.b(image[x, y])
51
+ end
52
+ write_data(output_name, bytes)
53
+ bytes = []
54
+ end
55
+
56
+ write_data(output_name, bytes) if bytes.any? # Write if any bytes still in array
57
+
58
+ File.delete(image_name) if cleanup
59
+ end
60
+
61
+ private
62
+
63
+ # Reads bytes from a file into a buffer, yielding the buffer to a given block.
64
+ #
65
+ # @param filename [String] the name of the file to read
66
+ # @param buffer_size[Integer, nil]
67
+ # amount of bytes to read into the buffer at a time
68
+ #
69
+ # @example Reading all of a file
70
+ # other_bytes = ''
71
+ # Colorpress::FileIO.read_bytes('filename.txt', 20) do |buffer|
72
+ # other_bytes += buffer
73
+ # end
74
+ # # other_bytes now contains all bytes of the file
75
+ def self.read_bytes(filename, buffer_size=20)
76
+ File.open(filename, 'rb') do |file|
77
+ until file.eof? do
78
+ buffer = file.read(buffer_size)
79
+ yield buffer
80
+ end
81
+ end
82
+ end
83
+
84
+ # Writes the given data to the given filename. Truncates the file if desired.
85
+ #
86
+ # @param filename [String] the name of the file to write
87
+ # @param data [String, Array] the data to write to the file,
88
+ # it may be a string or an array of bytes
89
+ # @param truncate [Boolean] whether or not to empty the file before writing
90
+ def self.write_data(filename, data, truncate=false)
91
+ mode = truncate ? 'wb' : 'ab'
92
+ File.open(filename, mode) do |file|
93
+ data = data.pack('C*') if data.is_a? Array
94
+ file.write(data)
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,3 @@
1
+ module Colorpress
2
+ VERSION = '2.0.0'
3
+ end
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: colorpress
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Luc Street
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-03-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: 2.1.2
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.1.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 13.0.1
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 13.0.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: yard
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.9.24
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.9.24
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 5.14.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 5.14.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: chunky_png
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 1.3.11
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 1.3.11
83
+ description:
84
+ email:
85
+ executables:
86
+ - console
87
+ - setup
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - CODE_OF_CONDUCT.md
93
+ - Gemfile
94
+ - LICENSE
95
+ - README.md
96
+ - Rakefile
97
+ - bin/console
98
+ - bin/setup
99
+ - colorpress.gemspec
100
+ - lib/colorpress.rb
101
+ - lib/colorpress/version.rb
102
+ homepage: https://github.com/lucis-fluxum/colorpress
103
+ licenses:
104
+ - MIT
105
+ metadata: {}
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubygems_version: 3.1.2
122
+ signing_key:
123
+ specification_version: 4
124
+ summary: Visualize arbitrary files as PNG images
125
+ test_files: []