source: pwntcha/trunk/src/image.c

Last change on this file was 2315, checked in by Sam Hocevar, 15 years ago
  • Update build system and licensing terms to 2008 standards.
  • Property svn:keywords set to Id
File size: 5.7 KB
Line 
1/*
2 * image.c: image I/O functions
3 * $Id: image.c 2315 2008-04-26 07:25:03Z sam $
4 *
5 * Copyright: (c) 2004 Sam Hocevar <sam@zoy.org>
6 *  This program is free software. It comes without any warranty, to
7 *  the extent permitted by applicable law. You can redistribute it
8 *  and/or modify it under the terms of the Do What The Fuck You Want
9 *  To Public License, Version 2, as published by Sam Hocevar. See
10 *  http://sam.zoy.org/wtfpl/COPYING for more details.
11 */
12
13#include <stdio.h>
14#include <stdlib.h>
15#include <string.h>
16
17#include "config.h"
18#include "common.h"
19
20#if defined(HAVE_SDL_IMAGE_H)
21#   include <SDL_image.h>
22#elif defined(HAVE_IMLIB2_H)
23#   include <Imlib2.h>
24#elif defined(HAVE_CV_H)
25#   include <cv.h>
26#   include <highgui.h>
27#else
28#   error "No imaging library"
29#endif
30
31struct image *image_load(const char *name)
32{
33    struct image *img;
34#if defined(HAVE_SDL_IMAGE_H)
35    SDL_Surface *priv = IMG_Load(name);
36#elif defined(HAVE_IMLIB2_H)
37    Imlib_Image priv = imlib_load_image(name);
38#elif defined(HAVE_CV_H)
39    IplImage *priv = cvLoadImage(name, -1);
40#endif
41
42    if(!priv)
43        return NULL;
44
45#if defined(HAVE_SDL_IMAGE_H)
46    if(priv->format->BytesPerPixel == 1)
47    {
48        img = image_new(priv->w, priv->h);
49        SDL_BlitSurface(priv, NULL, img->priv, NULL);
50        SDL_FreeSurface(priv);
51        return img;
52    }
53#endif
54
55    img = (struct image *)malloc(sizeof(struct image));
56#if defined(HAVE_SDL_IMAGE_H)
57    img->width = priv->w;
58    img->height = priv->h;
59    img->pitch = priv->pitch;
60    img->channels = priv->format->BytesPerPixel;
61    img->pixels = priv->pixels;
62#elif defined(HAVE_IMLIB2_H)
63    imlib_context_set_image(priv);
64    img->width = imlib_image_get_width();
65    img->height = imlib_image_get_height();
66    img->pitch = 4 * imlib_image_get_width();
67    img->channels = 4;
68    img->pixels = (char *)imlib_image_get_data();
69#elif defined(HAVE_CV_H)
70    img->width = priv->width;
71    img->height = priv->height;
72    img->pitch = priv->widthStep;
73    img->channels = priv->nChannels;
74    img->pixels = priv->imageData;
75#endif
76    img->priv = (void *)priv;
77
78    return img;
79}
80
81struct image *image_new(int width, int height)
82{
83    struct image *img;
84#if defined(HAVE_SDL_IMAGE_H)
85    SDL_Surface *priv;
86    Uint32 rmask, gmask, bmask, amask;
87#   if SDL_BYTEORDER == SDL_BIG_ENDIAN
88    rmask = 0xff000000;
89    gmask = 0x00ff0000;
90    bmask = 0x0000ff00;
91    amask = 0x00000000;
92#   else
93    rmask = 0x000000ff;
94    gmask = 0x0000ff00;
95    bmask = 0x00ff0000;
96    amask = 0x00000000;
97#   endif
98    priv = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32,
99                                rmask, gmask, bmask, amask);
100#elif defined(HAVE_IMLIB2_H)
101    Imlib_Image priv = imlib_create_image(width, height);
102#elif defined(HAVE_CV_H)
103    IplImage *priv = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 3);
104#endif
105
106    if(!priv)
107        return NULL;
108
109    img = (struct image *)malloc(sizeof(struct image));
110#if defined(HAVE_SDL_IMAGE_H)
111    img->width = priv->w;
112    img->height = priv->h;
113    img->pitch = priv->pitch;
114    img->channels = priv->format->BytesPerPixel;
115    img->pixels = priv->pixels;
116#elif defined(HAVE_IMLIB2_H)
117    imlib_context_set_image(priv);
118    img->width = imlib_image_get_width();
119    img->height = imlib_image_get_height();
120    img->pitch = 4 * imlib_image_get_width();
121    img->channels = 4;
122    img->pixels = (char *)imlib_image_get_data();
123#elif defined(HAVE_CV_H)
124    img->width = priv->width;
125    img->height = priv->height;
126    img->pitch = priv->widthStep;
127    img->channels = priv->nChannels;
128    img->pixels = priv->imageData;
129#endif
130    img->priv = (void *)priv;
131
132    return img;
133}
134
135struct image *image_dup(struct image *img)
136{
137    struct image *dst;
138    int x, y;
139    dst = image_new(img->width, img->height);
140    for(y = 0; y < img->height; y++)
141    {
142        for(x = 0; x < img->width; x++)
143        {
144            int r, g, b;
145            getpixel(img, x, y, &r, &g, &b);
146            setpixel(dst, x, y, r, g, b);
147        }
148    }
149    return dst;
150}
151
152void image_free(struct image *img)
153{
154#if defined(HAVE_SDL_IMAGE_H)
155    SDL_FreeSurface(img->priv);
156#elif defined(HAVE_IMLIB2_H)
157    imlib_context_set_image(img->priv);
158    imlib_free_image();
159#elif defined(HAVE_CV_H)
160    IplImage *iplimg;
161    iplimg = (IplImage *)img->priv;
162    cvReleaseImage(&iplimg);
163#endif
164
165    free(img);
166}
167
168void image_save(struct image *img, const char *name)
169{
170#if defined(HAVE_SDL_IMAGE_H)
171    SDL_SaveBMP(img->priv, name);
172#elif defined(HAVE_IMLIB2_H)
173    imlib_context_set_image(img->priv);
174    imlib_save_image(name);
175#elif defined(HAVE_CV_H)
176    cvSaveImage(name, img->priv);
177#endif
178}
179
180void image_swap(struct image *img1, struct image *img2)
181{
182    struct image tmp;
183    memcpy(&tmp, img1, sizeof(tmp));
184    memcpy(img1, img2, sizeof(tmp));
185    memcpy(img2, &tmp, sizeof(tmp));
186}
187
188int getgray(struct image *img, int x, int y, int *g)
189{
190    if(x < 0 || y < 0 || x >= img->width || y >= img->height)
191    {
192        *g = 255;
193        return -1;
194    }
195
196    *g = (unsigned char)img->pixels[y * img->pitch + x * img->channels + 1];
197
198    return 0;
199}
200
201int getpixel(struct image *img, int x, int y, int *r, int *g, int *b)
202{
203    if(x < 0 || y < 0 || x >= img->width || y >= img->height)
204    {
205        *r = 255;
206        *g = 255;
207        *b = 255;
208        return -1;
209    }
210
211    *b = (unsigned char)img->pixels[y * img->pitch + x * img->channels];
212    *g = (unsigned char)img->pixels[y * img->pitch + x * img->channels + 1];
213    *r = (unsigned char)img->pixels[y * img->pitch + x * img->channels + 2];
214
215    return 0;
216}
217
218int setpixel(struct image *img, int x, int y, int r, int g, int b)
219{
220    if(x < 0 || y < 0 || x >= img->width || y >= img->height)
221        return -1;
222
223    img->pixels[y * img->pitch + x * img->channels] = b;
224    img->pixels[y * img->pitch + x * img->channels + 1] = g;
225    img->pixels[y * img->pitch + x * img->channels + 2] = r;
226
227    return 0;
228}
229
Note: See TracBrowser for help on using the repository browser.