package org.bdgp.OpenHiCAMM.DB;

import org.micromanager.api.ImageCache;
import org.micromanager.utils.MDUtils;

import mmcorej.TaggedImage;

import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;

@DatabaseTable
public class Image {
    @DatabaseField(generatedId=true, canBeNull=false) private int id;
    @DatabaseField(canBeNull=false) private int slideId;
    @DatabaseField(canBeNull=false,uniqueCombo=true) private int acquisitionId;
    @DatabaseField(canBeNull=false,uniqueCombo=true) private int channel;
    @DatabaseField(canBeNull=false,uniqueCombo=true) private int slice;
    @DatabaseField(canBeNull=false,uniqueCombo=true) private int frame;
    @DatabaseField(canBeNull=false,uniqueCombo=true) private int position;
    
    public Image() {}
    public Image (int slideId, Acquisition acquisition, int channel, int slice, int frame, int position) {
        this.slideId = slideId;
        this.channel = channel;
        this.slice = slice;
        this.frame = frame;
        this.position = position;
        if (acquisition == null) throw new RuntimeException("Acquisition is null");
        this.acquisitionId = acquisition.getId();
    }
    public Image (int slideId, int acquisitionId, int channel, int slice, int frame, int position) {
        this.slideId = slideId;
        this.channel = channel;
        this.slice = slice;
        this.frame = frame;
        this.position = position;
        this.acquisitionId = acquisitionId;
    }
    public int getId() {return this.id;}
    public String getName() {return String.format("I%05d", this.id);}

    public int getSlideId() {return this.slideId;}
    public int getAcquisitionId() {return this.acquisitionId;}
    public int getChannel() {return this.channel;}
    public int getSlice() {return this.slice;}
    public int getFrame() {return this.frame;}
    public int getPosition() {return this.position;}
    
    public TaggedImage getImage(ImageCache imageCache) {
    	return imageCache.getImage(this.channel, this.slice, this.frame, this.position);
    }
    
    public String getLabel() {
        return MDUtils.generateLabel(this.channel, this.slice, this.frame, this.position);
    }
    
    public String toString() {
    	return String.format(
    			"%s(slideId=%d, channel=%d, slice=%d, frame=%d, position=%d, acquisitionId=%d)",
    			this.getClass().getSimpleName(),
    			this.slideId,
    			this.channel, 
    			this.slice,
    			this.frame,
    			this.position,
    			this.acquisitionId);
    }
}

@@ -26,16 +26,99 @@
 MM.DB;%0A%0A
+import org.bdgp.OpenHiCAMM.Dao;%0Aimport org.micromanager.acquisition.MMAcquisition;%0A
 import o
@@ -149,16 +149,58 @@
 eCache;%0A
+import org.micromanager.utils.ImageUtils;%0A
 import o
@@ -351,16 +351,119 @@
 Table;%0A%0A
+import ij.ImagePlus;%0Aimport ij.process.ImageProcessor;%0A%0Aimport static org.bdgp.OpenHiCAMM.Util.where;%0A%0A
 @Databas
@@ -2292,32 +2292,1131 @@
 position);%0A    %7D
+%0A%0A    public TaggedImage getTaggedImage(Dao%3CAcquisition%3E acqDao) %7B%0A        // Initialize the acquisition%0A        Acquisition acquisition = acqDao.selectOneOrDie(where(%22id%22,this.getAcquisitionId()));%0A        MMAcquisition mmacquisition = acquisition.getAcquisition(acqDao);%0A%0A        // Get the image cache object%0A        ImageCache imageCache = mmacquisition.getImageCache();%0A        if (imageCache == null) throw new RuntimeException(%22Acquisition was not initialized; imageCache is null!%22);%0A        // Get the tagged image from the image cache%0A        TaggedImage taggedImage = this.getImage(imageCache);%0A        if (taggedImage == null) throw new RuntimeException(String.format(%22Acqusition %25s, Image %25s is not in the image cache!%22,%0A                acquisition, this));%0A        return taggedImage;%0A    %7D%0A    %0A    public ImagePlus getImagePlus(Dao%3CAcquisition%3E acqDao) %7B%0A        TaggedImage taggedImage = this.getTaggedImage(acqDao);%0A        ImageProcessor processor = ImageUtils.makeProcessor(taggedImage);%0A        ImagePlus imp = new ImagePlus(this.toString(), processor);%0A        return imp;%0A    %7D
 %0A    %0A    public

add getTaggedImage() and getImagePlus() utility methods to Image module