rszr 0.3.2 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 430e9433e3e7d2564aa86a124e41b5285ffabf66
4
- data.tar.gz: 4477e613b5b98f39f1635cd669d1780e66391f64
3
+ metadata.gz: f6ab5d9447d5919f0bed35e0562f7645bb5a23db
4
+ data.tar.gz: 88988dc45c31575db6a1cf11618298be6641fca8
5
5
  SHA512:
6
- metadata.gz: 9ff2d67706cc334f496af3b522f9071809d774aaac4883bd6a948f27b1f0dc44d11b63c42834d9f2885d27a2d83f1c639e7a372da281b2b98482aee7c87360da
7
- data.tar.gz: 3fdf4147d6a4bcb6c99e6feefc017bdcca3b63e1b4b0dc28e90661acdc2d6e9f955669f85426ba590087195cc35332f14d4f67f73be052909ae66281885b55c0
6
+ metadata.gz: 4a5d7d5ba83088cce7de9af7a0d68c92d2e2c0f58029617dfa1c270a83aa58af96168b11b2bebdd0c3ad1a930598726acd0809b5b3a7dea901567402bf23ed1b
7
+ data.tar.gz: 1a53df4fcbd02f524d42cfadcd9dbacdba3945423f5a244af8da190e4cc59de2b87b267de78b0481c4c8d106d27dc16a2ec30f4f728310eced9e9da8a0191f0f
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  [![Gem Version](https://badge.fury.io/rb/rszr.svg)](http://badge.fury.io/rb/rszr) [![Build Status](https://travis-ci.org/mtgrosser/rszr.svg)](https://travis-ci.org/mtgrosser/rszr)
2
- # Rszr
2
+ # Rszr - fast image resizer for Ruby
3
3
 
4
- Rszr is a fast image resizer.
4
+ Rszr is an image resizer for Ruby based on the Imlib2 library. It is faster and consumes less memory than MiniMagick, rmagick and GD2.
5
5
 
6
6
  ## Installation
7
7
 
@@ -23,7 +23,7 @@ Using homebrew:
23
23
  brew install imlib2
24
24
  ```
25
25
 
26
- ### Linux
26
+ #### Linux
27
27
 
28
28
  Using your favourite package manager:
29
29
 
@@ -66,12 +66,17 @@ image.height => 300
66
66
  image.dimensions => [400, 300]
67
67
  ```
68
68
 
69
+ ## Thread safety
70
+
71
+ As of version 0.4.0, Rszr provides thread safety by synchronizing access to imlib2 function calls.
72
+ Use of any previous versions in a threaded environment is discouraged.
73
+
69
74
  ## Speed
70
75
 
71
76
  Resizing an 1500x997 JPEG image to 800x532, 100 times:
72
77
 
73
78
  Library | Time
74
79
  ----------------|-----------
75
- MiniMagick | 11.4 s
76
- GD2 | 7.2 s
77
- Rszr | 3.2 s
80
+ MiniMagick | 12.9 s
81
+ GD2 | 7.5 s
82
+ Rszr | 2.8 s
@@ -6,6 +6,7 @@ require 'pathname'
6
6
  require 'rszr/version'
7
7
  require 'rszr/errors'
8
8
  require 'rszr/lib'
9
+ require 'rszr/lock'
9
10
  require 'rszr/handle'
10
11
  require 'rszr/base'
11
12
  require 'rszr/image'
@@ -4,6 +4,8 @@ module Rszr
4
4
  def self.included(base)
5
5
  Rszr::Lib.delegate(base)
6
6
  Rszr::Lib.delegate(base.singleton_class)
7
+ base.include(Lock)
8
+ base.extend(Lock)
7
9
  end
8
10
 
9
11
  protected
@@ -8,7 +8,7 @@ module Rszr
8
8
  protected :instantiate
9
9
 
10
10
  def new(width, height)
11
- ptr = imlib_create_image(width, height)
11
+ ptr = with_lock { imlib_create_image(width, height) }
12
12
  raise Error, 'Could not instantiate image' if ptr.null?
13
13
  instantiate(ptr)
14
14
  end
@@ -17,8 +17,10 @@ module Rszr
17
17
  path = path.to_s
18
18
  raise FileNotFound unless File.exist?(path)
19
19
  load_error = LoadError.new
20
- imlib_set_cache_size(0)
21
- ptr = imlib_load_image_with_error_return(path, load_error.ptr)
20
+ ptr = with_lock do
21
+ imlib_set_cache_size(0)
22
+ imlib_load_image_with_error_return(path, load_error.ptr)
23
+ end
22
24
  raise load_error, load_error.message if ptr.null?
23
25
  return instantiate(ptr)
24
26
  end
@@ -27,8 +29,10 @@ module Rszr
27
29
  protected
28
30
 
29
31
  def finalize(ptr)
30
- imlib_context_set_image(ptr)
31
- imlib_free_image
32
+ with_lock do
33
+ imlib_context_set_image(ptr)
34
+ imlib_free_image
35
+ end
32
36
  end
33
37
 
34
38
  end
@@ -38,13 +42,11 @@ module Rszr
38
42
  end
39
43
 
40
44
  def width
41
- context_set_image
42
- imlib_image_get_width
45
+ with_image { imlib_image_get_width }
43
46
  end
44
47
 
45
48
  def height
46
- context_set_image
47
- imlib_image_get_height
49
+ with_image { imlib_image_get_height }
48
50
  end
49
51
 
50
52
  def dimensions
@@ -52,8 +54,7 @@ module Rszr
52
54
  end
53
55
 
54
56
  def format
55
- context_set_image
56
- str_ptr = imlib_image_format
57
+ str_ptr = with_image { imlib_image_format }
57
58
  return if str_ptr.null?
58
59
  str_ptr.to_s
59
60
  end
@@ -77,13 +78,14 @@ module Rszr
77
78
  end
78
79
 
79
80
  def save(path, format = nil)
80
- context_set_image
81
- format ||= format_from_filename(path) || 'jpg'
82
- imlib_image_set_format(format)
83
- save_error = SaveError.new
84
- imlib_save_image_with_error_return(path, save_error.ptr)
85
- raise save_error, save_error.message if save_error.error?
86
- true
81
+ with_image do
82
+ format ||= format_from_filename(path) || 'jpg'
83
+ imlib_image_set_format(format)
84
+ save_error = SaveError.new
85
+ imlib_save_image_with_error_return(path, save_error.ptr)
86
+ raise save_error, save_error.message if save_error.error?
87
+ true
88
+ end
87
89
  end
88
90
 
89
91
  def inspect
@@ -103,35 +105,35 @@ module Rszr
103
105
  def create_resized_image(*args)
104
106
  options = args.last.is_a?(Hash) ? args.pop : {}
105
107
  assert_valid_keys options, :crop, :background, :skew #:extend, :width, :height, :max_width, :max_height, :box
106
- context_set_image
108
+ original_width, original_height = width, height
107
109
  x, y, = 0, 0
108
110
  if args.size == 1
109
111
  scale = args.first
110
112
  raise ArgumentError, "scale #{scale.inspect} out of range" unless scale > 0 && scale < 1
111
- new_width = width.to_f * scale
112
- new_height = height.to_f * scale
113
+ new_width = original_width.to_f * scale
114
+ new_height = original_height.to_f * scale
113
115
  elsif args.size == 2
114
116
  box_width, box_height = args
115
117
  if :auto == box_width && box_height.is_a?(Numeric)
116
118
  new_height = box_height
117
- new_width = box_height.to_f / height.to_f * width.to_f
119
+ new_width = box_height.to_f / original_height.to_f * original_width.to_f
118
120
  elsif box_width.is_a?(Numeric) && :auto == box_height
119
121
  new_width = box_width
120
- new_height = box_width.to_f / width.to_f * height.to_f
122
+ new_height = box_width.to_f / original_width.to_f * original_height.to_f
121
123
  elsif box_width.is_a?(Numeric) && box_height.is_a?(Numeric)
122
124
  if options[:skew]
123
125
  new_width, new_height = box_width, box_height
124
126
  elsif options[:crop]
125
127
  # TODO: calculate x, y offset if crop
126
128
  else
127
- scale = width.to_f / height.to_f
129
+ scale = original_width.to_f / original_height.to_f
128
130
  box_scale = box_width.to_f / box_height.to_f
129
131
  if scale >= box_scale # wider
130
132
  new_width = box_width
131
- new_height = height.to_f * box_width.to_f / width.to_f
133
+ new_height = original_height.to_f * box_width.to_f / original_width.to_f
132
134
  else # narrower
133
135
  new_height = box_height
134
- new_width = width.to_f * box_height.to_f / height.to_f
136
+ new_width = original_width.to_f * box_height.to_f / original_height.to_f
135
137
  end
136
138
  end
137
139
  else
@@ -140,29 +142,16 @@ module Rszr
140
142
  else
141
143
  raise ArgumentError, "wrong number of arguments (#{args.size} for 1..2)"
142
144
  end
143
-
144
- #new_width = options[:width] || imlib_image_get_width
145
- #new_height = options[:height] || imlib_image_get_height
146
- #if max_width = options[:max_width] and new_width > max_width
147
- # scale = max_width.to_f / new_width.to_f
148
- # width = max_width
149
- # new_height = (scale * new_height).to_i
150
- #end
151
- #if max_height = options[:max_height] and new_height > max_height
152
- # scale = max_height.to_f / new_height.to_f
153
- # new_height = max_height
154
- # new_width = (scale * new_width).to_i
155
- #end
156
-
157
- imlib_context_set_anti_alias(1)
158
- resized_ptr = imlib_create_cropped_scaled_image(x, y, imlib_image_get_width, imlib_image_get_height, new_width.round, new_height.round)
145
+ resized_ptr = with_image do
146
+ imlib_context_set_anti_alias(1)
147
+ imlib_create_cropped_scaled_image(x, y, imlib_image_get_width, imlib_image_get_height, new_width.round, new_height.round)
148
+ end
159
149
  raise TransformationError, "error resizing image" if resized_ptr.null?
160
150
  resized_ptr
161
151
  end
162
152
 
163
153
  def create_cropped_image(x, y, width, height)
164
- context_set_image
165
- cropped_ptr = imlib_create_cropped_image(x, y, width, height)
154
+ cropped_ptr = with_image { imlib_create_cropped_image(x, y, width, height) }
166
155
  raise TransformationError, 'error cropping image' if cropped_ptr.null?
167
156
  cropped_ptr
168
157
  end
@@ -175,6 +164,13 @@ module Rszr
175
164
  imlib_context_set_image(ptr)
176
165
  end
177
166
 
167
+ def with_image
168
+ with_lock do
169
+ context_set_image
170
+ yield
171
+ end
172
+ end
173
+
178
174
  def instantiate(ptr)
179
175
  self.class.send(:instantiate, ptr)
180
176
  end
@@ -0,0 +1,23 @@
1
+ module Rszr
2
+ @mutex = Mutex.new
3
+
4
+ class << self
5
+
6
+ def with_lock(&block) # :nodoc:
7
+ mutex.synchronize(&block)
8
+ end
9
+
10
+ private
11
+
12
+ def mutex
13
+ @mutex
14
+ end
15
+ end
16
+
17
+ module Lock # :nodoc:
18
+ def with_lock(&block)
19
+ Rszr.with_lock(&block)
20
+ end
21
+ end
22
+
23
+ end
@@ -1,3 +1,3 @@
1
1
  module Rszr
2
- VERSION = "0.3.2"
2
+ VERSION = "0.4.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rszr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthias Grosser
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-26 00:00:00.000000000 Z
11
+ date: 2019-03-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -124,6 +124,7 @@ files:
124
124
  - lib/rszr/handle.rb
125
125
  - lib/rszr/image.rb
126
126
  - lib/rszr/lib.rb
127
+ - lib/rszr/lock.rb
127
128
  - lib/rszr/version.rb
128
129
  homepage: https://github.com/mtgrosser/rszr
129
130
  licenses:
@@ -145,7 +146,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
145
146
  version: '0'
146
147
  requirements: []
147
148
  rubyforge_project:
148
- rubygems_version: 2.4.5
149
+ rubygems_version: 2.5.2.3
149
150
  signing_key:
150
151
  specification_version: 4
151
152
  summary: Fast image resizer