'''
File main_single_image.py
Created on 21 nov. 2011
@author: Antoine Vacavant, ISIT lab, antoine.vacavant_AT_iut.u-clermont1.fr, http://isit.u-clermont1.fr/~anvacava
'''
'''
Copyright (c) Nov. 21 2011 Antoine VACAVANT

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
'''

#Possible use of matplotlib from http://http://matplotlib.sourceforge.net/
from pylab import *
import matplotlib.pyplot as plt

#More imports
import Image
import numpy
import ImageOps

#ssim.py to compute SSIM
import ssim

'''
Get 2D matrix from an image file, possibly displayed with matplotlib
@param path: Image file path on HD
@return A 2D matrix
'''
def build_mat_from_grayscale_image(path):
    img=Image.open(str(path))
    img=ImageOps.grayscale(img)
    imgData=img.getdata()
    imgTab=numpy.array(imgData)
    w,h=img.size
    imgMat=numpy.reshape(imgTab,(h,w))
    
    return imgMat

'''
Main program
'''
if __name__ == '__main__':

    #First image
    imgRefMat=build_mat_from_grayscale_image("einstein.gif")
    (w,h) = (imgRefMat.shape[0],imgRefMat.shape[1])
    
    #First subplot
    figure()
    subplot(121)
    plt.imshow(imgRefMat, cmap=cm.gray, hold=True)
    
    #Second image
    imgOutMat=build_mat_from_grayscale_image("einstein-noise-it1.png")
    
    #Second subplot
    subplot(122)
    plt.imshow(imgOutMat, cmap=cm.gray, hold=True)
    plt.show()
    
    #Compute SSIM
    cSSIM=ssim.compute_ssim(imgRefMat,imgOutMat)
    
    print "SSIM=", cSSIM