import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JLabel;

/**
 * Used for making JNI calls to C code that interfaces with OpenCV and the face finding algorithms we've developed
 */
public class OpenCV {
	private FaceRec rec; 
	
	native void openCamera();
	native void getImage(int[] pixels); 
	native int getImageWidth(); 
	native int getImageHeight(); 
	
	static {
	    System.loadLibrary("FaceFindLib");
	}
	
	public void attachFR(FaceRec rec) {
		this.rec = rec; 
	}
		
	public static BufferedImage pixelsToBufferedImage(int template[]) {
		int templateSize = (int) java.lang.Math.sqrt(template.length); 
		
		BufferedImage img = new BufferedImage(templateSize, templateSize, BufferedImage.TYPE_INT_ARGB); 
		int pixels[] = new int[templateSize*templateSize]; 
		pixels = img.getRGB(0, 0, templateSize, templateSize, pixels, 0, templateSize); 
		int curPixVal; 
		for (int i=0; i<templateSize*templateSize; i++) {
			curPixVal = template[i]&0xff; 
			pixels[i] = (curPixVal<<16) | (curPixVal<<8) | (curPixVal) | (0xff<<24); 
		}
		img.setRGB(0, 0, templateSize, templateSize, pixels, 0, templateSize); 
		return img; 
		
	}
	public void newTemplate(int personNum, int []template) {
		rec.addNewTemplate(personNum, template, pixelsToBufferedImage(template)); 
	}
	
	public void updateLastTemplate(int personNum, int[]template, int correlation) {
		rec.updateLastTemplate(personNum, template,  pixelsToBufferedImage(template), correlation); 
	}
}
