All files pureimage.js

90.63% Statements 58/64
100% Branches 12/12
78.95% Functions 15/19
91.94% Lines 57/62

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 1566x 6x 6x 6x 6x 6x 6x                     6x 34x                     6x 8x 8x 1x   8x         8x 1040x 200800x 200800x 200800x 200800x 803200x         8x 1x     7x                             6x 2x 2x 2x 1x   2x         2x 2x 2x 2x                           6x 1x 1x 1x 1x 1x 1x 1x 1x 1x 200x 26600x 26600x               1x   1x                                     6x 1x 1x   1x 1x 106400x   1x               6x  
const Bitmap = require('./bitmap');
const fs     = require('fs');
const JPEG   = require('jpeg-js');
const PNG_LIB    = require('pngjs')
const PNG = PNG_LIB.PNG;
const text   = require('./text');
const uint32 = require('./uint32');
 
/**
 * Create a new bitmap image
 *
 * @param {number} w       Image width
 * @param {number} h       Image height
 * @param {object} options Options to be passed to {@link Bitmap}
 *
 * @returns {Bitmap}
 */
exports.make = function(w,h,options) {
    return new Bitmap(w,h,options);
};
 
/**
 * Encode the PNG image to output stream
 *
 * @param {Bitmap} bitmap    An instance of {@link Bitmap} to be encoded to PNG, `bitmap.data` must be a buffer of raw PNG data
 * @param {Stream} outstream The stream to write the PNG file to
 *
 * @returns {Promise<void>}
 */
exports.encodePNGToStream = function(bitmap, outstream) {
    return new Promise((res,rej)=>{
        if(!bitmap.hasOwnProperty('data') || !bitmap.hasOwnProperty('width') || !bitmap.hasOwnProperty('height')) {
            rej(new TypeError('Invalid bitmap image provided'));
        }
        var png = new PNG({
            width:bitmap.width,
            height:bitmap.height
        });
 
        for(var i=0; i<bitmap.width; i++) {
            for(var j=0; j<bitmap.height; j++) {
                var rgba = bitmap.getPixelRGBA(i,j);
                var n = (j*bitmap.width+i)*4;
                var bytes = uint32.getBytesBigEndian(rgba);
                for(var k=0; k<4; k++) {
                    png.data[n+k] = bytes[k];
                }
            }
        }
 
        png
            .on('error', (err) => { rej(err); })
            .pack()
            .pipe(outstream)
            .on('finish', () => { res(); })
            .on('error', (err) => { rej(err); });
    });
}
 
/**
 * Encode JPEG To Stream
 *
 * Encode the JPEG image to output stream
 *
 * @param {Bitmap} img       An instance of {@link Bitmap} to be encoded to JPEG, `img.data` must be a buffer of raw JPEG data
 * @param {Stream} outstream The stream to write the raw JPEG buffer to
 * @param {Int} Number between 0 and 100 setting the JPEG quality
 * @returns {Promise<void>}
 */
exports.encodeJPEGToStream = function(img, outstream, quality) {
    quality = quality || 90;
    return new Promise((res,rej)=> {
        if(!img.hasOwnProperty('data') || !img.hasOwnProperty('width') || !img.hasOwnProperty('height')) {
            rej(new TypeError('Invalid bitmap image provided'));
        }
        var data = {
            data: img.data,
            width: img.width,
            height: img.height
        };
        outstream.on('error', (err) => rej(err));
        outstream.write(JPEG.encode(data, quality).data, () => {
            outstream.end();
            res();
        });
    });
};
 
/**
 * Decode JPEG From Stream
 *
 * Decode a JPEG image from an incoming stream of data
 *
 * @param {Stream} data A readable stream to decode JPEG data from
 *
 * @returns {Promise<Bitmap>}
 */
exports.decodeJPEGFromStream = function(data) {
    return new Promise((res,rej)=>{
        try {
            var chunks = [];
            data.on('data', chunk => chunks.push(chunk));
            data.on('end',() => {
                var buf = Buffer.concat(chunks);
                var rawImageData = JPEG.decode(buf);
                var bitmap = new Bitmap(rawImageData.width, rawImageData.height);
                for (var x_axis = 0; x_axis < rawImageData.width; x_axis++) {
                    for (var y_axis = 0; y_axis < rawImageData.height; y_axis++) {
                        var n = (y_axis * rawImageData.width + x_axis) * 4;
                        bitmap.setPixelRGBA_i(x_axis, y_axis,
                            rawImageData.data[n + 0],
                            rawImageData.data[n + 1],
                            rawImageData.data[n + 2],
                            rawImageData.data[n + 3]
                        );
                    }
                }
                res(bitmap);
            });
            data.on("error", (err) => {
                rej(err);
            });
        } catch (e) {
            console.log(e);
            rej(e);
        }
    });
};
 
/**
 * Decode PNG From Stream
 *
 * Decode a PNG file from an incoming readable stream
 *
 * @param {Stream} instream A readable stream containing raw PNG data
 *
 * @returns {Promise<Bitmap>}
 */
exports.decodePNGFromStream = function(instream) {
    return new Promise((res,rej)=>{
        instream.pipe(new PNG())
            .on("parsed", function() {
                var bitmap =  new Bitmap(this.width,this.height);
                for(var i=0; i<bitmap.data.length; i++) {
                    bitmap.data[i] = this.data[i];
                };
                res(bitmap);
            }).on("error", function(err) {
                rej(err);
            });
    })
};
 
/**@ignore */
exports.registerFont = text.registerFont;