/**
  * Returns an ImageProcessor for the specified slice, were 1<=n<=nslices. Returns null if the
  * stack is empty.
  */
 public ImageProcessor getProcessor(int n) {
   ImageProcessor ip;
   if (n < 1 || n > nSlices) throw new IllegalArgumentException(outOfRange + n);
   if (nSlices == 0) return null;
   if (stack[0] == null) throw new IllegalArgumentException("Pixel array is null");
   if (stack[0] instanceof byte[]) ip = new ByteProcessor(width, height, null, cm);
   else if (stack[0] instanceof short[]) ip = new ShortProcessor(width, height, null, cm);
   else if (stack[0] instanceof int[]) ip = new ColorProcessor(width, height, null);
   else if (stack[0] instanceof float[]) ip = new FloatProcessor(width, height, null, cm);
   else throw new IllegalArgumentException("Unknown stack type");
   ip.setPixels(stack[n - 1]);
   if (min != Double.MAX_VALUE && ip != null && !(ip instanceof ColorProcessor))
     ip.setMinAndMax(min, max);
   if (cTable != null) ip.setCalibrationTable(cTable);
   return ip;
 }
 private static Image5D createImage5D(CMMCore core, String wndTitle) throws Exception {
   core_ = core;
   ImageProcessor ip;
   int type = 0;
   int width_ = (int) core_.getImageWidth();
   int height_ = (int) core_.getImageHeight();
   long byteDepth = core_.getBytesPerPixel();
   long channels = core_.getNumberOfChannels();
   if (byteDepth == 1 && channels == 1) {
     type = ImagePlus.GRAY8;
     ip = new ByteProcessor(width_, height_);
     if (contrastSettings8_.getRange() == 0.0) ip.setMinAndMax(0, 255);
     else ip.setMinAndMax(contrastSettings8_.min, contrastSettings8_.max);
   } else if (byteDepth == 2 && channels == 1) {
     type = ImagePlus.GRAY16;
     ip = new ShortProcessor(width_, height_);
     if (contrastSettings16_.getRange() == 0.0) ip.setMinAndMax(0, 65535);
     else ip.setMinAndMax(contrastSettings16_.min, contrastSettings16_.max);
   } else if (byteDepth == 0) {
     throw (new Exception(logError("Imaging device not initialized")));
   } else if (byteDepth == 1 && channels == 4) {
     // assuming RGB32 format
     ip = new ColorProcessor(width_, height_);
     if (contrastSettings8_.getRange() == 0.0) ip.setMinAndMax(0, 255);
     else ip.setMinAndMax(contrastSettings8_.min, contrastSettings8_.max);
   } else {
     String message =
         "Unsupported pixel depth: "
             + core_.getBytesPerPixel()
             + " byte(s) and "
             + channels
             + " channel(s).";
     throw (new Exception(logError(message)));
   }
   ip.setColor(Color.black);
   if (currentColorModel_ != null) ip.setColorModel(currentColorModel_);
   ip.fill();
   Image5D img5d = new Image5D(wndTitle, type, width_, height_, 1, 1, 1, false);
   @SuppressWarnings("unused")
   Image5DWindow i5dw = new Image5DWindow(img5d);
   return img5d;
 }
Example #3
0
  void setCalibration(ImagePlus imp) {
    if (fi.fileType == FileInfo.GRAY16_SIGNED) {
      if (IJ.debugMode) IJ.log("16-bit signed");
      double[] coeff = new double[2];
      coeff[0] = -32768.0;
      coeff[1] = 1.0;
      imp.getLocalCalibration().setFunction(Calibration.STRAIGHT_LINE, coeff, "gray value");
    }

    Properties props = decodeDescriptionString(fi);
    Calibration cal = imp.getCalibration();
    boolean calibrated = false;
    if (fi.pixelWidth > 0.0 && fi.unit != null) {
      cal.pixelWidth = fi.pixelWidth;
      cal.pixelHeight = fi.pixelHeight;
      cal.pixelDepth = fi.pixelDepth;
      cal.setUnit(fi.unit);
      calibrated = true;
    }

    if (fi.valueUnit != null) {
      int f = fi.calibrationFunction;
      if ((f >= Calibration.STRAIGHT_LINE && f <= Calibration.RODBARD2 && fi.coefficients != null)
          || f == Calibration.UNCALIBRATED_OD) {
        boolean zeroClip = props != null && props.getProperty("zeroclip", "false").equals("true");
        cal.setFunction(f, fi.coefficients, fi.valueUnit, zeroClip);
        calibrated = true;
      }
    }

    if (calibrated) checkForCalibrationConflict(imp, cal);

    if (fi.frameInterval != 0.0) cal.frameInterval = fi.frameInterval;

    if (props == null) return;

    cal.xOrigin = getDouble(props, "xorigin");
    cal.yOrigin = getDouble(props, "yorigin");
    cal.zOrigin = getDouble(props, "zorigin");
    cal.info = props.getProperty("info");

    cal.fps = getDouble(props, "fps");
    cal.loop = getBoolean(props, "loop");
    cal.frameInterval = getDouble(props, "finterval");
    cal.setTimeUnit(props.getProperty("tunit", "sec"));

    double displayMin = getDouble(props, "min");
    double displayMax = getDouble(props, "max");
    if (!(displayMin == 0.0 && displayMax == 0.0)) {
      int type = imp.getType();
      ImageProcessor ip = imp.getProcessor();
      if (type == ImagePlus.GRAY8 || type == ImagePlus.COLOR_256)
        ip.setMinAndMax(displayMin, displayMax);
      else if (type == ImagePlus.GRAY16 || type == ImagePlus.GRAY32) {
        if (ip.getMin() != displayMin || ip.getMax() != displayMax)
          ip.setMinAndMax(displayMin, displayMax);
      }
    }

    int stackSize = imp.getStackSize();
    if (stackSize > 1) {
      int channels = (int) getDouble(props, "channels");
      int slices = (int) getDouble(props, "slices");
      int frames = (int) getDouble(props, "frames");
      if (channels == 0) channels = 1;
      if (slices == 0) slices = 1;
      if (frames == 0) frames = 1;
      // IJ.log("setCalibration: "+channels+"  "+slices+"  "+frames);
      if (channels * slices * frames == stackSize) {
        imp.setDimensions(channels, slices, frames);
        if (getBoolean(props, "hyperstack")) imp.setOpenAsHyperStack(true);
      }
    }
  }
Example #4
0
  public synchronized void updateImage() {
    int imageSize = width * height;
    int nChannels = getNChannels();
    int redValue, greenValue, blueValue;
    int ch = getChannel();

    // IJ.log("updateImage: "+ch+"/"+nChannels+" "+currentSlice+" "+currentFrame);
    if (ch > nChannels) ch = nChannels;
    boolean newChannel = false;
    if (ch - 1 != currentChannel) {
      previousChannel = currentChannel;
      currentChannel = ch - 1;
      newChannel = true;
    }

    ImageProcessor ip = getProcessor();
    if (mode != COMPOSITE) {
      if (newChannel) {
        setupLuts(nChannels);
        LUT cm = lut[currentChannel];
        if (mode == COLOR) ip.setColorModel(cm);
        if (!(cm.min == 0.0 && cm.max == 0.0)) ip.setMinAndMax(cm.min, cm.max);
        if (!IJ.isMacro()) ContrastAdjuster.update();
        for (int i = 0; i < MAX_CHANNELS; i++) active[i] = i == currentChannel ? true : false;
        Channels.updateChannels();
      }
      if (ip != null) img = ip.createImage();
      return;
    }

    if (nChannels == 1) {
      cip = null;
      rgbPixels = null;
      awtImage = null;
      if (ip != null) img = ip.createImage();
      return;
    }

    if (cip == null
        || cip[0].getWidth() != width
        || cip[0].getHeight() != height
        || getBitDepth() != bitDepth) {
      setup(nChannels, getImageStack());
      rgbPixels = null;
      rgbSampleModel = null;
      if (currentChannel >= nChannels) {
        setSlice(1);
        currentChannel = 0;
        newChannel = true;
      }
      bitDepth = getBitDepth();
    }

    if (newChannel) {
      getProcessor().setMinAndMax(cip[currentChannel].getMin(), cip[currentChannel].getMax());
      if (!IJ.isMacro()) ContrastAdjuster.update();
    }
    // IJ.log(nChannels+" "+ch+" "+currentChannel+"  "+newChannel);

    if (getSlice() != currentSlice || getFrame() != currentFrame) {
      currentSlice = getSlice();
      currentFrame = getFrame();
      int position = getStackIndex(1, currentSlice, currentFrame);
      if (cip == null) return;
      for (int i = 0; i < nChannels; ++i)
        cip[i].setPixels(getImageStack().getProcessor(position + i).getPixels());
    }

    if (rgbPixels == null) {
      rgbPixels = new int[imageSize];
      newPixels = true;
      imageSource = null;
      rgbRaster = null;
      rgbImage = null;
    }
    cip[currentChannel].setMinAndMax(ip.getMin(), ip.getMax());
    if (singleChannel && nChannels <= 3) {
      switch (currentChannel) {
        case 0:
          cip[0].updateComposite(rgbPixels, 1);
          break;
        case 1:
          cip[1].updateComposite(rgbPixels, 2);
          break;
        case 2:
          cip[2].updateComposite(rgbPixels, 3);
          break;
      }
    } else {
      if (cip == null) return;
      if (syncChannels) {
        ImageProcessor ip2 = getProcessor();
        double min = ip2.getMin(), max = ip2.getMax();
        for (int i = 0; i < nChannels; i++) {
          cip[i].setMinAndMax(min, max);
          lut[i].min = min;
          lut[i].max = max;
        }
        syncChannels = false;
      }
      if (active[0]) cip[0].updateComposite(rgbPixels, 4);
      else {
        for (int i = 1; i < imageSize; i++) rgbPixels[i] = 0;
      }
      if (cip == null || nChannels > cip.length) return;
      for (int i = 1; i < nChannels; i++) if (active[i]) cip[i].updateComposite(rgbPixels, 5);
    }
    if (IJ.isJava16()) createBufferedImage();
    else createImage();
    if (img == null && awtImage != null) img = awtImage;
    singleChannel = false;
  }