static int save_lua( lua_State *L )
{
    img_t *img = (img_t*)luaL_checkudata( L, 1, MODULE_MT );
    const char *path = luaL_checkstring( L, 2 );
    ImlibLoadError err = IMLIB_LOAD_ERROR_NONE;
    Imlib_Image work = imlib_create_image_using_data( img->size.w, img->size.h,
                                                      img->blob );
    
    // set current image
    imlib_context_set_image( work );
    work = imlib_create_cropped_scaled_image( 0, 0, img->size.w, img->size.h, 
                                              img->resize.w, img->resize.h );
    imlib_free_image_and_decache();
    imlib_context_set_image( work );
    save2path( img, path, &err );
    // failed
    if( err ){
        liberr2errno( err );
        lua_pushstring( L, strerror(errno) );
        return 2;
    }
    // success
    else {
        lua_pushnil( L );
    }
    
    return 1;
}
Exemple #2
0
void image_to_pixmap(image *img, GdkPixmap *pm, int w, int h)
{
    int realw, realh;
    int need_free;

    imlib_context_set_image(img->image);
    realw = imlib_image_get_width();
    realh = imlib_image_get_height();
    if (w != realw || h != realh) {
	Imlib_Image newimg;

	newimg = imlib_create_cropped_scaled_image(0, 0, realw, realh, w, h);
	imlib_context_set_image(newimg);
	need_free = TRUE;
    } else
	need_free = FALSE;

    imlib_context_set_display(GDK_WINDOW_XDISPLAY(pm));
    imlib_context_set_visual(GDK_VISUAL_XVISUAL(gdk_visual_get_system()));
    imlib_context_set_colormap
	(GDK_COLORMAP_XCOLORMAP(gdk_colormap_get_system()));
    imlib_context_set_drawable(GDK_WINDOW_XWINDOW(pm));
    imlib_context_set_blend(1);
    imlib_render_image_on_drawable(0, 0);

    if (need_free)
	imlib_free_image();
}
Exemple #3
0
Imlib_Image gib_imlib_create_cropped_scaled_image(Imlib_Image im, int sx,
                                                  int sy, int sw, int sh,
                                                  int dw, int dh, char alias)
{
   imlib_context_set_image(im);
   imlib_context_set_anti_alias(alias);
   return imlib_create_cropped_scaled_image(sx, sy, sw, sh, dw, dh);
}
static int save_trim_lua( lua_State *L )
{
    img_t *img = (img_t*)luaL_checkudata( L, 1, MODULE_MT );
    const char *path = luaL_checkstring( L, 2 );
    img_bounds_t bounds = (img_bounds_t){ 0, 0, 0, 0 };
    double aspect_org = 0;
    double aspect = 0;
    ImlibLoadError err = IMLIB_LOAD_ERROR_NONE;
    Imlib_Image work = NULL;
    
    // calculate bounds of image with maintaining aspect ratio.
    aspect_org = (double)img->size.w/(double)img->size.h;
    aspect = (double)img->resize.w/(double)img->resize.h;
    // based on width
    if( aspect_org > aspect ){
        bounds.w = img->resize.w;
        bounds.h = (int)((double)bounds.w / aspect_org);
    }
    // based on height
    else if( aspect_org < aspect ){
        bounds.h = img->resize.h;
        bounds.w = (int)((double)bounds.h * aspect_org);
    }
    // square
    else {
        bounds.w = img->resize.w;
        bounds.h = img->resize.h;
    }
    
    // create image
    work = imlib_create_image_using_data( img->size.w, img->size.h, img->blob );
    // set current image
    imlib_context_set_image( work );
    work = imlib_create_cropped_scaled_image( 0, 0, img->size.w, img->size.h, 
                                              bounds.w, bounds.h );
    imlib_free_image_and_decache();
    imlib_context_set_image( work );
    save2path( img, path, &err );
    
    // failed
    if( err ){
        liberr2errno( err );
        lua_pushstring( L, strerror(errno) );
    }
    // success
    else {
        lua_pushnil( L );
    }
    
    return 1;
}
Exemple #5
0
struct R_image *
image_clone_imlib2(Drawable drawable, struct R_image *image, int w, int h)
{
    struct R_image *newimage;
    Imlib_Image img, newimg;
    int imagew, imageh;
    int neww, newh;

    if (image == NULL) {

	return NULL;
    }

    newimage = NULL;
    img = image->im2img;
    if (img) {
	imlib_context_set_drawable(drawable);
	imlib_context_set_image(img);
	imagew = imlib_image_get_width();
	imageh = imlib_image_get_height();
	if (w == 0) {
	    neww = imagew;
	} else {
	    neww = w;
	}
	if (h == 0) {
	    newh = imageh;
	} else {
	    newh = h;
	}
	newimg = imlib_create_cropped_scaled_image(0, 0,
						   imagew, imageh,
						   neww, newh);
	if (newimg == NULL) {

	    return NULL;
	}
	newimage = image_alloc();
	if (newimage == NULL) {

	    return NULL;
	}
	newimage->w = neww;
	newimage->h = newh;
	newimage->im2img = newimg;
    }

    return newimage;
}
Exemple #6
0
static inline PyObject * 
ImageObject_scaled_image(PyObject* self, PyObject *args)
{
    int x = 0, y = 0, w = 0, h = 0, dw = 0, dh = 0;
    if (!PyArg_ParseTuple(args, "ii:cropped_scaled_image", &dw, &dh)){
        return NULL;
    }
    imlib_context_set_image(((ImageObject *)self)->image);
    w = imlib_image_get_width();
    h = imlib_image_get_height();
    Imlib_Image image = imlib_create_cropped_scaled_image(x, y, w, h, dw, dh);
    if(!image){
        return NULL;
    }
    return (PyObject *)ImageObject_New(image);
}
Exemple #7
0
// Called from backend functions.
gboolean reload_icon(Execp *execp)
{
	char *icon_path = execp->backend->icon_path;

	if (execp->backend->has_icon && icon_path) {
		if (execp->backend->icon) {
			imlib_context_set_image(execp->backend->icon);
			imlib_free_image();
		}
		execp->backend->icon = load_image(icon_path, execp->backend->cache_icon);
		if (execp->backend->icon) {
			imlib_context_set_image(execp->backend->icon);
			int w = imlib_image_get_width();
			int h = imlib_image_get_height();
			if (w && h) {
				if (execp->backend->icon_w) {
					if (!execp->backend->icon_h) {
						h = (int)(0.5 + h * execp->backend->icon_w / (float)(w));
						w = execp->backend->icon_w;
					} else {
						w = execp->backend->icon_w;
						h = execp->backend->icon_h;
					}
				} else {
					if (execp->backend->icon_h) {
						w = (int)(0.5 + w * execp->backend->icon_h / (float)(h));
						h = execp->backend->icon_h;
					}
				}
				if (w < 1)
					w = 1;
				if (h < 1)
					h = 1;
			}
			if (w != imlib_image_get_width() || h != imlib_image_get_height()) {
				Imlib_Image icon_scaled =
				    imlib_create_cropped_scaled_image(0, 0, imlib_image_get_width(), imlib_image_get_height(), w, h);
				imlib_context_set_image(execp->backend->icon);
				imlib_free_image();
				execp->backend->icon = icon_scaled;
			}
			return TRUE;
		}
	}
	return FALSE;
}
Exemple #8
0
Imlib_Image scale_icon(Imlib_Image original, int icon_size)
{
	Imlib_Image icon_scaled;
	if (original) {
		imlib_context_set_image (original);
		icon_scaled = imlib_create_cropped_scaled_image(0, 0, imlib_image_get_width(), imlib_image_get_height(), icon_size, icon_size);
		imlib_context_set_image (icon_scaled);
		imlib_image_set_has_alpha(1);
		DATA32* data = imlib_image_get_data();
		adjust_asb(data, icon_size, icon_size, launcher_alpha, (float)launcher_saturation/100, (float)launcher_brightness/100);
		imlib_image_put_back_data(data);
	} else {
		icon_scaled = imlib_create_image(icon_size, icon_size);
		imlib_context_set_image (icon_scaled);
		imlib_context_set_color(255, 255, 255, 255);
		imlib_image_fill_rectangle(0, 0, icon_size, icon_size);
	}
	return icon_scaled;
}
Exemple #9
0
static Imlib_Image regenerate_from_original(const char *filename, int size) {
	/* load image */

	Imlib_Load_Error err;
	Imlib_Image orig = imlib_load_image_with_error_return(filename, &err);

	if (orig == NULL) {
		const char *errmsg = imlib_load_error_string(err);
		warnx("couldn't load %s for thumbnailing: %s", filename, errmsg);
		return NULL; /* XXX do what with error message? extra param? */
	}

	imlib_context_set_image(orig);

	struct coord image_dim = COORD(
		imlib_image_get_width(),
		imlib_image_get_height()
	);

	/* make thumbnail, but without upscaling smaller images. */

	struct coord thumb_dim = coord_downscale_to_fit(image_dim, COORD(size, size));

	Imlib_Image thumb = imlib_create_cropped_scaled_image(0, 0,
		image_dim.width, image_dim.height,
		thumb_dim.width, thumb_dim.height
	);

	/* can free original image now */

	imlib_context_set_image(orig);
	imlib_free_image();

	/* done */

	if (thumb == NULL) {
		warnx("error while downscaling %s for thumbnailing", filename);
		return NULL;
	}

	return thumb;
}
Exemple #10
0
static inline PyObject * 
ImageObject_cropped_scaled_image(PyObject* self, PyObject *args)
{
    int x, y, w, h, dw, dh;
    if (!PyArg_ParseTuple(args, "iiiiii:cropped_scaled_image", &x, &y, &w, &h, &dw, &dh)){
        return NULL;
    }
    imlib_context_set_image(((ImageObject *)self)->image);
    int maxw = imlib_image_get_width();
    if(maxw < w){
        w = maxw;
    }
    int maxh = imlib_image_get_height();
    if(maxh < h){
        h = maxh;
    }
    Imlib_Image image = imlib_create_cropped_scaled_image(x, y, w, h, dw, dh);
    if(!image){
        return NULL;
    }
    return (PyObject *)ImageObject_New(image);
}
Exemple #11
0
void XImlib2Image::createPicture()
{
	DesktopIconConfig * dIconConfig =
			dynamic_cast<DesktopIconConfig *>(iconConfig);
	
	image = imlib_load_image(dIconConfig->getPictureFilename().c_str());

	if (image)
	{
		imlib_context_set_image(image);

		orgWidth = imlib_image_get_width();
		orgHeight = imlib_image_get_height();

		if (width == 0)
			width = imlib_image_get_width();
		if (height == 0)
			height= imlib_image_get_height();
		
		Imlib_Image tempImg = imlib_create_cropped_scaled_image(0, 0,
				orgWidth, orgHeight, width, height);

		imlib_free_image();

		image = tempImg;
        
		hasAlpha = true; //TODO add checks here
	}    
	else
	{
		cout << "Can't load: " << dIconConfig->getPictureFilename()
				<< " bailing -- "
				<< dIconConfig->getCaption() << endl
				<< "Check to see if the image and path to image is valid\n";
	}
	
}
Exemple #12
0
void Resources::reloadWallpaper()
{
    if (!_wallpaper)
        delete _wallpaper;

    Window rootWindow = XTools::rootWindow();
    XWindowAttributes attrs;
    XGetWindowAttributes(_dpy, rootWindow, &attrs);
    int width = attrs.width;
    int height = attrs.height;

    _wallpaper = new Image(_dpy, width, height, attrs.depth);

    printf("Loading background from '%s'\n", Settings::instance()->backgroundFilename());
    printf("Background mode: %d\n", Settings::instance()->backgroundMode());

    imlib_context_set_display(_dpy);

    int bgXpos = 0;
    int bgYpos = 0;
    Imlib_Image background = imlib_load_image(Settings::instance()->backgroundFilename());
    if (background == 0)
    {
        printf("Cannot load background\n");
        printf("rgb = (%d, %d, %d)\n",
            _backgroundColor.red   / 256,
            _backgroundColor.green / 256,
            _backgroundColor.blue  / 256
        );
        background = imlib_create_image(width, height);
        imlib_context_set_image(background);
        imlib_context_set_color(
            _backgroundColor.red   / 256,
            _backgroundColor.green / 256,
            _backgroundColor.blue  / 256,
            255
        );
        imlib_image_fill_rectangle(0, 0, width, height);
    }
    else
    {
        Imlib_Image bgToDraw = 0;

        imlib_context_set_image(background);
        switch (Settings::instance()->backgroundMode())
        {
            case Settings::Stretched:
            {
                bgToDraw = imlib_create_cropped_scaled_image(
                    0, 0,
                    imlib_image_get_width(), imlib_image_get_height(),
                    width, height
                );
                break;
            }

            case Settings::Centered:
            {
                // Hildon's logic is not so easy in this case

//                bgToDraw = imlib_create_cropped_image(
//                    (imlib_image_get_width() - width) / 2,
//                    (imlib_image_get_height()- height) / 2,
//                    width, height
//                );

                double scalex = 1.0;
                double scaley = 1.0;

                if (imlib_image_get_width() > width * 2)
                    scalex = (double)imlib_image_get_width() / 2.0 / width;
                if (imlib_image_get_height() > height * 2)
                    scaley = (double)imlib_image_get_height() / 2.0 / height;

                double scale = scalex;
                if (scaley > scale)
                    scale = scaley;

                int newwidth = (int)(scale * width);
                int newheight= (int)(scale * height);

                bgToDraw = imlib_create_cropped_scaled_image(
                    (imlib_image_get_width() - newwidth) / 2,
                    (imlib_image_get_height()- newheight)/ 2,
                    newwidth, newheight,
                    width, height
                );

                break;
            }

            case Settings::Scaled: case Settings::Cropped:
            {
                double scalex = (double)width / imlib_image_get_width();
                double scaley = (double)height / imlib_image_get_height();
                double scale = scalex;

                if (Settings::instance()->backgroundMode() == Settings::Scaled)
                {
                    if (scaley < scale) scale = scaley;
                }
                else
                {
                    if (scaley > scale) scale = scaley;
                }

                int newwidth = (int)(imlib_image_get_width() * scale);
                int newheight= (int)(imlib_image_get_height() * scale);

                bgToDraw = imlib_create_cropped_scaled_image(
                    0, 0,
                    imlib_image_get_width(), imlib_image_get_height(),
                    newwidth, newheight
                );

                bgXpos = (width - newwidth) / 2;
                bgYpos = (height - newheight) / 2;
                break;
            }

            default:
                printf("Unknown background mode: %d\n", Settings::instance()->backgroundMode());
        }

        imlib_free_image();

        imlib_context_set_image(bgToDraw);
    }

    imlib_context_set_drawable(_wallpaper->pixmap());
    imlib_context_set_visual(DefaultVisual(_dpy, DefaultScreen(_dpy)));

    imlib_render_image_on_drawable(bgXpos, bgYpos);

    imlib_free_image();
}
ngx_int_t ngx_http_small_light_imlib2_process(ngx_http_request_t *r, ngx_http_small_light_ctx_t *ctx)
{
    ngx_http_small_light_imlib2_ctx_t *ictx;
    ngx_http_small_light_image_size_t sz;
    Imlib_Image image_org, image_dst, image_tmp;
    Imlib_Load_Error err;
    ngx_file_info_t fi;
    ngx_fd_t fd;
    char *filename, *sharpen, *blur, *of, *buf;
    void *data;
    int w, h, radius, orientation;
    double iw, ih, q;
    ngx_int_t type;
    const char *ext;
    ssize_t size;

    ictx = (ngx_http_small_light_imlib2_ctx_t *)ctx->ictx;

    filename = (char *)ictx->tf->file.name.data;

    /* adjust image size */
    ngx_http_small_light_calc_image_size(r, ctx, &sz, 10000.0, 10000.0);

    if (sz.jpeghint_flg != 0) {
        if (ngx_http_small_light_load_jpeg((void**)&data, &w, &h, r, filename, sz.dw, sz.dh) != NGX_OK) {
            image_org = imlib_load_image_immediately_without_cache(filename);
            if (image_org == NULL) {
                ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
                              "failed to load image %s:%d",
                              __FUNCTION__,
                              __LINE__);
                return NGX_ERROR;
            }
        } else {
            image_org = imlib_create_image_using_data(w, h, data);
        }
    } else {
        image_org = imlib_load_image_immediately_without_cache(filename);
        if (image_org == NULL) {
            ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
                          "failed to load image %s:%d",
                          __FUNCTION__,
                          __LINE__);
            return NGX_ERROR;
        }
    }

    /* rotate. */
    if (sz.angle) {
        orientation = 0;
        switch (sz.angle) {
        case 90:
            orientation = 1;
            break;
        case 180:
            orientation = 2;
            break;
        case 270:
            orientation = 3;
            break;
        default:
            ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
                          "image not rotated. 'angle'(%d) must be 90 or 180 or 270. %s:%d",
                          sz.angle,
                          __FUNCTION__,
                          __LINE__);
            break;
        }

        imlib_context_set_image(image_org);
        imlib_image_orientate(orientation);
    }

    /* calc size. */
    imlib_context_set_image(image_org);
    iw = (double)imlib_image_get_width();
    ih = (double)imlib_image_get_height();
    ngx_http_small_light_calc_image_size(r, ctx, &sz, iw, ih);

    /* pass through. */
    if (sz.pt_flg != 0) {
        ctx->of = ctx->inf;
        return NGX_OK;
    }

    /* crop, scale. */
    if (sz.scale_flg != 0) {
        image_dst = imlib_create_cropped_scaled_image((int)sz.sx, (int)sz.sy, (int)sz.sw, (int)sz.sh, (int)sz.dw, (int)sz.dh);
        imlib_context_set_image(image_org);
        imlib_free_image();
    } else {
        image_dst = image_org;
    }

    if (image_dst == NULL) {
        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
                      "imlib_create_cropped_scaled_image failed. %s:%d",
                      __FUNCTION__,
                      __LINE__);
        return NGX_ERROR;
    }

    /* create canvas then draw image to the canvas. */
    if (sz.cw > 0.0 && sz.ch > 0.0) {
        image_tmp = imlib_create_image(sz.cw, sz.ch);
        if (image_tmp == NULL) {
            imlib_context_set_image(image_dst);
            imlib_free_image();
            return NGX_ERROR;
        }
        imlib_context_set_image(image_tmp);
        imlib_context_set_color(sz.cc.r, sz.cc.g, sz.cc.b, sz.cc.a);
        imlib_image_fill_rectangle(0, 0, sz.cw, sz.ch);
        imlib_blend_image_onto_image(image_dst, 255, 0, 0,
                                     (int)sz.dw, (int)sz.dh, (int)sz.dx, (int)sz.dy, (int)sz.dw, (int)sz.dh);
        imlib_context_set_image(image_dst);
        imlib_free_image();
        image_dst = image_tmp;
    }

    /* effects. */
    sharpen = NGX_HTTP_SMALL_LIGHT_PARAM_GET_LIT(&ctx->hash, "sharpen");
    if (sharpen) {
        radius = ngx_http_small_light_parse_int(sharpen);
        if (radius > 0) {
            imlib_context_set_image(image_dst);
            imlib_image_sharpen(radius);
        }
    }

    blur = NGX_HTTP_SMALL_LIGHT_PARAM_GET_LIT(&ctx->hash, "blur");
    if (blur) {
        radius = ngx_http_small_light_parse_int(blur);
        if (radius > 0) {
            imlib_context_set_image(image_dst);
            imlib_image_blur(radius);
        }
    }

    /* border. */
    if (sz.bw > 0.0 || sz.bh > 0.0) {
        imlib_context_set_color(sz.bc.r, sz.bc.g, sz.bc.b, sz.bc.a);
        imlib_context_set_image(image_dst);
        if (sz.cw > 0.0 && sz.ch > 0.0) {
            imlib_image_fill_rectangle(0, 0, sz.cw, sz.bh);
            imlib_image_fill_rectangle(0, 0, sz.bw, sz.ch);
            imlib_image_fill_rectangle(0, sz.ch - sz.bh, sz.cw, sz.bh);
            imlib_image_fill_rectangle(sz.cw - sz.bw, 0, sz.bw, sz.ch);
        } else {
            imlib_image_fill_rectangle(0, 0, sz.dw, sz.bh);
            imlib_image_fill_rectangle(0, 0, sz.bw, sz.ch);
            imlib_image_fill_rectangle(0, sz.dh - sz.bh, sz.dw, sz.bh);
            imlib_image_fill_rectangle(sz.dw - sz.bw, 0, sz.bw, sz.dh);
        }
    }

    /* set params. */
    imlib_context_set_image(image_dst);
    q = ngx_http_small_light_parse_double(NGX_HTTP_SMALL_LIGHT_PARAM_GET_LIT(&ctx->hash, "q"));
    if (q > 0.0) {
        imlib_image_attach_data_value("quality", NULL, q, NULL);
    }

    of = NGX_HTTP_SMALL_LIGHT_PARAM_GET_LIT(&ctx->hash, "of");
    if (ngx_strlen(of) > 0) {
        type = ngx_http_small_light_type(of);
        if (type == NGX_HTTP_SMALL_LIGHT_IMAGE_NONE) {
            ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
                          "of is invalid(%s) %s:%d",
                          of,
                          __FUNCTION__,
                          __LINE__);
            of = (char *)ngx_http_small_light_image_exts[ictx->type - 1];
        } else if (type == NGX_HTTP_SMALL_LIGHT_IMAGE_WEBP) {
            ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
                          "WebP is not supported %s:%d",
                          __FUNCTION__,
                          __LINE__);
            of = (char *)ngx_http_small_light_image_exts[ictx->type - 1];
        } else {
            ictx->type = type;
        }
        imlib_image_set_format(of);
        ctx->of = ngx_http_small_light_image_types[ictx->type - 1];
    } else {
        ext = ngx_http_small_light_image_exts[ictx->type - 1];
        imlib_image_set_format(ext);
        ctx->of = ctx->inf;
    }

    /* save image. */
    imlib_save_image_with_error_return(filename, &err);
    imlib_free_image();

    /* check error. */
    if (err != IMLIB_LOAD_ERROR_NONE) {
        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
                      "failed to imlib_save_error %s:%d",
                      __FUNCTION__,
                      __LINE__);
        return NGX_ERROR;
    }

    if (ngx_file_info(filename, &fi) == NGX_FILE_ERROR) {
        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
                      "failed to ngx_file_info %s:%d",
                      __FUNCTION__,
                      __LINE__);
        return NGX_ERROR;
    }

    fd = ngx_open_file(filename, NGX_FILE_RDONLY, NGX_FILE_OPEN, 0);
    if (fd == NGX_INVALID_FILE) {
        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
                      "failed to open fd %s:%d",
                      __FUNCTION__,
                      __LINE__);
        return NGX_ERROR;
    }

    if (ngx_fd_info(fd, &fi) == NGX_FILE_ERROR) {
        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
                      "failed to ngx_fd_info %s:%d",
                      __FUNCTION__,
                      __LINE__);
        ngx_close_file(fd);
        return NGX_ERROR;
    } 

    buf = ngx_palloc(r->pool, ngx_file_size(&fi));
    if (buf == NULL) {
        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
                      "failed to allocate memory from r->pool %s:%d",
                      __FUNCTION__,
                      __LINE__);
        ngx_close_file(fd);
        return NGX_ERROR;
    }
    size = ngx_read_fd(fd, buf, ngx_file_size(&fi));
    if (size == -1) {
        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
                      "failed to ngx_read_fd %s:%d",
                      __FUNCTION__,
                      __LINE__);
        ngx_close_file(fd);
        return NGX_ERROR;
    }

    if ((size_t)size > ctx->content_length) {
        ctx->content = ngx_palloc(r->pool, size);
        if (ctx->content == NULL) {
            ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
                          "failed to allocate memory from r->pool %s:%d",
                          __FUNCTION__,
                          __LINE__);
            ngx_close_file(fd);
            return NGX_ERROR;
        }
    }

    ngx_memcpy(ctx->content, buf, size);

    ngx_close_file(fd);

    ctx->content_length = size;

    return NGX_OK;
}
static int save_crop_lua( lua_State *L )
{
    img_t *img = (img_t*)luaL_checkudata( L, 1, MODULE_MT );
    const char *path = luaL_checkstring( L, 2 );
    uint8_t align = IMG_ALIGN_NONE;
    uint8_t halign = IMG_ALIGN_CENTER;
    uint8_t valign = IMG_ALIGN_MIDDLE;
    img_bounds_t bounds = (img_bounds_t){ 0, 0, 0, 0 };
    double aspect_org = 0;
    double aspect = 0;
    Imlib_Image work = NULL;
    ImlibLoadError err = IMLIB_LOAD_ERROR_NONE;
    
    // check alignment arguments
    // horizontal
    if( !lua_isnoneornil( L, 3 ) )
    {
        halign = (uint8_t)luaL_checkint( L, 3 );
        if( halign < IMG_ALIGN_LEFT || halign > IMG_ALIGN_RIGHT ){
            return luaL_argerror( L, 3, "horizontal align must be LEFT, RIGHT or CENTER" );
        }
    }
    // vertical
    if( !lua_isnoneornil( L, 4 ) )
    {
        valign = (uint8_t)luaL_checkinteger( L, 4 );
        if( valign < IMG_ALIGN_TOP || valign > IMG_ALIGN_BOTTOM ){
            return luaL_argerror( L, 4, "vertical align must be TOP, BOTTOM or MIDDLE" );
        }
    }
    
    // calculate bounds of cropped image by aspect ratio
    aspect_org = (double)img->size.w/(double)img->size.h;
    aspect = (double)img->resize.w/(double)img->resize.h;
    // based on height
    if( aspect_org > aspect ){
        bounds.h = img->size.h;
        bounds.w = (int)((double)img->size.h * aspect);
        align = (uint8_t)halign;
    }
    // based on width
    else if( aspect_org < aspect ){
        bounds.w = img->size.w;
        bounds.h = (int)((double)img->size.w / aspect);
        align = (uint8_t)valign;
    }
    // square
    else {
        bounds.w = img->size.w;
        bounds.h = img->size.h;
    }
    // calculate bounds position
    BOUNDS_ALIGN( bounds, align, img->size );
    
    // create image
    work = imlib_create_image_using_data( img->size.w, img->size.h, img->blob );
    imlib_context_set_image( work );
    work = imlib_create_cropped_scaled_image( bounds.x, bounds.y, bounds.w, 
                                              bounds.h, img->resize.w, 
                                              img->resize.h );
    imlib_free_image_and_decache();
    imlib_context_set_image( work );
    save2path( img, path, &err );
    
    // failed
    if( err ){
        liberr2errno( err );
        lua_pushstring( L, strerror(errno) );
    }
    // success
    else {
        lua_pushnil( L );
    }
    
    return 1;
}
Exemple #15
0
void get_icon (Task *tsk)
{
	Panel *panel = tsk->area.panel;
	if (!panel->g_task.icon) return;
	size_t i;
	Imlib_Image img = NULL;
	XWMHints *hints = 0;
	gulong *data = 0;

	int k;
	for (k=0; k<TASK_STATE_COUNT; ++k) {
		if (tsk->icon[k]) {
			imlib_context_set_image(tsk->icon[k]);
			imlib_free_image();
			tsk->icon[k] = 0;
		}
	}

	data = server_get_property (tsk->win, server.atom._NET_WM_ICON, XA_CARDINAL, (int*)&i);
	if (data) {
		// get ARGB icon
		int w, h;
		gulong *tmp_data;

		tmp_data = get_best_icon (data, get_icon_count (data, i), i, &w, &h, panel->g_task.icon_size1);
#ifdef __x86_64__
		DATA32 icon_data[w * h];
		size_t length = w * h;
		for (i = 0; i < length; ++i)
			icon_data[i] =  tmp_data[i];
		img = imlib_create_image_using_copied_data (w, h, icon_data);
#else
		img = imlib_create_image_using_data (w, h, (DATA32*)tmp_data);
#endif
	}
	else {
		// get Pixmap icon
		hints = XGetWMHints(server.dsp, tsk->win);
		if (hints) {
			if (hints->flags & IconPixmapHint && hints->icon_pixmap != 0) {
				// get width, height and depth for the pixmap
				Window root;
				int  icon_x, icon_y;
				uint32_t border_width, bpp;
				uint32_t w, h;

				//printf("  get pixmap\n");
				XGetGeometry(server.dsp, hints->icon_pixmap, &root, &icon_x, &icon_y, &w, &h, &border_width, &bpp);
				imlib_context_set_drawable(hints->icon_pixmap);
				img = imlib_create_image_from_drawable(hints->icon_mask, 0, 0, w, h, 0);
			}
		}
	}
	if (img == NULL) {
		imlib_context_set_image(default_icon);
		img = imlib_clone_image();
	}

	// transform icons
	imlib_context_set_image(img);
	imlib_image_set_has_alpha(1);
	int w, h;
	w = imlib_image_get_width();
	h = imlib_image_get_height();
	Imlib_Image orig_image = imlib_create_cropped_scaled_image(0, 0, w, h, panel->g_task.icon_size1, panel->g_task.icon_size1);
	imlib_free_image();

	imlib_context_set_image(orig_image);
	tsk->icon_width = imlib_image_get_width();
	tsk->icon_height = imlib_image_get_height();
	for (k=0; k<TASK_STATE_COUNT; ++k) {
		imlib_context_set_image(orig_image);
		tsk->icon[k] = imlib_clone_image();
		imlib_context_set_image(tsk->icon[k]);
		DATA32 *data32;
		if (panel->g_task.alpha[k] != 100 || panel->g_task.saturation[k] != 0 || panel->g_task.brightness[k] != 0) {
			data32 = imlib_image_get_data();
			adjust_asb(data32, tsk->icon_width, tsk->icon_height, panel->g_task.alpha[k], (float)panel->g_task.saturation[k]/100, (float)panel->g_task.brightness[k]/100);
			imlib_image_put_back_data(data32);
		}
	}
	imlib_context_set_image(orig_image);
	imlib_free_image();

	if (hints)
		XFree(hints);
	if (data)
		XFree (data);

	GPtrArray* task_group = task_get_tasks(tsk->win);
	if (task_group) {
		for (i=0; i<task_group->len; ++i) {
			Task* tsk2 = g_ptr_array_index(task_group, i);
			tsk2->icon_width = tsk->icon_width;
			tsk2->icon_height = tsk->icon_height;
			int k;
			for (k=0; k<TASK_STATE_COUNT; ++k)
				tsk2->icon[k] = tsk->icon[k];
			set_task_redraw(tsk2);
		}
	}
}