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

% DESCRIPTION
% Takes an image and creates a random dot stereogram, assuming you are wearing 3D glasses.
% Controls: x_variation, y_variation, threshold, left/right color screen, scattering

function image = randomdot(image_name);

% Parameters
x_variation = 8;   % horizontal separation between images
y_variation = 2;    % vertical separation between images
threshold = 150;   % threshold below which we can consider an image to have color

% Screens - depends on what kind of glasses you're using
left_screen = [0 1 1];     % cyan
right_screen = [1 0 0];    % red

% Scattering to determine obviousness of the random dots
scattering = 1.4;

% Get image data
image_data = imread(image_name);

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

% Convert the image to grayscale
intensities = color2gray(image_data);           % for threshold purposes

% Make initial assignment to output image
image = zeros(image_height - y_variation, image_width - x_variation, image_colorspace);

% If intensity is below the 'threshold' parameter, leave out the pixel
for current_y = y_variation + 1 : image_height - y_variation,
    for current_x = x_variation + 1 : image_width - x_variation,

        preference = 1;     % balanced evenly
        
        if(intensities(current_y + y_variation, current_x + x_variation) <= threshold)      % left image
            preference = preference * scattering;
        end
        if(intensities(current_y - y_variation, current_x - x_variation) <= threshold)      % right image
            preference = preference / scattering;
        end
        
        if(round(rand(1)*preference) == 0)
            image(current_y - y_variation, current_x - x_variation, 1) = intensities(current_y,current_x) * left_screen(1);
            image(current_y - y_variation, current_x - x_variation, 2) = intensities(current_y,current_x) * left_screen(2);
            image(current_y - y_variation, current_x - x_variation, 3) = intensities(current_y,current_x) * left_screen(3);
        else
            image(current_y - y_variation, current_x - x_variation, 1) = intensities(current_y,current_x) * right_screen(1);
            image(current_y - y_variation, current_x - x_variation, 2) = intensities(current_y,current_x) * right_screen(2);
            image(current_y - y_variation, current_x - x_variation, 3) = intensities(current_y,current_x) * right_screen(3);  
        end
        
    end
end
