pallete_png 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 77d085a91c6dff3c308277348b3e7378f4304589
4
+ data.tar.gz: 3ad138ed12a50b9b1b84f9f7aaefe8acb7d0eb64
5
+ SHA512:
6
+ metadata.gz: 3be4632c1339a4c0f5aee41a891c0882f3459c55e705a8d789de826743262aa3af0508682bb0f7334ef5720f1e2be1551a2220b5134f42fd7a9ceebe404dbec7
7
+ data.tar.gz: f9fb6b87f051ca04be25969d4feb21395af86cc0487482c398b6975d8507a2c9672377377c43f1b59294db5b7098a820454789ccf5f2f9aa523cc60e419bfd9f
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ /.bundle/
2
+ /Gemfile.lock
data/.travis.yml ADDED
@@ -0,0 +1,13 @@
1
+ language: ruby
2
+
3
+ sudo: false
4
+
5
+ rvm:
6
+ - 2.2.4
7
+ - 2.3.0
8
+
9
+ script: bundle exec rake test
10
+
11
+ branches:
12
+ only:
13
+ - master
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/MIT-LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Katsuya HIDAKA
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # PalletePNG
2
+
3
+ [![Build Status](https://travis-ci.org/hidakatsuya/pallete_png.svg?branch=master)](https://travis-ci.org/hidakatsuya/pallete_png)
4
+
5
+ PalletePNG is a pure-ruby library that makes a pallete-based PNG transparency.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'pallete_png'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install pallete_png
22
+
23
+ ## Usage
24
+
25
+ ```ruby
26
+ require 'pallete_png'
27
+
28
+ image_data = File.binread('/path/to/pallete-based-w-transparency.png')
29
+
30
+ PalletePNG.to_transparency(image_data) # => The converted image data
31
+ ```
32
+
33
+ ## Supported versions
34
+
35
+ Ruby 2.2, 2.3
36
+
37
+ ## Contributing
38
+
39
+ Bug reports and pull requests are welcome on GitHub at https://github.com/hidakatsuya/transparency_png.
40
+
41
+ ## License
42
+
43
+ © 2016 Katsuya HIDAKA. See MIT-LICENSE for further details.
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << 'test'
6
+ t.libs << 'lib'
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task default: :test
@@ -0,0 +1,35 @@
1
+ require 'chunky_png'
2
+
3
+ module PalletePNG
4
+ class Image
5
+ NotPNG = Class.new StandardError
6
+
7
+ # @return [ChunkyPNG::Image]
8
+ attr_reader :image
9
+
10
+ # @param [String] image_data The PNG image data
11
+ # @raise [ChunkyPNG::SignatureMismatch] if image_data is not a PNG image
12
+ def initialize(image_data)
13
+ @image = ChunkyPNG::Image.from_blob(image_data)
14
+ end
15
+
16
+ # @return [String] The new image data that the color_mode is converted to TRUE COLOR alpha
17
+ def to_truecolor_alpha
18
+ image.to_blob color_mode: ChunkyPNG::COLOR_TRUECOLOR_ALPHA
19
+ end
20
+
21
+ def pallete_based?
22
+ color_mode, _depth = image.palette.best_color_settings
23
+ color_mode == ChunkyPNG::COLOR_INDEXED
24
+ end
25
+
26
+ def transparency_chunk?
27
+ !datastream.transparency_chunk.nil?
28
+ end
29
+
30
+ # @return [ChunkyPNG::Datastream]
31
+ def datastream
32
+ @datastream ||= image.to_datastream
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,3 @@
1
+ module PalletePNG
2
+ VERSION = '1.0.0'
3
+ end
@@ -0,0 +1,22 @@
1
+ require 'pallete_png/version'
2
+ require 'pallete_png/image'
3
+
4
+ module PalletePNG
5
+ # @param [String] image_data The PNG image data
6
+ # @return [String] The PNG image data
7
+ # @raise [PalletePNG::Image::NotPNG] if image_data is not a PNG image
8
+ # @example
9
+ # image_data = PalletePNG.to_transparency File.binread('pallete-based-w-transparency.png')
10
+ def self.to_transparency(image_data)
11
+ begin
12
+ image = Image.new image_data
13
+ rescue ChunkyPNG::SignatureMismatch
14
+ raise Image::NotPNG
15
+ end
16
+
17
+ # Returns the raw image data if image is not a pallete-based PNG with transparency
18
+ return image_data unless image.pallete_based? && image.transparency_chunk?
19
+
20
+ image.to_truecolor_alpha
21
+ end
22
+ end
@@ -0,0 +1,28 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $:.unshift(lib) unless $:.include?(lib)
3
+ require 'pallete_png/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'pallete_png'
7
+ spec.version = PalletePNG::VERSION
8
+ spec.authors = ['Katsuya HIDAKA']
9
+ spec.email = ['hidakatsuya@gmail.com']
10
+
11
+ spec.summary = 'Makes a pallete-based PNG transparency'
12
+ spec.description = 'PalletePNG is a pure-ruby library that makes a pallete-based PNG transparency.'
13
+ spec.homepage = 'https://github.com/hidakatsuya/pallete_png'
14
+ spec.license = 'MIT'
15
+ spec.platform = Gem::Platform::RUBY
16
+ spec.required_ruby_version = '>= 2.2'
17
+
18
+ spec.files = `git ls-files`.split("\n")
19
+ spec.test_files = `git ls-files -- {test}/*`.split("\n")
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_runtime_dependency 'chunky_png', '~> 1.3.0'
23
+
24
+ spec.add_development_dependency 'bundler', '>= 1.0.0'
25
+ spec.add_development_dependency 'rake'
26
+ spec.add_development_dependency 'minitest'
27
+ spec.add_development_dependency 'rr'
28
+ end
Binary file
Binary file
@@ -0,0 +1,42 @@
1
+ require 'test_helper'
2
+
3
+ module PalletePNG
4
+ class ImageTest < Minitest::Test
5
+ include TestHelpers
6
+
7
+ def test_init
8
+ assert_raises ChunkyPNG::SignatureMismatch do
9
+ Image.new image_data('not-a-png.jpg')
10
+ end
11
+
12
+ begin
13
+ Image.new image_data('pallete-png-with-transparency.png')
14
+ rescue ChunkyPNG::SignatureMismatch
15
+ refute
16
+ end
17
+ end
18
+
19
+ def test_pallete_based?
20
+ image = Image.new image_data('pallete-png-with-transparency.png')
21
+ assert image.pallete_based?
22
+
23
+ image = Image.new image_data('not-a-pallete-png.png')
24
+ refute image.pallete_based?
25
+ end
26
+
27
+ def test_transparency_chunk?
28
+ image = Image.new image_data('pallete-png-with-transparency.png')
29
+ assert image.transparency_chunk?
30
+
31
+ image = Image.new image_data('pallete-png-without-transparency.png')
32
+ refute image.transparency_chunk?
33
+ end
34
+
35
+ def test_to_truecolor_alpha
36
+ image = Image.new image_data('pallete-png-with-transparency.png')
37
+
38
+ datastream = ChunkyPNG::Datastream.from_blob image.to_truecolor_alpha
39
+ assert_equal ChunkyPNG::COLOR_TRUECOLOR_ALPHA, datastream.header_chunk.color
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,13 @@
1
+ $:.unshift File.expand_path('../../lib', __FILE__)
2
+
3
+ require 'pallete_png'
4
+ require 'minitest/autorun'
5
+
6
+ module PalletePNG
7
+ module TestHelpers
8
+ def image_data(filename)
9
+ image_path = File.join File.expand_path('../files', __FILE__), filename
10
+ File.binread(image_path)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,33 @@
1
+ require 'test_helper'
2
+
3
+ module PalletePNG
4
+ class ToTransparencyTest < Minitest::Test
5
+ include TestHelpers
6
+
7
+ def test_not_a_png
8
+ assert_raises Image::NotPNG do
9
+ PalletePNG.to_transparency image_data('not-a-png.jpg')
10
+ end
11
+ end
12
+
13
+ def test_not_a_pallete_png
14
+ raw_blob = image_data('not-a-pallete-png.png')
15
+ assert_equal raw_blob, PalletePNG.to_transparency(raw_blob)
16
+ end
17
+
18
+ def test_pallete_png_without_transparency
19
+ raw_blob = image_data('pallete-png-without-transparency.png')
20
+ assert_equal raw_blob, PalletePNG.to_transparency(raw_blob)
21
+ end
22
+
23
+ def test_pallete_png_with_transparency
24
+ raw_blob = image_data('pallete-png-with-transparency.png')
25
+ result_blob = PalletePNG.to_transparency(raw_blob)
26
+
27
+ refute_equal result_blob, raw_blob
28
+
29
+ result_datastream = ChunkyPNG::Datastream.from_blob(result_blob)
30
+ assert_equal ChunkyPNG::COLOR_TRUECOLOR_ALPHA, result_datastream.header_chunk.color
31
+ end
32
+ end
33
+ end
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pallete_png
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Katsuya HIDAKA
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-01-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: chunky_png
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.3.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.3.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.0.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 1.0.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: '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: minitest
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'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rr
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: PalletePNG is a pure-ruby library that makes a pallete-based PNG transparency.
84
+ email:
85
+ - hidakatsuya@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".travis.yml"
92
+ - Gemfile
93
+ - MIT-LICENSE
94
+ - README.md
95
+ - Rakefile
96
+ - lib/pallete_png.rb
97
+ - lib/pallete_png/image.rb
98
+ - lib/pallete_png/version.rb
99
+ - pallete_png.gemspec
100
+ - test/files/not-a-pallete-png.png
101
+ - test/files/not-a-png.jpg
102
+ - test/files/pallete-png-with-transparency.png
103
+ - test/files/pallete-png-without-transparency.png
104
+ - test/pallete_png/image_test.rb
105
+ - test/test_helper.rb
106
+ - test/to_transparency_test.rb
107
+ homepage: https://github.com/hidakatsuya/pallete_png
108
+ licenses:
109
+ - MIT
110
+ metadata: {}
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '2.2'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 2.5.1
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: Makes a pallete-based PNG transparency
131
+ test_files: []
132
+ has_rdoc: