function points = poor_linear_warp(points, lo, hi, ref_lo, ref_hi, image_width, error)
% POOR_LINEAR_WARP: Perform a poor linear warp.
%
% Code written by Katherine Smith, 2003
%
%    GENERAL
%
%      new_images = average_smooth(images, window)
%
%    INPUT/S
%
%      -points:
%           The points before warping
%
%      -lo:
%           ?
%
%      -hi:
%           ?
%
%      -ref_lo:
%           ?
%
%      -ref_hi:
%           ?
%
%      -image_width:
%           The width of the image to be warped.
%
%      -error:
%           ??
%           
%    OUTPUT/S
%
%      -points:
%           The points after warping
%
%    PENDING WORK
%
%      -Find out about the different inputs
%      -Understand code and add comments.
%
%    KNOWN BUG/S
%
%      -None.
%
%    COMMENT/S
%
%      -Currently not used??
%
%    RELATED FUNCTION/S
%
%      LINEAR_WARP
%
%    ABOUT
%
%      -Created:     November 23rd, 2003
%      -Last update: Novermber 26th, 2003
%      -Revision:    0.0.3
%      -Author:      R. S. Schestowitz, University of Manchester
% ==============================================================


if ((hi - lo)<=0) 
  disp('Invalid values of high and low. Low must be lower than high');
  return; % break out of function
end

% NOTE: identical to simple warp. Perhaps functions should be merged and have an extra
%       argument regarding complexity as in strategy pattern.

% Smith: line up the edge points, do linear stretch in between!
% but get them off by a amount determined by error

ref_lo = floor(ref_lo - error*rand);
ref_hi = ceil(ref_hi + error*rand);

% first section - between 1 and lo
%scale_factor1 = (ref_lo-1)/(lo-1);
scale_factor1 = (lo-1)/(ref_lo-1);
points(:,1:ref_lo) = (points(:,1:ref_lo)-1)*scale_factor1 + 1;

% second section
if(ref_hi-ref_lo ~= 0)
  scale_factor2 = (hi - lo)/(ref_hi-ref_lo);
else
  scale_factor2 = 0;
end
points(:,ref_lo+1:ref_hi-1) = (points(:,ref_lo+1:ref_hi-1) - ref_lo)*scale_factor2 + lo;
%scale_factor2 = (ref_hi - ref_lo)/(hi-lo);
%points(:,lo+1:hi-1) = (points(:,lo+1:hi-1) - lo)*scale_factor2 + ref_lo;

% third section
scale_factor3 = (image_width-hi)/(image_width-ref_hi);
points(:,ref_hi:image_width) = ...
  (points(:,ref_hi:image_width) - ref_hi)*scale_factor3 + hi;
%scale_factor3 = (image_width-ref_hi)/(image_width-hi);
%points(:,hi:image_width) = ...
%  (points(:,hi:image_width) - hi)*scale_factor3 + ref_hi;