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

Last change on this file since 2844 was 2844, checked in by Sam Hocevar, 11 years ago

Fix headers.

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#include "common.h"
21
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25
26#include <cv.h>
27#include <highgui.h>
28
29#include "pipi.h"
30#include "pipi_internals.h"
31
[2629]32/* FIXME: this whole file is broken until we support BGR24 images */
33
[2837]34static int pipi_free_opencv(pipi_image_t *);
35
[2261]36pipi_image_t *pipi_load_opencv(const char *name)
37{
38    pipi_image_t *img;
[2814]39
[2631]40    IplImage *priv = cvLoadImage(name, 1);
[2261]41
42    if(!priv)
43        return NULL;
44
[2631]45    img = pipi_new(priv->width, priv->height);
[2261]46
[2772]47    img->p[PIPI_PIXELS_BGR_C].pixels = priv->imageData;
48    img->p[PIPI_PIXELS_BGR_C].w = priv->width;
49    img->p[PIPI_PIXELS_BGR_C].h = priv->height;
50    img->p[PIPI_PIXELS_BGR_C].pitch = priv->widthStep;
51    img->p[PIPI_PIXELS_BGR_C].bpp = 24;
52    img->p[PIPI_PIXELS_BGR_C].bytes = 3 * img->w * img->h;
53    img->last_modified = PIPI_PIXELS_BGR_C;
[2605]54
55    img->codec_priv = (void *)priv;
[2772]56    img->codec_format = PIPI_PIXELS_BGR_C;
[2837]57    img->codec_free = pipi_free_opencv;
[2605]58
[2756]59    img->wrap = 0;
60    img->u8 = 1;
61
[2261]62    return img;
63}
64
[2837]65int pipi_save_opencv(pipi_image_t *img, const char *name)
[2261]66{
[2631]67    if(!img->codec_priv)
68    {
69        IplImage *priv = cvCreateImage(cvSize(img->w, img->h),
70                                       IPL_DEPTH_8U, 3);
71
72        /* FIXME: check pitch differences here */
[2772]73        if(img->last_modified == PIPI_PIXELS_BGR_C)
[2631]74        {
[2772]75            memcpy(priv->imageData, img->p[PIPI_PIXELS_BGR_C].pixels,
[2631]76                   3 * img->w * img->h);
[2772]77            free(img->p[PIPI_PIXELS_BGR_C].pixels);
[2631]78        }
79
[2772]80        img->p[PIPI_PIXELS_BGR_C].pixels = priv->imageData;
81        img->p[PIPI_PIXELS_BGR_C].w = priv->width;
82        img->p[PIPI_PIXELS_BGR_C].h = priv->height;
83        img->p[PIPI_PIXELS_BGR_C].pitch = priv->widthStep;
84        img->p[PIPI_PIXELS_BGR_C].bpp = 24;
85        img->p[PIPI_PIXELS_BGR_C].bytes = 3 * img->w * img->h;
[2631]86
87        img->codec_priv = (void *)priv;
[2772]88        img->codec_format = PIPI_PIXELS_BGR_C;
[2841]89        img->codec_free = pipi_free_opencv;
[2756]90
91        img->wrap = 0;
92        img->u8 = 1;
[2631]93    }
94
[2605]95    pipi_getpixels(img, img->codec_format);
96    cvSaveImage(name, img->codec_priv);
[2837]97
98    return 0;
[2261]99}
100
[2837]101/*
102 * XXX: The following functions are local.
103 */
104
105static int pipi_free_opencv(pipi_image_t *img)
106{
107    IplImage *iplimg;
108    iplimg = (IplImage *)img->codec_priv;
109    cvReleaseImage(&iplimg);
110
111    return 0;
112}
113
Note: See TracBrowser for help on using the repository browser.