function stitched = stitch_channel(filenames, X, Y, channel)

% makes the assumption that all the images are the same size
image_count = size(filenames, 2);
single_size = size(imread(filenames{1}));
width = single_size(2)
height = single_size(1)
overlaps = width - X(1:image_count - 1);
Y = zeros(size(X));


total_width = width * image_count;

%mask = zeros(image_count, height, width);

stitched = zeros(height, total_width);
x=1;

% compute the weight masks for all the images
for i=1:image_count
    mask = ones(height, width);
    
    % take care of any right edge feathering that
    % needs to be done
    if i < image_count
       v = height - abs(Y(i)); 
       c1=width-overlaps(1,i)+1;
       c2=width;
       if Y(i) <= 0
           r1=-Y(i)+1;
           r2=height;
       else
           r1 = 1;
           r2 = v;
       end
       mask(r1:r2, c1:c2) = mask(r1:r2, c1:c2) .* ...
           (ones(v, 1) * [1:-1/(overlaps(i)-1):0]);
    end
    
    % take care of the left edge in a similar manner
    if i > 1
       v = height - abs(Y(i-1));
       c1=1;
       c2=overlaps(1,i-1);
       if Y(i-1) >= 0
           r1=Y(i-1)+1;
           r2=height;
       else
           r1 = 1;
           r2 = v;
       end
       mask(r1:r2, c1:c2) = mask(r1:r2, c1:c2) .* ...
           (ones(v, 1) * [0:1/(overlaps(i)-1):1]);

    end
    
    img = imread(filenames{i});
    this_width = width - X(i);
    x:(x+this_width);
    
    stitched(1:height, x:x+width-1) = ...
        stitched(1:height, x:x+width-1) + ...
        mask .* double(img(:,:,channel));     
        
    x = x + this_width;
end

stitched = stitched(1:height,1:x);

