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

Last change on this file since 413 was 413, checked in by Sam Hocevar, 16 years ago
  • fixed a bug in the SDL code: alpha mask should be set to 0.
  • implemented filter_black_stuff to remove black dots.
  • allow threshold to be negative in filter_equalize; in this case, invert colours.
  • implemented filter_crop.
  • Property svn:keywords set to Id
File size: 5.5 KB
Line 
1/*
2 * image.c: image I/O functions
3 * $Id: image.c 413 2005-01-04 17:06:04Z 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
15#include "config.h"
16#include "common.h"
17
18#if defined(HAVE_SDL_IMAGE_H)
19#   include <SDL_image.h>
20#elif defined(HAVE_IMLIB2_H)
21#   include <Imlib2.h>
22#elif defined(HAVE_CV_H)
23#   include <cv.h>
24#   include <highgui.h>
25#else
26#   error "No imaging library"
27#endif
28
29struct image *image_load(const char *name)
30{
31    struct image *img;
32#if defined(HAVE_SDL_IMAGE_H)
33    SDL_Surface *priv = IMG_Load(name);
34#elif defined(HAVE_IMLIB2_H)
35    Imlib_Image priv = imlib_load_image(name);
36#elif defined(HAVE_CV_H)
37    IplImage *priv = cvLoadImage(name, -1);
38#endif
39
40    if(!priv)
41        return NULL;
42
43    img = malloc(sizeof(struct image));
44#if defined(HAVE_SDL_IMAGE_H)
45    img->width = priv->w;
46    img->height = priv->h;
47    img->pitch = priv->pitch;
48    img->channels = priv->format->BytesPerPixel;
49    img->pixels = priv->pixels;
50#elif defined(HAVE_IMLIB2_H)
51    imlib_context_set_image(priv);
52    img->width = imlib_image_get_width();
53    img->height = imlib_image_get_height();
54    img->pitch = 4 * imlib_image_get_width();
55    img->channels = 4;
56    img->pixels = (char *)imlib_image_get_data();
57#elif defined(HAVE_CV_H)
58    img->width = priv->width;
59    img->height = priv->height;
60    img->pitch = priv->widthStep;
61    img->channels = priv->nChannels;
62    img->pixels = priv->imageData;
63#endif
64    img->priv = (void *)priv;
65
66    return img;
67}
68
69struct image *image_new(int width, int height)
70{
71    struct image *img;
72#if defined(HAVE_SDL_IMAGE_H)
73    SDL_Surface *priv;
74    Uint32 rmask, gmask, bmask, amask;
75#   if SDL_BYTEORDER == SDL_BIG_ENDIAN
76    rmask = 0xff000000;
77    gmask = 0x00ff0000;
78    bmask = 0x0000ff00;
79    amask = 0x00000000;
80#   else
81    rmask = 0x000000ff;
82    gmask = 0x0000ff00;
83    bmask = 0x00ff0000;
84    amask = 0x00000000;
85#   endif
86    priv = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32,
87                                rmask, gmask, bmask, amask);
88#elif defined(HAVE_IMLIB2_H)
89    Imlib_Image priv = imlib_create_image(width, height);
90#elif defined(HAVE_CV_H)
91    IplImage *priv = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 3);
92#endif
93
94    if(!priv)
95        return NULL;
96
97    img = malloc(sizeof(struct image));
98#if defined(HAVE_SDL_IMAGE_H)
99    img->width = priv->w;
100    img->height = priv->h;
101    img->pitch = priv->pitch;
102    img->channels = priv->format->BytesPerPixel;
103    img->pixels = priv->pixels;
104#elif defined(HAVE_IMLIB2_H)
105    imlib_context_set_image(priv);
106    img->width = imlib_image_get_width();
107    img->height = imlib_image_get_height();
108    img->pitch = 4 * imlib_image_get_width();
109    img->channels = 4;
110    img->pixels = (char *)imlib_image_get_data();
111#elif defined(HAVE_CV_H)
112    img->width = priv->width;
113    img->height = priv->height;
114    img->pitch = priv->widthStep;
115    img->channels = priv->nChannels;
116    img->pixels = priv->imageData;
117#endif
118    img->priv = (void *)priv;
119
120    return img;
121}
122
123void image_free(struct image *img)
124{
125#if defined(HAVE_SDL_IMAGE_H)
126    SDL_FreeSurface(img->priv);
127#elif defined(HAVE_IMLIB2_H)
128    imlib_context_set_image(img->priv);
129    imlib_free_image();
130#elif defined(HAVE_CV_H)
131    IplImage *iplimg;
132    iplimg = (IplImage *)img->priv;
133    cvReleaseImage(&iplimg);
134#endif
135
136    free(img);
137}
138
139void image_save(struct image *img, const char *name)
140{
141#if defined(HAVE_SDL_IMAGE_H)
142    SDL_SaveBMP(img->priv, name);
143#elif defined(HAVE_IMLIB2_H)
144    imlib_context_set_image(img->priv);
145    imlib_save_image(name);
146#elif defined(HAVE_CV_H)
147    cvSaveImage(name, img->priv);
148#endif
149}
150
151int getgray(struct image *img, int x, int y, int *g)
152{
153    if(x < 0 || y < 0 || x >= img->width || y >= img->height)
154    {
155        *g = 255;
156        return -1;
157    }
158
159    *g = (unsigned char)img->pixels[y * img->pitch + x * img->channels + 1];
160
161    return 0;
162}
163
164int getpixel(struct image *img, int x, int y, int *r, int *g, int *b)
165{
166    if(x < 0 || y < 0 || x >= img->width || y >= img->height)
167    {
168        *r = 255;
169        *g = 255;
170        *b = 255;
171        return -1;
172    }
173
174    *b = (unsigned char)img->pixels[y * img->pitch + x * img->channels];
175    *g = (unsigned char)img->pixels[y * img->pitch + x * img->channels + 1];
176    *r = (unsigned char)img->pixels[y * img->pitch + x * img->channels + 2];
177
178    return 0;
179}
180
181int setpixel(struct image *img, int x, int y, int r, int g, int b)
182{
183    if(x < 0 || y < 0 || x >= img->width || y >= img->height)
184        return -1;
185
186    img->pixels[y * img->pitch + x * img->channels] = b;
187    img->pixels[y * img->pitch + x * img->channels + 1] = g;
188    img->pixels[y * img->pitch + x * img->channels + 2] = r;
189
190    return 0;
191}
192
193void image_display(struct image *img)
194{
195#if defined(HAVE_SDL_IMAGE_H)
196    //Nothing to do here
197#elif defined(HAVE_IMLIB2_H)
198    //char name[BUFSIZ];
199    //static int i = 0;
200    //sprintf(name, "image%i-%ix%i.png", i++, img->width, img->height);
201    //imlib_context_set_image(img->priv);
202    //imlib_save_image(name);
203    //fprintf(stderr, "saved to %s\n", name);
204#elif defined(HAVE_CV_H)
205    char name[BUFSIZ];
206    sprintf(name, "Image %p (%i x %i)", img, img->width, img->height);
207    cvNamedWindow(name, 0);
208    cvShowImage(name, img->priv);
209    cvResizeWindow(name, img->width * 2, img->height * 2 + 50);
210    while((unsigned char)cvWaitKey(0) != 0x1b)
211        ;
212#endif
213}
214
Note: See TracBrowser for help on using the repository browser.