App
CurvesLUTLayer.java
Go to the documentation of this file.
1 package mhr.appcore.image.layers.lut;
2 
3 import org.w3c.dom.Document;
4 import org.w3c.dom.Element;
5 
6 import mhr.appcore.bitmap.NBitmap;
7 import mhr.appcore.bitmap.exceptions.BitmapAllocationException;
8 import mhr.appcore.bitmap.exceptions.UnsupportedBitmapException;
9 import mhr.appcore.exceptions.AlreadyDisposedException;
10 import mhr.appcore.image.Image;
11 import mhr.appcore.image.exceptions.InvalidLayerSpecificPresentationSuppliedException;
12 import mhr.appcore.image.exceptions.LayerCreationException;
13 import mhr.appcore.image.layers.Layer;
14 import mhr.appcore.image.layers.LayerPDInfo;
15 import mhr.appcore.image.layers.LayerType;
16 import mhr.appcore.interfaces.ImageFile;
17 import mhr.appcore.pointops.LUT;
18 import mhr.appcore.pointops.LUTAllocationException;
19 import mhr.appcore.utils.NotImplementedException;
20 import mhr.appcore.utils.Rect;
21 
25 public class CurvesLUTLayer extends LUTLayer {
26 
27  //===== INTERFACES, CLASSES, ENUMS ==========================================================================================================//
28  //----- NON-PUBLIC --------------------------------------------------------------------------------------------------------------------------//
29  //----- PUBLIC ------------------------------------------------------------------------------------------------------------------------------//
30 
31  //===== FIELDS ==============================================================================================================================//
32  //----- NON-PUBLIC --------------------------------------------------------------------------------------------------------------------------//
33  public float[][] M;
34  public float[][] R;
35  public float[][] G;
36  public float[][] B;
37  public float[][] A;
38 
39  //----- PUBLIC ------------------------------------------------------------------------------------------------------------------------------//
40 
41  //===== CONSTRUCTORS, DESTRUCTORS, RELATED METHODS ==========================================================================================//
42  //----- NON-PUBLIC --------------------------------------------------------------------------------------------------------------------------//
43  @Override
44  protected void finalize() throws Throwable {
45  dispose();
46  super.finalize();
47  }
48 
49  protected CurvesLUTLayer(CurvesLUTLayer l, int newId) {
50  super(l, newId);
51  this.M = new float[2][l.M[0].length];
52  this.R = new float[2][l.R[0].length];
53  this.G = new float[2][l.G[0].length];
54  this.B = new float[2][l.B[0].length];
55  this.A = new float[2][l.A[0].length];
56 
57  for (int i = 0; i < 2; i++) {
58  System.arraycopy(l.M[i], 0, this.M[i], 0, l.M[i].length);
59  System.arraycopy(l.R[i], 0, this.R[i], 0, l.R[i].length);
60  System.arraycopy(l.G[i], 0, this.G[i], 0, l.G[i].length);
61  System.arraycopy(l.B[i], 0, this.B[i], 0, l.B[i].length);
62  System.arraycopy(l.A[i], 0, this.A[i], 0, l.A[i].length);
63  }
64 
65  lut = LUT.getCurvesLut(M, R, G, B, A);
66  }
67 
68  //----- PUBLIC ------------------------------------------------------------------------------------------------------------------------------//
78  super(owner, id);
79  float[][] defVals = new float[][]{
80  {0.0f, 1.0f},
81  {0.0f, 1.0f}
82  };
83  M = defVals; R = defVals; G = defVals; B = defVals; A = defVals;
84  lut = LUT.getCurvesLut(M, R, G, B, A);
85  }
86 
87  public CurvesLUTLayer(Image owner, Element i, ImageFile f) {
88  super(owner, i, f);
89  try {
90  M = new float[2][];
91  M[0] = extractFrom(i.getElementsByTagName("MX").item(0).getTextContent());
92  M[1] = extractFrom(i.getElementsByTagName("MY").item(0).getTextContent());
93 
94  R = new float[2][];
95  R[0] = extractFrom(i.getElementsByTagName("RX").item(0).getTextContent());
96  R[1] = extractFrom(i.getElementsByTagName("RY").item(0).getTextContent());
97 
98  G = new float[2][];
99  G[0] = extractFrom(i.getElementsByTagName("GX").item(0).getTextContent());
100  G[1] = extractFrom(i.getElementsByTagName("GY").item(0).getTextContent());
101 
102  B = new float[2][];
103  B[0] = extractFrom(i.getElementsByTagName("BX").item(0).getTextContent());
104  B[1] = extractFrom(i.getElementsByTagName("BY").item(0).getTextContent());
105 
106  A = new float[2][];
107  A[0] = extractFrom(i.getElementsByTagName("AX").item(0).getTextContent());
108  A[1] = extractFrom(i.getElementsByTagName("AY").item(0).getTextContent());
109  } catch (Exception e) {
110  throw new LayerCreationException(e);
111  }
112  lut = LUT.getCurvesLut(M, R, G, B, A);
113  }
114 
115  @Override
116  public synchronized int dispose() {
117  super.dispose();
118  return 0;
119  }
120 
121  //===== METHODS =============================================================================================================================//
122  @Override
123  public Layer duplicate(int newId) {
124  return new CurvesLUTLayer(this, newId);
125  }
126 
127 
128 
129  protected void fillWith(StringBuilder b, float[] arr) {
130  b.setLength(0);
131  b.append(arr[0]);
132  for (int i = 1; i < arr.length; i++) {
133  b.append(' ');
134  b.append(arr[i]);
135  }
136  }
137 
138  protected float[] extractFrom(String s) {
139  String[] vals = s.split(" ");
140  float[] r = new float[vals.length];
141  for (int i = 0; i < vals.length; i++) {
142  r[i] = Float.parseFloat(vals[i]);
143  }
144  return r;
145  }
146  //----- NON-PUBLIC --------------------------------------------------------------------------------------------------------------------------//
147  protected void fillSpecificElement(Document d, ImageFile f, Element e) {
148  StringBuilder b = new StringBuilder();
149 
150  fillWith(b, M[0]);
151  appendLeafElement(d, e, "MX", b.toString());
152  fillWith(b, M[1]);
153  appendLeafElement(d, e, "MY", b.toString());
154 
155  fillWith(b, R[0]);
156  appendLeafElement(d, e, "RX", b.toString());
157  fillWith(b, R[1]);
158  appendLeafElement(d, e, "RY", b.toString());
159 
160  fillWith(b, G[0]);
161  appendLeafElement(d, e, "GX", b.toString());
162  fillWith(b, G[1]);
163  appendLeafElement(d, e, "GY", b.toString());
164 
165  fillWith(b, B[0]);
166  appendLeafElement(d, e, "BX", b.toString());
167  fillWith(b, B[1]);
168  appendLeafElement(d, e, "BY", b.toString());
169 
170  fillWith(b, A[0]);
171  appendLeafElement(d, e, "AX", b.toString());
172  fillWith(b, A[1]);
173  appendLeafElement(d, e, "AY", b.toString());
174  }
175 
176  //----- PUBLIC ------------------------------------------------------------------------------------------------------------------------------//
177  @Override
178  public LayerType getType() {
179  return LayerType.LAYER_CURVES;
180  }
181 
182 
183 //----- Pole --------------------------------------------------------------------------------------------------------------------------------//
184 
185 //----- Accessory a primitivní metody -------------------------------------------------------------------------------------------------------//
186 
187 //----- Pomocné metody ----------------------------------------------------------------------------------------------------------------------//
188 //----- Konstruktory ------------------------------------------------------------------------------------------------------------------------//
189 
190 //----- Destruktory -------------------------------------------------------------------------------------------------------------------------//
191 
192 //----- Metody ------------------------------------------------------------------------------------------------------------------------------//
193 
194  @Override
195  protected void updateSpecificData(LayerPDInfo info) {
197  info.extra = p;
198  }
199 
200  @Override
201  public void updateFromSpecificPresentation(LayerPDInfo presentation) {
202  if (!(presentation.extra instanceof CurvesLUTLayerSpecificPresentation)) {
203  throw new InvalidLayerSpecificPresentationSuppliedException("Invalid specific data supplied");
204  }
205  CurvesLUTLayerSpecificPresentation p = (CurvesLUTLayerSpecificPresentation) presentation.extra;
206  M = p.M;
207  R = p.R;
208  G = p.G;
209  B = p.B;
210  A = p.A;
211  if (lut != null) {
212  lut.dispose();
213  }
214  lut = LUT.getCurvesLut(M, R, G, B, A);
215 
216  presentation.isChanged = true;
217  }
218 }