image: add alpha property
[awesome.git] / image.c
blob06950afad601a964c3d1b2c041e6fe92285dd75f
1 /*
2 * image.c - image object
4 * Copyright © 2008 Julien Danjou <julien@danjou.info>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "structs.h"
24 extern awesome_t globalconf;
26 DO_LUA_NEW(extern, image_t, image, "image", image_ref)
27 DO_LUA_GC(image_t, image, "image", image_unref)
28 DO_LUA_EQ(image_t, image, "image")
30 static const char *
31 image_imlib_load_strerror(Imlib_Load_Error e)
33 switch(e)
35 case IMLIB_LOAD_ERROR_FILE_DOES_NOT_EXIST:
36 return "no such file or directory";
37 case IMLIB_LOAD_ERROR_FILE_IS_DIRECTORY:
38 return "file is a directory";
39 case IMLIB_LOAD_ERROR_PERMISSION_DENIED_TO_READ:
40 return "read permission denied";
41 case IMLIB_LOAD_ERROR_NO_LOADER_FOR_FILE_FORMAT:
42 return "no loader for file format";
43 case IMLIB_LOAD_ERROR_PATH_TOO_LONG:
44 return "path too long";
45 case IMLIB_LOAD_ERROR_PATH_COMPONENT_NON_EXISTANT:
46 return "path component non existant";
47 case IMLIB_LOAD_ERROR_PATH_COMPONENT_NOT_DIRECTORY:
48 return "path compoment not a directory";
49 case IMLIB_LOAD_ERROR_PATH_POINTS_OUTSIDE_ADDRESS_SPACE:
50 return "path points oustide address space";
51 case IMLIB_LOAD_ERROR_TOO_MANY_SYMBOLIC_LINKS:
52 return "too many symbolic links";
53 case IMLIB_LOAD_ERROR_OUT_OF_MEMORY:
54 return "out of memory";
55 case IMLIB_LOAD_ERROR_OUT_OF_FILE_DESCRIPTORS:
56 return "out of file descriptors";
57 case IMLIB_LOAD_ERROR_PERMISSION_DENIED_TO_WRITE:
58 return "write permission denied";
59 case IMLIB_LOAD_ERROR_OUT_OF_DISK_SPACE:
60 return "out of disk space";
61 case IMLIB_LOAD_ERROR_UNKNOWN:
62 return "unknown error, that's really bad";
63 case IMLIB_LOAD_ERROR_NONE:
64 return "no error, oops";
67 return "unknown error";
70 /** Recompute the ARGB32 data from an image.
71 * \param image The image.
72 * \return Data.
74 static void
75 image_compute(image_t *image)
77 int size, i;
78 uint32_t *data;
79 double alpha;
80 uint8_t *dataimg;
82 imlib_context_set_image(image->image);
84 data = imlib_image_get_data_for_reading_only();
86 image->width = imlib_image_get_width();
87 image->height = imlib_image_get_height();
89 size = image->width * image->height;
91 p_delete(&image->data);
92 image->data = dataimg = p_new(uint8_t, size * 4);
94 for(i = 0; i < size; i++, dataimg += 4)
96 dataimg[3] = (data[i] >> 24) & 0xff; /* A */
97 /* cairo wants pre-multiplied alpha */
98 alpha = dataimg[3] / 255.0;
99 dataimg[2] = ((data[i] >> 16) & 0xff) * alpha; /* R */
100 dataimg[1] = ((data[i] >> 8) & 0xff) * alpha; /* G */
101 dataimg[0] = (data[i] & 0xff) * alpha; /* B */
105 /** Create a new image from ARGB32 data.
106 * \param width The image width.
107 * \param height The image height.
108 * \param data The image data.
109 * \return A brand new image.
111 image_t *
112 image_new_from_argb32(int width, int height, uint32_t *data)
114 Imlib_Image imimage;
115 image_t *image = NULL;
117 if((imimage = imlib_create_image_using_copied_data(width, height, data)))
119 image = p_new(image_t, 1);
120 image->image = imimage;
121 image_compute(image);
124 return image;
127 /** Load an image from filename.
128 * \param filename The image file to load.
129 * \return A new image.
131 image_t *
132 image_new_from_file(const char *filename)
134 Imlib_Image imimage;
135 Imlib_Load_Error e = IMLIB_LOAD_ERROR_NONE;
136 image_t *image;
138 if(!filename)
139 return NULL;
141 if(!(imimage = imlib_load_image_with_error_return(filename, &e)))
143 warn("cannot load image %s: %s", filename, image_imlib_load_strerror(e));
144 return NULL;
147 image = p_new(image_t, 1);
148 image->image = imimage;
150 image_compute(image);
152 return image;
155 /** Create a new image object.
156 * \param L The Lua stack.
157 * \return The number of elements pushed on stack.
158 * \luastack
159 * \lparam The image path, or nil to create an empty image.
160 * \lparam The image width if nil was set as first arg.
161 * \lparam The image height if nil was set as first arg.
162 * \lreturn An image object.
164 static int
165 luaA_image_new(lua_State *L)
167 const char *filename;
169 if((filename = lua_tostring(L, 2)))
171 image_t *image;
172 if((image = image_new_from_file(filename)))
173 return luaA_image_userdata_new(L, image);
175 else if(lua_isnil(L, 2))
177 int width = luaL_checknumber(L, 3);
178 int height = luaL_checknumber(L, 4);
180 if(width <= 0 || height <= 0)
181 luaL_error(L, "request image has invalid size");
183 Imlib_Image imimage = imlib_create_image(width, height);
184 image_t *image = p_new(image_t, 1);
185 image->image = imimage;
186 image_compute(image);
187 return luaA_image_userdata_new(L, image);
190 return 0;
193 /** Create a new image object from ARGB32 data.
194 * \param L The Lua stack.
195 * \return The number of elements pushed on stack.
196 * \luastack
197 * \lparam The image width.
198 * \lparam The image height.
199 * \lparam The image data as a string in ARGB32 format.
200 * \lreturn An image object.
202 static int
203 luaA_image_argb32_new(lua_State *L)
205 size_t len;
206 image_t *image;
207 unsigned int width = luaL_checknumber(L, 1);
208 unsigned int height = luaL_checknumber(L, 2);
209 const char *data = luaL_checklstring(L, 3, &len);
211 if(width * height * 4 != len)
212 luaL_error(L, "string size does not match image size");
214 if((image = image_new_from_argb32(width, height, (uint32_t *) data)))
215 return luaA_image_userdata_new(L, image);
217 return 0;
220 /** Performs 90 degree rotations on the current image. Passing 0 orientation
221 * does not rotate, 1 rotates clockwise by 90 degree, 2, rotates clockwise by
222 * 180 degrees, 3 rotates clockwise by 270 degrees.
223 * \param L The Lua VM state.
224 * \return The number of elements pushed on stack.
225 * \luastack
226 * \lvalue An image.
227 * \lparam The rotation to perform.
229 static int
230 luaA_image_orientate(lua_State *L)
232 image_t **image = luaA_checkudata(L, 1, "image");
233 int orientation = luaL_checknumber(L, 2);
235 imlib_context_set_image((*image)->image);
236 imlib_image_orientate(orientation);
238 image_compute(*image);
240 return 0;
243 /** Rotate an image with specified angle radians and return a new image.
244 * \param L The Lua VM state.
245 * \return The number of elements pushed on stack.
246 * \luastack
247 * \lvalue An image.
248 * \lparam The angle in radians.
249 * \lreturn A rotated image.
251 static int
252 luaA_image_rotate(lua_State *L)
254 image_t **image = luaA_checkudata(L, 1, "image"), *new;
255 double angle = luaL_checknumber(L, 2);
257 new = p_new(image_t, 1);
259 imlib_context_set_image((*image)->image);
260 new->image = imlib_create_rotated_image(angle);
262 image_compute(new);
264 return luaA_image_userdata_new(L, new);
267 /** Crop an image to the given rectangle.
268 * \return The number of elements pushed on stack.
269 * \luastack
270 * \lvalue An image.
271 * \lparam The top left x coordinate of the rectangle.
272 * \lparam The top left y coordinate of the rectangle.
273 * \lparam The width of the rectangle.
274 * \lparam The height of the rectangle.
275 * \lreturn A cropped image.
277 static int
278 luaA_image_crop(lua_State *L)
280 image_t **image = luaA_checkudata(L, 1, "image"), *new;
281 int x = luaL_checkint(L, 2);
282 int y = luaL_checkint(L, 3);
283 int w = luaL_checkint(L, 4);
284 int h = luaL_checkint(L, 5);
286 new = p_new(image_t, 1);
288 imlib_context_set_image((*image)->image);
289 new->image = imlib_create_cropped_image(x, y, w, h);
291 image_compute(new);
293 return luaA_image_userdata_new(L, new);
296 /** Crop the image to the given rectangle and scales it.
297 * \param L The Lua VM state.
298 * \return The number of elements pushed on stack.
299 * \luastack
300 * \lvalue An image.
301 * \lparam The top left x coordinate of the source rectangle.
302 * \lparam The top left y coordinate of the source rectangle.
303 * \lparam The width of the source rectangle.
304 * \lparam The height of the source rectangle.
305 * \lparam The width of the destination rectangle.
306 * \lparam The height of the destination rectangle.
307 * \lreturn A cropped image.
309 static int
310 luaA_image_crop_and_scale(lua_State *L)
312 image_t **image = luaA_checkudata(L, 1, "image"), *new;
313 int source_x = luaL_checkint(L, 2);
314 int source_y = luaL_checkint(L, 3);
315 int w = luaL_checkint(L, 4);
316 int h = luaL_checkint(L, 5);
317 int dest_w = luaL_checkint(L, 6);
318 int dest_h = luaL_checkint(L, 7);
320 new = p_new(image_t, 1);
322 imlib_context_set_image((*image)->image);
323 new->image = imlib_create_cropped_scaled_image(source_x,
324 source_y,
325 w, h,
326 dest_w, dest_h);
328 image_compute(new);
330 return luaA_image_userdata_new(L, new);
333 /** Image object.
334 * \param L The Lua VM state.
335 * \return The number of elements pushed on stack.
336 * \luastack
337 * \lvalue An image
338 * \lfield width The image width.
339 * \lfield height The image height.
341 static int
342 luaA_image_index(lua_State *L)
344 if(luaA_usemetatable(L, 1, 2))
345 return 1;
347 image_t **image = luaA_checkudata(L, 1, "image");
348 size_t len;
349 const char *attr = luaL_checklstring(L, 2, &len);
351 switch(a_tokenize(attr, len))
353 case A_TK_WIDTH:
354 imlib_context_set_image((*image)->image);
355 lua_pushnumber(L, imlib_image_get_width());
356 break;
357 case A_TK_HEIGHT:
358 imlib_context_set_image((*image)->image);
359 lua_pushnumber(L, imlib_image_get_height());
360 break;
361 case A_TK_ALPHA:
362 imlib_context_set_image((*image)->image);
363 lua_pushboolean(L, imlib_image_has_alpha());
364 break;
365 default:
366 return 0;
369 return 1;
372 const struct luaL_reg awesome_image_methods[] =
374 { "__call", luaA_image_new },
375 { "argb32", luaA_image_argb32_new },
376 { NULL, NULL }
378 const struct luaL_reg awesome_image_meta[] =
380 { "__index", luaA_image_index },
381 { "rotate", luaA_image_rotate },
382 { "orientate", luaA_image_orientate },
383 { "crop", luaA_image_crop },
384 { "crop_and_scale", luaA_image_crop_and_scale },
385 { "__gc", luaA_image_gc },
386 { "__eq", luaA_image_eq },
387 { "__tostring", luaA_image_tostring },
388 { NULL, NULL }
391 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80