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.
- checksums.yaml +7 -0
- data/CMakeLists.txt +308 -0
- data/LICENSE +29 -0
- data/README.md +426 -0
- data/VERSION +1 -0
- data/bindings/ruby/ext/fastresize/extconf.rb +152 -0
- data/bindings/ruby/ext/fastresize/fastresize_ext.cpp +377 -0
- data/bindings/ruby/lib/fastresize/platform.rb +106 -0
- data/bindings/ruby/lib/fastresize/version.rb +5 -0
- data/bindings/ruby/lib/fastresize.rb +13 -0
- data/bindings/ruby/prebuilt/linux-aarch64/bin/fast_resize +0 -0
- data/bindings/ruby/prebuilt/linux-aarch64/include/fastresize.h +189 -0
- data/bindings/ruby/prebuilt/linux-aarch64/include/stb_image.h +7988 -0
- data/bindings/ruby/prebuilt/linux-aarch64/include/stb_image_resize2.h +10651 -0
- data/bindings/ruby/prebuilt/linux-aarch64/include/stb_image_write.h +1724 -0
- data/bindings/ruby/prebuilt/linux-aarch64/lib/libfastresize.a +0 -0
- data/bindings/ruby/prebuilt/linux-aarch64.tar.gz +0 -0
- data/bindings/ruby/prebuilt/linux-x86_64/bin/fast_resize +0 -0
- data/bindings/ruby/prebuilt/linux-x86_64/include/fastresize.h +189 -0
- data/bindings/ruby/prebuilt/linux-x86_64/include/stb_image.h +7988 -0
- data/bindings/ruby/prebuilt/linux-x86_64/include/stb_image_resize2.h +10651 -0
- data/bindings/ruby/prebuilt/linux-x86_64/include/stb_image_write.h +1724 -0
- data/bindings/ruby/prebuilt/linux-x86_64/lib/libfastresize.a +0 -0
- data/bindings/ruby/prebuilt/linux-x86_64.tar.gz +0 -0
- data/bindings/ruby/prebuilt/macos-arm64/bin/fast_resize +0 -0
- data/bindings/ruby/prebuilt/macos-arm64/include/fastresize.h +189 -0
- data/bindings/ruby/prebuilt/macos-arm64/include/stb_image.h +7988 -0
- data/bindings/ruby/prebuilt/macos-arm64/include/stb_image_resize2.h +10651 -0
- data/bindings/ruby/prebuilt/macos-arm64/include/stb_image_write.h +1724 -0
- data/bindings/ruby/prebuilt/macos-arm64/lib/libfastresize.a +0 -0
- data/bindings/ruby/prebuilt/macos-arm64.tar.gz +0 -0
- data/bindings/ruby/prebuilt/macos-x86_64/bin/fast_resize +0 -0
- data/bindings/ruby/prebuilt/macos-x86_64/include/fastresize.h +189 -0
- data/bindings/ruby/prebuilt/macos-x86_64/include/stb_image.h +7988 -0
- data/bindings/ruby/prebuilt/macos-x86_64/include/stb_image_resize2.h +10651 -0
- data/bindings/ruby/prebuilt/macos-x86_64/include/stb_image_write.h +1724 -0
- data/bindings/ruby/prebuilt/macos-x86_64/lib/libfastresize.a +0 -0
- data/bindings/ruby/prebuilt/macos-x86_64.tar.gz +0 -0
- data/include/fastresize.h +189 -0
- data/include/stb_image.h +7988 -0
- data/include/stb_image_resize2.h +10651 -0
- data/include/stb_image_write.h +1724 -0
- data/src/cli.cpp +540 -0
- data/src/decoder.cpp +647 -0
- data/src/encoder.cpp +376 -0
- data/src/fastresize.cpp +445 -0
- data/src/internal.h +108 -0
- data/src/pipeline.cpp +284 -0
- data/src/pipeline.h +175 -0
- data/src/resizer.cpp +199 -0
- data/src/simd_resize.cpp +384 -0
- data/src/simd_resize.h +72 -0
- data/src/simd_utils.h +127 -0
- data/src/thread_pool.cpp +232 -0
- metadata +158 -0
data/src/cli.cpp
ADDED
|
@@ -0,0 +1,540 @@
|
|
|
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 <fastresize.h>
|
|
42
|
+
#include <iostream>
|
|
43
|
+
#include <string>
|
|
44
|
+
#include <vector>
|
|
45
|
+
#include <cstring>
|
|
46
|
+
#include <cstdlib>
|
|
47
|
+
#include <climits>
|
|
48
|
+
#include <sys/stat.h>
|
|
49
|
+
#include <dirent.h>
|
|
50
|
+
#include <errno.h>
|
|
51
|
+
|
|
52
|
+
// Version from CMake (passed via -DFASTRESIZE_VERSION)
|
|
53
|
+
#ifndef FASTRESIZE_VERSION
|
|
54
|
+
#define FASTRESIZE_VERSION "1.0.0"
|
|
55
|
+
#endif
|
|
56
|
+
|
|
57
|
+
static const char* get_version() {
|
|
58
|
+
return FASTRESIZE_VERSION;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
void print_usage(const char* program_name) {
|
|
62
|
+
std::cout << "FastResize v" << get_version() << " - The Fastest Image Resizing Library\n\n";
|
|
63
|
+
std::cout << "Usage: " << program_name << " [OPTIONS] <input> <output> [width] [height]\n";
|
|
64
|
+
std::cout << " " << program_name << " batch [OPTIONS] <input_dir> <output_dir>\n";
|
|
65
|
+
std::cout << " " << program_name << " info <image>\n\n";
|
|
66
|
+
std::cout << "Commands:\n";
|
|
67
|
+
std::cout << " (default) Resize single image\n";
|
|
68
|
+
std::cout << " batch Batch resize all images in directory\n";
|
|
69
|
+
std::cout << " info Show image information\n\n";
|
|
70
|
+
std::cout << "Resize Options:\n";
|
|
71
|
+
std::cout << " -w, --width WIDTH Target width in pixels\n";
|
|
72
|
+
std::cout << " -h, --height HEIGHT Target height in pixels\n";
|
|
73
|
+
std::cout << " -s, --scale SCALE Scale factor (e.g., 0.5 = 50%, 2.0 = 200%)\n";
|
|
74
|
+
std::cout << " -q, --quality QUALITY JPEG/WebP quality 1-100 (default: 85)\n";
|
|
75
|
+
std::cout << " -f, --filter FILTER Resize filter: mitchell, catmull_rom, box, triangle\n";
|
|
76
|
+
std::cout << " (default: mitchell)\n";
|
|
77
|
+
std::cout << " --no-aspect-ratio Don't maintain aspect ratio\n";
|
|
78
|
+
std::cout << " -o, --overwrite Overwrite input file\n\n";
|
|
79
|
+
std::cout << "Batch Options:\n";
|
|
80
|
+
std::cout << " -t, --threads NUM Number of threads (default: auto)\n";
|
|
81
|
+
std::cout << " --stop-on-error Stop on first error\n";
|
|
82
|
+
std::cout << " --max-speed Enable pipeline mode (uses more RAM)\n\n";
|
|
83
|
+
std::cout << "Other Options:\n";
|
|
84
|
+
std::cout << " --help Show this help\n";
|
|
85
|
+
std::cout << " --version Show version\n\n";
|
|
86
|
+
std::cout << "Examples:\n";
|
|
87
|
+
std::cout << " # Resize to width 800 (height auto)\n";
|
|
88
|
+
std::cout << " " << program_name << " input.jpg output.jpg 800\n\n";
|
|
89
|
+
std::cout << " # Resize to exact 800x600\n";
|
|
90
|
+
std::cout << " " << program_name << " input.jpg output.jpg 800 600\n\n";
|
|
91
|
+
std::cout << " # Resize with options\n";
|
|
92
|
+
std::cout << " " << program_name << " input.jpg output.jpg -w 800 -q 95 -f catmull_rom\n\n";
|
|
93
|
+
std::cout << " # Scale to 50%\n";
|
|
94
|
+
std::cout << " " << program_name << " input.jpg output.jpg -s 0.5\n\n";
|
|
95
|
+
std::cout << " # Batch resize directory\n";
|
|
96
|
+
std::cout << " " << program_name << " batch photos/ thumbnails/ -w 800\n\n";
|
|
97
|
+
std::cout << " # Batch with max speed\n";
|
|
98
|
+
std::cout << " " << program_name << " batch photos/ thumbnails/ -w 800 --max-speed\n\n";
|
|
99
|
+
std::cout << " # Show image info\n";
|
|
100
|
+
std::cout << " " << program_name << " info photo.jpg\n\n";
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
bool parse_int(const char* str, int& value) {
|
|
104
|
+
char* end;
|
|
105
|
+
long val = strtol(str, &end, 10);
|
|
106
|
+
if (end == str || *end != '\0' || val < 0 || val > INT_MAX) {
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
value = static_cast<int>(val);
|
|
110
|
+
return true;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
bool parse_float(const char* str, float& value) {
|
|
114
|
+
char* end;
|
|
115
|
+
float val = strtof(str, &end);
|
|
116
|
+
if (end == str || *end != '\0' || val <= 0) {
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
value = val;
|
|
120
|
+
return true;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
bool parse_filter(const char* str, fastresize::ResizeOptions& opts) {
|
|
124
|
+
std::string filter = str;
|
|
125
|
+
if (filter == "mitchell") {
|
|
126
|
+
opts.filter = fastresize::ResizeOptions::MITCHELL;
|
|
127
|
+
} else if (filter == "catmull_rom" || filter == "catmull-rom") {
|
|
128
|
+
opts.filter = fastresize::ResizeOptions::CATMULL_ROM;
|
|
129
|
+
} else if (filter == "box") {
|
|
130
|
+
opts.filter = fastresize::ResizeOptions::BOX;
|
|
131
|
+
} else if (filter == "triangle") {
|
|
132
|
+
opts.filter = fastresize::ResizeOptions::TRIANGLE;
|
|
133
|
+
} else {
|
|
134
|
+
return false;
|
|
135
|
+
}
|
|
136
|
+
return true;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// Create directory recursively
|
|
140
|
+
bool mkdir_p(const std::string& path) {
|
|
141
|
+
struct stat st;
|
|
142
|
+
if (stat(path.c_str(), &st) == 0) {
|
|
143
|
+
return S_ISDIR(st.st_mode);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// Try to create directory
|
|
147
|
+
if (mkdir(path.c_str(), 0755) == 0) {
|
|
148
|
+
return true;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (errno != ENOENT) {
|
|
152
|
+
return false;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// Parent doesn't exist, create it
|
|
156
|
+
size_t pos = path.find_last_of('/');
|
|
157
|
+
if (pos == std::string::npos) {
|
|
158
|
+
return false;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
if (!mkdir_p(path.substr(0, pos))) {
|
|
162
|
+
return false;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
return mkdir(path.c_str(), 0755) == 0;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// Get all image files in directory
|
|
169
|
+
bool get_image_files(const std::string& dir, std::vector<std::string>& files) {
|
|
170
|
+
DIR* dp = opendir(dir.c_str());
|
|
171
|
+
if (!dp) {
|
|
172
|
+
std::cerr << "Error: Cannot open directory: " << dir << std::endl;
|
|
173
|
+
return false;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
struct dirent* entry;
|
|
177
|
+
while ((entry = readdir(dp)) != nullptr) {
|
|
178
|
+
std::string name = entry->d_name;
|
|
179
|
+
if (name == "." || name == "..") continue;
|
|
180
|
+
|
|
181
|
+
// Check if it's an image file
|
|
182
|
+
size_t dot = name.find_last_of('.');
|
|
183
|
+
if (dot == std::string::npos) continue;
|
|
184
|
+
|
|
185
|
+
std::string ext = name.substr(dot);
|
|
186
|
+
// Convert to lowercase
|
|
187
|
+
for (char& c : ext) c = tolower(c);
|
|
188
|
+
|
|
189
|
+
if (ext == ".jpg" || ext == ".jpeg" || ext == ".png" ||
|
|
190
|
+
ext == ".webp" || ext == ".bmp") {
|
|
191
|
+
std::string full_path = dir;
|
|
192
|
+
if (full_path.back() != '/') full_path += '/';
|
|
193
|
+
full_path += name;
|
|
194
|
+
files.push_back(full_path);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
closedir(dp);
|
|
199
|
+
return true;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// Extract filename from path
|
|
203
|
+
std::string get_filename(const std::string& path) {
|
|
204
|
+
size_t pos = path.find_last_of('/');
|
|
205
|
+
if (pos == std::string::npos) {
|
|
206
|
+
return path;
|
|
207
|
+
}
|
|
208
|
+
return path.substr(pos + 1);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// Command: info
|
|
212
|
+
int cmd_info(const std::string& image_path) {
|
|
213
|
+
fastresize::ImageInfo info = fastresize::get_image_info(image_path);
|
|
214
|
+
if (info.width == 0) {
|
|
215
|
+
std::cerr << "Error: " << fastresize::get_last_error() << std::endl;
|
|
216
|
+
return 1;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
std::cout << "Image: " << image_path << std::endl;
|
|
220
|
+
std::cout << " Format: " << info.format << std::endl;
|
|
221
|
+
std::cout << " Size: " << info.width << "x" << info.height << std::endl;
|
|
222
|
+
std::cout << " Channels: " << info.channels;
|
|
223
|
+
if (info.channels == 3) std::cout << " (RGB)";
|
|
224
|
+
else if (info.channels == 4) std::cout << " (RGBA)";
|
|
225
|
+
else if (info.channels == 1) std::cout << " (Grayscale)";
|
|
226
|
+
std::cout << std::endl;
|
|
227
|
+
|
|
228
|
+
return 0;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// Command: batch resize
|
|
232
|
+
int cmd_batch(int argc, char* argv[]) {
|
|
233
|
+
if (argc < 4) {
|
|
234
|
+
std::cerr << "Error: batch command requires input_dir and output_dir\n";
|
|
235
|
+
std::cerr << "Usage: " << argv[0] << " batch [OPTIONS] <input_dir> <output_dir>\n";
|
|
236
|
+
return 1;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
fastresize::ResizeOptions resize_opts;
|
|
240
|
+
fastresize::BatchOptions batch_opts;
|
|
241
|
+
std::string input_dir;
|
|
242
|
+
std::string output_dir;
|
|
243
|
+
|
|
244
|
+
// Parse arguments
|
|
245
|
+
for (int i = 2; i < argc; i++) {
|
|
246
|
+
std::string arg = argv[i];
|
|
247
|
+
|
|
248
|
+
if (arg == "-w" || arg == "--width") {
|
|
249
|
+
if (++i >= argc) {
|
|
250
|
+
std::cerr << "Error: " << arg << " requires an argument\n";
|
|
251
|
+
return 1;
|
|
252
|
+
}
|
|
253
|
+
if (!parse_int(argv[i], resize_opts.target_width)) {
|
|
254
|
+
std::cerr << "Error: Invalid width\n";
|
|
255
|
+
return 1;
|
|
256
|
+
}
|
|
257
|
+
} else if (arg == "-h" || arg == "--height") {
|
|
258
|
+
if (++i >= argc) {
|
|
259
|
+
std::cerr << "Error: " << arg << " requires an argument\n";
|
|
260
|
+
return 1;
|
|
261
|
+
}
|
|
262
|
+
if (!parse_int(argv[i], resize_opts.target_height)) {
|
|
263
|
+
std::cerr << "Error: Invalid height\n";
|
|
264
|
+
return 1;
|
|
265
|
+
}
|
|
266
|
+
} else if (arg == "-s" || arg == "--scale") {
|
|
267
|
+
if (++i >= argc) {
|
|
268
|
+
std::cerr << "Error: " << arg << " requires an argument\n";
|
|
269
|
+
return 1;
|
|
270
|
+
}
|
|
271
|
+
if (!parse_float(argv[i], resize_opts.scale_percent)) {
|
|
272
|
+
std::cerr << "Error: Invalid scale\n";
|
|
273
|
+
return 1;
|
|
274
|
+
}
|
|
275
|
+
resize_opts.mode = fastresize::ResizeOptions::SCALE_PERCENT;
|
|
276
|
+
} else if (arg == "-q" || arg == "--quality") {
|
|
277
|
+
if (++i >= argc) {
|
|
278
|
+
std::cerr << "Error: " << arg << " requires an argument\n";
|
|
279
|
+
return 1;
|
|
280
|
+
}
|
|
281
|
+
if (!parse_int(argv[i], resize_opts.quality) ||
|
|
282
|
+
resize_opts.quality < 1 || resize_opts.quality > 100) {
|
|
283
|
+
std::cerr << "Error: Quality must be between 1 and 100\n";
|
|
284
|
+
return 1;
|
|
285
|
+
}
|
|
286
|
+
} else if (arg == "-f" || arg == "--filter") {
|
|
287
|
+
if (++i >= argc) {
|
|
288
|
+
std::cerr << "Error: " << arg << " requires an argument\n";
|
|
289
|
+
return 1;
|
|
290
|
+
}
|
|
291
|
+
if (!parse_filter(argv[i], resize_opts)) {
|
|
292
|
+
std::cerr << "Error: Invalid filter. Use mitchell, catmull_rom, box, or triangle\n";
|
|
293
|
+
return 1;
|
|
294
|
+
}
|
|
295
|
+
} else if (arg == "--no-aspect-ratio") {
|
|
296
|
+
resize_opts.keep_aspect_ratio = false;
|
|
297
|
+
} else if (arg == "-t" || arg == "--threads") {
|
|
298
|
+
if (++i >= argc) {
|
|
299
|
+
std::cerr << "Error: " << arg << " requires an argument\n";
|
|
300
|
+
return 1;
|
|
301
|
+
}
|
|
302
|
+
if (!parse_int(argv[i], batch_opts.num_threads) || batch_opts.num_threads < 0) {
|
|
303
|
+
std::cerr << "Error: Invalid thread count\n";
|
|
304
|
+
return 1;
|
|
305
|
+
}
|
|
306
|
+
} else if (arg == "--stop-on-error") {
|
|
307
|
+
batch_opts.stop_on_error = true;
|
|
308
|
+
} else if (arg == "--max-speed") {
|
|
309
|
+
batch_opts.max_speed = true;
|
|
310
|
+
} else if (arg[0] == '-') {
|
|
311
|
+
std::cerr << "Error: Unknown option: " << arg << std::endl;
|
|
312
|
+
return 1;
|
|
313
|
+
} else {
|
|
314
|
+
// Non-option arguments
|
|
315
|
+
if (input_dir.empty()) {
|
|
316
|
+
input_dir = arg;
|
|
317
|
+
} else if (output_dir.empty()) {
|
|
318
|
+
output_dir = arg;
|
|
319
|
+
} else {
|
|
320
|
+
std::cerr << "Error: Too many arguments\n";
|
|
321
|
+
return 1;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
if (input_dir.empty() || output_dir.empty()) {
|
|
327
|
+
std::cerr << "Error: Missing input_dir or output_dir\n";
|
|
328
|
+
return 1;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
// Determine resize mode based on what was specified
|
|
332
|
+
if (resize_opts.mode != fastresize::ResizeOptions::SCALE_PERCENT) {
|
|
333
|
+
if (resize_opts.target_width > 0 && resize_opts.target_height > 0) {
|
|
334
|
+
resize_opts.mode = fastresize::ResizeOptions::EXACT_SIZE;
|
|
335
|
+
} else if (resize_opts.target_width > 0) {
|
|
336
|
+
resize_opts.mode = fastresize::ResizeOptions::FIT_WIDTH;
|
|
337
|
+
} else if (resize_opts.target_height > 0) {
|
|
338
|
+
resize_opts.mode = fastresize::ResizeOptions::FIT_HEIGHT;
|
|
339
|
+
} else {
|
|
340
|
+
std::cerr << "Error: Must specify width, height, or scale\n";
|
|
341
|
+
return 1;
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
// Get all image files
|
|
346
|
+
std::vector<std::string> input_files;
|
|
347
|
+
if (!get_image_files(input_dir, input_files)) {
|
|
348
|
+
return 1;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
if (input_files.empty()) {
|
|
352
|
+
std::cerr << "Error: No image files found in " << input_dir << std::endl;
|
|
353
|
+
return 1;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
std::cout << "Processing " << input_files.size() << " images..." << std::endl;
|
|
357
|
+
|
|
358
|
+
// Perform batch resize
|
|
359
|
+
fastresize::BatchResult result = fastresize::batch_resize(
|
|
360
|
+
input_files, output_dir, resize_opts, batch_opts);
|
|
361
|
+
|
|
362
|
+
std::cout << "Done: " << result.success << " success, "
|
|
363
|
+
<< result.failed << " failed" << std::endl;
|
|
364
|
+
|
|
365
|
+
if (!result.errors.empty()) {
|
|
366
|
+
std::cerr << "\nErrors:" << std::endl;
|
|
367
|
+
for (const auto& error : result.errors) {
|
|
368
|
+
std::cerr << " " << error << std::endl;
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
return result.failed > 0 ? 1 : 0;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
// Command: single image resize
|
|
376
|
+
int cmd_resize(int argc, char* argv[]) {
|
|
377
|
+
fastresize::ResizeOptions opts;
|
|
378
|
+
std::string input_path;
|
|
379
|
+
std::string output_path;
|
|
380
|
+
int positional_width = 0;
|
|
381
|
+
int positional_height = 0;
|
|
382
|
+
bool has_positional_args = false;
|
|
383
|
+
|
|
384
|
+
// Parse arguments
|
|
385
|
+
for (int i = 1; i < argc; i++) {
|
|
386
|
+
std::string arg = argv[i];
|
|
387
|
+
|
|
388
|
+
if (arg == "--help") {
|
|
389
|
+
print_usage(argv[0]);
|
|
390
|
+
return 0;
|
|
391
|
+
} else if (arg == "--version") {
|
|
392
|
+
std::cout << "FastResize v" << get_version() << std::endl;
|
|
393
|
+
return 0;
|
|
394
|
+
} else if (arg == "-w" || arg == "--width") {
|
|
395
|
+
if (++i >= argc) {
|
|
396
|
+
std::cerr << "Error: " << arg << " requires an argument\n";
|
|
397
|
+
return 1;
|
|
398
|
+
}
|
|
399
|
+
if (!parse_int(argv[i], opts.target_width)) {
|
|
400
|
+
std::cerr << "Error: Invalid width\n";
|
|
401
|
+
return 1;
|
|
402
|
+
}
|
|
403
|
+
} else if (arg == "-h" || arg == "--height") {
|
|
404
|
+
if (++i >= argc) {
|
|
405
|
+
std::cerr << "Error: " << arg << " requires an argument\n";
|
|
406
|
+
return 1;
|
|
407
|
+
}
|
|
408
|
+
if (!parse_int(argv[i], opts.target_height)) {
|
|
409
|
+
std::cerr << "Error: Invalid height\n";
|
|
410
|
+
return 1;
|
|
411
|
+
}
|
|
412
|
+
} else if (arg == "-s" || arg == "--scale") {
|
|
413
|
+
if (++i >= argc) {
|
|
414
|
+
std::cerr << "Error: " << arg << " requires an argument\n";
|
|
415
|
+
return 1;
|
|
416
|
+
}
|
|
417
|
+
if (!parse_float(argv[i], opts.scale_percent)) {
|
|
418
|
+
std::cerr << "Error: Invalid scale\n";
|
|
419
|
+
return 1;
|
|
420
|
+
}
|
|
421
|
+
opts.mode = fastresize::ResizeOptions::SCALE_PERCENT;
|
|
422
|
+
} else if (arg == "-q" || arg == "--quality") {
|
|
423
|
+
if (++i >= argc) {
|
|
424
|
+
std::cerr << "Error: " << arg << " requires an argument\n";
|
|
425
|
+
return 1;
|
|
426
|
+
}
|
|
427
|
+
if (!parse_int(argv[i], opts.quality) ||
|
|
428
|
+
opts.quality < 1 || opts.quality > 100) {
|
|
429
|
+
std::cerr << "Error: Quality must be between 1 and 100\n";
|
|
430
|
+
return 1;
|
|
431
|
+
}
|
|
432
|
+
} else if (arg == "-f" || arg == "--filter") {
|
|
433
|
+
if (++i >= argc) {
|
|
434
|
+
std::cerr << "Error: " << arg << " requires an argument\n";
|
|
435
|
+
return 1;
|
|
436
|
+
}
|
|
437
|
+
if (!parse_filter(argv[i], opts)) {
|
|
438
|
+
std::cerr << "Error: Invalid filter. Use mitchell, catmull_rom, box, or triangle\n";
|
|
439
|
+
return 1;
|
|
440
|
+
}
|
|
441
|
+
} else if (arg == "--no-aspect-ratio") {
|
|
442
|
+
opts.keep_aspect_ratio = false;
|
|
443
|
+
} else if (arg == "-o" || arg == "--overwrite") {
|
|
444
|
+
opts.overwrite_input = true;
|
|
445
|
+
} else if (arg[0] == '-') {
|
|
446
|
+
std::cerr << "Error: Unknown option: " << arg << std::endl;
|
|
447
|
+
return 1;
|
|
448
|
+
} else {
|
|
449
|
+
// Positional arguments: input output [width] [height]
|
|
450
|
+
if (input_path.empty()) {
|
|
451
|
+
input_path = arg;
|
|
452
|
+
} else if (output_path.empty()) {
|
|
453
|
+
output_path = arg;
|
|
454
|
+
} else if (positional_width == 0) {
|
|
455
|
+
if (!parse_int(arg.c_str(), positional_width)) {
|
|
456
|
+
std::cerr << "Error: Invalid width: " << arg << std::endl;
|
|
457
|
+
return 1;
|
|
458
|
+
}
|
|
459
|
+
has_positional_args = true;
|
|
460
|
+
} else if (positional_height == 0) {
|
|
461
|
+
if (!parse_int(arg.c_str(), positional_height)) {
|
|
462
|
+
std::cerr << "Error: Invalid height: " << arg << std::endl;
|
|
463
|
+
return 1;
|
|
464
|
+
}
|
|
465
|
+
has_positional_args = true;
|
|
466
|
+
} else {
|
|
467
|
+
std::cerr << "Error: Too many arguments\n";
|
|
468
|
+
return 1;
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
// Validate required arguments
|
|
474
|
+
if (input_path.empty() || output_path.empty()) {
|
|
475
|
+
std::cerr << "Error: Missing required arguments\n";
|
|
476
|
+
print_usage(argv[0]);
|
|
477
|
+
return 1;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
// Use positional width/height if provided
|
|
481
|
+
if (has_positional_args) {
|
|
482
|
+
if (positional_width > 0) opts.target_width = positional_width;
|
|
483
|
+
if (positional_height > 0) opts.target_height = positional_height;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
// Determine resize mode
|
|
487
|
+
if (opts.mode != fastresize::ResizeOptions::SCALE_PERCENT) {
|
|
488
|
+
if (opts.target_width > 0 && opts.target_height > 0) {
|
|
489
|
+
opts.mode = fastresize::ResizeOptions::EXACT_SIZE;
|
|
490
|
+
} else if (opts.target_width > 0) {
|
|
491
|
+
opts.mode = fastresize::ResizeOptions::FIT_WIDTH;
|
|
492
|
+
} else if (opts.target_height > 0) {
|
|
493
|
+
opts.mode = fastresize::ResizeOptions::FIT_HEIGHT;
|
|
494
|
+
} else {
|
|
495
|
+
std::cerr << "Error: Must specify width, height, or scale\n";
|
|
496
|
+
return 1;
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
// Perform resize
|
|
501
|
+
if (!fastresize::resize(input_path, output_path, opts)) {
|
|
502
|
+
std::cerr << "Error: " << fastresize::get_last_error() << std::endl;
|
|
503
|
+
return 1;
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
std::cout << "✓ Resized successfully: " << output_path << std::endl;
|
|
507
|
+
return 0;
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
int main(int argc, char* argv[]) {
|
|
511
|
+
if (argc < 2) {
|
|
512
|
+
print_usage(argv[0]);
|
|
513
|
+
return 1;
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
std::string command = argv[1];
|
|
517
|
+
|
|
518
|
+
// Check for global options
|
|
519
|
+
if (command == "--help") {
|
|
520
|
+
print_usage(argv[0]);
|
|
521
|
+
return 0;
|
|
522
|
+
} else if (command == "--version") {
|
|
523
|
+
std::cout << "FastResize v" << get_version() << std::endl;
|
|
524
|
+
return 0;
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
// Check for commands
|
|
528
|
+
if (command == "batch") {
|
|
529
|
+
return cmd_batch(argc, argv);
|
|
530
|
+
} else if (command == "info") {
|
|
531
|
+
if (argc < 3) {
|
|
532
|
+
std::cerr << "Error: info command requires image path\n";
|
|
533
|
+
return 1;
|
|
534
|
+
}
|
|
535
|
+
return cmd_info(argv[2]);
|
|
536
|
+
} else {
|
|
537
|
+
// Default: single image resize
|
|
538
|
+
return cmd_resize(argc, argv);
|
|
539
|
+
}
|
|
540
|
+
}
|