fast_resize 1.0.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.
Files changed (55) hide show
  1. checksums.yaml +7 -0
  2. data/CMakeLists.txt +308 -0
  3. data/LICENSE +29 -0
  4. data/README.md +426 -0
  5. data/VERSION +1 -0
  6. data/bindings/ruby/ext/fastresize/extconf.rb +152 -0
  7. data/bindings/ruby/ext/fastresize/fastresize_ext.cpp +377 -0
  8. data/bindings/ruby/lib/fastresize/platform.rb +106 -0
  9. data/bindings/ruby/lib/fastresize/version.rb +5 -0
  10. data/bindings/ruby/lib/fastresize.rb +13 -0
  11. data/bindings/ruby/prebuilt/linux-aarch64/bin/fast_resize +0 -0
  12. data/bindings/ruby/prebuilt/linux-aarch64/include/fastresize.h +189 -0
  13. data/bindings/ruby/prebuilt/linux-aarch64/include/stb_image.h +7988 -0
  14. data/bindings/ruby/prebuilt/linux-aarch64/include/stb_image_resize2.h +10651 -0
  15. data/bindings/ruby/prebuilt/linux-aarch64/include/stb_image_write.h +1724 -0
  16. data/bindings/ruby/prebuilt/linux-aarch64/lib/libfastresize.a +0 -0
  17. data/bindings/ruby/prebuilt/linux-aarch64.tar.gz +0 -0
  18. data/bindings/ruby/prebuilt/linux-x86_64/bin/fast_resize +0 -0
  19. data/bindings/ruby/prebuilt/linux-x86_64/include/fastresize.h +189 -0
  20. data/bindings/ruby/prebuilt/linux-x86_64/include/stb_image.h +7988 -0
  21. data/bindings/ruby/prebuilt/linux-x86_64/include/stb_image_resize2.h +10651 -0
  22. data/bindings/ruby/prebuilt/linux-x86_64/include/stb_image_write.h +1724 -0
  23. data/bindings/ruby/prebuilt/linux-x86_64/lib/libfastresize.a +0 -0
  24. data/bindings/ruby/prebuilt/linux-x86_64.tar.gz +0 -0
  25. data/bindings/ruby/prebuilt/macos-arm64/bin/fast_resize +0 -0
  26. data/bindings/ruby/prebuilt/macos-arm64/include/fastresize.h +189 -0
  27. data/bindings/ruby/prebuilt/macos-arm64/include/stb_image.h +7988 -0
  28. data/bindings/ruby/prebuilt/macos-arm64/include/stb_image_resize2.h +10651 -0
  29. data/bindings/ruby/prebuilt/macos-arm64/include/stb_image_write.h +1724 -0
  30. data/bindings/ruby/prebuilt/macos-arm64/lib/libfastresize.a +0 -0
  31. data/bindings/ruby/prebuilt/macos-arm64.tar.gz +0 -0
  32. data/bindings/ruby/prebuilt/macos-x86_64/bin/fast_resize +0 -0
  33. data/bindings/ruby/prebuilt/macos-x86_64/include/fastresize.h +189 -0
  34. data/bindings/ruby/prebuilt/macos-x86_64/include/stb_image.h +7988 -0
  35. data/bindings/ruby/prebuilt/macos-x86_64/include/stb_image_resize2.h +10651 -0
  36. data/bindings/ruby/prebuilt/macos-x86_64/include/stb_image_write.h +1724 -0
  37. data/bindings/ruby/prebuilt/macos-x86_64/lib/libfastresize.a +0 -0
  38. data/bindings/ruby/prebuilt/macos-x86_64.tar.gz +0 -0
  39. data/include/fastresize.h +189 -0
  40. data/include/stb_image.h +7988 -0
  41. data/include/stb_image_resize2.h +10651 -0
  42. data/include/stb_image_write.h +1724 -0
  43. data/src/cli.cpp +540 -0
  44. data/src/decoder.cpp +647 -0
  45. data/src/encoder.cpp +376 -0
  46. data/src/fastresize.cpp +445 -0
  47. data/src/internal.h +108 -0
  48. data/src/pipeline.cpp +284 -0
  49. data/src/pipeline.h +175 -0
  50. data/src/resizer.cpp +199 -0
  51. data/src/simd_resize.cpp +384 -0
  52. data/src/simd_resize.h +72 -0
  53. data/src/simd_utils.h +127 -0
  54. data/src/thread_pool.cpp +232 -0
  55. metadata +158 -0
@@ -0,0 +1,232 @@
1
+ /*
2
+ * FastResize - The Fastest Image Resizing Library On The Planet
3
+ * Copyright (C) 2025 Tran Huu Canh (0xTh3OKrypt) and FastResize Contributors
4
+ *
5
+ * Resize 1,000 images in 2 seconds. Up to 2.9x faster than libvips,
6
+ * 3.1x faster than imageflow. Uses 3-4x less RAM than alternatives.
7
+ *
8
+ * Author: Tran Huu Canh (0xTh3OKrypt)
9
+ * Email: tranhuucanh39@gmail.com
10
+ * Homepage: https://github.com/tranhuucanh/fast_resize
11
+ *
12
+ * BSD 3-Clause License
13
+ *
14
+ * Redistribution and use in source and binary forms, with or without
15
+ * modification, are permitted provided that the following conditions are met:
16
+ *
17
+ * 1. Redistributions of source code must retain the above copyright notice,
18
+ * this list of conditions and the following disclaimer.
19
+ *
20
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
21
+ * this list of conditions and the following disclaimer in the documentation
22
+ * and/or other materials provided with the distribution.
23
+ *
24
+ * 3. Neither the name of the copyright holder nor the names of its
25
+ * contributors may be used to endorse or promote products derived from
26
+ * this software without specific prior written permission.
27
+ *
28
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
29
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
32
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
38
+ * THE POSSIBILITY OF SUCH DAMAGE.
39
+ */
40
+
41
+ #include "internal.h"
42
+ #include <thread>
43
+ #include <mutex>
44
+ #include <condition_variable>
45
+ #include <queue>
46
+ #include <functional>
47
+ #include <atomic>
48
+ #include <vector>
49
+
50
+ namespace fastresize {
51
+ namespace internal {
52
+
53
+ class ThreadPool {
54
+ public:
55
+ explicit ThreadPool(size_t num_threads);
56
+ ~ThreadPool();
57
+
58
+ void enqueue(std::function<void()> task);
59
+
60
+ void wait();
61
+
62
+ size_t get_thread_count() const { return threads_.size(); }
63
+
64
+ private:
65
+ std::vector<std::thread> threads_;
66
+ std::queue<std::function<void()>> tasks_;
67
+ std::mutex queue_mutex_;
68
+ std::condition_variable condition_;
69
+ std::condition_variable wait_condition_;
70
+ std::atomic<bool> stop_;
71
+ std::atomic<int> active_tasks_;
72
+ std::atomic<int> queued_tasks_;
73
+ };
74
+
75
+ ThreadPool::ThreadPool(size_t num_threads)
76
+ : stop_(false)
77
+ , active_tasks_(0)
78
+ , queued_tasks_(0)
79
+ {
80
+ for (size_t i = 0; i < num_threads; ++i) {
81
+ threads_.emplace_back([this] {
82
+ while (true) {
83
+ std::function<void()> task;
84
+ {
85
+ std::unique_lock<std::mutex> lock(queue_mutex_);
86
+ condition_.wait(lock, [this] {
87
+ return stop_ || !tasks_.empty();
88
+ });
89
+
90
+ if (stop_ && tasks_.empty())
91
+ return;
92
+
93
+ task = std::move(tasks_.front());
94
+ tasks_.pop();
95
+ --queued_tasks_;
96
+ }
97
+
98
+ ++active_tasks_;
99
+ task();
100
+ --active_tasks_;
101
+ wait_condition_.notify_all();
102
+ }
103
+ });
104
+ }
105
+ }
106
+
107
+ ThreadPool::~ThreadPool() {
108
+ {
109
+ std::unique_lock<std::mutex> lock(queue_mutex_);
110
+ stop_ = true;
111
+ }
112
+ condition_.notify_all();
113
+ for (std::thread& thread : threads_) {
114
+ if (thread.joinable()) {
115
+ thread.join();
116
+ }
117
+ }
118
+ }
119
+
120
+ void ThreadPool::enqueue(std::function<void()> task) {
121
+ {
122
+ std::unique_lock<std::mutex> lock(queue_mutex_);
123
+ tasks_.emplace(std::move(task));
124
+ ++queued_tasks_;
125
+ }
126
+ condition_.notify_one();
127
+ }
128
+
129
+ void ThreadPool::wait() {
130
+ std::unique_lock<std::mutex> lock(queue_mutex_);
131
+ wait_condition_.wait(lock, [this] {
132
+ return queued_tasks_ == 0 && active_tasks_ == 0;
133
+ });
134
+ }
135
+
136
+ class BufferPool {
137
+ public:
138
+ BufferPool() = default;
139
+ ~BufferPool();
140
+
141
+ unsigned char* acquire(size_t size);
142
+ void release(unsigned char* buffer, size_t capacity);
143
+
144
+ private:
145
+ struct Buffer {
146
+ unsigned char* data;
147
+ size_t capacity;
148
+ };
149
+
150
+ std::vector<Buffer> pool_;
151
+ std::mutex mutex_;
152
+ };
153
+
154
+ BufferPool::~BufferPool() {
155
+ std::lock_guard<std::mutex> lock(mutex_);
156
+ for (Buffer& buf : pool_) {
157
+ delete[] buf.data;
158
+ }
159
+ pool_.clear();
160
+ }
161
+
162
+ unsigned char* BufferPool::acquire(size_t size) {
163
+ std::lock_guard<std::mutex> lock(mutex_);
164
+
165
+ for (auto it = pool_.begin(); it != pool_.end(); ++it) {
166
+ if (it->capacity >= size) {
167
+ unsigned char* buffer = it->data;
168
+ pool_.erase(it);
169
+ return buffer;
170
+ }
171
+ }
172
+
173
+ return new unsigned char[size];
174
+ }
175
+
176
+ void BufferPool::release(unsigned char* buffer, size_t capacity) {
177
+ if (!buffer) return;
178
+
179
+ std::lock_guard<std::mutex> lock(mutex_);
180
+
181
+ if (pool_.size() < 32) {
182
+ Buffer buf;
183
+ buf.data = buffer;
184
+ buf.capacity = capacity;
185
+ pool_.push_back(buf);
186
+ } else {
187
+ delete[] buffer;
188
+ }
189
+ }
190
+
191
+ ThreadPool* create_thread_pool(size_t num_threads) {
192
+ return new ThreadPool(num_threads);
193
+ }
194
+
195
+ void destroy_thread_pool(ThreadPool* pool) {
196
+ delete pool;
197
+ }
198
+
199
+ void thread_pool_enqueue(ThreadPool* pool, std::function<void()> task) {
200
+ if (pool) {
201
+ pool->enqueue(std::move(task));
202
+ }
203
+ }
204
+
205
+ void thread_pool_wait(ThreadPool* pool) {
206
+ if (pool) {
207
+ pool->wait();
208
+ }
209
+ }
210
+
211
+ BufferPool* create_buffer_pool() {
212
+ return new BufferPool();
213
+ }
214
+
215
+ void destroy_buffer_pool(BufferPool* pool) {
216
+ delete pool;
217
+ }
218
+
219
+ unsigned char* buffer_pool_acquire(BufferPool* pool, size_t size) {
220
+ return pool ? pool->acquire(size) : new unsigned char[size];
221
+ }
222
+
223
+ void buffer_pool_release(BufferPool* pool, unsigned char* buffer, size_t capacity) {
224
+ if (pool) {
225
+ pool->release(buffer, capacity);
226
+ } else {
227
+ delete[] buffer;
228
+ }
229
+ }
230
+
231
+ }
232
+ }
metadata ADDED
@@ -0,0 +1,158 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fast_resize
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Tran Huu Canh (0xTh3OKrypt)
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2025-12-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '13.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '13.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake-compiler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.2'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: benchmark-ips
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.0'
69
+ description: Resize 1,000 images in 2 seconds. Up to 2.9x faster than libvips, 3.1x
70
+ faster than imageflow. Uses 3-4x less RAM than alternatives.
71
+ email:
72
+ - tranhuucanh39@gmail.com
73
+ executables: []
74
+ extensions:
75
+ - bindings/ruby/ext/fastresize/extconf.rb
76
+ extra_rdoc_files: []
77
+ files:
78
+ - CMakeLists.txt
79
+ - LICENSE
80
+ - README.md
81
+ - VERSION
82
+ - bindings/ruby/ext/fastresize/extconf.rb
83
+ - bindings/ruby/ext/fastresize/fastresize_ext.cpp
84
+ - bindings/ruby/lib/fastresize.rb
85
+ - bindings/ruby/lib/fastresize/platform.rb
86
+ - bindings/ruby/lib/fastresize/version.rb
87
+ - bindings/ruby/prebuilt/linux-aarch64.tar.gz
88
+ - bindings/ruby/prebuilt/linux-aarch64/bin/fast_resize
89
+ - bindings/ruby/prebuilt/linux-aarch64/include/fastresize.h
90
+ - bindings/ruby/prebuilt/linux-aarch64/include/stb_image.h
91
+ - bindings/ruby/prebuilt/linux-aarch64/include/stb_image_resize2.h
92
+ - bindings/ruby/prebuilt/linux-aarch64/include/stb_image_write.h
93
+ - bindings/ruby/prebuilt/linux-aarch64/lib/libfastresize.a
94
+ - bindings/ruby/prebuilt/linux-x86_64.tar.gz
95
+ - bindings/ruby/prebuilt/linux-x86_64/bin/fast_resize
96
+ - bindings/ruby/prebuilt/linux-x86_64/include/fastresize.h
97
+ - bindings/ruby/prebuilt/linux-x86_64/include/stb_image.h
98
+ - bindings/ruby/prebuilt/linux-x86_64/include/stb_image_resize2.h
99
+ - bindings/ruby/prebuilt/linux-x86_64/include/stb_image_write.h
100
+ - bindings/ruby/prebuilt/linux-x86_64/lib/libfastresize.a
101
+ - bindings/ruby/prebuilt/macos-arm64.tar.gz
102
+ - bindings/ruby/prebuilt/macos-arm64/bin/fast_resize
103
+ - bindings/ruby/prebuilt/macos-arm64/include/fastresize.h
104
+ - bindings/ruby/prebuilt/macos-arm64/include/stb_image.h
105
+ - bindings/ruby/prebuilt/macos-arm64/include/stb_image_resize2.h
106
+ - bindings/ruby/prebuilt/macos-arm64/include/stb_image_write.h
107
+ - bindings/ruby/prebuilt/macos-arm64/lib/libfastresize.a
108
+ - bindings/ruby/prebuilt/macos-x86_64.tar.gz
109
+ - bindings/ruby/prebuilt/macos-x86_64/bin/fast_resize
110
+ - bindings/ruby/prebuilt/macos-x86_64/include/fastresize.h
111
+ - bindings/ruby/prebuilt/macos-x86_64/include/stb_image.h
112
+ - bindings/ruby/prebuilt/macos-x86_64/include/stb_image_resize2.h
113
+ - bindings/ruby/prebuilt/macos-x86_64/include/stb_image_write.h
114
+ - bindings/ruby/prebuilt/macos-x86_64/lib/libfastresize.a
115
+ - include/fastresize.h
116
+ - include/stb_image.h
117
+ - include/stb_image_resize2.h
118
+ - include/stb_image_write.h
119
+ - src/cli.cpp
120
+ - src/decoder.cpp
121
+ - src/encoder.cpp
122
+ - src/fastresize.cpp
123
+ - src/internal.h
124
+ - src/pipeline.cpp
125
+ - src/pipeline.h
126
+ - src/resizer.cpp
127
+ - src/simd_resize.cpp
128
+ - src/simd_resize.h
129
+ - src/simd_utils.h
130
+ - src/thread_pool.cpp
131
+ homepage: https://github.com/tranhuucanh/fast_resize
132
+ licenses:
133
+ - BSD-3-Clause
134
+ metadata:
135
+ bug_tracker_uri: https://github.com/tranhuucanh/fast_resize/issues
136
+ changelog_uri: https://github.com/tranhuucanh/fast_resize/blob/master/CHANGELOG.md
137
+ documentation_uri: https://github.com/tranhuucanh/fast_resize
138
+ source_code_uri: https://github.com/tranhuucanh/fast_resize
139
+ post_install_message:
140
+ rdoc_options: []
141
+ require_paths:
142
+ - bindings/ruby/lib
143
+ required_ruby_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ version: 2.5.0
148
+ required_rubygems_version: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ requirements: []
154
+ rubygems_version: 3.4.19
155
+ signing_key:
156
+ specification_version: 4
157
+ summary: The fastest image resizing library on the planet.
158
+ test_files: []