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.
- checksums.yaml +4 -4
- data/VERSION +1 -1
- data/bindings/ruby/ext/fastresize/extconf.rb +79 -105
- data/bindings/ruby/lib/fastresize/platform.rb +102 -56
- data/bindings/ruby/lib/fastresize/version.rb +1 -1
- data/bindings/ruby/lib/fastresize.rb +321 -6
- data/bindings/ruby/prebuilt/linux-aarch64/bin/fast_resize +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/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/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/lib/libfastresize.a +0 -0
- data/bindings/ruby/prebuilt/macos-x86_64.tar.gz +0 -0
- metadata +4 -22
- data/CMakeLists.txt +0 -311
- data/bindings/ruby/ext/fastresize/fastresize_ext.cpp +0 -377
- data/include/fastresize.h +0 -189
- data/include/stb_image.h +0 -7988
- data/include/stb_image_resize2.h +0 -10651
- data/include/stb_image_write.h +0 -1724
- data/src/cli.cpp +0 -540
- data/src/decoder.cpp +0 -647
- data/src/encoder.cpp +0 -376
- data/src/fastresize.cpp +0 -445
- data/src/internal.h +0 -108
- data/src/pipeline.cpp +0 -284
- data/src/pipeline.h +0 -175
- data/src/resizer.cpp +0 -199
- data/src/simd_resize.cpp +0 -384
- data/src/simd_resize.h +0 -72
- data/src/simd_utils.h +0 -127
- data/src/thread_pool.cpp +0 -232
data/src/encoder.cpp
DELETED
|
@@ -1,376 +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 "internal.h"
|
|
42
|
-
#include <cstdio>
|
|
43
|
-
#include <csetjmp>
|
|
44
|
-
|
|
45
|
-
#include <jpeglib.h>
|
|
46
|
-
#include <png.h>
|
|
47
|
-
#include <webp/encode.h>
|
|
48
|
-
|
|
49
|
-
#define STB_IMAGE_WRITE_IMPLEMENTATION
|
|
50
|
-
#include "stb_image_write.h"
|
|
51
|
-
|
|
52
|
-
#if defined(__ARM_NEON) || defined(__aarch64__)
|
|
53
|
-
#define USE_NEON 1
|
|
54
|
-
#include <arm_neon.h>
|
|
55
|
-
#elif defined(__SSE2__) || defined(__x86_64__) || defined(_M_X64)
|
|
56
|
-
#define USE_SSE2 1
|
|
57
|
-
#include <emmintrin.h>
|
|
58
|
-
#endif
|
|
59
|
-
|
|
60
|
-
namespace fastresize {
|
|
61
|
-
namespace internal {
|
|
62
|
-
|
|
63
|
-
#ifdef USE_NEON
|
|
64
|
-
static void convert_rgba_to_rgb_neon(const unsigned char* __restrict src,
|
|
65
|
-
unsigned char* __restrict dst,
|
|
66
|
-
int pixel_count) {
|
|
67
|
-
int i = 0;
|
|
68
|
-
|
|
69
|
-
for (; i + 16 <= pixel_count; i += 16) {
|
|
70
|
-
uint8x16x4_t rgba = vld4q_u8(src + i * 4);
|
|
71
|
-
|
|
72
|
-
uint8x16x3_t rgb;
|
|
73
|
-
rgb.val[0] = rgba.val[0]; // R
|
|
74
|
-
rgb.val[1] = rgba.val[1]; // G
|
|
75
|
-
rgb.val[2] = rgba.val[2]; // B
|
|
76
|
-
|
|
77
|
-
vst3q_u8(dst + i * 3, rgb);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
for (; i < pixel_count; i++) {
|
|
81
|
-
dst[i * 3 + 0] = src[i * 4 + 0];
|
|
82
|
-
dst[i * 3 + 1] = src[i * 4 + 1];
|
|
83
|
-
dst[i * 3 + 2] = src[i * 4 + 2];
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
#endif
|
|
87
|
-
|
|
88
|
-
#ifdef USE_SSE2
|
|
89
|
-
static void convert_rgba_to_rgb_sse2(const unsigned char* __restrict src,
|
|
90
|
-
unsigned char* __restrict dst,
|
|
91
|
-
int pixel_count) {
|
|
92
|
-
int i = 0;
|
|
93
|
-
|
|
94
|
-
for (; i + 4 <= pixel_count; i += 4) {
|
|
95
|
-
__m128i rgba = _mm_loadu_si128((__m128i*)(src + i * 4));
|
|
96
|
-
|
|
97
|
-
unsigned char temp[16];
|
|
98
|
-
_mm_storeu_si128((__m128i*)temp, rgba);
|
|
99
|
-
|
|
100
|
-
dst[i * 3 + 0] = temp[0]; dst[i * 3 + 1] = temp[1]; dst[i * 3 + 2] = temp[2];
|
|
101
|
-
dst[i * 3 + 3] = temp[4]; dst[i * 3 + 4] = temp[5]; dst[i * 3 + 5] = temp[6];
|
|
102
|
-
dst[i * 3 + 6] = temp[8]; dst[i * 3 + 7] = temp[9]; dst[i * 3 + 8] = temp[10];
|
|
103
|
-
dst[i * 3 + 9] = temp[12]; dst[i * 3 + 10] = temp[13]; dst[i * 3 + 11] = temp[14];
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
for (; i < pixel_count; i++) {
|
|
107
|
-
dst[i * 3 + 0] = src[i * 4 + 0];
|
|
108
|
-
dst[i * 3 + 1] = src[i * 4 + 1];
|
|
109
|
-
dst[i * 3 + 2] = src[i * 4 + 2];
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
#endif
|
|
113
|
-
|
|
114
|
-
static void convert_rgba_to_rgb_scalar(const unsigned char* __restrict src,
|
|
115
|
-
unsigned char* __restrict dst,
|
|
116
|
-
int pixel_count) {
|
|
117
|
-
for (int i = 0; i < pixel_count; i++) {
|
|
118
|
-
dst[i * 3 + 0] = src[i * 4 + 0];
|
|
119
|
-
dst[i * 3 + 1] = src[i * 4 + 1];
|
|
120
|
-
dst[i * 3 + 2] = src[i * 4 + 2];
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
static void convert_rgba_to_rgb(const unsigned char* src, unsigned char* dst, int pixel_count) {
|
|
125
|
-
#ifdef USE_NEON
|
|
126
|
-
convert_rgba_to_rgb_neon(src, dst, pixel_count);
|
|
127
|
-
#elif defined(USE_SSE2)
|
|
128
|
-
convert_rgba_to_rgb_sse2(src, dst, pixel_count);
|
|
129
|
-
#else
|
|
130
|
-
convert_rgba_to_rgb_scalar(src, dst, pixel_count);
|
|
131
|
-
#endif
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
struct jpeg_error_mgr_ext {
|
|
135
|
-
struct jpeg_error_mgr pub;
|
|
136
|
-
jmp_buf setjmp_buffer;
|
|
137
|
-
};
|
|
138
|
-
|
|
139
|
-
static void jpeg_encode_error_exit(j_common_ptr cinfo) {
|
|
140
|
-
jpeg_error_mgr_ext* myerr = (jpeg_error_mgr_ext*)cinfo->err;
|
|
141
|
-
longjmp(myerr->setjmp_buffer, 1);
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
static const int JPEG_SCANLINE_BATCH = 8;
|
|
145
|
-
|
|
146
|
-
bool encode_jpeg(const std::string& path, const ImageData& data, int quality, BufferPool* buffer_pool = nullptr) {
|
|
147
|
-
FILE* outfile = fopen(path.c_str(), "wb");
|
|
148
|
-
if (!outfile) return false;
|
|
149
|
-
|
|
150
|
-
unsigned char* rgb_buffer = nullptr;
|
|
151
|
-
size_t rgb_buffer_capacity = 0;
|
|
152
|
-
|
|
153
|
-
struct jpeg_compress_struct cinfo;
|
|
154
|
-
struct jpeg_error_mgr_ext jerr;
|
|
155
|
-
|
|
156
|
-
cinfo.err = jpeg_std_error(&jerr.pub);
|
|
157
|
-
jerr.pub.error_exit = jpeg_encode_error_exit;
|
|
158
|
-
|
|
159
|
-
if (setjmp(jerr.setjmp_buffer)) {
|
|
160
|
-
jpeg_destroy_compress(&cinfo);
|
|
161
|
-
fclose(outfile);
|
|
162
|
-
if (rgb_buffer) {
|
|
163
|
-
if (rgb_buffer_capacity > 0 && buffer_pool) {
|
|
164
|
-
buffer_pool_release(buffer_pool, rgb_buffer, rgb_buffer_capacity);
|
|
165
|
-
} else {
|
|
166
|
-
delete[] rgb_buffer;
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
return false;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
jpeg_create_compress(&cinfo);
|
|
173
|
-
jpeg_stdio_dest(&cinfo, outfile);
|
|
174
|
-
|
|
175
|
-
cinfo.image_width = data.width;
|
|
176
|
-
cinfo.image_height = data.height;
|
|
177
|
-
|
|
178
|
-
const unsigned char* encode_pixels = data.pixels;
|
|
179
|
-
int encode_components = data.channels;
|
|
180
|
-
|
|
181
|
-
if (data.channels == 1) {
|
|
182
|
-
cinfo.in_color_space = JCS_GRAYSCALE;
|
|
183
|
-
cinfo.input_components = 1;
|
|
184
|
-
} else if (data.channels == 3) {
|
|
185
|
-
cinfo.in_color_space = JCS_RGB;
|
|
186
|
-
cinfo.input_components = 3;
|
|
187
|
-
} else if (data.channels == 4) {
|
|
188
|
-
cinfo.in_color_space = JCS_RGB;
|
|
189
|
-
cinfo.input_components = 3;
|
|
190
|
-
encode_components = 3;
|
|
191
|
-
|
|
192
|
-
size_t rgb_size = static_cast<size_t>(data.width) * data.height * 3;
|
|
193
|
-
if (buffer_pool) {
|
|
194
|
-
rgb_buffer = buffer_pool_acquire(buffer_pool, rgb_size);
|
|
195
|
-
rgb_buffer_capacity = rgb_size;
|
|
196
|
-
} else {
|
|
197
|
-
rgb_buffer = new unsigned char[rgb_size];
|
|
198
|
-
rgb_buffer_capacity = 0;
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
convert_rgba_to_rgb(data.pixels, rgb_buffer, data.width * data.height);
|
|
202
|
-
encode_pixels = rgb_buffer;
|
|
203
|
-
} else {
|
|
204
|
-
jpeg_destroy_compress(&cinfo);
|
|
205
|
-
fclose(outfile);
|
|
206
|
-
return false;
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
jpeg_set_defaults(&cinfo);
|
|
210
|
-
jpeg_set_quality(&cinfo, quality, TRUE);
|
|
211
|
-
|
|
212
|
-
cinfo.dct_method = JDCT_IFAST;
|
|
213
|
-
|
|
214
|
-
jpeg_start_compress(&cinfo, TRUE);
|
|
215
|
-
|
|
216
|
-
size_t row_stride = data.width * encode_components;
|
|
217
|
-
JSAMPROW row_pointers[JPEG_SCANLINE_BATCH];
|
|
218
|
-
|
|
219
|
-
while (cinfo.next_scanline < cinfo.image_height) {
|
|
220
|
-
int remaining = cinfo.image_height - cinfo.next_scanline;
|
|
221
|
-
int batch_size = (remaining < JPEG_SCANLINE_BATCH) ? remaining : JPEG_SCANLINE_BATCH;
|
|
222
|
-
|
|
223
|
-
for (int i = 0; i < batch_size; i++) {
|
|
224
|
-
row_pointers[i] = const_cast<JSAMPROW>(
|
|
225
|
-
&encode_pixels[(cinfo.next_scanline + i) * row_stride]
|
|
226
|
-
);
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
jpeg_write_scanlines(&cinfo, row_pointers, batch_size);
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
jpeg_finish_compress(&cinfo);
|
|
233
|
-
jpeg_destroy_compress(&cinfo);
|
|
234
|
-
fclose(outfile);
|
|
235
|
-
|
|
236
|
-
if (rgb_buffer) {
|
|
237
|
-
if (rgb_buffer_capacity > 0 && buffer_pool) {
|
|
238
|
-
buffer_pool_release(buffer_pool, rgb_buffer, rgb_buffer_capacity);
|
|
239
|
-
} else {
|
|
240
|
-
delete[] rgb_buffer;
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
return true;
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
bool encode_png(const std::string& path, const ImageData& data, int quality) {
|
|
248
|
-
FILE* fp = fopen(path.c_str(), "wb");
|
|
249
|
-
if (!fp) return false;
|
|
250
|
-
|
|
251
|
-
png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
|
|
252
|
-
if (!png) {
|
|
253
|
-
fclose(fp);
|
|
254
|
-
return false;
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
png_infop info = png_create_info_struct(png);
|
|
258
|
-
if (!info) {
|
|
259
|
-
png_destroy_write_struct(&png, nullptr);
|
|
260
|
-
fclose(fp);
|
|
261
|
-
return false;
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
if (setjmp(png_jmpbuf(png))) {
|
|
265
|
-
png_destroy_write_struct(&png, &info);
|
|
266
|
-
fclose(fp);
|
|
267
|
-
return false;
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
png_init_io(png, fp);
|
|
271
|
-
|
|
272
|
-
int compression_level = 9 - ((quality - 1) * 9 / 99);
|
|
273
|
-
if (compression_level < 0) compression_level = 0;
|
|
274
|
-
if (compression_level > 9) compression_level = 9;
|
|
275
|
-
png_set_compression_level(png, compression_level);
|
|
276
|
-
|
|
277
|
-
int color_type;
|
|
278
|
-
switch (data.channels) {
|
|
279
|
-
case 1: color_type = PNG_COLOR_TYPE_GRAY; break;
|
|
280
|
-
case 2: color_type = PNG_COLOR_TYPE_GRAY_ALPHA; break;
|
|
281
|
-
case 3: color_type = PNG_COLOR_TYPE_RGB; break;
|
|
282
|
-
case 4: color_type = PNG_COLOR_TYPE_RGB_ALPHA; break;
|
|
283
|
-
default:
|
|
284
|
-
png_destroy_write_struct(&png, &info);
|
|
285
|
-
fclose(fp);
|
|
286
|
-
return false;
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
png_set_IHDR(png, info, data.width, data.height, 8,
|
|
290
|
-
color_type, PNG_INTERLACE_NONE,
|
|
291
|
-
PNG_COMPRESSION_TYPE_DEFAULT,
|
|
292
|
-
PNG_FILTER_TYPE_DEFAULT);
|
|
293
|
-
|
|
294
|
-
png_write_info(png, info);
|
|
295
|
-
|
|
296
|
-
size_t row_bytes = data.width * data.channels;
|
|
297
|
-
png_bytep* row_pointers = new png_bytep[data.height];
|
|
298
|
-
for (int y = 0; y < data.height; y++) {
|
|
299
|
-
row_pointers[y] = data.pixels + y * row_bytes;
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
png_write_image(png, row_pointers);
|
|
303
|
-
png_write_end(png, nullptr);
|
|
304
|
-
|
|
305
|
-
delete[] row_pointers;
|
|
306
|
-
png_destroy_write_struct(&png, &info);
|
|
307
|
-
fclose(fp);
|
|
308
|
-
|
|
309
|
-
return true;
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
bool encode_webp(const std::string& path, const ImageData& data, int quality) {
|
|
313
|
-
uint8_t* output = nullptr;
|
|
314
|
-
size_t output_size = 0;
|
|
315
|
-
|
|
316
|
-
if (data.channels == 4) {
|
|
317
|
-
output_size = WebPEncodeRGBA(data.pixels, data.width, data.height,
|
|
318
|
-
data.width * 4, quality, &output);
|
|
319
|
-
} else if (data.channels == 3) {
|
|
320
|
-
output_size = WebPEncodeRGB(data.pixels, data.width, data.height,
|
|
321
|
-
data.width * 3, quality, &output);
|
|
322
|
-
} else {
|
|
323
|
-
return false;
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
if (output_size == 0 || !output) {
|
|
327
|
-
if (output) WebPFree(output);
|
|
328
|
-
return false;
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
FILE* fp = fopen(path.c_str(), "wb");
|
|
332
|
-
if (!fp) {
|
|
333
|
-
WebPFree(output);
|
|
334
|
-
return false;
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
size_t written = fwrite(output, 1, output_size, fp);
|
|
338
|
-
fclose(fp);
|
|
339
|
-
WebPFree(output);
|
|
340
|
-
|
|
341
|
-
return written == output_size;
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
bool encode_image(const std::string& path, const ImageData& data, ImageFormat format, int quality, BufferPool* buffer_pool) {
|
|
345
|
-
if (!data.pixels || data.width <= 0 || data.height <= 0) {
|
|
346
|
-
return false;
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
switch (format) {
|
|
350
|
-
case FORMAT_JPEG:
|
|
351
|
-
return encode_jpeg(path, data, quality, buffer_pool);
|
|
352
|
-
|
|
353
|
-
case FORMAT_PNG:
|
|
354
|
-
return encode_png(path, data, quality);
|
|
355
|
-
|
|
356
|
-
case FORMAT_WEBP:
|
|
357
|
-
return encode_webp(path, data, quality);
|
|
358
|
-
|
|
359
|
-
case FORMAT_BMP:
|
|
360
|
-
{
|
|
361
|
-
int result = stbi_write_bmp(path.c_str(), data.width, data.height, data.channels, data.pixels);
|
|
362
|
-
if (!result) {
|
|
363
|
-
set_last_error(ENCODE_ERROR, "Failed to encode BMP image");
|
|
364
|
-
return false;
|
|
365
|
-
}
|
|
366
|
-
return true;
|
|
367
|
-
}
|
|
368
|
-
|
|
369
|
-
default:
|
|
370
|
-
set_last_error(UNSUPPORTED_FORMAT, "Unsupported output format");
|
|
371
|
-
return false;
|
|
372
|
-
}
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
}
|
|
376
|
-
}
|