change codename
[awesome.git] / image.c
blob39e09e5e745aae04bafb0d8776acf1342826f54b
1 /*
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.
22 #include "structs.h"
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")
30 static const char *
31 image_imlib_load_strerror(Imlib_Load_Error e)
33 switch(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.
72 * \return Data.
74 static void
75 image_compute(image_t *image)
77 int size, i;
78 uint32_t *data;
79 double alpha;
80 uint8_t *dataimg;
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_realloc(&image->data, size * 4);
92 dataimg = image->data;
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.
111 image_t *
112 image_new_from_argb32(int width, int height, uint32_t *data)
114 Imlib_Image imimage;
115 image_t *image = NULL;
117 if((imimage = imlib_create_image_using_copied_data(width, height, data)))
119 imlib_context_set_image(imimage);
120 imlib_image_set_has_alpha(true);
121 image = p_new(image_t, 1);
122 image->image = imimage;
123 image_compute(image);
126 return image;
129 /** Load an image from filename.
130 * \param filename The image file to load.
131 * \return A new image.
133 image_t *
134 image_new_from_file(const char *filename)
136 Imlib_Image imimage;
137 Imlib_Load_Error e = IMLIB_LOAD_ERROR_NONE;
138 image_t *image;
140 if(!filename)
141 return NULL;
143 if(!(imimage = imlib_load_image_with_error_return(filename, &e)))
145 warn("cannot load image %s: %s", filename, image_imlib_load_strerror(e));
146 return NULL;
149 image = p_new(image_t, 1);
150 image->image = imimage;
152 image_compute(image);
154 return image;
157 /** Create a new image object.
158 * \param L The Lua stack.
159 * \return The number of elements pushed on stack.
160 * \luastack
161 * \lparam The image path, or nil to create an empty image.
162 * \lparam The image width if nil was set as first arg.
163 * \lparam The image height if nil was set as first arg.
164 * \lreturn An image object.
166 static int
167 luaA_image_new(lua_State *L)
169 const char *filename;
171 if((filename = lua_tostring(L, 2)))
173 image_t *image;
174 if((image = image_new_from_file(filename)))
175 return luaA_image_userdata_new(L, image);
177 else if(lua_isnil(L, 2))
179 int width = luaL_checknumber(L, 3);
180 int height = luaL_checknumber(L, 4);
182 if(width <= 0 || height <= 0)
183 luaL_error(L, "request image has invalid size");
185 Imlib_Image imimage = imlib_create_image(width, height);
186 image_t *image = p_new(image_t, 1);
187 image->image = imimage;
188 image_compute(image);
189 return luaA_image_userdata_new(L, image);
192 return 0;
195 /** Create a new image object from ARGB32 data.
196 * \param L The Lua stack.
197 * \return The number of elements pushed on stack.
198 * \luastack
199 * \lparam The image width.
200 * \lparam The image height.
201 * \lparam The image data as a string in ARGB32 format.
202 * \lreturn An image object.
204 static int
205 luaA_image_argb32_new(lua_State *L)
207 size_t len;
208 image_t *image;
209 unsigned int width = luaL_checknumber(L, 1);
210 unsigned int height = luaL_checknumber(L, 2);
211 const char *data = luaL_checklstring(L, 3, &len);
213 if(width * height * 4 != len)
214 luaL_error(L, "string size does not match image size");
216 if((image = image_new_from_argb32(width, height, (uint32_t *) data)))
217 return luaA_image_userdata_new(L, image);
219 return 0;
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.
227 * \luastack
228 * \lvalue An image.
229 * \lparam The rotation to perform.
231 static int
232 luaA_image_orientate(lua_State *L)
234 image_t **image = luaA_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);
242 return 0;
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.
248 * \luastack
249 * \lvalue An image.
250 * \lparam The angle in radians.
251 * \lreturn A rotated image.
253 static int
254 luaA_image_rotate(lua_State *L)
256 image_t **image = luaA_checkudata(L, 1, "image"), *new;
257 double angle = luaL_checknumber(L, 2);
259 new = p_new(image_t, 1);
261 imlib_context_set_image((*image)->image);
262 new->image = imlib_create_rotated_image(angle);
264 image_compute(new);
266 return luaA_image_userdata_new(L, new);
269 /** Crop an image to the given rectangle.
270 * \return The number of elements pushed on stack.
271 * \luastack
272 * \lvalue An image.
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.
279 static int
280 luaA_image_crop(lua_State *L)
282 image_t **image = luaA_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);
288 new = p_new(image_t, 1);
290 imlib_context_set_image((*image)->image);
291 new->image = imlib_create_cropped_image(x, y, w, h);
293 image_compute(new);
295 return luaA_image_userdata_new(L, new);
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.
301 * \luastack
302 * \lvalue An image.
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.
311 static int
312 luaA_image_crop_and_scale(lua_State *L)
314 image_t **image = luaA_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);
322 new = p_new(image_t, 1);
324 imlib_context_set_image((*image)->image);
325 new->image = imlib_create_cropped_scaled_image(source_x,
326 source_y,
327 w, h,
328 dest_w, dest_h);
330 image_compute(new);
332 return luaA_image_userdata_new(L, new);
335 /** Saves the image to the given path. The file extension (e.g. .png or .jpg)
336 * will affect the output format.
337 * \param L The Lua VM state.
338 * \return The number of elements pushed on stack.
339 * \luastack
340 * \lvalue An image.
341 * \lparam The image path.
343 static int
344 luaA_image_save(lua_State *L)
346 image_t **image = luaA_checkudata(L, 1, "image");
347 const char *path = luaL_checkstring(L, 2);
348 Imlib_Load_Error err;
350 imlib_context_set_image((*image)->image);
351 imlib_save_image_with_error_return(path, &err);
353 if(err != IMLIB_LOAD_ERROR_NONE)
354 warn("cannot save image %s: %s", path, image_imlib_load_strerror(err));
356 return 0;
359 /** Image object.
360 * \param L The Lua VM state.
361 * \return The number of elements pushed on stack.
362 * \luastack
363 * \lvalue An image
364 * \lfield width The image width.
365 * \lfield height The image height.
367 static int
368 luaA_image_index(lua_State *L)
370 if(luaA_usemetatable(L, 1, 2))
371 return 1;
373 image_t **image = luaA_checkudata(L, 1, "image");
374 size_t len;
375 const char *attr = luaL_checklstring(L, 2, &len);
377 switch(a_tokenize(attr, len))
379 case A_TK_WIDTH:
380 imlib_context_set_image((*image)->image);
381 lua_pushnumber(L, imlib_image_get_width());
382 break;
383 case A_TK_HEIGHT:
384 imlib_context_set_image((*image)->image);
385 lua_pushnumber(L, imlib_image_get_height());
386 break;
387 case A_TK_ALPHA:
388 imlib_context_set_image((*image)->image);
389 lua_pushboolean(L, imlib_image_has_alpha());
390 break;
391 default:
392 return 0;
395 return 1;
398 const struct luaL_reg awesome_image_methods[] =
400 { "__call", luaA_image_new },
401 { "argb32", luaA_image_argb32_new },
402 { NULL, NULL }
404 const struct luaL_reg awesome_image_meta[] =
406 { "__index", luaA_image_index },
407 { "rotate", luaA_image_rotate },
408 { "orientate", luaA_image_orientate },
409 { "crop", luaA_image_crop },
410 { "crop_and_scale", luaA_image_crop_and_scale },
411 { "save", luaA_image_save },
412 { "__gc", luaA_image_gc },
413 { "__eq", luaA_image_eq },
414 { "__tostring", luaA_image_tostring },
415 { NULL, NULL }
418 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80