void exif_auto_orientate(const fileinfo_t *file) { switch (exif_orientation(file)) { case 5: imlib_image_orientate(1); case 2: imlib_image_flip_vertical(); break; case 3: imlib_image_orientate(2); break; case 7: imlib_image_orientate(1); case 4: imlib_image_flip_horizontal(); break; case 6: imlib_image_orientate(1); break; case 8: imlib_image_orientate(3); break; } }
void feh_edit_inplace(winwidget w, int op) { int tmp; Imlib_Image old = NULL; Imlib_Load_Error err = IMLIB_LOAD_ERROR_NONE; if (!w->file || !w->file->data || !FEH_FILE(w->file->data)->filename) return; if (!strcmp(gib_imlib_image_format(w->im), "jpeg") && !path_is_url(FEH_FILE(w->file->data)->filename)) { feh_edit_inplace_lossless(w, op); feh_reload_image(w, 1, 1); return; } old = imlib_load_image_with_error_return(FEH_FILE(w->file->data)->filename, &err); if ((old != NULL) && (err == IMLIB_LOAD_ERROR_NONE)) { imlib_context_set_image(old); if (op == INPLACE_EDIT_FLIP) imlib_image_flip_vertical(); else if (op == INPLACE_EDIT_MIRROR) imlib_image_flip_horizontal(); else imlib_image_orientate(op); gib_imlib_save_image_with_error_return(old, FEH_FILE(w->file->data)->filename, &err); gib_imlib_free_image(old); if (err) feh_imlib_print_load_error(FEH_FILE(w->file->data)->filename, w, err); feh_reload_image(w, 1, 1); } else { /* * Image was opened using curl/magick or has been deleted after * opening it */ imlib_context_set_image(w->im); if (op == INPLACE_EDIT_FLIP) imlib_image_flip_vertical(); else if (op == INPLACE_EDIT_MIRROR) imlib_image_flip_horizontal(); else { imlib_image_orientate(op); tmp = w->im_w; FEH_FILE(w->file->data)->info->width = w->im_w = w->im_h; FEH_FILE(w->file->data)->info->height = w->im_h = tmp; } im_weprintf(w, "unable to edit in place. Changes have not been saved."); winwidget_render_image(w, 1, 0); } return; }
void exif_auto_orientate(const fileinfo_t *file) { ExifData *ed; ExifEntry *entry; int byte_order, orientation; if ((ed = exif_data_new_from_file(file->path)) == NULL) return; entry = exif_content_get_entry(ed->ifd[EXIF_IFD_0], EXIF_TAG_ORIENTATION); if (entry != NULL) { byte_order = exif_data_get_byte_order(ed); orientation = exif_get_short(entry->data, byte_order); } exif_data_unref(ed); if (entry == NULL) return; switch (orientation) { case 5: imlib_image_orientate(1); case 2: imlib_image_flip_vertical(); break; case 3: imlib_image_orientate(2); break; case 7: imlib_image_orientate(1); case 4: imlib_image_flip_horizontal(); break; case 6: imlib_image_orientate(1); break; case 8: imlib_image_orientate(3); break; } }
static inline PyObject * ImageObject_orientate(PyObject* self, PyObject *args) { int orientate; if (!PyArg_ParseTuple(args, "i:orientate", &orientate)){ return NULL; } imlib_context_set_image(((ImageObject *)self)->image); imlib_image_orientate(orientate); Py_RETURN_NONE; }
void img_rotate(img_t *img, int d) { win_t *win; int ox, oy, tmp; if (img == NULL || img->im == NULL || img->win == NULL) return; win = img->win; ox = d == 1 ? img->x : win->w - img->x - img->w * img->zoom; oy = d == 3 ? img->y : win->h - img->y - img->h * img->zoom; imlib_context_set_image(img->im); imlib_image_orientate(d); img->x = oy + (win->w - win->h) / 2; img->y = ox + (win->h - win->w) / 2; tmp = img->w; img->w = img->h; img->h = tmp; img->checkpan = true; img->dirty = true; }
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; }
/* draw background to root */ void drawbg(void) { int i, w, h, nx, ny, nh, nw, tmp; double factor; Pixmap pm; Imlib_Image tmpimg, buffer; pm = XCreatePixmap(dpy, root, sw, sh, DefaultDepth(dpy, DefaultScreen(dpy))); if(!(buffer = imlib_create_image(sw, sh))) die("Error: Cannot allocate buffer.\n"); imlib_context_set_image(buffer); imlib_image_fill_rectangle(0, 0, sw, sh); imlib_context_set_blend(1); for(i = 0; i < nmonitor; i++) { imlib_context_set_image(images[i % nimage]); w = imlib_image_get_width(); h = imlib_image_get_height(); if(!(tmpimg = imlib_clone_image())) die("Error: Cannot clone image.\n"); imlib_context_set_image(tmpimg); if(rotate && ((monitors[i].w > monitors[i].h && w < h) || (monitors[i].w < monitors[i].h && w > h))) { imlib_image_orientate(1); tmp = w; w = h; h = tmp; } imlib_context_set_image(buffer); switch(mode) { case ModeCenter: nw = (monitors[i].w - w) / 2; nh = (monitors[i].h - h) / 2; nx = monitors[i].x + (monitors[i].w - nw) / 2; ny = monitors[i].y + (monitors[i].h - nh) / 2; break; case ModeZoom: nw = monitors[i].w; nh = monitors[i].h; if(w > h && (w / h > (monitors[i].w / monitors[i].h))) { nx = monitors[i].x + (monitors[i].w - nw) / 2; ny = monitors[i].y + (int)ceil(h * nx / w) / 2; } else { ny = monitors[i].y + (monitors[i].h - nh) / 2; nx = monitors[i].x + (int)ceil(w * ny / h) / 2; } break; default: /* ModeScale */ factor = MAX((double)w / monitors[i].w, (double)h / monitors[i].h); nw = w / factor; nh = h / factor; nx = monitors[i].x + (monitors[i].w - nw) / 2; ny = monitors[i].y + (monitors[i].h - nh) / 2; } imlib_blend_image_onto_image(tmpimg, 0, 0, 0, w, h, nx, ny, nw, nh); imlib_context_set_image(tmpimg); imlib_free_image(); } imlib_context_set_blend(0); imlib_context_set_image(buffer); imlib_context_set_drawable(root); imlib_render_image_on_drawable(0, 0); imlib_context_set_drawable(pm); imlib_render_image_on_drawable(0, 0); XSetWindowBackgroundPixmap(dpy, root, pm); imlib_context_set_image(buffer); imlib_free_image_and_decache(); XFreePixmap(dpy, pm); }