David Serrano
Well-known member
Click on a small image (not beyond 100x100) to make it active and run the script. Previews are OK.
Hacer clic en una imagen peque?a (no mucho m?s de 100x100) para hacerla activa, y ejecutar el script. Se pueden usar previews.
Code:
#include <pjsr/UndoFlag.jsh>
#include <pjsr/FillRule.jsh>
var xform_cache = [];
function xform_coords (x, y, z) {
var k = format ('%05d%05d%6.5f', x, y, z);
if (xform_cache[k]) { return xform_cache[k]; }
var angle = 10/180*Math.PI;
var alt = 60/180*Math.PI;
var new_x = x; var origin_x = 14;
var new_y = y; var origin_y = 14;
var scale = 7;
// rotation
var mod = Math.sqrt (
Math.pow (Math.abs (origin_x - x), 2) +
Math.pow (Math.abs (origin_y - y), 2)
);
var cur_angle = Math.atan2 (origin_y - new_y, new_x - origin_x);
//console.writeln ('got x (',x,') y (',y,') mod (',mod,') cur_angle (',(cur_angle/Math.PI*180),')');
new_x = origin_x + mod * Math.cos(cur_angle - angle);
new_y = origin_y - mod * Math.sin(cur_angle - angle);
//console.writeln ('new x (',new_x,') y (',new_y,')');
//console.writeln ('--');
// scaling and perspective
new_x *= scale;
new_y *= scale / (Math.PI/2 / alt); // alt == 90/2? perspective = scale/2
// translation
new_x += 180;
new_y += 150;
// z
new_y -= scale * scale * scale * z * Math.cos (alt);
return xform_cache[k] = new Point (new_x, new_y);
}
var pixel_cache = [];
function getpixel (i, x, y) {
var k = format ('%05d%05d', x, y);
if (pixel_cache[k]) { return pixel_cache[k]; }
return pixel_cache[k] = Math.avg (
i.sample (x, y, 0),
i.sample (x, y, 1),
i.sample (x, y, 2)
);
}
var src = ImageWindow.activeWindow.currentView.image;
var w = new ImageWindow (12*src.width, 12*src.height, 3, 8, false, true, '_3dplot');
var v = w.currentView;
var i = v.image;
// setup
var white_pen = new Pen (0xffffffff);
var black_pen = new Pen (0xff000000);
var red_pen = new Pen (0xffff0000);
var white_brush = new Brush (0xffffffff);
var black_brush = new Brush (0xff000000);
var red_brush = new Brush (0xffff0000);
var bmp = new Bitmap (i.width, i.height);
var g = new Graphics (bmp);
v.beginProcess (UndoFlag_PixelData);
bmp.fill (0xff008000);
g.pen = red_pen; g.brush = white_brush;
for (var n = 0; n < src.height-1; n++) {
for (var m = 0; m < src.width-1; m++) {
var p1 = xform_coords (m, n, getpixel (src, m, n ));
var p2 = xform_coords (m+1, n, getpixel (src, m+1, n ));
var p3 = xform_coords (m+1, n+1, getpixel (src, m+1, n+1));
var p4 = xform_coords (m, n+1, getpixel (src, m, n+1));
g.drawPolygon (new Array (p1, p2, p3, p4));
}
}
i.blend (bmp);
g.end();
v.endProcess();
w.show();
I didn't study in the university and this is the first time I try this kind of things, so I'd like to apologize to all those who read the code and take any offense at it .
No he ido a la universidad y es la primera vez que intento hacer esta clase de cosas, as? pues pido disculpas a aquellos que lean el c?digo y se vean profundamente ofendidos por ?l .