% Psych 221 Final Project
% Stereo Imaging
% AJ Minich, Ajminich@stanford.edu
% Winter 2008

% DESCRIPTION
% Takes an image and a background, creates an anaglyph of the image, and superimposes
% it over the background by centering. Viewing will require 3D glasses.

function image_3D = twolayer(image_name,background_name);

% Parameters
x_variation = 10;   % horizontal separation between images
y_variation = 2;    % vertical separation between images
threshold = 250;   % threshold below which we can consider an image to have color
left_screen = [0 1 1];  % cyan
right_screen = [1 0 0];     % red

scattering = .5;     % rough distance between pixels in the separated image

% Get image data
image_data = imread(image_name);
background_data = imread(background_name);

% Extract dimensions
image_dimensions = size(image_data);
image_height = image_dimensions(1);
image_width = image_dimensions(2);
image_colorspace = image_dimensions(3);

background_dimensions = size(background_data);
background_height = background_dimensions(1);
background_width = background_dimensions(2);
background_colorspace = background_dimensions(3);

% Convert the image to grayscale
grayscale_image = color2gray(image_data);
background_bw = color2shadow(background_data);

% Make initial assignment to output image
image_3D = background_bw;

% Determine separated image top left corners
left_x_start = (background_width - image_width)/2 - x_variation;
left_y_start = (background_height - image_height)/2 - y_variation;

right_x_start = (background_width - image_width)/2 + x_variation;
right_y_start = (background_height - image_height)/2 + y_variation;

overlay = zeros(background_height, background_width, background_colorspace);

% For each pixel, determine whether to use background or use conversion
% separation for the foreground image
for current_y = 1:background_height,
    for current_x = 1:background_width,

        pixel_replace = [0 0 0];
        
        % Left image
        if((current_x > left_x_start) && (current_x <= left_x_start + image_width) && (current_y > left_y_start) && (current_y <= left_y_start + image_height))
            if(grayscale_image(current_y - left_y_start, current_x - left_x_start) <= threshold)
                if(round(rand(1)*scattering) == 0)
                    pixel_replace(1) = pixel_replace(1) + grayscale_image(current_y - left_y_start, current_x - left_x_start) * left_screen(1);
                    pixel_replace(2) = pixel_replace(2) + grayscale_image(current_y - left_y_start, current_x - left_x_start) * left_screen(2);
                    pixel_replace(3) = pixel_replace(3) + grayscale_image(current_y - left_y_start, current_x - left_x_start) * left_screen(3);
                end
            end
        end
        
        % Right image
        if((current_x > right_x_start) && (current_x <= right_x_start + image_width) && (current_y > right_y_start) && (current_y <= right_y_start + image_height))
            if(grayscale_image(current_y - right_y_start, current_x - right_x_start) <= threshold)
                if(round(rand(1)*scattering) == 0)
                    pixel_replace(1) = pixel_replace(1) + grayscale_image(current_y - right_y_start, current_x - right_x_start) * right_screen(1);
                    pixel_replace(2) = pixel_replace(2) + grayscale_image(current_y - right_y_start, current_x - right_x_start) * right_screen(2);
                    pixel_replace(3) = pixel_replace(3) + grayscale_image(current_y - right_y_start, current_x - right_x_start) * right_screen(3);
                end
            end
        end
        
        overlay(current_y, current_x, 1) = pixel_replace(1)/255;
        overlay(current_y, current_x, 2) = pixel_replace(2)/255;
        overlay(current_y, current_x, 3) = pixel_replace(3)/255;        
        
        if(pixel_replace(1:3) == [0 0 0]) else
            image_3D(current_y, current_x, 1) = pixel_replace(1)/255;
            image_3D(current_y, current_x, 2) = pixel_replace(2)/255;
            image_3D(current_y, current_x, 3) = pixel_replace(3)/255;
        end
    end
end

image_3D = overlay;