[SignalingServer] Optimize dependent modules
[platform/framework/web/wrtjs.git] / device_home / node_modules / pngjs / lib / png.js
1 'use strict';
2
3 var util = require('util');
4 var Stream = require('stream');
5 var Parser = require('./parser-async');
6 var Packer = require('./packer-async');
7 var PNGSync = require('./png-sync');
8
9
10 var PNG = exports.PNG = function(options) {
11   Stream.call(this);
12
13   options = options || {}; // eslint-disable-line no-param-reassign
14
15   // coerce pixel dimensions to integers (also coerces undefined -> 0):
16   this.width = options.width | 0;
17   this.height = options.height | 0;
18
19   this.data = this.width > 0 && this.height > 0 ?
20     new Buffer(4 * this.width * this.height) : null;
21
22   if (options.fill && this.data) {
23     this.data.fill(0);
24   }
25
26   this.gamma = 0;
27   this.readable = this.writable = true;
28
29   this._parser = new Parser(options);
30
31   this._parser.on('error', this.emit.bind(this, 'error'));
32   this._parser.on('close', this._handleClose.bind(this));
33   this._parser.on('metadata', this._metadata.bind(this));
34   this._parser.on('gamma', this._gamma.bind(this));
35   this._parser.on('parsed', function(data) {
36     this.data = data;
37     this.emit('parsed', data);
38   }.bind(this));
39
40   this._packer = new Packer(options);
41   this._packer.on('data', this.emit.bind(this, 'data'));
42   this._packer.on('end', this.emit.bind(this, 'end'));
43   this._parser.on('close', this._handleClose.bind(this));
44   this._packer.on('error', this.emit.bind(this, 'error'));
45
46 };
47 util.inherits(PNG, Stream);
48
49 PNG.sync = PNGSync;
50
51 PNG.prototype.pack = function() {
52
53   if (!this.data || !this.data.length) {
54     this.emit('error', 'No data provided');
55     return this;
56   }
57
58   process.nextTick(function() {
59     this._packer.pack(this.data, this.width, this.height, this.gamma);
60   }.bind(this));
61
62   return this;
63 };
64
65
66 PNG.prototype.parse = function(data, callback) {
67
68   if (callback) {
69     var onParsed, onError;
70
71     onParsed = function(parsedData) {
72       this.removeListener('error', onError);
73
74       this.data = parsedData;
75       callback(null, this);
76     }.bind(this);
77
78     onError = function(err) {
79       this.removeListener('parsed', onParsed);
80
81       callback(err, null);
82     }.bind(this);
83
84     this.once('parsed', onParsed);
85     this.once('error', onError);
86   }
87
88   this.end(data);
89   return this;
90 };
91
92 PNG.prototype.write = function(data) {
93   this._parser.write(data);
94   return true;
95 };
96
97 PNG.prototype.end = function(data) {
98   this._parser.end(data);
99 };
100
101 PNG.prototype._metadata = function(metadata) {
102   this.width = metadata.width;
103   this.height = metadata.height;
104
105   this.emit('metadata', metadata);
106 };
107
108 PNG.prototype._gamma = function(gamma) {
109   this.gamma = gamma;
110 };
111
112 PNG.prototype._handleClose = function() {
113   if (!this._parser.writable && !this._packer.readable) {
114     this.emit('close');
115   }
116 };
117
118
119 PNG.bitblt = function(src, dst, srcX, srcY, width, height, deltaX, deltaY) { // eslint-disable-line max-params
120   // coerce pixel dimensions to integers (also coerces undefined -> 0):
121   /* eslint-disable no-param-reassign */
122   srcX |= 0;
123   srcY |= 0;
124   width |= 0;
125   height |= 0;
126   deltaX |= 0;
127   deltaY |= 0;
128   /* eslint-enable no-param-reassign */
129
130   if (srcX > src.width || srcY > src.height || srcX + width > src.width || srcY + height > src.height) {
131     throw new Error('bitblt reading outside image');
132   }
133
134   if (deltaX > dst.width || deltaY > dst.height || deltaX + width > dst.width || deltaY + height > dst.height) {
135     throw new Error('bitblt writing outside image');
136   }
137
138   for (var y = 0; y < height; y++) {
139     src.data.copy(dst.data,
140       ((deltaY + y) * dst.width + deltaX) << 2,
141       ((srcY + y) * src.width + srcX) << 2,
142       ((srcY + y) * src.width + srcX + width) << 2
143     );
144   }
145 };
146
147
148 PNG.prototype.bitblt = function(dst, srcX, srcY, width, height, deltaX, deltaY) { // eslint-disable-line max-params
149
150   PNG.bitblt(this, dst, srcX, srcY, width, height, deltaX, deltaY);
151   return this;
152 };
153
154 PNG.adjustGamma = function(src) {
155   if (src.gamma) {
156     for (var y = 0; y < src.height; y++) {
157       for (var x = 0; x < src.width; x++) {
158         var idx = (src.width * y + x) << 2;
159
160         for (var i = 0; i < 3; i++) {
161           var sample = src.data[idx + i] / 255;
162           sample = Math.pow(sample, 1 / 2.2 / src.gamma);
163           src.data[idx + i] = Math.round(sample * 255);
164         }
165       }
166     }
167     src.gamma = 0;
168   }
169 };
170
171 PNG.prototype.adjustGamma = function() {
172   PNG.adjustGamma(this);
173 };