用户注册 登录
珍珠湾全球网 返回首页

岳东晓 -- 珍珠湾全球网 ... http://ydx.zzwave.com [收藏] [复制] [分享] [RSS] 岳东晓 -- 珍珠湾全球网

日志

珍珠湾图像转换程序源代码

已有 263 次阅读2017-3-17 14:13 |个人分类:计算机|系统分类:科技| 源代码, 程序

在这篇博文里,我推出了这个图像转换程序,包含了保角变换与小地球变换。源代码如下(代码中包含其他变换,但效果不佳没有使用,另外有部分是废代码),提供作为编程学习资料。

//Image conformal
//copyright (c) YDX
//
var canvas = document.getElementById('canvas');

var context = canvas.getContext('2d');

var transFunc;
var lat0=Math.PI/2;
var lon0=0;

// (z+a)/(z+b)

function polar(a,b, w0, h0, w1, h1, x,y) {
x = x - w1/2;
y = y - h1/2;
var d = Math.sqrt(w0*w0+h0*h0);
var xp = w0/Math.PI/2 * (Math.atan2(y, x)) ;
var yp = 2*h0*x/d /Math.cos(2*Math.PI*xp/w0);
return [Math.floor(xp), Math.floor(yp)];
}

function fracConform(a,b, w0, h0, w1, h1, x,y) {

x = x*2/w1 -1;
y = -y*2/h1 +1;

var d = (b+x)*(b+x) + y*y;
var r = (x+a)*(x+b) + y*y;
var i = (b-a)*y;
y = r/d *h0/2 ;
x = i/d *w0/2 +w0/2;
return [Math.floor(x), Math.floor(y)];
}

// (z+a)/(z+b)

function fracConformInv(a,b,w0, h0, w1, h1, x,y) {
x = x*2/w1 -1;
y = - y*2/h1  +1;
var tmp =x;
x=-y;
y = tmp;

var d = (x-1)*(x-1) + y* y;
var r = (a-b*x)*(x-1) - b*y*y;
var i = -b*y*(x-1) - (a-b*x)*y;

x = i/d *w0/2 +w0/2;
y = r/d *h0/2 ;
return [Math.floor(x), Math.floor(y)];

}

function invConformInv(a,b, w0, h0, w1, h1, x,y) {
x = x*2/w1;
y = y*2/h1  -1;

var d = x*x + y* y;
var r = x;
var i = -y;
if(d==0) {
r=i=0;
}else {
r = r/d;
i = i/d;
}

x = (i+1) *w0/2;
y = (r-1/2)*h0/2 ;

return [Math.floor(x+0.5), Math.floor(y+0.5)];

}

function sinz(a,b, w0,h0, w1,h1, x,y) {
x = x*2/w1-1;
y = y*2/h1 -1;
var ip = math.complex(x, y);
var op = math.log(ip);

op=math.conj(op);
x = op.re * 2 /Math.PI +1;
y = op.im  /2 ;

x= x*w0;
y= y*h0;


return [Math.floor(x+0.5), Math.floor(y+0.5)];



}

function littlePlanet(a,b, w0, h0, w1, h1, x,y) {
x = x*2/w1-1;
y = y*2/h1 -1;

x = 2*x;
y = 2*y;

var d = Math.sqrt(x*x + y* y);
var R = 1/2;
var c = Math.atan2(d, 2*R)*2;

var lat = Math.asin(Math.cos(c) * Math.sin(lat0) + y*Math.sin(c) * Math.cos(lat0) /d);
var lon = lon0 + Math.atan2(x*Math.sin(c), d*Math.cos(lat0)*Math.cos(c) - y*Math.sin(lat0)*Math.sin(c));
//lat -pi/2 -> pi/2, lon -pi->pi
//
//
x = (lon/Math.PI +1) * w0/2; 
y = (lat*2/Math.PI +1) * h0/2; 

return [Math.floor(x+0.5), Math.floor(y+0.5)];

}

function expConf(a,b,w0, h0, w1, h1, x,y) {
x = x/w1*2 -1/2;
y = y/h1 ;

var d= Math.exp(-y);
var r = d*Math.cos(x);
var i = d*Math.sin(x);
x = r*d *w0/2 + w0/2 ;
y = i*d *w0/2  ;
return [Math.floor(x), Math.floor(y)];

}

function fracConformF(a,b, w0, h0, x,y, w1, h1) {

x = x -w0/2;
y = y -h0/2;

var d = (a+x)*(a+x) + y*y;
var r = (x+a)*(x+b) + y*y;
var i = (a-b)*y;
x = r/d *w1/2 + w1/2;
y = i/d *w1/2 + h1/2;

return [Math.floor(x), Math.floor(y)];
}

function sqConform(a,b, w0, h0, w1, h1, x,y) {

x = x/w1*2 -1;
y = y/h1*2 -1;
x = 3*x;
y = 3*y;

var r = x*x-y*y;
var i = 2*x*y;
x = r*w0/2 + w0/2;
y = i*h0/2 ;

return [Math.floor(x), Math.floor(y)];
}

function szConform(a,b, w0, h0, w1, h1, x,y) {

x = x/w1*2 -1;
y = y/h1*2 ;

var d = Math.sqrt(x*x+y*y);
var r= x+ x/d;
var i = y - y/d;
x = r*w0/2 +w0/2;
y = i*h0/2 +h0/2;

return [Math.floor(x), Math.floor(y)];
}

function inRect(w,h, x,y) {
return x>=0 && x<=w && y>=0 && y<h;
}

function getPix(pixels, w0, h0, w1, h1, x, y) {
var arr=[];
var func = transFunc;
var pos = func(-1,1, w0, h0, w1, h1, x,y);
var xp =pos[0];
var yp = pos[1];
var loc0 = (w0*yp +xp)*4;

var redu=1;
if(!inRect(w0,h0, xp,yp) && (loc0<0 || loc0> w0*h0*4)){
loc0 = Math.abs(loc0%(w0*h0*4));
redu = Math.abs(3.5*loc0/(w0*h0*4));
//return [123,0,0,255];
}
        arr[0] = pixels[loc0];
        arr[1]=  pixels[++loc0];
        arr[2]=  pixels[++loc0];
        arr[3]=  pixels[++loc0]/redu;
return arr;

}

function getPixLoc(pixels, w0, h0,x,y,w1, h1) {
var arr=[];
var pos = fracConformF(w1/2,-w1/2, w0, h0,x,y, w1, h1);

var xp =pos[0];
var yp = pos[1];

var loc0 = (w0*y +x)*4;
        arr[0] = pixels[loc0];
        arr[1]=  pixels[++loc0];
        arr[2]=  pixels[++loc0];
        arr[3]=  pixels[++loc0];
return {loc: yp*w1+xp, data: arr};

}


var image_obj = new Image(); 

function forMap (){
    var w0 = this.width;
    var h0 = this.height;

    var w1 = w0;
    var h1 = h0;

    canvas.width=800;
    canvas.height=800;

    var newImage = context.createImageData(w1, h1);

    context.drawImage(image_obj, 0, 0);
    var arr = context.getImageData(0, 0, w0, h0);

    var pixels = arr.data;
    var newdata = newImage.data;


    for (var y=0; y< h0; y++) {
   for (var x=0; x< w0; x++) {
   
   var pixl = getPixLoc(pixels,w0,h0, x,y, w1,h1);
   var loc1 = pixl.loc;
   var pix = pixl.data;
           newdata[loc1] = pix[0];
           newdata[++loc1] = pix[1];
           newdata[++loc1 ] = pix[2];
                    newdata[++loc1 ] = pix[3];
   }
    }

    //newImage.data =pixels;
    //context.clearRect(0, 0, w1, h1);
    context.putImageData(newImage, 0,0);
}

function revMap (){
    var w0 = this.width;
    var h0 = this.height;


    canvas.width=w0;
    canvas.height=h0;
    context.drawImage(image_obj, 0, 0);
    var arr = context.getImageData(0, 0, w0, h0);
    var pixels = arr.data;

    var w1 = 600;
    var h1 = 600;
    
    var newImage = context.createImageData(w1, h1);


    var newdata = newImage.data;


    for (var y=0; y< h1; y++) {
   for (var x=0; x< w1; x++) {
   var pix = getPix(pixels, w0,h0,w1,h1, x,y);
   var loc1 = (y*w1 + x)*4;

           newdata[loc1] = pix[0];
           newdata[++loc1] = pix[1];
           newdata[++loc1 ] = pix[2];
                    newdata[++loc1 ] = pix[3];
   }
    }

    //newImage.data =pixels;
    context.clearRect(0, 0, w0, h0);
    canvas.width=w1;
    canvas.height=h1;
    context.putImageData(newImage, 0,0);
}

image_obj.onload = revMap;

jQuery('#view').click (function() {
transFunc = fracConformInv;
var img = jQuery('#imgurl').val();
var iu = img.replace(/http:\/\/(www\.)?(zzwave|ohaiwan|zhenzhubay)\.com/i, '');
image_obj.src=iu;

});

jQuery('#view2').click (function() {
transFunc = littlePlanet;
var img = jQuery('#imgurl').val();
lat0 = parseFloat(trans.lat0)||90;
lon0 = parseFloat(trans.lon0)||0;
lat0 = lat0*Math.PI/180;
lon0 = lon0*Math.PI/180;

var iu = img.replace(/http:\/\/(www\.)?(zzwave|ohaiwan|zhenzhubay)\.com/i, '');
image_obj.src=iu;

});

jQuery('#savefile').click(function() {
var fn = image_obj.src.replace(/http:\/\/(www\.)?(zzwave|ohaiwan|zhenzhubay)\.com/i, '').replace(/\/|\\|:/g, '');
canvas.toBlob(function(blob) {
saveAs( blob,  fn + ".png");
}, "image/png");
alert("File saved as "+fn+'.png');
});


路过

鸡蛋

鲜花

支持

雷人

难过

搞笑
 

评论 (0 个评论)

facelist

您需要登录后才可以评论 登录 | 用户注册

Archiver|手机版|珍珠湾全球网

GMT+8, 2017-3-31 00:38 , Processed in 0.019048 second(s), 8 queries , Apc On.

Powered by Discuz! X2.5

回顶部