croptool

Image cropping tool
git clone git://lumidify.org/croptool.git
Log | Files | Refs | README | LICENSE

commit 65b55cbf01f250c4e34040c29c3baeefc8a2b2ca
parent c5e6364a0fbd3aa4950cc203ceda8e9f7aad75d9
Author: lumidify <nobody@lumidify.org>
Date:   Sat,  2 Jan 2021 09:06:23 +0100

Add croptool_crop

Diffstat:
MMakefile | 14+++++++-------
Mcroptool.1 | 3+++
Mcroptool.c | 2+-
Acroptool_crop.1 | 34++++++++++++++++++++++++++++++++++
Acroptool_crop.c | 79+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 124 insertions(+), 8 deletions(-)

diff --git a/Makefile b/Makefile @@ -6,7 +6,7 @@ VERSION = 1.2-dev PREFIX = /usr/local MANPREFIX = ${PREFIX}/man -BIN = ${NAME} +BIN = ${NAME} croptool_crop SRC = ${BIN:=.c} MAN1 = ${BIN:=.1} @@ -15,20 +15,20 @@ LDFLAGS += `pkg-config --libs x11` `imlib2-config --libs` -lm all: ${BIN} -${BIN}: - ${CC} ${CFLAGS} ${LDFLAGS} -o $@ ${SRC} +.c: + ${CC} ${CFLAGS} ${LDFLAGS} -o $@ $< install: all mkdir -p "${DESTDIR}${PREFIX}/bin" cp -f ${BIN} "${DESTDIR}${PREFIX}/bin" - chmod 755 ${PREFIX}/bin/${BIN} + for f in ${BIN}; do chmod 755 "${DESTDIR}${PREFIX}/bin/$$f"; done mkdir -p "${DESTDIR}${MANPREFIX}/man1" cp -f ${MAN1} "${DESTDIR}${MANPREFIX}/man1" - chmod 644 "${DESTDIR}${MANPREFIX}/man1/${MAN1}" + for m in ${MAN1}; do chmod 644 "${DESTDIR}${MANPREFIX}/man1/$$m"; done uninstall: - rm -f "${DESTDIR}${PREFIX}/bin/${BIN} - rm -f "${DESTDIR}${MANPREFIX}/man1/${MAN1}" + for f in ${BIN}; do rm -f "${DESTDIR}${PREFIX}/bin/$$f"; done + for m in ${MAN1}; do rm -f "${DESTDIR}${MANPREFIX}/man1/$$m"; done clean: rm -f ${BIN} diff --git a/croptool.1 b/croptool.1 @@ -105,6 +105,9 @@ may cause issues depending on the output format. Nothing in particular has been done to prevent screen flicker, so there is flickering when resizing the window or cropping rectangle. .Pp +Transparent portions of images should probably be shown differently, but I'm +too lazy to fix that and don't really care at the moment. +.Pp Since the coordinates of the cropping rectangle are stored as integers, they will become skewed while resizing. If this becomes a real problem, though, you're probably doing something wrong anyways. diff --git a/croptool.c b/croptool.c @@ -54,7 +54,7 @@ static short SELECTION_REDRAW = 1; %b: Bottom side of cropped area. %f: Filename of image. */ -static char *CMD_FORMAT = "mogrify -crop %wx%h+%l+%t '%f'"; +static char *CMD_FORMAT = "croptool_crop %wx%h+%l+%t '%f'"; extern char *optarg; extern int optind; diff --git a/croptool_crop.1 b/croptool_crop.1 @@ -0,0 +1,34 @@ +.Dd January 2, 2021 +.Dt CROPTOOL_CROP 1 +.Os +.Sh NAME +.Nm croptool_crop +.Nd simple image cropping tool +.Sh SYNOPSIS +.Nm +.Ar WIDTHxHEIGHT+X+Y +.Ar filename +.Sh DESCRIPTION +.Nm +crops the given +.Ar filename +to the dimensions specified, where +.Ar WIDTH +and +.Ar HEIGHT +are the width and height of the cropping rectangle, and +.Ar X +and +.Ar Y +specify the top left corner of the cropping rectangle. +This is primarily meant as a companion to +.Xr croptool 1 . +.Sh EXIT STATUS +.Ex -std +.Sh SEE ALSO +.Xr mogrify 1 +.Xr croptool 1 +.Sh BUGS +At least some versions of Imlib2 seem to have issues with saving gif files. +.Sh AUTHORS +.An lumidify Aq Mt nobody@lumidify.org diff --git a/croptool_crop.c b/croptool_crop.c @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2021 lumidify <nobody[at]lumidify.org> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <Imlib2.h> + +int +main(int argc, char *argv[]) { + char *format; + char alpha; + int w, h, x, y; + int orig_w, orig_h; + Imlib_Image src, dst; + Imlib_Load_Error error; + if (argc != 3) { + fprintf(stderr, "USAGE: croptool_crop WIDTHxHEIGHT+X+Y FILENAME\n"); + exit(1); + } + if (sscanf(argv[1], "%dx%d+%d+%d", &w, &h, &x, &y) < 4) { + fprintf(stderr, "Invalid cropping rectangle specified.\n"); + exit(1); + } + src = imlib_load_image(argv[2]); + if (!src) { + fprintf(stderr, "Unable to load image.\n"); + exit(1); + } + imlib_context_set_image(src); + alpha = imlib_image_has_alpha(); + format = strdup(imlib_image_format()); + if (!format) { + fprintf(stderr, "Unable to allocate memory.\n"); + exit(1); + } + orig_w = imlib_image_get_width(); + orig_h = imlib_image_get_height(); + if (w <= 0 || h <= 0 || x < 0 | y < 0 || x + w > orig_w || y + h > orig_h) { + fprintf(stderr, "Invalid cropping rectangle specified.\n"); + exit(1); + } + + dst = imlib_create_image(w, h); + if (!dst) { + fprintf(stderr, "Unable to create cropped image.\n"); + exit(1); + } + imlib_context_set_image(dst); + imlib_image_set_has_alpha(alpha); + imlib_blend_image_onto_image(src, 1, x, y, w, h, 0, 0, w, h); + + imlib_image_set_format(format); + imlib_save_image_with_error_return(argv[2], &error); + if (error) { + fprintf(stderr, "Unable to save cropped image.\n"); + exit(1); + } + + imlib_free_image(); + imlib_context_set_image(src); + imlib_free_image(); + free(format); + + return 0; +}