'''BlendedWarps.py Warp an image, blending the effects of multiple line-segment pairs that are controlling the warp. Blending is based on the Beier-Neely method of weighting the displacements according to a point's distance from each control segment. ''' from MorphMapper import MorphMapper import SetOfSegmentPairs #from JythonPixelMathInterface import pmGetPixel, pmSetPixel,\ # pmGetImageWidth, pmGetImageHeight def warp(methodToGetAPixel, methodToPutAPixel, w, h, ssp): # Use one line segment set to warp the source image. # w is the width of the images. # h is the height of the images. # First create a mapper for each line segment pair. mappers = [MorphMapper(lsp.dSeg.P, lsp.dSeg.Q, lsp.sSeg.P, lsp.sSeg.Q) for lsp in ssp.pairs] # Iterate to generate all the pixels of the target image. for yd in range(h): for xd in range(w): sum_x_displ = 0.0 sum_y_displ = 0.0 sum_weights = 0.0 for m in mappers: (xs,ys,dist,weight)=m(xd,yd) sum_x_displ += weight*(xs - xd) sum_y_displ += weight*(ys - yd) sum_weights += weight if sum_weights==0: sum_weights = 1 net_x_displ = sum_x_displ / sum_weights net_y_displ = sum_y_displ / sum_weights net_xs = int(xd + net_x_displ) net_ys = int(yd + net_y_displ) # Now fetch the source pixel data and insert in dest. if net_xs >= 0 and net_xs < w and net_ys >= 0 and net_ys < h: px = methodToGetAPixel(net_xs, net_ys) else: px = (127,127,127) # default value for out of range coords. methodToPutAPixel(xd, yd, px[0], px[1], px[2]) #return new_win