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"
25 DO_LUA_TOSTRING(image_t
, image
, "image");
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
);
39 image_imlib_load_strerror(Imlib_Load_Error 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.
83 image_compute(image_t
*image
)
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 */
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 */
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
)
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
);
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.
151 image_new_from_file(const char *filename
)
154 Imlib_Load_Error e
= IMLIB_LOAD_ERROR_NONE
;
160 if(!(imimage
= imlib_load_image_with_error_return(filename
, &e
)))
162 warn("cannot load image %s: %s", filename
, image_imlib_load_strerror(e
));
166 image
= image_new(globalconf
.L
);
167 image
->image
= imimage
;
169 image_compute(image
);
174 /** Create a new image object.
175 * \param L The Lua stack.
176 * \return The number of elements pushed on stack.
178 * \lparam The image path.
179 * \lreturn An image object.
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
);
191 /** Create a new image object from ARGB32 data.
192 * \param L The Lua stack.
193 * \return The number of elements pushed on stack.
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
199 * \lreturn An image object.
202 luaA_image_argb32_new(lua_State
*L
)
205 unsigned int width
= luaL_checknumber(L
, 1);
206 unsigned int height
= luaL_checknumber(L
, 2);
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.
229 * \lparam The rotation to perform.
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
);
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.
250 * \lparam The angle in radians.
251 * \lreturn A rotated image.
254 luaA_image_rotate(lua_State
*L
)
256 image_t
*image
= luaL_checkudata(L
, 1, "image"), *new;
257 double angle
= luaL_checknumber(L
, 2);
261 imlib_context_set_image(image
->image
);
262 new->image
= imlib_create_rotated_image(angle
);
269 /** Crop an image to the given rectangle.
270 * \return The number of elements pushed on stack.
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.
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);
290 imlib_context_set_image(image
->image
);
291 new->image
= imlib_create_cropped_image(x
, y
, w
, h
);
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.
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.
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);
324 imlib_context_set_image(image
->image
);
325 new->image
= imlib_create_cropped_scaled_image(source_x
,
335 /** Draw a pixel in an image
336 * \param L The Lua VM state
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
344 luaA_image_draw_pixel(lua_State
*L
)
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()))
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
);
367 /** Draw a line in an image
368 * \param L The Lua VM state
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
378 luaA_image_draw_line(lua_State
*L
)
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()))
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
);
403 /** Draw a rectangle in an image
404 * \param L The Lua VM state
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
415 luaA_image_draw_rectangle(lua_State
*L
)
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()))
434 if((y
> imlib_image_get_height()) || (y
+ height
> imlib_image_get_height()))
437 imlib_context_set_color(color
.red
, color
.green
, color
.blue
, color
.alpha
);
439 imlib_image_draw_rectangle(x
, y
, width
, height
);
441 imlib_image_fill_rectangle(x
, y
, width
, height
);
442 image_compute(image
);
446 /** Draw a circle in an image
447 * \param L The Lua VM state
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
458 luaA_image_draw_circle(lua_State
*L
)
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))
477 if((y
> imlib_image_get_height()) || (y
+ av
> imlib_image_get_height()) || (y
- av
< 0))
480 imlib_context_set_color(color
.red
, color
.green
, color
.blue
, color
.alpha
);
482 imlib_image_draw_ellipse(x
, y
, ah
, av
);
484 imlib_image_fill_ellipse(x
, y
, ah
, av
);
485 image_compute(image
);
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.
495 * \lparam The image path.
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
));
514 * \param L The Lua VM state.
515 * \return The number of elements pushed on stack.
518 * \lfield width The image width.
519 * \lfield height The image height.
522 luaA_image_index(lua_State
*L
)
524 if(luaA_usemetatable(L
, 1, 2))
527 image_t
*image
= luaL_checkudata(L
, 1, "image");
529 const char *attr
= luaL_checklstring(L
, 2, &len
);
531 switch(a_tokenize(attr
, len
))
534 imlib_context_set_image(image
->image
);
535 lua_pushnumber(L
, imlib_image_get_width());
538 imlib_context_set_image(image
->image
);
539 lua_pushnumber(L
, imlib_image_get_height());
542 imlib_context_set_image(image
->image
);
543 lua_pushboolean(L
, imlib_image_has_alpha());
552 const struct luaL_reg awesome_image_methods
[] =
554 { "__call", luaA_image_new
},
555 { "argb32", luaA_image_argb32_new
},
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
},
572 { "__gc", luaA_image_gc
},
573 { "__tostring", luaA_image_tostring
},
577 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80