#import jeputils
#reload(jeputils)
from jeputils import *


def img_get_int(fname, thres1, thres2, thres3, thres4, header, width, height, depth, x1,y1,x2,y2, bx1,by1,bx2,by2 , filter_median = False, filter_nsigma = 0):    
    reload=False#True
    ret =  call_jep("cpython/image_functions", "img_get_int", [fname,  thres1, thres2, thres3, thres4, header, width, height, depth, x1,y1,x2,y2, bx1,by1,bx2,by2, filter_median, filter_nsigma], reload)
    #return ret
    #TODO: Why I_sum ans I_sum_bgr are strings?
    (I_sum, area_I, thresh1_count, thresh2_count, thresh3_count, thresh4_count, I_sum_bgr, area_bgr) = ret
    return (int(I_sum), area_I, thresh1_count, thresh2_count, thresh3_count, thresh4_count, int(I_sum_bgr), area_bgr) 


tmp_file = get_context().setup.expandPath("{images}/px.img")
threshold1, threshold2, threshold3, threshold4 = 40, 500, 8000, 100000
PIX_COLOR_DEPTH = 32
PIX_XDIM = 487
PIX_YDIM = 195
image_header_length = 0
roi = [0, 0, 486, 194]
bkroi = [0, 0, 486, 194]

def print_output(ret):
    (I_sum, area_I, thresh1_count, thresh2_count, thresh3_count, thresh4_count, I_sum_bgr, area_bgr) = ret
    print "Time=",time.time()-start
    print "I_sum=",I_sum
    print "area_I=",area_I
    print "thresh1_count=",thresh1_count
    print "thresh2_count=",thresh2_count
    print "thresh3_count=",thresh3_count
    print "thresh4_count=",thresh4_count
    print "I_sum_bgr=",I_sum_bgr
    print "area_bgr=",area_bgr


print "------- filter_median = False, filter_nsigma = 0 ------"
start = time.time()
ret = img_get_int(tmp_file, threshold1, threshold2, threshold3, threshold4, \
              image_header_length, PIX_XDIM, PIX_YDIM,PIX_COLOR_DEPTH, \
              roi[0], roi[1], roi[2], roi[3], bkroi[0], bkroi[1], bkroi[2], bkroi[3], \
              filter_median = False, filter_nsigma = 0)
print_output(ret)
print "------ filter_median = True, filter_nsigma = 0 -------"
start = time.time()
ret = img_get_int(tmp_file, threshold1, threshold2, threshold3, threshold4, \
              image_header_length, PIX_XDIM, PIX_YDIM,PIX_COLOR_DEPTH, \
              roi[0], roi[1], roi[2], roi[3], bkroi[0], bkroi[1], bkroi[2], bkroi[3], \
              filter_median = True, filter_nsigma = 0)
print_output(ret)
print "------ filter_median = False, filter_nsigma = 30 -------"
start = time.time()
ret = img_get_int(tmp_file, threshold1, threshold2, threshold3, threshold4, \
              image_header_length, PIX_XDIM, PIX_YDIM,PIX_COLOR_DEPTH, \
              roi[0], roi[1], roi[2], roi[3], bkroi[0], bkroi[1], bkroi[2], bkroi[3], \
              filter_median = False, filter_nsigma = 30)
print_output(ret)
print "-------------"