-
Notifications
You must be signed in to change notification settings - Fork 11
/
histogram.min.js
1 lines (1 loc) · 1.47 KB
/
histogram.min.js
1
!function(){var t,o,e=function(e,h){if(-1===["string","object"].indexOf(typeof e))throw new TypeError("Expected imageBuffer to be a string or a buffer");if("function"!=typeof h)throw new TypeError("Expected callback to be a function");var r,n,a;r=e,n=function(e,r){if(e)return h(e);var n,a,t,o,i,g={red:new Array(256),green:new Array(256),blue:new Array(256),alpha:new Array(256),colors:{rgb:0,rgba:0},palettes:{rgb:[],rgba:[]},greyscale:!0,alphachannel:!1},l={},c={};for(i=0;i<256;i+=1)g.red[i]=g.green[i]=g.blue[i]=g.alpha[i]=0;for(i=0;i<r.length;i+=4){n=r[i],a=r[i+1],t=r[i+2],(o=r[i+3])<255&&(g.alphachannel=!0),(g.greyscale&&n!==a||n!==t)&&(g.greyscale=!1),g.red[n]+=1,g.green[a]+=1,g.blue[t]+=1,g.alpha[o]+=1;var s=(16777216*n+65536*a+256*t+o).toString(16),u="#"+"0000000".substr(0,8-s.length)+s,f=u.substr(0,7);f in l?l[f]+=1:(l[f]=1,g.palettes.rgb.push(f),g.colors.rgb+=1),u in c?c[u]+=1:(c[u]=1,g.palettes.rgba.push(u),g.colors.rgba+=1)}h(null,g)},(a=new o).crossOrigin="Anonymous",a.onerror=function(){n(new Error("error while reading from input stream"))},a.onload=function(){var e=new t(a.width,a.height).getContext("2d");e.drawImage(a,0,0),n(null,e.getImageData(0,0,a.width,a.height).data)},a.src=r};if("object"==typeof exports){var r=require("canvas");t=r.Canvas,o=r.Image,module.exports=e}else t=function(e,r){var n=document.createElement("canvas");return n.width=e,n.height=r,n},o=window.Image,"function"==typeof define?define([],function(){return e}):window&&(window.histogram=e)}();