App
Transform.java
Go to the documentation of this file.
1 package mhr.appcore.transform;
2 
3 import mhr.appcore.bitmap.BitmapInfo;
4 import mhr.appcore.bitmap.ChannelCount;
5 import mhr.appcore.bitmap.NBitmap;
6 import mhr.appcore.bitmap.NativeType;
7 import mhr.appcore.interpolators.InterpolatorType;
8 
9 public class Transform {
10 
11  //===== INTERFACES, CLASSES, ENUMS ==========================================================================================================//
12  //----- NON-PUBLIC --------------------------------------------------------------------------------------------------------------------------//
13  //----- PUBLIC ------------------------------------------------------------------------------------------------------------------------------//
14 
15  //===== FIELDS ==============================================================================================================================//
16  //----- NON-PUBLIC --------------------------------------------------------------------------------------------------------------------------//
17  //----- PUBLIC ------------------------------------------------------------------------------------------------------------------------------//
18 
19  //===== CONSTRUCTORS, DESTRUCTORS, RELATED METHODS ==========================================================================================//
20  //----- NON-PUBLIC --------------------------------------------------------------------------------------------------------------------------//
21  protected Transform() {
22  }
23  //----- PUBLIC ------------------------------------------------------------------------------------------------------------------------------//
24 
25 
26  //===== METHODS =============================================================================================================================//
27  //----- NON-PUBLIC --------------------------------------------------------------------------------------------------------------------------//
28  protected static native int applyMatrixTo(int nativeType, long dstAdress, long srcAdress, int interpolatorType,
29  double a11, double a12, double a13,
30  double a21, double a22, double a23,
31  double a31, double a32, double a33);
32  //----- PUBLIC ------------------------------------------------------------------------------------------------------------------------------//
33 
34  public static int applyMatrixTo(NBitmap dst, NBitmap src, TM m, InterpolatorType i) {
35  NativeType type = src.getNativeType();
36  if (type == dst.getNativeType() && type != NativeType.INVALID_TYPE) {
37  return applyMatrixTo(src.getNativeType().getValue(), dst.getAdress(), src.getAdress(), i.getValue(),
38  m.a11, m.a12, m.a13,
39  m.a21, m.a22, m.a23,
40  m.a31, m.a32, m.a33);
41  } else {
42  throw new IllegalArgumentException("Operation is not applicable to suplied NBitmaps.");
43  }
44  }
45 
46  //===== CALLBACKS ===========================================================================================================================//
47 
48 
49 
50 
51  public static TM getTranslationMatrix(double dx, double dy) {
52  return new TM( 1, 0, 0,
53  0, 1, 0,
54  dx, dy, 1);
55  }
56 
57  public static TM getRotationMatrix(double alpha) {
58  return new TM( Math.cos(alpha), Math.sin(alpha), 0,
59  -Math.sin(alpha), Math.cos(alpha), 0,
60  0, 0, 1);
61  }
62 
63  public static TM getScaleMatrix(double x, double y) {
64  return new TM( x, 0, 0,
65  0, y, 0,
66  0, 0, 1);
67  }
68 
69  public static TM getShearMatrix(double x, double y) {
70  return new TM( 1, y, 0,
71  x, 1, 0,
72  0, 0, 1);
73  }
74 
75  public static TM getProjectiveMatrix(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) {
76  TM m = new TM();
77  m.a13 = ((x1 - x2 + x4 - x3) * (y3 - y4) - (y1 - y2 + y4 - y3) * (x3 - x4)) / ((x2 - x4) * (y3 - y4) - (x3 - x4) * (y2 - y4));
78  m.a23 = ((y1 - y2 + y4 - y3) * (x2 - x4) - (x1 - x2 + x4 - x3) * (y2 - y4)) / ((x2 - x4) * (y3 - y4) - (x3 - x4) * (y2 - y4));
79 
80  m.a11 = x2 - x1 + m.a13 * x2;
81  m.a21 = x3 - x1 + m.a23 * x3;
82  m.a31 = x1;
83 
84  m.a12 = y2 - y1 + m.a13 * y2;
85  m.a22 = y3 - y1 + m.a23 * y3;
86  m.a32 = y1;
87 
88  m.a33 = 1;
89 
90  return m;
91 
92 // TM m = new TM();
93 // m.a31 = ((x1 - x2 + x4 - x3) * (y3 - y4) - (y1 - y2 + y4 - y3) * (x3 - x4)) / ((x2 - x4) * (y3 - y4) - (x3 - x4) * (y2 - y4));
94 // m.a32 = ((y1 - y2 + y4 - y3) * (x2 - x4) - (x1 - x2 + x4 - x3) * (y2 - y4)) / ((x2 - x4) * (y3 - y4) - (x3 - x4) * (y2 - y4));
95 //
96 // m.a11 = x2 - x1 + m.a31 * x2;
97 // m.a12 = x3 - x1 + m.a32 * x3;
98 // m.a13 = x1;
99 //
100 // m.a21 = y2 - y1 + m.a31 * y2;
101 // m.a22 = y3 - y1 + m.a32 * y3;
102 // m.a23 = y1;
103 //
104 // m.a33 = 1;
105 //
106 // return TM.transposeOf(m);
107  }
108 }