fast_resize 1.0.2 → 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 (36) hide show
  1. checksums.yaml +4 -4
  2. data/VERSION +1 -1
  3. data/bindings/ruby/ext/fastresize/extconf.rb +79 -105
  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.tar.gz +0 -0
  9. data/bindings/ruby/prebuilt/linux-x86_64/bin/fast_resize +0 -0
  10. data/bindings/ruby/prebuilt/linux-x86_64/lib/libfastresize.a +0 -0
  11. data/bindings/ruby/prebuilt/linux-x86_64.tar.gz +0 -0
  12. data/bindings/ruby/prebuilt/macos-arm64/bin/fast_resize +0 -0
  13. data/bindings/ruby/prebuilt/macos-arm64/lib/libfastresize.a +0 -0
  14. data/bindings/ruby/prebuilt/macos-arm64.tar.gz +0 -0
  15. data/bindings/ruby/prebuilt/macos-x86_64/bin/fast_resize +0 -0
  16. data/bindings/ruby/prebuilt/macos-x86_64/lib/libfastresize.a +0 -0
  17. data/bindings/ruby/prebuilt/macos-x86_64.tar.gz +0 -0
  18. metadata +4 -22
  19. data/CMakeLists.txt +0 -311
  20. data/bindings/ruby/ext/fastresize/fastresize_ext.cpp +0 -377
  21. data/include/fastresize.h +0 -189
  22. data/include/stb_image.h +0 -7988
  23. data/include/stb_image_resize2.h +0 -10651
  24. data/include/stb_image_write.h +0 -1724
  25. data/src/cli.cpp +0 -540
  26. data/src/decoder.cpp +0 -647
  27. data/src/encoder.cpp +0 -376
  28. data/src/fastresize.cpp +0 -445
  29. data/src/internal.h +0 -108
  30. data/src/pipeline.cpp +0 -284
  31. data/src/pipeline.h +0 -175
  32. data/src/resizer.cpp +0 -199
  33. data/src/simd_resize.cpp +0 -384
  34. data/src/simd_resize.h +0 -72
  35. data/src/simd_utils.h +0 -127
  36. data/src/thread_pool.cpp +0 -232
data/CMakeLists.txt DELETED
@@ -1,311 +0,0 @@
1
- cmake_minimum_required(VERSION 3.15)
2
- project(fast_resize VERSION 1.0.2 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
- # Disable LTO for static library builds to ensure compatibility across GCC versions
14
- # LTO bytecode format changes between GCC versions, causing link failures
15
- # when the static library is used with a different GCC version
16
- set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG")
17
- set(CMAKE_C_FLAGS_RELEASE "-O3 -DNDEBUG")
18
-
19
- # Option for building shared vs static libraries
20
- option(BUILD_SHARED_LIBS "Build shared libraries" OFF)
21
-
22
- # Platform-specific optimizations
23
- if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|amd64|AMD64")
24
- set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -march=native")
25
- set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -march=native")
26
- elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "arm64|aarch64|ARM64")
27
- # ARM-specific flags
28
- set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
29
- set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")
30
- endif()
31
-
32
- # Options
33
- option(FASTRESIZE_STATIC "Build static library" ON)
34
- option(FASTRESIZE_BUILD_TESTS "Build tests" ON)
35
- option(FASTRESIZE_BUILD_EXAMPLES "Build examples" ON)
36
- option(FASTRESIZE_BUILD_BENCHMARKS "Build benchmarks" ON)
37
- option(FASTRESIZE_BUILD_RUBY "Build Ruby binding" ON)
38
-
39
- # Find dependencies
40
- find_package(PkgConfig REQUIRED)
41
-
42
- # JPEG (libjpeg-turbo preferred)
43
- pkg_check_modules(JPEG libjpeg)
44
- if(NOT JPEG_FOUND)
45
- find_package(JPEG REQUIRED)
46
- endif()
47
-
48
- # PNG
49
- pkg_check_modules(PNG libpng)
50
- if(NOT PNG_FOUND)
51
- find_package(PNG REQUIRED)
52
- endif()
53
-
54
- # WebP
55
- pkg_check_modules(WEBP libwebp libwebpdecoder libwebpdemux libsharpyuv)
56
- if(NOT WEBP_FOUND)
57
- message(WARNING "libwebp not found, WEBP support will be limited")
58
- endif()
59
-
60
- # Source files
61
- set(FASTRESIZE_SOURCES
62
- src/fastresize.cpp
63
- src/decoder.cpp
64
- src/encoder.cpp
65
- src/resizer.cpp
66
- src/thread_pool.cpp
67
- src/pipeline.cpp
68
- src/simd_resize.cpp
69
- )
70
-
71
- # Create library
72
- add_library(fastresize ${FASTRESIZE_SOURCES})
73
-
74
- target_include_directories(fastresize
75
- PUBLIC
76
- $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
77
- $<INSTALL_INTERFACE:include>
78
- PRIVATE
79
- ${CMAKE_CURRENT_SOURCE_DIR}/src
80
- ${JPEG_INCLUDE_DIRS}
81
- ${PNG_INCLUDE_DIRS}
82
- ${WEBP_INCLUDE_DIRS}
83
- )
84
-
85
- # Link with threading library
86
- find_package(Threads REQUIRED)
87
-
88
- # Link libraries - improved static library finding like FastQR
89
- if(FASTRESIZE_STATIC OR NOT BUILD_SHARED_LIBS)
90
- message(STATUS "🔧 Configuring for static linking")
91
-
92
- # Find static libraries with explicit paths (like FastQR)
93
- find_library(JPEG_STATIC_LIBRARY
94
- NAMES libjpeg.a libturbojpeg.a
95
- PATHS
96
- /usr/local/lib
97
- /opt/homebrew/lib
98
- /usr/lib
99
- /usr/lib/x86_64-linux-gnu
100
- /usr/lib/aarch64-linux-gnu
101
- NO_DEFAULT_PATH
102
- )
103
-
104
- find_library(PNG_STATIC_LIBRARY
105
- NAMES libpng.a libpng16.a
106
- PATHS
107
- /usr/local/lib
108
- /opt/homebrew/lib
109
- /usr/lib
110
- /usr/lib/x86_64-linux-gnu
111
- /usr/lib/aarch64-linux-gnu
112
- NO_DEFAULT_PATH
113
- )
114
-
115
- find_library(WEBP_STATIC_LIBRARY
116
- NAMES libwebp.a
117
- PATHS
118
- /usr/local/lib
119
- /opt/homebrew/lib
120
- /usr/lib
121
- /usr/lib/x86_64-linux-gnu
122
- /usr/lib/aarch64-linux-gnu
123
- NO_DEFAULT_PATH
124
- )
125
-
126
- find_library(SHARPYUV_STATIC_LIBRARY
127
- NAMES libsharpyuv.a
128
- PATHS
129
- /usr/local/lib
130
- /opt/homebrew/lib
131
- /usr/lib
132
- /usr/lib/x86_64-linux-gnu
133
- /usr/lib/aarch64-linux-gnu
134
- NO_DEFAULT_PATH
135
- )
136
-
137
- find_library(ZLIB_STATIC_LIBRARY
138
- NAMES libz.a
139
- PATHS
140
- /usr/local/lib
141
- /usr/local/opt/zlib/lib
142
- /opt/homebrew/lib
143
- /opt/homebrew/opt/zlib/lib
144
- /usr/lib
145
- /usr/lib/x86_64-linux-gnu
146
- /usr/lib/aarch64-linux-gnu
147
- NO_DEFAULT_PATH
148
- )
149
-
150
- message(STATUS "📚 Static libraries found:")
151
- message(STATUS " JPEG: ${JPEG_STATIC_LIBRARY}")
152
- message(STATUS " PNG: ${PNG_STATIC_LIBRARY}")
153
- message(STATUS " WEBP: ${WEBP_STATIC_LIBRARY}")
154
- message(STATUS " SHARPYUV: ${SHARPYUV_STATIC_LIBRARY}")
155
- message(STATUS " ZLIB: ${ZLIB_STATIC_LIBRARY}")
156
-
157
- # Link with required static libraries (check if they exist first)
158
- if(JPEG_STATIC_LIBRARY AND PNG_STATIC_LIBRARY AND ZLIB_STATIC_LIBRARY)
159
- target_link_libraries(fastresize PRIVATE
160
- Threads::Threads
161
- ${JPEG_STATIC_LIBRARY}
162
- ${PNG_STATIC_LIBRARY}
163
- ${ZLIB_STATIC_LIBRARY}
164
- )
165
-
166
- if(WEBP_STATIC_LIBRARY)
167
- target_link_libraries(fastresize PRIVATE ${WEBP_STATIC_LIBRARY})
168
- if(SHARPYUV_STATIC_LIBRARY)
169
- target_link_libraries(fastresize PRIVATE ${SHARPYUV_STATIC_LIBRARY})
170
- endif()
171
- endif()
172
- else()
173
- message(WARNING "⚠️ Some static libraries not found, falling back to dynamic linking")
174
- message(WARNING " JPEG: ${JPEG_STATIC_LIBRARY}")
175
- message(WARNING " PNG: ${PNG_STATIC_LIBRARY}")
176
- message(WARNING " ZLIB: ${ZLIB_STATIC_LIBRARY}")
177
-
178
- target_link_libraries(fastresize PRIVATE
179
- Threads::Threads
180
- ${JPEG_LIBRARIES}
181
- ${PNG_LIBRARIES}
182
- )
183
-
184
- if(WEBP_FOUND)
185
- target_link_libraries(fastresize PRIVATE ${WEBP_LIBRARIES})
186
- endif()
187
- endif()
188
-
189
- # On Linux: ensure full static linking
190
- if(UNIX AND NOT APPLE)
191
- target_link_options(fastresize PRIVATE -static-libgcc -static-libstdc++)
192
- endif()
193
- else()
194
- message(STATUS "🔧 Configuring for dynamic linking")
195
- target_link_libraries(fastresize PRIVATE
196
- Threads::Threads
197
- ${JPEG_LIBRARIES}
198
- ${PNG_LIBRARIES}
199
- )
200
-
201
- if(WEBP_FOUND)
202
- target_link_libraries(fastresize PRIVATE ${WEBP_LIBRARIES})
203
- endif()
204
- endif()
205
-
206
- # ============================================================================
207
- # CLI Tool
208
- # ============================================================================
209
-
210
- option(FASTRESIZE_BUILD_CLI "Build CLI tool" ON)
211
-
212
- if(FASTRESIZE_BUILD_CLI)
213
- message(STATUS "🔧 Building CLI tool")
214
-
215
- # Read version from VERSION file
216
- file(READ "${CMAKE_CURRENT_SOURCE_DIR}/VERSION" FASTRESIZE_VERSION)
217
- string(STRIP "${FASTRESIZE_VERSION}" FASTRESIZE_VERSION)
218
-
219
- # CLI executable
220
- add_executable(fast_resize-cli
221
- src/cli.cpp
222
- )
223
-
224
- # Pass version to CLI
225
- target_compile_definitions(fast_resize-cli PRIVATE
226
- FASTRESIZE_VERSION="${FASTRESIZE_VERSION}"
227
- )
228
-
229
- # For standalone CLI binary, link statically
230
- if(FASTRESIZE_STATIC OR NOT BUILD_SHARED_LIBS)
231
- # Link CLI directly with library sources
232
- target_sources(fast_resize-cli PRIVATE ${FASTRESIZE_SOURCES})
233
-
234
- target_include_directories(fast_resize-cli PRIVATE
235
- ${CMAKE_CURRENT_SOURCE_DIR}/include
236
- ${CMAKE_CURRENT_SOURCE_DIR}/src
237
- ${JPEG_INCLUDE_DIRS}
238
- ${PNG_INCLUDE_DIRS}
239
- ${WEBP_INCLUDE_DIRS}
240
- )
241
-
242
- # Link with static libraries
243
- if(JPEG_STATIC_LIBRARY AND PNG_STATIC_LIBRARY AND ZLIB_STATIC_LIBRARY)
244
- target_link_libraries(fast_resize-cli PRIVATE
245
- Threads::Threads
246
- ${JPEG_STATIC_LIBRARY}
247
- ${PNG_STATIC_LIBRARY}
248
- ${ZLIB_STATIC_LIBRARY}
249
- )
250
-
251
- if(WEBP_STATIC_LIBRARY)
252
- target_link_libraries(fast_resize-cli PRIVATE ${WEBP_STATIC_LIBRARY})
253
- if(SHARPYUV_STATIC_LIBRARY)
254
- target_link_libraries(fast_resize-cli PRIVATE ${SHARPYUV_STATIC_LIBRARY})
255
- endif()
256
- endif()
257
-
258
- # On Linux: ensure full static linking
259
- if(UNIX AND NOT APPLE)
260
- target_link_options(fast_resize-cli PRIVATE -static)
261
- endif()
262
- else()
263
- # Fallback to library
264
- target_link_libraries(fast_resize-cli PRIVATE fastresize)
265
- endif()
266
- else()
267
- # Dynamic linking
268
- target_link_libraries(fast_resize-cli PRIVATE fastresize)
269
- endif()
270
-
271
- # Set output name to fast_resize (without -cli suffix)
272
- set_target_properties(fast_resize-cli PROPERTIES
273
- OUTPUT_NAME fast_resize
274
- )
275
-
276
- # Install CLI
277
- install(TARGETS fast_resize-cli
278
- RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
279
- )
280
- endif()
281
-
282
- # Tests
283
- if(FASTRESIZE_BUILD_TESTS)
284
- enable_testing()
285
- add_subdirectory(tests)
286
- endif()
287
-
288
- # Examples
289
- if(FASTRESIZE_BUILD_EXAMPLES)
290
- add_subdirectory(examples)
291
- endif()
292
-
293
- # Benchmarks
294
- if(FASTRESIZE_BUILD_BENCHMARKS)
295
- add_subdirectory(benchmark)
296
- endif()
297
-
298
- # Install
299
- include(GNUInstallDirs)
300
-
301
- install(TARGETS fastresize
302
- EXPORT fastresize-targets
303
- LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
304
- ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
305
- RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
306
- )
307
-
308
- install(DIRECTORY include/
309
- DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
310
- FILES_MATCHING PATTERN "*.h"
311
- )
@@ -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
- }