App
RasterLayer.java
Go to the documentation of this file.
1 package mhr.appcore.image.layers;
2 
3 import org.w3c.dom.Document;
4 import org.w3c.dom.Element;
5 
6 import mhr.appcore.bitmap.BitmapInfo;
7 import mhr.appcore.bitmap.ChannelCount;
8 import mhr.appcore.bitmap.ColorMode;
9 import mhr.appcore.bitmap.NBitmap;
10 import mhr.appcore.bitmap.exceptions.BitmapAllocationException;
11 import mhr.appcore.bitmap.exceptions.UnsupportedBitmapException;
12 import mhr.appcore.blending.BlendMode;
13 import mhr.appcore.blending.Blender;
14 import mhr.appcore.exceptions.AlreadyDisposedException;
15 import mhr.appcore.image.Image;
16 import mhr.appcore.image.exceptions.InvalidLayerSpecificPresentationSuppliedException;
17 import mhr.appcore.image.exceptions.LayerCreationException;
18 import mhr.appcore.image.layers.Layer.FileConstants;
19 import mhr.appcore.image.layers.lut.ContrastLUTLayer;
20 import mhr.appcore.interfaces.ImageFile;
21 import mhr.appcore.interfaces.PDBitmap;
22 import mhr.appcore.interpolators.Interpolator;
23 import mhr.appcore.interpolators.InterpolatorType;
24 import mhr.appcore.utils.Rect;
25 
29 public class RasterLayer extends Layer {
30 
31  //===== INTERFACES, CLASSES, ENUMS ==========================================================================================================//
32  //----- NON-PUBLIC --------------------------------------------------------------------------------------------------------------------------//
33  //----- PUBLIC ------------------------------------------------------------------------------------------------------------------------------//
34 
35  //===== FIELDS ==============================================================================================================================//
36  //----- NON-PUBLIC --------------------------------------------------------------------------------------------------------------------------//
37  protected BlendMode mode;
38  protected NBitmap bitmap = null;
39  //----- PUBLIC ------------------------------------------------------------------------------------------------------------------------------//
40 
41  //===== CONSTRUCTORS, DESTRUCTORS, RELATED METHODS ==========================================================================================//
42 
43  //----- NON-PUBLIC --------------------------------------------------------------------------------------------------------------------------//
44  @Override
45  protected void finalize() throws Throwable {
46  dispose();
47  super.finalize();
48  }
49 
50  protected RasterLayer(RasterLayer l, int newId) {
51  super(l, newId);
52  this.mode = l.mode;
53  this.bitmap = new NBitmap(l.bitmap);
54  }
55 
56  //----- PUBLIC ------------------------------------------------------------------------------------------------------------------------------//
65  super(owner, id);
68  try {
69  bitmap = new NBitmap(info); // Háže vyjímky
70  } catch (UnsupportedBitmapException e) {
71  throw new LayerCreationException(e);
72  }
73  }
74 
75  public RasterLayer(Image owner, Element i, ImageFile f) {
76  super(owner, i, f);
77  mode = BlendMode.valueOf(i.getElementsByTagName("BlendMode").item(0).getTextContent());
78 
80  try {
81  bitmap = new NBitmap(info); // Háže vyjímky
82  } catch (UnsupportedBitmapException e) {
83  throw new LayerCreationException(e);
84  }
85  PDBitmap tmpBitmap = null;
86  NBitmap tmp = null;
87  try {
88  tmpBitmap = f.loadBitmap(Integer.toString(id));
89  tmp = new NBitmap(tmpBitmap);
90  tmp.copyTo(bitmap, bitmap.getRect(), 0, 0);
91  tmp.dispose();
92  tmpBitmap.dispose();
93  } catch (Exception e) {
94  dispose();
95  if (tmp != null) {
96  tmp.dispose();
97  }
98  if (tmpBitmap != null) {
99  tmpBitmap.dispose();
100  }
101  throw new LayerCreationException(e);
102  }
103  }
104 
114  public RasterLayer(Image owner, int id, NBitmap from, boolean crop) throws LayerCreationException, BitmapAllocationException {
115  super(owner, id);
117  BitmapInfo i = from.getInfo();
119  throw new LayerCreationException("Supplied bitmap is not supported");
120  }
121  bitmap = new NBitmap(i);
122  from.copyTo(bitmap, from.getRect(), 0, 0);
123  }
124 
125  @Override
126  public synchronized int dispose() {
127  super.dispose();
128  if (bitmap != null) {
129  bitmap.dispose();
130  bitmap = null;
131  }
132  return 0;
133  }
134 
135 
136 
137  //===== METHODS =============================================================================================================================//
138  //----- NON-PUBLIC --------------------------------------------------------------------------------------------------------------------------//
139  protected void fillSpecificElement(Document d, ImageFile f, Element e) {
140  appendLeafElement(d, e, "BlendMode", mode.toString());
141  if (bitmap != null) {
143  NBitmap tmpWrap = new NBitmap(tmpBitmap);
144  bitmap.copyTo(tmpWrap, bitmap.getRect(), 0, 0);
145  tmpWrap.premultiply();
146  tmpWrap.dispose();
147  f.writeBitmap(tmpBitmap, Integer.toString(id));
148  tmpBitmap.dispose();
149  }
150  }
151 
152  @Override
153  protected void onCrop(Rect r) {
154  bitmap.cropTo(r);
155  }
156 
157  @Override
158  protected void onResize(int nWidth, int nHeight, InterpolatorType iType, double force) {
159  if (bitmap != null) {
160  NBitmap tmp = new NBitmap(new BitmapInfo(nWidth, nHeight, ChannelCount.FOUR_CHANNEL, depth, ColorMode.RGBA, false));
161  Interpolator.resampleTo(bitmap, tmp, iType, force);
162  bitmap.dispose();
163  bitmap = tmp;
164  }
165  }
166 
167  //----- PUBLIC ------------------------------------------------------------------------------------------------------------------------------//
168 
169  @Override
170  public Layer duplicate(int newId) {
171  return new RasterLayer(this, newId);
172  }
173 
174  @Override
175  public LayerType getType() {
176  return LayerType.LAYER_RASTER;
177  }
178 
183  public BlendMode getMode() {
184  return mode;
185  }
186 
191  public void setMode(BlendMode mode) {
192  this.mode = mode;
193  }
194 
199  public NBitmap getBitmap() {
200  return bitmap;
201  }
202 
203 
204 
205 //----- Pole --------------------------------------------------------------------------------------------------------------------------------//
206 
207 //----- Accessory a primitivní metody -------------------------------------------------------------------------------------------------------//
208 
209 //----- Pomocné metody ----------------------------------------------------------------------------------------------------------------------//
210 //----- Konstruktory ------------------------------------------------------------------------------------------------------------------------//
211 
212 //----- Destruktory -------------------------------------------------------------------------------------------------------------------------//
213 
214 //----- Metody ------------------------------------------------------------------------------------------------------------------------------//
215  @Override
216  public int applyTo(NBitmap dst) {
217  if (!visible) {
218  return 0;
219  }
220  if (bitmap == null) {
221  throw new AlreadyDisposedException("Layer is already disposed");
222  }
223  if (opacity < opacityAsNone) {
224  return 0;
225  }
226  if (isMaskActive()) {
227  if (opacity > opacityAsFull) {
228  return Blender.drawTo(mode, dst, mask, bitmap, mask.getRect(), 0, 0, 0, 0);
229  } else {
230  return Blender.drawTo(mode, dst, mask, bitmap, mask.getRect(), 0, 0, 0, 0, opacity);
231  }
232  } else {
233  if (opacity > opacityAsFull) {
234  return Blender.blendTo(mode, dst, bitmap, bitmap.getRect(), 0, 0);
235  } else {
236  return Blender.blendTo(mode, dst, bitmap, bitmap.getRect(), 0, 0, opacity);
237  }
238  }
239  }
240 
241  @Override
242  public int applyTo(NBitmap dst, Rect srcRect, int srcOrigX, int srcOrigY) {
243  if (!visible) {
244  return 0;
245  }
246  if (bitmap == null) {
247  throw new AlreadyDisposedException("Layer is already disposed");
248  }
249  if (opacity < opacityAsNone) {
250  return 0;
251  }
252  if (isMaskActive()) {
253  if (opacity > opacityAsFull) {
254  return Blender.drawTo(mode, dst, mask, bitmap, srcRect, 0, 0, srcOrigX, srcOrigY);
255  } else {
256  return Blender.drawTo(mode, dst, mask, bitmap, srcRect, 0, 0, srcOrigX, srcOrigY, opacity);
257  }
258  } else {
259  if (opacity > opacityAsFull) {
260  return Blender.blendTo(mode, dst, bitmap, srcRect, srcOrigX, srcOrigY);
261  } else {
262  return Blender.blendTo(mode, dst, bitmap, srcRect, srcOrigX, srcOrigY, opacity);
263  }
264  }
265  }
266 
267  @Override
268  public int applyTo(NBitmap dst, Rect srcRect, int srcOrigX, int srcOrigY, NBitmap forcedMask) {
269  if (!visible) {
270  return 0;
271  }
272  if (bitmap == null) {
273  throw new AlreadyDisposedException("Layer is already disposed");
274  }
275  if (opacity < opacityAsNone) {
276  return 0;
277  }
278  if (isMaskActive()) {
279  if (opacity > opacityAsFull) {
280  return Blender.drawTo(mode, dst, forcedMask, bitmap, srcRect, 0, 0, srcOrigX, srcOrigY);
281  } else {
282  return Blender.drawTo(mode, dst, forcedMask, bitmap, srcRect, 0, 0, srcOrigX, srcOrigY, opacity);
283  }
284  } else {
285  if (opacity > opacityAsFull) {
286  return Blender.blendTo(mode, dst, bitmap, srcRect, srcOrigX, srcOrigY);
287  } else {
288  return Blender.blendTo(mode, dst, bitmap, srcRect, srcOrigX, srcOrigY, opacity);
289  }
290  }
291  }
292 
293  @Override
294  protected void updateSpecificData(LayerPDInfo info) {
295  if (bitmap == null) {
296  throw new AlreadyDisposedException("Layer is already disposed");
297  }
298  NBitmap thumbWrap = new NBitmap(info.getLayerThumb(true));
299  Interpolator.resampleTo(bitmap, thumbWrap);
300  thumbWrap.setPremultiplied(false);
301  thumbWrap.premultiply();
302  thumbWrap.dispose();
304  p.mode = mode;
305  info.extra = p;
306  }
307 
308  @Override
309  public void updateFromSpecificPresentation(LayerPDInfo presentation) {
310  if (!(presentation.extra instanceof RasterLayerSpecificPresentation)) {
311  throw new InvalidLayerSpecificPresentationSuppliedException("Invalid specific data supplied");
312  }
313  mode = ((RasterLayerSpecificPresentation)presentation.extra).mode;
314  presentation.isChanged = true;
315  }
316 }