shrine-blurhash 0.2.1 → 0.2.3

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: 39c6d8fd3e19a78ac91d21d5adebb1a834fafcb80c1493b281a132f6d9880201
4
- data.tar.gz: e4e7faea183a676725b610353fc8d052417fdf1bdd58544ed73647b87e9f777c
3
+ metadata.gz: 1775a3abaa4ff444b6320adc7d3eb5125d2dbd20d32756166397667486ef8070
4
+ data.tar.gz: fa63be0fc7a4b6dc69074578762859d337e8b2f476c2e06603261b5831ae44a1
5
5
  SHA512:
6
- metadata.gz: 11e4c2bf34926fc35e6bc18b75c6642eeda93ecda13e3c8a767ce0e776a428e4a6d2fb05833f7e9e42701941dc5dc8c9acf34adf933c4afa05b5979e6aaa75f7
7
- data.tar.gz: 595c3ae789fd7267d195bec699893cb7b56972607b301a3a9da6d5abb566fadad0cc8447b78323324b0e970d33314b81e1f0f92392b1c4d288a76be0e79117fa
6
+ metadata.gz: 8c443841b6c082c801ac10a5b32484cd51023e8516e3c4cfceedb8e0c1383a72517e992ed673d8325a52282065ebd9c9c8c7407f53a51c15662b278393b21530
7
+ data.tar.gz: 267ab27ff58d5c7e9ff3948dfa0d165d39fb6ec12da8dae17500ef22ecf22964ba7db32596fe69d1197dc93d56df0742e5ff03ea47e9695ed4387547df2cf16b
@@ -53,6 +53,9 @@ class Shrine
53
53
  io.rewind
54
54
 
55
55
  blurhash
56
+ rescue StandardError => e
57
+ opts[:blurhash][:on_error].call(e)
58
+ nil
56
59
  end
57
60
 
58
61
  # Returns a hash of built-in pixels extractors, where keys are
@@ -66,9 +69,7 @@ class Shrine
66
69
 
67
70
  # Returns callable pixels extractor object.
68
71
  def pixels_extractor(name)
69
- on_error = opts[:blurhash][:on_error]
70
-
71
- PixelsExtractor.new(name, on_error: on_error).method(:call)
72
+ PixelsExtractor.new(name).method(:call)
72
73
  end
73
74
 
74
75
  private
@@ -106,13 +107,12 @@ class Shrine
106
107
  class PixelsExtractor
107
108
  SUPPORTED_EXTRACTORS = [:ruby_vips].freeze
108
109
 
109
- def initialize(tool, on_error: method(:fail))
110
+ def initialize(tool)
110
111
  unless SUPPORTED_EXTRACTORS.include?(tool)
111
112
  raise Error, "unknown pixel extractor #{tool.inspect}, supported extractors are: #{SUPPORTED_EXTRACTORS.join(',')}"
112
113
  end
113
114
 
114
115
  @tool = tool
115
- @on_error = on_error
116
116
  end
117
117
 
118
118
  def call(io, resize_to)
@@ -126,26 +126,31 @@ class Shrine
126
126
  def extract_with_ruby_vips(io, resize_to)
127
127
  require "vips"
128
128
 
129
- begin
130
- Shrine.with_file(io) do |file|
131
- image = Vips::Image.new_from_file(file.path, access: :sequential)
132
- image = image.resize(resize_to.fdiv(image.width), vscale: resize_to.fdiv(image.height)) if resize_to
133
- image = image.flatten if image.has_alpha?
134
-
135
- {
136
- width: image.width,
137
- height: image.height,
138
- pixels: image.to_a.flatten,
139
- }
129
+ Shrine.with_file(io) do |file|
130
+ image = Vips::Image.new_from_file(file.path, access: :sequential).colourspace(:srgb)
131
+ image = image.resize(resize_to.fdiv(image.width), vscale: resize_to.fdiv(image.height)) if resize_to
132
+ image = image.flatten if image.has_alpha?
133
+ # Blurhash requires exactly 3 bands
134
+ case image.bands
135
+ when 1
136
+ # Duplicate the only band into 2 new bands
137
+ image = image.bandjoin(Array.new(3 - image.bands, image))
138
+ when 2
139
+ # Duplicate the first band into a third band
140
+ image = image.bandjoin(image.extract_band(0))
141
+ when 3
142
+ # Do nothing, band count is already correct
143
+ else
144
+ # Only keep the first 3 bands
145
+ image = image.extract_band(0, n: 3)
140
146
  end
141
- rescue Vips::Error => e
142
- on_error(e)
143
- end
144
- end
145
147
 
146
- def on_error(error)
147
- @on_error.call(error)
148
- nil
148
+ {
149
+ width: image.width,
150
+ height: image.height,
151
+ pixels: image.to_a.flatten,
152
+ }
153
+ end
149
154
  end
150
155
  end
151
156
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |gem|
4
4
  gem.name = "shrine-blurhash"
5
- gem.version = "0.2.1"
5
+ gem.version = "0.2.3"
6
6
 
7
7
  gem.required_ruby_version = ">= 3.1"
8
8
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shrine-blurhash
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Renaud Chaput
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-10-25 00:00:00.000000000 Z
11
+ date: 2025-04-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: blurhash