-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSeamCarver.min.js
2 lines (2 loc) · 7.02 KB
/
SeamCarver.min.js
1
2
!function t(i,e,a){function r(n,s){if(!e[n]){if(!i[n]){var u="function"==typeof require&&require;if(!s&&u)return u(n,!0);if(h)return h(n,!0);var m=new Error("Cannot find module '"+n+"'");throw m.code="MODULE_NOT_FOUND",m}var o=e[n]={exports:{}};i[n][0].call(o.exports,function(t){var e=i[n][1][t];return r(e?e:t)},o,o.exports,t,i,e,a)}return e[n].exports}for(var h="function"==typeof require&&require,n=0;n<a.length;n++)r(a[n]);return r}({1:[function(t,i,e){"use strict";function a(t,i){if(!(t instanceof i))throw new TypeError("Cannot call a class as a function")}var r=function(){function t(t,i){for(var e=0;e<i.length;e++){var a=i[e];a.enumerable=a.enumerable||!1,a.configurable=!0,"value"in a&&(a.writable=!0),Object.defineProperty(t,a.key,a)}}return function(i,e,a){return e&&t(i.prototype,e),a&&t(i,a),i}}(),h=0,n=1,s=2,u=1e3,m=function(){function t(i){a(this,t),this.canvas=i,this.width=i.width,this.height=i.height,this.context=i.getContext("2d"),this.imageData=this.context.getImageData(0,0,this.width,this.height),this.picture=this.imageData.data,this.energyMatrix=new Array(this.width),this.minsumMatrix=new Array(this.width),this.minxMatrix=new Array(this.width);for(var e=0;e<this.width;e++)this.energyMatrix[e]=new Float32Array(this.height),this.minsumMatrix[e]=new Float32Array(this.height),this.minxMatrix[e]=new Uint16Array(this.height);console.time("createEnergyMatrix"),this.createEnergyMatrix(),console.timeEnd("createEnergyMatrix")}return r(t,[{key:"pixelToIndex",value:function(t,i){if(t<0||t>=4*this.width||i<0||i>=this.height)throw new Error("IndexOutOfBoundsException : "+t+","+i);return 4*(i*this.width+t)}},{key:"indexToX",value:function(t){return parseInt(t/4%this.width)}},{key:"indexToY",value:function(t){return parseInt(t/(4*this.width))}},{key:"rgbToNum",value:function(t,i,e){var a=t;return a=(a<<8)+i,a=(a<<8)+e}},{key:"numToRgb",value:function(t){var i=t>>16&255,e=t>>8&255,a=255&t;return[i,e,a]}},{key:"isBorderPixel",value:function(t,i){return t<=0||i<=0||t>=this.width-1||i>=this.height-1}},{key:"pixelInRange",value:function(t,i){return t>=0&&i>=0&&t<=this.width-1&&i<=this.height-1}},{key:"energy",value:function(t,i){if(this.isBorderPixel(t,i))return u;var e=this.pixelToIndex(t-1,i),a=this.pixelToIndex(t+1,i),r=this.pixelToIndex(t,i-1),m=this.pixelToIndex(t,i+1),o=this.picture,x=Math.sqrt((o[a+h]-o[e+h])*(o[a+h]-o[e+h])+(o[a+n]-o[e+n])*(o[a+n]-o[e+n])+(o[a+s]-o[e+s])*(o[a+s]-o[e+s])+(o[m+h]-o[r+h])*(o[m+h]-o[r+h])+(o[m+n]-o[r+n])*(o[m+n]-o[r+n])+(o[m+s]-o[r+s])*(o[m+s]-o[r+s]));return x}},{key:"recalculate",value:function(t,i){var e={};if(e.energy=this.energy(t,i),e.vminsum=Number.POSITIVE_INFINITY,i>=this.height-1)e.vminsum=e.energy,e.minx=t;else{var a=0;t-1>=0&&(e.vminsum=this.minsumMatrix[t-1][i+1]+e.energy,e.minx=t-1),t<this.width&&(a=this.minsumMatrix[t][i+1]+e.energy,a<e.vminsum&&(e.vminsum=a,e.minx=t)),t+1<this.width&&(a=this.minsumMatrix[t+1][i+1]+e.energy,a<e.vminsum&&(e.vminsum=a,e.minx=t+1))}return e}},{key:"createEnergyMatrix",value:function(){this.maxVminsum=0;for(var t=this.height-1;t>=0;t--)for(var i=0;i<this.width;i++){var e=this.recalculate(i,t);this.maxVminsum=Math.max(e.vminsum,this.maxVminsum),this.energyMatrix[i][t]=e.energy,this.minsumMatrix[i][t]=e.vminsum,this.minxMatrix[i][t]=e.minx}}},{key:"findVerticalSeam",value:function(){for(var t=[],i=0,e=Number.POSITIVE_INFINITY,a=0;a<this.width;a++)this.minsumMatrix[a][0]<e&&(e=this.minsumMatrix[a][0],i=a);t[0]=i;for(var r=0;r<this.height-1;)i=this.minxMatrix[i][r],r++,t[r]=i;return t}},{key:"removePixelsFromDataStructures",value:function(t){this.imageData=this.context.createImageData(this.width-1,this.height);for(var i=this.height-1;i>=0;i--){for(var e=t[i],a=0;a<e;a++)for(var r=this.pixelToIndex(a,i),h=r-4*i,n=0;n<4;n++)this.imageData.data[h+n]=this.picture[r+n];for(var a=e;a<this.width-1;a++){for(var h=this.pixelToIndex(a,i)-4*i,s=this.pixelToIndex(a+1,i),n=0;n<4;n++)this.imageData.data[h+n]=this.picture[s+n];var u=this.energyMatrix[a+1][i],m=this.minxMatrix[a+1][i],o=this.minsumMatrix[a+1][i];m--,this.energyMatrix[a][i]=u,this.minxMatrix[a][i]=m,this.minsumMatrix[a][i]=o}}this.energyMatrix.splice(this.width-1,1),this.minxMatrix.splice(this.width-1,1),this.minsumMatrix.splice(this.width-1,1),this.picture=this.imageData.data,this.width--}},{key:"recalculateEnergiesAndFindAffectedPixels",value:function(t){for(var i=[],e=this.height-2;e>=0;e--){for(var a=t[e],r=[],h=-1;h<1;h++){var n=a+h;this.pixelInRange(n,e)&&(this.energyMatrix[n][e]=this.energy(n,e),r.push(n))}i[e]=r}return i}},{key:"recalculateVminsumForAffectedPixels",value:function(t){for(var i={},e={},a=this.height-2,r=t[a],h=this.width-1;r;){var n=r.pop(),s=this.pixelToIndex(n,a);if(0===r.length&&(r=t[--a]),!i[s]){i[s]=!0;var u=this.energyMatrix[n][a],m=this.minsumMatrix[n][a];this.minsumMatrix[n][a]=Number.POSITIVE_INFINITY;for(var o=Math.max(n-1,0);o<Math.min(n+2,h+1);o++){var x=this.minsumMatrix[o][a+1],c=x+u;c<this.minsumMatrix[n][a]&&(this.minsumMatrix[n][a]=c,this.minxMatrix[n][a]=o)}if(0!==a&&m!==this.minsumMatrix[n][a])for(var o=Math.max(n-1,0);o<Math.min(n+2,h+1);o++){var g=this.pixelToIndex(o,a-1);e[g]||(e[g]=!0,t[a-1].push(o))}}}}},{key:"removeVerticalSeam",value:function(t){this.removePixelsFromDataStructures(t);var i=this.recalculateEnergiesAndFindAffectedPixels(t);this.recalculateVminsumForAffectedPixels(i)}},{key:"reDrawImage",value:function(t){var i=t.field,e=t.actualSize;if(this.context.clearRect(0,0,this.canvas.width,this.canvas.height),this.canvas.width=this.imageData.width,this.canvas.height=this.imageData.height,e?(this.canvas.style.width=this.imageData.width+"px",this.canvas.style.height=this.imageData.height+"px"):(this.canvas.style.width="inherit",this.canvas.style.height="inherit"),"energy"===i||"vminsum"===i||i!==this.imageData.dataField){this.imageData=this.context.createImageData(this.width,this.height),this.imageData.dataField=i;for(var a=0;a<this.height;a++)for(var r=0;r<this.width;r++){var h=this.pixelToIndex(r,a);if("energy"===i)var n=this.energyMatrix[r][a],s=Math.min(255,n/255*255);else{if("minsum"!==i){if("minx"===i){for(var n=this.minxMatrix[r][a],u=n-r+1,m=0;m<3;m++)this.imageData.data[h+m]=0;u>=0&&u<=2&&(this.imageData.data[h+u]=255),this.imageData.data[h+3]=255;continue}for(var m=0;m<4;m++)this.imageData.data[h+m]=this.picture[h+m];continue}var n=this.minsumMatrix[r][a],s=(n-1e3)/(this.maxVminsum-1e3)*255}for(var m=0;m<3;m++)this.imageData.data[h+m]=s;this.imageData.data[h+3]=255}}this.context.putImageData(this.imageData,0,0)}},{key:"printMatrix",value:function(t){console.log(this.toString(t))}},{key:"toString",value:function(t){t=t||"rgb";var i="";if("rgb"===t)for(var e=0;e<this.height;e++){for(var a=0;a<this.width;a++){var r=this.pixelToIndex(a,e),h=Array.prototype.slice.call(this.picture,r,r+3);i+=(this.rgbToNum(h[0],h[1],h[2])/1e5).toFixed(2)+"\t"}i+="\n"}else for(var e=0;e<this.height;e++){for(var a=0;a<this.width;a++){var n;"energy"===t?n=this.energyMatrix[a][e]:"minsum"===t?n=this.minsumMatrix[a][e]:"minx"===t&&(n=this.minxMatrix[a][e]),i+=n||0===n?n.toFixed(2)+"\t":"-----\t"}i+="\n"}return i}}]),t}();i.exports=m},{}]},{},[1]);
//# sourceMappingURL=maps/SeamCarver.min.js.map