function main()
{
    $is_error = false;
    //no errors yet . . .
    $continue = false;
    //continue on to next image in directory?
    // set directory of images to work in
    if (isset($_GET['directory']) && isset($_GET['action'])) {
        $directory = $_GET['directory'];
        $action = $_GET['action'];
    } else {
        // if no directory set, then display form to set the directory
        return <<<OUTPUT
<form action="" method="GET">
\tDirectory: <input type="text" name="directory">
\t<input type="radio" name="action" value="difference_series">Difference series
\t<input type="radio" name="action" value="average">Average
\t<input type="radio" name="action" value="threshold">Threshold
\t<input type="radio" name="action" value="to_data">To Data
\t<input type="radio" name="action" value="image_to_hough">To Hough Transform
\t<input type="submit" value="GO">
</form>
OUTPUT;
    }
    // set number of file in directory to start on
    if (isset($_GET['number'])) {
        $number = $_GET['number'];
    } else {
        $number = 0;
    }
    // start at the first file
    //compile list of files in directory
    $file_list = collect_files($directory);
    if (count($file_list) == 0) {
        return "Directory {$directory} is empty.";
    }
    //couldn't find anything
    $output = '';
    switch ($action) {
        case 'to_data':
            if (!isset($file_list[$number])) {
                $output .= 'Finished!';
                //all done!
                break;
            }
            //if
            $image = imagecreatefrompng($directory . $file_list[$number]);
            $data = image_to_data($image);
            break;
        case 'difference_series':
            if (!isset($file_list[$number]) || !isset($file_list[$number + 1])) {
                $output .= 'No more files to compare.';
                //all done!
                break;
            }
            //if
            $a = imagecreatefromjpeg($directory . $file_list[$number]);
            $b = imagecreatefromjpeg($directory . $file_list[$number + 1]);
            if (!($image = image_combine($a, $b, 'difference'))) {
                $output .= 'ERROR trying to combine images.';
                $is_error = true;
                break;
            }
            //if
            write_image($image, 'images/output/', '(' . $file_list[$number] . ')-(' . $file_list[$number + 1] . ')', 'png');
            $continue = true;
            break;
        case 'image_to_hough':
            if (!isset($file_list[$number])) {
                $output .= 'No more files.';
                //done!
                break;
            }
            //if
            $image = imagecreatefrompng($directory . $file_list[$number]);
            $image = image_to_hough($image);
            write_image($image, 'images/output/', "hough({$file_list[$number]})", 'png');
            $continue = true;
            break;
        case 'average':
            if (!isset($file_list[$number])) {
                $output .= 'No more files to add.';
                //all done!
                break;
            }
            //if
            $temp_path = 'images/temp/';
            $temp_filename = 'temp';
            //$image = imagecreatefromjpeg($directory . $file_list[$number]);
            $image = imagecreatefrompng($directory . $file_list[$number]);
            if (file_exists($temp_path . $temp_filename . '.png')) {
                $temp = imagecreatefrompng($temp_path . $temp_filename . '.png');
            } else {
                $temp = imagecreatetruecolor(imagesx($image), imagesy($image));
            }
            $num_in_series = count($file_list);
            $output .= "adding 1/{$num_in_series}th of image to temp.png";
            $constant = 1.0 / $num_in_series;
            $image = image_apply_constant($image, $constant, 'multiply');
            $image = image_combine($temp, $image, 'sum');
            write_image($image, $temp_path, $temp_filename, 'png');
            $continue = true;
            break;
        case 'sum_series':
            break;
        case 'threshold':
            if (!isset($file_list[$number])) {
                $output .= 'No more files.';
                //all done!
                break;
            }
            //if
            $image = imagecreatefrompng($directory . $file_list[$number]);
            $image = image_apply_constant($image, 34, 'threshold');
            write_image($image, 'images/output/', "thresh({$file_list[$number]})", 'png');
            $continue = true;
            break;
    }
    //switch
    $next = $number + 1;
    if ($is_error) {
        $output .= <<<OUTPUT
<br>Ohhh crap, there was an error performing the action {$action} on directory {$directory}<br><br>

Try next pair? <a href="?directory={$directory}&action={$action}&number={$next}">Sure.</a>
OUTPUT;
        $continue = false;
    } else {
        $output .= <<<OUTPUT
Success.<br>
OUTPUT;
    }
    //if
    if ($continue) {
        $output .= <<<OUTPUT
Loading next image . . .
<meta http-equiv="refresh" content="0;?number={$next}&action={$action}&directory={$directory}">\t\t
OUTPUT;
    }
    //if
    return $output;
}
function main()
{
    $is_error = false;
    //no errors yet . . .
    $continue = false;
    //continue on to next image in directory?
    if (isset($_GET['directory'])) {
        $directory = $_GET['directory'];
        if (substr($directory, -1) != '/') {
            $directory = $directory . '/';
        }
        //add trailing slash
        show_directory($directory);
    }
    // set directory of images to work in
    if (isset($_GET['directory']) && isset($_GET['action'])) {
        $action = $_GET['action'];
    } else {
        // if no directory set, then display form to set the directory
        return;
    }
    echo "performing {$action} on directory {$directory}<br />";
    $threshold = $_GET['threshold'];
    // set number of file in directory to start on
    if (isset($_GET['number'])) {
        $number = $_GET['number'];
    } else {
        $number = 0;
    }
    // start at the first file
    //compile list of files in directory
    $file_list = collect_files($directory);
    if (count($file_list) == 0) {
        return "Directory {$directory} is empty.";
    }
    //couldn't find anything
    $output_dir = 'images/output/';
    $output = '';
    $image_name = '';
    switch ($action) {
        case 'to_data':
            if (!isset($file_list[$number])) {
                $output .= 'Finished!';
                //all done!
                break;
            }
            //if
            $image = file_to_image($directory . $file_list[$number]);
            $data = image_to_data($image);
            break;
        case 'difference_series':
            if (!isset($file_list[$number]) || !isset($file_list[$number + 1])) {
                $output .= 'No more files to compare.';
                //all done!
                break;
            }
            //if
            $a = file_to_image($directory . $file_list[$number]);
            $b = file_to_image($directory . $file_list[$number + 1]);
            if (!($image = image_combine($a, $b, 'difference'))) {
                $output .= 'ERROR trying to combine images ' . $file_list[$number] . ' and ' . $file_list[$number + 1];
                $is_error = true;
                break;
            }
            //if
            $file_name = '(' . $file_list[$number] . ')-(' . $file_list[$number + 1] . ')';
            $image_name = $file_name . '.png';
            write_image($image, $output_dir, $file_name, 'png');
            $continue = true;
            break;
        case 'image_to_hough':
            if (!isset($file_list[$number])) {
                $output .= 'No more files.';
                //done!
                break;
            }
            //if
            if (!($image = file_to_image($directory . $file_list[$number]))) {
                return "could not create image from {$directory}{$file_list[$number]}";
            }
            if (isset($_GET['threshold'])) {
                $threshold = $_GET['threshold'];
            } else {
                $threshold = 128;
            }
            echo '<img src="' . $directory . $file_list[$number] . '" />';
            $image = image_to_hough($image, $threshold);
            $file_name = "hough({$file_list[$number]})";
            $image_name = $file_name . '.png';
            write_image($image, $output_dir, $file_name, 'png');
            echo hough_to_image($output_dir . $file_name . '.png');
            $continue = true;
            break;
        case 'average':
            if (!isset($file_list[$number])) {
                $output .= 'No more files to add.';
                //all done!
                break;
            }
            //if
            $temp_path = 'images/temp/';
            $temp_filename = 'temp';
            //$image = imagecreatefromjpeg($directory . $file_list[$number]);
            if (!file_exists($directory . $file_list[$number])) {
                return "file {$directory}{$file_list[$number]} does not seem to exist.<br />";
            }
            if (!($image = file_to_image($directory . $file_list[$number]))) {
                return "{$directory}{$file_list[$number]} is not an image file<br />";
            }
            if (file_exists($temp_path . $temp_filename . '.png')) {
                echo "temp file found in {$temp_path}{$temp_filename}.png<br />";
                if ($number == 0) {
                    //there shouldn't be a temp file
                    echo "temp file is from previous batch: deleting . . . <br />";
                    if (!unlink($temp_path . $temp_filename . '.png')) {
                        echo "error: could not delete file {$temp_path}{$temp_filename}";
                    }
                }
                $temp = file_to_image($temp_path . $temp_filename . '.png');
            } else {
                echo "{$temp_path}{$temp_filename}.png does not exist, creating . . . <br />";
                if (!($temp = imagecreatetruecolor(imagesx($image), imagesy($image)))) {
                    return 'could not create temporary image';
                }
            }
            $num_in_series = count($file_list);
            $output .= "adding the {$number}th 1/{$num_in_series}th of image to temp.png";
            $constant = 1.0 / $num_in_series;
            $image = image_apply_constant($image, $constant, 'multiply');
            $image = image_combine($temp, $image, 'sum');
            write_image($image, $temp_path, $temp_filename, 'png');
            $continue = true;
            break;
        case 'sum_series':
            break;
        case 'threshold':
            if (!isset($file_list[$number])) {
                $output .= 'No more files.';
                //all done!
                break;
            }
            //if
            if (isset($_GET['threshold'])) {
                $threshold = $_GET['threshold'];
            } else {
                $threshold = 34;
            }
            $image = file_to_image($directory . $file_list[$number]);
            $image = image_apply_constant($image, $threshold, 'threshold');
            $image_name = 'thresh(' . $file_list[$number] . ').png';
            write_image($image, $output_dir, "thresh({$file_list[$number]})", 'png');
            $continue = true;
            break;
    }
    //switch
    $next = $number + 1;
    if ($is_error) {
        $output .= <<<OUTPUT
<br>Ohhh crap, there was an error performing the action {$action} on directory {$directory}<br><br>

Try next pair? <a href="?directory={$directory}&action={$action}&number={$next}">Sure.</a>
OUTPUT;
        $continue = false;
    } else {
        $output .= <<<OUTPUT
Success.<br>

<img src="{$output_dir}{$image_name}" />
OUTPUT;
    }
    //if
    if ($continue) {
        $output .= <<<OUTPUT
<a href="?number={$next}&action={$action}&directory={$directory}&threshold={$threshold}">continue</a><br />\t
OUTPUT;
    }
    //if
    return $output;
}