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

Last change on this file since 407 was 407, checked in by Sam Hocevar, 16 years ago
  • src/image.c: SDL_image support.
  • Property svn:keywords set to Id
File size: 5.2 KB
Line 
1/*
2 * image.c: image I/O functions
3 * $Id: image.c 407 2005-01-04 10:30:52Z 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(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 = 0x000000ff;
80#   else
81    rmask = 0x000000ff;
82    gmask = 0x0000ff00;
83    bmask = 0x00ff0000;
84    amask = 0xff000000;
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
139int getgray(struct image *img, int x, int y, int *g)
140{
141    if(x < 0 || y < 0 || x >= img->width || y >= img->height)
142    {
143        *g = 255;
144        return -1;
145    }
146
147    *g = (unsigned char)img->pixels[y * img->pitch + x * img->channels + 1];
148
149    return 0;
150}
151
152int getpixel(struct image *img, int x, int y, int *r, int *g, int *b)
153{
154    if(x < 0 || y < 0 || x >= img->width || y >= img->height)
155    {
156        *r = 255;
157        *g = 255;
158        *b = 255;
159        return -1;
160    }
161
162    *b = (unsigned char)img->pixels[y * img->pitch + x * img->channels];
163    *g = (unsigned char)img->pixels[y * img->pitch + x * img->channels + 1];
164    *r = (unsigned char)img->pixels[y * img->pitch + x * img->channels + 2];
165
166    return 0;
167}
168
169int setpixel(struct image *img, int x, int y, int r, int g, int b)
170{
171    if(x < 0 || y < 0 || x >= img->width || y >= img->height)
172        return -1;
173
174    img->pixels[y * img->pitch + x * img->channels] = b;
175    img->pixels[y * img->pitch + x * img->channels + 1] = g;
176    img->pixels[y * img->pitch + x * img->channels + 2] = r;
177
178    return 0;
179}
180
181void image_display(struct image *img)
182{
183#if defined(HAVE_SDL_IMAGE_H)
184    //Nothing to do here
185#elif defined(HAVE_IMLIB2_H)
186    //char name[BUFSIZ];
187    //static int i = 0;
188    //sprintf(name, "image%i-%ix%i.png", i++, img->width, img->height);
189    //imlib_context_set_image(img->priv);
190    //imlib_save_image(name);
191    //fprintf(stderr, "saved to %s\n", name);
192#elif defined(HAVE_CV_H)
193    char name[BUFSIZ];
194    sprintf(name, "Image %p (%i x %i)", img, img->width, img->height);
195    cvNamedWindow(name, 0);
196    cvShowImage(name, img->priv);
197    cvResizeWindow(name, img->width * 2, img->height * 2 + 50);
198    while((unsigned char)cvWaitKey(0) != 0x1b)
199        ;
200#endif
201}
202
Note: See TracBrowser for help on using the repository browser.