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

v2.0.0

Published

A C++ module for node-js that converts RGB and RGBA buffers to a JPEG images (in memory).

Downloads

233

Readme

This is a node.js module, writen in C++, that uses libjpeg to produce a JPEG image (in memory) from a buffer of RGBA or RGB values. Since JPEG has no notion of A (alpha), the module always uses just RGB values.

It was written by Peteris Krumins ([email protected]). His blog is at http://www.catonmat.net -- good coders code, great reuse.


The module exports three objects: Jpeg, FixedJpegStack, DynamicJpegStack.

Jpeg allows to create fixed size jpegs from RGB, BGR, RGBA or BGRA buffers. FixedJpegStack allows to push multiple jpegs to a fixed size canvas. DynamicJpegStack allows to push multiple jpegs to a dynamic size canvas (it grows as you push jpegs to it).

All objects provide synchronous and asynchronous interfaces.

##Jpeg

Jpeg object that takes 4 arguments in its constructor:

    var jpeg = new Jpeg(buffer, width, height, [buffer_type]);

The first argument, buffer, is a nodee.js Buffer filled with RGBA or RGB values. The second argument is integer width of the image. The third argument is integer height of the image. The fourth argument is buffer type, either 'rgb' or 'rgba'. [Optional].

After you have constructed the object, call .encode() or .encodeSync to produce a jpeg:

    var jpeg_image = jpeg.encodeSync(); // synchronous encoding (blocks node.js)

Or:

    jpeg.encode(function (image, error) {
        // jpeg image is in 'image'
    });

See examples/ directory for examples.

##FixedJpegStack

First you create a FixedJpegStack object of fixed width and height:

    var stack = new FixedJpegStack(width, height, [buffer_type]);

Then you can push individual fragments to it, for example,

    stack.push(buf1, 10, 11, 100, 200); // pushes buf1 to (x,y)=(10,11)
                                        // 100 and 200 are width and height.

    // more pushes

After you're done, call .encode() to produce final jpeg asynchronously or .encodeSync() (just like in Jpeg object). The final jpeg will be of size width x height.

##DynamicJpegStack

DynamicJpegStack is the same as FixedJpegStack except its canvas grows dynamically.

First, create the stack:

    var stack = new DynamicJpegStack([buffer_type]);

Next push the RGB(A) buffers to it:

    stack.push(buf1, 5, 10, 100, 40);
    stack.push(buf2, 2, 210, 20, 20);

Now you can call encode to produce the final jpeg:

    var jpeg = stack.encodeSync();

Now let's see what the dimensions are,

    var dims = stack.dimensions();

Same asynchronously:

    stack.encode(function (jpeg, dims) {
        // jpeg is the image
        // dims are its dimensions
    });

In this particular example:

The x position dims.x is 2 because the 2nd jpeg is closer to the left. The y position dims.y is 10 because the 1st jpeg is closer to the top. The width dims.width is 103 because the first jpeg stretches from x=5 to x=105, but the 2nd jpeg starts only at x=2, so the first two pixels are not necessary and the width is 105-2=103. The height dims.height is 220 because the 2nd jpeg is located at 210 and its height is 20, so it stretches to position 230, but the first jpeg starts at 10, so the upper 10 pixels are not necessary and height becomes 230-10= 220.

##How to install?

To get it compiled, you need to have libjpeg and node installed. Then just run

    node-waf configure build

to build the Jpeg module. It will produce a jpeg.node file as the module.

See also http://github.com/pkrumins/node-png module that produces PNG images. See also http://github.com/pkrumins/node-gif module that produces GIF images.


Have fun!

Sincerely, Peteris Krumins http://www.catonmat.net