source: libpipi/trunk/pipi/io.c @ 2246

Last change on this file since 2246 was 2228, checked in by Sam Hocevar, 15 years ago
  • Import image loading features from PWNtcha. As they are more than 4-year old, they will certainly require some cleanup, eg. pkg-config transitions.
File size: 4.7 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 <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22
23#include "config.h"
24#include "common.h"
25
26#if defined(HAVE_SDL_IMAGE_H)
27#   include <SDL_image.h>
28#elif defined(HAVE_IMLIB2_H)
29#   include <Imlib2.h>
30#elif defined(HAVE_CV_H)
31#   include <cv.h>
32#   include <highgui.h>
33#else
34#   error "No imaging library"
35#endif
36
37#include "pipi_internals.h"
38#include "pipi.h"
39
40pipi_image_t *pipi_load(const char *name)
41{
42    pipi_image_t *img;
43#if defined(HAVE_SDL_IMAGE_H)
44    SDL_Surface *priv = IMG_Load(name);
45#elif defined(HAVE_IMLIB2_H)
46    Imlib_Image priv = imlib_load_image(name);
47#elif defined(HAVE_CV_H)
48    IplImage *priv = cvLoadImage(name, -1);
49#endif
50
51    if(!priv)
52        return NULL;
53
54#if defined(HAVE_SDL_IMAGE_H)
55    if(priv->format->BytesPerPixel == 1)
56    {
57        img = pipi_new(priv->w, priv->h);
58        SDL_BlitSurface(priv, NULL, img->priv, NULL);
59        SDL_FreeSurface(priv);
60        return img;
61    }
62#endif
63
64    img = (pipi_image_t *)malloc(sizeof(pipi_image_t));
65#if defined(HAVE_SDL_IMAGE_H)
66    img->width = priv->w;
67    img->height = priv->h;
68    img->pitch = priv->pitch;
69    img->channels = priv->format->BytesPerPixel;
70    img->pixels = priv->pixels;
71#elif defined(HAVE_IMLIB2_H)
72    imlib_context_set_image(priv);
73    img->width = imlib_image_get_width();
74    img->height = imlib_image_get_height();
75    img->pitch = 4 * imlib_image_get_width();
76    img->channels = 4;
77    img->pixels = (char *)imlib_image_get_data();
78#elif defined(HAVE_CV_H)
79    img->width = priv->width;
80    img->height = priv->height;
81    img->pitch = priv->widthStep;
82    img->channels = priv->nChannels;
83    img->pixels = priv->imageData;
84#endif
85    img->priv = (void *)priv;
86
87    return img;
88}
89
90pipi_image_t *pipi_new(int width, int height)
91{
92    pipi_image_t *img;
93#if defined(HAVE_SDL_IMAGE_H)
94    SDL_Surface *priv;
95    Uint32 rmask, gmask, bmask, amask;
96#   if SDL_BYTEORDER == SDL_BIG_ENDIAN
97    rmask = 0xff000000;
98    gmask = 0x00ff0000;
99    bmask = 0x0000ff00;
100    amask = 0x00000000;
101#   else
102    rmask = 0x000000ff;
103    gmask = 0x0000ff00;
104    bmask = 0x00ff0000;
105    amask = 0x00000000;
106#   endif
107    priv = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32,
108                                rmask, gmask, bmask, amask);
109#elif defined(HAVE_IMLIB2_H)
110    Imlib_Image priv = imlib_create_image(width, height);
111#elif defined(HAVE_CV_H)
112    IplImage *priv = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 3);
113#endif
114
115    if(!priv)
116        return NULL;
117
118    img = (pipi_image_t *)malloc(sizeof(pipi_image_t));
119#if defined(HAVE_SDL_IMAGE_H)
120    img->width = priv->w;
121    img->height = priv->h;
122    img->pitch = priv->pitch;
123    img->channels = priv->format->BytesPerPixel;
124    img->pixels = priv->pixels;
125#elif defined(HAVE_IMLIB2_H)
126    imlib_context_set_image(priv);
127    img->width = imlib_image_get_width();
128    img->height = imlib_image_get_height();
129    img->pitch = 4 * imlib_image_get_width();
130    img->channels = 4;
131    img->pixels = (char *)imlib_image_get_data();
132#elif defined(HAVE_CV_H)
133    img->width = priv->width;
134    img->height = priv->height;
135    img->pitch = priv->widthStep;
136    img->channels = priv->nChannels;
137    img->pixels = priv->imageData;
138#endif
139    img->priv = (void *)priv;
140
141    return img;
142}
143
144pipi_image_t *pipi_copy(pipi_image_t const *img)
145{
146    pipi_image_t *dst;
147    int x, y;
148    dst = pipi_new(img->width, img->height);
149    for(y = 0; y < img->height; y++)
150    {
151        for(x = 0; x < img->width; x++)
152        {
153            int r, g, b;
154            pipi_getpixel(img, x, y, &r, &g, &b);
155            pipi_setpixel(dst, x, y, r, g, b);
156        }
157    }
158    return dst;
159}
160
161void pipi_free(pipi_image_t *img)
162{
163#if defined(HAVE_SDL_IMAGE_H)
164    SDL_FreeSurface(img->priv);
165#elif defined(HAVE_IMLIB2_H)
166    imlib_context_set_image(img->priv);
167    imlib_free_image();
168#elif defined(HAVE_CV_H)
169    IplImage *iplimg;
170    iplimg = (IplImage *)img->priv;
171    cvReleaseImage(&iplimg);
172#endif
173
174    free(img);
175}
176
177void pipi_save(pipi_image_t *img, const char *name)
178{
179#if defined(HAVE_SDL_IMAGE_H)
180    SDL_SaveBMP(img->priv, name);
181#elif defined(HAVE_IMLIB2_H)
182    imlib_context_set_image(img->priv);
183    imlib_save_image(name);
184#elif defined(HAVE_CV_H)
185    cvSaveImage(name, img->priv);
186#endif
187}
188
Note: See TracBrowser for help on using the repository browser.