#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import base64
import cStringIO

from flask import Blueprint
from flask import jsonify, send_file, current_app

import utils
import config

from NIST.fingerprint import NISTf_auto

nist_view = Blueprint( "nist", __name__, template_folder = "templates" )

def cnm_candidate_get_image( cnm_uuid, file_uuid, fpc ):
    sql = """
        SELECT data
        FROM cnm_candidate
        WHERE
            cnm_result = %s AND
            uuid = %s
    """
    data = config.db.query_fetchone( sql, ( cnm_uuid, file_uuid, ) )[ "data" ]
    
    img = data2img( data, fpc )
    img.thumbnail( ( 800, 800 ) )
    return img

def data2img( data, fpc ):
    data = base64.decodestring( data )
    buffer = cStringIO.StringIO()
    buffer.write( data )
    
    n = NISTf_auto( buffer )
    
    fpc = int( fpc )
    if fpc == 0:
        fpc = n.get_fpc_list()[ 0 ]
    
    return n.get_image( fpc = fpc )

