source: pwntcha/trunk/src/image.c @ 1000

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