fast_thumbhash 0.3.0 → 0.4.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: 58ceda168edb83030206e690750cd035f7de93474447f15b7690934a9471da3a
4
- data.tar.gz: c88f2732b5a0614e92b28980dab11a48dbc8ecf813857bf572a6f3d667695151
3
+ metadata.gz: 60904812112ec4cbab475e36e04f7634c96cb36946d5c3e178ccbfe1ca985727
4
+ data.tar.gz: d195d956dcbc219971be4d25fbb486fb5d61515a1a9ea33c03ec184bb97c4a5a
5
5
  SHA512:
6
- metadata.gz: dc0bfb9cdf1206f0949ad46589bd8cecfe5c59bac958c1d812239cae38618d5cfe3ea6a61386ab9028386ff452472a37c3cc45082b6d462aebb8b7f542077126
7
- data.tar.gz: b2449a53c70003ea2ddef3701620e8021140f33d15c1fa9ca96225b553989ed9e661e093adbba66f89377a6294f19fe9b34199409e207cf6bc7cf1d4cc3a72ff
6
+ metadata.gz: ae83b2dd113977b72d4b89207c41119038b062b73d4d07e67864391764f6a6d3ef2791ac323c8ef2851a83c57c3cf9cefaca1c8feca6f717bcfe30ce177dcdb3
7
+ data.tar.gz: 3ffb0c9274880eb74092c866b1a618ca315a94348c4725f1e64ebba474982708e9d99f6d30e88d76352a8d6f3cad45ccabc92747e2e3d743c94baa6eeee83b14
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- fast_thumbhash (0.3.0)
4
+ fast_thumbhash (0.4.0)
5
5
  ffi
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -92,7 +92,7 @@ w, h, rgba = described_class.thumbhash_to_rgba(
92
92
 
93
93
  ##### `fill_mode`
94
94
 
95
- The `fill_mode` option specifies how to fill in any transparent areas in your image with a color of your choice. When `fill_mode` is `:solid`, you need to pass an additional `fill_color` option to specify the actual RGBA color to use. When `fill_mode` is `:blur`, the excess space will be filled with a blurred version of the original image itself.
95
+ The `fill_mode` option specifies how to fill in any transparent areas in your image with a color of your choice. When `fill_mode` is `:solid`, you need to pass an additional `fill_color` option to specify the actual RGBA color to use. When `fill_mode` is `:blur`, the excess space will be filled with a blurred version of the original image itself. When `fill_image` is `:clamp`, the excess space is filled with extended pixels from the edge of the image.
96
96
 
97
97
  ```ruby
98
98
  w, h, rgba = described_class.thumbhash_to_rgba(
@@ -379,7 +379,7 @@ void thumbhash_to_rgba(
379
379
 
380
380
  double r, g, b, a;
381
381
 
382
- if (fill_mode == BLUR) {
382
+ if (fill_mode == CLAMP) {
383
383
  if (x < 0)
384
384
  {
385
385
  x = 0;
@@ -398,7 +398,9 @@ void thumbhash_to_rgba(
398
398
  }
399
399
  }
400
400
 
401
- if (x >= 0 && x <= 1.0 && y >= 0 && y <= 1.0) {
401
+ bool inside_image = x >= 0 && x <= 1.0 && y >= 0 && y <= 1.0;
402
+
403
+ if (inside_image) {
402
404
  double l = l_dc, p = p_dc, q = q_dc;
403
405
  a = a_dc;
404
406
 
@@ -453,9 +455,9 @@ void thumbhash_to_rgba(
453
455
  r = (3.0 * l - b + q) / 2.0;
454
456
  g = r - q;
455
457
  } else {
456
- r = 0;
457
- g = 0;
458
- b = 0;
458
+ r = 255;
459
+ g = 255;
460
+ b = 255;
459
461
  a = 0;
460
462
  }
461
463
 
@@ -466,14 +468,17 @@ void thumbhash_to_rgba(
466
468
  fmax(0, 255 * fmin(1, a))
467
469
  };
468
470
 
469
- if (fill_mode == SOLID) {
470
- // Alpha-blending
471
- double top_alpha = (double) top[3] / 255.0;
472
-
473
- rgba[i] = roundf(top[0] * top_alpha + fill_color[0] * (1.0 - top_alpha));
474
- rgba[i+1] = roundf(top[1] * top_alpha + fill_color[1] * (1.0 - top_alpha));
475
- rgba[i+2] = roundf(top[2] * top_alpha + fill_color[2] * (1.0 - top_alpha));
476
- rgba[i+3] = roundf(top[3] * top_alpha + fill_color[3] * (1.0 - top_alpha));
471
+ if (fill_color && fill_color[3] > 0) {
472
+ double top_a = (double) top[3] / 255.0;
473
+ double fill_color_a = (double) fill_color[3] / 255.0;
474
+ double inverse_top_a = 1.0 - top_a;
475
+ double sum_a = top_a + fill_color_a * inverse_top_a;
476
+
477
+ // Alpha compositing (top over fill_color)
478
+ rgba[i] = roundf(((double) top[0] * top_a + (double) fill_color[0] * fill_color_a * inverse_top_a) / sum_a);
479
+ rgba[i+1] = roundf(((double) top[1] * top_a + (double) fill_color[1] * fill_color_a * inverse_top_a) / sum_a);
480
+ rgba[i+2] = roundf(((double) top[2] * top_a + (double) fill_color[2] * fill_color_a * inverse_top_a) / sum_a);
481
+ rgba[i+3] = roundf(sum_a * 255.0);
477
482
  } else {
478
483
  rgba[i] = top[0];
479
484
  rgba[i+1] = top[1];
@@ -4,9 +4,9 @@
4
4
  #include <stdint.h>
5
5
 
6
6
  enum FillMode {
7
- NO_FILL = 0,
8
7
  SOLID = 1,
9
8
  BLUR = 2,
9
+ CLAMP = 3,
10
10
  };
11
11
 
12
12
  void rgba_to_thumbhash(
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FastThumbhash
4
- VERSION = "0.3.0"
4
+ VERSION = "0.4.0"
5
5
  end
@@ -8,8 +8,8 @@ module FastThumbhash
8
8
  thumbhash,
9
9
  max_size: nil,
10
10
  size: nil,
11
- fill_mode: :no_fill,
12
- fill_color: nil,
11
+ fill_mode: :solid,
12
+ fill_color: [255, 255, 255, 0],
13
13
  homogeneous_transform: nil,
14
14
  saturation: 0
15
15
  )
@@ -28,22 +28,19 @@ module FastThumbhash
28
28
  binary_thumbhash,
29
29
  max_size: nil,
30
30
  size: nil,
31
- fill_mode: :no_fill,
32
- fill_color: nil,
31
+ fill_mode: :solid,
32
+ fill_color: [255, 255, 255, 0],
33
33
  homogeneous_transform: nil,
34
34
  saturation: 0
35
35
  )
36
36
  !max_size.nil? ^ !size.nil? or
37
37
  raise ArgumentError, "Pass either the `max_size` option, or an explicit `size`"
38
38
 
39
- %i[solid blur no_fill].include?(fill_mode) or
39
+ %i[solid blur clamp].include?(fill_mode) or
40
40
  raise ArgumentError, "Invalid `fill_mode` option"
41
41
 
42
42
  fill_color_pointer =
43
- if fill_mode == :solid
44
- fill_color or
45
- raise ArgumentError, "`fill_color` is required if fill_mode = :solid"
46
-
43
+ if fill_color
47
44
  fill_color.length == 4 or
48
45
  raise ArgumentError, "You need to pass [r, g, b, a] to the `fill_color` option"
49
46
 
@@ -52,6 +49,8 @@ module FastThumbhash
52
49
  end
53
50
  end
54
51
 
52
+ raise ArgumentError, "Option `fill_color` is required for :solid fill_mode" if fill_mode == :solid && fill_color.nil?
53
+
55
54
  transform_pointer =
56
55
  if homogeneous_transform
57
56
  (homogeneous_transform.size == 3 && homogeneous_transform.all? { |row| row.size == 3 }) or
@@ -143,9 +142,9 @@ module FastThumbhash
143
142
  ffi_lib File.join(File.expand_path(__dir__), "fast_thumbhash.#{RbConfig::CONFIG["DLEXT"]}")
144
143
 
145
144
  enum :fill_mode, [
146
- :no_fill, 0,
147
- :solid,
148
- :blur
145
+ :solid, 0,
146
+ :blur,
147
+ :clamp
149
148
  ]
150
149
 
151
150
  attach_function :thumb_size, %i[pointer uint8 pointer], :size_t
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fast_thumbhash
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stefano Verna