<commit_before>//
// ImageTransform.java
//

/*
VisBio application for visualization of multidimensional
biological image data. Copyright (C) 2002-2004 <NAME>.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

package loci.visbio.data;

import loci.visbio.util.VisUtil;
import visad.*;

/** ImageTransform is a DataTransform superclass that provides image data. */
public abstract class ImageTransform extends DataTransform {

  // -- Constants --

  /** Micron unit. */
  public static final ScaledUnit MICRON =
    new ScaledUnit(1e-6, SI.meter, "mu"); // VisAD font does not display ""


  // -- Fields --

  /** Physical image dimensions in microns. */
  protected double micronWidth, micronHeight, micronStep;

  /** X-axis type. */
  protected RealType xType;

  /** Y-axis type. */
  protected RealType yType;

  /** Z-axis type. */
  protected RealType zType;

  /** Range types. */
  private RealType[] rangeTypes;

  /** MathType of generated images. */
  private FunctionType functionType;


  // -- Constructors --

  /** Constructs an uninitialized image transform. */
  public ImageTransform() { this(null, null); }

  /** Creates an image transform with the given transform as its parent. */
  public ImageTransform(DataTransform parent, String name) {
    this(parent, name, Double.NaN, Double.NaN, Double.NaN);
  }

  /**
   * Creates an image transform with the given transform as its parent,
   * that is dimensioned according to the specified values in microns.
   */
  public ImageTransform(DataTransform parent, String name,
    double width, double height, double step)
  {
    super(parent, name);

    // initialize physical dimensions
    if (parent instanceof ImageTransform) {
      ImageTransform it = (ImageTransform) parent;
      // use parent values for physical dimensions if none specified
      if (width != width) width = it.getMicronWidth();
      if (height != height) height = it.getMicronHeight();
      if (step != step) step = it.getMicronStep();
    }
    micronWidth = width;
    micronHeight = height;
    micronStep = step;

    // initialize internal MathTypes
    xType = VisUtil.getRealType(name + "_X", MICRON);
    yType = VisUtil.getRealType(name + "_Y", MICRON);
    zType = VisUtil.getRealType(name + "_Z", MICRON);
  }


  // -- ImageTransform API methods --

  /** Gets the width in pixels of each image. */
  public abstract int getImageWidth();

  /** Gets the height in pixels of each image. */
  public abstract int getImageHeight();

  /** Gets number of range components at each pixel. */
  public abstract int getRangeCount();

  /** Gets physical image width in microns. */
  public double getMicronWidth() { return micronWidth; }

  /** Gets physical image height in microns. */
  public double getMicronHeight() { return micronHeight; }

  /** Gets physical distance between image slices in microns. */
  public double getMicronStep() { return micronStep; }

  /** Gets the RealType used for the X-axis. */
  public RealType getXType() { return xType; }

  /** Gets the RealType used for the Y-axis. */
  public RealType getYType() { return yType; }

  /** Gets the RealType used for the Z-axis. */
  public RealType getZType() { return zType; }

  /** Gets the RealTypes used for the range components. */
  public RealType[] getRangeTypes() {
    if (rangeTypes == null) {
      rangeTypes = new RealType[getRangeCount()];
      for (int i=0; i<rangeTypes.length; i++) {
        String s = "range" + (i + 1);
        rangeTypes[i] = VisUtil.getRealType(name + "_" + s);
      }
    }
    return rangeTypes;
  }

  /** Gets MathType of images generated by this image transform. */
  public FunctionType getType() {
    if (functionType == null) {
      try {
        RealTupleType domain = new RealTupleType(xType, yType);
        RealTupleType range = new RealTupleType(getRangeTypes());
        functionType = new FunctionType(domain, range);
      }
      catch (VisADException exc) { exc.printStackTrace(); }
    }
    return functionType;
  }

  /**
   * Gets X and Y units for use with image domain sets, computed from micron
   * information. Pixel range is assumed to be from 0 to width-1 (X) and
   * 0 to height-1 (Y).
   */
  public Unit[] getImageUnits() {
    Unit xu = new ScaledUnit(micronWidth / getImageWidth(), MICRON);
    Unit yu = new ScaledUnit(micronHeight / getImageHeight(), MICRON);
    return new Unit[] {xu, yu};
  }

  /**
   * Gets Z unit for use with image stack domain sets, computed from
   * micron information. Z range is assumed to be from -1 to 1.
   */
  public Unit getZUnit(int zAxis) {
    if (zAxis < 0 || zAxis >= lengths.length) return null;
    double q = micronStep * lengths[zAxis] / 2;
    return new OffsetUnit(q, new ScaledUnit(q, MICRON));
  }


  // -- DataTransform API methods --

  /** Retrieves a set of mappings for displaying this transform effectively. */
  public ScalarMap[] getSuggestedMaps() {
    RealType[] range = getRangeTypes();
    ScalarMap[] maps = new ScalarMap[2 + range.length];
    try {
      maps[0] = new ScalarMap(xType, Display.XAxis);
      maps[1] = new ScalarMap(yType, Display.YAxis);
      for (int i=0; i<range.length; i++) {
        maps[i + 2] = new ScalarMap(range[i], Display.RGBA);
      }
    }
    catch (VisADException exc) { exc.printStackTrace(); }
    return maps;
  }

  /** Most image transforms are not rendered immediately. */
  public boolean isImmediate() { return false; }

  /** Gets whether this data object can be displayed in 3D. */
  public boolean canDisplay3D() {
    return super.canDisplay3D() || canDisplay2D();
  }

}
<commit_msg>Units should be null if use microns is unchecked.<commit_after>//
// ImageTransform.java
//

/*
VisBio application for visualization of multidimensional
biological image data. Copyright (C) 2002-2004 <NAME>.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

package loci.visbio.data;

import loci.visbio.util.VisUtil;
import visad.*;

/** ImageTransform is a DataTransform superclass that provides image data. */
public abstract class ImageTransform extends DataTransform {

  // -- Constants --

  /** Micron unit. */
  public static final ScaledUnit MICRON =
    new ScaledUnit(1e-6, SI.meter, "mu"); // VisAD font does not display ""


  // -- Fields --

  /** Physical image dimensions in microns. */
  protected double micronWidth, micronHeight, micronStep;

  /** X-axis type. */
  protected RealType xType;

  /** Y-axis type. */
  protected RealType yType;

  /** Z-axis type. */
  protected RealType zType;

  /** Range types. */
  private RealType[] rangeTypes;

  /** MathType of generated images. */
  private FunctionType functionType;


  // -- Constructors --

  /** Constructs an uninitialized image transform. */
  public ImageTransform() { this(null, null); }

  /** Creates an image transform with the given transform as its parent. */
  public ImageTransform(DataTransform parent, String name) {
    this(parent, name, Double.NaN, Double.NaN, Double.NaN);
  }

  /**
   * Creates an image transform with the given transform as its parent,
   * that is dimensioned according to the specified values in microns.
   */
  public ImageTransform(DataTransform parent, String name,
    double width, double height, double step)
  {
    super(parent, name);

    // initialize physical dimensions
    if (parent instanceof ImageTransform) {
      ImageTransform it = (ImageTransform) parent;
      // use parent values for physical dimensions if none specified
      if (width != width) width = it.getMicronWidth();
      if (height != height) height = it.getMicronHeight();
      if (step != step) step = it.getMicronStep();
    }
    micronWidth = width;
    micronHeight = height;
    micronStep = step;

    // initialize internal MathTypes
    xType = VisUtil.getRealType(name + "_X", width == width ? MICRON : null);
    yType = VisUtil.getRealType(name + "_Y", height == height ? MICRON : null);
    zType = VisUtil.getRealType(name + "_Z", step == step ? MICRON : null);
  }


  // -- ImageTransform API methods --

  /** Gets the width in pixels of each image. */
  public abstract int getImageWidth();

  /** Gets the height in pixels of each image. */
  public abstract int getImageHeight();

  /** Gets number of range components at each pixel. */
  public abstract int getRangeCount();

  /** Gets physical image width in microns. */
  public double getMicronWidth() { return micronWidth; }

  /** Gets physical image height in microns. */
  public double getMicronHeight() { return micronHeight; }

  /** Gets physical distance between image slices in microns. */
  public double getMicronStep() { return micronStep; }

  /** Gets the RealType used for the X-axis. */
  public RealType getXType() { return xType; }

  /** Gets the RealType used for the Y-axis. */
  public RealType getYType() { return yType; }

  /** Gets the RealType used for the Z-axis. */
  public RealType getZType() { return zType; }

  /** Gets the RealTypes used for the range components. */
  public RealType[] getRangeTypes() {
    if (rangeTypes == null) {
      rangeTypes = new RealType[getRangeCount()];
      for (int i=0; i<rangeTypes.length; i++) {
        String s = "range" + (i + 1);
        rangeTypes[i] = VisUtil.getRealType(name + "_" + s);
      }
    }
    return rangeTypes;
  }

  /** Gets MathType of images generated by this image transform. */
  public FunctionType getType() {
    if (functionType == null) {
      try {
        RealTupleType domain = new RealTupleType(xType, yType);
        RealTupleType range = new RealTupleType(getRangeTypes());
        functionType = new FunctionType(domain, range);
      }
      catch (VisADException exc) { exc.printStackTrace(); }
    }
    return functionType;
  }

  /**
   * Gets X and Y units for use with image domain sets, computed from micron
   * information. Pixel range is assumed to be from 0 to width-1 (X) and
   * 0 to height-1 (Y).
   */
  public Unit[] getImageUnits() {
    Unit xu = micronWidth == micronWidth ?
      new ScaledUnit(micronWidth / getImageWidth(), MICRON) : null;
    Unit yu = micronHeight == micronHeight ?
      new ScaledUnit(micronHeight / getImageHeight(), MICRON) : null;
    return new Unit[] {xu, yu};
  }

  /**
   * Gets Z unit for use with image stack domain sets, computed from
   * micron information. Z range is assumed to be from -1 to 1.
   */
  public Unit getZUnit(int zAxis) {
    if (zAxis < 0 || zAxis >= lengths.length) return null;
    if (micronStep != micronStep) return null;
    double q = micronStep * lengths[zAxis] / 2;
    return new OffsetUnit(q, new ScaledUnit(q, MICRON));
  }


  // -- DataTransform API methods --

  /** Retrieves a set of mappings for displaying this transform effectively. */
  public ScalarMap[] getSuggestedMaps() {
    RealType[] range = getRangeTypes();
    ScalarMap[] maps = new ScalarMap[2 + range.length];
    try {
      maps[0] = new ScalarMap(xType, Display.XAxis);
      maps[1] = new ScalarMap(yType, Display.YAxis);
      for (int i=0; i<range.length; i++) {
        maps[i + 2] = new ScalarMap(range[i], Display.RGBA);
      }
    }
    catch (VisADException exc) { exc.printStackTrace(); }
    return maps;
  }

  /** Most image transforms are not rendered immediately. */
  public boolean isImmediate() { return false; }

  /** Gets whether this data object can be displayed in 3D. */
  public boolean canDisplay3D() {
    return super.canDisplay3D() || canDisplay2D();
  }

}

{'fraction_non_alphanumeric': 0.10701657892669128, 'fraction_numerical': 0.006914433880726016, 'mean_word_length': 3.103159252095422, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3087808', 'n_tokens_mistral': 3768, 'n_tokens_neox': 3571, 'n_words': 1380}