commit a15631bc6b987db7392577cdfda8ad158d4fb8d3
Author: leszek <leszek@koltunski.pl>
Date:   Fri May 19 10:17:15 2017 +0100

    Progress with Magnify.

diff --git a/src/main/java/org/distorted/library/Distorted.java b/src/main/java/org/distorted/library/Distorted.java
index bc8ecdc..8259083 100644
--- a/src/main/java/org/distorted/library/Distorted.java
+++ b/src/main/java/org/distorted/library/Distorted.java
@@ -34,7 +34,7 @@ public class Distorted
   static int GLSL;
   static String GLSL_VERSION;
 
-  static float mMagnify = 1.0f;
+  static int mMargin = 0;
 
   /**
    * When creating an instance of a DistortedTexture from another instance, clone the Bitmap that's
@@ -156,8 +156,8 @@ public class Distorted
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-  public static void setMagnify(float m)
+  public static void setMargin(int pixels)
     {
-    mMagnify = m;
+    mMargin = pixels;
     }
   }
\ No newline at end of file
diff --git a/src/main/java/org/distorted/library/EffectQueueMatrix.java b/src/main/java/org/distorted/library/EffectQueueMatrix.java
index d3ef63c..305d5bf 100644
--- a/src/main/java/org/distorted/library/EffectQueueMatrix.java
+++ b/src/main/java/org/distorted/library/EffectQueueMatrix.java
@@ -105,12 +105,75 @@ class EffectQueueMatrix extends EffectQueue
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-  private void magnify(float halfX, float halfY, float halfZ, float[] projection)
+  private void magnify(float halfX, float halfY, float halfZ, DistortedOutputSurface projection)
     {
-    float scale = Distorted.mMagnify;
+    float scale;
+    float[] result= new float[4];
     float[] point = new float[4];
+    float[] matrix= new float[16];
+    double d1, d2, d3, d4, d5, d6, d7, d8;
+    float nx, ny;
+    float w = projection.mWidth/2;
+    float h = projection.mHeight/2;
+    double sum = 0.0;
     point[3] = 0.0f;
 
+    Matrix.multiplyMM(matrix, 0, projection.mProjectionMatrix, 0, mViewMatrix, 0);
+
+    point[0] = +halfX; point[1] = +halfY; point[2] = +halfZ;
+    Matrix.multiplyMV(result,0,matrix,0,point,0);
+    nx = w*result[0]/result[3];
+    ny = h*result[1]/result[3];
+    d1 = Math.sqrt(nx*nx+ny*ny);
+
+    point[0] = +halfX; point[1] = +halfY; point[2] = -halfZ;
+    Matrix.multiplyMV(result,0,matrix,0,point,0);
+    nx = w*result[0]/result[3];
+    ny = h*result[1]/result[3];
+    d2 = Math.sqrt(nx*nx+ny*ny);
+
+    point[0] = +halfX; point[1] = -halfY; point[2] = +halfZ;
+    Matrix.multiplyMV(result,0,matrix,0,point,0);
+    nx = w*result[0]/result[3];
+    ny = h*result[1]/result[3];
+    d3 = Math.sqrt(nx*nx+ny*ny);
+
+    point[0] = +halfX; point[1] = -halfY; point[2] = -halfZ;
+    Matrix.multiplyMV(result,0,matrix,0,point,0);
+    nx = w*result[0]/result[3];
+    ny = h*result[1]/result[3];
+    d4 = Math.sqrt(nx*nx+ny*ny);
+
+    point[0] = -halfX; point[1] = +halfY; point[2] = +halfZ;
+    Matrix.multiplyMV(result,0,matrix,0,point,0);
+    nx = w*result[0]/result[3];
+    ny = h*result[1]/result[3];
+    d5 = Math.sqrt(nx*nx+ny*ny);
+
+    point[0] = -halfX; point[1] = +halfY; point[2] = -halfZ;
+    Matrix.multiplyMV(result,0,matrix,0,point,0);
+    nx = w*result[0]/result[3];
+    ny = h*result[1]/result[3];
+    d6 = Math.sqrt(nx*nx+ny*ny);
+
+    point[0] = -halfX; point[1] = -halfY; point[2] = +halfZ;
+    Matrix.multiplyMV(result,0,matrix,0,point,0);
+    nx = w*result[0]/result[3];
+    ny = h*result[1]/result[3];
+    d7 = Math.sqrt(nx*nx+ny*ny);
+
+    point[0] = -halfX; point[1] = -halfY; point[2] = -halfZ;
+    Matrix.multiplyMV(result,0,matrix,0,point,0);
+    nx = w*result[0]/result[3];
+    ny = h*result[1]/result[3];
+    d8 = Math.sqrt(nx*nx+ny*ny);
+
+    sum = (d1+d2+d3+d4+d5+d6+d7+d8)/8;
+
+    scale = 1.0f + (float)(Distorted.mMargin/sum);
+
+    android.util.Log.d("scale", "scale="+scale+" sum="+sum+" "+d1+" "+d2+" "+d3+" "+d4+" "+d5+" "+d6+" "+d7+" "+d8);
+
     Matrix.scaleM(mViewMatrix, 0, scale, scale, scale);
     }
 
@@ -198,7 +261,7 @@ class EffectQueueMatrix extends EffectQueue
 
     Matrix.translateM(mViewMatrix, 0, halfX,-halfY,-halfZ);
 
-    if( Distorted.mMagnify!=1.0f ) magnify(halfX,halfY,halfZ, projection.mProjectionMatrix);
+    if( Distorted.mMargin!=0 ) magnify(halfX,halfY,halfZ,projection);
 
     Matrix.multiplyMM(mMVPMatrix, 0, projection.mProjectionMatrix, 0, mViewMatrix, 0);
     }
