All Downloads are FREE. Search and download functionalities are using the official Maven repository.

uk.ac.sussex.gdsc.smlm.results.MemoryImageSource Maven / Gradle / Ivy

Go to download

Genome Damage and Stability Centre SMLM Package Software for single molecule localisation microscopy (SMLM)

The newest version!
/*-
 * #%L
 * Genome Damage and Stability Centre SMLM Package
 *
 * Software for single molecule localisation microscopy (SMLM)
 * %%
 * Copyright (C) 2011 - 2023 Alex Herbert
 * %%
 * 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 3 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, see
 * .
 * #L%
 */

package uk.ac.sussex.gdsc.smlm.results;

import com.thoughtworks.xstream.annotations.XStreamOmitField;
import uk.ac.sussex.gdsc.core.annotation.Nullable;
import uk.ac.sussex.gdsc.core.utils.ValidationUtils;

/**
 * Represent a named results source providing data from memory.
 */
public class MemoryImageSource extends ImageSource {
  @XStreamOmitField
  private int counter;
  private float[][] data;
  private boolean freeMemoryOnClose;

  /**
   * Create a new image source.
   *
   * @param width the width
   * @param height the height
   * @param data the data
   * @throws IllegalArgumentException If the image dimensions are invalid
   */
  public MemoryImageSource(int width, int height, float[]... data) {
    this(0, 0, width, height, data);
  }

  /**
   * Create a new image source.
   *
   * @param xOrigin the x origin
   * @param yOrigin the y origin
   * @param width the width
   * @param height the height
   * @param data the data
   * @throws IllegalArgumentException If the image dimensions are invalid
   */
  public MemoryImageSource(int xOrigin, int yOrigin, int width, int height, float[]... data) {
    super("Memory");
    ValidationUtils.checkStrictlyPositive(width, "width");
    ValidationUtils.checkStrictlyPositive(height, "height");
    ValidationUtils.checkNotNull(data, "Image data must not be null");
    final int length = width * height;
    for (final float[] f : data) {
      ValidationUtils.checkNotNull(f, "Image data must not be null");
      if (f.length != length) {
        throw new IllegalArgumentException("Image data length does not match width*height");
      }
    }
    this.xOrigin = xOrigin;
    this.yOrigin = yOrigin;
    this.width = width;
    this.height = height;
    this.data = data;
    this.frames = data.length;
  }

  @Override
  protected boolean openSource() {
    return true;
  }

  @Override
  protected void closeSource() {
    // Free the memory
    if (freeMemoryOnClose) {
      data = new float[0][0];
    }
  }

  @Override
  protected boolean initialiseSequentialRead() {
    counter = 0;
    return true;
  }

  @Override
  protected @Nullable float[] nextRawFrame() {
    if (counter < data.length) {
      return getRawFrame(++counter);
    }
    return null;
  }

  @Override
  protected @Nullable float[] getRawFrame(int frame) {
    if (frame > 0 && frame <= data.length) {
      return data[frame - 1];
    }
    return null;
  }

  @Override
  public boolean isValid(int frame) {
    return (frame > 0 && frame <= data.length);
  }

  /**
   * Checks if freeing the memory on calling {@link #close()}.
   *
   * @return Set to true if freeing the memory on calling {@link #close()}
   */
  public boolean isFreeMemoryOnClose() {
    return freeMemoryOnClose;
  }

  /**
   * Set this to true to free the memory when the {@link #close()} method is called. No subsequent
   * calls to {@link #open()} will be valid.
   *
   * @param freeMemoryOnClose Set to true to free the memory on calling {@link #close()}
   */
  public void setFreeMemoryOnClose(boolean freeMemoryOnClose) {
    this.freeMemoryOnClose = freeMemoryOnClose;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy