awful.tag: fix typo
[awesome.git] / image.c
blob04d3389d47ee03f105e90e9e8d802843e70348f1
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 imlib_context_set_image(p->image);
32 imlib_free_image();
33 p_delete(&p->data);
34 return 0;
37 static const char *
38 image_imlib_load_strerror(Imlib_Load_Error e)
40 switch(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";
77 /** Recompute the ARGB32 data from an image.
78 * \param image The image.
79 * \return Data.
81 static void
82 image_compute(image_t *image)
84 int size, i;
85 uint32_t *data;
86 double alpha;
87 uint8_t *dataimg;
89 imlib_context_set_image(image->image);
91 data = imlib_image_get_data_for_reading_only();
93 image->width = imlib_image_get_width();
94 image->height = imlib_image_get_height();
96 size = image->width * image->height;
98 p_realloc(&image->data, size * 4);
99 dataimg = image->data;
101 for(i = 0; i < size; i++, dataimg += 4)
103 #if AWESOME_IS_BIG_ENDIAN
104 dataimg[0] = (data[i] >> 24) & 0xff; /* A */
105 /* cairo wants pre-multiplied alpha */
106 alpha = dataimg[0] / 255.0;
107 dataimg[1] = ((data[i] >> 16) & 0xff) * alpha; /* R */
108 dataimg[2] = ((data[i] >> 8) & 0xff) * alpha; /* G */
109 dataimg[3] = (data[i] & 0xff) * alpha; /* B */
110 #else
111 dataimg[3] = (data[i] >> 24) & 0xff; /* A */
112 /* cairo wants pre-multiplied alpha */
113 alpha = dataimg[3] / 255.0;
114 dataimg[2] = ((data[i] >> 16) & 0xff) * alpha; /* R */
115 dataimg[1] = ((data[i] >> 8) & 0xff) * alpha; /* G */
116 dataimg[0] = (data[i] & 0xff) * alpha; /* B */
117 #endif
121 /** Create a new image from ARGB32 data.
122 * \param width The image width.
123 * \param height The image height.
124 * \param data The image data.
125 * \return 1 if an image has been pushed on stack, 0 otherwise.
128 image_new_from_argb32(int width, int height, uint32_t *data)
130 Imlib_Image imimage;
132 if((imimage = imlib_create_image_using_copied_data(width, height, data)))
134 imlib_context_set_image(imimage);
135 imlib_image_set_has_alpha(true);
136 image_t *image = image_new(globalconf.L);
137 image->image = imimage;
138 image_compute(image);
139 return 1;
142 return 0;
145 /** Load an image from filename.
146 * \param filename The image file to load.
147 * \return 1 if image is loaded and on stack, 0 otherwise.
149 static int
150 image_new_from_file(const char *filename)
152 Imlib_Image imimage;
153 Imlib_Load_Error e = IMLIB_LOAD_ERROR_NONE;
154 image_t *image;
156 if(!filename)
157 return 0;
159 if(!(imimage = imlib_load_image_with_error_return(filename, &e)))
161 warn("cannot load image %s: %s", filename, image_imlib_load_strerror(e));
162 return 0;
165 image = image_new(globalconf.L);
166 image->image = imimage;
168 image_compute(image);
170 return 1;
173 /** Create a new image object.
174 * \param L The Lua stack.
175 * \return The number of elements pushed on stack.
176 * \luastack
177 * \lparam The image path, or nil to create an empty image.
178 * \lparam The image width if nil was set as first arg.
179 * \lparam The image height if nil was set as first arg.
180 * \lreturn An image object.
182 static int
183 luaA_image_new(lua_State *L)
185 const char *filename;
187 if((filename = lua_tostring(L, 2)))
188 return image_new_from_file(filename);
189 else if(lua_isnil(L, 2))
191 int width = luaL_checknumber(L, 3);
192 int height = luaL_checknumber(L, 4);
194 if(width <= 0 || height <= 0)
195 luaL_error(L, "request image has invalid size");
197 uint32_t *data = p_new(uint32_t, width * height);
198 return image_new_from_argb32(width, height, data);
201 return 0;
204 /** Create a new image object from ARGB32 data.
205 * \param L The Lua stack.
206 * \return The number of elements pushed on stack.
207 * \luastack
208 * \lparam The image width.
209 * \lparam The image height.
210 * \lparam The image data as a string in ARGB32 format.
211 * \lreturn An image object.
213 static int
214 luaA_image_argb32_new(lua_State *L)
216 size_t len;
217 unsigned int width = luaL_checknumber(L, 1);
218 unsigned int height = luaL_checknumber(L, 2);
219 const char *data = luaL_checklstring(L, 3, &len);
221 if(width * height * 4 != len)
222 luaL_error(L, "string size does not match image size");
224 return image_new_from_argb32(width, height, (uint32_t *) data);
227 /** Performs 90 degree rotations on the current image. Passing 0 orientation
228 * does not rotate, 1 rotates clockwise by 90 degree, 2, rotates clockwise by
229 * 180 degrees, 3 rotates clockwise by 270 degrees.
230 * \param L The Lua VM state.
231 * \return The number of elements pushed on stack.
232 * \luastack
233 * \lvalue An image.
234 * \lparam The rotation to perform.
236 static int
237 luaA_image_orientate(lua_State *L)
239 image_t *image = luaL_checkudata(L, 1, "image");
240 int orientation = luaL_checknumber(L, 2);
242 imlib_context_set_image(image->image);
243 imlib_image_orientate(orientation);
245 image_compute(image);
247 return 0;
250 /** Rotate an image with specified angle radians and return a new image.
251 * \param L The Lua VM state.
252 * \return The number of elements pushed on stack.
253 * \luastack
254 * \lvalue An image.
255 * \lparam The angle in radians.
256 * \lreturn A rotated image.
258 static int
259 luaA_image_rotate(lua_State *L)
261 image_t *image = luaL_checkudata(L, 1, "image"), *new;
262 double angle = luaL_checknumber(L, 2);
264 new = image_new(L);
266 imlib_context_set_image(image->image);
267 new->image = imlib_create_rotated_image(angle);
269 image_compute(new);
271 return 1;
274 /** Crop an image to the given rectangle.
275 * \return The number of elements pushed on stack.
276 * \luastack
277 * \lvalue An image.
278 * \lparam The top left x coordinate of the rectangle.
279 * \lparam The top left y coordinate of the rectangle.
280 * \lparam The width of the rectangle.
281 * \lparam The height of the rectangle.
282 * \lreturn A cropped image.
284 static int
285 luaA_image_crop(lua_State *L)
287 image_t *image = luaL_checkudata(L, 1, "image"), *new;
288 int x = luaL_checkint(L, 2);
289 int y = luaL_checkint(L, 3);
290 int w = luaL_checkint(L, 4);
291 int h = luaL_checkint(L, 5);
293 new = image_new(L);
295 imlib_context_set_image(image->image);
296 new->image = imlib_create_cropped_image(x, y, w, h);
298 image_compute(new);
300 return 1;
303 /** Crop the image to the given rectangle and scales it.
304 * \param L The Lua VM state.
305 * \return The number of elements pushed on stack.
306 * \luastack
307 * \lvalue An image.
308 * \lparam The top left x coordinate of the source rectangle.
309 * \lparam The top left y coordinate of the source rectangle.
310 * \lparam The width of the source rectangle.
311 * \lparam The height of the source rectangle.
312 * \lparam The width of the destination rectangle.
313 * \lparam The height of the destination rectangle.
314 * \lreturn A cropped image.
316 static int
317 luaA_image_crop_and_scale(lua_State *L)
319 image_t *image = luaL_checkudata(L, 1, "image"), *new;
320 int source_x = luaL_checkint(L, 2);
321 int source_y = luaL_checkint(L, 3);
322 int w = luaL_checkint(L, 4);
323 int h = luaL_checkint(L, 5);
324 int dest_w = luaL_checkint(L, 6);
325 int dest_h = luaL_checkint(L, 7);
327 new = image_new(L);
329 imlib_context_set_image(image->image);
330 new->image = imlib_create_cropped_scaled_image(source_x,
331 source_y,
332 w, h,
333 dest_w, dest_h);
335 image_compute(new);
337 return 1;
340 /** Draw a pixel in an image
341 * \param L The Lua VM state
342 * \luastack
343 * \lvalua An image.
344 * \lparam The x coordinate of the pixel to draw
345 * \lparam The y coordinate of the pixel to draw
346 * \lparam The color to draw the pixel in
348 static int
349 luaA_image_draw_pixel(lua_State *L)
351 size_t len;
352 color_t color;
353 color_init_cookie_t cookie;
354 image_t *image = luaL_checkudata(L, 1, "image");
355 int x = luaL_checkint(L, 2);
356 int y = luaL_checkint(L, 3);
357 const char *buf = luaL_checklstring(L, 4, &len);
359 cookie = color_init_unchecked(&color, buf, len);
360 imlib_context_set_image(image->image);
361 color_init_reply(cookie);
363 if((x > imlib_image_get_width()) || (y > imlib_image_get_height()))
364 return 0;
366 imlib_context_set_color(color.red, color.green, color.blue, color.alpha);
367 imlib_image_draw_pixel(x, y, 1);
368 image_compute(image);
369 return 0;
372 /** Draw a line in an image
373 * \param L The Lua VM state
374 * \luastack
375 * \lvalua An image.
376 * \lparam The x1 coordinate of the line to draw
377 * \lparam The y1 coordinate of the line to draw
378 * \lparam The x2 coordinate of the line to draw
379 * \lparam The y2 coordinate of the line to draw
380 * \lparam The color to draw the line in
382 static int
383 luaA_image_draw_line(lua_State *L)
385 size_t len;
386 color_t color;
387 color_init_cookie_t cookie;
388 image_t *image = luaL_checkudata(L, 1, "image");
389 int x1 = luaL_checkint(L, 2);
390 int y1 = luaL_checkint(L, 3);
391 int x2 = luaL_checkint(L, 4);
392 int y2 = luaL_checkint(L, 5);
393 const char *buf = luaL_checklstring(L, 6, &len);
395 cookie = color_init_unchecked(&color, buf, len);
396 imlib_context_set_image(image->image);
397 color_init_reply(cookie);
399 if((MAX(x1,x2) > imlib_image_get_width()) || (MAX(y1,y2) > imlib_image_get_height()))
400 return 0;
402 imlib_context_set_color(color.red, color.green, color.blue, color.alpha);
403 imlib_image_draw_line(x1, y1, x2, y2, 1);
404 image_compute(image);
405 return 0;
408 /** Draw a rectangle in an image
409 * \param L The Lua VM state
410 * \luastack
411 * \lvalua An image.
412 * \lparam The x coordinate of the rectangles top left corner
413 * \lparam The y coordinate of the rectangles top left corner
414 * \lparam The width of the rectangle
415 * \lparam The height of the rectangle
416 * \lparam True if the rectangle should be filled, False otherwise
417 * \lparam The color to draw the rectangle in
419 static int
420 luaA_image_draw_rectangle(lua_State *L)
422 size_t len;
423 color_t color;
424 color_init_cookie_t cookie;
425 image_t *image = luaL_checkudata(L, 1, "image");
426 int x = luaL_checkint(L, 2);
427 int y = luaL_checkint(L, 3);
428 int width = luaL_checkint(L, 4);
429 int height = luaL_checkint(L, 5);
430 int fill = luaA_checkboolean(L, 6);
431 const char *buf = luaL_checklstring(L, 7, &len);
433 cookie = color_init_unchecked(&color, buf, len);
434 imlib_context_set_image(image->image);
435 color_init_reply(cookie);
437 if((x > imlib_image_get_width()) || (x + width > imlib_image_get_width()))
438 return 0;
439 if((y > imlib_image_get_height()) || (y + height > imlib_image_get_height()))
440 return 0;
442 imlib_context_set_color(color.red, color.green, color.blue, color.alpha);
443 if(!fill)
444 imlib_image_draw_rectangle(x, y, width, height);
445 else
446 imlib_image_fill_rectangle(x, y, width, height);
447 image_compute(image);
448 return 0;
451 /** Draw a circle in an image
452 * \param L The Lua VM state
453 * \luastack
454 * \lvalua An image.
455 * \lparam The x coordinate of the center of the circle
456 * \lparam The y coordinate of the center of the circle
457 * \lparam The horizontal amplitude (width)
458 * \lparam The vertical amplitude (height)
459 * \lparam True if the circle should be filled, False otherwise
460 * \lparam The color to draw the circle in
462 static int
463 luaA_image_draw_circle(lua_State *L)
465 size_t len;
466 color_t color;
467 color_init_cookie_t cookie;
468 image_t *image = luaL_checkudata(L, 1, "image");
469 int x = luaL_checkint(L, 2);
470 int y = luaL_checkint(L, 3);
471 int ah = luaL_checkint(L, 4);
472 int av = luaL_checkint(L, 5);
473 int fill = luaA_checkboolean(L, 6);
474 const char *buf = luaL_checklstring(L, 7, &len);
476 cookie = color_init_unchecked(&color, buf, len);
477 imlib_context_set_image(image->image);
478 color_init_reply(cookie);
480 if((x > imlib_image_get_width()) || (x + ah > imlib_image_get_width()) || (x - ah < 0))
481 return 0;
482 if((y > imlib_image_get_height()) || (y + av > imlib_image_get_height()) || (y - av < 0))
483 return 0;
485 imlib_context_set_color(color.red, color.green, color.blue, color.alpha);
486 if(!fill)
487 imlib_image_draw_ellipse(x, y, ah, av);
488 else
489 imlib_image_fill_ellipse(x, y, ah, av);
490 image_compute(image);
491 return 0;
494 /** Saves the image to the given path. The file extension (e.g. .png or .jpg)
495 * will affect the output format.
496 * \param L The Lua VM state.
497 * \return The number of elements pushed on stack.
498 * \luastack
499 * \lvalue An image.
500 * \lparam The image path.
502 static int
503 luaA_image_save(lua_State *L)
505 image_t *image = luaL_checkudata(L, 1, "image");
506 const char *path = luaL_checkstring(L, 2);
507 Imlib_Load_Error err;
509 imlib_context_set_image(image->image);
510 imlib_save_image_with_error_return(path, &err);
512 if(err != IMLIB_LOAD_ERROR_NONE)
513 warn("cannot save image %s: %s", path, image_imlib_load_strerror(err));
515 return 0;
518 /** Image object.
519 * \param L The Lua VM state.
520 * \return The number of elements pushed on stack.
521 * \luastack
522 * \lvalue An image
523 * \lfield width The image width.
524 * \lfield height The image height.
526 static int
527 luaA_image_index(lua_State *L)
529 if(luaA_usemetatable(L, 1, 2))
530 return 1;
532 image_t *image = luaL_checkudata(L, 1, "image");
533 size_t len;
534 const char *attr = luaL_checklstring(L, 2, &len);
536 switch(a_tokenize(attr, len))
538 case A_TK_WIDTH:
539 imlib_context_set_image(image->image);
540 lua_pushnumber(L, imlib_image_get_width());
541 break;
542 case A_TK_HEIGHT:
543 imlib_context_set_image(image->image);
544 lua_pushnumber(L, imlib_image_get_height());
545 break;
546 case A_TK_ALPHA:
547 imlib_context_set_image(image->image);
548 lua_pushboolean(L, imlib_image_has_alpha());
549 break;
550 default:
551 return 0;
554 return 1;
557 const struct luaL_reg awesome_image_methods[] =
559 { "__call", luaA_image_new },
560 { "argb32", luaA_image_argb32_new },
561 { NULL, NULL }
563 const struct luaL_reg awesome_image_meta[] =
565 { "__index", luaA_image_index },
566 { "rotate", luaA_image_rotate },
567 { "orientate", luaA_image_orientate },
568 { "crop", luaA_image_crop },
569 { "crop_and_scale", luaA_image_crop_and_scale },
570 { "save", luaA_image_save },
571 /* draw on images, whee! */
572 { "draw_pixel", luaA_image_draw_pixel },
573 { "draw_line", luaA_image_draw_line },
574 { "draw_rectangle", luaA_image_draw_rectangle },
575 { "draw_circle", luaA_image_draw_circle },
576 /* */
577 { "__gc", luaA_image_gc },
578 { "__tostring", luaA_image_tostring },
579 { NULL, NULL }
582 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80