/*
 *  image_handling.h
 *
 */

/*
 * We STILL NEED to RELEASE the old data to avoid memory leaks!
 *
 * This is only a major problem if we're switching sizes all the time...
 */

inline void ensure_image_size_hw (IplImage*& I,       // the image whose size we want to ensure matches the values below
                                  int desired_height, // num rows or max y
                                  int desired_width,  // num cols or max x
                                  int desired_depth)  // num color channels, either 3 for rgb or 1 for gs grayscale/binary
{
    if (   I == NULL                     // there is no image yet
        || I->width  != desired_width    // the width is wrong
        || I->height != desired_height)  // the height is wrong
    {
        I = cvCreateImage (cvSize (desired_width, desired_height), IPL_DEPTH_8U, desired_depth);
    }
}

inline void ensure_image_size (IplImage*& I,       // the image whose size we want to ensure matches the values below
                               IplImage* matching_image,
                               int desired_depth)  // num color channels, either 3 for rgb or 1 for gs grayscale/binary
{
    if (matching_image == NULL)
    {
        printf("Image to match in ensure_image_size was NULL. Doing nothing.\n");
        return;
    }
    int desired_height = matching_image->height;
    int desired_width = matching_image->width;
    ensure_image_size_hw (I, desired_height, desired_width, desired_depth);   // call function above
}




