icns 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 21907abf35ec200a4464c7f44871455d1f3d1053
4
+ data.tar.gz: 3da2ede6d26b008b40ce8d1fa0bb257be64deb2d
5
+ SHA512:
6
+ metadata.gz: a6afa750b73da5a50b20af734c48697920f7b06b16832c8544db25ff0c85eb42232ca0bb9279c7fce9641388fff654a98df02af69db511edbc57d198f432d396
7
+ data.tar.gz: 3c8af1ab4300fc43efe5573abd9b542512f46ecf954dc9d3b981d6291e3e2aec7697b1a05f60c5b7bfefe0711a206f1973cc58f7e893d7b523547e80892e5203
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.2
5
+ before_install: gem install bundler -v 1.16.1
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ gemspec
@@ -0,0 +1,22 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ icns (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ minitest (5.11.3)
10
+ rake (10.5.0)
11
+
12
+ PLATFORMS
13
+ ruby
14
+
15
+ DEPENDENCIES
16
+ bundler (~> 1.16)
17
+ icns!
18
+ minitest (~> 5.0)
19
+ rake (~> 10.0)
20
+
21
+ BUNDLED WITH
22
+ 1.16.1
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Sam Soffes, https://soff.es
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.
@@ -0,0 +1,62 @@
1
+ # Icns
2
+
3
+ Ruby library for extrating images from [ICNS](https://en.wikipedia.org/wiki/Apple_Icon_Image_format) files.
4
+
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'icns'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install icns
21
+
22
+
23
+ ## Usage
24
+
25
+ Read an icon out of an ICNS file:
26
+
27
+ ```ruby
28
+ require 'icns'
29
+
30
+ # Create a reader for an ICNS file
31
+ reader = Icns::Reader('/Applications/Preview.app/Contents/Resources/Preview.icns')
32
+
33
+ # Extract the image data for a given size
34
+ data = reader.image_for_size(1024)
35
+
36
+ # Write the image data out or do whatever you want with it
37
+ File.write('Preview.png', data)
38
+ ```
39
+
40
+ Annoyingly, icons can be either PNGs or JPEG-2000s. The image data will start with `"\x89PNG"` if it is a PNG. You need to always have them in one format or another, I’d recommend using [minimagick](https://github.com/minimagick/minimagick) to convert it.
41
+
42
+
43
+ ## Development
44
+
45
+ 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.
46
+
47
+ 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).
48
+
49
+
50
+ ## Contributing
51
+
52
+ Bug reports and pull requests are welcome on GitHub at https://github.com/soffes/icns.
53
+
54
+
55
+ ## License
56
+
57
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
58
+
59
+
60
+ ## References
61
+
62
+ https://en.wikipedia.org/wiki/Apple_Icon_Image_format
@@ -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,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'icns'
5
+
6
+ require 'irb'
7
+ IRB.start(__FILE__)
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'icns/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'icns'
9
+ spec.version = Icns::VERSION
10
+ spec.authors = ['Sam Soffes']
11
+ spec.email = ['sam@soff.es']
12
+ spec.summary = 'Ruby library for extrating images from ICNS files.'
13
+ spec.homepage = 'https://github.com/soffes/icns'
14
+ spec.license = 'MIT'
15
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
16
+ f.match(%r{^(test|spec|features)/})
17
+ end
18
+ spec.bindir = 'exe'
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+ spec.add_development_dependency 'bundler', '~> 1.16'
22
+ spec.add_development_dependency 'rake', '~> 10.0'
23
+ spec.add_development_dependency 'minitest', '~> 5.0'
24
+ end
@@ -0,0 +1,117 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'icns/version'
4
+ require 'icns/reader'
5
+
6
+ module Icns
7
+ # Map of type names to pixel sizes
8
+ TYPE_TO_SIZE = {
9
+ # 32×32 1-bit mono icon
10
+ 'ICON' => 32,
11
+
12
+ # 32×32 1-bit mono icon with 1-bit mask
13
+ 'ICN#' => 32,
14
+
15
+ # 16×12 1 bit mono icon with 1-bit mask
16
+ 'icm#' => 16,
17
+
18
+ # 16×12 4 bit icon
19
+ 'icm4' => 16,
20
+
21
+ # 16×12 8 bit icon
22
+ 'icm8' => 16,
23
+
24
+ # 16×16 1-bit mask
25
+ 'ics#' => 16,
26
+
27
+ # 16×16 4-bit icon
28
+ 'ics4' => 16,
29
+
30
+ # 16x16 8 bit icon
31
+ 'ics8' => 16,
32
+
33
+ # 16×16 24-bit icon
34
+ 'is32' => 16,
35
+
36
+ # 16x16 8-bit mask
37
+ 's8mk' => 16,
38
+
39
+ # 32×32 4-bit icon
40
+ 'icl4' => 32,
41
+
42
+ # 32×32 8-bit icon
43
+ 'icl8' => 32,
44
+
45
+ # 32x32 24-bit icon
46
+ 'il32' => 32,
47
+
48
+ # 32×32 8-bit mask
49
+ 'l8mk' => 32,
50
+
51
+ # 48×48 1-bit mask
52
+ 'ich#' => 48,
53
+
54
+ # 48×48 4-bit icon
55
+ 'ich4' => 48,
56
+
57
+ # 48×48 8-bit icon
58
+ 'ich8' => 48,
59
+
60
+ # 48×48 24-bit icon
61
+ 'ih32' => 48,
62
+
63
+ # 48×48 8-bit mask
64
+ 'h8mk' => 48,
65
+
66
+ # 128×128 24-bit icon
67
+ 'it32' => 128,
68
+
69
+ # 128×128 8-bit mask
70
+ 't8mk' => 128,
71
+
72
+ # 16x16 icon in JPEG 2000 or PNG format
73
+ 'icp4' => 16,
74
+
75
+ # 32x32 icon in JPEG 2000 or PNG format
76
+ 'icp5' => 32,
77
+
78
+ # 64x64 icon in JPEG 2000 or PNG format
79
+ 'icp6' => 64,
80
+
81
+ # 128x128 icon in JPEG 2000 or PNG format
82
+ 'ic07' => 128,
83
+
84
+ # 256×256 icon in JPEG 2000 or PNG format
85
+ 'ic08' => 256,
86
+
87
+ # 512×512 icon in JPEG 2000 or PNG format
88
+ 'ic09' => 512,
89
+
90
+ # 1024×1024 in 10.7 (or 512x512@2x "retina" in 10.8) icon in JPEG 2000 or
91
+ # PNG format
92
+ 'ic10' => 1024,
93
+
94
+ # 16x16@2x "retina" icon in JPEG 2000 or PNG format
95
+ 'ic11' => 32,
96
+
97
+ # 32x32@2x "retina" icon in JPEG 2000 or PNG format
98
+ 'ic12' => 64,
99
+
100
+ # 128x128@2x "retina" icon in JPEG 2000 or PNG format
101
+ 'ic13' => 256,
102
+
103
+ # 256x256@2x "retina" icon in JPEG 2000 or PNG format
104
+ 'ic14' => 512
105
+ }.freeze
106
+
107
+ # Types to try for each size
108
+ SIZE_TO_TYPE = {
109
+ 16 => %w(icp4),
110
+ 32 => %w(icp5 ic11),
111
+ 64 => %w(icp6 ic12),
112
+ 128 => %w(ic07),
113
+ 256 => %w(ic08 ic13),
114
+ 512 => %w(ic09 ic14),
115
+ 1024 => %w(ic10)
116
+ }
117
+ end
@@ -0,0 +1,6 @@
1
+ module Icns
2
+ class Error < StandardError; end
3
+
4
+ class FileNotFound < Error; end
5
+ class InvalidHeader < Error; end
6
+ end
@@ -0,0 +1,97 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'icns/errors'
4
+
5
+ module Icns
6
+ class Reader
7
+ # Initialize with a path to a valid ICNS file
8
+ def initialize(path)
9
+ throw FileNotFound unless File.exists?(path)
10
+
11
+ @path = path
12
+ @parts = {}
13
+
14
+ read_metadata
15
+ end
16
+
17
+ # Available types
18
+ def types
19
+ @parts.keys
20
+ end
21
+
22
+ # Raw data for a type
23
+ def data_for_type(type)
24
+ return nil unless part = @parts[type.to_s]
25
+
26
+ File.open(@path) do |file|
27
+ # Seek to the image data position
28
+ file.pos = part[:offset]
29
+
30
+ # Read the length of the image data
31
+ file.read(part[:length])
32
+ end
33
+ end
34
+
35
+ # PNG or JPEG-2000 data for a size
36
+ def image_for_size(size)
37
+ return nil unless types = SIZE_TO_TYPE[size.to_i]
38
+
39
+ data = nil
40
+ types.each do |type|
41
+ break if data = data_for_type(type)
42
+ end
43
+
44
+ data
45
+ end
46
+
47
+ # Available image sizes
48
+ def sizes
49
+ types = SIZE_TO_TYPE.values.flatten
50
+ @parts.keys.select { |k| types.include?(k) }.
51
+ map { |k| TYPE_TO_SIZE[k] }.sort.uniq
52
+ end
53
+
54
+ private
55
+
56
+ # These types are just metadata and not images.
57
+ TYPES_BLACKLIST = [
58
+ # An optional table of contents
59
+ 'TOC ',
60
+
61
+ # 4-byte big endian float - equal to the bundle version number of Icon
62
+ # Composer.app that created to icon
63
+ 'icnV'
64
+ ].freeze
65
+
66
+ def read_metadata
67
+ # TODO: Parse `TOC ` part if available
68
+
69
+ File.open(@path) do |file|
70
+ # The first 4 bytes must be 'icns'
71
+ throw InvalidHeader unless file.read(4) == 'icns'
72
+
73
+ # The next 4 bytes are the total length of the file. Skip ahead.
74
+ file.pos += 4
75
+
76
+ # Keep reading until we hit the end
77
+ while true
78
+ # If we can't read the icon type and length, we’re at the end
79
+ break unless (type = file.read(4)) && (length = file.read(4))
80
+
81
+ # Convert the length into an integer. The length includes the icon
82
+ # header. We just want the length of the image data.
83
+ length = length.unpack('N')[0] - 8
84
+
85
+ unless TYPES_BLACKLIST.include?(type)
86
+ # Add to our hash of type name to image data locations
87
+ puts "WARNING: duplicate part '#{type}'" if @parts[type]
88
+ @parts[type] = { offset: file.pos, length: length }
89
+ end
90
+
91
+ # Advance past the image data
92
+ file.pos += length
93
+ end
94
+ end
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Icns
4
+ VERSION = '0.1.0'
5
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: icns
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sam Soffes
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-05-18 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.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
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: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ description:
56
+ email:
57
+ - sam@soff.es
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".travis.yml"
64
+ - Gemfile
65
+ - Gemfile.lock
66
+ - LICENSE
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - icns.gemspec
72
+ - lib/icns.rb
73
+ - lib/icns/errors.rb
74
+ - lib/icns/reader.rb
75
+ - lib/icns/version.rb
76
+ homepage: https://github.com/soffes/icns
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.6.13
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Ruby library for extrating images from ICNS files.
100
+ test_files: []