Skip to content

Commit

Permalink
Fixed ImageServices null behavior.
Browse files Browse the repository at this point in the history
Fixed #521 Make services like fit() in ImageServices robust against null inputs.
  • Loading branch information
ylussaud committed Apr 17, 2024
1 parent eb9fc40 commit fc407fc
Show file tree
Hide file tree
Showing 49 changed files with 234 additions and 58 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2016, 2023 Obeo.
* Copyright (c) 2016, 2024 Obeo.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -80,8 +80,16 @@ public ImageServices(URIConverter uriConverter, URI templateURI) {
)
// @formatter:on
public MImage asImage(String uriStr) {
final URI imageURI = URI.createURI(uriStr, true);
return asImage(uriStr, PictureType.toType(imageURI));
final MImage res;

if (uriStr != null) {
final URI imageURI = URI.createURI(uriStr, true);
res = asImage(uriStr, PictureType.toType(imageURI));
} else {
res = null;
}

return res;
}

// @formatter:off
Expand All @@ -97,7 +105,15 @@ public MImage asImage(String uriStr) {
)
// @formatter:on
public MImage asImage(String uriStr, String type) {
return asImage(uriStr, PictureType.valueOf(type.toUpperCase()));
final MImage res;

if (uriStr != null && type != null) {
res = asImage(uriStr, PictureType.valueOf(type.toUpperCase()));
} else {
res = null;
}

return res;
}

/**
Expand All @@ -110,10 +126,17 @@ public MImage asImage(String uriStr, String type) {
* @return the {@link MImage} corresponding to the given path
*/
private MImage asImage(String uriStr, PictureType type) {
final URI imageURI = URI.createURI(uriStr, true);
final URI uri = imageURI.resolve(templateURI);
final MImage res;

return new MImageImpl(uriConverter, uri, type);
if (uriStr != null && type != null) {
final URI imageURI = URI.createURI(uriStr, true);
final URI uri = imageURI.resolve(templateURI);
res = new MImageImpl(uriConverter, uri, type);
} else {
res = null;
}

return res;
}

// @formatter:off
Expand All @@ -122,14 +145,22 @@ private MImage asImage(String uriStr, PictureType type) {
params = {
@Param(name = "image", value = "The Image"),
},
result = "gets the width of the image",
result = "gets the width of the image, -1 if the image is null",
examples = {
@Example(expression = "myImage.getWidth()", result = "300"),
}
)
// @formatter:on
public Integer getWidth(MImage image) {
return image.getWidth();
final Integer res;

if (image != null) {
res = image.getWidth();
} else {
res = -1;
}

return res;
}

// @formatter:off
Expand All @@ -146,7 +177,9 @@ public Integer getWidth(MImage image) {
)
// @formatter:on
public MImage setWidth(MImage image, Integer width) {
image.setWidth(width);
if (image != null && width != null) {
image.setWidth(width);
}

return image;
}
Expand All @@ -157,14 +190,22 @@ public MImage setWidth(MImage image, Integer width) {
params = {
@Param(name = "image", value = "The Image"),
},
result = "gets the height of the image",
result = "gets the height of the image, -1 if the image is null",
examples = {
@Example(expression = "myImage.getHeight()", result = "300"),
}
)
// @formatter:on
public Integer getHeight(MImage image) {
return image.getHeight();
final Integer res;

if (image != null) {
res = image.getHeight();
} else {
res = -1;
}

return res;
}

// @formatter:off
Expand All @@ -181,7 +222,9 @@ public Integer getHeight(MImage image) {
)
// @formatter:on
public MImage setHeight(MImage image, Integer height) {
image.setHeight(height);
if (image != null && height != null) {
image.setHeight(height);
}

return image;
}
Expand All @@ -200,7 +243,9 @@ public MImage setHeight(MImage image, Integer height) {
)
// @formatter:on
public MImage setConserveRatio(MImage image, Boolean conserve) {
image.setConserveRatio(conserve);
if (image != null && conserve != null) {
image.setConserveRatio(conserve);
}

return image;
}
Expand All @@ -220,12 +265,20 @@ public MImage setConserveRatio(MImage image, Boolean conserve) {
)
// @formatter:on
public MImage fit(MImage image, Integer width, Integer height) {
image.setWidth(width);
if (!image.conserveRatio() || image.getHeight() > height) {
image.setHeight(height);
final MImage res;

if (image != null && width != null && height != null) {
image.setWidth(width);
if (!image.conserveRatio() || image.getHeight() > height) {
image.setHeight(height);
}

res = fit(image, width, height, true);
} else {
res = null;
}

return fit(image, width, height, true);
return res;
}

// @formatter:off
Expand All @@ -244,21 +297,23 @@ public MImage fit(MImage image, Integer width, Integer height) {
)
// @formatter:on
public MImage fit(MImage image, Integer width, Integer height, boolean zoomIn) {
if (zoomIn) {
image.setWidth(width);
if (!image.conserveRatio() || image.getHeight() > height) {
image.setHeight(height);
}
} else {
if (image.getWidth() > width) {
if (image != null && width != null && height != null) {
if (zoomIn) {
image.setWidth(width);
if (image.getHeight() > height) {
if (!image.conserveRatio() || image.getHeight() > height) {
image.setHeight(height);
}
} else if (image.getHeight() > height) {
image.setHeight(height);
} else {
if (image.getWidth() > width) {
image.setWidth(width);
if (image.getHeight() > height) {
image.setHeight(height);
}
} else if (image.getHeight() > height) {
image.setHeight(height);
if (image.getWidth() > width) {
image.setWidth(width);
}
}
}
}
Expand All @@ -280,16 +335,25 @@ public MImage fit(MImage image, Integer width, Integer height, boolean zoomIn) {
)
// @formatter:on
public MImage resize(MImage image, Double factor) throws IOException {
final BufferedImage bufferedImage = MImageAWTImpl.getBufferedImage(image);
final MImage res;

if (image != null && factor != null) {
final BufferedImage bufferedImage = MImageAWTImpl.getBufferedImage(image);

final BufferedImage resized = new BufferedImage((int) (bufferedImage.getWidth() * factor),
(int) (bufferedImage.getHeight() * factor), bufferedImage.getType());

final BufferedImage resized = new BufferedImage((int) (bufferedImage.getWidth() * factor),
(int) (bufferedImage.getHeight() * factor), bufferedImage.getType());
final AffineTransform zoomTransfort = AffineTransform.getScaleInstance(factor, factor);
final AffineTransformOp retaillerImage = new AffineTransformOp(zoomTransfort,
AffineTransformOp.TYPE_BILINEAR);
retaillerImage.filter(bufferedImage, resized);

final AffineTransform zoomTransfort = AffineTransform.getScaleInstance(factor, factor);
final AffineTransformOp retaillerImage = new AffineTransformOp(zoomTransfort, AffineTransformOp.TYPE_BILINEAR);
retaillerImage.filter(bufferedImage, resized);
res = new MImageAWTImpl(resized, image.getURI());
} else {
res = null;
}

return new MImageAWTImpl(resized, image.getURI());
return res;
}

// @formatter:off
Expand All @@ -306,29 +370,37 @@ public MImage resize(MImage image, Double factor) throws IOException {
)
// @formatter:on
public MImage rotate(MImage image, Integer angle) throws IOException {
final BufferedImage bufferedImage = MImageAWTImpl.getBufferedImage(image);

final double rads = Math.toRadians(angle);
final double sin = Math.abs(Math.sin(rads));
final double cos = Math.abs(Math.cos(rads));
final int width = bufferedImage.getWidth();
final int height = bufferedImage.getHeight();
final int x = width / 2;
final int y = height / 2;
final int newWidth = (int) Math.floor(width * cos + height * sin);
final int newHeight = (int) Math.floor(height * cos + width * sin);

final BufferedImage rotated = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB);
final AffineTransform translateTransform = new AffineTransform();
translateTransform.translate((newWidth - width) / 2, (newHeight - height) / 2);
translateTransform.rotate(rads, x, y);

final Graphics2D g2d = rotated.createGraphics();
g2d.setTransform(translateTransform);
g2d.drawImage(bufferedImage, 0, 0, null);
g2d.dispose();

return new MImageAWTImpl(rotated, image.getURI());
final MImage res;

if (image != null && angle != null) {
final BufferedImage bufferedImage = MImageAWTImpl.getBufferedImage(image);

final double rads = Math.toRadians(angle);
final double sin = Math.abs(Math.sin(rads));
final double cos = Math.abs(Math.cos(rads));
final int width = bufferedImage.getWidth();
final int height = bufferedImage.getHeight();
final int x = width / 2;
final int y = height / 2;
final int newWidth = (int) Math.floor(width * cos + height * sin);
final int newHeight = (int) Math.floor(height * cos + width * sin);

final BufferedImage rotated = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB);
final AffineTransform translateTransform = new AffineTransform();
translateTransform.translate((newWidth - width) / 2, (newHeight - height) / 2);
translateTransform.rotate(rads, x, y);

final Graphics2D g2d = rotated.createGraphics();
g2d.setTransform(translateTransform);
g2d.drawImage(bufferedImage, 0, 0, null);
g2d.dispose();

res = new MImageAWTImpl(rotated, image.getURI());
} else {
res = null;
}

return res;
}

// @formatter:off
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

=== HEADER ===

=== BODY ===

Checks ImageServices registration :
[query: .asImage(null)]
End of demonstration.
=== FOOTER ===

=== TEMPLATES ===
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="ASCII"?>
<genconf:Generation xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:genconf="http://www.obeonetwork.org/m2doc/genconf/1.0" name="asImage" templateFileName="asImage-template.docx" resultFileName="asImage-actual-generation.docx" validationFileName="asImage-actual-validation.docx"/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

=== HEADER ===

=== BODY ===

Checks ImageServices registration :
[query: .fitAll(null, null, null, true)]
End of demonstration.
=== FOOTER ===

=== TEMPLATES ===
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="ASCII"?>
<genconf:Generation xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:genconf="http://www.obeonetwork.org/m2doc/genconf/1.0" name="fitHigh" templateFileName="fitHigh-template.docx" resultFileName="fitHigh-actual-generation.docx" validationFileName="fitHigh-actual-validation.docx"/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

=== HEADER ===

=== BODY ===

Checks ImageServices registration :
[query: .fit(null, null, null)]
End of demonstration.
=== FOOTER ===

=== TEMPLATES ===
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="ASCII"?>
<genconf:Generation xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:genconf="http://www.obeonetwork.org/m2doc/genconf/1.0" name="fitHigh" templateFileName="fitHigh-template.docx" resultFileName="fitHigh-actual-generation.docx" validationFileName="fitHigh-actual-validation.docx"/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

=== HEADER ===

=== BODY ===

Checks ImageServices registration :
[query: .resize(null, null)]
End of demonstration.
=== FOOTER ===

=== TEMPLATES ===
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="ASCII"?>
<genconf:Generation xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:genconf="http://www.obeonetwork.org/m2doc/genconf/1.0" name="resize" templateFileName="resize-template.docx" resultFileName="resize-actual-generation.docx" validationFileName="resize-actual-validation.docx"/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

=== HEADER ===

=== BODY ===

Checks ImageServices registration :
[query: .rotate(null, null)]
End of demonstration.
=== FOOTER ===

=== TEMPLATES ===
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="ASCII"?>
<genconf:Generation xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:genconf="http://www.obeonetwork.org/m2doc/genconf/1.0" name="rotate" templateFileName="rotate-template.docx" resultFileName="rotate-actual-generation.docx" validationFileName="rotate-actual-validation.docx"/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

=== HEADER ===

=== BODY ===

Checks ImageServices registration :
[query: .setConserveRatio(null, true)]
End of demonstration.
=== FOOTER ===

=== TEMPLATES ===
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="ASCII"?>
<genconf:Generation xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:genconf="http://www.obeonetwork.org/m2doc/genconf/1.0" name="setConserveRatioTrueInvalidImageFormat" templateFileName="setConserveRatioTrueInvalidImageFormat-template.docx" resultFileName="setConserveRatioTrueInvalidImageFormat-actual-generation.docx" validationFileName="setConserveRatioTrueInvalidImageFormat-actual-validation.docx"/>
Loading

0 comments on commit fc407fc

Please sign in to comment.