npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jpeg-turbo

v0.4.0

Published

Limited libjpeg-turbo bindings for Node.js.

Downloads

391

Readme

node-jpeg-turbo

Build Status npm

node-jpeg-turbo provides minimal libjpeg-turbo bindings for Node.js. It is very, very fast compared to other alternatives, such as node-imagemagick-native or jpeg-js.

Please ask if you need more methods exposed.

Requirements

We use NAN to guarantee maximum v8 API compatibility, so in theory any Node.js or io.js version should work fine. For maximum convenience we also use node-pre-gyp to publish prebuilt binaries for a few common platforms. For example, if you're on OS X and using the latest stable Node, you probably won't need to do a thing.

If you must build from source

First, if you're building from the repo, make sure to init and update submodules or you'll get confusing errors about missing targets when building. We include libjpeg-turbo as a submodule.

git submodule init
git submodule update

(or just use git clone --recursive when cloning the repo)

Due to massive linking pain on Ubuntu, we embed and build libjpeg-turbo directly with node-gyp. Unfortunately this adds an extra requirement, as the build process needs yasm to enable all optimizations. Note that this step is only required for x86 and x86_64 architectures. You don't need yasm if you're building on arm, for example.

Here's how to install yasm:

On OS X

brew install yasm

On Ubuntu 14.04

apt-get install yasm

On Ubuntu 12.04

apt-get install yasm

Important! Ubuntu 12.04 comes with GCC 4.6, which is too old to compile the add-on (and most other modules since Node.js 4.0 was released). More information is available here.

If you really must use this module on Ubuntu 12.04, the following may work:

apt-get install python-software-properties
add-apt-repository -y ppa:ubuntu-toolchain-r/test
apt-get -y install g++-4.8
export CXX=g++-4.8

Remember to export CXX when you npm install.

On Debian

apt-get install yasm

On Alpine Linux

apk add yasm

Others

Search your package manager for yasm.

Installation

Make sure you've got the requirements installed. Then simply install from NPM:

npm install --save jpeg-turbo

API

jpg.bufferSize(options)Number

If you'd like to preallocate a Buffer for jpg.compressSync(), use this method to get the worst-case upper bound. The options argument is fully compatible with the jpg.compressSync() method, so that you can pass the same options to both functions.

  • options is an Object with the following properties:
    • width Required. The width of the image.
    • height Required. The height of the image.
    • subsampling Optional. The subsampling method to use. Defaults to jpg.SAMP_420.
  • Returns The Number of bytes required in a worst-case scenario.
var fs = require('fs')
var jpg = require('jpeg-turbo')

var raw = fs.readFileSync('raw.rgba')

var options = {
  format: jpg.FORMAT_RGBA,
  width: 1080,
  height: 1920,
  subsampling: jpg.SAMP_444,
}

var preallocated = new Buffer(jpg.bufferSize(options))

var encoded = jpg.compressSync(raw, preallocated, options)

jpg.compressSync(raw[, out], options)Buffer

Compresses (i.e. encodes) the raw pixel data into a JPG. This method is not capable of resizing the image.

For efficiency reasons you may choose to encode into a preallocated Buffer. While fast, it has a number of drawbacks. Namely, you'll have to be careful not to reuse the buffer in async processing before processing (e.g. saving, displaying or transmitting) the entire encoded image. Otherwise you risk corrupting the image. Also, it wastes a huge amount of space compared to on-demand allocation.

  • raw is a Buffer with the raw pixel data in options.format.
  • out is an optional preallocated Buffer for the encoded image. The size of the buffer is checked. See jpg.bufferSize() for an example of how to preallocate a sufficient Buffer. If not given, memory is allocated and reallocated as needed, which eliminates most of the wasted space but is slower and lacks consistency with varying source images.
  • options is an Object with the following properties:
    • format Required. The format of the raw pixel data (e.g. jpg.FORMAT_RGBA).
    • width Required. The width of the image.
    • height Required. The height of the image.
    • subsampling Optional. The subsampling method to use. Defaults to jpg.SAMP_420.
    • quality Optional. The desired JPG quality. Defaults to 80.
  • Returns The encoded image as a Buffer. Note that the buffer may actually be a slice of the preallocated Buffer, if given. Be careful not to reuse the preallocated buffer before you've finished processing the encoded image, as it may corrupt the image.
var fs = require('fs')
var jpg = require('jpeg-turbo')

var raw = fs.readFileSync('raw.rgba')

var options = {
  format: jpg.FORMAT_RGBA,
  width: 1080,
  height: 1920,
  subsampling: jpg.SAMP_444,
}

var encoded = jpg.compressSync(raw, options)

See jpg.bufferSize() for an example of preallocated Buffer usage.

jpg.decompressSync(image[, out], options)Object

Decompresses (i.e. decodes) the JPG image into raw pixel data.

  • image is a Buffer with the JPG image data.
  • out is an optional preallocated Buffer for the decoded image. The size of the buffer is checked, and should be at least width * height * bytes_per_pixel or larger. If not given, one is created for you. The only benefit of providing the Buffer yourself is that you can reuse the same buffer between multiple jpg.decompressSync() calls. Note that this can lead to issues with concurrency. See jpg.compressSync() for related discussion.
  • options is an Object with the following properties:
    • format Required. The desired format of the raw pixel data (e.g. jpg.FORMAT_RGBA).
    • out Deprecated. Use the out argument instead.
  • Returns An Object with the following properties:
    • data A Buffer with the raw pixel data.
    • width The width of the image.
    • height The height of the image.
    • subsampling The subsampling method used in the JPG.
    • size Deprecated. Use data.length instead.
    • bpp The number of bytes per pixel.
var fs = require('fs')
var jpg = require('jpeg-turbo')

var image = fs.readFileSync('image.jpg')

var options = {
  format: jpg.FORMAT_RGBA,
}

var decoded = jpg.decompressSync(image, options)

Thanks

  • https://github.com/A2K/node-jpeg-turbo-scaler
  • https://github.com/mash/node-imagemagick-native
  • https://github.com/google/skia/blob/master/gyp/libjpeg-turbo.gyp
  • https://github.com/openstf/android-libjpeg-turbo

License

See LICENSE.

Copyright © Simo Kinnunen. All Rights Reserved.