Imlib_Image create_transparent_image(Imlib_Image w_image, Imlib_Image b_image) { int w, h; DATA32 *dst_data, *src_data; imlib_context_set_image(w_image); dst_data = imlib_image_get_data(); imlib_context_set_image(b_image); src_data = imlib_image_get_data(); h = gib_imlib_image_get_height(w_image); w = gib_imlib_image_get_width(w_image); unsigned long i; for(i=0; i<w*h; i++) if(dst_data[i] != src_data[i]) { DATA32 alpha; alpha = (src_data[i] & 0xff) - (dst_data[i] & 0xff); alpha = (alpha << 24) & 0xff000000; dst_data[i] = (src_data[i] & 0xffffff) | alpha; } Imlib_Image ret_img; ret_img = imlib_create_image_using_data(w, h, dst_data); imlib_context_set_image(ret_img); imlib_image_set_has_alpha(1); return ret_img; }
ExcCode screen_cursor_blend(int x, int y, Imlib_Image image) { xcb_xfixes_get_cursor_image_cookie_t cookie = xcb_xfixes_get_cursor_image(display); xcb_xfixes_get_cursor_image_reply_t *reply = xcb_xfixes_get_cursor_image_reply(display, cookie, NULL); if (reply == NULL) return 0; unsigned *cursor_data = xcb_xfixes_get_cursor_image_cursor_image(reply); if (cursor_data == NULL) return 0; Imlib_Image cursor = imlib_create_image_using_data( reply->width, reply->height, cursor_data); if (cursor == NULL) PANIC(ERR_IMAGE); imlib_context_set_image(cursor); imlib_image_set_has_alpha(1); imlib_context_set_image(image); imlib_blend_image_onto_image(cursor, 0, 0, 0, reply->width, reply->height, reply->x - reply->xhot - x, reply->y - reply->yhot - y, reply->width, reply->height); imlib_context_set_image(cursor); imlib_free_image_and_decache(); free(reply); return 0; }
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; }
static void save_image(char *filename) { Imlib_Image img; int width = image.width; int height = image.height; DATA32 *data; char *suffix; if (filename == NULL) { return; } suffix = strrchr(filename, '.'); if (suffix) { data = (DATA32 *)image.pixels; img = imlib_create_image_using_data(width, height, data); if (img) { imlib_context_set_image(img); imlib_image_set_format(suffix + 1); imlib_save_image(filename); imlib_free_image(); } } else { fprintf(stderr, "wrong file name: %s\n", filename); exit(EXIT_FAILURE); } }
static void *temp_image(GB_IMG *img) { Imlib_Image image; if (!img->data) image = NULL; else { image = imlib_create_image_using_data(img->width, img->height, (DATA32 *)img->data); } return image; }
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; }
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; }
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); } } }