size 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: b27ecf1e43545195565e4ff80ca2af55ab2084680f72d71bb7717bc1a622b561
4
+ data.tar.gz: 9b9d435f4dad02cf291efcc8eb6676690ca051f59d9fd19cd35b8392f4b35d18
5
+ SHA512:
6
+ metadata.gz: 249659309d407e2ceb90da295911eed121d9ed48737787a4db47eb1edbf848396ad62774c6b203e71a3aad69c37d825470049bbaa5445fed878ec774153542f1
7
+ data.tar.gz: b2232e87e7b8b0deef83c4cf1c864450d0db310171fb54552e77eac3f4fc06d4908dcc0c775b92d37313bf929bcf2e572d5ccd214e84deb9bd8a3e3078055fd1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Shannon Skipper
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,54 @@
1
+ # Size
2
+
3
+ Detect AVIF, GIF, HEIF, JPEG, PNG and WebP image dimensions with minimal memory usage. Pure Ruby, no deps.
4
+
5
+ ## Usage
6
+
7
+ ```ruby
8
+ size = Size.of("photo.jpg")
9
+ size.width #=> 1920
10
+ size.height #=> 1080
11
+ size.pixels #=> 2073600
12
+ size.class #=> Size::JPEG
13
+ ```
14
+
15
+ Accepts a file path, `Pathname` or any readable IO:
16
+
17
+ ```ruby
18
+ File.open("photo.png", "rb") { |io| Size.of(io) }
19
+ ```
20
+
21
+ Pattern matching works since `Size` is a `Data` class:
22
+
23
+ ```ruby
24
+ case Size.of(path)
25
+ in Size::PNG[width:, height:]
26
+ "PNG: #{width}x#{height}"
27
+ in Size::JPEG[width:, height:]
28
+ "JPEG: #{width}x#{height}"
29
+ end
30
+ ```
31
+
32
+ Supports AVIF, GIF, HEIF, JPEG, PNG and WebP. Raises `Size::FormatError` for unrecognized or truncated formats.
33
+
34
+ ## Installation
35
+
36
+ ```bash
37
+ gem install size
38
+ ```
39
+
40
+ Or add to your Gemfile:
41
+
42
+ ```ruby
43
+ gem "size"
44
+ ```
45
+
46
+ ## Alternatives
47
+
48
+ **[FastImage](https://github.com/sdsykes/fastimage)** is the most popular choice. It fetches dimensions from URLs, covers many more formats (BMP, TIFF, ICO, PSD, SVG and others) and has years of production use behind it. Locally it reads through a Fiber-based pipeline in 256-byte chunks.
49
+
50
+ **[ImageSize](https://github.com/toy/image_size)** has the broadest format coverage, with BMP, PSD, SWF, XPM and more. It reads in cached 4,096-byte chunks.
51
+
52
+ **[Size](https://github.com/havenwood/size)** covers six formats and works only with local files and I/O objects. It reads the minimum each format requires: 10 bytes for a GIF, 24 for a PNG, 25–30 for WebP and up to 512 for AVIF and HEIF. All three gems stream through JPEG markers, but FastImage begins with 256 bytes in memory, ImageSize with 4,096 and Size with 12.
53
+
54
+ For URL support or broad format coverage, use FastImage or ImageSize. For the smallest possible memory usage, use Size.
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "minitest/test_task"
5
+ require "standard/rake"
6
+
7
+ task default: %i[test standard]
8
+
9
+ Minitest::TestTask.create
data/lib/size/avif.rb ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Size::AVIF < Size
4
+ BRANDS = %w[avif avis].freeze
5
+
6
+ extend Size::ISOBMFF
7
+ end
data/lib/size/gif.rb ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Size::GIF < Size
4
+ def self.read(_io, header)
5
+ unless header.start_with?("GIF87a", "GIF89a")
6
+ raise Size::FormatError, "invalid GIF signature"
7
+ end
8
+
9
+ width, height = header.unpack("x6v2")
10
+ new(width:, height:)
11
+ end
12
+ end
data/lib/size/heif.rb ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Size::HEIF < Size
4
+ BRANDS = %w[heic heix hevc hevx heim heis hevm hevs].freeze
5
+
6
+ extend Size::ISOBMFF
7
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Size::ISOBMFF
4
+ def read_isobmff(data) = find_ispe(data, data.unpack1("N"))
5
+
6
+ def brand?(data, ftyp_size)
7
+ return true if self::BRANDS.include?(data.byteslice(8, 4))
8
+
9
+ offset = 16
10
+ while offset + 4 <= ftyp_size
11
+ return true if self::BRANDS.include?(data.byteslice(offset, 4))
12
+
13
+ offset += 4
14
+ end
15
+
16
+ false
17
+ end
18
+
19
+ private
20
+
21
+ def find_ispe(data, pos)
22
+ format_name = name.split("::").last
23
+
24
+ while pos + 8 <= data.bytesize
25
+ size = data.unpack1("N", offset: pos)
26
+ type = data.byteslice(pos + 4, 4)
27
+ raise Size::FormatError, "invalid #{format_name} box" if size < 8
28
+
29
+ case type
30
+ when "ispe"
31
+ raise Size::FormatError, "truncated #{format_name}" unless pos + 20 <= data.bytesize
32
+ width, height = data.unpack("N2", offset: pos + 12)
33
+
34
+ return new(width:, height:)
35
+ when "meta"
36
+ pos += 12 # FullBox header
37
+ when "iprp", "ipco"
38
+ pos += 8
39
+ else
40
+ pos += size
41
+ end
42
+ end
43
+
44
+ raise Size::FormatError, "no dimensions found in #{format_name}"
45
+ end
46
+ end
data/lib/size/jpeg.rb ADDED
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Size::JPEG < Size
4
+ SOF_MARKERS = ((0xC0..0xCF).to_a - [0xC4, 0xC8, 0xCC]).freeze
5
+ STANDALONE = [0x00, 0x01, *0xD0..0xD9].freeze
6
+
7
+ class << self
8
+ def read(io, header)
9
+ io = Size::PrefixedIO.new(header, io)
10
+ raise Size::FormatError, "invalid JPEG" unless io.read(2) == "\xFF\xD8".b
11
+
12
+ loop do
13
+ marker = read_marker(io)
14
+ raise Size::FormatError, "no SOF marker before end of image" if marker == 0xD9
15
+
16
+ if SOF_MARKERS.include?(marker)
17
+ seg = io.read(7)
18
+ raise Size::FormatError, "truncated JPEG SOF" unless seg&.bytesize == 7
19
+
20
+ height, width = seg.unpack("x3n2")
21
+ return new(width:, height:)
22
+ end
23
+
24
+ skip_segment(io)
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def read_marker(io)
31
+ loop do
32
+ byte = io.read(1)&.ord
33
+ raise Size::FormatError, "truncated JPEG" unless byte
34
+ next unless byte == 0xFF
35
+
36
+ loop do
37
+ marker = io.read(1)&.ord
38
+ raise Size::FormatError, "truncated JPEG" unless marker
39
+ next if marker == 0xFF
40
+ next if STANDALONE.include?(marker)
41
+
42
+ return marker
43
+ end
44
+ end
45
+ end
46
+
47
+ def skip_segment(io)
48
+ len_bytes = io.read(2)
49
+ raise Size::FormatError, "truncated JPEG" unless len_bytes&.bytesize == 2
50
+
51
+ len = len_bytes.unpack1("n")
52
+ skipped = io.read(len - 2)
53
+ raise Size::FormatError, "truncated JPEG" unless skipped&.bytesize == len - 2
54
+ end
55
+ end
56
+ end
data/lib/size/png.rb ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Size::PNG < Size
4
+ SIGNATURE = "\x89PNG\r\n\x1A\n".b
5
+
6
+ def self.read(io, header)
7
+ data = header
8
+ rest = io.read(12)
9
+ data += rest if rest
10
+ raise Size::FormatError, "truncated PNG" unless data.bytesize == 24
11
+ raise Size::FormatError, "invalid PNG signature" unless data.start_with?(SIGNATURE)
12
+
13
+ width, height = data.unpack("x16N2")
14
+ new(width:, height:)
15
+ end
16
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Size::PrefixedIO
4
+ def initialize(prefix, io)
5
+ @prefix = prefix
6
+ @io = io
7
+ @pos = 0
8
+ end
9
+
10
+ def read(n)
11
+ return @io.read(n) if @pos >= @prefix.bytesize
12
+
13
+ from_prefix = @prefix.byteslice(@pos, n)
14
+ @pos += from_prefix.bytesize
15
+ remaining = n - from_prefix.bytesize
16
+
17
+ if remaining > 0
18
+ from_io = @io.read(remaining)
19
+ from_prefix += from_io if from_io
20
+ end
21
+
22
+ from_prefix unless from_prefix.empty?
23
+ end
24
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Size
4
+ VERSION = "0.1.0"
5
+ end
data/lib/size/webp.rb ADDED
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Size::WebP < Size
4
+ class << self
5
+ def read(io, header)
6
+ data = header
7
+ rest = io.read(18)
8
+ data += rest if rest
9
+ fourcc = data.byteslice(12, 4)
10
+
11
+ case fourcc
12
+ when "VP8 "
13
+ read_vp8(data)
14
+ when "VP8L"
15
+ read_vp8l(data)
16
+ when "VP8X"
17
+ read_vp8x(data)
18
+ else
19
+ raise Size::FormatError, "unrecognized WebP variant: #{fourcc.inspect}"
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def read_vp8(data)
26
+ raise Size::FormatError, "truncated WebP VP8" unless data.bytesize >= 30
27
+ raise Size::FormatError, "invalid VP8 keyframe" unless data.byteslice(23, 3) == "\x9D\x01\x2A".b
28
+
29
+ width, height = data.unpack("x26v2")
30
+ new(width: width & 0x3FFF, height: height & 0x3FFF)
31
+ end
32
+
33
+ def read_vp8l(data)
34
+ raise Size::FormatError, "truncated WebP VP8L" unless data.bytesize >= 25
35
+ raise Size::FormatError, "invalid VP8L signature" unless data.getbyte(20) == 0x2F
36
+
37
+ bits = data.unpack1("V", offset: 21)
38
+ new(width: (bits & 0x3FFF) + 1, height: ((bits >> 14) & 0x3FFF) + 1)
39
+ end
40
+
41
+ def read_vp8x(data)
42
+ raise Size::FormatError, "truncated WebP VP8X" unless data.bytesize >= 30
43
+
44
+ w = data.getbyte(24) | (data.getbyte(25) << 8) | (data.getbyte(26) << 16)
45
+ h = data.getbyte(27) | (data.getbyte(28) << 8) | (data.getbyte(29) << 16)
46
+ new(width: w + 1, height: h + 1)
47
+ end
48
+ end
49
+ end
data/lib/size.rb ADDED
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ Size = Data.define(:width, :height)
4
+
5
+ require_relative "size/version"
6
+ require_relative "size/prefixed_io"
7
+ require_relative "size/isobmff"
8
+ require_relative "size/avif"
9
+ require_relative "size/gif"
10
+ require_relative "size/heif"
11
+ require_relative "size/jpeg"
12
+ require_relative "size/png"
13
+ require_relative "size/webp"
14
+
15
+ class Size
16
+ def pixels = width * height
17
+
18
+ class FormatError < StandardError
19
+ def initialize(message = "unrecognized image format") = super
20
+ end
21
+
22
+ class << self
23
+ def of(input)
24
+ case input
25
+ when String, Pathname
26
+ File.open(input, "rb") { |io| read(io) }
27
+ else
28
+ read(input)
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ def read(io)
35
+ header = io.read(12)
36
+ raise FormatError, "could not read image header" unless header&.bytesize == 12
37
+
38
+ if header.start_with?("\x89PNG".b)
39
+ PNG.read(io, header)
40
+ elsif header.start_with?("\xFF\xD8".b)
41
+ JPEG.read(io, header)
42
+ elsif header.start_with?("GIF".b)
43
+ GIF.read(io, header)
44
+ elsif header[0, 4] == "RIFF" && header[8, 4] == "WEBP"
45
+ WebP.read(io, header)
46
+ elsif header[4, 4] == "ftyp"
47
+ isobmff_read(io, header)
48
+ else
49
+ raise FormatError
50
+ end
51
+ end
52
+
53
+ def isobmff_read(io, header)
54
+ data = header
55
+ rest = io.read(500)
56
+ data += rest if rest
57
+ ftyp_size = data.unpack1("N")
58
+
59
+ klass = [AVIF, HEIF].find { |format| format.brand?(data, ftyp_size) }
60
+ raise FormatError unless klass
61
+
62
+ klass.read_isobmff(data)
63
+ end
64
+ end
65
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: size
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Shannon Skipper
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: A pure Ruby library for detecting image dimensions by reading the minimum
13
+ bytes from AVIF, GIF, HEIF, JPEG, PNG and WebP files
14
+ email:
15
+ - shannonskipper@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - LICENSE.txt
21
+ - README.md
22
+ - Rakefile
23
+ - lib/size.rb
24
+ - lib/size/avif.rb
25
+ - lib/size/gif.rb
26
+ - lib/size/heif.rb
27
+ - lib/size/isobmff.rb
28
+ - lib/size/jpeg.rb
29
+ - lib/size/png.rb
30
+ - lib/size/prefixed_io.rb
31
+ - lib/size/version.rb
32
+ - lib/size/webp.rb
33
+ homepage: https://github.com/havenwood/size
34
+ licenses:
35
+ - MIT
36
+ metadata:
37
+ source_code_uri: https://github.com/havenwood/size
38
+ rubygems_mfa_required: 'true'
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '4.0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubygems_version: 4.0.7
54
+ specification_version: 4
55
+ summary: Detect image dimensions with minimal reads
56
+ test_files: []