| 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 | |
|---|
| 34 | static int pipi_free_opencv(pipi_image_t *); |
|---|
| 35 | |
|---|
| 36 | pipi_image_t *pipi_load_opencv(const char *name) |
|---|
| 37 | { |
|---|
| 38 | pipi_image_t *img; |
|---|
| 39 | |
|---|
| 40 | IplImage *priv = cvLoadImage(name, 1); |
|---|
| 41 | |
|---|
| 42 | if(!priv) |
|---|
| 43 | return NULL; |
|---|
| 44 | |
|---|
| 45 | img = pipi_new(priv->width, priv->height); |
|---|
| 46 | |
|---|
| 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; |
|---|
| 54 | |
|---|
| 55 | img->codec_priv = (void *)priv; |
|---|
| 56 | img->codec_format = PIPI_PIXELS_BGR_C; |
|---|
| 57 | img->codec_free = pipi_free_opencv; |
|---|
| 58 | |
|---|
| 59 | img->wrap = 0; |
|---|
| 60 | img->u8 = 1; |
|---|
| 61 | |
|---|
| 62 | return img; |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | int pipi_save_opencv(pipi_image_t *img, const char *name) |
|---|
| 66 | { |
|---|
| 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 */ |
|---|
| 73 | if(img->last_modified == PIPI_PIXELS_BGR_C) |
|---|
| 74 | { |
|---|
| 75 | memcpy(priv->imageData, img->p[PIPI_PIXELS_BGR_C].pixels, |
|---|
| 76 | 3 * img->w * img->h); |
|---|
| 77 | free(img->p[PIPI_PIXELS_BGR_C].pixels); |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 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; |
|---|
| 86 | |
|---|
| 87 | img->codec_priv = (void *)priv; |
|---|
| 88 | img->codec_format = PIPI_PIXELS_BGR_C; |
|---|
| 89 | |
|---|
| 90 | img->wrap = 0; |
|---|
| 91 | img->u8 = 1; |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | pipi_getpixels(img, img->codec_format); |
|---|
| 95 | cvSaveImage(name, img->codec_priv); |
|---|
| 96 | |
|---|
| 97 | return 0; |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | /* |
|---|
| 101 | * XXX: The following functions are local. |
|---|
| 102 | */ |
|---|
| 103 | |
|---|
| 104 | static 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 | |
|---|