#! /usr/bin/env python
# -*- python -*-

####################################################################################################
# 
# PyDvi - A Python Library to Process DVI Stream
# Copyright (C) 2014 Fabrice Salvaire
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# 
####################################################################################################

####################################################################################################

import argparse

import numpy as np

from PIL import Image

####################################################################################################
#
# Options
#

parser = argparse.ArgumentParser(description='Diff PNG image.')

parser.add_argument('png_file1', metavar='FILE1.png',
                    help='PNG File 1')

parser.add_argument('png_file2', metavar='FILE2.png',
                    help='PNG File 2')

parser.add_argument('diff_file', metavar='DIFF.png',
                    help='Diff PNG File')

args = parser.parse_args()

####################################################################################################

image1 = Image.open(args.png_file1)
image2 = Image.open(args.png_file2)

buffer1 = np.array(image1)
buffer2 = np.array(image2)

dimensions1 = buffer1.shape
dimensions2 = buffer2.shape

if dimensions1 != dimensions2:
    print "Shape mismatch {} versus {}".format(dimensions1, dimensions2)
    height1, width1, depth1 = dimensions1
    height2, width2, depth2 = dimensions2
    if depth1 != depth2:
        raise NameError("Depth don't match")
    depth = depth1
    height = min(height1, height2)
    width = min(width1, width2)
    buffer1_ = buffer1
    buffer2_ = buffer2
    buffer1 = np.zeros((height, width, depth), dtype=np.uint8)
    buffer2 = np.zeros((height, width, depth), dtype=np.uint8)
    buffer1 = buffer1_[:height,:width]
    buffer2 = buffer2_[:height,:width]
else:
    height, width, depth = dimensions1

diff_buffer = np.zeros((height, width, depth),  dtype=np.uint8)
diff_buffer[:,:,0] = (buffer1 - buffer2)[:,:,0]
diff_buffer[:,:,1] = (buffer2 - buffer1)[:,:,1]
# diff_buffer = np.abs(buffer1 - buffer2)
# diff_buffer = np.xor(buffer1, buffer2)

mode = 'RGB'
size = width, height
diff_image = Image.frombuffer(mode, size, diff_buffer, 'raw', mode, 0, 1)
diff_image.save(args.diff_file)

####################################################################################################
#
# End
#
####################################################################################################
