fast_resize 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. checksums.yaml +7 -0
  2. data/CMakeLists.txt +308 -0
  3. data/LICENSE +29 -0
  4. data/README.md +426 -0
  5. data/VERSION +1 -0
  6. data/bindings/ruby/ext/fastresize/extconf.rb +152 -0
  7. data/bindings/ruby/ext/fastresize/fastresize_ext.cpp +377 -0
  8. data/bindings/ruby/lib/fastresize/platform.rb +106 -0
  9. data/bindings/ruby/lib/fastresize/version.rb +5 -0
  10. data/bindings/ruby/lib/fastresize.rb +13 -0
  11. data/bindings/ruby/prebuilt/linux-aarch64/bin/fast_resize +0 -0
  12. data/bindings/ruby/prebuilt/linux-aarch64/include/fastresize.h +189 -0
  13. data/bindings/ruby/prebuilt/linux-aarch64/include/stb_image.h +7988 -0
  14. data/bindings/ruby/prebuilt/linux-aarch64/include/stb_image_resize2.h +10651 -0
  15. data/bindings/ruby/prebuilt/linux-aarch64/include/stb_image_write.h +1724 -0
  16. data/bindings/ruby/prebuilt/linux-aarch64/lib/libfastresize.a +0 -0
  17. data/bindings/ruby/prebuilt/linux-aarch64.tar.gz +0 -0
  18. data/bindings/ruby/prebuilt/linux-x86_64/bin/fast_resize +0 -0
  19. data/bindings/ruby/prebuilt/linux-x86_64/include/fastresize.h +189 -0
  20. data/bindings/ruby/prebuilt/linux-x86_64/include/stb_image.h +7988 -0
  21. data/bindings/ruby/prebuilt/linux-x86_64/include/stb_image_resize2.h +10651 -0
  22. data/bindings/ruby/prebuilt/linux-x86_64/include/stb_image_write.h +1724 -0
  23. data/bindings/ruby/prebuilt/linux-x86_64/lib/libfastresize.a +0 -0
  24. data/bindings/ruby/prebuilt/linux-x86_64.tar.gz +0 -0
  25. data/bindings/ruby/prebuilt/macos-arm64/bin/fast_resize +0 -0
  26. data/bindings/ruby/prebuilt/macos-arm64/include/fastresize.h +189 -0
  27. data/bindings/ruby/prebuilt/macos-arm64/include/stb_image.h +7988 -0
  28. data/bindings/ruby/prebuilt/macos-arm64/include/stb_image_resize2.h +10651 -0
  29. data/bindings/ruby/prebuilt/macos-arm64/include/stb_image_write.h +1724 -0
  30. data/bindings/ruby/prebuilt/macos-arm64/lib/libfastresize.a +0 -0
  31. data/bindings/ruby/prebuilt/macos-arm64.tar.gz +0 -0
  32. data/bindings/ruby/prebuilt/macos-x86_64/bin/fast_resize +0 -0
  33. data/bindings/ruby/prebuilt/macos-x86_64/include/fastresize.h +189 -0
  34. data/bindings/ruby/prebuilt/macos-x86_64/include/stb_image.h +7988 -0
  35. data/bindings/ruby/prebuilt/macos-x86_64/include/stb_image_resize2.h +10651 -0
  36. data/bindings/ruby/prebuilt/macos-x86_64/include/stb_image_write.h +1724 -0
  37. data/bindings/ruby/prebuilt/macos-x86_64/lib/libfastresize.a +0 -0
  38. data/bindings/ruby/prebuilt/macos-x86_64.tar.gz +0 -0
  39. data/include/fastresize.h +189 -0
  40. data/include/stb_image.h +7988 -0
  41. data/include/stb_image_resize2.h +10651 -0
  42. data/include/stb_image_write.h +1724 -0
  43. data/src/cli.cpp +540 -0
  44. data/src/decoder.cpp +647 -0
  45. data/src/encoder.cpp +376 -0
  46. data/src/fastresize.cpp +445 -0
  47. data/src/internal.h +108 -0
  48. data/src/pipeline.cpp +284 -0
  49. data/src/pipeline.h +175 -0
  50. data/src/resizer.cpp +199 -0
  51. data/src/simd_resize.cpp +384 -0
  52. data/src/simd_resize.h +72 -0
  53. data/src/simd_utils.h +127 -0
  54. data/src/thread_pool.cpp +232 -0
  55. metadata +158 -0
data/README.md ADDED
@@ -0,0 +1,426 @@
1
+ # FastResize
2
+
3
+ <div align="center">
4
+
5
+ **The fastest image resizing library on the planet.** 🚀
6
+
7
+ Resize 1,000 images in **2 seconds**. Up to **2.9x faster** than libvips, **3.1x faster** than imageflow. Uses **3-4x less RAM** than alternatives.
8
+
9
+ [![GitHub Stars](https://img.shields.io/github/stars/tranhuucanh/fast_resize?style=social)](https://github.com/tranhuucanh/fast_resize/stargazers)
10
+ [![GitHub Forks](https://img.shields.io/github/forks/tranhuucanh/fast_resize?style=social)](https://github.com/tranhuucanh/fast_resize/network/members)
11
+ [![Ask DeepWiki](https://deepwiki.com/badge.svg)](https://deepwiki.com/tranhuucanh/fast_resize)
12
+
13
+ [Installation](#-installation) • [Quick Start](#-quick-start) • [Documentation](#-documentation) • [Benchmarks](#-performance-benchmarks)
14
+
15
+ ---
16
+
17
+ ### 🛠️ Tech Stack & Stats
18
+
19
+ [![License](https://img.shields.io/badge/License-BSD--3--Clause-blue.svg)](LICENSE)
20
+ [![C++](https://img.shields.io/badge/C++-14-blue.svg)](https://isocpp.org/)
21
+ [![CMake](https://img.shields.io/badge/CMake-3.15+-064F8C.svg)](https://cmake.org/)
22
+ [![Shell](https://img.shields.io/badge/Shell-Bash-4EAA25.svg)](https://www.gnu.org/software/bash/)
23
+
24
+ 💎 **Ruby:** [![Gem](https://img.shields.io/badge/Gem-fast_resize-red.svg)](https://rubygems.org/gems/fast_resize) [![Gem Downloads](https://badgen.net/rubygems/dt/fast_resize)](https://rubygems.org/gems/fast_resize)
25
+
26
+ 📦 **CLI:** [![GitHub Downloads](https://img.shields.io/github/downloads/tranhuucanh/fast_resize/total)](https://github.com/tranhuucanh/fast_resize/releases)
27
+
28
+ </div>
29
+
30
+ ---
31
+
32
+ ## 📑 Table of Contents
33
+
34
+ - [Why Fastest on the Planet?](#-why-fastest-on-the-planet)
35
+ - [Performance Benchmarks](#-performance-benchmarks)
36
+ - [Key Features](#-key-features)
37
+ - [Installation](#-installation)
38
+ - [Quick Start](#-quick-start)
39
+ - [API Reference](#-api-reference)
40
+ - [Documentation](#-documentation)
41
+ - [Architecture](#️-architecture)
42
+ - [License](#-license)
43
+ - [Contributing](#-contributing)
44
+ - [Contact & Support](#-contact--support)
45
+
46
+ ---
47
+
48
+ ## 🏆 Why Fastest on the Planet?
49
+
50
+ <div align="center">
51
+
52
+ **FastResize has defeated the two most legendary image processing libraries in the world:**
53
+
54
+ </div>
55
+
56
+ | Library | Description | FastResize Advantage |
57
+ |---------|-------------|---------------------|
58
+ | **[libvips](https://www.libvips.org/)** | The gold standard for high-performance image processing. Used by Sharp (Node.js), Shopify, and Wikipedia. Widely recognized as one of the fastest image processing libraries available. | FastResize is **1.7x - 2.9x faster** |
59
+ | **[imageflow](https://www.imageflow.io/)** | A Rust-based image manipulation engine designed for high throughput and accuracy. Known for powering modern large-scale image resizing pipelines. | FastResize is **1.1x - 3.1x faster** |
60
+
61
+ ---
62
+
63
+ ## 🔥 Performance Benchmarks
64
+
65
+ <div align="center">
66
+
67
+ **Resize 1,000 images to 800px width**
68
+
69
+ *Tested on MacBook Pro M2, macOS Sonoma*
70
+
71
+ | Format | FastResize | libvips (parallel) | imageflow | 🚀 vs libvips | 🚀 vs imageflow |
72
+ |--------|------------|-------------------|-----------|---------------|-----------------|
73
+ | **JPG** | **2.10s** ⚡ | 5.24s | 6.60s | **2.5x faster** | **3.1x faster** 🏆 |
74
+ | **PNG** | **3.43s** ⚡ | 6.18s | 8.41s | **1.8x faster** | **2.5x faster** |
75
+ | **BMP** | **1.65s** ⚡ | 4.72s | ❌ N/A | **2.9x faster** | - |
76
+ | **WEBP** | **14.03s** ⚡ | 23.52s | 15.69s | **1.7x faster** | **1.1x faster** |
77
+
78
+ **FastResize wins in ALL formats!** 🎯
79
+
80
+ </div>
81
+
82
+ <details closed>
83
+ <summary>📊 <b>Speed Comparison Details</b></summary>
84
+
85
+ <br/>
86
+
87
+ ### JPG Performance (1179×1409, 3 channels)
88
+
89
+ | Tool | Run 1 | Run 2 | Avg Time | Throughput |
90
+ |------|-------|-------|----------|------------|
91
+ | **FastResize** | 2.02s | 2.17s | **2.10s** | **~477 img/s** ⚡ |
92
+ | libvips (parallel) | 5.20s | 5.28s | 5.24s | ~191 img/s |
93
+ | libvips (sequential) | 16.32s | 15.91s | 16.11s | ~62 img/s |
94
+ | imageflow | 6.62s | 6.58s | 6.60s | ~151 img/s |
95
+
96
+ ### PNG Performance (2104×1160, 4 channels)
97
+
98
+ | Tool | Run 1 | Run 2 | Avg Time | Throughput |
99
+ |------|-------|-------|----------|------------|
100
+ | **FastResize** | 3.55s | 3.32s | **3.43s** | **~292 img/s** ⚡ |
101
+ | libvips (parallel) | 6.18s | 6.18s | 6.18s | ~162 img/s |
102
+ | libvips (sequential) | 31.95s | 31.46s | 31.71s | ~32 img/s |
103
+ | imageflow | 8.45s | 8.37s | 8.41s | ~119 img/s |
104
+
105
+ ### BMP Performance (1045×1045, 3 channels)
106
+
107
+ | Tool | Run 1 | Run 2 | Avg Time | Throughput |
108
+ |------|-------|-------|----------|------------|
109
+ | **FastResize** | 1.65s | 1.64s | **1.65s** | **~606 img/s** ⚡ |
110
+ | libvips (parallel) | 4.75s | 4.70s | 4.72s | ~212 img/s |
111
+ | imageflow | ❌ | ❌ | Not supported | - |
112
+
113
+ ### WEBP Performance (4275×2451, 3 channels)
114
+
115
+ | Tool | Run 1 | Run 2 | Avg Time | Throughput |
116
+ |------|-------|-------|----------|------------|
117
+ | **FastResize** | 13.99s | 14.07s | **14.03s** | **~71 img/s** ⚡ |
118
+ | libvips (parallel) | 23.94s | 23.10s | 23.52s | ~43 img/s |
119
+ | imageflow | 15.64s | 15.73s | 15.69s | ~64 img/s |
120
+
121
+ </details>
122
+
123
+ <details closed>
124
+ <summary>💾 <b>RAM Usage Comparison</b></summary>
125
+
126
+ <br/>
127
+
128
+ FastResize uses **3-4x less RAM** than alternatives!
129
+
130
+ ### PNG (1000 images, 2104×1160)
131
+
132
+ | Library | Time | Throughput | RAM Peak | Compared |
133
+ |---------|------|------------|----------|----------|
134
+ | **FastResize** | 3.19s | 313 img/s | **138 MB** ⚡ | - |
135
+ | libvips | 6.11s | 164 img/s | 439 MB | 3.2x more |
136
+ | imageflow | 8.39s | 119 img/s | 514 MB | 3.7x more |
137
+
138
+ ### JPG (1000 images, 1179×1409)
139
+
140
+ | Library | Time | Throughput | RAM Peak | Compared |
141
+ |---------|------|------------|----------|----------|
142
+ | **FastResize** | 2.03s | 493 img/s | **271 MB** ⚡ | - |
143
+ | libvips | 5.07s | 197 img/s | 460 MB | 1.7x more |
144
+ | imageflow | 6.59s | 152 img/s | 541 MB | 2.0x more |
145
+
146
+ </details>
147
+
148
+ [Full Benchmark Details →](docs/BENCHMARKS.md)
149
+
150
+ ---
151
+
152
+ ## 💪 Key Features
153
+
154
+ <table width="100%">
155
+ <tr>
156
+ <td width="50%">
157
+
158
+ ### ⚡ Performance
159
+ - **Up to 3.1x faster** than libvips & imageflow
160
+ - **3-4x less RAM** usage
161
+ - Optimized C++ core with SIMD
162
+ - Multi-threaded batch processing
163
+
164
+ </td>
165
+ <td width="50%">
166
+
167
+ ### 🖼️ Format Support
168
+ - **JPEG** (via libjpeg-turbo)
169
+ - **PNG** (via libpng)
170
+ - **WebP** (via libwebp)
171
+ - **BMP** (via stb_image)
172
+
173
+ </td>
174
+ </tr>
175
+ <tr>
176
+ <td width="50%">
177
+
178
+ ### 🎯 Resize Modes
179
+ - **Exact size** - Fixed width & height
180
+ - **Fit width** - Auto height
181
+ - **Fit height** - Auto width
182
+ - **Scale percent** - Proportional resize
183
+
184
+ </td>
185
+ <td width="50%">
186
+
187
+ ### 🔧 Quality Filters
188
+ - **Mitchell** - Balanced (default)
189
+ - **Catmull-Rom** - Sharp edges
190
+ - **Box** - Fast, lower quality
191
+ - **Triangle** - Bilinear
192
+
193
+ </td>
194
+ </tr>
195
+ </table>
196
+
197
+ ---
198
+
199
+ ## 📦 Installation
200
+
201
+ <table>
202
+ <tr>
203
+ <td width="50%">
204
+
205
+ ### 💎 Ruby
206
+
207
+ ```bash
208
+ gem install fast_resize
209
+ ```
210
+
211
+ ### 🍎 macOS (Homebrew)
212
+
213
+ ```bash
214
+ brew tap tranhuucanh/fast_resize
215
+ brew install fast_resize
216
+ ```
217
+
218
+ ### 🐧 Linux
219
+
220
+ ```bash
221
+ # Download latest release
222
+ VERSION="1.0.0"
223
+ wget https://github.com/tranhuucanh/fast_resize/releases/download/v${VERSION}/fast_resize-${VERSION}-linux-x86_64.tar.gz
224
+ tar -xzf fast_resize-${VERSION}-linux-x86_64.tar.gz
225
+ sudo cp fast_resize /usr/local/bin/
226
+ sudo chmod +x /usr/local/bin/fast_resize
227
+
228
+ # Verify installation
229
+ fast_resize --version
230
+ ```
231
+
232
+ </td>
233
+ </tr>
234
+ </table>
235
+
236
+ ---
237
+
238
+ ## 🎯 Quick Start
239
+
240
+ ### CLI
241
+
242
+ ```bash
243
+ # Resize to width 800 (auto height)
244
+ fast_resize input.jpg output.jpg 800
245
+
246
+ # Resize to exact 800x600
247
+ fast_resize input.jpg output.jpg 800 600
248
+
249
+ # Batch resize all images in directory
250
+ fast_resize batch input_dir/ output_dir/ --width 800
251
+
252
+ # Convert format (JPG → PNG)
253
+ fast_resize input.jpg output.png 800
254
+ ```
255
+
256
+ ### 💎 Ruby
257
+
258
+ ```ruby
259
+ require 'fast_resize'
260
+
261
+ # Simple resize (width 800, auto height)
262
+ FastResize.resize('input.jpg', 'output.jpg', width: 800)
263
+
264
+ # Exact dimensions
265
+ FastResize.resize('input.jpg', 'output.jpg', width: 800, height: 600)
266
+
267
+ # With quality and filter options
268
+ FastResize.resize('input.jpg', 'output.jpg',
269
+ width: 800,
270
+ quality: 90,
271
+ filter: :catmull_rom
272
+ )
273
+
274
+ # Batch resize (multi-threaded)
275
+ files = Dir['images/*.jpg']
276
+ result = FastResize.batch_resize(files, 'output/', width: 800)
277
+ puts "Processed: #{result[:success]}/#{result[:total]}"
278
+
279
+ # Maximum speed mode (uses more RAM)
280
+ FastResize.batch_resize(files, 'output/',
281
+ width: 800,
282
+ max_speed: true
283
+ )
284
+ ```
285
+
286
+ [Full Ruby Guide →](docs/RUBY_USAGE.md) • [Full CLI Guide →](docs/CLI_USAGE.md)
287
+
288
+ ---
289
+
290
+ ## 📖 API Reference
291
+
292
+ ### Resize Options
293
+
294
+ | Option | Type | Default | Description |
295
+ |--------|------|---------|-------------|
296
+ | `width` | int | - | Target width in pixels |
297
+ | `height` | int | - | Target height in pixels |
298
+ | `scale` | float | - | Scale factor (0.5 = 50%) |
299
+ | `quality` | int | 85 | JPEG/WebP quality (1-100) |
300
+ | `keep_aspect_ratio` | bool | true | Maintain aspect ratio |
301
+ | `filter` | symbol | `:mitchell` | Resize filter algorithm |
302
+
303
+ ### Resize Modes
304
+
305
+ | Mode | When | Example |
306
+ |------|------|---------|
307
+ | **Fit Width** | Only `width` specified | `width: 800` → height auto |
308
+ | **Fit Height** | Only `height` specified | `height: 600` → width auto |
309
+ | **Exact Size** | Both specified | `width: 800, height: 600` |
310
+ | **Scale** | `scale` specified | `scale: 0.5` → 50% size |
311
+
312
+ ### Filter Options
313
+
314
+ | Filter | Quality | Speed | Best For |
315
+ |--------|---------|-------|----------|
316
+ | `:mitchell` | ★★★★☆ | ★★★☆☆ | General use (default) |
317
+ | `:catmull_rom` | ★★★★★ | ★★☆☆☆ | Sharp edges, text |
318
+ | `:triangle` | ★★★☆☆ | ★★★★☆ | Smooth gradients |
319
+ | `:box` | ★★☆☆☆ | ★★★★★ | Maximum speed |
320
+
321
+ ### Batch Options
322
+
323
+ | Option | Type | Default | Description |
324
+ |--------|------|---------|-------------|
325
+ | `threads` | int | auto | Number of threads (0 = auto) |
326
+ | `stop_on_error` | bool | false | Stop on first error |
327
+ | `max_speed` | bool | false | Enable pipeline mode (faster, uses more RAM) |
328
+
329
+ ---
330
+
331
+ ## 📚 Documentation
332
+
333
+ Complete usage guides:
334
+
335
+ - **[CLI Usage Guide](docs/CLI_USAGE.md)** - Complete command-line reference
336
+ - **[Ruby Usage Guide](docs/RUBY_USAGE.md)** - Ruby integration and examples
337
+ - **[Benchmark Details](docs/BENCHMARKS.md)** - Full performance analysis
338
+ - **[API Reference](docs/API.md)** - Complete API documentation
339
+
340
+ ---
341
+
342
+ ## 🏗️ Architecture
343
+
344
+ FastResize is built on industry-standard, high-performance libraries:
345
+
346
+ | Library | License | Purpose |
347
+ |---------|---------|---------|
348
+ | **[libjpeg-turbo](https://libjpeg-turbo.org/)** | BSD-3-Clause | JPEG decode/encode with SIMD |
349
+ | **[libpng](http://www.libpng.org/)** | libpng license | PNG decode/encode |
350
+ | **[libwebp](https://developers.google.com/speed/webp)** | BSD-3-Clause | WebP decode/encode |
351
+ | **[stb_image](https://github.com/nothings/stb)** | Public Domain | BMP and fallback decode |
352
+ | **[stb_image_resize2](https://github.com/nothings/stb)** | Public Domain | High-quality resize with SIMD |
353
+
354
+ ### Why So Fast?
355
+
356
+ - ⚡ **SIMD Optimization** - SSE2/AVX (x86) and NEON (ARM) acceleration
357
+ - 🚀 **Zero-copy Pipeline** - Minimal memory allocation
358
+ - 💪 **Multi-threaded** - Parallel batch processing
359
+ - 🔥 **Memory-mapped I/O** - Efficient file reading
360
+ - 🎯 **libjpeg-turbo** - 2-6x faster JPEG than standard libjpeg
361
+
362
+ ---
363
+
364
+ ## 📄 License
365
+
366
+ FastResize is licensed under the **BSD-3-Clause License**.
367
+
368
+ See [LICENSE](LICENSE) for full details.
369
+
370
+ ---
371
+
372
+ ## 🤝 Contributing
373
+
374
+ Contributions are welcome! Please feel free to submit a Pull Request.
375
+
376
+ 1. Fork the repository
377
+ 2. Create your feature branch (`git checkout -b feature/AmazingFeature`)
378
+ 3. Commit your changes (`git commit -m 'Add some AmazingFeature'`)
379
+ 4. Push to the branch (`git push origin feature/AmazingFeature`)
380
+ 5. Open a Pull Request
381
+
382
+ ---
383
+
384
+ ## 🐛 Bug Reports
385
+
386
+ Found a bug? Please open an issue with:
387
+ - Your OS and version
388
+ - FastResize version
389
+ - Steps to reproduce
390
+ - Expected vs actual behavior
391
+
392
+ ---
393
+
394
+ ## 💖 Support This Project
395
+
396
+ If FastResize helps you save time and money:
397
+ - ⭐ Star this repository
398
+ - 🐛 Report bugs and suggest features
399
+ - 📖 Improve documentation
400
+ - 💬 Share FastResize with others
401
+
402
+ ---
403
+
404
+ ## 📮 Contact & Support
405
+
406
+ **GitHub:** [@tranhuucanh](https://github.com/tranhuucanh) • [Issues](https://github.com/tranhuucanh/fast_resize/issues) • [Discussions](https://github.com/tranhuucanh/fast_resize/discussions)
407
+
408
+ ---
409
+
410
+ ## 🙏 Acknowledgments
411
+
412
+ Built with: **[libjpeg-turbo](https://libjpeg-turbo.org/)** • **[libpng](http://www.libpng.org/)** • **[libwebp](https://developers.google.com/speed/webp)** • **[stb](https://github.com/nothings/stb)** by Sean Barrett
413
+
414
+ ---
415
+
416
+ <div align="center">
417
+
418
+ **Made with ❤️ by FastResize Project**
419
+
420
+ *If FastResize saves you time, give us a star!* ⭐
421
+
422
+ [![Star History Chart](https://api.star-history.com/svg?repos=tranhuucanh/fast_resize&type=Date)](https://star-history.com/#tranhuucanh/fast_resize&Date)
423
+
424
+ [⬆ Back to top](#fast_resize)
425
+
426
+ </div>
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
@@ -0,0 +1,152 @@
1
+ # FastResize - The Fastest Image Resizing Library On The Planet
2
+ # Copyright (C) 2025 Tran Huu Canh (0xTh3OKrypt) and FastResize Contributors
3
+ #
4
+ # Resize 1,000 images in 2 seconds. Up to 2.9x faster than libvips,
5
+ # 3.1x faster than imageflow. Uses 3-4x less RAM than alternatives.
6
+ #
7
+ # Author: Tran Huu Canh (0xTh3OKrypt)
8
+ # Email: tranhuucanh39@gmail.com
9
+ # Homepage: https://github.com/tranhuucanh/fast_resize
10
+ #
11
+ # BSD 3-Clause License
12
+ #
13
+ # Redistribution and use in source and binary forms, with or without
14
+ # modification, are permitted provided that the following conditions are met:
15
+ #
16
+ # 1. Redistributions of source code must retain the above copyright notice,
17
+ # this list of conditions and the following disclaimer.
18
+ #
19
+ # 2. Redistributions in binary form must reproduce the above copyright notice,
20
+ # this list of conditions and the following disclaimer in the documentation
21
+ # and/or other materials provided with the distribution.
22
+ #
23
+ # 3. Neither the name of the copyright holder nor the names of its
24
+ # contributors may be used to endorse or promote products derived from
25
+ # this software without specific prior written permission.
26
+ #
27
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30
+ # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
31
+ # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32
+ # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33
+ # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34
+ # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35
+ # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36
+ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
37
+ # THE POSSIBILITY OF SUCH DAMAGE.
38
+
39
+ require 'mkmf'
40
+ require 'rbconfig'
41
+
42
+ def check_prebuilt_library
43
+ puts "🔍 Checking for pre-built library..."
44
+
45
+ os = RbConfig::CONFIG['host_os']
46
+ arch = RbConfig::CONFIG['host_cpu']
47
+
48
+ platform = case os
49
+ when /darwin/
50
+ case arch
51
+ when /arm64|aarch64/ then 'macos-arm64'
52
+ when /x86_64|x64/ then 'macos-x86_64'
53
+ end
54
+ when /linux/
55
+ case arch
56
+ when /x86_64|x64/ then 'linux-x86_64'
57
+ when /arm64|aarch64/ then 'linux-aarch64'
58
+ end
59
+ end
60
+
61
+ return false unless platform
62
+
63
+ ext_dir = File.dirname(__FILE__)
64
+ prebuilt_dir = File.expand_path("../../prebuilt/#{platform}", ext_dir)
65
+ lib_path = File.join(prebuilt_dir, 'lib', 'libfastresize.a')
66
+
67
+ if File.exist?(lib_path)
68
+ puts "✅ Found pre-built library at #{lib_path}"
69
+ puts "⚡ Skipping compilation (using pre-built binary)"
70
+
71
+ File.open('Makefile', 'w') do |f|
72
+ f.puts "all:"
73
+ f.puts "\t@echo '✅ Using pre-built library for #{platform}'"
74
+ f.puts ""
75
+ f.puts "install:"
76
+ f.puts "\t@echo '✅ Using pre-built library for #{platform}'"
77
+ f.puts ""
78
+ f.puts "clean:"
79
+ f.puts "\t@echo 'Nothing to clean'"
80
+ end
81
+
82
+ return true
83
+ end
84
+
85
+ puts "⚠️ No pre-built library found for #{platform}"
86
+ puts "📦 Will compile from source..."
87
+ false
88
+ end
89
+
90
+ if check_prebuilt_library
91
+ puts "🎉 Installation complete (pre-built binary)!"
92
+ exit 0
93
+ end
94
+
95
+ puts "🔨 Compiling FastResize from source..."
96
+
97
+ $CXXFLAGS << " -std=c++14"
98
+
99
+ $CXXFLAGS << " -O3"
100
+
101
+ ext_dir = File.dirname(__FILE__)
102
+ project_root = File.expand_path('../../../../', ext_dir)
103
+ include_dir = File.join(project_root, 'include')
104
+ src_dir = File.join(project_root, 'src')
105
+
106
+ unless File.directory?(include_dir)
107
+ abort "❌ Include directory not found: #{include_dir}"
108
+ end
109
+
110
+ unless File.directory?(src_dir)
111
+ abort "❌ Source directory not found: #{src_dir}"
112
+ end
113
+
114
+ puts "📂 Building from source tree: #{project_root}"
115
+
116
+ $INCFLAGS << " -I#{include_dir}"
117
+
118
+ ['/opt/homebrew/include', '/usr/local/include'].each do |path|
119
+ $INCFLAGS << " -I#{path}" if File.directory?(path)
120
+ end
121
+
122
+ puts "🔍 Searching for required libraries..."
123
+
124
+ unless find_library('jpeg', 'jpeg_CreateDecompress', '/opt/homebrew/lib', '/usr/local/lib', '/usr/lib')
125
+ abort "❌ ERROR: libjpeg not found. Please install: brew install jpeg (macOS) or apt-get install libjpeg-dev (Linux)"
126
+ end
127
+
128
+ unless find_library('png', 'png_create_read_struct', '/opt/homebrew/lib', '/usr/local/lib', '/usr/lib')
129
+ abort "❌ ERROR: libpng not found. Please install: brew install libpng (macOS) or apt-get install libpng-dev (Linux)"
130
+ end
131
+
132
+ unless find_library('z', 'inflate', '/opt/homebrew/lib', '/usr/local/lib', '/usr/lib')
133
+ abort "❌ ERROR: zlib not found. Please install: brew install zlib (macOS) or apt-get install zlib1g-dev (Linux)"
134
+ end
135
+
136
+ if find_library('webp', 'WebPDecode', '/opt/homebrew/lib', '/usr/local/lib', '/usr/lib')
137
+ puts "✅ WebP support enabled"
138
+ find_library('webpdemux', 'WebPDemuxGetFrame', '/opt/homebrew/lib', '/usr/local/lib', '/usr/lib')
139
+ find_library('sharpyuv', 'SharpYuvGetCPUInfo', '/opt/homebrew/lib', '/usr/local/lib', '/usr/lib')
140
+ else
141
+ puts "⚠️ WebP support disabled (library not found)"
142
+ end
143
+
144
+ $srcs = ['fastresize_ext.cpp'] + Dir.glob(File.join(src_dir, '*.cpp')).map { |f| File.basename(f) }
145
+ $VPATH << src_dir
146
+
147
+ puts "📝 Generating Makefile..."
148
+
149
+ create_makefile('fastresize/fastresize_ext')
150
+
151
+ puts "✅ Makefile generated successfully!"
152
+ puts "💡 Run 'make' to compile the extension"