lib/blurhash.rb in blurhash-0.1.6 vs lib/blurhash.rb in blurhash-0.1.7
- old
+ new
@@ -1,31 +1,23 @@
# frozen_string_literal: true
require 'blurhash/version'
-require 'ffi'
+require 'blurhash_ext'
module Blurhash
def self.encode(width, height, pixels, x_comp: 4, y_comp: 3)
- FFI::MemoryPointer.new(:u_int8_t, pixels.size) do |p|
- p.write_array_of_uint8(pixels)
- return Unstable.blurHashForPixels(x_comp, y_comp, width, height, p, width * 3)
- end
+ p = pixels.pack("C#{pixels.size}")
+ return Unstable.blurHashForPixels(x_comp, y_comp, width, height, p)
end
def self.components(str)
size_flag = Base83.decode83(str[0])
y_comp = (size_flag / 9) + 1
x_comp = (size_flag % 9) + 1
return if str.size != 4 + 2 * x_comp * y_comp
[x_comp, y_comp]
- end
-
- module Unstable
- extend FFI::Library
- ffi_lib File.join(File.expand_path(File.dirname(__FILE__)), '..', 'ext', 'blurhash', 'encode.' + RbConfig::CONFIG['DLEXT'])
- attach_function :blurHashForPixels, %i(int int int int pointer size_t), :string
end
module Base83
DIGIT_CHARACTERS = %w(
0 1 2 3 4 5 6 7 8 9