[SignalingServer] Optimize dependent modules
[platform/framework/web/wrtjs.git] / signaling_server / service / node_modules / pngjs / lib / packer.js
1 'use strict';
2
3 var constants = require('./constants');
4 var CrcStream = require('./crc');
5 var bitPacker = require('./bitpacker');
6 var filter = require('./filter-pack');
7 var zlib = require('zlib');
8
9 var Packer = module.exports = function(options) {
10   this._options = options;
11
12   options.deflateChunkSize = options.deflateChunkSize || 32 * 1024;
13   options.deflateLevel = options.deflateLevel != null ? options.deflateLevel : 9;
14   options.deflateStrategy = options.deflateStrategy != null ? options.deflateStrategy : 3;
15   options.inputHasAlpha = options.inputHasAlpha != null ? options.inputHasAlpha : true;
16   options.deflateFactory = options.deflateFactory || zlib.createDeflate;
17   options.bitDepth = options.bitDepth || 8;
18   // This is outputColorType
19   options.colorType = (typeof options.colorType === 'number') ? options.colorType : constants.COLORTYPE_COLOR_ALPHA;
20   options.inputColorType = (typeof options.inputColorType === 'number') ? options.inputColorType : constants.COLORTYPE_COLOR_ALPHA;
21
22   if ([
23     constants.COLORTYPE_GRAYSCALE,
24     constants.COLORTYPE_COLOR,
25     constants.COLORTYPE_COLOR_ALPHA,
26     constants.COLORTYPE_ALPHA
27   ].indexOf(options.colorType) === -1) {
28     throw new Error('option color type:' + options.colorType + ' is not supported at present');
29   }
30   if ([
31     constants.COLORTYPE_GRAYSCALE,
32     constants.COLORTYPE_COLOR,
33     constants.COLORTYPE_COLOR_ALPHA,
34     constants.COLORTYPE_ALPHA
35   ].indexOf(options.inputColorType) === -1) {
36     throw new Error('option input color type:' + options.inputColorType + ' is not supported at present');
37   }
38   if (options.bitDepth !== 8 && options.bitDepth !== 16) {
39     throw new Error('option bit depth:' + options.bitDepth + ' is not supported at present');
40   }
41 };
42
43 Packer.prototype.getDeflateOptions = function() {
44   return {
45     chunkSize: this._options.deflateChunkSize,
46     level: this._options.deflateLevel,
47     strategy: this._options.deflateStrategy
48   };
49 };
50
51 Packer.prototype.createDeflate = function() {
52   return this._options.deflateFactory(this.getDeflateOptions());
53 };
54
55 Packer.prototype.filterData = function(data, width, height) {
56   // convert to correct format for filtering (e.g. right bpp and bit depth)
57   var packedData = bitPacker(data, width, height, this._options);
58
59   // filter pixel data
60   var bpp = constants.COLORTYPE_TO_BPP_MAP[this._options.colorType];
61   var filteredData = filter(packedData, width, height, this._options, bpp);
62   return filteredData;
63 };
64
65 Packer.prototype._packChunk = function(type, data) {
66
67   var len = (data ? data.length : 0);
68   var buf = new Buffer(len + 12);
69
70   buf.writeUInt32BE(len, 0);
71   buf.writeUInt32BE(type, 4);
72
73   if (data) {
74     data.copy(buf, 8);
75   }
76
77   buf.writeInt32BE(CrcStream.crc32(buf.slice(4, buf.length - 4)), buf.length - 4);
78   return buf;
79 };
80
81 Packer.prototype.packGAMA = function(gamma) {
82   var buf = new Buffer(4);
83   buf.writeUInt32BE(Math.floor(gamma * constants.GAMMA_DIVISION), 0);
84   return this._packChunk(constants.TYPE_gAMA, buf);
85 };
86
87 Packer.prototype.packIHDR = function(width, height) {
88
89   var buf = new Buffer(13);
90   buf.writeUInt32BE(width, 0);
91   buf.writeUInt32BE(height, 4);
92   buf[8] = this._options.bitDepth; // Bit depth
93   buf[9] = this._options.colorType; // colorType
94   buf[10] = 0; // compression
95   buf[11] = 0; // filter
96   buf[12] = 0; // interlace
97
98   return this._packChunk(constants.TYPE_IHDR, buf);
99 };
100
101 Packer.prototype.packIDAT = function(data) {
102   return this._packChunk(constants.TYPE_IDAT, data);
103 };
104
105 Packer.prototype.packIEND = function() {
106   return this._packChunk(constants.TYPE_IEND, null);
107 };