use of ini.trakem2.display.Displayable in project TrakEM2 by trakem2.
the class DBLoader method getProjectThing.
private ProjectThing getProjectThing(ResultSet r, Project project, HashMap<String, TemplateThing> hs_tt, HashMap<Long, Displayable> hs_d) throws Exception {
long id = r.getLong("id");
String type = r.getString("type");
TemplateThing tt = (TemplateThing) hs_tt.get(type);
if (null == tt) {
Utils.log("Loader.getProjectThing: can not find a proper TemplateThing of type " + type + " for the ProjectThing of id=" + id);
return null;
}
long object_id = r.getLong("object_id");
// may be null
Object ob = r.getString("title");
if (-1 != object_id) {
ob = getProjectObject(project, object_id);
if (ob instanceof Displayable)
hs_d.put(new Long(((Displayable) ob).getId()), (Displayable) ob);
else
Utils.log("Loader.getProjectThing: not adding to hs_d: " + ob);
}
return new ProjectThing(tt, project, id, ob, getChildrenProjectThings(project, id, type, hs_tt, hs_d));
}
use of ini.trakem2.display.Displayable in project TrakEM2 by trakem2.
the class DBLoader method updateInDatabase.
/**
* The ImagePlus, if updated, is saved in the 'tiff_working' column always.
*/
private void updateInDatabase(Patch patch, String key) throws Exception {
if (key.equals("tiff_snapshot")) {
/* // DEPRECATED, now using mipmaps
InputStream i_stream = null;
try {
ImagePlus imp = new ImagePlus("s", snaps.get(patch.getId())); // not calling fetchSnapshot because old code could end in a loop.
if (null == imp) {
Utils.log2("DBLoader: snapshot ImagePlus is null!");
stmt_update_snap.setNull(1, java.sql.Types.BINARY);
} else {
i_stream = createZippedStream(imp);
stmt_update_snap.setBinaryStream(1, i_stream, i_stream.available());
flush(imp);
}
stmt_update_snap.setLong(2, patch.getId());
stmt_update_snap.executeUpdate();
} catch (Exception e) {
IJError.print(e);
} finally {
if (null != i_stream) try { i_stream.close(); } catch (Exception e1) { IJError.print(e1); }
}
*/
return;
}
StringBuffer sb = new StringBuffer("UPDATE ab_patches SET ");
boolean update_imp = false;
if (key.equals("tiff_working")) {
sb.append("imp_type=").append(patch.getType()).append(", tiff_working=?");
update_imp = true;
} else if (key.equals("remove_tiff_working")) {
sb.append("tiff_working=NULL");
} else if (key.equals("min_and_max")) {
sb.append("min=").append(patch.getMin()).append(", max=").append(patch.getMax());
} else {
// try the Displayable level
updateInDatabase((Displayable) patch, key);
return;
}
PreparedStatement st = connection.prepareStatement(sb.append(" WHERE id=").append(patch.getId()).toString());
int i = 1;
InputStream i_stream2 = null;
try {
if (update_imp) {
// WARNING if the cache is very small relative to the size of the images, this strategy may fail
ImagePlus imp = mawts.get(patch.getId());
i_stream2 = createZippedStream(imp);
st.setBinaryStream(i, i_stream2, i_stream2.available());
// defensive programming: if later I add any other ..
i++;
}
st.executeUpdate();
if (null != i_stream2)
i_stream2.close();
} catch (Exception e) {
IJError.print(e);
if (null != i_stream2)
try {
i_stream2.close();
} catch (Exception e2) {
IJError.print(e2);
}
}
}
use of ini.trakem2.display.Displayable in project TrakEM2 by trakem2.
the class DBLoader method getRootLayerThing.
/**
* Fetches the root LayerSet, fills it with children (recursively) and uses the profiles, pipes, etc., from the project_thing. Will reconnect the links and open Displays for the layers that have one.
*/
public LayerThing getRootLayerThing(Project project, ProjectThing project_thing, TemplateThing layer_set_tt, TemplateThing layer_tt) {
synchronized (db_lock) {
// connect if disconnected
if (!connectToDatabase()) {
return null;
}
HashMap hs_pt = new HashMap();
unpack(project_thing, hs_pt);
LayerThing root = null;
try {
// -1 signals root
ResultSet r = connection.prepareStatement("SELECT * FROM ab_things WHERE project_id=" + project.getId() + " AND type='layer_set' AND parent_id=-1").executeQuery();
if (r.next()) {
root = getLayerThing(r, project, hs_pt, layer_set_tt, layer_tt);
}
r.close();
if (null == root) {
Utils.log("Loader.getRootLayerThing: can't find it for project id=" + project.getId());
return null;
}
// Redo the links! hs_pt contains now all Displayable objects.
ResultSet rl = connection.prepareStatement("SELECT * FROM ab_links WHERE project_id=" + project.getId()).executeQuery();
while (rl.next()) {
Long id1 = new Long(rl.getLong("id1"));
Long id2 = new Long(rl.getLong("id2"));
Object ob1 = hs_pt.get(id1);
Object ob2 = hs_pt.get(id2);
if (null != ob1 && null != ob2) {
Displayable d = (Displayable) ob1;
d.link((Displayable) ob2, false);
} else {
Utils.log("Loader: broken link between " + id1 + " and " + id2);
}
}
rl.close();
} catch (Exception e) {
IJError.print(e);
return null;
}
return root;
}
}
use of ini.trakem2.display.Displayable in project TrakEM2 by trakem2.
the class Blending method blendPatches.
public static final void blendPatches(final Set<Patch> patches, final boolean respect_current_mask) {
ExecutorService exe = null;
try {
if (null == patches || patches.size() < 2)
return;
final Layer layer = patches.iterator().next().getLayer();
for (final Patch p : patches) {
if (null != p.getCoordinateTransform()) {
Utils.log("CANNOT blend: at least one image has a coordinate transform.\nBlending of coordinate-transformed images will be enabled in the near future.");
return;
}
if (p.getLayer() != layer) {
Utils.log("CANNOT blend: all images must belong to the same layer!\n Otherwise the overlap cannot be computed.");
return;
}
}
final HashMap<Patch, TransformMesh> meshes = new HashMap<Patch, TransformMesh>();
for (final Patch p : patches) {
meshes.put(p, null == p.getCoordinateTransform() ? null : new TransformMesh(p.getCoordinateTransform(), p.getMeshResolution(), p.getOWidth(), p.getOHeight()));
}
exe = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
final List<Future<?>> futures = Collections.synchronizedList(new ArrayList<Future<?>>());
final List<Future<?>> futures2 = Collections.synchronizedList(new ArrayList<Future<?>>());
// Cache the indices that determine overlap order within the layer
final HashMap<Patch, Integer> indices = new HashMap<Patch, Integer>();
int i = 0;
for (final Displayable d : layer.getDisplayables()) {
if (d.getClass() == Patch.class && patches.contains((Patch) d)) {
indices.put((Patch) d, i);
}
i += 1;
}
for (final Patch p : patches) {
if (Thread.currentThread().isInterrupted())
break;
futures.add(exe.submit(new Runnable() {
@Override
public void run() {
final int pLayerIndex = indices.get(p);
final Set<Patch> overlapping = new HashSet<Patch>();
for (final Patch op : patches) {
if (indices.get(op) < pLayerIndex)
overlapping.add(op);
}
if (setBlendingMask(p, overlapping, meshes, respect_current_mask)) {
futures2.add(p.updateMipMaps());
}
}
}, null));
}
// join all:
Utils.waitIfAlive(futures, false);
Utils.waitIfAlive(futures2, false);
} catch (final Exception e) {
IJError.print(e);
} finally {
if (null != exe)
exe.shutdown();
Display.repaint();
}
}
use of ini.trakem2.display.Displayable in project TrakEM2 by trakem2.
the class ContrastEnhancerWrapper method apply.
public boolean apply(final Collection<Displayable> patches_) {
if (null == patches_)
return false;
// Create appropriate patch list
ArrayList<Patch> patches = new ArrayList<Patch>();
for (final Displayable d : patches_) {
if (d.getClass() == Patch.class)
patches.add((Patch) d);
}
if (0 == patches.size())
return false;
// Check that all images are of the same size and type
Patch firstp = (Patch) patches.get(0);
final int ptype = firstp.getType();
final double pw = firstp.getOWidth();
final double ph = firstp.getOHeight();
for (final Patch p : patches) {
if (p.getType() != ptype) {
// can't continue
Utils.log("Can't homogenize histograms: images are not all of the same type.\nFirst offending image is: " + p);
return false;
}
if (!equalize && 0 == stats_mode && p.getOWidth() != pw || p.getOHeight() != ph) {
Utils.log("Can't homogenize histograms: images are not all of the same size.\nFirst offending image is: " + p);
return false;
}
}
try {
if (equalize) {
for (final Patch p : patches) {
if (Thread.currentThread().isInterrupted())
return false;
p.appendFilters(new IFilter[] { new EqualizeHistogram() });
/*
p.getProject().getLoader().releaseToFit(p.getOWidth(), p.getOHeight(), p.getType(), 3);
ImageProcessor ip = p.getImageProcessor().duplicate(); // a throw-away copy
if (this.from_existing_min_and_max) {
ip.setMinAndMax(p.getMin(), p.getMax());
}
ce.equalize(ip);
p.setMinAndMax(ip.getMin(), ip.getMax());
*/
// submit for regeneration
p.getProject().getLoader().decacheImagePlus(p.getId());
regenerateMipMaps(p);
}
return true;
}
// Else, call stretchHistogram with an appropriate stats object
final ImageStatistics stats;
if (1 == stats_mode) {
// use each image independent stats
stats = null;
} else if (0 == stats_mode) {
// use stack statistics
final ArrayList<Patch> sub = new ArrayList<Patch>();
if (use_full_stack) {
sub.addAll(patches);
} else {
// build stack statistics, ordered by stdDev
final SortedMap<Stats, Patch> sp = Collections.synchronizedSortedMap(new TreeMap<Stats, Patch>());
Process.progressive(patches, new TaskFactory<Patch, Stats>() {
public Stats process(final Patch p) {
if (Thread.currentThread().isInterrupted())
return null;
ImagePlus imp = p.getImagePlus();
p.getProject().getLoader().releaseToFit(p.getOWidth(), p.getOHeight(), p.getType(), 2);
Stats s = new Stats(imp.getStatistics());
sp.put(s, p);
return s;
}
});
if (Thread.currentThread().isInterrupted())
return false;
final ArrayList<Patch> a = new ArrayList<Patch>(sp.values());
final int count = a.size();
if (count < 3) {
sub.addAll(a);
} else if (3 == count) {
// the middle one
sub.add(a.get(1));
} else if (4 == count) {
sub.addAll(a.subList(1, 3));
} else if (count > 4) {
int first = (int) (count / 4.0 + 0.5);
int last = (int) (count / 4.0 * 3 + 0.5);
sub.addAll(a.subList(first, last));
}
}
stats = new StackStatistics(new PatchStack(sub.toArray(new Patch[sub.size()]), 1));
} else {
stats = reference_stats;
}
final Calibration cal = patches.get(0).getLayer().getParent().getCalibrationCopy();
Process.progressive(patches, new TaskFactory<Patch, Object>() {
public Object process(final Patch p) {
if (Thread.currentThread().isInterrupted())
return null;
p.getProject().getLoader().releaseToFit(p.getOWidth(), p.getOHeight(), p.getType(), 3);
// a throw-away copy
ImageProcessor ip = p.getImageProcessor().duplicate();
if (ContrastEnhancerWrapper.this.from_existing_min_and_max) {
ip.setMinAndMax(p.getMin(), p.getMax());
}
ImageStatistics st = stats;
if (null == stats) {
Utils.log2("Null stats, using image's self");
st = ImageStatistics.getStatistics(ip, Measurements.MIN_MAX, cal);
}
ce.stretchHistogram(ip, saturated, st);
// This is all we care about from stretching the histogram:
p.setMinAndMax(ip.getMin(), ip.getMax());
regenerateMipMaps(p);
return null;
}
});
} catch (Exception e) {
IJError.print(e);
return false;
}
return true;
}
Aggregations