wibox: do not set need_arrange on wibox detach
[awesome.git] / image.c
blob680c52dd2a1c59606a8216c782c200ad7c41d254
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 /** Get image width.
79 * \param image The image.
80 * \return The image width in pixel.
82 int
83 image_getwidth(image_t *image)
85 imlib_context_set_image(image->image);
86 return imlib_image_get_width();
89 /** Get image height.
90 * \param image The image.
91 * \return The image height in pixel.
93 int
94 image_getheight(image_t *image)
96 imlib_context_set_image(image->image);
97 return imlib_image_get_height();
100 /** Get the ARGB32 data from an image.
101 * \param image The image.
102 * \return Data.
104 uint8_t *
105 image_getdata(image_t *image)
107 int size, i;
108 uint32_t *data;
109 double alpha;
110 uint8_t *dataimg;
111 #if AWESOME_IS_BIG_ENDIAN
112 const int index_a = 0, index_r = 1, index_g = 2, index_b = 3;
113 #else
114 const int index_a = 3, index_r = 2, index_g = 1, index_b = 0;
115 #endif
117 if(image->isupdated)
118 return image->data;
120 imlib_context_set_image(image->image);
122 data = imlib_image_get_data_for_reading_only();
124 size = imlib_image_get_width() * imlib_image_get_height();
126 p_realloc(&image->data, size * 4);
127 dataimg = image->data;
129 for(i = 0; i < size; i++, dataimg += 4)
131 dataimg[index_a] = (data[i] >> 24) & 0xff; /* A */
132 /* cairo wants pre-multiplied alpha */
133 alpha = dataimg[index_a] / 255.0;
134 dataimg[index_r] = ((data[i] >> 16) & 0xff) * alpha; /* R */
135 dataimg[index_g] = ((data[i] >> 8) & 0xff) * alpha; /* G */
136 dataimg[index_b] = (data[i] & 0xff) * alpha; /* B */
139 image->isupdated = true;
141 return image->data;
144 /** Create a new image from ARGB32 data.
145 * \param width The image width.
146 * \param height The image height.
147 * \param data The image data.
148 * \return 1 if an image has been pushed on stack, 0 otherwise.
151 image_new_from_argb32(int width, int height, uint32_t *data)
153 Imlib_Image imimage;
155 if((imimage = imlib_create_image_using_copied_data(width, height, data)))
157 imlib_context_set_image(imimage);
158 imlib_image_set_has_alpha(true);
159 image_t *image = image_new(globalconf.L);
160 image->image = imimage;
161 return 1;
164 return 0;
167 /** Load an image from filename.
168 * \param filename The image file to load.
169 * \return 1 if image is loaded and on stack, 0 otherwise.
171 static int
172 image_new_from_file(const char *filename)
174 Imlib_Image imimage;
175 Imlib_Load_Error e = IMLIB_LOAD_ERROR_NONE;
176 image_t *image;
178 if(!filename)
179 return 0;
181 if(!(imimage = imlib_load_image_with_error_return(filename, &e)))
183 warn("cannot load image %s: %s", filename, image_imlib_load_strerror(e));
184 return 0;
187 image = image_new(globalconf.L);
188 image->image = imimage;
190 return 1;
193 /** Create a new image object.
194 * \param L The Lua stack.
195 * \return The number of elements pushed on stack.
196 * \luastack
197 * \lparam The image path.
198 * \lreturn An image object.
200 static int
201 luaA_image_new(lua_State *L)
203 const char *filename;
205 if((filename = lua_tostring(L, 2)))
206 return image_new_from_file(filename);
207 return 0;
210 /** Create a new image object from ARGB32 data.
211 * \param L The Lua stack.
212 * \return The number of elements pushed on stack.
213 * \luastack
214 * \lparam The image width.
215 * \lparam The image height.
216 * \lparam The image data as a string in ARGB32 format, or nil to create an
217 * empty image.
218 * \lreturn An image object.
220 static int
221 luaA_image_argb32_new(lua_State *L)
223 size_t len;
224 unsigned int width = luaL_checknumber(L, 1);
225 unsigned int height = luaL_checknumber(L, 2);
227 if(lua_isnil(L, 3))
229 uint32_t *data = p_new(uint32_t, width * height);
230 return image_new_from_argb32(width, height, data);
233 const char *data = luaL_checklstring(L, 3, &len);
235 if(width * height * 4 != len)
236 luaL_error(L, "string size does not match image size");
238 return image_new_from_argb32(width, height, (uint32_t *) data);
241 /** Performs 90 degree rotations on the current image. Passing 0 orientation
242 * does not rotate, 1 rotates clockwise by 90 degree, 2, rotates clockwise by
243 * 180 degrees, 3 rotates clockwise by 270 degrees.
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 rotation to perform.
250 static int
251 luaA_image_orientate(lua_State *L)
253 image_t *image = luaL_checkudata(L, 1, "image");
254 int orientation = luaL_checknumber(L, 2);
256 imlib_context_set_image(image->image);
257 imlib_image_orientate(orientation);
259 image->isupdated = false;
261 return 0;
264 /** Rotate an image with specified angle radians and return a new image.
265 * \param L The Lua VM state.
266 * \return The number of elements pushed on stack.
267 * \luastack
268 * \lvalue An image.
269 * \lparam The angle in radians.
270 * \lreturn A rotated image.
272 static int
273 luaA_image_rotate(lua_State *L)
275 image_t *image = luaL_checkudata(L, 1, "image"), *new;
276 double angle = luaL_checknumber(L, 2);
278 new = image_new(L);
280 imlib_context_set_image(image->image);
281 new->image = imlib_create_rotated_image(angle);
283 return 1;
286 /** Crop an image to the given rectangle.
287 * \return The number of elements pushed on stack.
288 * \luastack
289 * \lvalue An image.
290 * \lparam The top left x coordinate of the rectangle.
291 * \lparam The top left y coordinate of the rectangle.
292 * \lparam The width of the rectangle.
293 * \lparam The height of the rectangle.
294 * \lreturn A cropped image.
296 static int
297 luaA_image_crop(lua_State *L)
299 image_t *image = luaL_checkudata(L, 1, "image"), *new;
300 int x = luaL_checkint(L, 2);
301 int y = luaL_checkint(L, 3);
302 int w = luaL_checkint(L, 4);
303 int h = luaL_checkint(L, 5);
305 new = image_new(L);
307 imlib_context_set_image(image->image);
308 new->image = imlib_create_cropped_image(x, y, w, h);
310 return 1;
313 /** Crop the image to the given rectangle and scales it.
314 * \param L The Lua VM state.
315 * \return The number of elements pushed on stack.
316 * \luastack
317 * \lvalue An image.
318 * \lparam The top left x coordinate of the source rectangle.
319 * \lparam The top left y coordinate of the source rectangle.
320 * \lparam The width of the source rectangle.
321 * \lparam The height of the source rectangle.
322 * \lparam The width of the destination rectangle.
323 * \lparam The height of the destination rectangle.
324 * \lreturn A cropped image.
326 static int
327 luaA_image_crop_and_scale(lua_State *L)
329 image_t *image = luaL_checkudata(L, 1, "image"), *new;
330 int source_x = luaL_checkint(L, 2);
331 int source_y = luaL_checkint(L, 3);
332 int w = luaL_checkint(L, 4);
333 int h = luaL_checkint(L, 5);
334 int dest_w = luaL_checkint(L, 6);
335 int dest_h = luaL_checkint(L, 7);
337 new = image_new(L);
339 imlib_context_set_image(image->image);
340 new->image = imlib_create_cropped_scaled_image(source_x,
341 source_y,
342 w, h,
343 dest_w, dest_h);
345 return 1;
348 /** Draw a pixel in an image
349 * \param L The Lua VM state
350 * \luastack
351 * \lvalua An image.
352 * \lparam The x coordinate of the pixel to draw
353 * \lparam The y coordinate of the pixel to draw
354 * \lparam The color to draw the pixel in
356 static int
357 luaA_image_draw_pixel(lua_State *L)
359 size_t len;
360 color_t color;
361 color_init_cookie_t cookie;
362 image_t *image = luaL_checkudata(L, 1, "image");
363 int x = luaL_checkint(L, 2);
364 int y = luaL_checkint(L, 3);
365 const char *buf = luaL_checklstring(L, 4, &len);
367 cookie = color_init_unchecked(&color, buf, len);
368 imlib_context_set_image(image->image);
369 color_init_reply(cookie);
371 if((x > imlib_image_get_width()) || (y > imlib_image_get_height()))
372 return 0;
374 imlib_context_set_color(color.red, color.green, color.blue, color.alpha);
375 imlib_image_draw_pixel(x, y, 1);
376 image->isupdated = false;
377 return 0;
380 /** Draw a line in an image
381 * \param L The Lua VM state
382 * \luastack
383 * \lvalua An image.
384 * \lparam The x1 coordinate of the line to draw
385 * \lparam The y1 coordinate of the line to draw
386 * \lparam The x2 coordinate of the line to draw
387 * \lparam The y2 coordinate of the line to draw
388 * \lparam The color to draw the line in
390 static int
391 luaA_image_draw_line(lua_State *L)
393 size_t len;
394 color_t color;
395 color_init_cookie_t cookie;
396 image_t *image = luaL_checkudata(L, 1, "image");
397 int x1 = luaL_checkint(L, 2);
398 int y1 = luaL_checkint(L, 3);
399 int x2 = luaL_checkint(L, 4);
400 int y2 = luaL_checkint(L, 5);
401 const char *buf = luaL_checklstring(L, 6, &len);
403 cookie = color_init_unchecked(&color, buf, len);
404 imlib_context_set_image(image->image);
405 color_init_reply(cookie);
407 if((MAX(x1,x2) > imlib_image_get_width()) || (MAX(y1,y2) > imlib_image_get_height()))
408 return 0;
410 imlib_context_set_color(color.red, color.green, color.blue, color.alpha);
411 imlib_image_draw_line(x1, y1, x2, y2, 0);
412 image->isupdated = false;
413 return 0;
416 /** Draw a rectangle in an image
417 * \param L The Lua VM state
418 * \luastack
419 * \lvalua An image.
420 * \lparam The x coordinate of the rectangles top left corner
421 * \lparam The y coordinate of the rectangles top left corner
422 * \lparam The width of the rectangle
423 * \lparam The height of the rectangle
424 * \lparam True if the rectangle should be filled, False otherwise
425 * \lparam The color to draw the rectangle in
427 static int
428 luaA_image_draw_rectangle(lua_State *L)
430 size_t len;
431 color_t color;
432 color_init_cookie_t cookie;
433 image_t *image = luaL_checkudata(L, 1, "image");
434 int x = luaL_checkint(L, 2);
435 int y = luaL_checkint(L, 3);
436 int width = luaL_checkint(L, 4);
437 int height = luaL_checkint(L, 5);
438 int fill = luaA_checkboolean(L, 6);
439 const char *buf = luaL_checklstring(L, 7, &len);
441 cookie = color_init_unchecked(&color, buf, len);
442 imlib_context_set_image(image->image);
443 color_init_reply(cookie);
445 if((x > imlib_image_get_width()) || (x + width > imlib_image_get_width()))
446 return 0;
447 if((y > imlib_image_get_height()) || (y + height > imlib_image_get_height()))
448 return 0;
450 imlib_context_set_color(color.red, color.green, color.blue, color.alpha);
451 if(!fill)
452 imlib_image_draw_rectangle(x, y, width, height);
453 else
454 imlib_image_fill_rectangle(x, y, width, height);
455 image->isupdated = false;
456 return 0;
459 /** Convert a table to a color range and set it as current color range.
460 * \param L The Lua VM state.
461 * \param ud The index of the table.
462 * \return A color range that you are responsible to free.
464 static Imlib_Color_Range
465 luaA_table_to_color_range(lua_State *L, int ud)
467 Imlib_Color_Range range = imlib_create_color_range();
469 imlib_context_set_color_range(range);
471 for(size_t i = 1; i <= lua_objlen(L, ud); i++)
473 /* get t[i] */
474 lua_pushnumber(L, i);
475 lua_gettable(L, ud);
477 size_t len;
478 const char *colstr = lua_tolstring(L, -1, &len);
480 if(colstr)
482 color_t color;
484 color_init_cookie_t cookie = color_init_unchecked(&color, colstr, len);
486 /* get value with colstr as key in table */
487 lua_pushvalue(L, -1);
488 lua_gettable(L, ud);
490 /* convert the distance, if any, to number, or set 1 */
491 int distance = lua_tonumber(L, -1);
492 /* remove distance */
493 lua_pop(L, 1);
495 color_init_reply(cookie);
497 imlib_context_set_color(color.red, color.green, color.blue, color.alpha);
499 imlib_add_color_to_color_range(distance);
502 /* remove color string (value) */
503 lua_pop(L, 1);
506 return range;
509 /** Draw a rectangle in an image with gradient color.
510 * \param L The Lua VM state.
511 * \return The number of elements pushed on stack.
512 * \luastack
513 * \lvalua An image.
514 * \lparam The x coordinate of the rectangles top left corner.
515 * \lparam The y coordinate of the rectangles top left corner.
516 * \lparam The width of the rectangle.
517 * \lparam The height of the rectangle.
518 * \lparam A table with the color to draw the rectangle. You can specified the
519 * color distance from the previous one by setting t[color] = distance.
520 * \lparam The angle of gradient.
522 static int
523 luaA_image_draw_rectangle_gradient(lua_State *L)
525 image_t *image = luaL_checkudata(L, 1, "image");
526 int x = luaL_checkint(L, 2);
527 int y = luaL_checkint(L, 3);
528 int width = luaL_checkint(L, 4);
529 int height = luaL_checkint(L, 5);
530 luaA_checktable(L, 6);
531 double angle = luaL_checknumber(L, 7);
533 if((x > imlib_image_get_width()) || (x + width > imlib_image_get_width()))
534 return 0;
535 if((y > imlib_image_get_height()) || (y + height > imlib_image_get_height()))
536 return 0;
538 imlib_context_set_image(image->image);
540 luaA_table_to_color_range(L, 6);
542 imlib_image_fill_color_range_rectangle(x, y, width, height, angle);
544 imlib_free_color_range();
546 image->isupdated = false;
548 return 0;
551 /** Draw a circle in an image
552 * \param L The Lua VM state
553 * \luastack
554 * \lvalua An image.
555 * \lparam The x coordinate of the center of the circle
556 * \lparam The y coordinate of the center of the circle
557 * \lparam The horizontal amplitude (width)
558 * \lparam The vertical amplitude (height)
559 * \lparam True if the circle should be filled, False otherwise
560 * \lparam The color to draw the circle in
562 static int
563 luaA_image_draw_circle(lua_State *L)
565 size_t len;
566 color_t color;
567 color_init_cookie_t cookie;
568 image_t *image = luaL_checkudata(L, 1, "image");
569 int x = luaL_checkint(L, 2);
570 int y = luaL_checkint(L, 3);
571 int ah = luaL_checkint(L, 4);
572 int av = luaL_checkint(L, 5);
573 int fill = luaA_checkboolean(L, 6);
574 const char *buf = luaL_checklstring(L, 7, &len);
576 cookie = color_init_unchecked(&color, buf, len);
577 imlib_context_set_image(image->image);
578 color_init_reply(cookie);
580 if((x > imlib_image_get_width()) || (x + ah > imlib_image_get_width()) || (x - ah < 0))
581 return 0;
582 if((y > imlib_image_get_height()) || (y + av > imlib_image_get_height()) || (y - av < 0))
583 return 0;
585 imlib_context_set_color(color.red, color.green, color.blue, color.alpha);
586 if(!fill)
587 imlib_image_draw_ellipse(x, y, ah, av);
588 else
589 imlib_image_fill_ellipse(x, y, ah, av);
590 image->isupdated = false;
591 return 0;
594 /** Saves the image to the given path. The file extension (e.g. .png or .jpg)
595 * will affect the output format.
596 * \param L The Lua VM state.
597 * \return The number of elements pushed on stack.
598 * \luastack
599 * \lvalue An image.
600 * \lparam The image path.
602 static int
603 luaA_image_save(lua_State *L)
605 image_t *image = luaL_checkudata(L, 1, "image");
606 const char *path = luaL_checkstring(L, 2);
607 Imlib_Load_Error err;
609 imlib_context_set_image(image->image);
610 imlib_save_image_with_error_return(path, &err);
612 if(err != IMLIB_LOAD_ERROR_NONE)
613 warn("cannot save image %s: %s", path, image_imlib_load_strerror(err));
615 return 0;
618 /** Insert one image into another.
619 * \param L The Lua VM state.
620 * \return The number of elements pushed on stack.
621 * \luastack
622 * \lvalue An image.
623 * \lparam The image to insert.
624 * \lparam The X offset of the image to insert (optional).
625 * \lparam The Y offset of the image to insert (optional).
626 * \lparam The horizontal offset of the upper right image corner (optional).
627 * \lparam The vertical offset of the upper right image corner (optional).
628 * \lparam The horizontal offset of the lower left image corner (optional).
629 * \lparam The vertical offset of the lower left image corner (optional).
630 * \lparam The X coordinate of the source rectangle (optional).
631 * \lparam The Y coordinate of the source rectangle (optional).
632 * \lparam The width of the source rectangle (optional).
633 * \lparam The height of the source rectangle (optional).
635 static int
636 luaA_image_insert(lua_State *L)
638 image_t *image_target = luaL_checkudata(L, 1, "image");
639 image_t *image_source = luaL_checkudata(L, 2, "image");
640 int xoff = luaL_optnumber(L, 3, 0);
641 int yoff = luaL_optnumber(L, 4, 0);
643 int xsrc = luaL_optnumber(L, 5, 0);
644 int ysrc = luaL_optnumber(L, 6, 0);
645 int wsrc = luaL_optnumber(L, 7, image_getwidth(image_source));
646 int hsrc = luaL_optnumber(L, 8, image_getheight(image_source));
648 int hxoff = luaL_optnumber(L, 9, image_getwidth(image_source));
649 int hyoff = luaL_optnumber(L, 10, 0);
651 int vxoff = luaL_optnumber(L, 11, 0);
652 int vyoff = luaL_optnumber(L, 12, image_getheight(image_source));
654 imlib_context_set_image(image_target->image);
656 imlib_blend_image_onto_image_skewed(image_source->image, 0,
657 /* source rectangle */
658 xsrc, ysrc, wsrc, hsrc,
659 /* position of the source image in the target image */
660 xoff, yoff,
661 /* axis offsets for the source image, (w|0|0|h)
662 * is the default */
663 hxoff, hyoff, vxoff, vyoff);
665 image_target->isupdated = false;
667 return 0;
670 /** Image object.
671 * \param L The Lua VM state.
672 * \return The number of elements pushed on stack.
673 * \luastack
674 * \lvalue An image
675 * \lfield width The image width.
676 * \lfield height The image height.
678 static int
679 luaA_image_index(lua_State *L)
681 if(luaA_usemetatable(L, 1, 2))
682 return 1;
684 image_t *image = luaL_checkudata(L, 1, "image");
685 size_t len;
686 const char *attr = luaL_checklstring(L, 2, &len);
688 switch(a_tokenize(attr, len))
690 case A_TK_WIDTH:
691 lua_pushnumber(L, image_getwidth(image));
692 break;
693 case A_TK_HEIGHT:
694 lua_pushnumber(L, image_getheight(image));
695 break;
696 case A_TK_ALPHA:
697 imlib_context_set_image(image->image);
698 lua_pushboolean(L, imlib_image_has_alpha());
699 break;
700 default:
701 return 0;
704 return 1;
707 const struct luaL_reg awesome_image_methods[] =
709 { "__call", luaA_image_new },
710 { "argb32", luaA_image_argb32_new },
711 { NULL, NULL }
713 const struct luaL_reg awesome_image_meta[] =
715 { "__index", luaA_image_index },
716 { "rotate", luaA_image_rotate },
717 { "orientate", luaA_image_orientate },
718 { "crop", luaA_image_crop },
719 { "crop_and_scale", luaA_image_crop_and_scale },
720 { "save", luaA_image_save },
721 { "insert", luaA_image_insert },
722 /* draw on images, whee! */
723 { "draw_pixel", luaA_image_draw_pixel },
724 { "draw_line", luaA_image_draw_line },
725 { "draw_rectangle", luaA_image_draw_rectangle },
726 { "draw_rectangle_gradient", luaA_image_draw_rectangle_gradient },
727 { "draw_circle", luaA_image_draw_circle },
728 { "__gc", luaA_image_gc },
729 { "__tostring", luaA_image_tostring },
730 { NULL, NULL }
733 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80