Use 'sh' instead of 'sh -e'
[awesome.git] / image.c
blobd36a74a23888452c4017d87817787462031bd4d1
1 /*
2 * image.c - image object
4 * Copyright © 2008-2009 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"
23 #include "common/tokenize.h"
25 DO_LUA_TOSTRING(image_t, image, "image");
27 static int
28 luaA_image_gc(lua_State *L)
30 image_t *p = luaL_checkudata(L, 1, "image");
31 luaA_ref_array_wipe(&p->refs);
32 imlib_context_set_image(p->image);
33 imlib_free_image();
34 p_delete(&p->data);
35 return 0;
38 static const char *
39 image_imlib_load_strerror(Imlib_Load_Error e)
41 switch(e)
43 case IMLIB_LOAD_ERROR_FILE_DOES_NOT_EXIST:
44 return "no such file or directory";
45 case IMLIB_LOAD_ERROR_FILE_IS_DIRECTORY:
46 return "file is a directory";
47 case IMLIB_LOAD_ERROR_PERMISSION_DENIED_TO_READ:
48 return "read permission denied";
49 case IMLIB_LOAD_ERROR_NO_LOADER_FOR_FILE_FORMAT:
50 return "no loader for file format";
51 case IMLIB_LOAD_ERROR_PATH_TOO_LONG:
52 return "path too long";
53 case IMLIB_LOAD_ERROR_PATH_COMPONENT_NON_EXISTANT:
54 return "path component non existant";
55 case IMLIB_LOAD_ERROR_PATH_COMPONENT_NOT_DIRECTORY:
56 return "path compoment not a directory";
57 case IMLIB_LOAD_ERROR_PATH_POINTS_OUTSIDE_ADDRESS_SPACE:
58 return "path points oustide address space";
59 case IMLIB_LOAD_ERROR_TOO_MANY_SYMBOLIC_LINKS:
60 return "too many symbolic links";
61 case IMLIB_LOAD_ERROR_OUT_OF_MEMORY:
62 return "out of memory";
63 case IMLIB_LOAD_ERROR_OUT_OF_FILE_DESCRIPTORS:
64 return "out of file descriptors";
65 case IMLIB_LOAD_ERROR_PERMISSION_DENIED_TO_WRITE:
66 return "write permission denied";
67 case IMLIB_LOAD_ERROR_OUT_OF_DISK_SPACE:
68 return "out of disk space";
69 case IMLIB_LOAD_ERROR_UNKNOWN:
70 return "unknown error, that's really bad";
71 case IMLIB_LOAD_ERROR_NONE:
72 return "no error, oops";
75 return "unknown error";
78 /** Recompute the ARGB32 data from an image.
79 * \param image The image.
80 * \return Data.
82 static void
83 image_compute(image_t *image)
85 int size, i;
86 uint32_t *data;
87 double alpha;
88 uint8_t *dataimg;
90 imlib_context_set_image(image->image);
92 data = imlib_image_get_data_for_reading_only();
94 image->width = imlib_image_get_width();
95 image->height = imlib_image_get_height();
97 size = image->width * image->height;
99 p_realloc(&image->data, size * 4);
100 dataimg = image->data;
102 for(i = 0; i < size; i++, dataimg += 4)
104 #if AWESOME_IS_BIG_ENDIAN
105 dataimg[0] = (data[i] >> 24) & 0xff; /* A */
106 /* cairo wants pre-multiplied alpha */
107 alpha = dataimg[0] / 255.0;
108 dataimg[1] = ((data[i] >> 16) & 0xff) * alpha; /* R */
109 dataimg[2] = ((data[i] >> 8) & 0xff) * alpha; /* G */
110 dataimg[3] = (data[i] & 0xff) * alpha; /* B */
111 #else
112 dataimg[3] = (data[i] >> 24) & 0xff; /* A */
113 /* cairo wants pre-multiplied alpha */
114 alpha = dataimg[3] / 255.0;
115 dataimg[2] = ((data[i] >> 16) & 0xff) * alpha; /* R */
116 dataimg[1] = ((data[i] >> 8) & 0xff) * alpha; /* G */
117 dataimg[0] = (data[i] & 0xff) * alpha; /* B */
118 #endif
122 /** Create a new image from ARGB32 data.
123 * \param width The image width.
124 * \param height The image height.
125 * \param data The image data.
126 * \return 1 if an image has been pushed on stack, 0 otherwise.
129 image_new_from_argb32(int width, int height, uint32_t *data)
131 Imlib_Image imimage;
133 if((imimage = imlib_create_image_using_copied_data(width, height, data)))
135 imlib_context_set_image(imimage);
136 imlib_image_set_has_alpha(true);
137 image_t *image = image_new(globalconf.L);
138 image->image = imimage;
139 image_compute(image);
140 return 1;
143 return 0;
146 /** Load an image from filename.
147 * \param filename The image file to load.
148 * \return 1 if image is loaded and on stack, 0 otherwise.
150 static int
151 image_new_from_file(const char *filename)
153 Imlib_Image imimage;
154 Imlib_Load_Error e = IMLIB_LOAD_ERROR_NONE;
155 image_t *image;
157 if(!filename)
158 return 0;
160 if(!(imimage = imlib_load_image_with_error_return(filename, &e)))
162 warn("cannot load image %s: %s", filename, image_imlib_load_strerror(e));
163 return 0;
166 image = image_new(globalconf.L);
167 image->image = imimage;
169 image_compute(image);
171 return 1;
174 /** Create a new image object.
175 * \param L The Lua stack.
176 * \return The number of elements pushed on stack.
177 * \luastack
178 * \lparam The image path.
179 * \lreturn An image object.
181 static int
182 luaA_image_new(lua_State *L)
184 const char *filename;
186 if((filename = lua_tostring(L, 2)))
187 return image_new_from_file(filename);
188 return 0;
191 /** Create a new image object from ARGB32 data.
192 * \param L The Lua stack.
193 * \return The number of elements pushed on stack.
194 * \luastack
195 * \lparam The image width.
196 * \lparam The image height.
197 * \lparam The image data as a string in ARGB32 format, or nil to create an
198 * empty image.
199 * \lreturn An image object.
201 static int
202 luaA_image_argb32_new(lua_State *L)
204 size_t len;
205 unsigned int width = luaL_checknumber(L, 1);
206 unsigned int height = luaL_checknumber(L, 2);
208 if(lua_isnil(L, 3))
210 uint32_t *data = p_new(uint32_t, width * height);
211 return image_new_from_argb32(width, height, data);
214 const char *data = luaL_checklstring(L, 3, &len);
216 if(width * height * 4 != len)
217 luaL_error(L, "string size does not match image size");
219 return image_new_from_argb32(width, height, (uint32_t *) data);
222 /** Performs 90 degree rotations on the current image. Passing 0 orientation
223 * does not rotate, 1 rotates clockwise by 90 degree, 2, rotates clockwise by
224 * 180 degrees, 3 rotates clockwise by 270 degrees.
225 * \param L The Lua VM state.
226 * \return The number of elements pushed on stack.
227 * \luastack
228 * \lvalue An image.
229 * \lparam The rotation to perform.
231 static int
232 luaA_image_orientate(lua_State *L)
234 image_t *image = luaL_checkudata(L, 1, "image");
235 int orientation = luaL_checknumber(L, 2);
237 imlib_context_set_image(image->image);
238 imlib_image_orientate(orientation);
240 image_compute(image);
242 return 0;
245 /** Rotate an image with specified angle radians and return a new image.
246 * \param L The Lua VM state.
247 * \return The number of elements pushed on stack.
248 * \luastack
249 * \lvalue An image.
250 * \lparam The angle in radians.
251 * \lreturn A rotated image.
253 static int
254 luaA_image_rotate(lua_State *L)
256 image_t *image = luaL_checkudata(L, 1, "image"), *new;
257 double angle = luaL_checknumber(L, 2);
259 new = image_new(L);
261 imlib_context_set_image(image->image);
262 new->image = imlib_create_rotated_image(angle);
264 image_compute(new);
266 return 1;
269 /** Crop an image to the given rectangle.
270 * \return The number of elements pushed on stack.
271 * \luastack
272 * \lvalue An image.
273 * \lparam The top left x coordinate of the rectangle.
274 * \lparam The top left y coordinate of the rectangle.
275 * \lparam The width of the rectangle.
276 * \lparam The height of the rectangle.
277 * \lreturn A cropped image.
279 static int
280 luaA_image_crop(lua_State *L)
282 image_t *image = luaL_checkudata(L, 1, "image"), *new;
283 int x = luaL_checkint(L, 2);
284 int y = luaL_checkint(L, 3);
285 int w = luaL_checkint(L, 4);
286 int h = luaL_checkint(L, 5);
288 new = image_new(L);
290 imlib_context_set_image(image->image);
291 new->image = imlib_create_cropped_image(x, y, w, h);
293 image_compute(new);
295 return 1;
298 /** Crop the image to the given rectangle and scales it.
299 * \param L The Lua VM state.
300 * \return The number of elements pushed on stack.
301 * \luastack
302 * \lvalue An image.
303 * \lparam The top left x coordinate of the source rectangle.
304 * \lparam The top left y coordinate of the source rectangle.
305 * \lparam The width of the source rectangle.
306 * \lparam The height of the source rectangle.
307 * \lparam The width of the destination rectangle.
308 * \lparam The height of the destination rectangle.
309 * \lreturn A cropped image.
311 static int
312 luaA_image_crop_and_scale(lua_State *L)
314 image_t *image = luaL_checkudata(L, 1, "image"), *new;
315 int source_x = luaL_checkint(L, 2);
316 int source_y = luaL_checkint(L, 3);
317 int w = luaL_checkint(L, 4);
318 int h = luaL_checkint(L, 5);
319 int dest_w = luaL_checkint(L, 6);
320 int dest_h = luaL_checkint(L, 7);
322 new = image_new(L);
324 imlib_context_set_image(image->image);
325 new->image = imlib_create_cropped_scaled_image(source_x,
326 source_y,
327 w, h,
328 dest_w, dest_h);
330 image_compute(new);
332 return 1;
335 /** Draw a pixel in an image
336 * \param L The Lua VM state
337 * \luastack
338 * \lvalua An image.
339 * \lparam The x coordinate of the pixel to draw
340 * \lparam The y coordinate of the pixel to draw
341 * \lparam The color to draw the pixel in
343 static int
344 luaA_image_draw_pixel(lua_State *L)
346 size_t len;
347 color_t color;
348 color_init_cookie_t cookie;
349 image_t *image = luaL_checkudata(L, 1, "image");
350 int x = luaL_checkint(L, 2);
351 int y = luaL_checkint(L, 3);
352 const char *buf = luaL_checklstring(L, 4, &len);
354 cookie = color_init_unchecked(&color, buf, len);
355 imlib_context_set_image(image->image);
356 color_init_reply(cookie);
358 if((x > imlib_image_get_width()) || (y > imlib_image_get_height()))
359 return 0;
361 imlib_context_set_color(color.red, color.green, color.blue, color.alpha);
362 imlib_image_draw_pixel(x, y, 1);
363 image_compute(image);
364 return 0;
367 /** Draw a line in an image
368 * \param L The Lua VM state
369 * \luastack
370 * \lvalua An image.
371 * \lparam The x1 coordinate of the line to draw
372 * \lparam The y1 coordinate of the line to draw
373 * \lparam The x2 coordinate of the line to draw
374 * \lparam The y2 coordinate of the line to draw
375 * \lparam The color to draw the line in
377 static int
378 luaA_image_draw_line(lua_State *L)
380 size_t len;
381 color_t color;
382 color_init_cookie_t cookie;
383 image_t *image = luaL_checkudata(L, 1, "image");
384 int x1 = luaL_checkint(L, 2);
385 int y1 = luaL_checkint(L, 3);
386 int x2 = luaL_checkint(L, 4);
387 int y2 = luaL_checkint(L, 5);
388 const char *buf = luaL_checklstring(L, 6, &len);
390 cookie = color_init_unchecked(&color, buf, len);
391 imlib_context_set_image(image->image);
392 color_init_reply(cookie);
394 if((MAX(x1,x2) > imlib_image_get_width()) || (MAX(y1,y2) > imlib_image_get_height()))
395 return 0;
397 imlib_context_set_color(color.red, color.green, color.blue, color.alpha);
398 imlib_image_draw_line(x1, y1, x2, y2, 0);
399 image_compute(image);
400 return 0;
403 /** Draw a rectangle in an image
404 * \param L The Lua VM state
405 * \luastack
406 * \lvalua An image.
407 * \lparam The x coordinate of the rectangles top left corner
408 * \lparam The y coordinate of the rectangles top left corner
409 * \lparam The width of the rectangle
410 * \lparam The height of the rectangle
411 * \lparam True if the rectangle should be filled, False otherwise
412 * \lparam The color to draw the rectangle in
414 static int
415 luaA_image_draw_rectangle(lua_State *L)
417 size_t len;
418 color_t color;
419 color_init_cookie_t cookie;
420 image_t *image = luaL_checkudata(L, 1, "image");
421 int x = luaL_checkint(L, 2);
422 int y = luaL_checkint(L, 3);
423 int width = luaL_checkint(L, 4);
424 int height = luaL_checkint(L, 5);
425 int fill = luaA_checkboolean(L, 6);
426 const char *buf = luaL_checklstring(L, 7, &len);
428 cookie = color_init_unchecked(&color, buf, len);
429 imlib_context_set_image(image->image);
430 color_init_reply(cookie);
432 if((x > imlib_image_get_width()) || (x + width > imlib_image_get_width()))
433 return 0;
434 if((y > imlib_image_get_height()) || (y + height > imlib_image_get_height()))
435 return 0;
437 imlib_context_set_color(color.red, color.green, color.blue, color.alpha);
438 if(!fill)
439 imlib_image_draw_rectangle(x, y, width, height);
440 else
441 imlib_image_fill_rectangle(x, y, width, height);
442 image_compute(image);
443 return 0;
446 /** Draw a circle in an image
447 * \param L The Lua VM state
448 * \luastack
449 * \lvalua An image.
450 * \lparam The x coordinate of the center of the circle
451 * \lparam The y coordinate of the center of the circle
452 * \lparam The horizontal amplitude (width)
453 * \lparam The vertical amplitude (height)
454 * \lparam True if the circle should be filled, False otherwise
455 * \lparam The color to draw the circle in
457 static int
458 luaA_image_draw_circle(lua_State *L)
460 size_t len;
461 color_t color;
462 color_init_cookie_t cookie;
463 image_t *image = luaL_checkudata(L, 1, "image");
464 int x = luaL_checkint(L, 2);
465 int y = luaL_checkint(L, 3);
466 int ah = luaL_checkint(L, 4);
467 int av = luaL_checkint(L, 5);
468 int fill = luaA_checkboolean(L, 6);
469 const char *buf = luaL_checklstring(L, 7, &len);
471 cookie = color_init_unchecked(&color, buf, len);
472 imlib_context_set_image(image->image);
473 color_init_reply(cookie);
475 if((x > imlib_image_get_width()) || (x + ah > imlib_image_get_width()) || (x - ah < 0))
476 return 0;
477 if((y > imlib_image_get_height()) || (y + av > imlib_image_get_height()) || (y - av < 0))
478 return 0;
480 imlib_context_set_color(color.red, color.green, color.blue, color.alpha);
481 if(!fill)
482 imlib_image_draw_ellipse(x, y, ah, av);
483 else
484 imlib_image_fill_ellipse(x, y, ah, av);
485 image_compute(image);
486 return 0;
489 /** Saves the image to the given path. The file extension (e.g. .png or .jpg)
490 * will affect the output format.
491 * \param L The Lua VM state.
492 * \return The number of elements pushed on stack.
493 * \luastack
494 * \lvalue An image.
495 * \lparam The image path.
497 static int
498 luaA_image_save(lua_State *L)
500 image_t *image = luaL_checkudata(L, 1, "image");
501 const char *path = luaL_checkstring(L, 2);
502 Imlib_Load_Error err;
504 imlib_context_set_image(image->image);
505 imlib_save_image_with_error_return(path, &err);
507 if(err != IMLIB_LOAD_ERROR_NONE)
508 warn("cannot save image %s: %s", path, image_imlib_load_strerror(err));
510 return 0;
513 /** Image object.
514 * \param L The Lua VM state.
515 * \return The number of elements pushed on stack.
516 * \luastack
517 * \lvalue An image
518 * \lfield width The image width.
519 * \lfield height The image height.
521 static int
522 luaA_image_index(lua_State *L)
524 if(luaA_usemetatable(L, 1, 2))
525 return 1;
527 image_t *image = luaL_checkudata(L, 1, "image");
528 size_t len;
529 const char *attr = luaL_checklstring(L, 2, &len);
531 switch(a_tokenize(attr, len))
533 case A_TK_WIDTH:
534 imlib_context_set_image(image->image);
535 lua_pushnumber(L, imlib_image_get_width());
536 break;
537 case A_TK_HEIGHT:
538 imlib_context_set_image(image->image);
539 lua_pushnumber(L, imlib_image_get_height());
540 break;
541 case A_TK_ALPHA:
542 imlib_context_set_image(image->image);
543 lua_pushboolean(L, imlib_image_has_alpha());
544 break;
545 default:
546 return 0;
549 return 1;
552 const struct luaL_reg awesome_image_methods[] =
554 { "__call", luaA_image_new },
555 { "argb32", luaA_image_argb32_new },
556 { NULL, NULL }
558 const struct luaL_reg awesome_image_meta[] =
560 { "__index", luaA_image_index },
561 { "rotate", luaA_image_rotate },
562 { "orientate", luaA_image_orientate },
563 { "crop", luaA_image_crop },
564 { "crop_and_scale", luaA_image_crop_and_scale },
565 { "save", luaA_image_save },
566 /* draw on images, whee! */
567 { "draw_pixel", luaA_image_draw_pixel },
568 { "draw_line", luaA_image_draw_line },
569 { "draw_rectangle", luaA_image_draw_rectangle },
570 { "draw_circle", luaA_image_draw_circle },
571 /* */
572 { "__gc", luaA_image_gc },
573 { "__tostring", luaA_image_tostring },
574 { NULL, NULL }
577 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80