/* ----------------------------------------
 * Origin  : GFI INFORMATIQUE 
 * Project : CDPP Propagation Tool 
 * ----------------------------------------
 * History 
 * Version : 01/05/2015 : SCA : V2.1
 * Version : 06/11/2017 : SCA : V2.2.1 
 *  Redmine #5403 - DMPT3 
 * ----------------------------------------
 */
package fr.tlse.irap.pt.client.controller.tools.common;

import java.awt.Color;
import java.awt.image.RenderedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

public class FXImageHelper {

	/**
	 * Create a java FX image from an array of bytes.
	 * @param witdh of the image
	 * @param height of the image
	 * @param colorMap used for colorized
	 */
	public static javafx.scene.image.Image createImage(byte[] pixelValues, int witdh, int height, Color[] colorMap) throws IOException {
		//System.out.println("CreateImage - size pixelValues=" + pixelValues.length + " - witdh=" + witdh + " - height=" + height);
		
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		
		ImageIO.write((RenderedImage) ImageHelper.createBufferedImage(pixelValues, witdh, height, colorMap), "png", out);
		out.flush();
		//System.out.println("CreateImage - size of out=" + out.size());
		ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
		
		return new javafx.scene.image.Image(in);
	}
	
	/**
	 * Create a java FX image from an array of floats.
	 * @param contrast in ]0, 10]
	 * @param witdh of the image
	 * @param height of the image
	 * @param colorMap used for colorized
	 */
	public static javafx.scene.image.Image createImage(float[] pixelValues, int width, int height, int contrast, Color[] colorMap) throws IOException {
		//System.out.println("CreateImage - size pixelValues=" + pixelValues.length + " - witdh=" + witdh + " - height=" + height);
		
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		
		ImageIO.write((RenderedImage) ImageHelper.createBufferedImage(pixelValues, width, height, contrast, colorMap), "png", out);
		out.flush();
		//System.out.println("CreateImage - size of out=" + out.size());
		ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
		
		return new javafx.scene.image.Image(in);
	}

}
