Comparing version 0.10.1 to 0.11.0
/*! | ||
* icojs v0.10.1 | ||
* icojs v0.11.0 | ||
* (c) egy186 | ||
* https://github.com/egy186/icojs/blob/master/LICENSE | ||
*/ | ||
!function(t,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):t.ICO=n()}(this,function(){"use strict";var t="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{},n=function(t){for(var n=atob(t.replace(/.+,/,"")),e=new Uint8Array(n.length),r=0;r<n.length;r++)e[r]=n.charCodeAt(r);return e.buffer},e={decode:function(t){return new Promise(function(n){var e=URL.createObjectURL(new Blob([t])),r=document.createElement("img");r.src=e,r.onload=function(){var t=r.naturalWidth,e=r.naturalHeight,i=document.createElement("canvas");i.width=t,i.height=e;var a=i.getContext("2d");a.drawImage(r,0,0);var o=a.getImageData(0,0,t,e);n({width:o.width,height:o.height,data:o.data})}})},encode:function(t,e){return new Promise(function(r){var i=t.data,a=document.createElement("canvas");a.width=t.width,a.height=t.height;for(var o=a.getContext("2d"),u=o.createImageData(t.width,t.height),f=u.data,c=0;c<f.length;c++)f[c]=i[c];o.putImageData(u,0,0),r(n(a.toDataURL(e||"image/png")))})}},r=function(t){if(!(t instanceof ArrayBuffer))return!1;var n=new DataView(t);return 0===n.getUint16(0,!0)&&1===n.getUint16(2,!0)},i=function(t){var n=new DataView(t),e=n.getUint8(24),r=n.getUint8(25);return 2===r?3*e:4===r?2*e:6===r?4*e:e},a=function(t,n,e){var r={bit:t.bit,buffer:n,height:t.height,width:t.width};return e&&(r.hotspot=e),r},o=function(t){if(!(t instanceof ArrayBuffer))return!1;var n=new DataView(t);return 2303741511===n.getUint32(0,!1)&&218765834===n.getUint32(4,!1)},u={of1:function(t){for(var n=new Uint8Array(t),e="",r=0;r<n.byteLength;r++)e+=("000000000"+n[r].toString(2)).slice(-8);return e.split("").map(function(t){return parseInt(t,2)})},of4:function(t){for(var n=new Uint8Array(t),e="",r=0;r<n.byteLength;r++)e+=("00"+n[r].toString(16)).slice(-2);return e.split("").map(function(t){return parseInt(t,16)})},of8:function(t){var n=new Uint8Array(t);return Array.from(n)}},f=function(t){return new Array(t).fill(0).map(function(t,n){return n})},c=function(t){return t%4==0?t:t+4-t%4},h=function(t){for(var n=t.width,e=t.height,r=new Uint8ClampedArray(n*e*4),i=new Uint8Array(t.xor),a=u.of1(t.and),o=t.bit/8,f=c(n*o),h=8*c(n/8),d=0;d<e;d++)for(var g=0;g<n;g++){var w=d*f+g*o;r.set([i[w+2],i[w+1],i[w],a[d*h+g]?0:255],function(t,r){return 4*((e-r-1)*n+t)}(g,d))}return{data:r,height:e,width:n}},d=function(t){for(var n=t.width,e=t.height,r=new Uint8ClampedArray(n*e*4),i=new Uint8Array(t.xor),a=!u.of8(i).every(function(t,n){return(n+1)%4!=0||0===t}),o=u.of1(t.and),f=t.bit/8,h=c(n*f),d=8*c(n/8),g=0;g<e;g++)for(var w=0;w<n;w++){var s=g*h+w*f;r.set([i[s+2],i[s+1],i[s],a?i[s+3]:o[g*d+w]?0:255],function(t,r){return 4*((e-r-1)*n+t)}(w,g))}return{data:r,height:e,width:n}},g=function(t){for(var n=t.width,e=t.height,r=new Uint8ClampedArray(n*e*4),i=u["of"+t.bit](t.xor),a=u.of1(t.and),o=8*c(n*t.bit/8)/t.bit,f=8*c(n/8),h=0;h<e;h++)for(var d=0;d<n;d++){var g=h*o+d,w=t.colors[i[g]];r.set([w[2],w[1],w[0],a[h*f+d]?0:255],function(t,r){return 4*((e-r-1)*n+t)}(d,h))}return{data:r,height:e,width:n}},w=function(t){return 32===t.bit?d(t):24===t.bit?h(t):g(t)},s=function(t,n,e){var r=new DataView(e),i=r.getUint32(4,!0),a=r.getUint32(8,!0)/2,o=0===i?t:i,h=0===a?n:a,d=r.getUint32(0,!0),g=r.getUint16(14,!0),s=r.getUint32(32,!0);0===s&&g<=8&&(s=1<<g);var l=d+4*s,v=l+c(o*g/8)*h,m={and:e.slice(v,v+c(o/8)*h),bit:g,colors:f(s).map(function(t){var n=d+4*t;return u.of8(e.slice(n,n+4))}),height:h,width:o,xor:e.slice(l,v)};return Object.assign(w(m),{bit:g})},l=function(t){if(!(t instanceof ArrayBuffer))return!1;var n=new DataView(t);return 0===n.getUint16(0,!0)&&2===n.getUint16(2,!0)},v=function(t){if(!(t instanceof ArrayBuffer))throw new TypeError('"buffer" argument must be an ArrayBuffer');if(!l(t)&&!r(t))throw new Error("buffer is not ico");var n=new DataView(t).getUint16(4,!0),e=f(n).map(function(n){var e=6+16*n;return t.slice(e,e+16)});return f(n).map(function(n){var r=new DataView(e[n]),i=r.getUint32(8,!0),a=r.getUint32(12,!0),o={height:r.getUint8(1)||256,iconImage:t.slice(a,a+i),width:r.getUint8(0)||256};return l(t)&&(o.hotspot={x:r.getUint16(4,!0),y:r.getUint16(6,!0)}),o})},m=function(t,n,e){var r=[];try{r=v(t)}catch(t){return Promise.reject(t)}var u=function(t,n,r){if(o(r)){var a=i(r);return e.decode(r).then(function(t){return Object.assign(t,{bit:a})})}return Promise.resolve(s(t,n,r))},f=r.map(function(t){return u(t.width,t.height,t.iconImage).then(function(r){return e.encode(r,n).then(function(n){return a(r,n,t.hotspot)})})});return Promise.all(f)},U=t.ICO;return{isICO:r,noConflict:function(){return t.ICO=U,this},parse:function(t,n){return m(t,n,e)}}}); | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.ICO=e()}(this,function(){"use strict";var t="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{};function e(t,e){return t(e={exports:{}},e.exports),e.exports}var r=e(function(t){var e={decode:function(t){return new Promise(function(e){var r=URL.createObjectURL(new Blob([t])),n=document.createElement("img");n.src=r,n.onload=function(){var t=n.naturalWidth,r=n.naturalHeight,i=document.createElement("canvas");i.width=t,i.height=r;var o=i.getContext("2d");o.drawImage(n,0,0);var a=o.getImageData(0,0,t,r);e({width:a.width,height:a.height,data:a.data})}})},encode:function(t,e){return new Promise(function(r){var n=t.data,i=document.createElement("canvas");i.width=t.width,i.height=t.height;for(var o=i.getContext("2d"),a=o.createImageData(t.width,t.height),h=a.data,f=0;f<h.length;f++)h[f]=n[f];o.putImageData(a,0,0),r(function(t){for(var e=atob(t.replace(/.+,/,"")),r=new Uint8Array(e.length),n=0;n<e.length;n++)r[n]=e.charCodeAt(n);return r.buffer}(i.toDataURL(e||"image/png")))})}};t.exports=e}),n=function(t){if(t instanceof Uint8Array)return new DataView(t.buffer,t.byteOffset,t.byteLength);if(t instanceof ArrayBuffer)return new DataView(t);throw new TypeError("Expected `data` to be an ArrayBuffer or Uint8Array")},i=e(function(t){t.exports=function(t){var e=n(t);return 0===e.getUint16(0,!0)&&1===e.getUint16(2,!0)}}),o=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")},a=function(){function t(t,e){for(var r=0;r<e.length;r++){var n=e[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}return function(e,r,n){return r&&t(e.prototype,r),n&&t(e,n),e}}();var h=function(){function t(e,r,n){var i,a;if(o(this,t),this.format=n.format,this.offset=r,this.depth=n.colorDepth,this.stride=(i=n.width*this.depth/8,(a=i%4)?i+4-a:i),this.size=this.stride*n.height,this.data=e.slice(this.offset,this.offset+this.size),this.size!==this.data.byteLength)throw new Error("Truncated bitmap data")}return a(t,[{key:"get",value:function(t,e,r){var n=this.format.indexOf(r);return 1===this.depth?(this.data[e*this.stride+(t/8|0)]&1<<7-t%8*1)>>7-t%8*1:2===this.depth?(this.data[e*this.stride+(t/4|0)]&3<<6-t%4*2)>>>6-t%4*2:4===this.depth?(this.data[e*this.stride+(t/2|0)]&15<<4-t%2*4)>>>4-t%2*4:this.data[e*this.stride+t*(this.depth/8)+n]}}]),t}();function f(t,e,r){var n=t.getUint32(0,!0),i=t.getUint32(4,!0)/1|0,o=t.getUint32(8,!0)/2|0,a=t.getUint16(14,!0),f=t.getUint32(32,!0);0===f&&a<=8&&(f=1<<a);var u=0===i?e:i,d=0===o?r:o,s=new Uint8Array(t.buffer,t.byteOffset+n,t.byteLength-n);return{width:u,height:d,data:f?function(t,e){var r=e.colorCount,n=e.colorDepth,i=e.height,o=e.width;if(8!==n&&4!==n&&2!==n&&1!==n)throw new Error("A color depth of "+n+" is not supported");for(var a=new h(t,0,{width:r,height:1,colorDepth:32,format:"BGRA"}),f=new h(t,a.offset+a.size,{width:o,height:i,colorDepth:n,format:"C"}),u=new h(t,f.offset+f.size,{width:o,height:i,colorDepth:1,format:"A"}),d=new Uint8Array(o*i*4),s=0,c=0;c<i;c++)for(var g=0;g<o;g++){var p=f.get(g,i-c-1,"C");d[s++]=a.get(p,0,"R"),d[s++]=a.get(p,0,"G"),d[s++]=a.get(p,0,"B"),d[s++]=u.get(g,i-c-1,"A")?0:255}return d}(s,{width:u,height:d,colorDepth:a,colorCount:f}):function(t,e){var r=e.colorDepth,n=e.height,i=e.width;if(32!==r&&24!==r)throw new Error("A color depth of "+r+" is not supported");for(var o=new h(t,0,{width:i,height:n,colorDepth:r,format:"BGRA"}),a=24===r?new h(t,o.offset+o.size,{width:i,height:n,colorDepth:1,format:"A"}):null,f=new Uint8Array(i*n*4),u=0,d=0;d<n;d++)for(var s=0;s<i;s++)f[u++]=o.get(s,n-d-1,"R"),f[u++]=o.get(s,n-d-1,"G"),f[u++]=o.get(s,n-d-1,"B"),f[u++]=32===r?o.get(s,n-d-1,"A"):a.get(s,n-d-1,"A")?0:255;return f}(s,{width:u,height:d,colorDepth:a}),colorDepth:a}}var u=function(t){var e=n(t);if(e.byteLength<6)throw new Error("Truncated header");if(0!==e.getUint16(0,!0))throw new Error("Invalid magic bytes");var r=e.getUint16(2,!0);if(1!==r&&2!==r)throw new Error("Invalid image type");var i=e.getUint16(4,!0);if(e.byteLength<6+16*i)throw new Error("Truncated image list");return Array.from({length:i},function(t,n){var i,o,a,h,u,d,s=e.getUint8(6+16*n+0),c=e.getUint8(6+16*n+1),g=e.getUint32(6+16*n+8,!0),p=e.getUint32(6+16*n+12,!0),l=2!==r?null:{x:e.getUint16(6+16*n+4,!0),y:e.getUint16(6+16*n+6,!0)};if(d=p,2303741511===(u=e).getUint32(d+0)&&218765834===u.getUint32(d+4))return{bpp:function(t,e){var r=t.getUint8(e+24),n=t.getUint8(e+25);if(0===n)return 1*r;if(2===n)return 3*r;if(3===n)return 1*r;if(4===n)return 2*r;if(6===n)return 4*r;throw new Error("Invalid PNG colorType")}(e,p),data:new Uint8Array(e.buffer,e.byteOffset+p,g),height:(a=e,h=p,a.getUint32(h+20,!1)),hotspot:l,type:"png",width:(i=e,o=p,i.getUint32(o+16,!1))};var w=f(new DataView(e.buffer,e.byteOffset+p,g),s,c);return{bpp:w.colorDepth,data:w.data,height:w.height,hotspot:l,type:"bmp",width:w.width}})},d=e(function(t){t.exports=function(t,e,r){var n=null;try{n=u(t)}catch(t){return Promise.reject(t)}var i=function(t){return r.encode(t,e).then(function(r){return Object.assign(t,{type:e.replace("image/",""),buffer:r})})};return Promise.all(n.map(function(t){return"image/png"===e&&"png"===t.type?Promise.resolve(Object.assign({buffer:t.data.buffer.slice(t.data.byteOffset,t.data.byteOffset+t.data.byteLength)},t)):(n=t,"png"!==n.type?Promise.resolve(n):r.decode(n.data).then(function(t){return Object.assign(n,{type:"bmp",data:t.data})})).then(i);var n}))}}),s=t.ICO;return{isICO:i,noConflict:function(){return t.ICO=s,this},parse:function(t,e){return d(t,e||"image/png",r)}}}); | ||
//# sourceMappingURL=ico.js.map |
{ | ||
"name": "icojs", | ||
"description": "parse ico file", | ||
"version": "0.10.1", | ||
"version": "0.11.0", | ||
"author": "egy186", | ||
@@ -11,6 +11,8 @@ "bugs": { | ||
"bmp-js": "0.0.3", | ||
"decode-ico": "^0.2.1", | ||
"file-type": "^7.2.0", | ||
"jpeg-js": "^0.3.3", | ||
"pngjs": "^3.3.0", | ||
"safe-buffer": "^5.1.1" | ||
"safe-buffer": "^5.1.1", | ||
"to-data-view": "^1.0.0" | ||
}, | ||
@@ -26,6 +28,6 @@ "devDependencies": { | ||
"eslint": "~4.12.0", | ||
"jsdoc-to-markdown": "^3.0.0", | ||
"jsdoc-to-markdown": "^4.0.1", | ||
"jsonfile": "^4.0.0", | ||
"jstransformer-markdown-it": "^2.0.0", | ||
"mocha": "^4.0.0", | ||
"mocha": "^5.0.0", | ||
"npm-run-all": "^4.0.1", | ||
@@ -36,6 +38,7 @@ "nyc": "^11.1.0", | ||
"rimraf": "^2.5.2", | ||
"rollup": "^0.54.0", | ||
"rollup": "^0.55.0", | ||
"rollup-plugin-babel": "^3.0.2", | ||
"rollup-plugin-commonjs": "^8.2.1", | ||
"rollup-plugin-uglify": "^2.0.1" | ||
"rollup-plugin-node-resolve": "^3.0.2", | ||
"rollup-plugin-uglify": "^3.0.0" | ||
}, | ||
@@ -42,0 +45,0 @@ "engines": { |
@@ -51,3 +51,3 @@ # icojs | ||
images.forEach(image => { | ||
const file = `${image.width}x${image.height}-${image.bit}bit.png`; | ||
const file = `${image.width}x${image.height}-${image.bpp}bit.png`; | ||
const data = Buffer.from(image.buffer); | ||
@@ -142,3 +142,3 @@ fs.writeFileSync(file, data); | ||
* [.noConflict()](#ICO.noConflict) ⇒ [<code>ICO</code>](#ICO) | ||
* [.isICO(arrayBuffer)](#ICO.isICO) ⇒ <code>Boolean</code> | ||
* [.isICO(source)](#ICO.isICO) ⇒ <code>Boolean</code> | ||
@@ -167,3 +167,3 @@ <a name="ICO.parse"></a> | ||
#### ICO.isICO(arrayBuffer) ⇒ <code>Boolean</code> | ||
#### ICO.isICO(source) ⇒ <code>Boolean</code> | ||
Check the ArrayBuffer is valid ICO. | ||
@@ -176,3 +176,3 @@ | ||
| --- | --- | --- | | ||
| arrayBuffer | <code>ArrayBuffer</code> | ICO file data. | | ||
| source | <code>ArrayBuffer</code> \| <code>Buffer</code> | ICO file data. | | ||
@@ -192,3 +192,3 @@ | ||
| height | <code>Number</code> | Image height. | | ||
| bit | <code>Number</code> | Image bit depth. | | ||
| bpp | <code>Number</code> | Image color depth as bits per pixel. | | ||
| buffer | <code>ArrayBuffer</code> | Image buffer. | | ||
@@ -195,0 +195,0 @@ |
@@ -16,3 +16,3 @@ 'use strict'; | ||
*/ | ||
const parse = (arrayBuffer, mime) => parseICO(arrayBuffer, mime, Image); | ||
const parse = (arrayBuffer, mime) => parseICO(arrayBuffer, mime || 'image/png', Image); | ||
@@ -19,0 +19,0 @@ /** |
'use strict'; | ||
const toDataView = require('to-data-view'); | ||
/** | ||
* Check the ArrayBuffer is valid CUR. | ||
* @access private | ||
* @param {ArrayBuffer} arrayBuffer ArrayBuffer object | ||
* @param {ArrayBuffer|Buffer} source ArrayBuffer or Buffer object | ||
* @returns {Boolean} arg is CUR or not | ||
*/ | ||
const isCUR = arrayBuffer => { | ||
if (!(arrayBuffer instanceof ArrayBuffer)) { | ||
return false; | ||
} | ||
const dataView = new DataView(arrayBuffer); | ||
const isCUR = source => { | ||
const dataView = toDataView(source); | ||
return dataView.getUint16(0, true) === 0 && dataView.getUint16(2, true) === 2; | ||
@@ -15,0 +14,0 @@ }; |
'use strict'; | ||
const toDataView = require('to-data-view'); | ||
/** | ||
* Check the ArrayBuffer is valid ICO. | ||
* @memberof ICO | ||
* @param {ArrayBuffer} arrayBuffer ICO file data. | ||
* @param {ArrayBuffer|Buffer} source ICO file data. | ||
* @returns {Boolean} True if arg is ICO. | ||
*/ | ||
const isICO = arrayBuffer => { | ||
if (!(arrayBuffer instanceof ArrayBuffer)) { | ||
return false; | ||
} | ||
const dataView = new DataView(arrayBuffer); | ||
const isICO = source => { | ||
const dataView = toDataView(source); | ||
return dataView.getUint16(0, true) === 0 && dataView.getUint16(2, true) === 1; | ||
@@ -15,0 +14,0 @@ }; |
'use strict'; | ||
const toDataView = require('to-data-view'); | ||
/** | ||
* Check the ArrayBuffer is valid PNG. | ||
* @access private | ||
* @param {ArrayBuffer} arrayBuffer ArrayBuffer object | ||
* @param {ArrayBuffer|Buffer} source ArrayBuffer or Buffer object | ||
* @returns {Boolean} arg is PNG or not | ||
*/ | ||
const isPNG = arrayBuffer => { | ||
if (!(arrayBuffer instanceof ArrayBuffer)) { | ||
return false; | ||
} | ||
const dataView = new DataView(arrayBuffer); | ||
const isPNG = source => { | ||
const dataView = toDataView(source); | ||
return dataView.getUint32(0, false) === 0x89504E47 && dataView.getUint32(4, false) === 0x0D0A1A0A; | ||
@@ -15,0 +14,0 @@ }; |
@@ -6,3 +6,2 @@ 'use strict'; | ||
const bmp = require('bmp-js'); | ||
const bufferToArrayBuffer = require('./buffer-to-arraybuffer'); | ||
const fileType = require('file-type'); | ||
@@ -46,3 +45,3 @@ const jpeg = require('jpeg-js'); | ||
* @access private | ||
* @param {ArrayBuffer} arrayBuffer image buffer | ||
* @param {ArrayBuffer|Buffer} arrayBuffer image buffer | ||
* @returns {ImageData} imageData | ||
@@ -102,3 +101,3 @@ */ | ||
const imageBuffer = encoder(imageData); | ||
return bufferToArrayBuffer(imageBuffer); | ||
return imageBuffer.buffer.slice(imageBuffer.byteOffset, imageBuffer.byteOffset + imageBuffer.byteLength); | ||
} | ||
@@ -105,0 +104,0 @@ }; |
@@ -5,3 +5,2 @@ 'use strict'; | ||
const arrayBufferIsICO = require('../is-ico'); | ||
const bufferToArrayBuffer = require('./buffer-to-arraybuffer'); | ||
const parseICO = require('../parse'); | ||
@@ -16,9 +15,3 @@ const parseICOSync = require('../parse/sync'); | ||
*/ | ||
const isICO = buffer => { | ||
const arrayBuffer = bufferToArrayBuffer(buffer); | ||
if (!arrayBuffer) { | ||
return false; | ||
} | ||
return arrayBufferIsICO(arrayBuffer); | ||
}; | ||
const isICO = buffer => arrayBufferIsICO(buffer); | ||
@@ -32,9 +25,3 @@ /** | ||
*/ | ||
const parse = (buffer, mime) => { | ||
const arrayBuffer = bufferToArrayBuffer(buffer); | ||
if (!arrayBuffer) { | ||
return Promise.reject(new TypeError('"buffer" argument must be a Buffer or ArrayBuffer')); | ||
} | ||
return parseICO(arrayBuffer, mime, Image); | ||
}; | ||
const parse = (buffer, mime) => parseICO(buffer, mime || 'image/png', Image); | ||
@@ -48,9 +35,3 @@ /** | ||
*/ | ||
const parseSync = (buffer, mime) => { | ||
const arrayBuffer = bufferToArrayBuffer(buffer); | ||
if (!arrayBuffer) { | ||
throw new TypeError('"buffer" argument must be a Buffer or ArrayBuffer'); | ||
} | ||
return parseICOSync(arrayBuffer, mime, Image); | ||
}; | ||
const parseSync = (buffer, mime) => parseICOSync(buffer, mime || 'image/png', Image); | ||
@@ -57,0 +38,0 @@ /** |
'use strict'; | ||
const bitDepthOfPNG = require('../bit-depth-of-png'); | ||
const parsedImage = require('./parsed-image'); | ||
const isPNG = require('../is-png'); | ||
const parseBMP = require('../parse-bmp'); | ||
const split = require('./split'); | ||
const decodeIco = require('decode-ico'); | ||
/** | ||
* @typedef {Object} ParsedImage | ||
* @property {Number} width Image width. | ||
* @property {Number} height Image height. | ||
* @property {Number} bpp Image color depth as bits per pixel. | ||
* @property {ArrayBuffer} buffer Image buffer. | ||
*/ | ||
/** | ||
* Parse ICO and return some image object. | ||
* @access private | ||
* @param {ArrayBuffer} arrayBuffer ICO file data. | ||
* @param {ArrayBuffer|Buffer} data ICO file data. | ||
* @param {String} mime MIME type for output. | ||
@@ -17,6 +21,7 @@ * @param {Object} Image Image encoder/decoder | ||
*/ | ||
const parse = (arrayBuffer, mime, Image) => { | ||
let icons = []; | ||
const parse = (data, mime, Image) => { | ||
let icons = null; | ||
try { | ||
icons = split(arrayBuffer); | ||
icons = decodeIco(data); | ||
} catch (err) { | ||
@@ -26,16 +31,28 @@ return Promise.reject(err); | ||
const parseIconImage = (width, height, iconImage) => { | ||
if (isPNG(iconImage)) { | ||
const bit = bitDepthOfPNG(iconImage); | ||
return Image.decode(iconImage).then(imageData => Object.assign(imageData, { bit })); | ||
const decodePng = icon => { | ||
if (icon.type !== 'png') { | ||
return Promise.resolve(icon); | ||
} | ||
return Promise.resolve(parseBMP(width, height, iconImage)); | ||
return Image.decode(icon.data).then(decoded => Object.assign(icon, { | ||
type: 'bmp', | ||
data: decoded.data | ||
})); | ||
}; | ||
const parsedImages = icons | ||
.map(iconImageData => parseIconImage(iconImageData.width, iconImageData.height, iconImageData.iconImage) | ||
.then(imageData => Image.encode(imageData, mime) | ||
.then(imageBuffer => parsedImage(imageData, imageBuffer, iconImageData.hotspot)))); | ||
return Promise.all(parsedImages); | ||
const encodeImage = icon => Image.encode(icon, mime).then(encoded => Object.assign(icon, { | ||
type: mime.replace('image/', ''), | ||
buffer: encoded | ||
})); | ||
const transcodeImage = icon => { | ||
if (mime === 'image/png' && icon.type === 'png') { | ||
return Promise.resolve(Object.assign({ buffer: icon.data.buffer.slice(icon.data.byteOffset, icon.data.byteOffset + icon.data.byteLength) }, icon)); | ||
} | ||
return decodePng(icon).then(encodeImage); | ||
}; | ||
return Promise.all(icons.map(transcodeImage)); | ||
}; | ||
module.exports = parse; |
'use strict'; | ||
const bitDepthOfPNG = require('../bit-depth-of-png'); | ||
const parsedImage = require('./parsed-image'); | ||
const isPNG = require('../is-png'); | ||
const parseBMP = require('../parse-bmp'); | ||
const split = require('./split'); | ||
const decodeIco = require('decode-ico'); | ||
@@ -12,3 +8,3 @@ /** | ||
* @access private | ||
* @param {ArrayBuffer} arrayBuffer ICO file data. | ||
* @param {ArrayBuffer|Buffer} data ICO file data. | ||
* @param {String} mime MIME type for output. | ||
@@ -18,22 +14,27 @@ * @param {Object} Image Image encoder/decoder | ||
*/ | ||
const parseSync = (arrayBuffer, mime, Image) => { | ||
const icons = split(arrayBuffer); | ||
const parseSync = (data, mime, Image) => { | ||
const icons = decodeIco(data); | ||
const parseIconImage = (width, height, iconImage) => { | ||
if (isPNG(iconImage)) { | ||
const bit = bitDepthOfPNG(iconImage); | ||
const imageData = Image.decodeSync(iconImage); | ||
return Object.assign(imageData, { bit }); | ||
const transcodeImage = icon => { | ||
if (mime === 'image/png' && icon.type === 'png') { | ||
return Object.assign({ buffer: icon.data.buffer.slice(icon.data.byteOffset, icon.data.byteOffset + icon.data.byteLength) }, icon); | ||
} | ||
return parseBMP(width, height, iconImage); | ||
if (icon.type === 'png') { | ||
const decoded = Image.decodeSync(icon.data); | ||
Object.assign(icon, { | ||
type: 'bmp', | ||
data: decoded.data | ||
}); | ||
} | ||
return Object.assign(icon, { | ||
type: mime.replace('image/', ''), | ||
buffer: Image.encodeSync(icon, mime) | ||
}); | ||
}; | ||
const parsedImages = icons | ||
.map(iconImageData => { | ||
const imageData = parseIconImage(iconImageData.width, iconImageData.height, iconImageData.iconImage); | ||
const imageBuffer = Image.encodeSync(imageData, mime); | ||
return parsedImage(imageData, imageBuffer, iconImageData.hotspot); | ||
}); | ||
return parsedImages; | ||
return icons.map(transcodeImage); | ||
}; | ||
module.exports = parseSync; |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
44924
7
22
17
383
+ Addeddecode-ico@^0.2.1
+ Addedto-data-view@^1.0.0
+ Addeddecode-ico@0.2.1(transitive)
+ Addedto-data-view@1.1.0(transitive)