Skip to content

Commit

Permalink
Merge pull request #131 from henrypinkard/main
Browse files Browse the repository at this point in the history
removed storage callbacks in favor of acquisiton notifications. Add a…
  • Loading branch information
henrypinkard committed Sep 6, 2023
2 parents 41d212a + 95da15b commit 923d34e
Show file tree
Hide file tree
Showing 8 changed files with 229 additions and 73 deletions.
2 changes: 1 addition & 1 deletion java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.micro-manager.ndtiffstorage</groupId>
<artifactId>NDTiffStorage</artifactId>
<version>2.14.0</version>
<version>2.15.0</version>
<packaging>jar</packaging>
<name>NDTiff Storage file format</name>
<description>Java-based writer and reader used for NDTiffStorage format</description>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.micromanager.ndtiffstorage;

import java.nio.ByteBuffer;
import mmcorej.org.json.JSONObject;

/**
* Width, height, pixel type etc.
*/
Expand All @@ -14,4 +17,5 @@ public EssentialImageMetadata(int w, int h, int bd, boolean isRGB) {
bitDepth = bd;
rgb = isRGB;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
/**
* Used to signal that an image has been written to disk
*/
@Deprecated
public interface ImageWrittenListener {

public void imageWritten(IndexEntryData ied);

/**
* Block until this listenter is shut down and all its resources freed
*/
@Deprecated
public void awaitCompletion();

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.micromanager.ndtiffstorage;

import java.nio.charset.StandardCharsets;
import mmcorej.org.json.JSONException;
import mmcorej.org.json.JSONObject;

Expand Down Expand Up @@ -80,7 +81,9 @@ public IndexEntryData() {
}

public String toString() {
return "IndexEntryData: " + axesKey_;
// convert bytebuffer to string
Buffer b = this.asByteBuffer();
return StandardCharsets.UTF_8.decode((ByteBuffer) b).toString();
}

public boolean isDataSetFinishedEntry() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public interface MultiresNDTiffAPI extends NDTiffAPI {
* @param imageWidth
* @return
*/
public Future putImageMultiRes(Object pixels, JSONObject metadata, HashMap<String, Object> axes,
public Future<IndexEntryData> putImageMultiRes(Object pixels, JSONObject metadata, HashMap<String, Object> axes,
boolean rgb, int bitDepth, int imageHeight, int imageWidth) ;

/**
Expand Down
169 changes: 169 additions & 0 deletions java/src/main/java/org/micromanager/ndtiffstorage/NDRAMStorage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
package org.micromanager.ndtiffstorage;

import java.util.HashMap;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
import java.util.function.Consumer;
import mmcorej.TaggedImage;
import mmcorej.org.json.JSONObject;

/**
* A class that implements the NDTiff API, but holds images in memory
*/
public class NDRAMStorage implements NDTiffAPI {

private boolean finished_ = false;

private HashMap<HashMap<String, Object>, Object> images_ = new HashMap<HashMap<String, Object>, Object>();
private HashMap<HashMap<String, Object>, JSONObject> metadata_ = new HashMap<HashMap<String, Object>, JSONObject>();

private boolean rgb_;
private int bitDepth_;
private int imageHeight_ = -1;
private int imageWidth_ = -1;
private JSONObject displaySettings_;
private JSONObject summaryMetadata_;


public NDRAMStorage(JSONObject summaryMetadata) {
summaryMetadata_ = summaryMetadata;
}

@Override
public Future<IndexEntryData> putImage(Object pixels, JSONObject metadata, HashMap<String, Object> axes,
boolean rgb, int bitDepth, int imageHeight, int imageWidth) {
images_.put(axes, pixels);
metadata_.put(axes, metadata);
rgb_ = rgb;
bitDepth_ = bitDepth;
imageHeight_ = imageHeight;
imageWidth_ = imageWidth;
Future<IndexEntryData> future = CompletableFuture.completedFuture(null);
return future;
}

@Override
public boolean isFinished() {
return finished_;
}

@Override
public void checkForWritingException() throws Exception {}

@Override
public void setDisplaySettings(JSONObject displaySettings) {
displaySettings_ = displaySettings;
}

@Override
public JSONObject getDisplaySettings() {
return displaySettings_;
}

@Override
public JSONObject getSummaryMetadata() {
return summaryMetadata_;
}

@Override
public void finishedWriting() {
finished_ = true;
}

@Override
public void addImageWrittenListener(ImageWrittenListener iwc) {

}

@Override
public String getDiskLocation() {
return null;
}

@Override
public void close() {
images_.clear();
metadata_.clear();
}

@Override
public void closeAndWait() throws InterruptedException {
images_.clear();
metadata_.clear();
}

@Override
public int[] getImageBounds() {
if (imageHeight_ == -1 || imageWidth_ == -1) {
return null; // not initialized
}
return new int[]{0, 0, imageWidth_, imageHeight_};
}

@Override
public TaggedImage getImage(HashMap<String, Object> axes) {
return new TaggedImage(images_.get(axes), metadata_.get(axes));
}

@Override
public TaggedImage getSubImage(HashMap<String, Object> axes, int xOffset, int yOffset, int width,
int height) {
TaggedImage ti = getImage(axes);
if (ti.pix == null) {
return null;
}
if (ti.pix instanceof byte[]) {
byte[] original = (byte[]) ti.pix;
byte[] cropped = new byte[width * height];

for (int y = 0; y < height; y++) {
System.arraycopy(original, ((y + yOffset) * this.imageWidth_) + xOffset, cropped, y * width, width);
}
return new TaggedImage(cropped, ti.tags);
}

if (ti.pix instanceof short[]) {
short[] original = (short[]) ti.pix;
short[] cropped = new short[width * height];

for (int y = 0; y < height; y++) {
System.arraycopy(original, ((y + yOffset) * this.imageWidth_) + xOffset, cropped, y * width, width);
}
return new TaggedImage(cropped, ti.tags);
}
throw new UnsupportedOperationException("Unsupported pixel type");
}

@Override
public EssentialImageMetadata getEssentialImageMetadata(HashMap<String, Object> axes) {
return new EssentialImageMetadata(imageWidth_, imageHeight_, bitDepth_, rgb_);
}

@Override
public boolean hasImage(HashMap<String, Object> axes) {
return images_.containsKey(axes);
}

@Override
public Set<HashMap<String, Object>> getAxesSet() {
return images_.keySet();
}

@Override
public String getUniqueAcqName() {
return null;
}

@Override
public int getWritingQueueTaskSize() {
return 0;
}

@Override
public int getWritingQueueTaskMaxSize() {
return 0;
}
}
15 changes: 11 additions & 4 deletions java/src/main/java/org/micromanager/ndtiffstorage/NDTiffAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public interface NDTiffAPI {
* @param imageHeight
* @param imageWidth
*/
public Future putImage(Object pixels, JSONObject metadata, HashMap<String, Object> axes,
public Future<IndexEntryData> putImage(Object pixels, JSONObject metadata, HashMap<String, Object> axes,
boolean rgb, int bitDepth, int imageHeight, int imageWidth);

/**
Expand Down Expand Up @@ -77,9 +77,9 @@ public Future putImage(Object pixels, JSONObject metadata, HashMap<String, Objec
public void finishedWriting();

/**
* Add a listener that gets notified when something written to disk
* To minimize performance hit, this listener should be extremely fast
* @deprecated
*/
@Deprecated
public void addImageWrittenListener(ImageWrittenListener iwc);

/**
Expand Down Expand Up @@ -107,13 +107,20 @@ public Future putImage(Object pixels, JSONObject metadata, HashMap<String, Objec
public int[] getImageBounds();

/**
* Get a single image from full resolution data
* Get a single image
*
* @param axes
* @return
*/
public TaggedImage getImage(HashMap<String, Object> axes);

/**
* Get a sub-image of the image with the specified axes
*
*/
public TaggedImage getSubImage(HashMap<String, Object> axes,
int xOffset, int yOffset, int width, int height);

/**
* Get the essential metadata for the image (width, height, byte depth, rgb),
* without retrieving pixels and full metadata
Expand Down

0 comments on commit 923d34e

Please sign in to comment.