2008-09-11 14:39:47 +02:00
|
|
|
/*
|
|
|
|
* image.c - image object
|
|
|
|
*
|
2009-04-09 11:38:56 +02:00
|
|
|
* Copyright © 2008-2009 Julien Danjou <julien@danjou.info>
|
2008-09-11 14:39:47 +02:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "structs.h"
|
2009-04-17 16:52:25 +02:00
|
|
|
#include "common/tokenize.h"
|
2009-06-18 14:34:14 +02:00
|
|
|
#include "common/xutil.h"
|
|
|
|
#include <xcb/xcb_image.h>
|
2008-09-11 14:39:47 +02:00
|
|
|
|
2009-04-09 11:38:56 +02:00
|
|
|
DO_LUA_TOSTRING(image_t, image, "image");
|
|
|
|
|
|
|
|
static int
|
|
|
|
luaA_image_gc(lua_State *L)
|
|
|
|
{
|
|
|
|
image_t *p = luaL_checkudata(L, 1, "image");
|
|
|
|
imlib_context_set_image(p->image);
|
|
|
|
imlib_free_image();
|
|
|
|
p_delete(&p->data);
|
2009-06-16 16:24:58 +02:00
|
|
|
return luaA_object_gc(L);
|
2009-04-09 11:38:56 +02:00
|
|
|
}
|
2008-09-11 14:39:47 +02:00
|
|
|
|
2008-09-17 16:38:38 +02:00
|
|
|
static const char *
|
|
|
|
image_imlib_load_strerror(Imlib_Load_Error e)
|
|
|
|
{
|
|
|
|
switch(e)
|
|
|
|
{
|
|
|
|
case IMLIB_LOAD_ERROR_FILE_DOES_NOT_EXIST:
|
|
|
|
return "no such file or directory";
|
|
|
|
case IMLIB_LOAD_ERROR_FILE_IS_DIRECTORY:
|
|
|
|
return "file is a directory";
|
|
|
|
case IMLIB_LOAD_ERROR_PERMISSION_DENIED_TO_READ:
|
|
|
|
return "read permission denied";
|
|
|
|
case IMLIB_LOAD_ERROR_NO_LOADER_FOR_FILE_FORMAT:
|
|
|
|
return "no loader for file format";
|
|
|
|
case IMLIB_LOAD_ERROR_PATH_TOO_LONG:
|
|
|
|
return "path too long";
|
|
|
|
case IMLIB_LOAD_ERROR_PATH_COMPONENT_NON_EXISTANT:
|
|
|
|
return "path component non existant";
|
|
|
|
case IMLIB_LOAD_ERROR_PATH_COMPONENT_NOT_DIRECTORY:
|
|
|
|
return "path compoment not a directory";
|
|
|
|
case IMLIB_LOAD_ERROR_PATH_POINTS_OUTSIDE_ADDRESS_SPACE:
|
|
|
|
return "path points oustide address space";
|
|
|
|
case IMLIB_LOAD_ERROR_TOO_MANY_SYMBOLIC_LINKS:
|
|
|
|
return "too many symbolic links";
|
|
|
|
case IMLIB_LOAD_ERROR_OUT_OF_MEMORY:
|
|
|
|
return "out of memory";
|
|
|
|
case IMLIB_LOAD_ERROR_OUT_OF_FILE_DESCRIPTORS:
|
|
|
|
return "out of file descriptors";
|
|
|
|
case IMLIB_LOAD_ERROR_PERMISSION_DENIED_TO_WRITE:
|
|
|
|
return "write permission denied";
|
|
|
|
case IMLIB_LOAD_ERROR_OUT_OF_DISK_SPACE:
|
|
|
|
return "out of disk space";
|
|
|
|
case IMLIB_LOAD_ERROR_UNKNOWN:
|
|
|
|
return "unknown error, that's really bad";
|
|
|
|
case IMLIB_LOAD_ERROR_NONE:
|
|
|
|
return "no error, oops";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "unknown error";
|
|
|
|
}
|
|
|
|
|
2009-05-04 10:34:54 +02:00
|
|
|
/** Get image width.
|
|
|
|
* \param image The image.
|
|
|
|
* \return The image width in pixel.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
image_getwidth(image_t *image)
|
|
|
|
{
|
|
|
|
imlib_context_set_image(image->image);
|
|
|
|
return imlib_image_get_width();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Get image height.
|
|
|
|
* \param image The image.
|
|
|
|
* \return The image height in pixel.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
image_getheight(image_t *image)
|
|
|
|
{
|
|
|
|
imlib_context_set_image(image->image);
|
|
|
|
return imlib_image_get_height();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Get the ARGB32 data from an image.
|
2008-09-17 17:28:38 +02:00
|
|
|
* \param image The image.
|
|
|
|
* \return Data.
|
2008-09-17 16:38:38 +02:00
|
|
|
*/
|
2009-05-04 10:34:54 +02:00
|
|
|
uint8_t *
|
|
|
|
image_getdata(image_t *image)
|
2008-09-17 16:38:38 +02:00
|
|
|
{
|
2008-09-17 17:28:38 +02:00
|
|
|
int size, i;
|
2008-09-17 16:38:38 +02:00
|
|
|
uint32_t *data;
|
|
|
|
double alpha;
|
2008-09-17 17:28:38 +02:00
|
|
|
uint8_t *dataimg;
|
2009-05-22 11:22:16 +02:00
|
|
|
#if AWESOME_IS_BIG_ENDIAN
|
|
|
|
const int index_a = 0, index_r = 1, index_g = 2, index_b = 3;
|
|
|
|
#else
|
|
|
|
const int index_a = 3, index_r = 2, index_g = 1, index_b = 0;
|
|
|
|
#endif
|
2008-09-17 16:38:38 +02:00
|
|
|
|
2009-05-04 10:34:54 +02:00
|
|
|
if(image->isupdated)
|
|
|
|
return image->data;
|
|
|
|
|
2008-09-17 17:28:38 +02:00
|
|
|
imlib_context_set_image(image->image);
|
2008-09-17 16:38:38 +02:00
|
|
|
|
2008-09-17 17:28:38 +02:00
|
|
|
data = imlib_image_get_data_for_reading_only();
|
2008-09-17 16:38:38 +02:00
|
|
|
|
2009-05-04 10:34:54 +02:00
|
|
|
size = imlib_image_get_width() * imlib_image_get_height();
|
2008-09-17 16:38:38 +02:00
|
|
|
|
2008-12-29 15:39:31 +01:00
|
|
|
p_realloc(&image->data, size * 4);
|
|
|
|
dataimg = image->data;
|
2008-09-17 16:38:38 +02:00
|
|
|
|
|
|
|
for(i = 0; i < size; i++, dataimg += 4)
|
|
|
|
{
|
2009-05-22 11:22:16 +02:00
|
|
|
dataimg[index_a] = (data[i] >> 24) & 0xff; /* A */
|
2009-04-27 12:09:04 +02:00
|
|
|
/* cairo wants pre-multiplied alpha */
|
2009-05-22 11:22:16 +02:00
|
|
|
alpha = dataimg[index_a] / 255.0;
|
|
|
|
dataimg[index_r] = ((data[i] >> 16) & 0xff) * alpha; /* R */
|
|
|
|
dataimg[index_g] = ((data[i] >> 8) & 0xff) * alpha; /* G */
|
|
|
|
dataimg[index_b] = (data[i] & 0xff) * alpha; /* B */
|
2008-09-17 16:38:38 +02:00
|
|
|
}
|
2009-05-04 10:34:54 +02:00
|
|
|
|
|
|
|
image->isupdated = true;
|
|
|
|
|
|
|
|
return image->data;
|
2009-06-18 14:34:14 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
image_draw_to_1bit_ximage(image_t *image, xcb_image_t *img)
|
|
|
|
{
|
|
|
|
imlib_context_set_image(image->image);
|
|
|
|
|
|
|
|
uint32_t *data = imlib_image_get_data_for_reading_only();
|
|
|
|
|
|
|
|
int width = imlib_image_get_width();
|
|
|
|
int height = imlib_image_get_height();
|
|
|
|
|
|
|
|
for(int y = 0; y < height; y++)
|
|
|
|
for(int x = 0; x < width; x++)
|
|
|
|
{
|
|
|
|
int i, pixel, tmp;
|
|
|
|
|
|
|
|
i = y * width + x;
|
|
|
|
|
|
|
|
// Sum up all color components ignoring alpha
|
|
|
|
tmp = (data[i] >> 16) & 0xff;
|
|
|
|
tmp += (data[i] >> 8) & 0xff;
|
|
|
|
tmp += data[i] & 0xff;
|
|
|
|
|
|
|
|
pixel = (tmp / 3 < 127) ? 0 : 1;
|
|
|
|
|
|
|
|
xcb_image_put_pixel(img, x, y, pixel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert an image to a 1bit pixmap
|
|
|
|
xcb_pixmap_t
|
|
|
|
image_to_1bit_pixmap(image_t *image, xcb_drawable_t d)
|
|
|
|
{
|
|
|
|
xcb_pixmap_t pixmap;
|
|
|
|
xcb_gcontext_t gc;
|
|
|
|
xcb_image_t *img;
|
|
|
|
uint16_t width, height;
|
|
|
|
|
|
|
|
width = image_getwidth(image);
|
|
|
|
height = image_getheight(image);
|
|
|
|
|
|
|
|
/* Prepare the pixmap and gc */
|
|
|
|
pixmap = xcb_generate_id(globalconf.connection);
|
|
|
|
xcb_create_pixmap(globalconf.connection, 1, pixmap, d, width, height);
|
|
|
|
|
|
|
|
gc = xcb_generate_id(globalconf.connection);
|
|
|
|
xcb_create_gc(globalconf.connection, gc, pixmap, 0, NULL);
|
|
|
|
|
|
|
|
/* Prepare the image */
|
|
|
|
img = xcb_image_create_native(globalconf.connection, width, height,
|
|
|
|
XCB_IMAGE_FORMAT_XY_BITMAP, 1, NULL, 0, NULL);
|
|
|
|
image_draw_to_1bit_ximage(image, img);
|
|
|
|
|
|
|
|
/* Paint the image to the pixmap */
|
|
|
|
xcb_image_put(globalconf.connection, pixmap, gc, img, 0, 0, 0);
|
|
|
|
|
|
|
|
xcb_free_gc(globalconf.connection, gc);
|
|
|
|
|
|
|
|
xcb_image_destroy(img);
|
|
|
|
|
|
|
|
return pixmap;
|
2008-09-17 17:28:38 +02:00
|
|
|
}
|
2008-09-17 16:38:38 +02:00
|
|
|
|
2008-09-17 17:28:38 +02:00
|
|
|
/** Create a new image from ARGB32 data.
|
|
|
|
* \param width The image width.
|
|
|
|
* \param height The image height.
|
|
|
|
* \param data The image data.
|
2009-04-09 11:38:56 +02:00
|
|
|
* \return 1 if an image has been pushed on stack, 0 otherwise.
|
2008-09-17 17:28:38 +02:00
|
|
|
*/
|
2009-04-09 11:38:56 +02:00
|
|
|
int
|
2008-09-17 17:28:38 +02:00
|
|
|
image_new_from_argb32(int width, int height, uint32_t *data)
|
|
|
|
{
|
|
|
|
Imlib_Image imimage;
|
2008-09-17 16:38:38 +02:00
|
|
|
|
2008-12-16 16:44:25 +01:00
|
|
|
if((imimage = imlib_create_image_using_copied_data(width, height, data)))
|
2008-09-17 17:28:38 +02:00
|
|
|
{
|
2008-12-18 17:39:41 +01:00
|
|
|
imlib_context_set_image(imimage);
|
|
|
|
imlib_image_set_has_alpha(true);
|
2009-04-09 11:38:56 +02:00
|
|
|
image_t *image = image_new(globalconf.L);
|
2008-09-17 17:28:38 +02:00
|
|
|
image->image = imimage;
|
2009-04-09 11:38:56 +02:00
|
|
|
return 1;
|
2008-09-17 17:28:38 +02:00
|
|
|
}
|
|
|
|
|
2009-04-09 11:38:56 +02:00
|
|
|
return 0;
|
2008-09-17 17:28:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Load an image from filename.
|
|
|
|
* \param filename The image file to load.
|
2009-04-09 11:38:56 +02:00
|
|
|
* \return 1 if image is loaded and on stack, 0 otherwise.
|
2008-09-17 17:28:38 +02:00
|
|
|
*/
|
2009-04-09 11:38:56 +02:00
|
|
|
static int
|
2008-09-17 17:28:38 +02:00
|
|
|
image_new_from_file(const char *filename)
|
|
|
|
{
|
|
|
|
Imlib_Image imimage;
|
|
|
|
Imlib_Load_Error e = IMLIB_LOAD_ERROR_NONE;
|
|
|
|
image_t *image;
|
|
|
|
|
|
|
|
if(!filename)
|
2009-04-09 11:38:56 +02:00
|
|
|
return 0;
|
2008-09-17 17:28:38 +02:00
|
|
|
|
|
|
|
if(!(imimage = imlib_load_image_with_error_return(filename, &e)))
|
|
|
|
{
|
|
|
|
warn("cannot load image %s: %s", filename, image_imlib_load_strerror(e));
|
2009-04-09 11:38:56 +02:00
|
|
|
return 0;
|
2008-09-17 17:28:38 +02:00
|
|
|
}
|
|
|
|
|
2009-04-09 11:38:56 +02:00
|
|
|
image = image_new(globalconf.L);
|
2008-09-17 17:28:38 +02:00
|
|
|
image->image = imimage;
|
2008-09-17 16:38:38 +02:00
|
|
|
|
2009-04-09 11:38:56 +02:00
|
|
|
return 1;
|
2008-09-17 16:38:38 +02:00
|
|
|
}
|
|
|
|
|
2008-09-11 14:39:47 +02:00
|
|
|
/** Create a new image object.
|
|
|
|
* \param L The Lua stack.
|
|
|
|
* \return The number of elements pushed on stack.
|
|
|
|
* \luastack
|
2009-05-07 20:24:46 +02:00
|
|
|
* \lparam The image path.
|
2008-09-11 14:39:47 +02:00
|
|
|
* \lreturn An image object.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
luaA_image_new(lua_State *L)
|
|
|
|
{
|
2008-11-05 13:38:11 +01:00
|
|
|
const char *filename;
|
2008-09-11 14:39:47 +02:00
|
|
|
|
2008-11-05 13:38:11 +01:00
|
|
|
if((filename = lua_tostring(L, 2)))
|
2009-04-09 11:38:56 +02:00
|
|
|
return image_new_from_file(filename);
|
2008-09-11 14:39:47 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-10-22 20:57:27 +02:00
|
|
|
/** Create a new image object from ARGB32 data.
|
|
|
|
* \param L The Lua stack.
|
|
|
|
* \return The number of elements pushed on stack.
|
|
|
|
* \luastack
|
|
|
|
* \lparam The image width.
|
|
|
|
* \lparam The image height.
|
2009-05-04 10:49:45 +02:00
|
|
|
* \lparam The image data as a string in ARGB32 format, or nil to create an
|
|
|
|
* empty image.
|
2008-10-22 20:57:27 +02:00
|
|
|
* \lreturn An image object.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
luaA_image_argb32_new(lua_State *L)
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
unsigned int width = luaL_checknumber(L, 1);
|
|
|
|
unsigned int height = luaL_checknumber(L, 2);
|
2009-05-04 10:49:45 +02:00
|
|
|
|
|
|
|
if(lua_isnil(L, 3))
|
|
|
|
{
|
|
|
|
uint32_t *data = p_new(uint32_t, width * height);
|
|
|
|
return image_new_from_argb32(width, height, data);
|
|
|
|
}
|
|
|
|
|
2008-10-22 20:57:27 +02:00
|
|
|
const char *data = luaL_checklstring(L, 3, &len);
|
|
|
|
|
|
|
|
if(width * height * 4 != len)
|
|
|
|
luaL_error(L, "string size does not match image size");
|
|
|
|
|
2009-04-09 11:38:56 +02:00
|
|
|
return image_new_from_argb32(width, height, (uint32_t *) data);
|
2008-10-22 20:57:27 +02:00
|
|
|
}
|
|
|
|
|
2008-12-16 16:45:24 +01:00
|
|
|
/** Performs 90 degree rotations on the current image. Passing 0 orientation
|
|
|
|
* does not rotate, 1 rotates clockwise by 90 degree, 2, rotates clockwise by
|
|
|
|
* 180 degrees, 3 rotates clockwise by 270 degrees.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \return The number of elements pushed on stack.
|
|
|
|
* \luastack
|
|
|
|
* \lvalue An image.
|
|
|
|
* \lparam The rotation to perform.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
luaA_image_orientate(lua_State *L)
|
|
|
|
{
|
2009-04-09 11:38:56 +02:00
|
|
|
image_t *image = luaL_checkudata(L, 1, "image");
|
2008-12-16 16:45:24 +01:00
|
|
|
int orientation = luaL_checknumber(L, 2);
|
|
|
|
|
2009-04-09 11:38:56 +02:00
|
|
|
imlib_context_set_image(image->image);
|
2008-12-16 16:45:24 +01:00
|
|
|
imlib_image_orientate(orientation);
|
|
|
|
|
2009-05-04 10:34:54 +02:00
|
|
|
image->isupdated = false;
|
2008-12-16 16:45:24 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-09-17 17:47:28 +02:00
|
|
|
/** Rotate an image with specified angle radians and return a new image.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \return The number of elements pushed on stack.
|
|
|
|
* \luastack
|
|
|
|
* \lvalue An image.
|
|
|
|
* \lparam The angle in radians.
|
|
|
|
* \lreturn A rotated image.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
luaA_image_rotate(lua_State *L)
|
|
|
|
{
|
2009-04-09 11:38:56 +02:00
|
|
|
image_t *image = luaL_checkudata(L, 1, "image"), *new;
|
2008-09-17 17:47:28 +02:00
|
|
|
double angle = luaL_checknumber(L, 2);
|
|
|
|
|
2009-04-09 11:38:56 +02:00
|
|
|
new = image_new(L);
|
2008-09-17 17:47:28 +02:00
|
|
|
|
2009-04-09 11:38:56 +02:00
|
|
|
imlib_context_set_image(image->image);
|
2008-09-17 17:47:28 +02:00
|
|
|
new->image = imlib_create_rotated_image(angle);
|
|
|
|
|
2009-04-09 11:38:56 +02:00
|
|
|
return 1;
|
2008-09-17 17:47:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Crop an image to the given rectangle.
|
|
|
|
* \return The number of elements pushed on stack.
|
|
|
|
* \luastack
|
|
|
|
* \lvalue An image.
|
|
|
|
* \lparam The top left x coordinate of the rectangle.
|
|
|
|
* \lparam The top left y coordinate of the rectangle.
|
|
|
|
* \lparam The width of the rectangle.
|
|
|
|
* \lparam The height of the rectangle.
|
|
|
|
* \lreturn A cropped image.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
luaA_image_crop(lua_State *L)
|
|
|
|
{
|
2009-04-09 11:38:56 +02:00
|
|
|
image_t *image = luaL_checkudata(L, 1, "image"), *new;
|
2008-09-17 17:47:28 +02:00
|
|
|
int x = luaL_checkint(L, 2);
|
|
|
|
int y = luaL_checkint(L, 3);
|
|
|
|
int w = luaL_checkint(L, 4);
|
|
|
|
int h = luaL_checkint(L, 5);
|
|
|
|
|
2009-04-09 11:38:56 +02:00
|
|
|
new = image_new(L);
|
2008-09-17 17:47:28 +02:00
|
|
|
|
2009-04-09 11:38:56 +02:00
|
|
|
imlib_context_set_image(image->image);
|
2008-09-17 17:47:28 +02:00
|
|
|
new->image = imlib_create_cropped_image(x, y, w, h);
|
|
|
|
|
2009-04-09 11:38:56 +02:00
|
|
|
return 1;
|
2008-09-17 17:47:28 +02:00
|
|
|
}
|
|
|
|
|
2008-10-14 17:37:39 +02:00
|
|
|
/** Crop the image to the given rectangle and scales it.
|
2008-10-21 18:29:02 +02:00
|
|
|
* \param L The Lua VM state.
|
2008-10-14 17:37:39 +02:00
|
|
|
* \return The number of elements pushed on stack.
|
|
|
|
* \luastack
|
|
|
|
* \lvalue An image.
|
|
|
|
* \lparam The top left x coordinate of the source rectangle.
|
|
|
|
* \lparam The top left y coordinate of the source rectangle.
|
|
|
|
* \lparam The width of the source rectangle.
|
|
|
|
* \lparam The height of the source rectangle.
|
|
|
|
* \lparam The width of the destination rectangle.
|
|
|
|
* \lparam The height of the destination rectangle.
|
|
|
|
* \lreturn A cropped image.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
luaA_image_crop_and_scale(lua_State *L)
|
|
|
|
{
|
2009-04-09 11:38:56 +02:00
|
|
|
image_t *image = luaL_checkudata(L, 1, "image"), *new;
|
2008-10-14 17:37:39 +02:00
|
|
|
int source_x = luaL_checkint(L, 2);
|
|
|
|
int source_y = luaL_checkint(L, 3);
|
|
|
|
int w = luaL_checkint(L, 4);
|
|
|
|
int h = luaL_checkint(L, 5);
|
|
|
|
int dest_w = luaL_checkint(L, 6);
|
|
|
|
int dest_h = luaL_checkint(L, 7);
|
|
|
|
|
2009-04-09 11:38:56 +02:00
|
|
|
new = image_new(L);
|
2008-10-14 17:37:39 +02:00
|
|
|
|
2009-04-09 11:38:56 +02:00
|
|
|
imlib_context_set_image(image->image);
|
2008-10-14 17:37:39 +02:00
|
|
|
new->image = imlib_create_cropped_scaled_image(source_x,
|
|
|
|
source_y,
|
|
|
|
w, h,
|
|
|
|
dest_w, dest_h);
|
|
|
|
|
2009-04-09 11:38:56 +02:00
|
|
|
return 1;
|
2008-10-14 17:37:39 +02:00
|
|
|
}
|
|
|
|
|
2009-04-16 03:05:16 +02:00
|
|
|
/** Draw a pixel in an image
|
|
|
|
* \param L The Lua VM state
|
|
|
|
* \luastack
|
2009-07-27 06:20:00 +02:00
|
|
|
* \lvalue An image.
|
2009-04-16 03:05:16 +02:00
|
|
|
* \lparam The x coordinate of the pixel to draw
|
|
|
|
* \lparam The y coordinate of the pixel to draw
|
|
|
|
* \lparam The color to draw the pixel in
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
luaA_image_draw_pixel(lua_State *L)
|
|
|
|
{
|
|
|
|
size_t len;
|
2009-04-17 18:49:27 +02:00
|
|
|
color_t color;
|
|
|
|
color_init_cookie_t cookie;
|
2009-04-16 03:05:16 +02:00
|
|
|
image_t *image = luaL_checkudata(L, 1, "image");
|
|
|
|
int x = luaL_checkint(L, 2);
|
|
|
|
int y = luaL_checkint(L, 3);
|
|
|
|
const char *buf = luaL_checklstring(L, 4, &len);
|
|
|
|
|
2009-04-17 18:49:27 +02:00
|
|
|
cookie = color_init_unchecked(&color, buf, len);
|
2009-04-16 03:05:16 +02:00
|
|
|
imlib_context_set_image(image->image);
|
2009-04-17 18:49:27 +02:00
|
|
|
color_init_reply(cookie);
|
2009-04-16 03:05:16 +02:00
|
|
|
|
|
|
|
if((x > imlib_image_get_width()) || (y > imlib_image_get_height()))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
imlib_context_set_color(color.red, color.green, color.blue, color.alpha);
|
|
|
|
imlib_image_draw_pixel(x, y, 1);
|
2009-05-04 10:34:54 +02:00
|
|
|
image->isupdated = false;
|
2009-04-16 03:05:16 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Draw a line in an image
|
|
|
|
* \param L The Lua VM state
|
|
|
|
* \luastack
|
2009-07-27 06:20:00 +02:00
|
|
|
* \lvalue An image.
|
2009-04-16 03:05:16 +02:00
|
|
|
* \lparam The x1 coordinate of the line to draw
|
|
|
|
* \lparam The y1 coordinate of the line to draw
|
|
|
|
* \lparam The x2 coordinate of the line to draw
|
|
|
|
* \lparam The y2 coordinate of the line to draw
|
|
|
|
* \lparam The color to draw the line in
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
luaA_image_draw_line(lua_State *L)
|
|
|
|
{
|
|
|
|
size_t len;
|
2009-04-17 18:49:27 +02:00
|
|
|
color_t color;
|
|
|
|
color_init_cookie_t cookie;
|
2009-04-16 03:05:16 +02:00
|
|
|
image_t *image = luaL_checkudata(L, 1, "image");
|
|
|
|
int x1 = luaL_checkint(L, 2);
|
|
|
|
int y1 = luaL_checkint(L, 3);
|
|
|
|
int x2 = luaL_checkint(L, 4);
|
|
|
|
int y2 = luaL_checkint(L, 5);
|
|
|
|
const char *buf = luaL_checklstring(L, 6, &len);
|
|
|
|
|
2009-04-17 18:49:27 +02:00
|
|
|
cookie = color_init_unchecked(&color, buf, len);
|
2009-04-16 03:05:16 +02:00
|
|
|
imlib_context_set_image(image->image);
|
2009-04-17 18:49:27 +02:00
|
|
|
color_init_reply(cookie);
|
2009-04-16 03:05:16 +02:00
|
|
|
|
|
|
|
if((MAX(x1,x2) > imlib_image_get_width()) || (MAX(y1,y2) > imlib_image_get_height()))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
imlib_context_set_color(color.red, color.green, color.blue, color.alpha);
|
2009-05-04 18:11:49 +02:00
|
|
|
imlib_image_draw_line(x1, y1, x2, y2, 0);
|
2009-05-04 10:34:54 +02:00
|
|
|
image->isupdated = false;
|
2009-04-16 03:05:16 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Draw a rectangle in an image
|
|
|
|
* \param L The Lua VM state
|
|
|
|
* \luastack
|
2009-07-27 06:20:00 +02:00
|
|
|
* \lvalue An image.
|
2009-04-16 03:05:16 +02:00
|
|
|
* \lparam The x coordinate of the rectangles top left corner
|
|
|
|
* \lparam The y coordinate of the rectangles top left corner
|
|
|
|
* \lparam The width of the rectangle
|
|
|
|
* \lparam The height of the rectangle
|
|
|
|
* \lparam True if the rectangle should be filled, False otherwise
|
|
|
|
* \lparam The color to draw the rectangle in
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
luaA_image_draw_rectangle(lua_State *L)
|
|
|
|
{
|
|
|
|
size_t len;
|
2009-04-17 18:49:27 +02:00
|
|
|
color_t color;
|
|
|
|
color_init_cookie_t cookie;
|
2009-04-16 03:05:16 +02:00
|
|
|
image_t *image = luaL_checkudata(L, 1, "image");
|
|
|
|
int x = luaL_checkint(L, 2);
|
|
|
|
int y = luaL_checkint(L, 3);
|
|
|
|
int width = luaL_checkint(L, 4);
|
|
|
|
int height = luaL_checkint(L, 5);
|
|
|
|
int fill = luaA_checkboolean(L, 6);
|
|
|
|
const char *buf = luaL_checklstring(L, 7, &len);
|
|
|
|
|
2009-04-17 18:49:27 +02:00
|
|
|
cookie = color_init_unchecked(&color, buf, len);
|
2009-04-16 03:05:16 +02:00
|
|
|
imlib_context_set_image(image->image);
|
2009-04-17 18:49:27 +02:00
|
|
|
color_init_reply(cookie);
|
2009-04-16 03:05:16 +02:00
|
|
|
|
|
|
|
if((x > imlib_image_get_width()) || (x + width > imlib_image_get_width()))
|
|
|
|
return 0;
|
|
|
|
if((y > imlib_image_get_height()) || (y + height > imlib_image_get_height()))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
imlib_context_set_color(color.red, color.green, color.blue, color.alpha);
|
|
|
|
if(!fill)
|
|
|
|
imlib_image_draw_rectangle(x, y, width, height);
|
|
|
|
else
|
|
|
|
imlib_image_fill_rectangle(x, y, width, height);
|
2009-05-04 10:34:54 +02:00
|
|
|
image->isupdated = false;
|
2009-04-16 03:05:16 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-05-04 16:36:59 +02:00
|
|
|
/** Convert a table to a color range and set it as current color range.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \param ud The index of the table.
|
|
|
|
* \return A color range that you are responsible to free.
|
|
|
|
*/
|
|
|
|
static Imlib_Color_Range
|
|
|
|
luaA_table_to_color_range(lua_State *L, int ud)
|
|
|
|
{
|
|
|
|
Imlib_Color_Range range = imlib_create_color_range();
|
|
|
|
|
|
|
|
imlib_context_set_color_range(range);
|
|
|
|
|
|
|
|
for(size_t i = 1; i <= lua_objlen(L, ud); i++)
|
|
|
|
{
|
|
|
|
/* get t[i] */
|
|
|
|
lua_pushnumber(L, i);
|
|
|
|
lua_gettable(L, ud);
|
|
|
|
|
|
|
|
size_t len;
|
|
|
|
const char *colstr = lua_tolstring(L, -1, &len);
|
|
|
|
|
|
|
|
if(colstr)
|
|
|
|
{
|
|
|
|
color_t color;
|
|
|
|
|
|
|
|
color_init_cookie_t cookie = color_init_unchecked(&color, colstr, len);
|
|
|
|
|
|
|
|
/* get value with colstr as key in table */
|
|
|
|
lua_pushvalue(L, -1);
|
|
|
|
lua_gettable(L, ud);
|
|
|
|
|
|
|
|
/* convert the distance, if any, to number, or set 1 */
|
|
|
|
int distance = lua_tonumber(L, -1);
|
|
|
|
/* remove distance */
|
|
|
|
lua_pop(L, 1);
|
|
|
|
|
|
|
|
color_init_reply(cookie);
|
|
|
|
|
|
|
|
imlib_context_set_color(color.red, color.green, color.blue, color.alpha);
|
|
|
|
|
|
|
|
imlib_add_color_to_color_range(distance);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* remove color string (value) */
|
|
|
|
lua_pop(L, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return range;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Draw a rectangle in an image with gradient color.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \return The number of elements pushed on stack.
|
|
|
|
* \luastack
|
2009-07-27 06:20:00 +02:00
|
|
|
* \lvalue An image.
|
2009-05-04 16:36:59 +02:00
|
|
|
* \lparam The x coordinate of the rectangles top left corner.
|
|
|
|
* \lparam The y coordinate of the rectangles top left corner.
|
|
|
|
* \lparam The width of the rectangle.
|
|
|
|
* \lparam The height of the rectangle.
|
|
|
|
* \lparam A table with the color to draw the rectangle. You can specified the
|
|
|
|
* color distance from the previous one by setting t[color] = distance.
|
|
|
|
* \lparam The angle of gradient.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
luaA_image_draw_rectangle_gradient(lua_State *L)
|
|
|
|
{
|
|
|
|
image_t *image = luaL_checkudata(L, 1, "image");
|
|
|
|
int x = luaL_checkint(L, 2);
|
|
|
|
int y = luaL_checkint(L, 3);
|
|
|
|
int width = luaL_checkint(L, 4);
|
|
|
|
int height = luaL_checkint(L, 5);
|
|
|
|
luaA_checktable(L, 6);
|
|
|
|
double angle = luaL_checknumber(L, 7);
|
|
|
|
|
|
|
|
if((x > imlib_image_get_width()) || (x + width > imlib_image_get_width()))
|
|
|
|
return 0;
|
|
|
|
if((y > imlib_image_get_height()) || (y + height > imlib_image_get_height()))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
imlib_context_set_image(image->image);
|
|
|
|
|
|
|
|
luaA_table_to_color_range(L, 6);
|
|
|
|
|
|
|
|
imlib_image_fill_color_range_rectangle(x, y, width, height, angle);
|
|
|
|
|
|
|
|
imlib_free_color_range();
|
|
|
|
|
2009-05-04 10:34:54 +02:00
|
|
|
image->isupdated = false;
|
2009-05-04 16:36:59 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-04-16 03:05:16 +02:00
|
|
|
/** Draw a circle in an image
|
|
|
|
* \param L The Lua VM state
|
|
|
|
* \luastack
|
2009-07-27 06:20:00 +02:00
|
|
|
* \lvalue An image.
|
2009-04-16 03:05:16 +02:00
|
|
|
* \lparam The x coordinate of the center of the circle
|
|
|
|
* \lparam The y coordinate of the center of the circle
|
|
|
|
* \lparam The horizontal amplitude (width)
|
|
|
|
* \lparam The vertical amplitude (height)
|
|
|
|
* \lparam True if the circle should be filled, False otherwise
|
|
|
|
* \lparam The color to draw the circle in
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
luaA_image_draw_circle(lua_State *L)
|
|
|
|
{
|
|
|
|
size_t len;
|
2009-04-17 18:49:27 +02:00
|
|
|
color_t color;
|
|
|
|
color_init_cookie_t cookie;
|
2009-04-16 03:05:16 +02:00
|
|
|
image_t *image = luaL_checkudata(L, 1, "image");
|
|
|
|
int x = luaL_checkint(L, 2);
|
|
|
|
int y = luaL_checkint(L, 3);
|
|
|
|
int ah = luaL_checkint(L, 4);
|
|
|
|
int av = luaL_checkint(L, 5);
|
|
|
|
int fill = luaA_checkboolean(L, 6);
|
|
|
|
const char *buf = luaL_checklstring(L, 7, &len);
|
|
|
|
|
2009-04-17 18:49:27 +02:00
|
|
|
cookie = color_init_unchecked(&color, buf, len);
|
2009-04-16 03:05:16 +02:00
|
|
|
imlib_context_set_image(image->image);
|
2009-04-17 18:49:27 +02:00
|
|
|
color_init_reply(cookie);
|
2009-04-16 03:05:16 +02:00
|
|
|
|
|
|
|
if((x > imlib_image_get_width()) || (x + ah > imlib_image_get_width()) || (x - ah < 0))
|
|
|
|
return 0;
|
|
|
|
if((y > imlib_image_get_height()) || (y + av > imlib_image_get_height()) || (y - av < 0))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
imlib_context_set_color(color.red, color.green, color.blue, color.alpha);
|
|
|
|
if(!fill)
|
|
|
|
imlib_image_draw_ellipse(x, y, ah, av);
|
|
|
|
else
|
|
|
|
imlib_image_fill_ellipse(x, y, ah, av);
|
2009-05-04 10:34:54 +02:00
|
|
|
image->isupdated = false;
|
2009-04-16 03:05:16 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-12-29 15:19:01 +01:00
|
|
|
/** Saves the image to the given path. The file extension (e.g. .png or .jpg)
|
|
|
|
* will affect the output format.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \return The number of elements pushed on stack.
|
|
|
|
* \luastack
|
|
|
|
* \lvalue An image.
|
|
|
|
* \lparam The image path.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
luaA_image_save(lua_State *L)
|
|
|
|
{
|
2009-04-09 11:38:56 +02:00
|
|
|
image_t *image = luaL_checkudata(L, 1, "image");
|
2008-12-29 15:19:01 +01:00
|
|
|
const char *path = luaL_checkstring(L, 2);
|
|
|
|
Imlib_Load_Error err;
|
|
|
|
|
2009-04-09 11:38:56 +02:00
|
|
|
imlib_context_set_image(image->image);
|
2008-12-29 15:19:01 +01:00
|
|
|
imlib_save_image_with_error_return(path, &err);
|
|
|
|
|
|
|
|
if(err != IMLIB_LOAD_ERROR_NONE)
|
|
|
|
warn("cannot save image %s: %s", path, image_imlib_load_strerror(err));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-05-27 15:14:47 +02:00
|
|
|
/** Insert one image into another.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \return The number of elements pushed on stack.
|
|
|
|
* \luastack
|
|
|
|
* \lvalue An image.
|
|
|
|
* \lparam The image to insert.
|
|
|
|
* \lparam The X offset of the image to insert (optional).
|
|
|
|
* \lparam The Y offset of the image to insert (optional).
|
|
|
|
* \lparam The horizontal offset of the upper right image corner (optional).
|
|
|
|
* \lparam The vertical offset of the upper right image corner (optional).
|
|
|
|
* \lparam The horizontal offset of the lower left image corner (optional).
|
|
|
|
* \lparam The vertical offset of the lower left image corner (optional).
|
|
|
|
* \lparam The X coordinate of the source rectangle (optional).
|
|
|
|
* \lparam The Y coordinate of the source rectangle (optional).
|
|
|
|
* \lparam The width of the source rectangle (optional).
|
|
|
|
* \lparam The height of the source rectangle (optional).
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
luaA_image_insert(lua_State *L)
|
|
|
|
{
|
|
|
|
image_t *image_target = luaL_checkudata(L, 1, "image");
|
|
|
|
image_t *image_source = luaL_checkudata(L, 2, "image");
|
|
|
|
int xoff = luaL_optnumber(L, 3, 0);
|
|
|
|
int yoff = luaL_optnumber(L, 4, 0);
|
|
|
|
|
|
|
|
int xsrc = luaL_optnumber(L, 5, 0);
|
|
|
|
int ysrc = luaL_optnumber(L, 6, 0);
|
2009-05-04 10:34:54 +02:00
|
|
|
int wsrc = luaL_optnumber(L, 7, image_getwidth(image_source));
|
|
|
|
int hsrc = luaL_optnumber(L, 8, image_getheight(image_source));
|
2009-05-27 15:14:47 +02:00
|
|
|
|
2009-05-04 10:34:54 +02:00
|
|
|
int hxoff = luaL_optnumber(L, 9, image_getwidth(image_source));
|
2009-05-27 15:14:47 +02:00
|
|
|
int hyoff = luaL_optnumber(L, 10, 0);
|
|
|
|
|
|
|
|
int vxoff = luaL_optnumber(L, 11, 0);
|
2009-05-04 10:34:54 +02:00
|
|
|
int vyoff = luaL_optnumber(L, 12, image_getheight(image_source));
|
2009-05-27 15:14:47 +02:00
|
|
|
|
|
|
|
imlib_context_set_image(image_target->image);
|
|
|
|
|
|
|
|
imlib_blend_image_onto_image_skewed(image_source->image, 0,
|
|
|
|
/* source rectangle */
|
|
|
|
xsrc, ysrc, wsrc, hsrc,
|
|
|
|
/* position of the source image in the target image */
|
|
|
|
xoff, yoff,
|
|
|
|
/* axis offsets for the source image, (w|0|0|h)
|
|
|
|
* is the default */
|
|
|
|
hxoff, hyoff, vxoff, vyoff);
|
|
|
|
|
2009-05-04 10:34:54 +02:00
|
|
|
image_target->isupdated = false;
|
2009-05-27 15:14:47 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-10-21 18:29:02 +02:00
|
|
|
/** Image object.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \return The number of elements pushed on stack.
|
|
|
|
* \luastack
|
|
|
|
* \lvalue An image
|
|
|
|
* \lfield width The image width.
|
|
|
|
* \lfield height The image height.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
luaA_image_index(lua_State *L)
|
|
|
|
{
|
|
|
|
if(luaA_usemetatable(L, 1, 2))
|
|
|
|
return 1;
|
|
|
|
|
2009-04-09 11:38:56 +02:00
|
|
|
image_t *image = luaL_checkudata(L, 1, "image");
|
2008-10-21 18:29:02 +02:00
|
|
|
size_t len;
|
|
|
|
const char *attr = luaL_checklstring(L, 2, &len);
|
|
|
|
|
|
|
|
switch(a_tokenize(attr, len))
|
|
|
|
{
|
|
|
|
case A_TK_WIDTH:
|
2009-05-04 10:34:54 +02:00
|
|
|
lua_pushnumber(L, image_getwidth(image));
|
2008-10-21 18:29:02 +02:00
|
|
|
break;
|
|
|
|
case A_TK_HEIGHT:
|
2009-05-04 10:34:54 +02:00
|
|
|
lua_pushnumber(L, image_getheight(image));
|
2008-10-21 18:29:02 +02:00
|
|
|
break;
|
2008-12-18 15:18:47 +01:00
|
|
|
case A_TK_ALPHA:
|
2009-04-09 11:38:56 +02:00
|
|
|
imlib_context_set_image(image->image);
|
2008-12-18 15:18:47 +01:00
|
|
|
lua_pushboolean(L, imlib_image_has_alpha());
|
|
|
|
break;
|
2008-10-21 18:29:02 +02:00
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-09-11 14:39:47 +02:00
|
|
|
const struct luaL_reg awesome_image_methods[] =
|
|
|
|
{
|
|
|
|
{ "__call", luaA_image_new },
|
2008-10-22 20:57:27 +02:00
|
|
|
{ "argb32", luaA_image_argb32_new },
|
2008-09-11 14:39:47 +02:00
|
|
|
{ NULL, NULL }
|
|
|
|
};
|
|
|
|
const struct luaL_reg awesome_image_meta[] =
|
|
|
|
{
|
2008-10-21 18:29:02 +02:00
|
|
|
{ "__index", luaA_image_index },
|
2008-09-17 17:47:28 +02:00
|
|
|
{ "rotate", luaA_image_rotate },
|
2008-12-16 16:45:24 +01:00
|
|
|
{ "orientate", luaA_image_orientate },
|
2008-09-17 17:47:28 +02:00
|
|
|
{ "crop", luaA_image_crop },
|
2008-10-14 17:37:39 +02:00
|
|
|
{ "crop_and_scale", luaA_image_crop_and_scale },
|
2008-12-29 15:19:01 +01:00
|
|
|
{ "save", luaA_image_save },
|
2009-05-27 15:14:47 +02:00
|
|
|
{ "insert", luaA_image_insert },
|
2009-04-16 03:05:16 +02:00
|
|
|
/* draw on images, whee! */
|
|
|
|
{ "draw_pixel", luaA_image_draw_pixel },
|
|
|
|
{ "draw_line", luaA_image_draw_line },
|
|
|
|
{ "draw_rectangle", luaA_image_draw_rectangle },
|
2009-05-04 16:36:59 +02:00
|
|
|
{ "draw_rectangle_gradient", luaA_image_draw_rectangle_gradient },
|
2009-04-16 03:05:16 +02:00
|
|
|
{ "draw_circle", luaA_image_draw_circle },
|
2008-09-11 14:39:47 +02:00
|
|
|
{ "__gc", luaA_image_gc },
|
|
|
|
{ "__tostring", luaA_image_tostring },
|
|
|
|
{ NULL, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80
|