This repository has been archived by the owner on Dec 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ApplyDisplaySettingsCommand.java
87 lines (67 loc) · 3.84 KB
/
ApplyDisplaySettingsCommand.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package ch.epfl.biop.qupath.commands;
import javafx.collections.ObservableList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import qupath.lib.display.ChannelDisplayInfo;
import qupath.lib.display.DirectServerChannelInfo;
import qupath.lib.display.ImageDisplay;
import qupath.lib.gui.QuPathGUI;
import qupath.lib.gui.dialogs.Dialogs;
import qupath.lib.gui.scripting.QPEx;
import qupath.lib.images.ImageData;
import qupath.lib.images.servers.ImageServer;
import qupath.lib.projects.ProjectImageEntry;
import java.awt.image.BufferedImage;
import java.util.List;
import java.util.stream.Collectors;
public class ApplyDisplaySettingsCommand implements Runnable {
final static Logger logger = LoggerFactory.getLogger(ImageDisplay.class);
private static String title = "Apply current display settings to project";
private QuPathGUI qupath;
//ApplyDisplaySettingsCommand
public ApplyDisplaySettingsCommand(final QuPathGUI qupath) {
if (!Dialogs.showConfirmDialog("Apply Brightness And Contrast", "Apply current display settings to all images?\n\nWill apply on images with the same image type and number of channels."))
return;
this.qupath = qupath;
}
public void run() {
ImageData currentImageData = qupath.getImageData();
currentImageData.removeProperty("qupath.lib.display.ImageDisplay");
ImageServer currentServer = currentImageData.getServer();
ObservableList<ChannelDisplayInfo> currentChannels = qupath.getViewer().getImageDisplay().availableChannels();
// Careful with channel names that have (C1), (C2), etc...
List<String> channel_names = currentChannels.stream().map(c -> {
if (c instanceof DirectServerChannelInfo)
return currentServer.getChannel(((DirectServerChannelInfo) c).getChannel()).getName();
else
return (c.getName());
}
).collect(Collectors.toList());
List<Float> channel_min = currentChannels.stream().map(c -> c.getMinDisplay()).collect(Collectors.toList());
List<Float> channel_max = currentChannels.stream().map(c -> c.getMaxDisplay()).collect(Collectors.toList());
List<Integer> channel_colors = currentChannels.stream().map(c -> c.getColor()).collect(Collectors.toList());
// Get all images from Project
List<ProjectImageEntry<BufferedImage>> imageList = qupath.getProject().getImageList();
imageList.stream().forEach(entry -> {
ImageData<BufferedImage> imageData = null;
try {
imageData = entry.readImageData();
ImageServer server = entry.getServerBuilder().build();
if (imageData == null) imageData = qupath.createNewImageData(server, true);
logger.info("Ref Type: {} vs Current Type {}", currentImageData.getImageType(), imageData.getImageType());
logger.info("Ref nC: {} vs Current nC {}", currentServer.getMetadata().getSizeC(), server.getMetadata().getSizeC());
if (currentImageData.getImageType().equals(imageData.getImageType()) && currentServer.getMetadata().getSizeC() == server.getMetadata().getSizeC()) {
QPEx.setChannelColors(imageData, channel_colors.toArray(new Integer[0]));
QPEx.setChannelNames(imageData, channel_names.toArray(new String[0]));
for (int i = 0; i < channel_min.size(); i++) {
QPEx.setChannelDisplayRange(imageData, channel_names.get(i), channel_min.get(i), channel_max.get(i));
}
logger.info("Saving Display Settings for Image {}", entry.getImageName());
entry.saveImageData(imageData);
}
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
});
}
}