Разбери что делает этот код: "function procImg(isLvl,isRed){\r\n"
    "if (document.getElementsByClassName('sourceImage').length == 0){\r\n"
        "alert('First select image');\r\n"
        "return;\r\n"
    "}\r\n"
    
    "var palInd=epdArr[epdInd][2];\r\n"
    
    "if (isRed&&((palInd&1)==0)){\r\n"
        "alert('This white-black display');\r\n"
        "return;\r\n"
    "}\r\n"
    
    "if (!isRed)palInd=palInd&0xFE;\r\n"
    "curPal=palArr[palInd];\r\n"
    "getElm('dstBox').innerHTML=\r\n"
    "'<span class=\"title\">Processed image</span><br><canvas id=\"canvas\"></canvas>';\r\n" 
    "var canvas=getElm('canvas');\r\n"
    "sW=srcImg.width;\r\n"
    "sH=srcImg.height;\r\n"
    "source=getElm('source');\r\n"
    "source.width=sW;\r\n"
    "source.height=sH;\r\n"
    "source.getContext('2d').drawImage(srcImg,0,0,sW,sH);\r\n"
    "dX=parseInt(getElm('nud_x').value);\r\n"
    "dY=parseInt(getElm('nud_y').value);\r\n"
    "dW=parseInt(getElm('nud_w').value);\r\n"
    "dH=parseInt(getElm('nud_h').value);\r\n"
    
    "if((dW<3)||(dH<3)){\r\n"
        "alert('Image is too small');\r\n"
        "return;\r\n"
    "}\r\n"
    
    "canvas.width=dW;\r\n"
    "canvas.height=dH;\r\n"
    "var index=0;\r\n"
    "var pSrc=source.getContext('2d').getImageData(0,0,sW,sH);\r\n"
    "var pDst=canvas.getContext('2d').getImageData(0,0,dW,dH);\r\n"
    
    "if(isLvl){\r\n"
        "for (var j=0;j<dH;j++){\r\n"
            "var y=dY+j;\r\n"
            "if ((y<0)||(y>=sH)){\r\n"
                "for (var i=0;i<dW;i++,index+=4) setVal(pDst,index,(i+j)%2==0?1:0);\r\n"
                "continue;\r\n"
            "}\r\n"
            
            "for (var i=0;i<dW;i++){\r\n"
                "var x=dX+i;\r\n"
                
                "if ((x<0)||(x>=sW)){\r\n"
                    "setVal(pDst,index,(i+j)%2==0?1:0);\r\n"
                    "index+=4;\r\n"
                     "continue;\r\n"
                 "}\r\n"
                 
                 "var pos=(y*sW+x)*4;\r\n"     
                 "setVal(pDst,index,getNear(pSrc.data[pos],pSrc.data[pos+1],pSrc.data[pos+2]));\r\n"
                 "index+=4;\r\n"
             "}\r\n"
         "}\r\n"
    "}else{\r\n"
        "var aInd=0;\r\n"
        "var bInd=1;\r\n"
        "var errArr=new Array(2);\r\n"
        "errArr[0]=new Array(dW);\r\n"
        "errArr[1]=new Array(dW);\r\n"
        
        "for (var i=0;i<dW;i++)\r\n"
            "errArr[bInd][i]=[0,0,0];\r\n"
            
        "for (var j=0;j<dH;j++){\r\n"
            "var y=dY+j;\r\n"
            
            "if ((y<0)||(y>=sH)){\r\n"
                "for (var i=0;i<dW;i++,index+=4)setVal(pDst,index,(i+j)%2==0?1:0);\r\n"  
                "continue;\r\n"
            "}\r\n"
            
            "aInd=((bInd=aInd)+1)&1;\r\n"
            "for (var i=0;i<dW;i++)errArr[bInd][i]=[0,0,0];\r\n"
            
            "for (var i=0;i<dW;i++){\r\n"
                "var x=dX+i;\r\n"
                
                "if ((x<0)||(x>=sW)){\r\n"
                    "setVal(pDst,index,(i+j)%2==0?1:0);\r\n"
                    "index+=4;\r\n"
                    "continue;\r\n"
                "}\r\n"
                
                "var pos=(y*sW+x)*4;\r\n"
                "var old=errArr[aInd][i];\r\n"
                "var r=pSrc.data[pos  ]+old[0];\r\n"
                "var g=pSrc.data[pos+1]+old[1];\r\n"
                "var b=pSrc.data[pos+2]+old[2];\r\n"
                "var colVal = curPal[getNear(r,g,b)];\r\n"
                "pDst.data[index++]=colVal[0];\r\n"
                "pDst.data[index++]=colVal[1];\r\n"
                "pDst.data[index++]=colVal[2];\r\n"
                "pDst.data[index++]=255;\r\n"
                "r=(r-colVal[0]);\r\n"
                "g=(g-colVal[1]);\r\n"
                "b=(b-colVal[2]);\r\n"
                
                "if (i==0){\r\n"
                    "errArr[bInd][i  ]=addVal(errArr[bInd][i  ],r,g,b,7.0);\r\n"
                    "errArr[bInd][i+1]=addVal(errArr[bInd][i+1],r,g,b,2.0);\r\n"
                    "errArr[aInd][i+1]=addVal(errArr[aInd][i+1],r,g,b,7.0);\r\n"
                "}else if (i==dW-1){\r\n"
                    "errArr[bInd][i-1]=addVal(errArr[bInd][i-1],r,g,b,7.0);\r\n"
                    "errArr[bInd][i  ]=addVal(errArr[bInd][i  ],r,g,b,9.0);\r\n"
                "}else{\r\n"
                    "errArr[bInd][i-1]=addVal(errArr[bInd][i-1],r,g,b,3.0);\r\n"
                    "errArr[bInd][i  ]=addVal(errArr[bInd][i  ],r,g,b,5.0);\r\n"
                    "errArr[bInd][i+1]=addVal(errArr[bInd][i+1],r,g,b,1.0);\r\n"
                    "errArr[aInd][i+1]=addVal(errArr[aInd][i+1],r,g,b,7.0);\r\n"
                "}\r\n"
            "}\r\n"
        "}\r\n"
    "}\r\n"

    "canvas.getContext('2d').putImageData(pDst,0,0);\r\n"
"}\r\n");
}