/*******************************************************************************
 *  Copyright 2017-2018 Centre for Development of Advanced Computing(C-DAC), Pune
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *   http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
package cdac.medinfo.sdk.dcm30_04.baselibs;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import cdac.medinfo.sdk.dcm30_04.commoninterface.IDicomStream;
import cdac.medinfo.sdk.dcm30_04.dicomstreams.DicomFileStream;


/**
 * This class represent a Frame of a DICOM Image.
 */

public class Frame 
{
	
	
	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 1L;
	
	private File file = null;
	
	private int iFrameLength = 0;
	
	private int iStarringPoint = 0;
	
	/**
	 * Retrieves the length of the frame. 
	 * @return iFrameLength int value for length of a frame.
	 */
	public int getFrameLength() 
	{
		return iFrameLength;
	}
	
	/**
	 * Initializes object of frame.
	 * @param file Object of File.
	 * @param iLenght integer value for length of a DICOM frame.
	 * @param iStartingPoint integer value for length of starting point
	 */
	public Frame(File file, int iLenght, int iStartingPoint)
	{
		this.file = file;
		this.iFrameLength = iLenght;
		this.iStarringPoint = iStartingPoint;
	}
	
	/**
	 * Returns the DICOM image Frame.
	 * @return byte[] byte array of Frame of DICOM image.
	 * @throws IOException
	 */
	public byte[] getFrame() throws IOException
	{
		byte arrByte[] = new byte[iFrameLength];
		
		FileInputStream objFileInputStream = new FileInputStream(file); 
		DicomFileStream objDicomFileStream = new DicomFileStream();
		objDicomFileStream.setInputStream(objFileInputStream);
		
		objDicomFileStream.readBuffer(arrByte);
		
		objDicomFileStream.close();
		
		return arrByte;
	}
	
	/**
	 * Returns the DICOM image Frame.
	 * @param objDicomStream Object of IDicomStream representing frame
	 * @return boolean - true - if frame is converted into DICOM stream, else true.
	 * @throws IOException
	 */
	public boolean getFrame(IDicomStream objDicomStream) throws IOException
	{
		FileInputStream objFileInputStream = new FileInputStream(file);
		DicomFileStream objDicomFileStream = new DicomFileStream();
		objDicomFileStream.setInputStream(objFileInputStream);
		byte arrByte[] = null;
		if(iStarringPoint <= file.length())
		{
			objDicomFileStream.skip(iStarringPoint);

			int iFrameLenght = this.iFrameLength;
			while(iFrameLenght >= 0)
			{
				if(iFrameLenght >= 1024)
					arrByte = new byte[1024];
				else
					arrByte = new byte[iFrameLenght];

				objDicomFileStream.readBuffer(arrByte);
				objDicomStream.writeBuffer(arrByte, 0, arrByte.length);
				iFrameLenght = iFrameLenght - 1024;
			}
			objDicomFileStream.close();
			return true;
		}
		return false;
	}
}
