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

Revision 2814, 2.6 KB checked in by jylam, 5 years ago (diff)
  • Moved accessors to their own file, and added a pipi_get_format_name()
Line 
1/*
2 *  libpipi       Proper image processing implementation 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#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
32/* FIXME: this whole file is broken until we support BGR24 images */
33
34pipi_image_t *pipi_load_opencv(const char *name)
35{
36    pipi_image_t *img;
37
38    IplImage *priv = cvLoadImage(name, 1);
39
40    if(!priv)
41        return NULL;
42
43    img = pipi_new(priv->width, priv->height);
44
45    img->p[PIPI_PIXELS_BGR_C].pixels = priv->imageData;
46    img->p[PIPI_PIXELS_BGR_C].w = priv->width;
47    img->p[PIPI_PIXELS_BGR_C].h = priv->height;
48    img->p[PIPI_PIXELS_BGR_C].pitch = priv->widthStep;
49    img->p[PIPI_PIXELS_BGR_C].bpp = 24;
50    img->p[PIPI_PIXELS_BGR_C].bytes = 3 * img->w * img->h;
51    img->last_modified = PIPI_PIXELS_BGR_C;
52
53    img->codec_priv = (void *)priv;
54    img->codec_format = PIPI_PIXELS_BGR_C;
55
56    img->wrap = 0;
57    img->u8 = 1;
58
59    return img;
60}
61
62void pipi_free_opencv(pipi_image_t *img)
63{
64    IplImage *iplimg;
65    iplimg = (IplImage *)img->codec_priv;
66    cvReleaseImage(&iplimg);
67}
68
69void pipi_save_opencv(pipi_image_t *img, const char *name)
70{
71    if(!img->codec_priv)
72    {
73        IplImage *priv = cvCreateImage(cvSize(img->w, img->h),
74                                       IPL_DEPTH_8U, 3);
75
76        /* FIXME: check pitch differences here */
77        if(img->last_modified == PIPI_PIXELS_BGR_C)
78        {
79            memcpy(priv->imageData, img->p[PIPI_PIXELS_BGR_C].pixels,
80                   3 * img->w * img->h);
81            free(img->p[PIPI_PIXELS_BGR_C].pixels);
82        }
83
84        img->p[PIPI_PIXELS_BGR_C].pixels = priv->imageData;
85        img->p[PIPI_PIXELS_BGR_C].w = priv->width;
86        img->p[PIPI_PIXELS_BGR_C].h = priv->height;
87        img->p[PIPI_PIXELS_BGR_C].pitch = priv->widthStep;
88        img->p[PIPI_PIXELS_BGR_C].bpp = 24;
89        img->p[PIPI_PIXELS_BGR_C].bytes = 3 * img->w * img->h;
90
91        img->codec_priv = (void *)priv;
92        img->codec_format = PIPI_PIXELS_BGR_C;
93
94        img->wrap = 0;
95        img->u8 = 1;
96    }
97
98    pipi_getpixels(img, img->codec_format);
99    cvSaveImage(name, img->codec_priv);
100}
101
Note: See TracBrowser for help on using the repository browser.