3 var interlaceUtils = require('./interlace');
10 // 0: 0, 1: 0, 2: 0, 3: 0xff
11 function(pxData, data, pxPos, rawPos) {
12 if (rawPos === data.length) {
13 throw new Error('Ran out of data');
16 var pixel = data[rawPos];
17 pxData[pxPos] = pixel;
18 pxData[pxPos + 1] = pixel;
19 pxData[pxPos + 2] = pixel;
20 pxData[pxPos + 3] = 0xff;
24 // 0: 0, 1: 0, 2: 0, 3: 1
25 function(pxData, data, pxPos, rawPos) {
26 if (rawPos + 1 >= data.length) {
27 throw new Error('Ran out of data');
30 var pixel = data[rawPos];
31 pxData[pxPos] = pixel;
32 pxData[pxPos + 1] = pixel;
33 pxData[pxPos + 2] = pixel;
34 pxData[pxPos + 3] = data[rawPos + 1];
38 // 0: 0, 1: 1, 2: 2, 3: 0xff
39 function(pxData, data, pxPos, rawPos) {
40 if (rawPos + 2 >= data.length) {
41 throw new Error('Ran out of data');
44 pxData[pxPos] = data[rawPos];
45 pxData[pxPos + 1] = data[rawPos + 1];
46 pxData[pxPos + 2] = data[rawPos + 2];
47 pxData[pxPos + 3] = 0xff;
51 // 0: 0, 1: 1, 2: 2, 3: 3
52 function(pxData, data, pxPos, rawPos) {
53 if (rawPos + 3 >= data.length) {
54 throw new Error('Ran out of data');
57 pxData[pxPos] = data[rawPos];
58 pxData[pxPos + 1] = data[rawPos + 1];
59 pxData[pxPos + 2] = data[rawPos + 2];
60 pxData[pxPos + 3] = data[rawPos + 3];
64 var pixelBppCustomMapper = [
69 // 0: 0, 1: 0, 2: 0, 3: 0xff
70 function(pxData, pixelData, pxPos, maxBit) {
71 var pixel = pixelData[0];
72 pxData[pxPos] = pixel;
73 pxData[pxPos + 1] = pixel;
74 pxData[pxPos + 2] = pixel;
75 pxData[pxPos + 3] = maxBit;
79 // 0: 0, 1: 0, 2: 0, 3: 1
80 function(pxData, pixelData, pxPos) {
81 var pixel = pixelData[0];
82 pxData[pxPos] = pixel;
83 pxData[pxPos + 1] = pixel;
84 pxData[pxPos + 2] = pixel;
85 pxData[pxPos + 3] = pixelData[1];
89 // 0: 0, 1: 1, 2: 2, 3: 0xff
90 function(pxData, pixelData, pxPos, maxBit) {
91 pxData[pxPos] = pixelData[0];
92 pxData[pxPos + 1] = pixelData[1];
93 pxData[pxPos + 2] = pixelData[2];
94 pxData[pxPos + 3] = maxBit;
98 // 0: 0, 1: 1, 2: 2, 3: 3
99 function(pxData, pixelData, pxPos) {
100 pxData[pxPos] = pixelData[0];
101 pxData[pxPos + 1] = pixelData[1];
102 pxData[pxPos + 2] = pixelData[2];
103 pxData[pxPos + 3] = pixelData[3];
107 function bitRetriever(data, depth) {
113 if (i === data.length) {
114 throw new Error('Ran out of data');
118 var byte8, byte7, byte6, byte5, byte4, byte3, byte2, byte1;
121 throw new Error('unrecognised depth');
125 leftOver.push(((byte << 8) + byte2));
130 leftOver.push(byte1, byte2);
134 byte3 = byte >> 2 & 3;
135 byte2 = byte >> 4 & 3;
136 byte1 = byte >> 6 & 3;
137 leftOver.push(byte1, byte2, byte3, byte4);
141 byte7 = byte >> 1 & 1;
142 byte6 = byte >> 2 & 1;
143 byte5 = byte >> 3 & 1;
144 byte4 = byte >> 4 & 1;
145 byte3 = byte >> 5 & 1;
146 byte2 = byte >> 6 & 1;
147 byte1 = byte >> 7 & 1;
148 leftOver.push(byte1, byte2, byte3, byte4, byte5, byte6, byte7, byte8);
154 get: function(count) {
155 while (leftOver.length < count) {
158 var returner = leftOver.slice(0, count);
159 leftOver = leftOver.slice(count);
162 resetAfterLine: function() {
166 if (i !== data.length) {
167 throw new Error('extra data found');
173 function mapImage8Bit(image, pxData, getPxPos, bpp, data, rawPos) { // eslint-disable-line max-params
174 var imageWidth = image.width;
175 var imageHeight = image.height;
176 var imagePass = image.index;
177 for (var y = 0; y < imageHeight; y++) {
178 for (var x = 0; x < imageWidth; x++) {
179 var pxPos = getPxPos(x, y, imagePass);
180 pixelBppMapper[bpp](pxData, data, pxPos, rawPos);
181 rawPos += bpp; //eslint-disable-line no-param-reassign
187 function mapImageCustomBit(image, pxData, getPxPos, bpp, bits, maxBit) { // eslint-disable-line max-params
188 var imageWidth = image.width;
189 var imageHeight = image.height;
190 var imagePass = image.index;
191 for (var y = 0; y < imageHeight; y++) {
192 for (var x = 0; x < imageWidth; x++) {
193 var pixelData = bits.get(bpp);
194 var pxPos = getPxPos(x, y, imagePass);
195 pixelBppCustomMapper[bpp](pxData, pixelData, pxPos, maxBit);
197 bits.resetAfterLine();
201 exports.dataToBitMap = function(data, bitmapInfo) {
203 var width = bitmapInfo.width;
204 var height = bitmapInfo.height;
205 var depth = bitmapInfo.depth;
206 var bpp = bitmapInfo.bpp;
207 var interlace = bitmapInfo.interlace;
210 var bits = bitRetriever(data, depth);
214 pxData = new Buffer(width * height * 4);
217 pxData = new Uint16Array(width * height * 4);
219 var maxBit = Math.pow(2, depth) - 1;
225 images = interlaceUtils.getImagePasses(width, height);
226 getPxPos = interlaceUtils.getInterlaceIterator(width, height);
229 var nonInterlacedPxPos = 0;
230 getPxPos = function() {
231 var returner = nonInterlacedPxPos;
232 nonInterlacedPxPos += 4;
235 images = [{ width: width, height: height }];
238 for (var imageIndex = 0; imageIndex < images.length; imageIndex++) {
240 rawPos = mapImage8Bit(images[imageIndex], pxData, getPxPos, bpp, data, rawPos);
243 mapImageCustomBit(images[imageIndex], pxData, getPxPos, bpp, bits, maxBit);
247 if (rawPos !== data.length) {
248 throw new Error('extra data found');