/* Standard headers */
#include <stdio.h>
#include <stdlib.h>

/* Required OpenCV headers */
#include <cv.h>                 /* Contains all cv* function calls */
#include <highgui.h>            /* Contains all cvv* 'helper' functions */

/* Notes:
 - /usr/local/include/opencv/cvtypes.h contains all Cv* structs
 - CvArr* is just an IplImage*.
 - Load the same image twice into 2 different IplImage structs because
   some functions have source (src) and destination (dest) images passed
   in. By loading the same image twice, you guarantee the 2 buffers are
   exactly the same size!
*/

int main (int argc, char* argv[]){

    IplImage* image1 = NULL;
    IplImage* image2 = NULL;
    CvPoint pt1, pt2;

    /* PGM files (Grayscale images) */
    /* ========================================================= */

    /* Load the file */
    image1 = cvvLoadImage("test1.pgm");
    if (!image1) {
        printf("Could not load test1.pgm");
        exit(0);
    }

    /* Load the same PPM file so we have a working buffer of the
       same size */
    image2 = cvvLoadImage("test1.pgm");
    if (!image2) {
        printf("Could not load test1.pgm");
        exit(0);
    }

    /* Draw a rectangle */
    pt1.x = 20;
    pt1.y = 20;
    pt2.x = image1->width-pt1.x;
    pt2.y = image1->height-pt1.y;
    cvRectangle(image1, pt1, pt2, 0, 5);

    /* Draw a circle */
    pt1.x = 127;
    pt1.y = 127;
    cvCircle(image1, pt1, 50, 100, 3);

    /* Threshold our image */
    /* NOTE: threshold MUST be on grayscale images (.pgm) */
    cvThreshold(image1, image2, 127, 255, CV_THRESH_TOZERO);

    /* Save our image */
    cvvSaveImage("out_test1.pgm", image2);

    /* Free the image header and data */
    cvReleaseImage(&image1);
    cvReleaseImage(&image2);

    return 0;
}
