1 | /* |
---|
2 | * image.c: image I/O functions |
---|
3 | * $Id: image.c 416 2005-01-04 18:42:23Z 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 | |
---|
29 | struct 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 | #if defined(HAVE_SDL_IMAGE_H) |
---|
44 | if(priv->format->BytesPerPixel == 1) |
---|
45 | { |
---|
46 | img = image_new(priv->w, priv->h); |
---|
47 | SDL_BlitSurface(priv, NULL, img->priv, NULL); |
---|
48 | SDL_FreeSurface(priv); |
---|
49 | return img; |
---|
50 | } |
---|
51 | #endif |
---|
52 | |
---|
53 | img = malloc(sizeof(struct image)); |
---|
54 | #if defined(HAVE_SDL_IMAGE_H) |
---|
55 | img->width = priv->w; |
---|
56 | img->height = priv->h; |
---|
57 | img->pitch = priv->pitch; |
---|
58 | img->channels = priv->format->BytesPerPixel; |
---|
59 | img->pixels = priv->pixels; |
---|
60 | #elif defined(HAVE_IMLIB2_H) |
---|
61 | imlib_context_set_image(priv); |
---|
62 | img->width = imlib_image_get_width(); |
---|
63 | img->height = imlib_image_get_height(); |
---|
64 | img->pitch = 4 * imlib_image_get_width(); |
---|
65 | img->channels = 4; |
---|
66 | img->pixels = (char *)imlib_image_get_data(); |
---|
67 | #elif defined(HAVE_CV_H) |
---|
68 | img->width = priv->width; |
---|
69 | img->height = priv->height; |
---|
70 | img->pitch = priv->widthStep; |
---|
71 | img->channels = priv->nChannels; |
---|
72 | img->pixels = priv->imageData; |
---|
73 | #endif |
---|
74 | img->priv = (void *)priv; |
---|
75 | |
---|
76 | return img; |
---|
77 | } |
---|
78 | |
---|
79 | struct image *image_new(int width, int height) |
---|
80 | { |
---|
81 | struct image *img; |
---|
82 | #if defined(HAVE_SDL_IMAGE_H) |
---|
83 | SDL_Surface *priv; |
---|
84 | Uint32 rmask, gmask, bmask, amask; |
---|
85 | # if SDL_BYTEORDER == SDL_BIG_ENDIAN |
---|
86 | rmask = 0xff000000; |
---|
87 | gmask = 0x00ff0000; |
---|
88 | bmask = 0x0000ff00; |
---|
89 | amask = 0x00000000; |
---|
90 | # else |
---|
91 | rmask = 0x000000ff; |
---|
92 | gmask = 0x0000ff00; |
---|
93 | bmask = 0x00ff0000; |
---|
94 | amask = 0x00000000; |
---|
95 | # endif |
---|
96 | priv = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32, |
---|
97 | rmask, gmask, bmask, amask); |
---|
98 | #elif defined(HAVE_IMLIB2_H) |
---|
99 | Imlib_Image priv = imlib_create_image(width, height); |
---|
100 | #elif defined(HAVE_CV_H) |
---|
101 | IplImage *priv = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 3); |
---|
102 | #endif |
---|
103 | |
---|
104 | if(!priv) |
---|
105 | return NULL; |
---|
106 | |
---|
107 | img = malloc(sizeof(struct image)); |
---|
108 | #if defined(HAVE_SDL_IMAGE_H) |
---|
109 | img->width = priv->w; |
---|
110 | img->height = priv->h; |
---|
111 | img->pitch = priv->pitch; |
---|
112 | img->channels = priv->format->BytesPerPixel; |
---|
113 | img->pixels = priv->pixels; |
---|
114 | #elif defined(HAVE_IMLIB2_H) |
---|
115 | imlib_context_set_image(priv); |
---|
116 | img->width = imlib_image_get_width(); |
---|
117 | img->height = imlib_image_get_height(); |
---|
118 | img->pitch = 4 * imlib_image_get_width(); |
---|
119 | img->channels = 4; |
---|
120 | img->pixels = (char *)imlib_image_get_data(); |
---|
121 | #elif defined(HAVE_CV_H) |
---|
122 | img->width = priv->width; |
---|
123 | img->height = priv->height; |
---|
124 | img->pitch = priv->widthStep; |
---|
125 | img->channels = priv->nChannels; |
---|
126 | img->pixels = priv->imageData; |
---|
127 | #endif |
---|
128 | img->priv = (void *)priv; |
---|
129 | |
---|
130 | return img; |
---|
131 | } |
---|
132 | |
---|
133 | void image_free(struct image *img) |
---|
134 | { |
---|
135 | #if defined(HAVE_SDL_IMAGE_H) |
---|
136 | SDL_FreeSurface(img->priv); |
---|
137 | #elif defined(HAVE_IMLIB2_H) |
---|
138 | imlib_context_set_image(img->priv); |
---|
139 | imlib_free_image(); |
---|
140 | #elif defined(HAVE_CV_H) |
---|
141 | IplImage *iplimg; |
---|
142 | iplimg = (IplImage *)img->priv; |
---|
143 | cvReleaseImage(&iplimg); |
---|
144 | #endif |
---|
145 | |
---|
146 | free(img); |
---|
147 | } |
---|
148 | |
---|
149 | void image_save(struct image *img, const char *name) |
---|
150 | { |
---|
151 | #if defined(HAVE_SDL_IMAGE_H) |
---|
152 | SDL_SaveBMP(img->priv, name); |
---|
153 | #elif defined(HAVE_IMLIB2_H) |
---|
154 | imlib_context_set_image(img->priv); |
---|
155 | imlib_save_image(name); |
---|
156 | #elif defined(HAVE_CV_H) |
---|
157 | cvSaveImage(name, img->priv); |
---|
158 | #endif |
---|
159 | } |
---|
160 | |
---|
161 | int getgray(struct image *img, int x, int y, int *g) |
---|
162 | { |
---|
163 | if(x < 0 || y < 0 || x >= img->width || y >= img->height) |
---|
164 | { |
---|
165 | *g = 255; |
---|
166 | return -1; |
---|
167 | } |
---|
168 | |
---|
169 | *g = (unsigned char)img->pixels[y * img->pitch + x * img->channels + 1]; |
---|
170 | |
---|
171 | return 0; |
---|
172 | } |
---|
173 | |
---|
174 | int getpixel(struct image *img, int x, int y, int *r, int *g, int *b) |
---|
175 | { |
---|
176 | if(x < 0 || y < 0 || x >= img->width || y >= img->height) |
---|
177 | { |
---|
178 | *r = 255; |
---|
179 | *g = 255; |
---|
180 | *b = 255; |
---|
181 | return -1; |
---|
182 | } |
---|
183 | |
---|
184 | *b = (unsigned char)img->pixels[y * img->pitch + x * img->channels]; |
---|
185 | *g = (unsigned char)img->pixels[y * img->pitch + x * img->channels + 1]; |
---|
186 | *r = (unsigned char)img->pixels[y * img->pitch + x * img->channels + 2]; |
---|
187 | |
---|
188 | return 0; |
---|
189 | } |
---|
190 | |
---|
191 | int setpixel(struct image *img, int x, int y, int r, int g, int b) |
---|
192 | { |
---|
193 | if(x < 0 || y < 0 || x >= img->width || y >= img->height) |
---|
194 | return -1; |
---|
195 | |
---|
196 | img->pixels[y * img->pitch + x * img->channels] = b; |
---|
197 | img->pixels[y * img->pitch + x * img->channels + 1] = g; |
---|
198 | img->pixels[y * img->pitch + x * img->channels + 2] = r; |
---|
199 | |
---|
200 | return 0; |
---|
201 | } |
---|
202 | |
---|
203 | void image_display(struct image *img) |
---|
204 | { |
---|
205 | #if defined(HAVE_SDL_IMAGE_H) |
---|
206 | //Nothing to do here |
---|
207 | #elif defined(HAVE_IMLIB2_H) |
---|
208 | //char name[BUFSIZ]; |
---|
209 | //static int i = 0; |
---|
210 | //sprintf(name, "image%i-%ix%i.png", i++, img->width, img->height); |
---|
211 | //imlib_context_set_image(img->priv); |
---|
212 | //imlib_save_image(name); |
---|
213 | //fprintf(stderr, "saved to %s\n", name); |
---|
214 | #elif defined(HAVE_CV_H) |
---|
215 | char name[BUFSIZ]; |
---|
216 | sprintf(name, "Image %p (%i x %i)", img, img->width, img->height); |
---|
217 | cvNamedWindow(name, 0); |
---|
218 | cvShowImage(name, img->priv); |
---|
219 | cvResizeWindow(name, img->width * 2, img->height * 2 + 50); |
---|
220 | while((unsigned char)cvWaitKey(0) != 0x1b) |
---|
221 | ; |
---|
222 | #endif |
---|
223 | } |
---|
224 | |
---|