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.
23 #include "common/tokenize.h"
24 #include "common/xutil.h"
25 #include <xcb/xcb_image.h>
28 luaA_image_gc(lua_State
*L
)
30 image_t
*p
= luaL_checkudata(L
, 1, "image");
31 imlib_context_set_image(p
->image
);
34 return luaA_object_gc(L
);
38 image_imlib_load_strerror(Imlib_Load_Error e
)
42 case IMLIB_LOAD_ERROR_FILE_DOES_NOT_EXIST
:
43 return "no such file or directory";
44 case IMLIB_LOAD_ERROR_FILE_IS_DIRECTORY
:
45 return "file is a directory";
46 case IMLIB_LOAD_ERROR_PERMISSION_DENIED_TO_READ
:
47 return "read permission denied";
48 case IMLIB_LOAD_ERROR_NO_LOADER_FOR_FILE_FORMAT
:
49 return "no loader for file format";
50 case IMLIB_LOAD_ERROR_PATH_TOO_LONG
:
51 return "path too long";
52 case IMLIB_LOAD_ERROR_PATH_COMPONENT_NON_EXISTANT
:
53 return "path component non existant";
54 case IMLIB_LOAD_ERROR_PATH_COMPONENT_NOT_DIRECTORY
:
55 return "path compoment not a directory";
56 case IMLIB_LOAD_ERROR_PATH_POINTS_OUTSIDE_ADDRESS_SPACE
:
57 return "path points oustide address space";
58 case IMLIB_LOAD_ERROR_TOO_MANY_SYMBOLIC_LINKS
:
59 return "too many symbolic links";
60 case IMLIB_LOAD_ERROR_OUT_OF_MEMORY
:
61 return "out of memory";
62 case IMLIB_LOAD_ERROR_OUT_OF_FILE_DESCRIPTORS
:
63 return "out of file descriptors";
64 case IMLIB_LOAD_ERROR_PERMISSION_DENIED_TO_WRITE
:
65 return "write permission denied";
66 case IMLIB_LOAD_ERROR_OUT_OF_DISK_SPACE
:
67 return "out of disk space";
68 case IMLIB_LOAD_ERROR_UNKNOWN
:
69 return "unknown error, that's really bad";
70 case IMLIB_LOAD_ERROR_NONE
:
71 return "no error, oops";
74 return "unknown error";
78 * \param image The image.
79 * \return The image width in pixel.
82 image_getwidth(image_t
*image
)
84 imlib_context_set_image(image
->image
);
85 return imlib_image_get_width();
89 * \param image The image.
90 * \return The image height in pixel.
93 image_getheight(image_t
*image
)
95 imlib_context_set_image(image
->image
);
96 return imlib_image_get_height();
99 /** Get the ARGB32 data from an image.
100 * \param image The image.
104 image_getdata(image_t
*image
)
110 #if AWESOME_IS_BIG_ENDIAN
111 const int index_a
= 0, index_r
= 1, index_g
= 2, index_b
= 3;
113 const int index_a
= 3, index_r
= 2, index_g
= 1, index_b
= 0;
119 imlib_context_set_image(image
->image
);
121 data
= imlib_image_get_data_for_reading_only();
123 size
= imlib_image_get_width() * imlib_image_get_height();
125 p_realloc(&image
->data
, size
* 4);
126 dataimg
= image
->data
;
128 for(i
= 0; i
< size
; i
++, dataimg
+= 4)
130 dataimg
[index_a
] = (data
[i
] >> 24) & 0xff; /* A */
131 /* cairo wants pre-multiplied alpha */
132 alpha
= dataimg
[index_a
] / 255.0;
133 dataimg
[index_r
] = ((data
[i
] >> 16) & 0xff) * alpha
; /* R */
134 dataimg
[index_g
] = ((data
[i
] >> 8) & 0xff) * alpha
; /* G */
135 dataimg
[index_b
] = (data
[i
] & 0xff) * alpha
; /* B */
138 image
->isupdated
= true;
145 image_draw_to_1bit_ximage(image_t
*image
, xcb_image_t
*img
)
147 imlib_context_set_image(image
->image
);
149 uint32_t *data
= imlib_image_get_data_for_reading_only();
151 int width
= imlib_image_get_width();
152 int height
= imlib_image_get_height();
154 for(int y
= 0; y
< height
; y
++)
155 for(int x
= 0; x
< width
; x
++)
161 // Sum up all color components ignoring alpha
162 tmp
= (data
[i
] >> 16) & 0xff;
163 tmp
+= (data
[i
] >> 8) & 0xff;
164 tmp
+= data
[i
] & 0xff;
166 pixel
= (tmp
/ 3 < 127) ? 0 : 1;
168 xcb_image_put_pixel(img
, x
, y
, pixel
);
172 // Convert an image to a 1bit pixmap
174 image_to_1bit_pixmap(image_t
*image
, xcb_drawable_t d
)
179 uint16_t width
, height
;
181 width
= image_getwidth(image
);
182 height
= image_getheight(image
);
184 /* Prepare the pixmap and gc */
185 pixmap
= xcb_generate_id(globalconf
.connection
);
186 xcb_create_pixmap(globalconf
.connection
, 1, pixmap
, d
, width
, height
);
188 gc
= xcb_generate_id(globalconf
.connection
);
189 xcb_create_gc(globalconf
.connection
, gc
, pixmap
, 0, NULL
);
191 /* Prepare the image */
192 img
= xcb_image_create_native(globalconf
.connection
, width
, height
,
193 XCB_IMAGE_FORMAT_XY_BITMAP
, 1, NULL
, 0, NULL
);
194 image_draw_to_1bit_ximage(image
, img
);
196 /* Paint the image to the pixmap */
197 xcb_image_put(globalconf
.connection
, pixmap
, gc
, img
, 0, 0, 0);
199 xcb_free_gc(globalconf
.connection
, gc
);
201 xcb_image_destroy(img
);
206 /** Create a new image from ARGB32 data.
207 * \param width The image width.
208 * \param height The image height.
209 * \param data The image data.
210 * \return 1 if an image has been pushed on stack, 0 otherwise.
213 image_new_from_argb32(int width
, int height
, uint32_t *data
)
217 if((imimage
= imlib_create_image_using_copied_data(width
, height
, data
)))
219 imlib_context_set_image(imimage
);
220 imlib_image_set_has_alpha(true);
221 image_t
*image
= image_new(globalconf
.L
);
222 image
->image
= imimage
;
229 /** Load an image from filename.
230 * \param filename The image file to load.
231 * \return 1 if image is loaded and on stack, 0 otherwise.
234 image_new_from_file(const char *filename
)
237 Imlib_Load_Error e
= IMLIB_LOAD_ERROR_NONE
;
243 if(!(imimage
= imlib_load_image_with_error_return(filename
, &e
)))
245 warn("cannot load image %s: %s", filename
, image_imlib_load_strerror(e
));
249 image
= image_new(globalconf
.L
);
250 image
->image
= imimage
;
255 /** Create a new image object.
256 * \param L The Lua stack.
257 * \return The number of elements pushed on stack.
259 * \lparam The image path.
260 * \lreturn An image object.
263 luaA_image_new(lua_State
*L
)
265 const char *filename
;
267 if((filename
= lua_tostring(L
, 2)))
268 return image_new_from_file(filename
);
272 /** Create a new image object from ARGB32 data.
273 * \param L The Lua stack.
274 * \return The number of elements pushed on stack.
276 * \lparam The image width.
277 * \lparam The image height.
278 * \lparam The image data as a string in ARGB32 format, or nil to create an
280 * \lreturn An image object.
283 luaA_image_argb32_new(lua_State
*L
)
286 unsigned int width
= luaL_checknumber(L
, 1);
287 unsigned int height
= luaL_checknumber(L
, 2);
291 uint32_t *data
= p_new(uint32_t, width
* height
);
292 return image_new_from_argb32(width
, height
, data
);
295 const char *data
= luaL_checklstring(L
, 3, &len
);
297 if(width
* height
* 4 != len
)
298 luaL_error(L
, "string size does not match image size");
300 return image_new_from_argb32(width
, height
, (uint32_t *) data
);
303 /** Performs 90 degree rotations on the current image. Passing 0 orientation
304 * does not rotate, 1 rotates clockwise by 90 degree, 2, rotates clockwise by
305 * 180 degrees, 3 rotates clockwise by 270 degrees.
306 * \param L The Lua VM state.
307 * \return The number of elements pushed on stack.
310 * \lparam The rotation to perform.
313 luaA_image_orientate(lua_State
*L
)
315 image_t
*image
= luaL_checkudata(L
, 1, "image");
316 int orientation
= luaL_checknumber(L
, 2);
318 imlib_context_set_image(image
->image
);
319 imlib_image_orientate(orientation
);
321 image
->isupdated
= false;
326 /** Rotate an image with specified angle radians and return a new image.
327 * \param L The Lua VM state.
328 * \return The number of elements pushed on stack.
331 * \lparam The angle in radians.
332 * \lreturn A rotated image.
335 luaA_image_rotate(lua_State
*L
)
337 image_t
*image
= luaL_checkudata(L
, 1, "image"), *new;
338 double angle
= luaL_checknumber(L
, 2);
342 imlib_context_set_image(image
->image
);
343 new->image
= imlib_create_rotated_image(angle
);
348 /** Crop an image to the given rectangle.
349 * \return The number of elements pushed on stack.
352 * \lparam The top left x coordinate of the rectangle.
353 * \lparam The top left y coordinate of the rectangle.
354 * \lparam The width of the rectangle.
355 * \lparam The height of the rectangle.
356 * \lreturn A cropped image.
359 luaA_image_crop(lua_State
*L
)
361 image_t
*image
= luaL_checkudata(L
, 1, "image"), *new;
362 int x
= luaL_checkint(L
, 2);
363 int y
= luaL_checkint(L
, 3);
364 int w
= luaL_checkint(L
, 4);
365 int h
= luaL_checkint(L
, 5);
369 imlib_context_set_image(image
->image
);
370 new->image
= imlib_create_cropped_image(x
, y
, w
, h
);
375 /** Crop the image to the given rectangle and scales it.
376 * \param L The Lua VM state.
377 * \return The number of elements pushed on stack.
380 * \lparam The top left x coordinate of the source rectangle.
381 * \lparam The top left y coordinate of the source rectangle.
382 * \lparam The width of the source rectangle.
383 * \lparam The height of the source rectangle.
384 * \lparam The width of the destination rectangle.
385 * \lparam The height of the destination rectangle.
386 * \lreturn A cropped image.
389 luaA_image_crop_and_scale(lua_State
*L
)
391 image_t
*image
= luaL_checkudata(L
, 1, "image"), *new;
392 int source_x
= luaL_checkint(L
, 2);
393 int source_y
= luaL_checkint(L
, 3);
394 int w
= luaL_checkint(L
, 4);
395 int h
= luaL_checkint(L
, 5);
396 int dest_w
= luaL_checkint(L
, 6);
397 int dest_h
= luaL_checkint(L
, 7);
401 imlib_context_set_image(image
->image
);
402 new->image
= imlib_create_cropped_scaled_image(source_x
,
410 /** Draw a pixel in an image
411 * \param L The Lua VM state
414 * \lparam The x coordinate of the pixel to draw
415 * \lparam The y coordinate of the pixel to draw
416 * \lparam The color to draw the pixel in
419 luaA_image_draw_pixel(lua_State
*L
)
423 color_init_cookie_t cookie
;
424 image_t
*image
= luaL_checkudata(L
, 1, "image");
425 int x
= luaL_checkint(L
, 2);
426 int y
= luaL_checkint(L
, 3);
427 const char *buf
= luaL_checklstring(L
, 4, &len
);
429 cookie
= color_init_unchecked(&color
, buf
, len
);
430 imlib_context_set_image(image
->image
);
431 color_init_reply(cookie
);
433 if((x
> imlib_image_get_width()) || (y
> imlib_image_get_height()))
436 imlib_context_set_color(color
.red
, color
.green
, color
.blue
, color
.alpha
);
437 imlib_image_draw_pixel(x
, y
, 1);
438 image
->isupdated
= false;
442 /** Draw a line in an image
443 * \param L The Lua VM state
446 * \lparam The x1 coordinate of the line to draw
447 * \lparam The y1 coordinate of the line to draw
448 * \lparam The x2 coordinate of the line to draw
449 * \lparam The y2 coordinate of the line to draw
450 * \lparam The color to draw the line in
453 luaA_image_draw_line(lua_State
*L
)
457 color_init_cookie_t cookie
;
458 image_t
*image
= luaL_checkudata(L
, 1, "image");
459 int x1
= luaL_checkint(L
, 2);
460 int y1
= luaL_checkint(L
, 3);
461 int x2
= luaL_checkint(L
, 4);
462 int y2
= luaL_checkint(L
, 5);
463 const char *buf
= luaL_checklstring(L
, 6, &len
);
465 cookie
= color_init_unchecked(&color
, buf
, len
);
466 imlib_context_set_image(image
->image
);
467 color_init_reply(cookie
);
469 if((MAX(x1
,x2
) > imlib_image_get_width()) || (MAX(y1
,y2
) > imlib_image_get_height()))
472 imlib_context_set_color(color
.red
, color
.green
, color
.blue
, color
.alpha
);
473 imlib_image_draw_line(x1
, y1
, x2
, y2
, 0);
474 image
->isupdated
= false;
478 /** Draw a rectangle in an image
479 * \param L The Lua VM state
482 * \lparam The x coordinate of the rectangles top left corner
483 * \lparam The y coordinate of the rectangles top left corner
484 * \lparam The width of the rectangle
485 * \lparam The height of the rectangle
486 * \lparam True if the rectangle should be filled, False otherwise
487 * \lparam The color to draw the rectangle in
490 luaA_image_draw_rectangle(lua_State
*L
)
494 color_init_cookie_t cookie
;
495 image_t
*image
= luaL_checkudata(L
, 1, "image");
496 int x
= luaL_checkint(L
, 2);
497 int y
= luaL_checkint(L
, 3);
498 int width
= luaL_checkint(L
, 4);
499 int height
= luaL_checkint(L
, 5);
500 int fill
= luaA_checkboolean(L
, 6);
501 const char *buf
= luaL_checklstring(L
, 7, &len
);
503 cookie
= color_init_unchecked(&color
, buf
, len
);
504 imlib_context_set_image(image
->image
);
505 color_init_reply(cookie
);
507 if((x
> imlib_image_get_width()) || (x
+ width
> imlib_image_get_width()))
509 if((y
> imlib_image_get_height()) || (y
+ height
> imlib_image_get_height()))
512 imlib_context_set_color(color
.red
, color
.green
, color
.blue
, color
.alpha
);
514 imlib_image_draw_rectangle(x
, y
, width
, height
);
516 imlib_image_fill_rectangle(x
, y
, width
, height
);
517 image
->isupdated
= false;
521 /** Convert a table to a color range and set it as current color range.
522 * \param L The Lua VM state.
523 * \param ud The index of the table.
524 * \return A color range that you are responsible to free.
526 static Imlib_Color_Range
527 luaA_table_to_color_range(lua_State
*L
, int ud
)
529 Imlib_Color_Range range
= imlib_create_color_range();
531 imlib_context_set_color_range(range
);
533 for(size_t i
= 1; i
<= lua_objlen(L
, ud
); i
++)
536 lua_pushnumber(L
, i
);
540 const char *colstr
= lua_tolstring(L
, -1, &len
);
546 color_init_cookie_t cookie
= color_init_unchecked(&color
, colstr
, len
);
548 /* get value with colstr as key in table */
549 lua_pushvalue(L
, -1);
552 /* convert the distance, if any, to number, or set 1 */
553 int distance
= lua_tonumber(L
, -1);
554 /* remove distance */
557 color_init_reply(cookie
);
559 imlib_context_set_color(color
.red
, color
.green
, color
.blue
, color
.alpha
);
561 imlib_add_color_to_color_range(distance
);
564 /* remove color string (value) */
571 /** Draw a rectangle in an image with gradient color.
572 * \param L The Lua VM state.
573 * \return The number of elements pushed on stack.
576 * \lparam The x coordinate of the rectangles top left corner.
577 * \lparam The y coordinate of the rectangles top left corner.
578 * \lparam The width of the rectangle.
579 * \lparam The height of the rectangle.
580 * \lparam A table with the color to draw the rectangle. You can specified the
581 * color distance from the previous one by setting t[color] = distance.
582 * \lparam The angle of gradient.
585 luaA_image_draw_rectangle_gradient(lua_State
*L
)
587 image_t
*image
= luaL_checkudata(L
, 1, "image");
588 int x
= luaL_checkint(L
, 2);
589 int y
= luaL_checkint(L
, 3);
590 int width
= luaL_checkint(L
, 4);
591 int height
= luaL_checkint(L
, 5);
592 luaA_checktable(L
, 6);
593 double angle
= luaL_checknumber(L
, 7);
595 if((x
> imlib_image_get_width()) || (x
+ width
> imlib_image_get_width()))
597 if((y
> imlib_image_get_height()) || (y
+ height
> imlib_image_get_height()))
600 imlib_context_set_image(image
->image
);
602 luaA_table_to_color_range(L
, 6);
604 imlib_image_fill_color_range_rectangle(x
, y
, width
, height
, angle
);
606 imlib_free_color_range();
608 image
->isupdated
= false;
613 /** Draw a circle in an image
614 * \param L The Lua VM state
617 * \lparam The x coordinate of the center of the circle
618 * \lparam The y coordinate of the center of the circle
619 * \lparam The horizontal amplitude (width)
620 * \lparam The vertical amplitude (height)
621 * \lparam True if the circle should be filled, False otherwise
622 * \lparam The color to draw the circle in
625 luaA_image_draw_circle(lua_State
*L
)
629 color_init_cookie_t cookie
;
630 image_t
*image
= luaL_checkudata(L
, 1, "image");
631 int x
= luaL_checkint(L
, 2);
632 int y
= luaL_checkint(L
, 3);
633 int ah
= luaL_checkint(L
, 4);
634 int av
= luaL_checkint(L
, 5);
635 int fill
= luaA_checkboolean(L
, 6);
636 const char *buf
= luaL_checklstring(L
, 7, &len
);
638 cookie
= color_init_unchecked(&color
, buf
, len
);
639 imlib_context_set_image(image
->image
);
640 color_init_reply(cookie
);
642 if((x
> imlib_image_get_width()) || (x
+ ah
> imlib_image_get_width()) || (x
- ah
< 0))
644 if((y
> imlib_image_get_height()) || (y
+ av
> imlib_image_get_height()) || (y
- av
< 0))
647 imlib_context_set_color(color
.red
, color
.green
, color
.blue
, color
.alpha
);
649 imlib_image_draw_ellipse(x
, y
, ah
, av
);
651 imlib_image_fill_ellipse(x
, y
, ah
, av
);
652 image
->isupdated
= false;
656 /** Saves the image to the given path. The file extension (e.g. .png or .jpg)
657 * will affect the output format.
658 * \param L The Lua VM state.
659 * \return The number of elements pushed on stack.
662 * \lparam The image path.
665 luaA_image_save(lua_State
*L
)
667 image_t
*image
= luaL_checkudata(L
, 1, "image");
668 const char *path
= luaL_checkstring(L
, 2);
669 Imlib_Load_Error err
;
671 imlib_context_set_image(image
->image
);
672 imlib_save_image_with_error_return(path
, &err
);
674 if(err
!= IMLIB_LOAD_ERROR_NONE
)
675 warn("cannot save image %s: %s", path
, image_imlib_load_strerror(err
));
680 /** Insert one image into another.
681 * \param L The Lua VM state.
682 * \return The number of elements pushed on stack.
685 * \lparam The image to insert.
686 * \lparam The X offset of the image to insert (optional).
687 * \lparam The Y offset of the image to insert (optional).
688 * \lparam The horizontal offset of the upper right image corner (optional).
689 * \lparam The vertical offset of the upper right image corner (optional).
690 * \lparam The horizontal offset of the lower left image corner (optional).
691 * \lparam The vertical offset of the lower left image corner (optional).
692 * \lparam The X coordinate of the source rectangle (optional).
693 * \lparam The Y coordinate of the source rectangle (optional).
694 * \lparam The width of the source rectangle (optional).
695 * \lparam The height of the source rectangle (optional).
698 luaA_image_insert(lua_State
*L
)
700 image_t
*image_target
= luaL_checkudata(L
, 1, "image");
701 image_t
*image_source
= luaL_checkudata(L
, 2, "image");
702 int xoff
= luaL_optnumber(L
, 3, 0);
703 int yoff
= luaL_optnumber(L
, 4, 0);
705 int xsrc
= luaL_optnumber(L
, 5, 0);
706 int ysrc
= luaL_optnumber(L
, 6, 0);
707 int wsrc
= luaL_optnumber(L
, 7, image_getwidth(image_source
));
708 int hsrc
= luaL_optnumber(L
, 8, image_getheight(image_source
));
710 int hxoff
= luaL_optnumber(L
, 9, image_getwidth(image_source
));
711 int hyoff
= luaL_optnumber(L
, 10, 0);
713 int vxoff
= luaL_optnumber(L
, 11, 0);
714 int vyoff
= luaL_optnumber(L
, 12, image_getheight(image_source
));
716 imlib_context_set_image(image_target
->image
);
718 imlib_blend_image_onto_image_skewed(image_source
->image
, 0,
719 /* source rectangle */
720 xsrc
, ysrc
, wsrc
, hsrc
,
721 /* position of the source image in the target image */
723 /* axis offsets for the source image, (w|0|0|h)
725 hxoff
, hyoff
, vxoff
, vyoff
);
727 image_target
->isupdated
= false;
733 * \param L The Lua VM state.
734 * \return The number of elements pushed on stack.
737 * \lfield width The image width.
738 * \lfield height The image height.
741 luaA_image_index(lua_State
*L
)
743 if(luaA_usemetatable(L
, 1, 2))
746 image_t
*image
= luaL_checkudata(L
, 1, "image");
748 const char *attr
= luaL_checklstring(L
, 2, &len
);
750 switch(a_tokenize(attr
, len
))
753 lua_pushnumber(L
, image_getwidth(image
));
756 lua_pushnumber(L
, image_getheight(image
));
759 imlib_context_set_image(image
->image
);
760 lua_pushboolean(L
, imlib_image_has_alpha());
769 const struct luaL_reg awesome_image_methods
[] =
771 LUA_CLASS_METHODS(image
)
772 { "__call", luaA_image_new
},
773 { "argb32", luaA_image_argb32_new
},
776 const struct luaL_reg awesome_image_meta
[] =
778 LUA_OBJECT_META(image
)
779 { "__index", luaA_image_index
},
780 { "rotate", luaA_image_rotate
},
781 { "orientate", luaA_image_orientate
},
782 { "crop", luaA_image_crop
},
783 { "crop_and_scale", luaA_image_crop_and_scale
},
784 { "save", luaA_image_save
},
785 { "insert", luaA_image_insert
},
786 /* draw on images, whee! */
787 { "draw_pixel", luaA_image_draw_pixel
},
788 { "draw_line", luaA_image_draw_line
},
789 { "draw_rectangle", luaA_image_draw_rectangle
},
790 { "draw_rectangle_gradient", luaA_image_draw_rectangle_gradient
},
791 { "draw_circle", luaA_image_draw_circle
},
792 { "__gc", luaA_image_gc
},
796 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80