[Service] Integrate DeviceHome and SignalingServer
[platform/framework/web/wrtjs.git] / device_home / node_modules / pngjs / lib / format-normaliser.js
1 'use strict';
2
3 function dePalette(indata, outdata, width, height, palette) {
4   var pxPos = 0;
5   // use values from palette
6   for (var y = 0; y < height; y++) {
7     for (var x = 0; x < width; x++) {
8       var color = palette[indata[pxPos]];
9
10       if (!color) {
11         throw new Error('index ' + indata[pxPos] + ' not in palette');
12       }
13
14       for (var i = 0; i < 4; i++) {
15         outdata[pxPos + i] = color[i];
16       }
17       pxPos += 4;
18     }
19   }
20 }
21
22 function replaceTransparentColor(indata, outdata, width, height, transColor) {
23   var pxPos = 0;
24   for (var y = 0; y < height; y++) {
25     for (var x = 0; x < width; x++) {
26       var makeTrans = false;
27
28       if (transColor.length === 1) {
29         if (transColor[0] === indata[pxPos]) {
30           makeTrans = true;
31         }
32       }
33       else if (transColor[0] === indata[pxPos] && transColor[1] === indata[pxPos + 1] && transColor[2] === indata[pxPos + 2]) {
34         makeTrans = true;
35       }
36       if (makeTrans) {
37         for (var i = 0; i < 4; i++) {
38           outdata[pxPos + i] = 0;
39         }
40       }
41       pxPos += 4;
42     }
43   }
44 }
45
46 function scaleDepth(indata, outdata, width, height, depth) {
47   var maxOutSample = 255;
48   var maxInSample = Math.pow(2, depth) - 1;
49   var pxPos = 0;
50
51   for (var y = 0; y < height; y++) {
52     for (var x = 0; x < width; x++) {
53       for (var i = 0; i < 4; i++) {
54         outdata[pxPos + i] = Math.floor((indata[pxPos + i] * maxOutSample) / maxInSample + 0.5);
55       }
56       pxPos += 4;
57     }
58   }
59 }
60
61 module.exports = function(indata, imageData) {
62
63   var depth = imageData.depth;
64   var width = imageData.width;
65   var height = imageData.height;
66   var colorType = imageData.colorType;
67   var transColor = imageData.transColor;
68   var palette = imageData.palette;
69
70   var outdata = indata; // only different for 16 bits
71
72   if (colorType === 3) { // paletted
73     dePalette(indata, outdata, width, height, palette);
74   }
75   else {
76     if (transColor) {
77       replaceTransparentColor(indata, outdata, width, height, transColor);
78     }
79     // if it needs scaling
80     if (depth !== 8) {
81       // if we need to change the buffer size
82       if (depth === 16) {
83         outdata = new Buffer(width * height * 4);
84       }
85       scaleDepth(indata, outdata, width, height, depth);
86     }
87   }
88   return outdata;
89 };