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

Last change on this file since 2756 was 2756, checked in by Sam Hocevar, 13 years ago
  • Add a new image flag, u8, which indicates whether the image samples come from an 8-bpp source.
File size: 2.6 KB
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    IplImage *priv = cvLoadImage(name, 1);
38
39    if(!priv)
40        return NULL;
41
42    img = pipi_new(priv->width, priv->height);
43
44    img->p[PIPI_PIXELS_BGR24].pixels = priv->imageData;
45    img->p[PIPI_PIXELS_BGR24].w = priv->width;
46    img->p[PIPI_PIXELS_BGR24].h = priv->height;
47    img->p[PIPI_PIXELS_BGR24].pitch = priv->widthStep;
48    img->p[PIPI_PIXELS_BGR24].bpp = 24;
49    img->p[PIPI_PIXELS_BGR24].bytes = 3 * img->w * img->h;
50    img->last_modified = PIPI_PIXELS_BGR24;
51
52    img->codec_priv = (void *)priv;
53    img->codec_format = PIPI_PIXELS_BGR24;
54
55    img->wrap = 0;
56    img->u8 = 1;
57
58    return img;
59}
60
61void pipi_free_opencv(pipi_image_t *img)
62{
63    IplImage *iplimg;
64    iplimg = (IplImage *)img->codec_priv;
65    cvReleaseImage(&iplimg);
66}
67
68void pipi_save_opencv(pipi_image_t *img, const char *name)
69{
70    if(!img->codec_priv)
71    {
72        IplImage *priv = cvCreateImage(cvSize(img->w, img->h),
73                                       IPL_DEPTH_8U, 3);
74
75        /* FIXME: check pitch differences here */
76        if(img->last_modified == PIPI_PIXELS_BGR24)
77        {
78            memcpy(priv->imageData, img->p[PIPI_PIXELS_BGR24].pixels,
79                   3 * img->w * img->h);
80            free(img->p[PIPI_PIXELS_BGR24].pixels);
81        }
82
83        img->p[PIPI_PIXELS_BGR24].pixels = priv->imageData;
84        img->p[PIPI_PIXELS_BGR24].w = priv->width;
85        img->p[PIPI_PIXELS_BGR24].h = priv->height;
86        img->p[PIPI_PIXELS_BGR24].pitch = priv->widthStep;
87        img->p[PIPI_PIXELS_BGR24].bpp = 24;
88        img->p[PIPI_PIXELS_BGR24].bytes = 3 * img->w * img->h;
89
90        img->codec_priv = (void *)priv;
91        img->codec_format = PIPI_PIXELS_BGR24;
92
93        img->wrap = 0;
94        img->u8 = 1;
95    }
96
97    pipi_getpixels(img, img->codec_format);
98    cvSaveImage(name, img->codec_priv);
99}
100
Note: See TracBrowser for help on using the repository browser.