1 package mhr.appcore.image;
3 import java.lang.reflect.Constructor;
4 import java.lang.reflect.InvocationTargetException;
5 import java.util.ArrayList;
6 import java.util.LinkedList;
8 import org.w3c.dom.Document;
9 import org.w3c.dom.Element;
10 import org.w3c.dom.NodeList;
12 import mhr.appcore.bitmap.BitmapInfo;
13 import mhr.appcore.bitmap.ChannelCount;
14 import mhr.appcore.bitmap.ColorMode;
15 import mhr.appcore.bitmap.Depth;
16 import mhr.appcore.bitmap.NBitmap;
17 import mhr.appcore.bitmap.exceptions.BitmapAllocationException;
18 import mhr.appcore.bitmap.exceptions.UnsupportedBitmapException;
19 import mhr.appcore.blending.BlendMode;
20 import mhr.appcore.blending.Blender;
21 import mhr.appcore.exceptions.AllocationException;
22 import mhr.appcore.tools.Tool;
23 import mhr.appcore.image.exceptions.ImageException;
24 import mhr.appcore.image.exceptions.ImageLoadingException;
25 import mhr.appcore.image.exceptions.ImageSavingException;
26 import mhr.appcore.image.exceptions.ImageStateException;
27 import mhr.appcore.image.exceptions.LayerCreationException;
28 import mhr.appcore.image.exceptions.LayerNotFoundException;
29 import mhr.appcore.image.layers.ColorLayer;
30 import mhr.appcore.image.layers.Layer;
31 import mhr.appcore.image.layers.LayerPDInfo;
32 import mhr.appcore.image.layers.LayerType;
33 import mhr.appcore.image.layers.RasterLayer;
34 import mhr.appcore.image.layers.filter.DifferenceOfGaussianSharpenFilterLayer;
35 import mhr.appcore.image.layers.filter.GaussianBlurFilterLayer;
36 import mhr.appcore.image.layers.filter.GaussianSharpenFilterLayer;
37 import mhr.appcore.image.layers.filter.LaplaceSharpenFilterLayer;
38 import mhr.appcore.image.layers.filter.LaplacianOfGaussianSharpenFilterLayer;
39 import mhr.appcore.image.layers.lut.BrightnessLUTLayer;
40 import mhr.appcore.image.layers.lut.ContrastLUTLayer;
41 import mhr.appcore.image.layers.lut.CurvesLUTLayer;
42 import mhr.appcore.image.layers.lut.GammaLUTLayer;
43 import mhr.appcore.interfaces.ImageFile;
44 import mhr.appcore.interfaces.PDBitmap;
45 import mhr.appcore.interfaces.PDImageDataPresentation;
46 import mhr.appcore.interpolators.Interpolator;
47 import mhr.appcore.interpolators.InterpolatorType;
48 import mhr.appcore.utils.Rect;
163 }
catch (ClassCastException e) {
178 case SELECTED_MASTER_MASK:
186 if (target == null ) {
217 protected ArrayList<Layer>
layers =
new ArrayList<Layer>();
302 r = (Element) d.getElementsByTagName(c.
ROOT_NODE).item(0);
303 ii = (Element) r.getElementsByTagName(c.
INFO_NODE).item(0);
305 width = Integer.parseInt(ii.getElementsByTagName(c.
WIDTH_NODE).item(0).getTextContent());
306 height = Integer.parseInt(ii.getElementsByTagName(c.
HEIGHT_NODE).item(0).getTextContent());
308 nextId = Integer.parseInt(ii.getElementsByTagName(c.
NEXT_ID_NODE).item(0).getTextContent());
312 }
catch (ClassCastException e) {
314 }
catch (NullPointerException e) {
329 }
catch (Exception e) {
333 if (tmpMask != null) {
345 NodeList ll = il.getElementsByTagName(c.
LAYER_NODE);
347 int iMax = ll.getLength();
356 for (
int i = 0; i < iMax; i++) {
357 li = (Element) ll.item(i);
446 case LAYER_BRIGHTNESS:
454 case LAYER_GAUSSIAN_BLUR:
456 case LAYER_LAPLACE_SHARPEN:
458 case LAYER_GAUSSIAN_SHARPEN:
460 case LAYER_LAPLACIAN_OF_GAUSSIAN_SHARPEN:
462 case LAYER_DIFFERENCE_OF_GAUSSIAN_SHARPEN:
481 Constructor<? extends Layer> ctor = cls.getConstructor(
new Class[] {
Image.class,
int.class });
482 return ctor.newInstance(
new Object[] {
this,
assignId() });
483 }
catch (NoSuchMethodException exc) {
484 throw new ImageException(
"Layer not created, reflection failed due to: " + exc.toString() +
" " + exc.getMessage());
485 }
catch (IllegalAccessException exc) {
486 throw new ImageException(
"Layer not created, reflection failed due to: " + exc.toString() +
" " + exc.getMessage());
487 }
catch (IllegalArgumentException exc) {
488 throw new ImageException(
"Layer not created, reflection failed due to: " + exc.toString() +
" " + exc.getMessage());
489 }
catch (InstantiationException exc) {
490 throw new ImageException(
"Layer not created, reflection failed due to: " + exc.toString() +
" " + exc.getMessage());
491 }
catch (InvocationTargetException exc) {
492 throw new ImageException(
"Layer not created, reflection failed due to: " + exc.toString() +
" " + exc.getMessage());
493 }
catch (ExceptionInInitializerError exc) {
494 throw new ImageException(
"Layer not created, reflection failed due to: " + exc.toString() +
" " + exc.getMessage());
508 Constructor<? extends Layer> ctor = cls.getConstructor(
new Class[] {
Image.class, Element.class,
ImageFile.class });
509 return ctor.newInstance(
new Object[] {
this, info, file});
510 }
catch (NoSuchMethodException exc) {
511 throw new ImageException(
"Layer not created, reflection failed due to: " + exc.toString() +
" " + exc.getMessage());
512 }
catch (IllegalAccessException exc) {
513 throw new ImageException(
"Layer not created, reflection failed due to: " + exc.toString() +
" " + exc.getMessage());
514 }
catch (IllegalArgumentException exc) {
515 throw new ImageException(
"Layer not created, reflection failed due to: " + exc.toString() +
" " + exc.getMessage());
516 }
catch (InstantiationException exc) {
517 throw new ImageException(
"Layer not created, reflection failed due to: " + exc.toString() +
" " + exc.getMessage());
518 }
catch (InvocationTargetException exc) {
519 throw new ImageException(
"Layer not created, reflection failed due to: " + exc.toString() +
" " + exc.getMessage());
520 }
catch (ExceptionInInitializerError exc) {
521 throw new ImageException(
"Layer not created, reflection failed due to: " + exc.toString() +
" " + exc.getMessage());
533 Element e = d.createElement(eName);
534 e.setTextContent(eContetnt);
535 parent.appendChild(e);
550 l.applyTo(dst,
getRect(), 0, 0);
553 layers =
new ArrayList<Layer>();
584 }
catch (ClassCastException e) {
599 case SELECTED_MASTER_MASK:
607 if (target == null ) {
623 }
catch (IllegalArgumentException e) {
636 synchronized (
pres) {
675 case SELECTED_MASTER_MASK:
683 if (target == null ) {
695 }
catch (IllegalArgumentException e) {
708 synchronized (
pres) {
747 case SELECTED_MASTER_MASK:
755 if (target == null ) {
785 }
catch (IllegalArgumentException e) {
798 synchronized (
pres) {
831 }
catch (ClassCastException e) {
846 case SELECTED_MASTER_MASK:
854 if (source == null ) {
891 }
catch (ClassCastException e) {
906 case SELECTED_MASTER_MASK:
914 if (target == null ) {
940 }
catch (IllegalArgumentException e) {
953 synchronized (
pres) {
985 }
catch (ClassCastException e) {
1000 case SELECTED_MASTER_MASK:
1008 if (target == null ) {
1028 }
catch (IllegalArgumentException e) {
1033 if (
patch != null) {
1041 synchronized (
pres) {
1067 case SELECTED_LAYER:
1073 }
catch (ClassCastException e) {
1088 case SELECTED_MASTER_MASK:
1096 if (target == null ) {
1116 }
catch (IllegalArgumentException e) {
1121 if (
patch != null) {
1129 synchronized (
pres) {
1155 case SELECTED_LAYER:
1161 }
catch (ClassCastException e) {
1169 case SELECTED_MASTER_MASK:
1176 if (target == null ) {
1188 }
catch (IllegalArgumentException e) {
1193 if (
patch != null) {
1201 synchronized (
pres) {
1244 l.resizeTo(nWidth, nHeight, iType, force);
1250 if (
patch != null) {
1263 synchronized (
pres) {
1268 for (
int i = 0; i < layers.size(); i++) {
1269 Layer l = layers.get(i);
1305 if (
patch != null) {
1318 synchronized (
pres) {
1323 for (
int i = 0; i < layers.size(); i++) {
1324 Layer l = layers.get(i);
1363 }
catch (Exception exc) {
1369 l.fillLayerElement(d, f, e);
1372 }
catch (Exception exc) {
1379 tmpWrap =
new NBitmap(tmpBitmap);
1384 }
catch (Exception exc) {
1385 if (tmpWrap != null) {
1388 if (tmpBitmap != null) {
1395 }
catch (Exception exc) {
1404 if (
patch != null) {
1409 synchronized (
pres) {
1474 synchronized (
pres) {
1485 for (
int i = 0; i <
layers.size(); i++) {
1524 case SELECTED_LAYER: {
1529 case SELECTED_MASK: {
1535 case SELECTED_MASTER_MASK:
1543 synchronized (
pres) {
1560 if (l.getId() == id) {
1572 tmp = l.getExtraMargin();
1573 extra = Math.max(extra, tmp);
1654 synchronized (
pres) {
1673 overLayerWithId =
layers.get(
layers.size() - 1).getId();
1678 synchronized (
pres) {
1694 synchronized (
pres) {
1712 if (
layers.size() == 1) {
1722 int oldIndex =
layers.indexOf(l);
1734 synchronized (
pres) {
1752 int oldIndex =
layers.indexOf(l);
1753 int newIndex = (beforeLayerWithId < 0) ?
layers.size() :
layers.indexOf(
getLayer(beforeLayerWithId));
1755 if (oldIndex < newIndex) {
1760 synchronized (
pres) {
1775 synchronized (
pres) {
1793 throw new IllegalStateException(
"Canvas is already pending.");
1826 throw new IllegalStateException(
"Tool is not canvas owner");
1838 throw new IllegalStateException(
"Canvas is not prepared.");
1857 throw new IllegalStateException(
"Canvas is not prepared.");
1861 if (
patch != null) {
1866 case SELECTED_LAYER: {
1874 case SELECTED_MASK: {
1881 case SELECTED_MASTER_MASK: {
1894 synchronized (
pres) {
1923 srcOrigX = tmpRect.
tlx;
1924 srcOrigY = tmpRect.
tly;
1929 dst.
fill(tmpRect, 0xFF000000);
1936 case SELECTED_LAYER:
1944 case SELECTED_MASTER_MASK:
1945 l.applyTo(dst, tmpRect, srcOrigX, srcOrigY);
1950 l.applyTo(dst, tmpRect, srcOrigX, srcOrigY);
1953 l.applyTo(dst, tmpRect, srcOrigX, srcOrigY);
1971 throw new IllegalArgumentException(
"Target bitmap is of incompatible type");
1991 long incrementalArea;
1994 if (master == null) {
1997 incrementalArea = master.
getArea();
1998 masterPreview =
new Rect(master);
2000 incrementalArea += r.
getArea();
2001 masterPreview.
cover(r);
2002 if (incrementalArea < masterPreview.
getArea()) {