2 * image.c - image object
4 * Copyright © 2008 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.
24 extern awesome_t globalconf
;
26 DO_LUA_NEW(extern, image_t
, image
, "image", image_ref
)
27 DO_LUA_GC(image_t
, image
, "image", image_unref
)
28 DO_LUA_EQ(image_t
, image
, "image")
31 image_imlib_load_strerror(Imlib_Load_Error e
)
35 case IMLIB_LOAD_ERROR_FILE_DOES_NOT_EXIST
:
36 return "no such file or directory";
37 case IMLIB_LOAD_ERROR_FILE_IS_DIRECTORY
:
38 return "file is a directory";
39 case IMLIB_LOAD_ERROR_PERMISSION_DENIED_TO_READ
:
40 return "read permission denied";
41 case IMLIB_LOAD_ERROR_NO_LOADER_FOR_FILE_FORMAT
:
42 return "no loader for file format";
43 case IMLIB_LOAD_ERROR_PATH_TOO_LONG
:
44 return "path too long";
45 case IMLIB_LOAD_ERROR_PATH_COMPONENT_NON_EXISTANT
:
46 return "path component non existant";
47 case IMLIB_LOAD_ERROR_PATH_COMPONENT_NOT_DIRECTORY
:
48 return "path compoment not a directory";
49 case IMLIB_LOAD_ERROR_PATH_POINTS_OUTSIDE_ADDRESS_SPACE
:
50 return "path points oustide address space";
51 case IMLIB_LOAD_ERROR_TOO_MANY_SYMBOLIC_LINKS
:
52 return "too many symbolic links";
53 case IMLIB_LOAD_ERROR_OUT_OF_MEMORY
:
54 return "out of memory";
55 case IMLIB_LOAD_ERROR_OUT_OF_FILE_DESCRIPTORS
:
56 return "out of file descriptors";
57 case IMLIB_LOAD_ERROR_PERMISSION_DENIED_TO_WRITE
:
58 return "write permission denied";
59 case IMLIB_LOAD_ERROR_OUT_OF_DISK_SPACE
:
60 return "out of disk space";
61 case IMLIB_LOAD_ERROR_UNKNOWN
:
62 return "unknown error, that's really bad";
63 case IMLIB_LOAD_ERROR_NONE
:
64 return "no error, oops";
67 return "unknown error";
70 /** Recompute the ARGB32 data from an image.
71 * \param image The image.
75 image_compute(image_t
*image
)
82 imlib_context_set_image(image
->image
);
84 data
= imlib_image_get_data_for_reading_only();
86 image
->width
= imlib_image_get_width();
87 image
->height
= imlib_image_get_height();
89 size
= image
->width
* image
->height
;
91 p_delete(&image
->data
);
92 image
->data
= dataimg
= p_new(uint8_t, size
* 4);
94 for(i
= 0; i
< size
; i
++, dataimg
+= 4)
96 dataimg
[3] = (data
[i
] >> 24) & 0xff; /* A */
97 /* cairo wants pre-multiplied alpha */
98 alpha
= dataimg
[3] / 255.0;
99 dataimg
[2] = ((data
[i
] >> 16) & 0xff) * alpha
; /* R */
100 dataimg
[1] = ((data
[i
] >> 8) & 0xff) * alpha
; /* G */
101 dataimg
[0] = (data
[i
] & 0xff) * alpha
; /* B */
105 /** Create a new image from ARGB32 data.
106 * \param width The image width.
107 * \param height The image height.
108 * \param data The image data.
109 * \return A brand new image.
112 image_new_from_argb32(int width
, int height
, uint32_t *data
)
115 image_t
*image
= NULL
;
117 if((imimage
= imlib_create_image_using_data(width
, height
, data
)))
119 image
= p_new(image_t
, 1);
120 image
->image
= imimage
;
121 image_compute(image
);
127 /** Load an image from filename.
128 * \param filename The image file to load.
129 * \return A new image.
132 image_new_from_file(const char *filename
)
135 Imlib_Load_Error e
= IMLIB_LOAD_ERROR_NONE
;
141 if(!(imimage
= imlib_load_image_with_error_return(filename
, &e
)))
143 warn("cannot load image %s: %s", filename
, image_imlib_load_strerror(e
));
147 image
= p_new(image_t
, 1);
148 image
->image
= imimage
;
150 image_compute(image
);
155 /** Create a new image object.
156 * \param L The Lua stack.
157 * \return The number of elements pushed on stack.
159 * \lparam The image path.
160 * \lreturn An image object.
163 luaA_image_new(lua_State
*L
)
165 const char *filename
= luaL_checkstring(L
, 2);
168 if((image
= image_new_from_file(filename
)))
169 return luaA_image_userdata_new(L
, image
);
174 /** Create a new image object from ARGB32 data.
175 * \param L The Lua stack.
176 * \return The number of elements pushed on stack.
178 * \lparam The image width.
179 * \lparam The image height.
180 * \lparam The image data as a string in ARGB32 format.
181 * \lreturn An image object.
184 luaA_image_argb32_new(lua_State
*L
)
188 unsigned int width
= luaL_checknumber(L
, 1);
189 unsigned int height
= luaL_checknumber(L
, 2);
190 const char *data
= luaL_checklstring(L
, 3, &len
);
192 if(width
* height
* 4 != len
)
193 luaL_error(L
, "string size does not match image size");
195 if((image
= image_new_from_argb32(width
, height
, (uint32_t *) data
)))
196 return luaA_image_userdata_new(L
, image
);
201 /** Rotate an image with specified angle radians and return a new image.
202 * \param L The Lua VM state.
203 * \return The number of elements pushed on stack.
206 * \lparam The angle in radians.
207 * \lreturn A rotated image.
210 luaA_image_rotate(lua_State
*L
)
212 image_t
**image
= luaA_checkudata(L
, 1, "image"), *new;
213 double angle
= luaL_checknumber(L
, 2);
215 new = p_new(image_t
, 1);
217 imlib_context_set_image((*image
)->image
);
218 new->image
= imlib_create_rotated_image(angle
);
222 return luaA_image_userdata_new(L
, new);
225 /** Crop an image to the given rectangle.
226 * \return The number of elements pushed on stack.
229 * \lparam The top left x coordinate of the rectangle.
230 * \lparam The top left y coordinate of the rectangle.
231 * \lparam The width of the rectangle.
232 * \lparam The height of the rectangle.
233 * \lreturn A cropped image.
236 luaA_image_crop(lua_State
*L
)
238 image_t
**image
= luaA_checkudata(L
, 1, "image"), *new;
239 int x
= luaL_checkint(L
, 2);
240 int y
= luaL_checkint(L
, 3);
241 int w
= luaL_checkint(L
, 4);
242 int h
= luaL_checkint(L
, 5);
244 new = p_new(image_t
, 1);
246 imlib_context_set_image((*image
)->image
);
247 new->image
= imlib_create_cropped_image(x
, y
, w
, h
);
251 return luaA_image_userdata_new(L
, new);
254 /** Crop the image to the given rectangle and scales it.
255 * \param L The Lua VM state.
256 * \return The number of elements pushed on stack.
259 * \lparam The top left x coordinate of the source rectangle.
260 * \lparam The top left y coordinate of the source rectangle.
261 * \lparam The width of the source rectangle.
262 * \lparam The height of the source rectangle.
263 * \lparam The width of the destination rectangle.
264 * \lparam The height of the destination rectangle.
265 * \lreturn A cropped image.
268 luaA_image_crop_and_scale(lua_State
*L
)
270 image_t
**image
= luaA_checkudata(L
, 1, "image"), *new;
271 int source_x
= luaL_checkint(L
, 2);
272 int source_y
= luaL_checkint(L
, 3);
273 int w
= luaL_checkint(L
, 4);
274 int h
= luaL_checkint(L
, 5);
275 int dest_w
= luaL_checkint(L
, 6);
276 int dest_h
= luaL_checkint(L
, 7);
278 new = p_new(image_t
, 1);
280 imlib_context_set_image((*image
)->image
);
281 new->image
= imlib_create_cropped_scaled_image(source_x
,
288 return luaA_image_userdata_new(L
, new);
292 * \param L The Lua VM state.
293 * \return The number of elements pushed on stack.
296 * \lfield width The image width.
297 * \lfield height The image height.
300 luaA_image_index(lua_State
*L
)
302 if(luaA_usemetatable(L
, 1, 2))
305 image_t
**image
= luaA_checkudata(L
, 1, "image");
307 const char *attr
= luaL_checklstring(L
, 2, &len
);
309 switch(a_tokenize(attr
, len
))
312 imlib_context_set_image((*image
)->image
);
313 lua_pushnumber(L
, imlib_image_get_width());
316 imlib_context_set_image((*image
)->image
);
317 lua_pushnumber(L
, imlib_image_get_height());
326 /** Return a formated string for an image.
327 * \param L The Lua VM state.
333 luaA_image_tostring(lua_State
*L
)
335 image_t
**p
= luaA_checkudata(L
, 1, "image");
336 lua_pushfstring(L
, "[image udata(%p) width(%d) height(%d)]", *p
,
337 (*p
)->width
, (*p
)->height
);
341 const struct luaL_reg awesome_image_methods
[] =
343 { "__call", luaA_image_new
},
344 { "argb32", luaA_image_argb32_new
},
347 const struct luaL_reg awesome_image_meta
[] =
349 { "__index", luaA_image_index
},
350 { "rotate", luaA_image_rotate
},
351 { "crop", luaA_image_crop
},
352 { "crop_and_scale", luaA_image_crop_and_scale
},
353 { "__gc", luaA_image_gc
},
354 { "__eq", luaA_image_eq
},
355 { "__tostring", luaA_image_tostring
},
359 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80