# Copyright 2015 Myriam Johnson
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#     http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

'''A program for playing with texture expansion. 

Methods:
compare -- find (square of) colour-space distance between two pixels
compareRegion -- find weighted sum of colour-space distances between all
    pixels in two texture regions
expand -- expand one texture into another

Author: mym
'''

from __future__ import print_function
#from math import sqrt
import texture
#import random

def compare(pix1, pix2):
    '''Compare two pixels, returning square of the colour-space distance between
    
    Arguments:
    pix1 -- tuple containing the channels of the first pixel
    pix2 -- tuple containing the channels of the second pixel
    
    Return: square of colour space distance
    
    Preconditions: both pixels have the same number of channels
    '''
    assert len(pix1) == len(pix2)
    collect = 0
    for pair in zip(pix1, pix2):
        collect += (pair[0] - pair[1])**2
    return collect

def compareRegion(tex1, tex2, cen1, cen2, region):
    '''Compare regions of two Textures.
    Returns the weighted sum of colour-space distances between corresponding
    pixels, or Infinity if no pixels can be compared.
    
    Arguments:
    tex1, tex2 -- Textures to compare
    cen1, cen2 -- 2-tuple centres of comparison regions
    region -- list of 2-tuple shifts defining points for comparison
    
    Returns: floating-point weighted sum of distances
    
    Preconditions: region is valid about cen in both textures (untested)
    '''
    # abort if nothing to compare (avoid divide-by-zero)
    if (len(region) == 0): return float('inf')
    
    # loop over shifts
    total = 0
    for shift in region: