fast_resize 1.0.1 → 1.0.3

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 (37) hide show
  1. checksums.yaml +4 -4
  2. data/VERSION +1 -1
  3. data/bindings/ruby/ext/fastresize/extconf.rb +79 -80
  4. data/bindings/ruby/lib/fastresize/platform.rb +102 -56
  5. data/bindings/ruby/lib/fastresize/version.rb +1 -1
  6. data/bindings/ruby/lib/fastresize.rb +321 -6
  7. data/bindings/ruby/prebuilt/linux-aarch64/bin/fast_resize +0 -0
  8. data/bindings/ruby/prebuilt/linux-aarch64/lib/libfastresize.a +0 -0
  9. data/bindings/ruby/prebuilt/linux-aarch64.tar.gz +0 -0
  10. data/bindings/ruby/prebuilt/linux-x86_64/bin/fast_resize +0 -0
  11. data/bindings/ruby/prebuilt/linux-x86_64/lib/libfastresize.a +0 -0
  12. data/bindings/ruby/prebuilt/linux-x86_64.tar.gz +0 -0
  13. data/bindings/ruby/prebuilt/macos-arm64/bin/fast_resize +0 -0
  14. data/bindings/ruby/prebuilt/macos-arm64/lib/libfastresize.a +0 -0
  15. data/bindings/ruby/prebuilt/macos-arm64.tar.gz +0 -0
  16. data/bindings/ruby/prebuilt/macos-x86_64/bin/fast_resize +0 -0
  17. data/bindings/ruby/prebuilt/macos-x86_64/lib/libfastresize.a +0 -0
  18. data/bindings/ruby/prebuilt/macos-x86_64.tar.gz +0 -0
  19. metadata +4 -22
  20. data/CMakeLists.txt +0 -308
  21. data/bindings/ruby/ext/fastresize/fastresize_ext.cpp +0 -377
  22. data/include/fastresize.h +0 -189
  23. data/include/stb_image.h +0 -7988
  24. data/include/stb_image_resize2.h +0 -10651
  25. data/include/stb_image_write.h +0 -1724
  26. data/src/cli.cpp +0 -540
  27. data/src/decoder.cpp +0 -647
  28. data/src/encoder.cpp +0 -376
  29. data/src/fastresize.cpp +0 -445
  30. data/src/internal.h +0 -108
  31. data/src/pipeline.cpp +0 -284
  32. data/src/pipeline.h +0 -175
  33. data/src/resizer.cpp +0 -199
  34. data/src/simd_resize.cpp +0 -384
  35. data/src/simd_resize.h +0 -72
  36. data/src/simd_utils.h +0 -127
  37. data/src/thread_pool.cpp +0 -232
data/CMakeLists.txt DELETED
@@ -1,308 +0,0 @@
1
- cmake_minimum_required(VERSION 3.15)
2
- project(fast_resize VERSION 1.0.1 LANGUAGES CXX C)
3
-
4
- set(CMAKE_CXX_STANDARD 14)
5
- set(CMAKE_CXX_STANDARD_REQUIRED ON)
6
- set(CMAKE_POSITION_INDEPENDENT_CODE ON)
7
-
8
- # Optimization flags
9
- if(NOT CMAKE_BUILD_TYPE)
10
- set(CMAKE_BUILD_TYPE Release)
11
- endif()
12
-
13
- set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG -flto")
14
- set(CMAKE_C_FLAGS_RELEASE "-O3 -DNDEBUG -flto")
15
-
16
- # Option for building shared vs static libraries
17
- option(BUILD_SHARED_LIBS "Build shared libraries" OFF)
18
-
19
- # Platform-specific optimizations
20
- if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|amd64|AMD64")
21
- set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -march=native")
22
- set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -march=native")
23
- elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "arm64|aarch64|ARM64")
24
- # ARM-specific flags
25
- set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
26
- set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")
27
- endif()
28
-
29
- # Options
30
- option(FASTRESIZE_STATIC "Build static library" ON)
31
- option(FASTRESIZE_BUILD_TESTS "Build tests" ON)
32
- option(FASTRESIZE_BUILD_EXAMPLES "Build examples" ON)
33
- option(FASTRESIZE_BUILD_BENCHMARKS "Build benchmarks" ON)
34
- option(FASTRESIZE_BUILD_RUBY "Build Ruby binding" ON)
35
-
36
- # Find dependencies
37
- find_package(PkgConfig REQUIRED)
38
-
39
- # JPEG (libjpeg-turbo preferred)
40
- pkg_check_modules(JPEG libjpeg)
41
- if(NOT JPEG_FOUND)
42
- find_package(JPEG REQUIRED)
43
- endif()
44
-
45
- # PNG
46
- pkg_check_modules(PNG libpng)
47
- if(NOT PNG_FOUND)
48
- find_package(PNG REQUIRED)
49
- endif()
50
-
51
- # WebP
52
- pkg_check_modules(WEBP libwebp libwebpdecoder libwebpdemux libsharpyuv)
53
- if(NOT WEBP_FOUND)
54
- message(WARNING "libwebp not found, WEBP support will be limited")
55
- endif()
56
-
57
- # Source files
58
- set(FASTRESIZE_SOURCES
59
- src/fastresize.cpp
60
- src/decoder.cpp
61
- src/encoder.cpp
62
- src/resizer.cpp
63
- src/thread_pool.cpp
64
- src/pipeline.cpp
65
- src/simd_resize.cpp
66
- )
67
-
68
- # Create library
69
- add_library(fastresize ${FASTRESIZE_SOURCES})
70
-
71
- target_include_directories(fastresize
72
- PUBLIC
73
- $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
74
- $<INSTALL_INTERFACE:include>
75
- PRIVATE
76
- ${CMAKE_CURRENT_SOURCE_DIR}/src
77
- ${JPEG_INCLUDE_DIRS}
78
- ${PNG_INCLUDE_DIRS}
79
- ${WEBP_INCLUDE_DIRS}
80
- )
81
-
82
- # Link with threading library
83
- find_package(Threads REQUIRED)
84
-
85
- # Link libraries - improved static library finding like FastQR
86
- if(FASTRESIZE_STATIC OR NOT BUILD_SHARED_LIBS)
87
- message(STATUS "🔧 Configuring for static linking")
88
-
89
- # Find static libraries with explicit paths (like FastQR)
90
- find_library(JPEG_STATIC_LIBRARY
91
- NAMES libjpeg.a libturbojpeg.a
92
- PATHS
93
- /usr/local/lib
94
- /opt/homebrew/lib
95
- /usr/lib
96
- /usr/lib/x86_64-linux-gnu
97
- /usr/lib/aarch64-linux-gnu
98
- NO_DEFAULT_PATH
99
- )
100
-
101
- find_library(PNG_STATIC_LIBRARY
102
- NAMES libpng.a libpng16.a
103
- PATHS
104
- /usr/local/lib
105
- /opt/homebrew/lib
106
- /usr/lib
107
- /usr/lib/x86_64-linux-gnu
108
- /usr/lib/aarch64-linux-gnu
109
- NO_DEFAULT_PATH
110
- )
111
-
112
- find_library(WEBP_STATIC_LIBRARY
113
- NAMES libwebp.a
114
- PATHS
115
- /usr/local/lib
116
- /opt/homebrew/lib
117
- /usr/lib
118
- /usr/lib/x86_64-linux-gnu
119
- /usr/lib/aarch64-linux-gnu
120
- NO_DEFAULT_PATH
121
- )
122
-
123
- find_library(SHARPYUV_STATIC_LIBRARY
124
- NAMES libsharpyuv.a
125
- PATHS
126
- /usr/local/lib
127
- /opt/homebrew/lib
128
- /usr/lib
129
- /usr/lib/x86_64-linux-gnu
130
- /usr/lib/aarch64-linux-gnu
131
- NO_DEFAULT_PATH
132
- )
133
-
134
- find_library(ZLIB_STATIC_LIBRARY
135
- NAMES libz.a
136
- PATHS
137
- /usr/local/lib
138
- /usr/local/opt/zlib/lib
139
- /opt/homebrew/lib
140
- /opt/homebrew/opt/zlib/lib
141
- /usr/lib
142
- /usr/lib/x86_64-linux-gnu
143
- /usr/lib/aarch64-linux-gnu
144
- NO_DEFAULT_PATH
145
- )
146
-
147
- message(STATUS "📚 Static libraries found:")
148
- message(STATUS " JPEG: ${JPEG_STATIC_LIBRARY}")
149
- message(STATUS " PNG: ${PNG_STATIC_LIBRARY}")
150
- message(STATUS " WEBP: ${WEBP_STATIC_LIBRARY}")
151
- message(STATUS " SHARPYUV: ${SHARPYUV_STATIC_LIBRARY}")
152
- message(STATUS " ZLIB: ${ZLIB_STATIC_LIBRARY}")
153
-
154
- # Link with required static libraries (check if they exist first)
155
- if(JPEG_STATIC_LIBRARY AND PNG_STATIC_LIBRARY AND ZLIB_STATIC_LIBRARY)
156
- target_link_libraries(fastresize PRIVATE
157
- Threads::Threads
158
- ${JPEG_STATIC_LIBRARY}
159
- ${PNG_STATIC_LIBRARY}
160
- ${ZLIB_STATIC_LIBRARY}
161
- )
162
-
163
- if(WEBP_STATIC_LIBRARY)
164
- target_link_libraries(fastresize PRIVATE ${WEBP_STATIC_LIBRARY})
165
- if(SHARPYUV_STATIC_LIBRARY)
166
- target_link_libraries(fastresize PRIVATE ${SHARPYUV_STATIC_LIBRARY})
167
- endif()
168
- endif()
169
- else()
170
- message(WARNING "⚠️ Some static libraries not found, falling back to dynamic linking")
171
- message(WARNING " JPEG: ${JPEG_STATIC_LIBRARY}")
172
- message(WARNING " PNG: ${PNG_STATIC_LIBRARY}")
173
- message(WARNING " ZLIB: ${ZLIB_STATIC_LIBRARY}")
174
-
175
- target_link_libraries(fastresize PRIVATE
176
- Threads::Threads
177
- ${JPEG_LIBRARIES}
178
- ${PNG_LIBRARIES}
179
- )
180
-
181
- if(WEBP_FOUND)
182
- target_link_libraries(fastresize PRIVATE ${WEBP_LIBRARIES})
183
- endif()
184
- endif()
185
-
186
- # On Linux: ensure full static linking
187
- if(UNIX AND NOT APPLE)
188
- target_link_options(fastresize PRIVATE -static-libgcc -static-libstdc++)
189
- endif()
190
- else()
191
- message(STATUS "🔧 Configuring for dynamic linking")
192
- target_link_libraries(fastresize PRIVATE
193
- Threads::Threads
194
- ${JPEG_LIBRARIES}
195
- ${PNG_LIBRARIES}
196
- )
197
-
198
- if(WEBP_FOUND)
199
- target_link_libraries(fastresize PRIVATE ${WEBP_LIBRARIES})
200
- endif()
201
- endif()
202
-
203
- # ============================================================================
204
- # CLI Tool
205
- # ============================================================================
206
-
207
- option(FASTRESIZE_BUILD_CLI "Build CLI tool" ON)
208
-
209
- if(FASTRESIZE_BUILD_CLI)
210
- message(STATUS "🔧 Building CLI tool")
211
-
212
- # Read version from VERSION file
213
- file(READ "${CMAKE_CURRENT_SOURCE_DIR}/VERSION" FASTRESIZE_VERSION)
214
- string(STRIP "${FASTRESIZE_VERSION}" FASTRESIZE_VERSION)
215
-
216
- # CLI executable
217
- add_executable(fast_resize-cli
218
- src/cli.cpp
219
- )
220
-
221
- # Pass version to CLI
222
- target_compile_definitions(fast_resize-cli PRIVATE
223
- FASTRESIZE_VERSION="${FASTRESIZE_VERSION}"
224
- )
225
-
226
- # For standalone CLI binary, link statically
227
- if(FASTRESIZE_STATIC OR NOT BUILD_SHARED_LIBS)
228
- # Link CLI directly with library sources
229
- target_sources(fast_resize-cli PRIVATE ${FASTRESIZE_SOURCES})
230
-
231
- target_include_directories(fast_resize-cli PRIVATE
232
- ${CMAKE_CURRENT_SOURCE_DIR}/include
233
- ${CMAKE_CURRENT_SOURCE_DIR}/src
234
- ${JPEG_INCLUDE_DIRS}
235
- ${PNG_INCLUDE_DIRS}
236
- ${WEBP_INCLUDE_DIRS}
237
- )
238
-
239
- # Link with static libraries
240
- if(JPEG_STATIC_LIBRARY AND PNG_STATIC_LIBRARY AND ZLIB_STATIC_LIBRARY)
241
- target_link_libraries(fast_resize-cli PRIVATE
242
- Threads::Threads
243
- ${JPEG_STATIC_LIBRARY}
244
- ${PNG_STATIC_LIBRARY}
245
- ${ZLIB_STATIC_LIBRARY}
246
- )
247
-
248
- if(WEBP_STATIC_LIBRARY)
249
- target_link_libraries(fast_resize-cli PRIVATE ${WEBP_STATIC_LIBRARY})
250
- if(SHARPYUV_STATIC_LIBRARY)
251
- target_link_libraries(fast_resize-cli PRIVATE ${SHARPYUV_STATIC_LIBRARY})
252
- endif()
253
- endif()
254
-
255
- # On Linux: ensure full static linking
256
- if(UNIX AND NOT APPLE)
257
- target_link_options(fast_resize-cli PRIVATE -static)
258
- endif()
259
- else()
260
- # Fallback to library
261
- target_link_libraries(fast_resize-cli PRIVATE fastresize)
262
- endif()
263
- else()
264
- # Dynamic linking
265
- target_link_libraries(fast_resize-cli PRIVATE fastresize)
266
- endif()
267
-
268
- # Set output name to fast_resize (without -cli suffix)
269
- set_target_properties(fast_resize-cli PROPERTIES
270
- OUTPUT_NAME fast_resize
271
- )
272
-
273
- # Install CLI
274
- install(TARGETS fast_resize-cli
275
- RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
276
- )
277
- endif()
278
-
279
- # Tests
280
- if(FASTRESIZE_BUILD_TESTS)
281
- enable_testing()
282
- add_subdirectory(tests)
283
- endif()
284
-
285
- # Examples
286
- if(FASTRESIZE_BUILD_EXAMPLES)
287
- add_subdirectory(examples)
288
- endif()
289
-
290
- # Benchmarks
291
- if(FASTRESIZE_BUILD_BENCHMARKS)
292
- add_subdirectory(benchmark)
293
- endif()
294
-
295
- # Install
296
- include(GNUInstallDirs)
297
-
298
- install(TARGETS fastresize
299
- EXPORT fastresize-targets
300
- LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
301
- ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
302
- RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
303
- )
304
-
305
- install(DIRECTORY include/
306
- DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
307
- FILES_MATCHING PATTERN "*.h"
308
- )
@@ -1,377 +0,0 @@
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 <ruby.h>
42
- #include <ruby/thread.h>
43
- #include <fastresize.h>
44
- #include <string>
45
- #include <vector>
46
-
47
- static VALUE rb_mFastResize;
48
-
49
- static std::string rb_string_to_cpp(VALUE rb_str) {
50
- Check_Type(rb_str, T_STRING);
51
- return std::string(RSTRING_PTR(rb_str), RSTRING_LEN(rb_str));
52
- }
53
-
54
- static fastresize::ResizeOptions parse_resize_options(VALUE options) {
55
- fastresize::ResizeOptions opts;
56
-
57
- if (NIL_P(options)) {
58
- return opts;
59
- }
60
-
61
- Check_Type(options, T_HASH);
62
-
63
- VALUE width = rb_hash_aref(options, ID2SYM(rb_intern("width")));
64
- if (!NIL_P(width)) {
65
- opts.target_width = NUM2INT(width);
66
- }
67
-
68
- VALUE height = rb_hash_aref(options, ID2SYM(rb_intern("height")));
69
- if (!NIL_P(height)) {
70
- opts.target_height = NUM2INT(height);
71
- }
72
-
73
- VALUE scale = rb_hash_aref(options, ID2SYM(rb_intern("scale")));
74
- if (!NIL_P(scale)) {
75
- opts.scale_percent = (float)NUM2DBL(scale) / 100.0f;
76
- if (opts.scale_percent <= 0) {
77
- rb_raise(rb_eArgError, "Scale must be positive");
78
- }
79
- opts.mode = fastresize::ResizeOptions::SCALE_PERCENT;
80
- } else if (!NIL_P(width) && !NIL_P(height)) {
81
- opts.mode = fastresize::ResizeOptions::EXACT_SIZE;
82
- } else if (!NIL_P(width)) {
83
- opts.mode = fastresize::ResizeOptions::FIT_WIDTH;
84
- } else if (!NIL_P(height)) {
85
- opts.mode = fastresize::ResizeOptions::FIT_HEIGHT;
86
- }
87
-
88
- VALUE quality = rb_hash_aref(options, ID2SYM(rb_intern("quality")));
89
- if (!NIL_P(quality)) {
90
- opts.quality = NUM2INT(quality);
91
- if (opts.quality < 1 || opts.quality > 100) {
92
- rb_raise(rb_eArgError, "Quality must be between 1 and 100");
93
- }
94
- }
95
-
96
- VALUE keep_aspect = rb_hash_aref(options, ID2SYM(rb_intern("keep_aspect_ratio")));
97
- if (!NIL_P(keep_aspect)) {
98
- opts.keep_aspect_ratio = RTEST(keep_aspect);
99
- }
100
-
101
- VALUE overwrite = rb_hash_aref(options, ID2SYM(rb_intern("overwrite")));
102
- if (!NIL_P(overwrite)) {
103
- opts.overwrite_input = RTEST(overwrite);
104
- }
105
-
106
- if (opts.target_width < 0) {
107
- rb_raise(rb_eArgError, "Width must be non-negative");
108
- }
109
- if (opts.target_height < 0) {
110
- rb_raise(rb_eArgError, "Height must be non-negative");
111
- }
112
-
113
- VALUE filter = rb_hash_aref(options, ID2SYM(rb_intern("filter")));
114
- if (!NIL_P(filter)) {
115
- Check_Type(filter, T_SYMBOL);
116
- ID filter_id = SYM2ID(filter);
117
-
118
- if (filter_id == rb_intern("mitchell")) {
119
- opts.filter = fastresize::ResizeOptions::MITCHELL;
120
- } else if (filter_id == rb_intern("catmull_rom")) {
121
- opts.filter = fastresize::ResizeOptions::CATMULL_ROM;
122
- } else if (filter_id == rb_intern("box")) {
123
- opts.filter = fastresize::ResizeOptions::BOX;
124
- } else if (filter_id == rb_intern("triangle")) {
125
- opts.filter = fastresize::ResizeOptions::TRIANGLE;
126
- } else {
127
- rb_raise(rb_eArgError, "Invalid filter. Use :mitchell, :catmull_rom, :box, or :triangle");
128
- }
129
- }
130
-
131
- return opts;
132
- }
133
-
134
- struct ResizeParams {
135
- std::string input;
136
- std::string output;
137
- fastresize::ResizeOptions opts;
138
- bool success;
139
- std::string error;
140
- };
141
-
142
- static void* resize_without_gvl(void* data) {
143
- ResizeParams* params = static_cast<ResizeParams*>(data);
144
- try {
145
- params->success = fastresize::resize(params->input, params->output, params->opts);
146
- if (!params->success) {
147
- params->error = fastresize::get_last_error();
148
- }
149
- } catch (const std::exception& e) {
150
- params->success = false;
151
- params->error = std::string("Exception: ") + e.what();
152
- }
153
- return nullptr;
154
- }
155
-
156
- static VALUE rb_fastresize_resize(int argc, VALUE* argv, VALUE self) {
157
- VALUE input_path, output_path, options;
158
- rb_scan_args(argc, argv, "21", &input_path, &output_path, &options);
159
-
160
- try {
161
- ResizeParams params;
162
- params.input = rb_string_to_cpp(input_path);
163
- params.output = rb_string_to_cpp(output_path);
164
- params.opts = parse_resize_options(options);
165
-
166
- rb_thread_call_without_gvl(resize_without_gvl, &params, RUBY_UBF_IO, nullptr);
167
-
168
- if (!params.success) {
169
- rb_raise(rb_eRuntimeError, "Resize failed: %s", params.error.c_str());
170
- }
171
-
172
- return Qtrue;
173
- } catch (const std::exception& e) {
174
- rb_raise(rb_eRuntimeError, "Resize failed: %s", e.what());
175
- }
176
- }
177
-
178
- struct ResizeWithFormatParams {
179
- std::string input;
180
- std::string output;
181
- std::string format;
182
- fastresize::ResizeOptions opts;
183
- bool success;
184
- std::string error;
185
- };
186
-
187
- static void* resize_with_format_without_gvl(void* data) {
188
- ResizeWithFormatParams* params = static_cast<ResizeWithFormatParams*>(data);
189
- try {
190
- params->success = fastresize::resize_with_format(params->input, params->output, params->format, params->opts);
191
- if (!params->success) {
192
- params->error = fastresize::get_last_error();
193
- }
194
- } catch (const std::exception& e) {
195
- params->success = false;
196
- params->error = std::string("Exception: ") + e.what();
197
- }
198
- return nullptr;
199
- }
200
-
201
- static VALUE rb_fastresize_resize_with_format(int argc, VALUE* argv, VALUE self) {
202
- VALUE input_path, output_path, format, options;
203
- rb_scan_args(argc, argv, "31", &input_path, &output_path, &format, &options);
204
-
205
- try {
206
- ResizeWithFormatParams params;
207
- params.input = rb_string_to_cpp(input_path);
208
- params.output = rb_string_to_cpp(output_path);
209
- params.format = rb_string_to_cpp(format);
210
- params.opts = parse_resize_options(options);
211
-
212
- rb_thread_call_without_gvl(resize_with_format_without_gvl, &params, RUBY_UBF_IO, nullptr);
213
-
214
- if (!params.success) {
215
- rb_raise(rb_eRuntimeError, "Resize with format failed: %s", params.error.c_str());
216
- }
217
-
218
- return Qtrue;
219
- } catch (const std::exception& e) {
220
- rb_raise(rb_eRuntimeError, "Resize with format failed: %s", e.what());
221
- }
222
- }
223
-
224
- static VALUE rb_fastresize_image_info(VALUE self, VALUE path) {
225
- try {
226
- std::string path_str = rb_string_to_cpp(path);
227
- fastresize::ImageInfo info = fastresize::get_image_info(path_str);
228
-
229
- VALUE result = rb_hash_new();
230
- rb_hash_aset(result, ID2SYM(rb_intern("width")), INT2NUM(info.width));
231
- rb_hash_aset(result, ID2SYM(rb_intern("height")), INT2NUM(info.height));
232
- rb_hash_aset(result, ID2SYM(rb_intern("channels")), INT2NUM(info.channels));
233
- rb_hash_aset(result, ID2SYM(rb_intern("format")), rb_str_new_cstr(info.format.c_str()));
234
-
235
- return result;
236
- } catch (const std::exception& e) {
237
- rb_raise(rb_eRuntimeError, "Get image info failed: %s", e.what());
238
- }
239
- }
240
-
241
- static VALUE rb_fastresize_batch_resize(int argc, VALUE* argv, VALUE self) {
242
- VALUE input_paths, output_dir, options;
243
- rb_scan_args(argc, argv, "21", &input_paths, &output_dir, &options);
244
-
245
- Check_Type(input_paths, T_ARRAY);
246
-
247
- try {
248
- std::vector<std::string> inputs;
249
- long len = RARRAY_LEN(input_paths);
250
- for (long i = 0; i < len; ++i) {
251
- VALUE item = rb_ary_entry(input_paths, i);
252
- inputs.push_back(rb_string_to_cpp(item));
253
- }
254
-
255
- std::string out_dir = rb_string_to_cpp(output_dir);
256
- fastresize::ResizeOptions resize_opts = parse_resize_options(options);
257
-
258
- fastresize::BatchOptions batch_opts;
259
- if (!NIL_P(options)) {
260
- VALUE threads = rb_hash_aref(options, ID2SYM(rb_intern("threads")));
261
- if (!NIL_P(threads)) {
262
- batch_opts.num_threads = NUM2INT(threads);
263
- }
264
-
265
- VALUE stop_on_error = rb_hash_aref(options, ID2SYM(rb_intern("stop_on_error")));
266
- if (!NIL_P(stop_on_error)) {
267
- batch_opts.stop_on_error = RTEST(stop_on_error);
268
- }
269
-
270
- VALUE max_speed = rb_hash_aref(options, ID2SYM(rb_intern("max_speed")));
271
- if (!NIL_P(max_speed)) {
272
- batch_opts.max_speed = RTEST(max_speed);
273
- }
274
- }
275
-
276
- fastresize::BatchResult result = fastresize::batch_resize(inputs, out_dir, resize_opts, batch_opts);
277
-
278
- VALUE rb_result = rb_hash_new();
279
- rb_hash_aset(rb_result, ID2SYM(rb_intern("total")), INT2NUM(result.total));
280
- rb_hash_aset(rb_result, ID2SYM(rb_intern("success")), INT2NUM(result.success));
281
- rb_hash_aset(rb_result, ID2SYM(rb_intern("failed")), INT2NUM(result.failed));
282
-
283
- VALUE errors = rb_ary_new();
284
- for (const auto& error : result.errors) {
285
- rb_ary_push(errors, rb_str_new_cstr(error.c_str()));
286
- }
287
- rb_hash_aset(rb_result, ID2SYM(rb_intern("errors")), errors);
288
-
289
- return rb_result;
290
- } catch (const std::exception& e) {
291
- rb_raise(rb_eRuntimeError, "Batch resize failed: %s", e.what());
292
- }
293
- }
294
-
295
- static VALUE rb_fastresize_batch_resize_custom(int argc, VALUE* argv, VALUE self) {
296
- VALUE items, options;
297
- rb_scan_args(argc, argv, "11", &items, &options);
298
-
299
- Check_Type(items, T_ARRAY);
300
-
301
- try {
302
- std::vector<fastresize::BatchItem> batch_items;
303
- long len = RARRAY_LEN(items);
304
- for (long i = 0; i < len; ++i) {
305
- VALUE item = rb_ary_entry(items, i);
306
- Check_Type(item, T_HASH);
307
-
308
- fastresize::BatchItem batch_item;
309
-
310
- VALUE input = rb_hash_aref(item, ID2SYM(rb_intern("input")));
311
- if (NIL_P(input)) {
312
- rb_raise(rb_eArgError, "Item %ld missing 'input' key", i);
313
- }
314
- batch_item.input_path = rb_string_to_cpp(input);
315
-
316
- VALUE output = rb_hash_aref(item, ID2SYM(rb_intern("output")));
317
- if (NIL_P(output)) {
318
- rb_raise(rb_eArgError, "Item %ld missing 'output' key", i);
319
- }
320
- batch_item.output_path = rb_string_to_cpp(output);
321
-
322
- batch_item.options = parse_resize_options(item);
323
-
324
- batch_items.push_back(batch_item);
325
- }
326
-
327
- fastresize::BatchOptions batch_opts;
328
- if (!NIL_P(options)) {
329
- VALUE threads = rb_hash_aref(options, ID2SYM(rb_intern("threads")));
330
- if (!NIL_P(threads)) {
331
- batch_opts.num_threads = NUM2INT(threads);
332
- }
333
-
334
- VALUE stop_on_error = rb_hash_aref(options, ID2SYM(rb_intern("stop_on_error")));
335
- if (!NIL_P(stop_on_error)) {
336
- batch_opts.stop_on_error = RTEST(stop_on_error);
337
- }
338
-
339
- VALUE max_speed = rb_hash_aref(options, ID2SYM(rb_intern("max_speed")));
340
- if (!NIL_P(max_speed)) {
341
- batch_opts.max_speed = RTEST(max_speed);
342
- }
343
- }
344
-
345
- fastresize::BatchResult result = fastresize::batch_resize_custom(batch_items, batch_opts);
346
-
347
- VALUE rb_result = rb_hash_new();
348
- rb_hash_aset(rb_result, ID2SYM(rb_intern("total")), INT2NUM(result.total));
349
- rb_hash_aset(rb_result, ID2SYM(rb_intern("success")), INT2NUM(result.success));
350
- rb_hash_aset(rb_result, ID2SYM(rb_intern("failed")), INT2NUM(result.failed));
351
-
352
- VALUE errors = rb_ary_new();
353
- for (const auto& error : result.errors) {
354
- rb_ary_push(errors, rb_str_new_cstr(error.c_str()));
355
- }
356
- rb_hash_aset(rb_result, ID2SYM(rb_intern("errors")), errors);
357
-
358
- return rb_result;
359
- } catch (const std::exception& e) {
360
- rb_raise(rb_eRuntimeError, "Batch resize custom failed: %s", e.what());
361
- }
362
- }
363
-
364
- extern "C" void Init_fastresize_ext(void) {
365
- rb_mFastResize = rb_define_module("FastResize");
366
-
367
- rb_define_singleton_method(rb_mFastResize, "resize",
368
- RUBY_METHOD_FUNC(rb_fastresize_resize), -1);
369
- rb_define_singleton_method(rb_mFastResize, "resize_with_format",
370
- RUBY_METHOD_FUNC(rb_fastresize_resize_with_format), -1);
371
- rb_define_singleton_method(rb_mFastResize, "image_info",
372
- RUBY_METHOD_FUNC(rb_fastresize_image_info), 1);
373
- rb_define_singleton_method(rb_mFastResize, "batch_resize",
374
- RUBY_METHOD_FUNC(rb_fastresize_batch_resize), -1);
375
- rb_define_singleton_method(rb_mFastResize, "batch_resize_custom",
376
- RUBY_METHOD_FUNC(rb_fastresize_batch_resize_custom), -1);
377
- }