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 <xcb/xcb_image.h>
28 #include "common/luaobject.h"
37 /** Flag telling if the image is up to date or needs computing before
42 LUA_OBJECT_FUNCS(image_class
, image_t
, image
)
45 luaA_image_gc(lua_State
*L
)
47 image_t
*p
= luaA_checkudata(L
, 1, &image_class
);
48 imlib_context_set_image(p
->image
);
51 return luaA_object_gc(L
);
55 image_imlib_load_strerror(Imlib_Load_Error e
)
59 case IMLIB_LOAD_ERROR_FILE_DOES_NOT_EXIST
:
60 return "no such file or directory";
61 case IMLIB_LOAD_ERROR_FILE_IS_DIRECTORY
:
62 return "file is a directory";
63 case IMLIB_LOAD_ERROR_PERMISSION_DENIED_TO_READ
:
64 return "read permission denied";
65 case IMLIB_LOAD_ERROR_NO_LOADER_FOR_FILE_FORMAT
:
66 return "no loader for file format";
67 case IMLIB_LOAD_ERROR_PATH_TOO_LONG
:
68 return "path too long";
69 case IMLIB_LOAD_ERROR_PATH_COMPONENT_NON_EXISTANT
:
70 return "path component non existant";
71 case IMLIB_LOAD_ERROR_PATH_COMPONENT_NOT_DIRECTORY
:
72 return "path compoment not a directory";
73 case IMLIB_LOAD_ERROR_PATH_POINTS_OUTSIDE_ADDRESS_SPACE
:
74 return "path points oustide address space";
75 case IMLIB_LOAD_ERROR_TOO_MANY_SYMBOLIC_LINKS
:
76 return "too many symbolic links";
77 case IMLIB_LOAD_ERROR_OUT_OF_MEMORY
:
78 return "out of memory";
79 case IMLIB_LOAD_ERROR_OUT_OF_FILE_DESCRIPTORS
:
80 return "out of file descriptors";
81 case IMLIB_LOAD_ERROR_PERMISSION_DENIED_TO_WRITE
:
82 return "write permission denied";
83 case IMLIB_LOAD_ERROR_OUT_OF_DISK_SPACE
:
84 return "out of disk space";
85 case IMLIB_LOAD_ERROR_UNKNOWN
:
86 return "unknown error, that's really bad";
87 case IMLIB_LOAD_ERROR_NONE
:
88 return "no error, oops";
91 return "unknown error";
95 * \param image The image.
96 * \return The image width in pixel.
99 image_getwidth(image_t
*image
)
101 imlib_context_set_image(image
->image
);
102 return imlib_image_get_width();
105 /** Get image height.
106 * \param image The image.
107 * \return The image height in pixel.
110 image_getheight(image_t
*image
)
112 imlib_context_set_image(image
->image
);
113 return imlib_image_get_height();
116 /** Get the ARGB32 data from an image.
117 * \param image The image.
121 image_getdata(image_t
*image
)
127 #if AWESOME_IS_BIG_ENDIAN
128 const int index_a
= 0, index_r
= 1, index_g
= 2, index_b
= 3;
130 const int index_a
= 3, index_r
= 2, index_g
= 1, index_b
= 0;
136 imlib_context_set_image(image
->image
);
138 data
= imlib_image_get_data_for_reading_only();
140 size
= imlib_image_get_width() * imlib_image_get_height();
142 p_realloc(&image
->data
, size
* 4);
143 dataimg
= image
->data
;
145 for(i
= 0; i
< size
; i
++, dataimg
+= 4)
147 dataimg
[index_a
] = (data
[i
] >> 24) & 0xff; /* A */
148 /* cairo wants pre-multiplied alpha */
149 alpha
= dataimg
[index_a
] / 255.0;
150 dataimg
[index_r
] = ((data
[i
] >> 16) & 0xff) * alpha
; /* R */
151 dataimg
[index_g
] = ((data
[i
] >> 8) & 0xff) * alpha
; /* G */
152 dataimg
[index_b
] = (data
[i
] & 0xff) * alpha
; /* B */
155 image
->isupdated
= true;
162 image_draw_to_1bit_ximage(image_t
*image
, xcb_image_t
*img
)
164 imlib_context_set_image(image
->image
);
166 uint32_t *data
= imlib_image_get_data_for_reading_only();
168 int width
= imlib_image_get_width();
169 int height
= imlib_image_get_height();
171 for(int y
= 0; y
< height
; y
++)
172 for(int x
= 0; x
< width
; x
++)
178 // Sum up all color components ignoring alpha
179 tmp
= (data
[i
] >> 16) & 0xff;
180 tmp
+= (data
[i
] >> 8) & 0xff;
181 tmp
+= data
[i
] & 0xff;
183 pixel
= (tmp
/ 3 < 127) ? 0 : 1;
185 xcb_image_put_pixel(img
, x
, y
, pixel
);
189 // Convert an image to a 1bit pixmap
191 image_to_1bit_pixmap(image_t
*image
, xcb_drawable_t d
)
196 uint16_t width
, height
;
198 width
= image_getwidth(image
);
199 height
= image_getheight(image
);
201 /* Prepare the pixmap and gc */
202 pixmap
= xcb_generate_id(globalconf
.connection
);
203 xcb_create_pixmap(globalconf
.connection
, 1, pixmap
, d
, width
, height
);
205 gc
= xcb_generate_id(globalconf
.connection
);
206 xcb_create_gc(globalconf
.connection
, gc
, pixmap
, 0, NULL
);
208 /* Prepare the image */
209 img
= xcb_image_create_native(globalconf
.connection
, width
, height
,
210 XCB_IMAGE_FORMAT_XY_BITMAP
, 1, NULL
, 0, NULL
);
211 image_draw_to_1bit_ximage(image
, img
);
213 /* Paint the image to the pixmap */
214 xcb_image_put(globalconf
.connection
, pixmap
, gc
, img
, 0, 0, 0);
216 xcb_free_gc(globalconf
.connection
, gc
);
218 xcb_image_destroy(img
);
223 /** Create a new image from ARGB32 data.
224 * \param width The image width.
225 * \param height The image height.
226 * \param data The image data.
227 * \return 1 if an image has been pushed on stack, 0 otherwise.
230 image_new_from_argb32(int width
, int height
, uint32_t *data
)
234 if((imimage
= imlib_create_image_using_copied_data(width
, height
, data
)))
236 imlib_context_set_image(imimage
);
237 imlib_image_set_has_alpha(true);
238 image_t
*image
= image_new(globalconf
.L
);
239 image
->image
= imimage
;
246 /** Load an image from filename.
247 * \param filename The image file to load.
248 * \return 1 if image is loaded and on stack, 0 otherwise.
251 image_new_from_file(const char *filename
)
254 Imlib_Load_Error e
= IMLIB_LOAD_ERROR_NONE
;
260 if(!(imimage
= imlib_load_image_with_error_return(filename
, &e
)))
262 warn("cannot load image %s: %s", filename
, image_imlib_load_strerror(e
));
266 image
= image_new(globalconf
.L
);
267 image
->image
= imimage
;
272 /** Create a new image object.
273 * \param L The Lua stack.
274 * \return The number of elements pushed on stack.
276 * \lparam The image path.
277 * \lreturn An image object.
280 luaA_image_new(lua_State
*L
)
282 const char *filename
;
284 if((filename
= lua_tostring(L
, 2)))
285 return image_new_from_file(filename
);
289 /** Create a new image object from ARGB32 data.
290 * \param L The Lua stack.
291 * \return The number of elements pushed on stack.
293 * \lparam The image width.
294 * \lparam The image height.
295 * \lparam The image data as a string in ARGB32 format, or nil to create an
297 * \lreturn An image object.
300 luaA_image_argb32_new(lua_State
*L
)
303 unsigned int width
= luaL_checknumber(L
, 1);
304 unsigned int height
= luaL_checknumber(L
, 2);
308 uint32_t *data
= p_new(uint32_t, width
* height
);
309 return image_new_from_argb32(width
, height
, data
);
312 const char *data
= luaL_checklstring(L
, 3, &len
);
314 if(width
* height
* 4 != len
)
315 luaL_error(L
, "string size does not match image size");
317 return image_new_from_argb32(width
, height
, (uint32_t *) data
);
320 /** Performs 90 degree rotations on the current image. Passing 0 orientation
321 * does not rotate, 1 rotates clockwise by 90 degree, 2, rotates clockwise by
322 * 180 degrees, 3 rotates clockwise by 270 degrees.
323 * \param L The Lua VM state.
324 * \return The number of elements pushed on stack.
327 * \lparam The rotation to perform.
330 luaA_image_orientate(lua_State
*L
)
332 image_t
*image
= luaA_checkudata(L
, 1, &image_class
);
333 int orientation
= luaL_checknumber(L
, 2);
335 imlib_context_set_image(image
->image
);
336 imlib_image_orientate(orientation
);
338 image
->isupdated
= false;
343 /** Rotate an image with specified angle radians and return a new image.
344 * \param L The Lua VM state.
345 * \return The number of elements pushed on stack.
348 * \lparam The angle in radians.
349 * \lreturn A rotated image.
352 luaA_image_rotate(lua_State
*L
)
354 image_t
*image
= luaA_checkudata(L
, 1, &image_class
), *new;
355 double angle
= luaL_checknumber(L
, 2);
359 imlib_context_set_image(image
->image
);
360 new->image
= imlib_create_rotated_image(angle
);
365 /** Crop an image to the given rectangle.
366 * \return The number of elements pushed on stack.
369 * \lparam The top left x coordinate of the rectangle.
370 * \lparam The top left y coordinate of the rectangle.
371 * \lparam The width of the rectangle.
372 * \lparam The height of the rectangle.
373 * \lreturn A cropped image.
376 luaA_image_crop(lua_State
*L
)
378 image_t
*image
= luaA_checkudata(L
, 1, &image_class
), *new;
379 int x
= luaL_checkint(L
, 2);
380 int y
= luaL_checkint(L
, 3);
381 int w
= luaL_checkint(L
, 4);
382 int h
= luaL_checkint(L
, 5);
386 imlib_context_set_image(image
->image
);
387 new->image
= imlib_create_cropped_image(x
, y
, w
, h
);
392 /** Crop the image to the given rectangle and scales it.
393 * \param L The Lua VM state.
394 * \return The number of elements pushed on stack.
397 * \lparam The top left x coordinate of the source rectangle.
398 * \lparam The top left y coordinate of the source rectangle.
399 * \lparam The width of the source rectangle.
400 * \lparam The height of the source rectangle.
401 * \lparam The width of the destination rectangle.
402 * \lparam The height of the destination rectangle.
403 * \lreturn A cropped image.
406 luaA_image_crop_and_scale(lua_State
*L
)
408 image_t
*image
= luaA_checkudata(L
, 1, &image_class
), *new;
409 int source_x
= luaL_checkint(L
, 2);
410 int source_y
= luaL_checkint(L
, 3);
411 int w
= luaL_checkint(L
, 4);
412 int h
= luaL_checkint(L
, 5);
413 int dest_w
= luaL_checkint(L
, 6);
414 int dest_h
= luaL_checkint(L
, 7);
418 imlib_context_set_image(image
->image
);
419 new->image
= imlib_create_cropped_scaled_image(source_x
,
427 /** Draw a pixel in an image
428 * \param L The Lua VM state
431 * \lparam The x coordinate of the pixel to draw
432 * \lparam The y coordinate of the pixel to draw
433 * \lparam The color to draw the pixel in
436 luaA_image_draw_pixel(lua_State
*L
)
440 color_init_cookie_t cookie
;
441 image_t
*image
= luaA_checkudata(L
, 1, &image_class
);
442 int x
= luaL_checkint(L
, 2);
443 int y
= luaL_checkint(L
, 3);
444 const char *buf
= luaL_checklstring(L
, 4, &len
);
446 cookie
= color_init_unchecked(&color
, buf
, len
);
447 imlib_context_set_image(image
->image
);
448 color_init_reply(cookie
);
450 if((x
> imlib_image_get_width()) || (y
> imlib_image_get_height()))
453 imlib_context_set_color(color
.red
, color
.green
, color
.blue
, color
.alpha
);
454 imlib_image_draw_pixel(x
, y
, 1);
455 image
->isupdated
= false;
459 /** Draw a line in an image
460 * \param L The Lua VM state
463 * \lparam The x1 coordinate of the line to draw
464 * \lparam The y1 coordinate of the line to draw
465 * \lparam The x2 coordinate of the line to draw
466 * \lparam The y2 coordinate of the line to draw
467 * \lparam The color to draw the line in
470 luaA_image_draw_line(lua_State
*L
)
474 color_init_cookie_t cookie
;
475 image_t
*image
= luaA_checkudata(L
, 1, &image_class
);
476 int x1
= luaL_checkint(L
, 2);
477 int y1
= luaL_checkint(L
, 3);
478 int x2
= luaL_checkint(L
, 4);
479 int y2
= luaL_checkint(L
, 5);
480 const char *buf
= luaL_checklstring(L
, 6, &len
);
482 cookie
= color_init_unchecked(&color
, buf
, len
);
483 imlib_context_set_image(image
->image
);
484 color_init_reply(cookie
);
486 if((MAX(x1
,x2
) > imlib_image_get_width()) || (MAX(y1
,y2
) > imlib_image_get_height()))
489 imlib_context_set_color(color
.red
, color
.green
, color
.blue
, color
.alpha
);
490 imlib_image_draw_line(x1
, y1
, x2
, y2
, 0);
491 image
->isupdated
= false;
495 /** Draw a rectangle in an image
496 * \param L The Lua VM state
499 * \lparam The x coordinate of the rectangles top left corner
500 * \lparam The y coordinate of the rectangles top left corner
501 * \lparam The width of the rectangle
502 * \lparam The height of the rectangle
503 * \lparam True if the rectangle should be filled, False otherwise
504 * \lparam The color to draw the rectangle in
507 luaA_image_draw_rectangle(lua_State
*L
)
511 color_init_cookie_t cookie
;
512 image_t
*image
= luaA_checkudata(L
, 1, &image_class
);
513 int x
= luaL_checkint(L
, 2);
514 int y
= luaL_checkint(L
, 3);
515 int width
= luaL_checkint(L
, 4);
516 int height
= luaL_checkint(L
, 5);
517 int fill
= luaA_checkboolean(L
, 6);
518 const char *buf
= luaL_checklstring(L
, 7, &len
);
520 cookie
= color_init_unchecked(&color
, buf
, len
);
521 imlib_context_set_image(image
->image
);
522 color_init_reply(cookie
);
524 if((x
> imlib_image_get_width()) || (x
+ width
> imlib_image_get_width()))
526 if((y
> imlib_image_get_height()) || (y
+ height
> imlib_image_get_height()))
529 imlib_context_set_color(color
.red
, color
.green
, color
.blue
, color
.alpha
);
531 imlib_image_draw_rectangle(x
, y
, width
, height
);
533 imlib_image_fill_rectangle(x
, y
, width
, height
);
534 image
->isupdated
= false;
538 /** Convert a table to a color range and set it as current color range.
539 * \param L The Lua VM state.
540 * \param ud The index of the table.
541 * \return A color range that you are responsible to free.
543 static Imlib_Color_Range
544 luaA_table_to_color_range(lua_State
*L
, int ud
)
546 Imlib_Color_Range range
= imlib_create_color_range();
548 imlib_context_set_color_range(range
);
550 for(size_t i
= 1; i
<= lua_objlen(L
, ud
); i
++)
553 lua_pushnumber(L
, i
);
557 const char *colstr
= lua_tolstring(L
, -1, &len
);
563 color_init_cookie_t cookie
= color_init_unchecked(&color
, colstr
, len
);
565 /* get value with colstr as key in table */
566 lua_pushvalue(L
, -1);
569 /* convert the distance, if any, to number, or set 1 */
570 int distance
= lua_tonumber(L
, -1);
571 /* remove distance */
574 color_init_reply(cookie
);
576 imlib_context_set_color(color
.red
, color
.green
, color
.blue
, color
.alpha
);
578 imlib_add_color_to_color_range(distance
);
581 /* remove color string (value) */
588 /** Draw a rectangle in an image with gradient color.
589 * \param L The Lua VM state.
590 * \return The number of elements pushed on stack.
593 * \lparam The x coordinate of the rectangles top left corner.
594 * \lparam The y coordinate of the rectangles top left corner.
595 * \lparam The width of the rectangle.
596 * \lparam The height of the rectangle.
597 * \lparam A table with the color to draw the rectangle. You can specified the
598 * color distance from the previous one by setting t[color] = distance.
599 * \lparam The angle of gradient.
602 luaA_image_draw_rectangle_gradient(lua_State
*L
)
604 image_t
*image
= luaA_checkudata(L
, 1, &image_class
);
605 int x
= luaL_checkint(L
, 2);
606 int y
= luaL_checkint(L
, 3);
607 int width
= luaL_checkint(L
, 4);
608 int height
= luaL_checkint(L
, 5);
609 luaA_checktable(L
, 6);
610 double angle
= luaL_checknumber(L
, 7);
612 if((x
> imlib_image_get_width()) || (x
+ width
> imlib_image_get_width()))
614 if((y
> imlib_image_get_height()) || (y
+ height
> imlib_image_get_height()))
617 imlib_context_set_image(image
->image
);
619 luaA_table_to_color_range(L
, 6);
621 imlib_image_fill_color_range_rectangle(x
, y
, width
, height
, angle
);
623 imlib_free_color_range();
625 image
->isupdated
= false;
630 /** Draw a circle in an image
631 * \param L The Lua VM state
634 * \lparam The x coordinate of the center of the circle
635 * \lparam The y coordinate of the center of the circle
636 * \lparam The horizontal amplitude (width)
637 * \lparam The vertical amplitude (height)
638 * \lparam True if the circle should be filled, False otherwise
639 * \lparam The color to draw the circle in
642 luaA_image_draw_circle(lua_State
*L
)
646 color_init_cookie_t cookie
;
647 image_t
*image
= luaA_checkudata(L
, 1, &image_class
);
648 int x
= luaL_checkint(L
, 2);
649 int y
= luaL_checkint(L
, 3);
650 int ah
= luaL_checkint(L
, 4);
651 int av
= luaL_checkint(L
, 5);
652 int fill
= luaA_checkboolean(L
, 6);
653 const char *buf
= luaL_checklstring(L
, 7, &len
);
655 cookie
= color_init_unchecked(&color
, buf
, len
);
656 imlib_context_set_image(image
->image
);
657 color_init_reply(cookie
);
659 if((x
> imlib_image_get_width()) || (x
+ ah
> imlib_image_get_width()) || (x
- ah
< 0))
661 if((y
> imlib_image_get_height()) || (y
+ av
> imlib_image_get_height()) || (y
- av
< 0))
664 imlib_context_set_color(color
.red
, color
.green
, color
.blue
, color
.alpha
);
666 imlib_image_draw_ellipse(x
, y
, ah
, av
);
668 imlib_image_fill_ellipse(x
, y
, ah
, av
);
669 image
->isupdated
= false;
673 /** Saves the image to the given path. The file extension (e.g. .png or .jpg)
674 * will affect the output format.
675 * \param L The Lua VM state.
676 * \return The number of elements pushed on stack.
679 * \lparam The image path.
682 luaA_image_save(lua_State
*L
)
684 image_t
*image
= luaA_checkudata(L
, 1, &image_class
);
685 const char *path
= luaL_checkstring(L
, 2);
686 Imlib_Load_Error err
;
688 imlib_context_set_image(image
->image
);
689 imlib_save_image_with_error_return(path
, &err
);
691 if(err
!= IMLIB_LOAD_ERROR_NONE
)
692 warn("cannot save image %s: %s", path
, image_imlib_load_strerror(err
));
697 /** Insert one image into another.
698 * \param L The Lua VM state.
699 * \return The number of elements pushed on stack.
702 * \lparam The image to insert.
703 * \lparam The X offset of the image to insert (optional).
704 * \lparam The Y offset of the image to insert (optional).
705 * \lparam The horizontal offset of the upper right image corner (optional).
706 * \lparam The vertical offset of the upper right image corner (optional).
707 * \lparam The horizontal offset of the lower left image corner (optional).
708 * \lparam The vertical offset of the lower left image corner (optional).
709 * \lparam The X coordinate of the source rectangle (optional).
710 * \lparam The Y coordinate of the source rectangle (optional).
711 * \lparam The width of the source rectangle (optional).
712 * \lparam The height of the source rectangle (optional).
715 luaA_image_insert(lua_State
*L
)
717 image_t
*image_target
= luaA_checkudata(L
, 1, &image_class
);
718 image_t
*image_source
= luaA_checkudata(L
, 2, &image_class
);
719 int xoff
= luaL_optnumber(L
, 3, 0);
720 int yoff
= luaL_optnumber(L
, 4, 0);
722 int xsrc
= luaL_optnumber(L
, 5, 0);
723 int ysrc
= luaL_optnumber(L
, 6, 0);
724 int wsrc
= luaL_optnumber(L
, 7, image_getwidth(image_source
));
725 int hsrc
= luaL_optnumber(L
, 8, image_getheight(image_source
));
727 int hxoff
= luaL_optnumber(L
, 9, image_getwidth(image_source
));
728 int hyoff
= luaL_optnumber(L
, 10, 0);
730 int vxoff
= luaL_optnumber(L
, 11, 0);
731 int vyoff
= luaL_optnumber(L
, 12, image_getheight(image_source
));
733 imlib_context_set_image(image_target
->image
);
735 imlib_blend_image_onto_image_skewed(image_source
->image
, 0,
736 /* source rectangle */
737 xsrc
, ysrc
, wsrc
, hsrc
,
738 /* position of the source image in the target image */
740 /* axis offsets for the source image, (w|0|0|h)
742 hxoff
, hyoff
, vxoff
, vyoff
);
744 image_target
->isupdated
= false;
750 luaA_image_get_width(lua_State
*L
, image_t
*image
)
752 lua_pushnumber(L
, image_getwidth(image
));
757 luaA_image_get_height(lua_State
*L
, image_t
*image
)
759 lua_pushnumber(L
, image_getheight(image
));
764 luaA_image_get_alpha(lua_State
*L
, image_t
*image
)
766 imlib_context_set_image(image
->image
);
767 lua_pushboolean(L
, imlib_image_has_alpha());
772 image_class_setup(lua_State
*L
)
774 static const struct luaL_reg image_methods
[] =
776 LUA_CLASS_METHODS(image
)
777 { "__call", luaA_image_new
},
778 { "argb32", luaA_image_argb32_new
},
782 static const struct luaL_reg image_meta
[] =
784 LUA_OBJECT_META(image
)
786 { "rotate", luaA_image_rotate
},
787 { "orientate", luaA_image_orientate
},
788 { "crop", luaA_image_crop
},
789 { "crop_and_scale", luaA_image_crop_and_scale
},
790 { "save", luaA_image_save
},
791 { "insert", luaA_image_insert
},
792 /* draw on images, whee! */
793 { "draw_pixel", luaA_image_draw_pixel
},
794 { "draw_line", luaA_image_draw_line
},
795 { "draw_rectangle", luaA_image_draw_rectangle
},
796 { "draw_rectangle_gradient", luaA_image_draw_rectangle_gradient
},
797 { "draw_circle", luaA_image_draw_circle
},
798 { "__gc", luaA_image_gc
},
802 luaA_class_setup(L
, &image_class
, "image", (lua_class_allocator_t
) image_new
,
803 luaA_class_index_miss_property
, luaA_class_newindex_miss_property
,
804 image_methods
, image_meta
);
805 luaA_class_add_property(&image_class
, A_TK_WIDTH
,
807 (lua_class_propfunc_t
) luaA_image_get_width
,
809 luaA_class_add_property(&image_class
, A_TK_HEIGHT
,
811 (lua_class_propfunc_t
) luaA_image_get_height
,
813 luaA_class_add_property(&image_class
, A_TK_ALPHA
,
815 (lua_class_propfunc_t
) luaA_image_get_alpha
,
819 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80