Jimp-native is a fast C++ re-implementation of Jimp
Run npm install jimp-native
in your existing project and import Jimp from 'jimp-native'
. Jimp native aims to be a drop-in replacement for Jimp. If your code is already working with Jimp it should also work with Jimp native. If you need even more performance, check out Multithreading
It's highly recommended to use the asynchronous version of an image operation where possible.
Depending on how you call Jimp native methods, calls will be either singlethreaded or multithreaded. There's two ways to have the library run in multithreaded mode for a given operation:
- This library ships an
Async
version of each method (e.g.resize
becomesresizeAsync
) these methods return a promise and also cause said operation to run on another thread. - If you provide a callback to any covered image operation, that method will run on another thread.
See Jimp's documentation, this library should function the same way. The only difference you'll need to keep in mind is that any Async
version of a call returns a promise and does not expect a callback function.
Jimp native aims to be visually indistinguishable from Jimp. Tests are usually kept to a β 0.392% tolerance for each colour component of a pixel. There are cases where the tolerances are set higher, notably blur
and some resize
algorithms. Reasoning for this usually comes down to speed benefits of using another algorithm or using integer arithmetic over JavaScript doubles. In any case, unless you need 100% accuracy on a binary level, using this library should be fine output-wise.
The following Jimp plugins/functions have been optimized with this library:
- composite β
- blit β
- blur β
- circle β
- colour π¨
- brightness β
- contrast β
- posterize β
- grayscale/greyscale β
- opacity β
- sepia β
- fade β
- convolution β
- opaque β
- pixelate β
- convolute β
- colour/color β
- contain π¨ (uses resize internally so it's covered)
- cover π¨ (crop and resize internally so it's covered)
- crop β
- crop β
- autocrop β
- displace β
- dither β
- dither16 β
- dither565 β
- fisheye β
- gaussian β (can be done by using convolution for now, or just using blur if a true gaussian blur isn't required)
- invert β
- mask β
- normalize β
- print π¨ (uses blit internally so it's covered)
- resize β
- rotate β
- scale π¨ (uses resize internally so it's covered)
- shadow β
- threshold β
The following numbers are some samples from a benchmark on a Core i9-13900K
using a 512x512 image. You can find the benchmark code and all results with differing image sizes in the benchmark folder.
Operations using the synchronous API
Operation | avg. time Jimp | avg. time Jimp native |
---|---|---|
Gaussian blur convolution | 149.08ms | 22.42ms |
Rotate 90deg | 11.25ms | 1.77ms |
Crop | 3.26ms | 180.84ΞΌs |
Default resize 2x | 17.95ms | 15.40ms |
Bicubic resize 2x | 59.44ms | 18.19ms |
Nearest neighbour resize 2x | 18.04ms | 1.55ms |
Operations using the callback/async API (32 calls launched at the same time). Imagine a busy web server handling tons of requests at once.
Only jimp native is able to use multiple threads out of the box, so note that while the benchmark runs the same code on both implementations, only jimp native is actually multithreading in these examples. In other words, the more cores your CPU has the more drastic the improvement.
Operation | time taken Jimp | time taken Jimp native |
---|---|---|
Gaussian blur convolution | 4.73sec | 56.52ms |
Rotate 90deg | 401.64ms | 14.73ms |
Crop | 105.03ms | 1.90ms |
Default resize 2x | 589.60ms | 49.05ms |
Here are some things I'd like to look into doing with this project:
- Implement remaining Jimp plugins / functions
- Handle image decoding in C++
- Build WASM version
- Improve the testing harness and benchmarking tool
Most C++ optimized functions are based on their JavaScript equivalents in Jimp. Jimp and Jimp native are both available under the MIT license. For the original license, see ORIGINAL_JIMP_LICENSE, for the license that applies to this port, see LICENSE.
Jimp also contains portions of source code from other projects. C++ ports of this code will be marked with licensing info where applicable. External projects that have been partially ported to work with Jimp native include:
- ImageJS - All resize algorithms except the default bilinear algorithm are from this project. ImageJS is available under the MIT license (See IMAGEJS_LICENSE). Links: C++ source, Jimp source, Original source.
- JS-Image-Resizer - Jimp will use the resize algorithm from this project when no algorithm is specified (this is not the same algorithm as when
'bilinearInterpolation'
is specified). JS-Image-Resizer is in the public domain. Links: C++ source, Jimp source, Original source.