package imseProc.jotter;

import java.io.IOException;

import org.eclipse.swt.widgets.Composite;

import binaryMatrixFile.BinaryMatrixFile;

import imseProc.core.DoubleFlatArrayImage;
import imseProc.core.ImagePipeController;
import imseProc.core.Img;
import imseProc.core.ImgSource;

import imseProc.core.ImgPipe;

public class JotterSource extends JotterPipe implements ImgSource {
	
	private DoubleFlatArrayImage sourceImg;
	private int pulse;
	
	@Override
	public ImgSource clone() { return new JotterSource(); }

	@Override
	public Img getImage(int imgIdx) {
		return (imgIdx == 0) ? sourceImg : null;
	}
	
	@Override
	public Img[] getImageSet() {
		return (sourceImg != null) ? new Img[]{ sourceImg } : null;
	}

	@Override
	public int getNumImages() {
		return (sourceImg != null) ? 1 : 0;		
	}
	
	public void loadImage(int pulse){
		this.pulse = pulse;
		String fileName = notesDB.getPath() + "/img" + pulse + ".bin";
		try{
			double vals[][] = BinaryMatrixFile.load(fileName, false);
			int nY = vals.length;
			int nX = vals[0].length;
			sourceImg = new DoubleFlatArrayImage(this, 0, nX, nY);
			for(int y=0;y < sourceImg.getHeight(); y++){
				for(int x=0; x < sourceImg.getWidth(); x++){
					sourceImg.setPixelValue(x, y, vals[y][x]);
				}
			}
			notifyImageSetChanged();

		}catch(IOException err){
			System.err.println("ERROR reading '"+fileName+"': " + err.getMessage());
			sourceImg = null;
		}
	}
	
	public int getCurrentPulse(){ return pulse; }
	
	public boolean isIdle() { return false; };
}
