void feh_edit_inplace_lossless(winwidget w, int op) { char *filename = FEH_FILE(w->file->data)->filename; int len = strlen(filename) + 1; char *file_str = emalloc(len); int pid, status; int devnull = -1; char op_name[] = "rotate"; /* message */ char op_op[] = "-rotate"; /* jpegtran option */ char op_value[] = "horizontal"; /* jpegtran option's value */ if (op == INPLACE_EDIT_FLIP) { sprintf(op_name, "flip"); sprintf(op_op, "-flip"); sprintf(op_value, "vertical"); } else if (op == INPLACE_EDIT_MIRROR) { sprintf(op_name, "mirror"); sprintf(op_op, "-flip"); } else snprintf(op_value, 4, "%d", 90 * op); snprintf(file_str, len, "%s", filename); if ((pid = fork()) < 0) { im_weprintf(w, "lossless %s: fork failed:", op_name); exit(1); } else if (pid == 0) { execlp("jpegtran", "jpegtran", "-copy", "all", op_op, op_value, "-outfile", file_str, file_str, NULL); im_weprintf(w, "lossless %s: Is 'jpegtran' installed? Failed to exec:", op_name); exit(1); } else { waitpid(pid, &status, 0); if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { im_weprintf(w, "lossless %s: Got exitcode %d from jpegtran." " Commandline was: " "jpegtran -copy all %s %s -outfile %s %s", op_name, status >> 8, op_op, op_value, file_str, file_str); free(file_str); return; } }
void slideshow_save_image(winwidget win) { char *tmpname; Imlib_Load_Error err; if (win->file) { tmpname = feh_unique_filename("", FEH_FILE(win->file->data)->name); } else if (mode) { char *tmp; tmp = estrjoin(".", mode, "png", NULL); tmpname = feh_unique_filename("", tmp); free(tmp); } else { tmpname = feh_unique_filename("", "noname.png"); } if (opt.verbose) printf("saving image to filename '%s'\n", tmpname); /* XXX gib_imlib_save_image_with_error_return breaks with *.XXX and * similar because it tries to set the image format, which only works * with .xxx . * So we leave that part out. */ imlib_context_set_image(win->im); imlib_save_image_with_error_return(tmpname, &err); if (err) im_weprintf(win, "Can't save image %s:", tmpname); free(tmpname); return; }
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 feh_edit_inplace(winwidget w, int op) { int ret; Imlib_Image old; Imlib_Load_Error err; if (!w->file || !w->file->data || !FEH_FILE(w->file->data)->filename) return; if (!strcmp(gib_imlib_image_format(w->im), "jpeg")) { feh_edit_inplace_lossless(w, op); feh_reload_image(w, 1, 1); return; } ret = feh_load_image(&old, FEH_FILE(w->file->data)); if (ret) { if (op == INPLACE_EDIT_FLIP) { imlib_context_set_image(old); imlib_image_flip_vertical(); } else if (op == INPLACE_EDIT_MIRROR) { imlib_context_set_image(old); imlib_image_flip_horizontal(); } else gib_imlib_image_orientate(old, op); ungib_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 { im_weprintf(w, "failed to load image from disk to edit it in place"); } return; }
void feh_imlib_print_load_error(char *file, winwidget w, Imlib_Load_Error err) { if (err == IMLIB_LOAD_ERROR_OUT_OF_FILE_DESCRIPTORS) eprintf("%s - Out of file descriptors while loading", file); else if (!opt.quiet || w) { switch (err) { case IMLIB_LOAD_ERROR_FILE_DOES_NOT_EXIST: im_weprintf(w, "%s - File does not exist", file); break; case IMLIB_LOAD_ERROR_FILE_IS_DIRECTORY: im_weprintf(w, "%s - Directory specified for image filename", file); break; case IMLIB_LOAD_ERROR_PERMISSION_DENIED_TO_READ: im_weprintf(w, "%s - No read access", file); break; case IMLIB_LOAD_ERROR_UNKNOWN: case IMLIB_LOAD_ERROR_NO_LOADER_FOR_FILE_FORMAT: im_weprintf(w, "%s - No Imlib2 loader for that file format", file); break; case IMLIB_LOAD_ERROR_PATH_TOO_LONG: im_weprintf(w, "%s - Path specified is too long", file); break; case IMLIB_LOAD_ERROR_PATH_COMPONENT_NON_EXISTANT: im_weprintf(w, "%s - Path component does not exist", file); break; case IMLIB_LOAD_ERROR_PATH_COMPONENT_NOT_DIRECTORY: im_weprintf(w, "%s - Path component is not a directory", file); break; case IMLIB_LOAD_ERROR_PATH_POINTS_OUTSIDE_ADDRESS_SPACE: im_weprintf(w, "%s - Path points outside address space", file); break; case IMLIB_LOAD_ERROR_TOO_MANY_SYMBOLIC_LINKS: im_weprintf(w, "%s - Too many levels of symbolic links", file); break; case IMLIB_LOAD_ERROR_OUT_OF_MEMORY: im_weprintf(w, "While loading %s - Out of memory", file); break; case IMLIB_LOAD_ERROR_PERMISSION_DENIED_TO_WRITE: im_weprintf(w, "%s - Cannot write to directory", file); break; case IMLIB_LOAD_ERROR_OUT_OF_DISK_SPACE: im_weprintf(w, "%s - Cannot write - out of disk space", file); break; default: im_weprintf(w, "While loading %s - Unknown error (%d)", file, err); break; } } }
void feh_reload_image(winwidget w, int resize, int force_new) { char *title, *new_title; int len; Imlib_Image tmp; int old_w, old_h; unsigned char tmode =0; int tim_x =0; int tim_y =0; double tzoom =0; tmode = w->mode; tim_x = w->im_x; tim_y = w->im_y; tzoom = w->zoom; if (!w->file) { im_weprintf(w, "couldn't reload, this image has no file associated with it."); winwidget_render_image(w, 0, 0); return; } D(("resize %d, force_new %d\n", resize, force_new)); free(FEH_FILE(w->file->data)->caption); FEH_FILE(w->file->data)->caption = NULL; len = strlen(w->name) + sizeof("Reloading: ") + 1; new_title = emalloc(len); snprintf(new_title, len, "Reloading: %s", w->name); title = estrdup(w->name); winwidget_rename(w, new_title); old_w = gib_imlib_image_get_width(w->im); old_h = gib_imlib_image_get_height(w->im); /* * If we don't free the old image before loading the new one, Imlib2's * caching will get in our way. * However, if --reload is used (force_new == 0), we want to continue if * the new image cannot be loaded, so we must not free the old image yet. */ if (force_new) winwidget_free_image(w); if ((feh_load_image(&tmp, FEH_FILE(w->file->data))) == 0) { if (force_new) eprintf("failed to reload image\n"); else { im_weprintf(w, "Couldn't reload image. Is it still there?"); winwidget_render_image(w, 0, 0); } winwidget_rename(w, title); free(title); free(new_title); return; } if (!resize && ((old_w != gib_imlib_image_get_width(tmp)) || (old_h != gib_imlib_image_get_height(tmp)))) resize = 1; if (!force_new) winwidget_free_image(w); w->im = tmp; winwidget_reset_image(w); w->mode = MODE_NORMAL; if ((w->im_w != gib_imlib_image_get_width(w->im)) || (w->im_h != gib_imlib_image_get_height(w->im))) w->had_resize = 1; if (w->has_rotated) { Imlib_Image temp; temp = gib_imlib_create_rotated_image(w->im, 0.0); w->im_w = gib_imlib_image_get_width(temp); w->im_h = gib_imlib_image_get_height(temp); gib_imlib_free_image_and_decache(temp); } else { w->im_w = gib_imlib_image_get_width(w->im); w->im_h = gib_imlib_image_get_height(w->im); } if (opt.keep_zoom_vp) { /* put back in: */ w->mode = tmode; w->im_x = tim_x; w->im_y = tim_y; w->zoom = tzoom; winwidget_render_image(w, 0, 0); } else { winwidget_render_image(w, resize, 0); } winwidget_rename(w, title); free(title); free(new_title); return; }