source: libpipi/trunk/pipi/codec/opencv.c

Last change on this file was 4693, checked in by Sam Hocevar, 10 years ago

Update opencv codec to more recent cvSaveImage prototype.

File size: 2.8 KB
RevLine 
[2261]1/*
[2844]2 *  libpipi       Pathetic image processing interface library
[2261]3 *  Copyright (c) 2004-2008 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id$
7 *
8 *  This library is free software. It comes without any warranty, to
9 *  the extent permitted by applicable law. You can redistribute it
10 *  and/or modify it under the terms of the Do What The Fuck You Want
11 *  To Public License, Version 2, as published by Sam Hocevar. See
12 *  http://sam.zoy.org/wtfpl/COPYING for more details.
13 */
14
15/*
16 * image.c: image I/O functions
17 */
18
19#include "config.h"
20
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24
25#include <cv.h>
26#include <highgui.h>
27
28#include "pipi.h"
29#include "pipi_internals.h"
30
[2629]31/* FIXME: this whole file is broken until we support BGR24 images */
32
[2837]33static int pipi_free_opencv(pipi_image_t *);
34
[2261]35pipi_image_t *pipi_load_opencv(const char *name)
36{
37    pipi_image_t *img;
[2814]38
[2631]39    IplImage *priv = cvLoadImage(name, 1);
[2261]40
41    if(!priv)
42        return NULL;
43
[2631]44    img = pipi_new(priv->width, priv->height);
[2261]45
[3342]46    img->p[PIPI_PIXELS_BGR_U8].pixels = priv->imageData;
47    img->p[PIPI_PIXELS_BGR_U8].w = priv->width;
48    img->p[PIPI_PIXELS_BGR_U8].h = priv->height;
49    img->p[PIPI_PIXELS_BGR_U8].pitch = priv->widthStep;
50    img->p[PIPI_PIXELS_BGR_U8].bpp = 24;
51    img->p[PIPI_PIXELS_BGR_U8].bytes = 3 * img->w * img->h;
52    img->last_modified = PIPI_PIXELS_BGR_U8;
[2605]53
54    img->codec_priv = (void *)priv;
[3342]55    img->codec_format = PIPI_PIXELS_BGR_U8;
[2837]56    img->codec_free = pipi_free_opencv;
[2605]57
[2756]58    img->wrap = 0;
59    img->u8 = 1;
60
[2261]61    return img;
62}
63
[2837]64int pipi_save_opencv(pipi_image_t *img, const char *name)
[2261]65{
[2631]66    if(!img->codec_priv)
67    {
68        IplImage *priv = cvCreateImage(cvSize(img->w, img->h),
69                                       IPL_DEPTH_8U, 3);
70
71        /* FIXME: check pitch differences here */
[3342]72        if(img->last_modified == PIPI_PIXELS_BGR_U8)
[2631]73        {
[3342]74            memcpy(priv->imageData, img->p[PIPI_PIXELS_BGR_U8].pixels,
[2631]75                   3 * img->w * img->h);
[3342]76            free(img->p[PIPI_PIXELS_BGR_U8].pixels);
[2631]77        }
78
[3342]79        img->p[PIPI_PIXELS_BGR_U8].pixels = priv->imageData;
80        img->p[PIPI_PIXELS_BGR_U8].w = priv->width;
81        img->p[PIPI_PIXELS_BGR_U8].h = priv->height;
82        img->p[PIPI_PIXELS_BGR_U8].pitch = priv->widthStep;
83        img->p[PIPI_PIXELS_BGR_U8].bpp = 24;
84        img->p[PIPI_PIXELS_BGR_U8].bytes = 3 * img->w * img->h;
[2631]85
86        img->codec_priv = (void *)priv;
[3342]87        img->codec_format = PIPI_PIXELS_BGR_U8;
[2841]88        img->codec_free = pipi_free_opencv;
[2756]89
90        img->wrap = 0;
91        img->u8 = 1;
[2631]92    }
93
[3341]94    pipi_set_colorspace(img, img->codec_format);
[4693]95    cvSaveImage(name, img->codec_priv, NULL);
[2837]96
97    return 0;
[2261]98}
99
[2837]100/*
101 * XXX: The following functions are local.
102 */
103
104static int pipi_free_opencv(pipi_image_t *img)
105{
106    IplImage *iplimg;
107    iplimg = (IplImage *)img->codec_priv;
108    cvReleaseImage(&iplimg);
109
110    return 0;
111}
112
Note: See TracBrowser for help on using the repository browser.