naughty: add vertical margin
[awesome.git] / image.c
bloba9d54a3a6d1198c81baac12b04375c2c9debc90d
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;
112 if(image->isupdated)
113 return image->data;
115 imlib_context_set_image(image->image);
117 data = imlib_image_get_data_for_reading_only();
119 size = imlib_image_get_width() * imlib_image_get_height();
121 p_realloc(&image->data, size * 4);
122 dataimg = image->data;
124 for(i = 0; i < size; i++, dataimg += 4)
126 #if AWESOME_IS_BIG_ENDIAN
127 dataimg[0] = (data[i] >> 24) & 0xff; /* A */
128 /* cairo wants pre-multiplied alpha */
129 alpha = dataimg[0] / 255.0;
130 dataimg[1] = ((data[i] >> 16) & 0xff) * alpha; /* R */
131 dataimg[2] = ((data[i] >> 8) & 0xff) * alpha; /* G */
132 dataimg[3] = (data[i] & 0xff) * alpha; /* B */
133 #else
134 dataimg[3] = (data[i] >> 24) & 0xff; /* A */
135 /* cairo wants pre-multiplied alpha */
136 alpha = dataimg[3] / 255.0;
137 dataimg[2] = ((data[i] >> 16) & 0xff) * alpha; /* R */
138 dataimg[1] = ((data[i] >> 8) & 0xff) * alpha; /* G */
139 dataimg[0] = (data[i] & 0xff) * alpha; /* B */
140 #endif
143 image->isupdated = true;
145 return image->data;
148 /** Create a new image from ARGB32 data.
149 * \param width The image width.
150 * \param height The image height.
151 * \param data The image data.
152 * \return 1 if an image has been pushed on stack, 0 otherwise.
155 image_new_from_argb32(int width, int height, uint32_t *data)
157 Imlib_Image imimage;
159 if((imimage = imlib_create_image_using_copied_data(width, height, data)))
161 imlib_context_set_image(imimage);
162 imlib_image_set_has_alpha(true);
163 image_t *image = image_new(globalconf.L);
164 image->image = imimage;
165 return 1;
168 return 0;
171 /** Load an image from filename.
172 * \param filename The image file to load.
173 * \return 1 if image is loaded and on stack, 0 otherwise.
175 static int
176 image_new_from_file(const char *filename)
178 Imlib_Image imimage;
179 Imlib_Load_Error e = IMLIB_LOAD_ERROR_NONE;
180 image_t *image;
182 if(!filename)
183 return 0;
185 if(!(imimage = imlib_load_image_with_error_return(filename, &e)))
187 warn("cannot load image %s: %s", filename, image_imlib_load_strerror(e));
188 return 0;
191 image = image_new(globalconf.L);
192 image->image = imimage;
194 return 1;
197 /** Create a new image object.
198 * \param L The Lua stack.
199 * \return The number of elements pushed on stack.
200 * \luastack
201 * \lparam The image path.
202 * \lreturn An image object.
204 static int
205 luaA_image_new(lua_State *L)
207 const char *filename;
209 if((filename = lua_tostring(L, 2)))
210 return image_new_from_file(filename);
211 return 0;
214 /** Create a new image object from ARGB32 data.
215 * \param L The Lua stack.
216 * \return The number of elements pushed on stack.
217 * \luastack
218 * \lparam The image width.
219 * \lparam The image height.
220 * \lparam The image data as a string in ARGB32 format, or nil to create an
221 * empty image.
222 * \lreturn An image object.
224 static int
225 luaA_image_argb32_new(lua_State *L)
227 size_t len;
228 unsigned int width = luaL_checknumber(L, 1);
229 unsigned int height = luaL_checknumber(L, 2);
231 if(lua_isnil(L, 3))
233 uint32_t *data = p_new(uint32_t, width * height);
234 return image_new_from_argb32(width, height, data);
237 const char *data = luaL_checklstring(L, 3, &len);
239 if(width * height * 4 != len)
240 luaL_error(L, "string size does not match image size");
242 return image_new_from_argb32(width, height, (uint32_t *) data);
245 /** Performs 90 degree rotations on the current image. Passing 0 orientation
246 * does not rotate, 1 rotates clockwise by 90 degree, 2, rotates clockwise by
247 * 180 degrees, 3 rotates clockwise by 270 degrees.
248 * \param L The Lua VM state.
249 * \return The number of elements pushed on stack.
250 * \luastack
251 * \lvalue An image.
252 * \lparam The rotation to perform.
254 static int
255 luaA_image_orientate(lua_State *L)
257 image_t *image = luaL_checkudata(L, 1, "image");
258 int orientation = luaL_checknumber(L, 2);
260 imlib_context_set_image(image->image);
261 imlib_image_orientate(orientation);
263 image->isupdated = false;
265 return 0;
268 /** Rotate an image with specified angle radians and return a new image.
269 * \param L The Lua VM state.
270 * \return The number of elements pushed on stack.
271 * \luastack
272 * \lvalue An image.
273 * \lparam The angle in radians.
274 * \lreturn A rotated image.
276 static int
277 luaA_image_rotate(lua_State *L)
279 image_t *image = luaL_checkudata(L, 1, "image"), *new;
280 double angle = luaL_checknumber(L, 2);
282 new = image_new(L);
284 imlib_context_set_image(image->image);
285 new->image = imlib_create_rotated_image(angle);
287 return 1;
290 /** Crop an image to the given rectangle.
291 * \return The number of elements pushed on stack.
292 * \luastack
293 * \lvalue An image.
294 * \lparam The top left x coordinate of the rectangle.
295 * \lparam The top left y coordinate of the rectangle.
296 * \lparam The width of the rectangle.
297 * \lparam The height of the rectangle.
298 * \lreturn A cropped image.
300 static int
301 luaA_image_crop(lua_State *L)
303 image_t *image = luaL_checkudata(L, 1, "image"), *new;
304 int x = luaL_checkint(L, 2);
305 int y = luaL_checkint(L, 3);
306 int w = luaL_checkint(L, 4);
307 int h = luaL_checkint(L, 5);
309 new = image_new(L);
311 imlib_context_set_image(image->image);
312 new->image = imlib_create_cropped_image(x, y, w, h);
314 return 1;
317 /** Crop the image to the given rectangle and scales it.
318 * \param L The Lua VM state.
319 * \return The number of elements pushed on stack.
320 * \luastack
321 * \lvalue An image.
322 * \lparam The top left x coordinate of the source rectangle.
323 * \lparam The top left y coordinate of the source rectangle.
324 * \lparam The width of the source rectangle.
325 * \lparam The height of the source rectangle.
326 * \lparam The width of the destination rectangle.
327 * \lparam The height of the destination rectangle.
328 * \lreturn A cropped image.
330 static int
331 luaA_image_crop_and_scale(lua_State *L)
333 image_t *image = luaL_checkudata(L, 1, "image"), *new;
334 int source_x = luaL_checkint(L, 2);
335 int source_y = luaL_checkint(L, 3);
336 int w = luaL_checkint(L, 4);
337 int h = luaL_checkint(L, 5);
338 int dest_w = luaL_checkint(L, 6);
339 int dest_h = luaL_checkint(L, 7);
341 new = image_new(L);
343 imlib_context_set_image(image->image);
344 new->image = imlib_create_cropped_scaled_image(source_x,
345 source_y,
346 w, h,
347 dest_w, dest_h);
349 return 1;
352 /** Draw a pixel in an image
353 * \param L The Lua VM state
354 * \luastack
355 * \lvalua An image.
356 * \lparam The x coordinate of the pixel to draw
357 * \lparam The y coordinate of the pixel to draw
358 * \lparam The color to draw the pixel in
360 static int
361 luaA_image_draw_pixel(lua_State *L)
363 size_t len;
364 color_t color;
365 color_init_cookie_t cookie;
366 image_t *image = luaL_checkudata(L, 1, "image");
367 int x = luaL_checkint(L, 2);
368 int y = luaL_checkint(L, 3);
369 const char *buf = luaL_checklstring(L, 4, &len);
371 cookie = color_init_unchecked(&color, buf, len);
372 imlib_context_set_image(image->image);
373 color_init_reply(cookie);
375 if((x > imlib_image_get_width()) || (y > imlib_image_get_height()))
376 return 0;
378 imlib_context_set_color(color.red, color.green, color.blue, color.alpha);
379 imlib_image_draw_pixel(x, y, 1);
380 image->isupdated = false;
381 return 0;
384 /** Draw a line in an image
385 * \param L The Lua VM state
386 * \luastack
387 * \lvalua An image.
388 * \lparam The x1 coordinate of the line to draw
389 * \lparam The y1 coordinate of the line to draw
390 * \lparam The x2 coordinate of the line to draw
391 * \lparam The y2 coordinate of the line to draw
392 * \lparam The color to draw the line in
394 static int
395 luaA_image_draw_line(lua_State *L)
397 size_t len;
398 color_t color;
399 color_init_cookie_t cookie;
400 image_t *image = luaL_checkudata(L, 1, "image");
401 int x1 = luaL_checkint(L, 2);
402 int y1 = luaL_checkint(L, 3);
403 int x2 = luaL_checkint(L, 4);
404 int y2 = luaL_checkint(L, 5);
405 const char *buf = luaL_checklstring(L, 6, &len);
407 cookie = color_init_unchecked(&color, buf, len);
408 imlib_context_set_image(image->image);
409 color_init_reply(cookie);
411 if((MAX(x1,x2) > imlib_image_get_width()) || (MAX(y1,y2) > imlib_image_get_height()))
412 return 0;
414 imlib_context_set_color(color.red, color.green, color.blue, color.alpha);
415 imlib_image_draw_line(x1, y1, x2, y2, 0);
416 image->isupdated = false;
417 return 0;
420 /** Draw a rectangle in an image
421 * \param L The Lua VM state
422 * \luastack
423 * \lvalua An image.
424 * \lparam The x coordinate of the rectangles top left corner
425 * \lparam The y coordinate of the rectangles top left corner
426 * \lparam The width of the rectangle
427 * \lparam The height of the rectangle
428 * \lparam True if the rectangle should be filled, False otherwise
429 * \lparam The color to draw the rectangle in
431 static int
432 luaA_image_draw_rectangle(lua_State *L)
434 size_t len;
435 color_t color;
436 color_init_cookie_t cookie;
437 image_t *image = luaL_checkudata(L, 1, "image");
438 int x = luaL_checkint(L, 2);
439 int y = luaL_checkint(L, 3);
440 int width = luaL_checkint(L, 4);
441 int height = luaL_checkint(L, 5);
442 int fill = luaA_checkboolean(L, 6);
443 const char *buf = luaL_checklstring(L, 7, &len);
445 cookie = color_init_unchecked(&color, buf, len);
446 imlib_context_set_image(image->image);
447 color_init_reply(cookie);
449 if((x > imlib_image_get_width()) || (x + width > imlib_image_get_width()))
450 return 0;
451 if((y > imlib_image_get_height()) || (y + height > imlib_image_get_height()))
452 return 0;
454 imlib_context_set_color(color.red, color.green, color.blue, color.alpha);
455 if(!fill)
456 imlib_image_draw_rectangle(x, y, width, height);
457 else
458 imlib_image_fill_rectangle(x, y, width, height);
459 image->isupdated = false;
460 return 0;
463 /** Convert a table to a color range and set it as current color range.
464 * \param L The Lua VM state.
465 * \param ud The index of the table.
466 * \return A color range that you are responsible to free.
468 static Imlib_Color_Range
469 luaA_table_to_color_range(lua_State *L, int ud)
471 Imlib_Color_Range range = imlib_create_color_range();
473 imlib_context_set_color_range(range);
475 for(size_t i = 1; i <= lua_objlen(L, ud); i++)
477 /* get t[i] */
478 lua_pushnumber(L, i);
479 lua_gettable(L, ud);
481 size_t len;
482 const char *colstr = lua_tolstring(L, -1, &len);
484 if(colstr)
486 color_t color;
488 color_init_cookie_t cookie = color_init_unchecked(&color, colstr, len);
490 /* get value with colstr as key in table */
491 lua_pushvalue(L, -1);
492 lua_gettable(L, ud);
494 /* convert the distance, if any, to number, or set 1 */
495 int distance = lua_tonumber(L, -1);
496 /* remove distance */
497 lua_pop(L, 1);
499 color_init_reply(cookie);
501 imlib_context_set_color(color.red, color.green, color.blue, color.alpha);
503 imlib_add_color_to_color_range(distance);
506 /* remove color string (value) */
507 lua_pop(L, 1);
510 return range;
513 /** Draw a rectangle in an image with gradient color.
514 * \param L The Lua VM state.
515 * \return The number of elements pushed on stack.
516 * \luastack
517 * \lvalua An image.
518 * \lparam The x coordinate of the rectangles top left corner.
519 * \lparam The y coordinate of the rectangles top left corner.
520 * \lparam The width of the rectangle.
521 * \lparam The height of the rectangle.
522 * \lparam A table with the color to draw the rectangle. You can specified the
523 * color distance from the previous one by setting t[color] = distance.
524 * \lparam The angle of gradient.
526 static int
527 luaA_image_draw_rectangle_gradient(lua_State *L)
529 image_t *image = luaL_checkudata(L, 1, "image");
530 int x = luaL_checkint(L, 2);
531 int y = luaL_checkint(L, 3);
532 int width = luaL_checkint(L, 4);
533 int height = luaL_checkint(L, 5);
534 luaA_checktable(L, 6);
535 double angle = luaL_checknumber(L, 7);
537 if((x > imlib_image_get_width()) || (x + width > imlib_image_get_width()))
538 return 0;
539 if((y > imlib_image_get_height()) || (y + height > imlib_image_get_height()))
540 return 0;
542 imlib_context_set_image(image->image);
544 luaA_table_to_color_range(L, 6);
546 imlib_image_fill_color_range_rectangle(x, y, width, height, angle);
548 imlib_free_color_range();
550 image->isupdated = false;
552 return 0;
555 /** Draw a circle in an image
556 * \param L The Lua VM state
557 * \luastack
558 * \lvalua An image.
559 * \lparam The x coordinate of the center of the circle
560 * \lparam The y coordinate of the center of the circle
561 * \lparam The horizontal amplitude (width)
562 * \lparam The vertical amplitude (height)
563 * \lparam True if the circle should be filled, False otherwise
564 * \lparam The color to draw the circle in
566 static int
567 luaA_image_draw_circle(lua_State *L)
569 size_t len;
570 color_t color;
571 color_init_cookie_t cookie;
572 image_t *image = luaL_checkudata(L, 1, "image");
573 int x = luaL_checkint(L, 2);
574 int y = luaL_checkint(L, 3);
575 int ah = luaL_checkint(L, 4);
576 int av = luaL_checkint(L, 5);
577 int fill = luaA_checkboolean(L, 6);
578 const char *buf = luaL_checklstring(L, 7, &len);
580 cookie = color_init_unchecked(&color, buf, len);
581 imlib_context_set_image(image->image);
582 color_init_reply(cookie);
584 if((x > imlib_image_get_width()) || (x + ah > imlib_image_get_width()) || (x - ah < 0))
585 return 0;
586 if((y > imlib_image_get_height()) || (y + av > imlib_image_get_height()) || (y - av < 0))
587 return 0;
589 imlib_context_set_color(color.red, color.green, color.blue, color.alpha);
590 if(!fill)
591 imlib_image_draw_ellipse(x, y, ah, av);
592 else
593 imlib_image_fill_ellipse(x, y, ah, av);
594 image->isupdated = false;
595 return 0;
598 /** Saves the image to the given path. The file extension (e.g. .png or .jpg)
599 * will affect the output format.
600 * \param L The Lua VM state.
601 * \return The number of elements pushed on stack.
602 * \luastack
603 * \lvalue An image.
604 * \lparam The image path.
606 static int
607 luaA_image_save(lua_State *L)
609 image_t *image = luaL_checkudata(L, 1, "image");
610 const char *path = luaL_checkstring(L, 2);
611 Imlib_Load_Error err;
613 imlib_context_set_image(image->image);
614 imlib_save_image_with_error_return(path, &err);
616 if(err != IMLIB_LOAD_ERROR_NONE)
617 warn("cannot save image %s: %s", path, image_imlib_load_strerror(err));
619 return 0;
622 /** Insert one image into another.
623 * \param L The Lua VM state.
624 * \return The number of elements pushed on stack.
625 * \luastack
626 * \lvalue An image.
627 * \lparam The image to insert.
628 * \lparam The X offset of the image to insert (optional).
629 * \lparam The Y offset of the image to insert (optional).
630 * \lparam The horizontal offset of the upper right image corner (optional).
631 * \lparam The vertical offset of the upper right image corner (optional).
632 * \lparam The horizontal offset of the lower left image corner (optional).
633 * \lparam The vertical offset of the lower left image corner (optional).
634 * \lparam The X coordinate of the source rectangle (optional).
635 * \lparam The Y coordinate of the source rectangle (optional).
636 * \lparam The width of the source rectangle (optional).
637 * \lparam The height of the source rectangle (optional).
639 static int
640 luaA_image_insert(lua_State *L)
642 image_t *image_target = luaL_checkudata(L, 1, "image");
643 image_t *image_source = luaL_checkudata(L, 2, "image");
644 int xoff = luaL_optnumber(L, 3, 0);
645 int yoff = luaL_optnumber(L, 4, 0);
647 int xsrc = luaL_optnumber(L, 5, 0);
648 int ysrc = luaL_optnumber(L, 6, 0);
649 int wsrc = luaL_optnumber(L, 7, image_getwidth(image_source));
650 int hsrc = luaL_optnumber(L, 8, image_getheight(image_source));
652 int hxoff = luaL_optnumber(L, 9, image_getwidth(image_source));
653 int hyoff = luaL_optnumber(L, 10, 0);
655 int vxoff = luaL_optnumber(L, 11, 0);
656 int vyoff = luaL_optnumber(L, 12, image_getheight(image_source));
658 imlib_context_set_image(image_target->image);
660 imlib_blend_image_onto_image_skewed(image_source->image, 0,
661 /* source rectangle */
662 xsrc, ysrc, wsrc, hsrc,
663 /* position of the source image in the target image */
664 xoff, yoff,
665 /* axis offsets for the source image, (w|0|0|h)
666 * is the default */
667 hxoff, hyoff, vxoff, vyoff);
669 image_target->isupdated = false;
671 return 0;
674 /** Image object.
675 * \param L The Lua VM state.
676 * \return The number of elements pushed on stack.
677 * \luastack
678 * \lvalue An image
679 * \lfield width The image width.
680 * \lfield height The image height.
682 static int
683 luaA_image_index(lua_State *L)
685 if(luaA_usemetatable(L, 1, 2))
686 return 1;
688 image_t *image = luaL_checkudata(L, 1, "image");
689 size_t len;
690 const char *attr = luaL_checklstring(L, 2, &len);
692 switch(a_tokenize(attr, len))
694 case A_TK_WIDTH:
695 lua_pushnumber(L, image_getwidth(image));
696 break;
697 case A_TK_HEIGHT:
698 lua_pushnumber(L, image_getheight(image));
699 break;
700 case A_TK_ALPHA:
701 imlib_context_set_image(image->image);
702 lua_pushboolean(L, imlib_image_has_alpha());
703 break;
704 default:
705 return 0;
708 return 1;
711 const struct luaL_reg awesome_image_methods[] =
713 { "__call", luaA_image_new },
714 { "argb32", luaA_image_argb32_new },
715 { NULL, NULL }
717 const struct luaL_reg awesome_image_meta[] =
719 { "__index", luaA_image_index },
720 { "rotate", luaA_image_rotate },
721 { "orientate", luaA_image_orientate },
722 { "crop", luaA_image_crop },
723 { "crop_and_scale", luaA_image_crop_and_scale },
724 { "save", luaA_image_save },
725 { "insert", luaA_image_insert },
726 /* draw on images, whee! */
727 { "draw_pixel", luaA_image_draw_pixel },
728 { "draw_line", luaA_image_draw_line },
729 { "draw_rectangle", luaA_image_draw_rectangle },
730 { "draw_rectangle_gradient", luaA_image_draw_rectangle_gradient },
731 { "draw_circle", luaA_image_draw_circle },
732 { "__gc", luaA_image_gc },
733 { "__tostring", luaA_image_tostring },
734 { NULL, NULL }
737 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80