/*
 *  libpipi       Proper image processing implementation library
 *  Copyright (c) 2004-2008 Sam Hocevar <sam@zoy.org>
 *                All Rights Reserved
 *
 *  $Id$
 *
 *  This library is free software. It comes without any warranty, to
 *  the extent permitted by applicable law. You can redistribute it
 *  and/or modify it under the terms of the Do What The Fuck You Want
 *  To Public License, Version 2, as published by Sam Hocevar. See
 *  http://sam.zoy.org/wtfpl/COPYING for more details.
 */

/*
 * image.c: image I/O functions
 */

#include "config.h"
#include "common.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <cv.h>
#include <highgui.h>

#include "pipi.h"
#include "pipi_internals.h"

pipi_image_t *pipi_load_opencv(const char *name)
{
    pipi_image_t *img;
    IplImage *priv = cvLoadImage(name, -1);

    if(!priv)
        return NULL;

    img = (pipi_image_t *)malloc(sizeof(pipi_image_t));
    img->width = priv->width;
    img->height = priv->height;
    img->pitch = priv->widthStep;
    img->channels = priv->nChannels;
    img->pixels = priv->imageData;
    img->priv = (void *)priv;

    return img;
}

pipi_image_t *pipi_new_opencv(int width, int height)
{
    pipi_image_t *img;
    IplImage *priv = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 3);

    if(!priv)
        return NULL;

    img = (pipi_image_t *)malloc(sizeof(pipi_image_t));
    img->width = priv->width;
    img->height = priv->height;
    img->pitch = priv->widthStep;
    img->channels = priv->nChannels;
    img->pixels = priv->imageData;
    img->priv = (void *)priv;

    return img;
}

void pipi_free_opencv(pipi_image_t *img)
{
    IplImage *iplimg;
    iplimg = (IplImage *)img->priv;
    cvReleaseImage(&iplimg);

    free(img);
}

void pipi_save_opencv(pipi_image_t *img, const char *name)
{
    cvSaveImage(name, img->priv);
}