c# - Image warping in .NET -
i searching stretch warp algorithm apply in windows 8 store application. found android (java) code below. things can port c# android sdk specific things blurry me port .net code.
for example call : canvas.drawbitmapmesh , there .net counterpart this? matrix classes in .net bit different think can figure out think. tips on helping converting/porting code below .net welcome.
public class bitmapmesh extends graphicsactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(new sampleview(this)); } private static class sampleview extends view { private static final int width = 20; private static final int height = 20; private static final int count = (width + 1) * (height + 1); private final bitmap mbitmap; private final float[] mverts = new float[count*2]; private final float[] morig = new float[count*2]; private final matrix mmatrix = new matrix(); private final matrix minverse = new matrix(); private static void setxy(float[] array, int index, float x, float y) { array[index*2 + 0] = x; array[index*2 + 1] = y; } public sampleview(context context) { super(context); setfocusable(true); mbitmap = bitmapfactory.decoderesource(getresources(), r.drawable.beach); float w = mbitmap.getwidth(); float h = mbitmap.getheight(); // construct our mesh int index = 0; (int y = 0; y <= height; y++) { float fy = h * y / height; (int x = 0; x <= width; x++) { float fx = w * x / width; setxy(mverts, index, fx, fy); setxy(morig, index, fx, fy); index += 1; } } mmatrix.settranslate(10, 10); mmatrix.invert(minverse); } @override protected void ondraw(canvas canvas) { canvas.drawcolor(0xffcccccc); canvas.concat(mmatrix); canvas.drawbitmapmesh(mbitmap, width, height, mverts, 0, null, 0, null); } private void warp(float cx, float cy) { final float k = 10000; float[] src = morig; float[] dst = mverts; (int = 0; < count*2; += 2) { float x = src[i+0]; float y = src[i+1]; float dx = cx - x; float dy = cy - y; float dd = dx*dx + dy*dy; float d = floatmath.sqrt(dd); float pull = k / (dd + 0.000001f); pull /= (d + 0.000001f); // android.util.log.d("skia", "index " + + " dist=" + d + " pull=" + pull); if (pull >= 1) { dst[i+0] = cx; dst[i+1] = cy; } else { dst[i+0] = x + dx * pull; dst[i+1] = y + dy * pull; } } } private int mlastwarpx = -9999; // don't match touch coordinate private int mlastwarpy; @override public boolean ontouchevent(motionevent event) { float[] pt = { event.getx(), event.gety() }; minverse.mappoints(pt); int x = (int)pt[0]; int y = (int)pt[1]; if (mlastwarpx != x || mlastwarpy != y) { mlastwarpx = x; mlastwarpy = y; warp(pt[0], pt[1]); invalidate(); } return true; } } }
there no direct equivalent in windows store apps able draw texture mapped mesh (and represent image warping , distortion way). have use direct3d draw texture mapped mesh onto page.
if you're reluctant mix c++ , c# in app, have 2 choices.
- sharpdx - these .net bindings directx.
- monogame - xna-compliant, cross-platform graphics interface uses directx on winrt.
hope helps!
Comments
Post a Comment