blurhash_ruby 0.0.14 → 0.0.16

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: ef513fb805c4bdc62e5236843403bb3af8cb06c034c48ac2abc1a9d94a6863e8
4
- data.tar.gz: 037752a9951af438c498f2caf27ab31f3717023124fa3c7e4300761cbb7f8c12
3
+ metadata.gz: 315b833a934d58fd6e79ed73d3437b696d34f0627ecbab6d2daf1ca955b55b9b
4
+ data.tar.gz: d7edf9e3acea388dc8205644dbbcbe52e3f7ccd864f459b28dd9382706330e27
5
5
  SHA512:
6
- metadata.gz: 751b20bc24a4a36e0f97121e1faabef567afdf04e3c9e69763b121a6c7c5c4e9746eeba1b38ef266467109985752868c9b9b26ec5930e2f0afff4f43a9a97e93
7
- data.tar.gz: 4b69c4baef24c4e7d696a818b92a6d04d685a01c1c6f8865a84ce31d6102ab2709113ea72070220a87be349c832cc9c2ee49efef8a4e6fd3d82b6380b66eb217
6
+ metadata.gz: 5178d705ee916a4a8e1fc5904265dcd2b41ba8e9a35cbfd6033644dd5891a5f7d2f27c2fb48cbca2fb64468b2ee8c0e48332a0b9eaa4363b239e847a2b2ab96b
7
+ data.tar.gz: e74a6c098687d9b2e530a10698cf3303ef7f687a9b1b6bc37b1f8ab078d8fa5b1005f53d26acd3927ce3e791e60b4914efc8b3012b700f127e4a39a62fb54a9c
data/.gitignore CHANGED
@@ -1,4 +1,5 @@
1
1
  tmp
2
- lib/blurhash_decoder/binary/*.bundle
3
- lib/blurhash_encoder/binary/*.bundle
4
- *.gem
2
+ lib/blurhash_ruby/*.bundle
3
+ lib/blurhash_encoder/*.bundle
4
+ *.gem
5
+ .DS_Store
data/README.md CHANGED
@@ -1 +1,43 @@
1
1
  ## Blurhash Ruby
2
+ Generate a compact representation of a placeholder for an image in Ruby that can be saved in database, returned as API response and can be displayed as a blurred preview before the real image loads. This gem provides different options depending on the use case, either you want to store blurhash representation of the image(about 20-30 characters) and decode on the fly or you want to directly store the more longer(about 150 characters) base64 representation to skip decoding step, choice is yours!!
3
+ If you want to learn how blurhash works plese refer to the [original repository](https://github.com/woltapp/blurhash).
4
+
5
+ ### Why this gem?
6
+ ✅ No external dependencies \
7
+ ✅ Contains both encoder and decoder \
8
+ ✅ Faster than other ruby implementations
9
+
10
+ ### Installation
11
+ Add this line to your application's Gemfile:
12
+ ```ruby
13
+ gem 'blurhash_ruby'
14
+ ```
15
+
16
+ And then execute:
17
+ ```ruby
18
+ $ bundle
19
+ ```
20
+
21
+ Or install it yourself as:
22
+ ```ruby
23
+ $ gem install blurhash_ruby
24
+ ```
25
+
26
+ ### Usage
27
+ ```ruby
28
+ require 'blurhash_ruby'
29
+
30
+ BlurhashRuby.encode_image('https://cdn.pixabay.com/photo/2018/01/14/23/12/nature-3082832_
31
+ _480.jpg')
32
+ # => "LMCFqys-0gIp-os,NHNI={s,R+I;"
33
+
34
+ BlurhashRuby.decode_blurhash('LHB3~nxvjYax0Mo#o#t7-cayWBWE')
35
+ # => "iVBORw0KGgoAAAANSUhEUgAAAAQAAAACCAYAAAB/qH1jAAAALElEQVR4XmMMSUj///3bB4YfX18xfP/xkYGFjdmQ4S8bA8Pv/z8ZOFl/MgAANdoP6+z0zPQAAAAASUVORK5CYII="
36
+ ```
37
+
38
+ ### Contributing
39
+ 1. Fork it
40
+ 2. Create your feature branch (git checkout -b my-new-feature)
41
+ 3. Commit your changes (git commit -am 'Add some feature')
42
+ 4. Push to the branch (git push origin my-new-feature)
43
+ 5. Create new Pull Request
data/Rakefile CHANGED
@@ -1,9 +1,9 @@
1
1
  require "rake/extensiontask"
2
2
 
3
3
  Rake::ExtensionTask.new "blurhash_decoder" do |ext|
4
- ext.lib_dir = 'lib/blurhash_decoder/binary'
4
+ ext.lib_dir = 'lib/blurhash_ruby'
5
5
  end
6
6
 
7
7
  Rake::ExtensionTask.new "blurhash_encoder" do |ext|
8
- ext.lib_dir = 'lib/blurhash_encoder/binary'
8
+ ext.lib_dir = 'lib/blurhash_ruby'
9
9
  end
data/benchmark.rb ADDED
@@ -0,0 +1,21 @@
1
+ require 'benchmark'
2
+ require 'blurhash_ruby'
3
+ require 'blurhash'
4
+ require 'rmagick'
5
+
6
+ Benchmark.bm do |x|
7
+ x.report {
8
+ BlurhashRuby.encode_image('https://twilio-cms-prod.s3.amazonaws.com/images/MSjyG8qNtcrbONBpWKztYRJqSpr4R2M6K83KXW4dj05n5.width-1616.png')
9
+ }
10
+ end
11
+
12
+ Benchmark.bm do |x|
13
+ x.report {
14
+ image_url = 'https://twilio-cms-prod.s3.amazonaws.com/images/MSjyG8qNtcrbONBpWKztYRJqSpr4R2M6K83KXW4dj05n5.width-1616.png'
15
+ filename = 'in.png'
16
+ File.write filename, URI.open(image_url).read
17
+ file_path = Dir.pwd + "/#{filename}"
18
+ image = Magick::ImageList.new(file_path)
19
+ Blurhash.encode(image.columns, image.rows, image.export_pixels)
20
+ }
21
+ end
@@ -1,9 +1,6 @@
1
- # lib = File.expand_path('../lib/', __FILE__)
2
- # $:.unshift lib unless $:.include?(lib)
3
-
4
1
  Gem::Specification.new do |s|
5
2
  s.name = 'blurhash_ruby'
6
- s.version = '0.0.14'
3
+ s.version = '0.0.16'
7
4
  s.summary = "A fast blurhash encoder/decoder gem!"
8
5
  s.description = "A fast blurhash encoder/decoder gem."
9
6
  s.authors = ["Rabin Poudyal"]
@@ -6,13 +6,13 @@
6
6
 
7
7
  VALUE DECODER = Qnil; /* Ruby Module */
8
8
 
9
- VALUE method_decode(VALUE self, VALUE blurhash, VALUE heigh, VALUE widt, VALUE punc) {
9
+ VALUE method_decode(VALUE self, VALUE path, VALUE blurhash, VALUE heigh, VALUE widt, VALUE punc) {
10
10
  const char * hash = StringValuePtr(blurhash);
11
11
  int height = NUM2INT(heigh);
12
12
  int width = NUM2INT(widt);
13
13
  int punch = NUM2INT(punc);
14
14
 
15
- const char * output_file = "tmp/out.png";
15
+ const char * output_file = StringValuePtr(path);
16
16
 
17
17
  const int nChannels = 4;
18
18
 
@@ -34,6 +34,6 @@ VALUE method_decode(VALUE self, VALUE blurhash, VALUE heigh, VALUE widt, VALUE p
34
34
  void
35
35
  Init_blurhash_decoder(void) {
36
36
  DECODER = rb_define_module("DECODER");
37
- rb_define_method(DECODER, "decode", method_decode, 4);
37
+ rb_define_method(DECODER, "decode", method_decode, 5);
38
38
  }
39
39
 
@@ -1,6 +1,6 @@
1
1
  #ifndef __BLURHASH_DECODE_H__
2
2
 
3
- #define __BLURHASH_DECODE_H
3
+ #define __BLURHASH_DECODE_H__
4
4
 
5
5
  #include <math.h>
6
6
  #include <stdbool.h>
@@ -3,4 +3,4 @@ require "mkmf"
3
3
  abort "missing malloc()" unless have_func "malloc"
4
4
  abort "missing free()" unless have_func "free"
5
5
 
6
- create_makefile("blurhash_decoder/blurhash_decoder")
6
+ create_makefile("blurhash_ruby/blurhash_decoder")
@@ -12,11 +12,11 @@ VALUE ENCODER = Qnil; /* Ruby Module */
12
12
 
13
13
  const char *blurHashForFile(int xComponents, int yComponents,const char *filename);
14
14
 
15
- VALUE method_encode(VALUE self, VALUE x, VALUE y) {
15
+ VALUE method_encode(VALUE self, VALUE path ,VALUE x, VALUE y) {
16
16
  int xComponents = NUM2INT(x);
17
17
  int yComponents = NUM2INT(y);
18
18
 
19
- const char * input_file = "tmp/in.png";
19
+ const char * input_file = StringValuePtr(path);
20
20
 
21
21
  if(xComponents < 1 || xComponents > 8 || yComponents < 1 || yComponents > 8) {
22
22
  fprintf(stderr, "Component counts must be between 1 and 8.\n");
@@ -47,6 +47,6 @@ const char *blurHashForFile(int xComponents, int yComponents,const char *filenam
47
47
  void
48
48
  Init_blurhash_encoder(void) {
49
49
  ENCODER = rb_define_module("ENCODER");
50
- rb_define_method(ENCODER, "encode", method_encode, 2);
50
+ rb_define_method(ENCODER, "encode", method_encode, 3);
51
51
  }
52
52
 
@@ -3,4 +3,4 @@ require "mkmf"
3
3
  abort "missing malloc()" unless have_func "malloc"
4
4
  abort "missing free()" unless have_func "free"
5
5
 
6
- create_makefile("blurhash_encoder/blurhash_encoder")
6
+ create_makefile("blurhash_ruby/blurhash_encoder")
data/lib/blurhash_ruby.rb CHANGED
@@ -1,15 +1,31 @@
1
- require File.expand_path('../blurhash_encoder/blurhash_encoder', __FILE__)
2
- require File.expand_path('../blurhash_decoder/blurhash_decoder', __FILE__)
1
+ require 'blurhash_ruby/blurhash_encoder'
2
+ require 'blurhash_ruby/blurhash_decoder'
3
3
 
4
- class Blurhash
5
- def self.encode(image_url, x_comp = 4, y_comp = 3)
6
- # Usage: Blurhash.encode('https://cdn.pixabay.com/photo/2018/01/14/23/12/nature-3082832__480.jpg')
7
- BlurhashEncoder.new.blurhash(image_url, x_comp, y_comp)
4
+ require 'open-uri'
5
+ require 'base64'
6
+
7
+
8
+ class BlurhashRuby
9
+ extend DECODER
10
+ extend ENCODER
11
+
12
+ def self.encode_image(image_url, x_comp = 4, y_comp = 3)
13
+ # Usage: BlurhashRuby.encode_image('https://cdn.pixabay.com/photo/2018/01/14/23/12/nature-3082832__480.jpg')
14
+ filename = 'in.png'
15
+ File.write filename, URI.open(image_url).read
16
+ file_path = Dir.pwd + "/#{filename}"
17
+ encode(file_path, x_comp, y_comp)
8
18
  end
9
19
 
10
- def self.decode(blurhash, height = 2, width = 4, punch = 1, as_img = false)
11
- # Usage: Blurhash.decode('LHB3~nxvjYax0Mo#o#t7-cayWBWE')
12
- hash = BlurhashDecoder.new.decode_blurhash(blurhash, height, width, punch)
13
- as_img ? 'data:image/png;base64,' + hash : hash
20
+ def self.decode_blurhash(blurhash, height = 2, width = 4, punch = 1, as_img = false)
21
+ # Usage: BlurhashRuby.decode_blurhash('LHB3~nxvjYax0Mo#o#t7-cayWBWE')
22
+ filename = 'out.png'
23
+ file_path = Dir.pwd + "/#{filename}"
24
+
25
+ decode(file_path, blurhash, height, width, punch)
26
+ base64_image = File.open(file_path, "rb") do |file|
27
+ Base64.strict_encode64(file.read)
28
+ end
29
+ as_img ? 'data:image/png;base64,' + base64_image : base64_image
14
30
  end
15
- end
31
+ end
data/run.sh ADDED
@@ -0,0 +1,6 @@
1
+ gem uninstall blurhash_ruby
2
+ rm -rf tmp/
3
+ gem build blurhash_ruby.gemspec
4
+ gem install blurhash_ruby-0.0.15.gem
5
+ cd ..
6
+ irb
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: blurhash_ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.14
4
+ version: 0.0.16
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rabin Poudyal
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-02-01 00:00:00.000000000 Z
11
+ date: 2022-02-03 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A fast blurhash encoder/decoder gem.
14
14
  email: rabin@trip101.com
@@ -23,6 +23,7 @@ files:
23
23
  - Gemfile.lock
24
24
  - README.md
25
25
  - Rakefile
26
+ - benchmark.rb
26
27
  - blurhash_ruby.gemspec
27
28
  - ext/blurhash_decoder/blurhash_decoder.c
28
29
  - ext/blurhash_decoder/decode.c
@@ -35,9 +36,8 @@ files:
35
36
  - ext/blurhash_encoder/extconf.rb
36
37
  - ext/blurhash_encoder/stb_image.h
37
38
  - ext/common.h
38
- - lib/blurhash_decoder/blurhash_decoder.rb
39
- - lib/blurhash_encoder/blurhash_encoder.rb
40
39
  - lib/blurhash_ruby.rb
40
+ - run.sh
41
41
  homepage: https://rubygems.org/gems/blurhash_ruby
42
42
  licenses:
43
43
  - MIT
@@ -1,15 +0,0 @@
1
- require File.expand_path('lib/blurhash_decoder/binary/blurhash_decoder')
2
- require 'base64'
3
-
4
- class BlurhashDecoder
5
- include DECODER
6
-
7
- def decode_blurhash(blurhash, height, width, punch)
8
- decode(blurhash, height, width, punch)
9
- base64_image = File.open("tmp/out.png", "rb") do |file|
10
- Base64.strict_encode64(file.read)
11
- end
12
- base64_image
13
- end
14
-
15
- end
@@ -1,12 +0,0 @@
1
- require 'open-uri'
2
- require File.expand_path('lib/blurhash_encoder/binary/blurhash_encoder')
3
-
4
- class BlurhashEncoder
5
- include ENCODER
6
-
7
- def blurhash(image_url, x, y)
8
- File.write 'tmp/in.png', URI.open(image_url).read
9
- encode(x,y)
10
- end
11
-
12
- end