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

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

Update opencv codec to more recent cvSaveImage prototype.

File size: 2.8 KB
Line 
1/*
2 *  libpipi       Pathetic image processing interface library
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
31/* FIXME: this whole file is broken until we support BGR24 images */
32
33static int pipi_free_opencv(pipi_image_t *);
34
35pipi_image_t *pipi_load_opencv(const char *name)
36{
37    pipi_image_t *img;
38
39    IplImage *priv = cvLoadImage(name, 1);
40
41    if(!priv)
42        return NULL;
43
44    img = pipi_new(priv->width, priv->height);
45
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;
53
54    img->codec_priv = (void *)priv;
55    img->codec_format = PIPI_PIXELS_BGR_U8;
56    img->codec_free = pipi_free_opencv;
57
58    img->wrap = 0;
59    img->u8 = 1;
60
61    return img;
62}
63
64int pipi_save_opencv(pipi_image_t *img, const char *name)
65{
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 */
72        if(img->last_modified == PIPI_PIXELS_BGR_U8)
73        {
74            memcpy(priv->imageData, img->p[PIPI_PIXELS_BGR_U8].pixels,
75                   3 * img->w * img->h);
76            free(img->p[PIPI_PIXELS_BGR_U8].pixels);
77        }
78
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;
85
86        img->codec_priv = (void *)priv;
87        img->codec_format = PIPI_PIXELS_BGR_U8;
88        img->codec_free = pipi_free_opencv;
89
90        img->wrap = 0;
91        img->u8 = 1;
92    }
93
94    pipi_set_colorspace(img, img->codec_format);
95    cvSaveImage(name, img->codec_priv, NULL);
96
97    return 0;
98}
99
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.