ini.trakem2.display
Class Patch

java.lang.Object
  extended by ini.trakem2.persistence.DBObject
      extended by ini.trakem2.display.Displayable
          extended by ini.trakem2.display.Patch
All Implemented Interfaces:
ImageData, Paintable

public final class Patch
extends Displayable
implements ImageData


Nested Class Summary
 class Patch.PatchImage
           
static class Patch.TransformProperties
           
 
Nested classes/interfaces inherited from class ini.trakem2.display.Displayable
Displayable.DataPackage, Displayable.DoEdit, Displayable.DoEdits, Displayable.DoTransforms, Displayable.SliderListener
 
Field Summary
static java.awt.image.DirectColorModel DCM
           
 
Fields inherited from class ini.trakem2.display.Displayable
alpha, annotation, at, color, COMPOSITE_ADD, COMPOSITE_COLOR_YCBCR, COMPOSITE_DIFFERENCE, COMPOSITE_MULTIPLY, COMPOSITE_NORMAL, COMPOSITE_SUBTRACT, compositeModes, height, hs_linked, last_color, layer, linked_props, linked_props_origins, locked, props, TAG_ATTR1, TAG_ATTR2, title, visible, width
 
Fields inherited from class ini.trakem2.persistence.DBObject
id, project
 
Constructor Summary
Patch(Project project, long id, java.util.HashMap<java.lang.String,java.lang.String> ht_attributes, java.util.HashMap<Displayable,java.lang.String> ht_links)
          Reconstruct from an XML entry.
Patch(Project project, long id, java.lang.String title, float width, float height, int o_width, int o_height, int type, boolean locked, double min, double max, java.awt.geom.AffineTransform at)
          Reconstruct a Patch from the database.
Patch(Project project, java.lang.String title, double x, double y, ij.ImagePlus imp)
          Construct a Patch from an image.
 
Method Summary
 void addAlphaMask(ij.gui.Roi roi, int value)
          Add the given roi, in world coords, to the alpha mask, using the given fill value.
 java.awt.Image adjustChannels(java.awt.Image awt)
          Takes an image and scales its channels according to the values packed in this.channels.
 void appendCoordinateTransform(CoordinateTransform ct)
          Append a CoordinateTransform to the current CoordinateTransformList.
 void cacheCurrentPath(java.lang.String path)
          Cache a proper, good, known path to the image wrapped by this Patch.
 Displayable clone(Project pr, boolean copy_id)
          Performs a copy of this object, without the links, unlocked and visible, except for the image which is NOT duplicated.
 boolean contains(int x_p, int y_p)
          Considers the alpha mask.
 Patch.PatchImage createCoordinateTransformedImage()
           
 java.awt.Image createImage()
           
 java.awt.Image createImage(ij.ImagePlus imp)
           
 Patch.PatchImage createTransformedImage()
          Returns a PatchImage object containing the bottom-of-transformation-stack image and alpha mask, if any (except the AffineTransform, which is used for direct hw-accel screen rendering).
protected static void crosslink(java.util.Collection<Displayable> patches, boolean overlapping_only)
           
 void debug()
           
static void exportDTD(java.lang.StringBuilder sb_header, java.util.HashSet<java.lang.String> hs, java.lang.String indent)
           
 void exportXML(java.lang.StringBuilder sb_body, java.lang.String indent, java.lang.Object any)
          Opens and closes the tag and exports data.
 java.awt.geom.Area getArea()
          Returns an Area in world coords representing the inside of this Patch.
protected  java.awt.geom.Area getAreaForBucket(Layer layer)
          Use this instead of getAreaAt which calls getArea which is ...
 int getChannelAlphas()
           
 CoordinateTransform getCoordinateTransform()
           
 java.awt.Rectangle getCoordinateTransformBoundingBox()
          Get the bounding rectangle of the transformed image relative to the original image.
 java.lang.String getCurrentPath()
          Returns the value of the field current_path, which may be null.
 java.lang.String getFilePath()
          If this patch is part of a stack, the file path will contain the slice number attached to it, in the form -----#slice=10 for slice number 10.
 java.lang.String getImageFilePath()
          Returns the absolute path to the image file, as read by the OS.
 ij.ImagePlus getImagePlus()
          Fetches the ImagePlus from the cache; be warned: the returned ImagePlus may have been flushed, removed and then recreated if the program had memory needs that required flushing part of the cache; use @getImageProcessor to get the pixels guaranteed not to be ever null.
 ij.process.ImageProcessor getImageProcessor()
          Fetches the ImageProcessor from the cache, which will never be flushed or its pixels set to null.
 double getMax()
           
 double getMin()
           
 int getOHeight()
          The original height of the pixels in the source image file.
 java.lang.String getOriginalPath()
          Returns the value of the field original_path, which may be null.
 int getOWidth()
          The original width of the pixels in the source image file.
 int getPixel(double mag, int x, int y)
          Magnification-dependent counterpart to ImageProcessor.getPixel(x, y).
 int[] getPixel(double mag, int x, int y, int[] iArray)
          Magnification-dependent counterpart to ImageProcessor.getPixel(x, y, iArray).
 int[] getPixel(int x, int y, double mag)
          Expects x,y in world coordinates.
 java.lang.String getPreprocessorScriptPath()
           
 java.util.ArrayList<Patch> getStackPatches()
           
 Patch.TransformProperties getTransformPropertiesCopy()
           
 int getType()
          Returns the ImagePlus type of this Patch.
 boolean hasAlphaChannel()
           
 boolean hasAlphaMask()
          Caching system to avoid repeated checks.
 boolean isDeletable()
           
protected  boolean isRoughlyInside(Layer layer, java.awt.Rectangle r)
          If this Displayable intersects with @param r or almost intersects, then returns true.
 boolean isStack()
          Returns true if this Patch holds direct links to at least one other image in a different layer.
 void keyPressed(java.awt.event.KeyEvent ke)
           
 boolean linkPatches()
          Override to cancel.
static ij.process.ImageProcessor makeFlatImage(int type, Layer layer, java.awt.Rectangle srcRect, double scale, java.util.Collection<Patch> patches, java.awt.Color background)
          Defaults to setMinAndMax = true.
static ij.process.ImageProcessor makeFlatImage(int type, Layer layer, java.awt.Rectangle srcRect, double scale, java.util.Collection<Patch> patches, java.awt.Color background, boolean setMinAndMax)
          Creates an ImageProcessor of the specified type.
 PatchStack makePatchStack()
          Retuns a virtual ImagePlus with a virtual stack if necessary.
 boolean maskBorder(int size)
          Make the border have an alpha of zero.
 boolean maskBorder(int left, int top, int right, int bottom)
          Make the border have an alpha of zero.
 void paint(java.awt.Graphics2D g, java.awt.Rectangle srcRect, double magnification, boolean active, int channels, Layer active_layer, java.util.List<Layer> _ignored)
           
 void paintOffscreen(java.awt.Graphics2D g, java.awt.Rectangle srcRect, double magnification, boolean active, int channels, Layer active_layer)
           
 void paintSnapshot(java.awt.Graphics2D g, Layer layer, java.util.List<Layer> layers, java.awt.Rectangle srcRect, double mag)
           
 void prePaint(java.awt.Graphics2D g, java.awt.Rectangle srcRect, double magnification, boolean active, int channels, Layer active_layer, java.util.List<Layer> _ignored)
          Paint first whatever is available, then request that the proper image be loaded and painted.
 boolean remove(boolean check)
          Remove only if linked to other Patches or to noone.
 boolean revert()
          Revert the ImagePlus to the one stored in original_path, if any; will revert all linked patches if this is part of a stack.
 java.lang.String set(ij.ImagePlus new_imp)
          Set a new ImagePlus for this Patch.
protected  void setAlpha(float alpha, boolean update)
           
 void setAlphaMask(ij.process.ByteProcessor bp)
          Must call updateMipMaps() afterwards.
 void setCoordinateTransform(CoordinateTransform ct)
          Set a CoordinateTransform to this Patch.
 void setCoordinateTransformSilently(CoordinateTransform ct)
          For reconstruction purposes, overwrites the present CoordinateTransform, if any, with the given one.
 void setMinAndMax(double min, double max)
          The min and max values are stored with the Patch, so that the image can be flushed away but the non-destructive contrast settings preserved.
 void setPreprocessorScriptPath(java.lang.String path)
          After setting a preprocessor script, it is advisable that you call updateMipMaps() immediately.
 java.util.concurrent.Future<java.lang.Boolean> updateMipMaps()
          Recreate mipmaps and flush away any cached ones.
 void updatePixelProperties(ij.ImagePlus imp)
          Update type, original dimensions and min,max from the ImagePlus.
 
Methods inherited from class ini.trakem2.display.Displayable
adjustProperties, areThereLayerCrossLinks, canSendTo, clone, clone, commonDTDChildren, contains, deselect, destroy, displacePoints, exportDTD, exportSVG, findById, findNearestPoint, findNearestPoint, getAffineTransform, getAffineTransformCopy, getAlpha, getAnnotation, getAreaAt, getBoundingBox, getBoundingBox, getBoundingBox, getBounds, getBounds, getBucketable, getColor, getComposite, getComposite, getCompositeMode, getHeight, getIntersection, getLayer, getLayerIds, getLayerSet, getLayersWithData, getLinked, getLinked, getLinkedBox, getLinkedGroup, getLinkedProperties, getLinkedProperties, getLinkedProperty, getLinkedProperty, getMinimalBoundingBox, getNameId, getPerimeter, getPerimeter, getProperties, getProperty, getProperty, getShortTitle, getTitle, getWidth, getX, getXMLSafeValue, getXMLSafeValue, getY, hasLinkedGroupWithinLayer, hasProperties, intersects, intersects, intersects, intersects, inverseTransformPoint, isLinked, isLinked, isLinked, isLocked, isLocked2, isOnlyLinkedTo, isOnlyLinkedTo, isOutOfRepaintingClip, isOutOfRepaintingClip, isVisible, link, link, makeAdjustPropertiesDialog, measure, mouseDragged, mousePressed, mouseReleased, mouseWheelMoved, paintAsBox, paintOffscreen, preTransform, processAdjustPropertiesDialog, remove2, removeLinkedProperties, removeLinkedPropertiesFromOrigins, repaint, restXML, rotate, rotate, rotatePoints, scale, scale, scalePoints, setAffineTransform, setAlpha, setAnnotation, setColor, setCompositeMode, setDimensions, setDimensions, setLayer, setLayer, setLinkedProperties, setLinkedProperty, setLocation, setLocked, setProperty, setTitle, setVisible, setVisible, snapTo, softRemove, toString, transform, transformPoint, transformPoint, transformPoints, transformPoints, transformPoints, transformPoints, transformPoints, transformPoints, transformPoints, transformPoints, transformPoints, transformRectangle, translate, translate, unlink, unlink, unlinkAll, updateBucket, updateInDatabase, xmlError, xmlError
 
Methods inherited from class ini.trakem2.persistence.DBObject
addToDatabase, getId, getInfo, getProject, getUniqueIdentifier, removeFromDatabase, updateInDatabase
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DCM

public static final java.awt.image.DirectColorModel DCM
Constructor Detail

Patch

public Patch(Project project,
             java.lang.String title,
             double x,
             double y,
             ij.ImagePlus imp)
Construct a Patch from an image.


Patch

public Patch(Project project,
             long id,
             java.lang.String title,
             float width,
             float height,
             int o_width,
             int o_height,
             int type,
             boolean locked,
             double min,
             double max,
             java.awt.geom.AffineTransform at)
Reconstruct a Patch from the database. The ImagePlus will be loaded when necessary.


Patch

public Patch(Project project,
             long id,
             java.util.HashMap<java.lang.String,java.lang.String> ht_attributes,
             java.util.HashMap<Displayable,java.lang.String> ht_links)
Reconstruct from an XML entry.

Method Detail

getOWidth

public int getOWidth()
The original width of the pixels in the source image file.


getOHeight

public int getOHeight()
The original height of the pixels in the source image file.


getImagePlus

public ij.ImagePlus getImagePlus()
Fetches the ImagePlus from the cache; be warned: the returned ImagePlus may have been flushed, removed and then recreated if the program had memory needs that required flushing part of the cache; use @getImageProcessor to get the pixels guaranteed not to be ever null.


getImageProcessor

public ij.process.ImageProcessor getImageProcessor()
Fetches the ImageProcessor from the cache, which will never be flushed or its pixels set to null. If you keep many of these, you may end running out of memory: I advise you to call this method everytime you need the processor.


updateMipMaps

public java.util.concurrent.Future<java.lang.Boolean> updateMipMaps()
Recreate mipmaps and flush away any cached ones. This method is essentially the same as patch.getProject().getLoader().update(patch); which in turn it's the same as the following two calls: patch.getProject().getLoader().generateMipMaps(patch); patch.getProject().getLoader().decacheAWT(patch.getId()); If you want to update lots of Patch instances in parallel, consider also project.getLoader().generateMipMaps(ArrayList patches, boolean overwrite);


updatePixelProperties

public void updatePixelProperties(ij.ImagePlus imp)
Update type, original dimensions and min,max from the ImagePlus. This is automatically done after a preprocessor script has modified the image.


set

public java.lang.String set(ij.ImagePlus new_imp)
Set a new ImagePlus for this Patch. The original path and image remain untouched. Any later image is deleted and replaced by the new one.


setMinAndMax

public void setMinAndMax(double min,
                         double max)
The min and max values are stored with the Patch, so that the image can be flushed away but the non-destructive contrast settings preserved.


getMin

public double getMin()

getMax

public double getMax()

getType

public int getType()
Returns the ImagePlus type of this Patch.


createImage

public java.awt.Image createImage(ij.ImagePlus imp)

createImage

public java.awt.Image createImage()

getChannelAlphas

public int getChannelAlphas()

adjustChannels

public final java.awt.Image adjustChannels(java.awt.Image awt)
Takes an image and scales its channels according to the values packed in this.channels. This method is intended for fixing RGB images which are loaded from jpegs (the mipmaps), and which have then the full colorization of the original image present in their pixels array. Otherwise the channel opacity scaling makes no sense. If 0xffffffff == this.channels the awt is returned as is. If the awt is null returns null.


paintOffscreen

public void paintOffscreen(java.awt.Graphics2D g,
                           java.awt.Rectangle srcRect,
                           double magnification,
                           boolean active,
                           int channels,
                           Layer active_layer)

paint

public void paint(java.awt.Graphics2D g,
                  java.awt.Rectangle srcRect,
                  double magnification,
                  boolean active,
                  int channels,
                  Layer active_layer,
                  java.util.List<Layer> _ignored)
Specified by:
paint in interface Paintable
Overrides:
paint in class Displayable

prePaint

public void prePaint(java.awt.Graphics2D g,
                     java.awt.Rectangle srcRect,
                     double magnification,
                     boolean active,
                     int channels,
                     Layer active_layer,
                     java.util.List<Layer> _ignored)
Paint first whatever is available, then request that the proper image be loaded and painted.

Specified by:
prePaint in interface Paintable
Overrides:
prePaint in class Displayable

isDeletable

public boolean isDeletable()
Specified by:
isDeletable in class Displayable

remove

public boolean remove(boolean check)
Remove only if linked to other Patches or to noone.

Overrides:
remove in class Displayable

isStack

public final boolean isStack()
Returns true if this Patch holds direct links to at least one other image in a different layer. Doesn't check for total overlap.


makePatchStack

public PatchStack makePatchStack()
Retuns a virtual ImagePlus with a virtual stack if necessary.


getStackPatches

public java.util.ArrayList<Patch> getStackPatches()

exportXML

public void exportXML(java.lang.StringBuilder sb_body,
                      java.lang.String indent,
                      java.lang.Object any)
Opens and closes the tag and exports data. The image is saved in the directory provided in @param any as a String.

Overrides:
exportXML in class Displayable

exportDTD

public static void exportDTD(java.lang.StringBuilder sb_header,
                             java.util.HashSet<java.lang.String> hs,
                             java.lang.String indent)

clone

public Displayable clone(Project pr,
                         boolean copy_id)
Performs a copy of this object, without the links, unlocked and visible, except for the image which is NOT duplicated.

Specified by:
clone in class Displayable

getTransformPropertiesCopy

public Patch.TransformProperties getTransformPropertiesCopy()

linkPatches

public boolean linkPatches()
Override to cancel.

Overrides:
linkPatches in class Displayable
Returns:
whether the locking state changed.

paintSnapshot

public void paintSnapshot(java.awt.Graphics2D g,
                          Layer layer,
                          java.util.List<Layer> layers,
                          java.awt.Rectangle srcRect,
                          double mag)
Overrides:
paintSnapshot in class Displayable

crosslink

protected static void crosslink(java.util.Collection<Displayable> patches,
                                boolean overlapping_only)

getPixel

public int getPixel(double mag,
                    int x,
                    int y)
Magnification-dependent counterpart to ImageProcessor.getPixel(x, y). Expects x,y in world coordinates. This method is intended for grabing an occasional pixel; to grab all pixels, see @getImageProcessor method.


getPixel

public int[] getPixel(double mag,
                      int x,
                      int y,
                      int[] iArray)
Magnification-dependent counterpart to ImageProcessor.getPixel(x, y, iArray). Expects x,y in world coordinates. This method is intended for grabing an occasional pixel; to grab all pixels, see @getImageProcessor method.


getPixel

public int[] getPixel(int x,
                      int y,
                      double mag)
Expects x,y in world coordinates. This method is intended for grabing an occasional pixel; to grab all pixels, see @getImageProcessor method.


getFilePath

public final java.lang.String getFilePath()
If this patch is part of a stack, the file path will contain the slice number attached to it, in the form -----#slice=10 for slice number 10.


getImageFilePath

public final java.lang.String getImageFilePath()
Returns the absolute path to the image file, as read by the OS.


getCurrentPath

public final java.lang.String getCurrentPath()
Returns the value of the field current_path, which may be null. If not null, the value may contain the slice info in it if it's part of a stack.


cacheCurrentPath

public final void cacheCurrentPath(java.lang.String path)
Cache a proper, good, known path to the image wrapped by this Patch.


getOriginalPath

public java.lang.String getOriginalPath()
Returns the value of the field original_path, which may be null. If not null, the value may contain the slice info in it if it's part of a stack.


setAlpha

protected void setAlpha(float alpha,
                        boolean update)
Overrides:
setAlpha in class Displayable

debug

public void debug()

revert

public boolean revert()
Revert the ImagePlus to the one stored in original_path, if any; will revert all linked patches if this is part of a stack.


setCoordinateTransformSilently

public void setCoordinateTransformSilently(CoordinateTransform ct)
For reconstruction purposes, overwrites the present CoordinateTransform, if any, with the given one.


setCoordinateTransform

public final void setCoordinateTransform(CoordinateTransform ct)
Set a CoordinateTransform to this Patch. The resulting image of applying the coordinate transform does not need to be rectangular: an alpha mask will take care of the borders. You should call updateMipMaps() afterwards to update the mipmap images used for painting this Patch to the screen.


appendCoordinateTransform

public final void appendCoordinateTransform(CoordinateTransform ct)
Append a CoordinateTransform to the current CoordinateTransformList. If there is no transform yet, it just sets it. If there is only one transform, it replaces it by a list containing both.


getCoordinateTransformBoundingBox

public final java.awt.Rectangle getCoordinateTransformBoundingBox()
Get the bounding rectangle of the transformed image relative to the original image. TODO Currently, this is done in a very expensive way. The TransformMesh is built and its bounding rectangle is returned. Think about just storing this rectangle in the Patch instance.

Returns:

getCoordinateTransform

public final CoordinateTransform getCoordinateTransform()

createCoordinateTransformedImage

public final Patch.PatchImage createCoordinateTransformedImage()

createTransformedImage

public Patch.PatchImage createTransformedImage()
Returns a PatchImage object containing the bottom-of-transformation-stack image and alpha mask, if any (except the AffineTransform, which is used for direct hw-accel screen rendering).


hasAlphaMask

public final boolean hasAlphaMask()
Caching system to avoid repeated checks. No automatic memoization ... snif


hasAlphaChannel

public boolean hasAlphaChannel()

setAlphaMask

public void setAlphaMask(ij.process.ByteProcessor bp)
                  throws java.lang.IllegalArgumentException
Must call updateMipMaps() afterwards. Set it to null to remove it.

Throws:
java.lang.IllegalArgumentException

keyPressed

public void keyPressed(java.awt.event.KeyEvent ke)
Overrides:
keyPressed in class Displayable

contains

public boolean contains(int x_p,
                        int y_p)
Considers the alpha mask.

Overrides:
contains in class Displayable

setPreprocessorScriptPath

public void setPreprocessorScriptPath(java.lang.String path)
After setting a preprocessor script, it is advisable that you call updateMipMaps() immediately.


addAlphaMask

public void addAlphaMask(ij.gui.Roi roi,
                         int value)
Add the given roi, in world coords, to the alpha mask, using the given fill value.


getPreprocessorScriptPath

public java.lang.String getPreprocessorScriptPath()

getArea

public java.awt.geom.Area getArea()
Returns an Area in world coords representing the inside of this Patch. The fully alpha pixels are considered outside.

Overrides:
getArea in class Displayable

makeFlatImage

public static ij.process.ImageProcessor makeFlatImage(int type,
                                                      Layer layer,
                                                      java.awt.Rectangle srcRect,
                                                      double scale,
                                                      java.util.Collection<Patch> patches,
                                                      java.awt.Color background)
Defaults to setMinAndMax = true.


makeFlatImage

public static ij.process.ImageProcessor makeFlatImage(int type,
                                                      Layer layer,
                                                      java.awt.Rectangle srcRect,
                                                      double scale,
                                                      java.util.Collection<Patch> patches,
                                                      java.awt.Color background,
                                                      boolean setMinAndMax)
Creates an ImageProcessor of the specified type.

Parameters:
type - Any of ImagePlus.GRAY_8, GRAY_16, GRAY_32 or COLOR_RGB.
srcRect - the box in world coordinates to make an image out of.
scale - may be up to 1.0.
patches - The list of patches to paint. The first gets painted first (at the bottom).
background - The color with which to paint the outsides where no image paints into.
setMinAndMax - defines whether the min and max of each Patch is set before pasting the Patch.

maskBorder

public boolean maskBorder(int size)
Make the border have an alpha of zero.


maskBorder

public boolean maskBorder(int left,
                          int top,
                          int right,
                          int bottom)
Make the border have an alpha of zero.


getAreaForBucket

protected java.awt.geom.Area getAreaForBucket(Layer layer)
Use this instead of getAreaAt which calls getArea which is ... dog slow for something like buckets.

Overrides:
getAreaForBucket in class Displayable

isRoughlyInside

protected boolean isRoughlyInside(Layer layer,
                                  java.awt.Rectangle r)
Description copied from class: Displayable
If this Displayable intersects with @param r or almost intersects, then returns true. This method is meant to be very fast, and err on the "yes" and never on the "no".

Overrides:
isRoughlyInside in class Displayable