Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
nroduit committed Dec 20, 2024
2 parents 72b180e + c381904 commit 3e1ece4
Show file tree
Hide file tree
Showing 59 changed files with 5,221 additions and 1,163 deletions.
2 changes: 1 addition & 1 deletion weasis-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

<properties>
<jxlayer.version>3.0.4</jxlayer.version>
<xchart.version>3.8.7</xchart.version>
<xchart.version>3.8.8</xchart.version>
<LGoodDatePicker.version>11.3.0-r1</LGoodDatePicker.version>
<lablib-checkboxtree.version>4.0</lablib-checkboxtree.version>
<VectorGraphics2D.version>0.13</VectorGraphics2D.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.awt.geom.Path2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.Arrays;
import java.util.List;
import org.weasis.core.util.MathUtil;

Expand All @@ -27,7 +28,7 @@ public static boolean isLineValid(Point2D ptA, Point2D ptB) {
}

/**
* @return angle between BA & BC line segment in Degree <br>
* @return angle between BA & BC line segment in Radiant<br>
* 0 is returned if any argument is invalid
*/
public static double getAngleRad(Point2D ptA, Point2D ptB, Point2D ptC) {
Expand All @@ -38,7 +39,7 @@ public static double getAngleRad(Point2D ptA, Point2D ptB, Point2D ptC) {
}

/**
* @return angle between BA & BC line segment in Radiant<br>
* @return angle between BA & BC line segment in Degree<br>
* 0 is returned if any argument is invalid
*/
public static double getAngleDeg(Point2D ptA, Point2D ptB, Point2D ptC) {
Expand Down Expand Up @@ -70,9 +71,26 @@ public static double getAngleDeg(Point2D ptA, Point2D ptB) {
return Math.toDegrees(getAngleRad(ptA, ptB));
}

/**
* Normalizes angle in the range of [-π, +π]
*
* @param angle in Radiant
* @return the normalized angle in radiant in the range of [-π, +π]
*/
public static double normalizeAngle(double angle) {
angle = (angle + Math.PI) % (2 * Math.PI) - Math.PI;
// Normalize angle to [-π, +π]
if (angle > Math.PI) {
angle -= 2 * Math.PI;
} else if (angle < -Math.PI) {
angle += 2 * Math.PI;
}
return angle;
}

/**
* @param angle in Radiant
* @return angle in the range of [ -pi ; pi ]
* @return angle in the range of [-π, +π]
*/
public static double getSmallestRotationAngleRad(double angle) {
double a = angle % (2 * Math.PI);
Expand All @@ -84,7 +102,7 @@ public static double getSmallestRotationAngleRad(double angle) {

/**
* @param angle in Degree
* @return angle in the range of [ -180 ; 180 ]
* @return angle in the range of [ -180, 180 ]
*/
public static double getSmallestRotationAngleDeg(double angle) {
double a = angle % 360.0;
Expand All @@ -96,7 +114,7 @@ public static double getSmallestRotationAngleDeg(double angle) {

/**
* @param angle in Radiant
* @return angle in the range of [ -pi ; pi ]
* @return angle in the range of [-π, +π]
*/
public static double getSmallestAngleRad(double angle) {
double a = angle % Math.PI;
Expand Down Expand Up @@ -272,6 +290,52 @@ public static Point2D getIntersectPoint(Line2D line, Rectangle2D rect) {
return p;
}

public static Line2D cropLine(Line2D line, Rectangle2D rect) {
if (line == null || rect == null || rect.isEmpty()) {
return line;
}

Point2D p1 =
lineIntersection(
line,
new Line2D.Double(rect.getMinX(), rect.getMinY(), rect.getMaxX(), rect.getMinY()));
Point2D p2 =
lineIntersection(
line,
new Line2D.Double(rect.getMinX(), rect.getMaxY(), rect.getMaxX(), rect.getMaxY()));
Point2D p3 =
lineIntersection(
line,
new Line2D.Double(rect.getMinX(), rect.getMinY(), rect.getMinX(), rect.getMaxY()));
Point2D p4 =
lineIntersection(
line,
new Line2D.Double(rect.getMaxX(), rect.getMinY(), rect.getMaxX(), rect.getMaxY()));

Point2D pl1 = null;
Point2D pl2 = null;

for (Point2D p : Arrays.asList(p1, p2, p3, p4)) {
if (p != null) {
if (pl1 == null) {
pl1 = p;
} else {
pl2 = p;
break;
}
}
}

if (pl1 != null && pl2 != null) {
if (line.getP1().distance(pl1) < line.getP1().distance(pl2)) {
return new Line2D.Double(pl1, pl2);
} else {
return new Line2D.Double(pl2, pl1);
}
}
return line;
}

private static Point2D lineIntersection(Line2D line1, Line2D line2) {
if (line1.intersectsLine(line2)) {
return GeomUtil.getIntersectPoint(line1, line2);
Expand Down
35 changes: 19 additions & 16 deletions weasis-core/src/main/java/org/weasis/core/api/image/cv/CvUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,28 +76,17 @@ public static VolatileImage getVolatileImage(GraphicsConfiguration gc, BufferedI

public static ImageCV meanStack(List<ImageElement> sources) {
if (sources.size() > 1) {
ImageElement firstImg = sources.get(0);
ImageElement firstImg = sources.getFirst();
PlanarImage img = firstImg.getImage(null, false);

Integer type = img.type();
;
int type = img.type();
Mat mean = new Mat(img.height(), img.width(), CvType.CV_32F);
img.toMat().convertTo(mean, CvType.CV_32F);
int numbSrc = sources.size();
for (int i = 1; i < numbSrc; i++) {
ImageElement imgElement = sources.get(i);
PlanarImage image = imgElement.getImage(null, false);
if (image instanceof Mat mat && image.width() == img.width()) {
// Accumulate not supported 16-bit signed:
// https://docs.opencv.org/3.3.0/d7/df3/group__imgproc__motion.html#ga1a567a79901513811ff3b9976923b199
if (CvType.depth(image.type()) == CvType.CV_16S) {
Mat floatImage = new Mat(img.height(), img.width(), CvType.CV_32F);
image.toMat().convertTo(floatImage, CvType.CV_32F);
Imgproc.accumulate(floatImage, mean);
} else {
Imgproc.accumulate(mat, mean);
}
}
accumulateFloatStack(image, img, mean);
}
ImageCV dstImg = new ImageCV();
Core.divide(mean, new Scalar(numbSrc), mean);
Expand All @@ -107,9 +96,23 @@ public static ImageCV meanStack(List<ImageElement> sources) {
return null;
}

public static void accumulateFloatStack(PlanarImage image, PlanarImage refImage, Mat floatStack) {
if (image instanceof Mat mat && image.width() == refImage.width()) {
// Accumulate not supported 16-bit signed:
// https://docs.opencv.org/3.3.0/d7/df3/group__imgproc__motion.html#ga1a567a79901513811ff3b9976923b199
if (CvType.depth(image.type()) == CvType.CV_16S) {
Mat floatImage = new Mat(refImage.height(), refImage.width(), CvType.CV_32F);
image.toMat().convertTo(floatImage, CvType.CV_32F);
Imgproc.accumulate(floatImage, floatStack);
} else {
Imgproc.accumulate(mat, floatStack);
}
}
}

public static ImageCV minStack(List<ImageElement> sources) {
if (sources.size() > 1) {
ImageElement firstImg = sources.get(0);
ImageElement firstImg = sources.getFirst();
ImageCV dstImg = new ImageCV();
PlanarImage img = firstImg.getImage(null, false);
img.toMat().copyTo(dstImg);
Expand All @@ -129,7 +132,7 @@ public static ImageCV minStack(List<ImageElement> sources) {

public static ImageCV maxStack(List<ImageElement> sources) {
if (sources.size() > 1) {
ImageElement firstImg = sources.get(0);
ImageElement firstImg = sources.getFirst();
ImageCV dstImg = new ImageCV();
PlanarImage img = firstImg.getImage(null, false);
img.toMat().copyTo(dstImg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@
import java.awt.image.DataBuffer;
import java.awt.image.IndexColorModel;
import java.awt.image.RenderedImage;
import java.net.URI;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.opencv.core.Core.MinMaxLocResult;
import org.opencv.core.CvType;
import org.slf4j.Logger;
Expand Down Expand Up @@ -371,19 +374,26 @@ public PlanarImage getImage(OpManager manager) {

@Override
public String toString() {
return getMediaURI().toString();
URI uri = getMediaURI();
if (uri == null) {
return "Memory image"; // NON-NLS
}
return uri.toString();
}

public synchronized PlanarImage getImage(OpManager manager, boolean findMinMax) {
try {
return getCacheImage(startImageLoading(), manager, findMinMax);
} catch (OutOfMemoryError e1) {
LOGGER.warn("Out of MemoryError: {}", this, e1);

mCache.expungeStaleEntries();
CvUtil.runGarbageCollectorAndWait(100);

return getCacheImage(startImageLoading(), manager, findMinMax);
try {
return getCacheImage(startImageLoading(), manager, findMinMax);
} catch (OutOfMemoryError e) {
LOGGER.warn("Reading image data: {}", this, e1);
}
return null;
}
}

Expand Down Expand Up @@ -426,8 +436,8 @@ private PlanarImage startImageLoading() throws OutOfMemoryError {
Future<PlanarImage> future = IMAGE_LOADER.submit(ref);
PlanarImage img = null;
try {
img = future.get();
} catch (InterruptedException e) {
img = future.get(45, TimeUnit.SECONDS);
} catch (InterruptedException | TimeoutException e) {
// Re-assert the thread's interrupted status
Thread.currentThread().interrupt();
// We don't need the result, so cancel the task too
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,15 @@ public interface Taggable extends TagReadable {
void setTag(TagW tag, Object value);

void setTagNoNull(TagW tag, Object value);

default void copyTags(TagW[] tagList, MediaElement media, boolean allowNullValue) {
if (tagList != null && media != null) {
for (TagW tag : tagList) {
Object value = media.getTagValue(tag);
if (allowNullValue || value != null) {
setTag(tag, value);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -343,15 +343,12 @@ public void mouseMoved(MouseEvent e) {
mouseEvt.setImageCoordinates(vImg.getImageCoordinatesFromMouse(e.getX(), e.getY()));

// Handle special case when drawing in mode [click > release > move/drag > release] instead of
// [click + drag >
// release]
// [click + drag > release]
if (ds instanceof DefaultDragSequence) {
ds.drag(mouseEvt);
} else {

Cursor newCursor = cursorSet.getDrawingCursor();
Cursor newCursor = null;
GraphicModel graphicList = vImg.getGraphicManager();

if (!mouseEvt.isShiftDown()) {
// Evaluates if mouse is on a dragging position, and changes cursor image consequently
Optional<Graphic> firstGraphicIntersecting =
Expand All @@ -366,8 +363,9 @@ public void mouseMoved(MouseEvent e) {
newCursor = getCursor(mouseEvt, selectedDragGraphList, dragGraph, cursorSet);
}
}
vImg.getJComponent()
.setCursor(Optional.ofNullable(newCursor).orElse(cursorSet.getDrawingCursor()));
if (newCursor != null) {
vImg.getJComponent().setCursor(newCursor);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.awt.GridBagConstraints;
import java.awt.Rectangle;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.EnumMap;
import java.util.Map;
Expand Down Expand Up @@ -80,6 +81,8 @@ public record ImageProperties(
protected String name;
protected boolean useGlobalPreferences;

private final Map<Position, Point2D> positions = new EnumMap<>(Position.class);

protected AbstractInfoLayer(ViewCanvas<E> view2DPane) {
this(view2DPane, true);
}
Expand All @@ -89,6 +92,23 @@ protected AbstractInfoLayer(ViewCanvas<E> view2DPane, boolean useGlobalPreferenc
this.pixelInfoBound = new Rectangle();
this.preloadingProgressBound = new Rectangle();
this.useGlobalPreferences = useGlobalPreferences;
positions.put(Position.TopLeft, new Point2D.Double(0, 0));
positions.put(Position.TopRight, new Point2D.Double(0, 0));
positions.put(Position.BottomLeft, new Point2D.Double(0, 0));
positions.put(Position.BottomRight, new Point2D.Double(0, 0));
}

@Override
public Point2D getPosition(Position position) {
return positions.get(position);
}

@Override
public void setPosition(Position position, double x, double y) {
Point2D p = positions.get(position);
if (p != null) {
p.setLocation(x, y);
}
}

public ViewCanvas<E> getView2DPane() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,18 @@

import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Point2D;
import org.weasis.core.ui.editor.image.PixelInfo;
import org.weasis.core.ui.editor.image.ViewCanvas;

public interface LayerAnnotation extends Layer {
enum Position {
TopLeft,
TopRight,
BottomLeft,
BottomRight
}

boolean getDisplayPreferences(LayerItem item);

boolean setDisplayPreferencesValue(LayerItem displayItem, boolean selected);
Expand All @@ -40,4 +48,8 @@ public interface LayerAnnotation extends Layer {
void setShowBottomScale(Boolean showBottomScale);

void resetToDefault();

Point2D getPosition(Position position);

void setPosition(Position position, double x, double y);
}
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ public void setVolumePreset(Preset preset) {
List<PresetWindowLevel> list = getVolTexture().getPresetList(true, volumePreset, originalLUT);
if (originalLUT) {
if (!list.isEmpty()) {
changePresetWindowLevel(list.get(0));
changePresetWindowLevel(list.getFirst());
}
} else {
list.stream()
Expand Down
Loading

0 comments on commit 3e1ece4

Please sign in to comment.