pura-webp 0.1.0 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f68662dc2cbe5f2e2c4feeb36180b4a8fbe8227f059afc490ffeb9a636c90288
4
- data.tar.gz: 8cb72c6e9fcab068f71a1f0a4a42b05a43343b4b0f14b4cf768dc2217c31bcbd
3
+ metadata.gz: 02daf4b19a84439a95985701909b9a269cfa02c74932440abe0cb2e369c66669
4
+ data.tar.gz: addad87ed565e4f4ffb906f03a50894737829dabf598180b76240c586b847266
5
5
  SHA512:
6
- metadata.gz: 03aa144fae7b98ea9e1d921ea91d4ad699152ff83e30e2c106d229193077ede5a7fb7e37ce07e634d1beb7fd1b2fc9d07675bdb082be5f50b4fe63de42fcebf2
7
- data.tar.gz: f0753fbe3a783224e2976f5c3d24ad814f950fb7a0d65c755bc741929cac703c05682c47baed9f030f6317af1513db016be5e4c69a9b4935aa21e8b6d4e28b28
6
+ metadata.gz: 3ec5fd7e14f33bea4582eeee83462b2ae6eaf880f45822a073df095ed0720dff8716740a94570a17a5c903b5cce5eac0cc378b602626ea2aea9e27a1fae003bf
7
+ data.tar.gz: 3e4d23bcd37b0bcfd1c5f2728affb2d8ab1425cbeeb658f1a92a0a26a428377d58a538007d26ee5193686b42ba1abf4c5f78cbaffa49aa5190671ffc34598ebb
data/LICENSE-GO ADDED
@@ -0,0 +1,35 @@
1
+ The VP8 decoder in this gem (lib/pura/webp/decoder.rb,
2
+ lib/pura/webp/bool_decoder.rb, and the static tables in
3
+ lib/pura/webp/vp8_tables.rb) is a Ruby port of the Go package
4
+ golang.org/x/image/vp8, which is distributed under the following
5
+ BSD-3-Clause license:
6
+
7
+ ---
8
+
9
+ Copyright (c) 2009 The Go Authors. All rights reserved.
10
+
11
+ Redistribution and use in source and binary forms, with or without
12
+ modification, are permitted provided that the following conditions are
13
+ met:
14
+
15
+ * Redistributions of source code must retain the above copyright
16
+ notice, this list of conditions and the following disclaimer.
17
+ * Redistributions in binary form must reproduce the above
18
+ copyright notice, this list of conditions and the following disclaimer
19
+ in the documentation and/or other materials provided with the
20
+ distribution.
21
+ * Neither the name of Google LLC nor the names of its
22
+ contributors may be used to endorse or promote products derived from
23
+ this software without specific prior written permission.
24
+
25
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29
+ OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30
+ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31
+ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README.md CHANGED
@@ -73,4 +73,10 @@ Decode performance on a 400×400 WebP image, Ruby 4.0.2 + YJIT:
73
73
 
74
74
  ## License
75
75
 
76
- MIT
76
+ MIT — see [LICENSE](LICENSE).
77
+
78
+ The VP8 decoder is a Ruby port of the Go package
79
+ [golang.org/x/image/vp8](https://pkg.go.dev/golang.org/x/image/vp8),
80
+ which is distributed under the BSD-3-Clause license. See
81
+ [LICENSE-GO](LICENSE-GO) for that notice. The Go copyright is retained
82
+ on the ported files as required.
@@ -1,96 +1,111 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ #
4
+ # Ruby port of golang.org/x/image/vp8/partition.go. Copyright on the
5
+ # Go original is retained; see LICENSE-GO for the upstream BSD-3-Clause
6
+ # license. Modifications for Ruby are under the gem's MIT license.
7
+ #
8
+ # Copyright 2011 The Go Authors. All rights reserved.
9
+
3
10
  module Pura
4
11
  module Webp
12
+ # VP8 arithmetic bit decoder. Direct port of golang.org/x/image/vp8/partition.go.
13
+ # Follows libwebp's optimized formulation with precomputed shift/range lookup
14
+ # tables so normalization is O(1) per bit.
5
15
  class BoolDecoder
6
- def initialize(data, offset = 0, size = nil)
7
- @data = data
8
- @pos = offset
9
- @end_pos = size ? offset + size : data.bytesize
10
-
11
- @range = 255
12
- @value = 0
13
- @bits_left = 0
14
-
15
- load_initial
16
+ LUT_SHIFT = [
17
+ 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
18
+ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
19
+ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
20
+ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
21
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
22
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
23
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
24
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
25
+ ].freeze
26
+
27
+ LUT_RANGE_M1 = [
28
+ 127,
29
+ 127, 191,
30
+ 127, 159, 191, 223,
31
+ 127, 143, 159, 175, 191, 207, 223, 239,
32
+ 127, 135, 143, 151, 159, 167, 175, 183, 191, 199, 207, 215, 223, 231, 239, 247,
33
+ 127, 131, 135, 139, 143, 147, 151, 155, 159, 163, 167, 171, 175, 179, 183, 187,
34
+ 191, 195, 199, 203, 207, 211, 215, 219, 223, 227, 231, 235, 239, 243, 247, 251,
35
+ 127, 129, 131, 133, 135, 137, 139, 141, 143, 145, 147, 149, 151, 153, 155, 157,
36
+ 159, 161, 163, 165, 167, 169, 171, 173, 175, 177, 179, 181, 183, 185, 187, 189,
37
+ 191, 193, 195, 197, 199, 201, 203, 205, 207, 209, 211, 213, 215, 217, 219, 221,
38
+ 223, 225, 227, 229, 231, 233, 235, 237, 239, 241, 243, 245, 247, 249, 251, 253
39
+ ].freeze
40
+
41
+ UNIFORM_PROB = 128
42
+
43
+ attr_reader :unexpected_eof
44
+
45
+ def init(buf)
46
+ @buf = buf
47
+ @r = 0
48
+ @range_m1 = 254
49
+ @bits = 0
50
+ @n_bits = 0
51
+ @unexpected_eof = false
16
52
  end
17
53
 
18
- def read_bool(prob)
19
- split = 1 + (((@range - 1) * prob) >> 8)
20
- big_split = split << @bits_left
54
+ def initialize(buf = nil)
55
+ init(buf) if buf
56
+ end
21
57
 
22
- if @value >= big_split
23
- @range -= split
24
- @value -= big_split
25
- bit = 1
58
+ # Reads a single arithmetic-coded bit. Returns true or false (not 0/1).
59
+ def read_bit(prob)
60
+ if @n_bits < 8
61
+ if @r >= @buf.bytesize
62
+ @unexpected_eof = true
63
+ return false
64
+ end
65
+ x = @buf.getbyte(@r)
66
+ @bits |= x << (8 - @n_bits)
67
+ @r += 1
68
+ @n_bits += 8
69
+ end
70
+ split = ((@range_m1 * prob) >> 8) + 1
71
+ bit = @bits >= (split << 8)
72
+ if bit
73
+ @range_m1 -= split
74
+ @bits -= split << 8
26
75
  else
27
- @range = split
28
- bit = 0
76
+ @range_m1 = split - 1
29
77
  end
30
-
31
- normalize
32
- bit
33
- end
34
-
35
- def read_literal(n)
36
- val = 0
37
- n.times do
38
- val = (val << 1) | read_bool(128)
78
+ if @range_m1 < 127
79
+ shift = LUT_SHIFT[@range_m1]
80
+ @range_m1 = LUT_RANGE_M1[@range_m1]
81
+ @bits = (@bits << shift) & 0xFFFFFFFF
82
+ @n_bits -= shift
39
83
  end
40
- val
41
- end
42
-
43
- def read_flag
44
- read_bool(128) == 1
45
- end
46
-
47
- def read_signed(n)
48
- val = read_literal(n)
49
- read_flag ? -val : val
84
+ bit
50
85
  end
51
86
 
52
- def read_tree(tree, probs)
53
- idx = 0
54
- loop do
55
- idx += read_bool(probs[idx >> 1])
56
- val = tree[idx]
57
- return val if val <= 0
58
-
59
- idx = val
87
+ # Reads an n-bit unsigned integer, MSB first.
88
+ def read_uint(prob, n)
89
+ u = 0
90
+ while n.positive?
91
+ n -= 1
92
+ u |= (1 << n) if read_bit(prob)
60
93
  end
94
+ u
61
95
  end
62
96
 
63
- private
64
-
65
- def load_initial
66
- # Load enough bytes to fill value register
67
- 4.times do
68
- if @pos < @end_pos
69
- @value = (@value << 8) | @data.getbyte(@pos)
70
- @pos += 1
71
- else
72
- @value <<= 8
73
- end
74
- @bits_left += 8
75
- end
76
- @bits_left -= 8 # We work with bits_left relative to range position
97
+ # Reads an n-bit signed integer: magnitude bits followed by sign bit.
98
+ def read_int(prob, n)
99
+ u = read_uint(prob, n)
100
+ read_bit(prob) ? -u : u
77
101
  end
78
102
 
79
- def normalize
80
- while @range < 128
81
- @range <<= 1
82
- @bits_left -= 1
83
-
84
- next unless @bits_left.negative?
103
+ # Reads an "optional int": 1 prob-bit flag; if set, read n-bit signed int;
104
+ # otherwise return 0.
105
+ def read_optional_int(prob, n)
106
+ return 0 unless read_bit(prob)
85
107
 
86
- @bits_left += 8
87
- if @pos < @end_pos
88
- @value = (@value << 8) | @data.getbyte(@pos)
89
- @pos += 1
90
- else
91
- @value <<= 8
92
- end
93
- end
108
+ read_int(prob, n)
94
109
  end
95
110
  end
96
111
  end