croptool

Image cropping tool
git clone git://lumidify.org/croptool.git (fast, but not encrypted)
git clone https://lumidify.org/croptool.git (encrypted, but very slow)
git clone git://4kcetb7mo7hj6grozzybxtotsub5bempzo4lirzc3437amof2c2impyd.onion/croptool.git (over tor)
Log | Files | Refs | README | LICENSE

croptool_crop.c (2521B)


      1 /*
      2  * Copyright (c) 2021-2022 lumidify <nobody@lumidify.org>
      3  *
      4  * Permission to use, copy, modify, and/or distribute this software for any
      5  * purpose with or without fee is hereby granted, provided that the above
      6  * copyright notice and this permission notice appear in all copies.
      7  *
      8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
      9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     15  */
     16 
     17 #include <stdio.h>
     18 #include <string.h>
     19 #include <stdlib.h>
     20 #include <Imlib2.h>
     21 
     22 int
     23 main(int argc, char *argv[]) {
     24 	char *dst_filename;
     25 	char *format;
     26 	char alpha;
     27 	int w, h, x, y, n;
     28 	int orig_w, orig_h;
     29 	Imlib_Image src, dst;
     30 	Imlib_Load_Error error;
     31 	if (argc < 3 || argc > 4) {
     32 		fprintf(stderr,
     33 		    "USAGE: croptool_crop <width>x<height>+<x>+<y> "
     34 		    "<source filename> [destination filename]\n"
     35 		);
     36 		exit(1);
     37 	}
     38 	if (argc == 4)
     39 		dst_filename = argv[3];
     40 	else
     41 		dst_filename = argv[2];
     42 	if (sscanf(argv[1], "%dx%d+%d+%d%n", &w, &h, &x, &y, &n) != 4 ||
     43 	    (unsigned)n != strlen(argv[1])) {
     44 		fprintf(stderr, "Invalid cropping rectangle specified.\n");
     45 		exit(1);
     46 	}
     47 	src = imlib_load_image(argv[2]);
     48 	if (!src) {
     49 		fprintf(stderr, "Unable to load image.\n");
     50 		exit(1);
     51 	}
     52 	imlib_context_set_image(src);
     53 	alpha = imlib_image_has_alpha();
     54 	format = strdup(imlib_image_format());
     55 	if (!format) {
     56 		fprintf(stderr, "Unable to allocate memory.\n");
     57 		exit(1);
     58 	}
     59 	orig_w = imlib_image_get_width();
     60 	orig_h = imlib_image_get_height();
     61 	if (w <= 0 || h <= 0 || x < 0 || y < 0 || x + w > orig_w || y + h > orig_h) {
     62 		fprintf(stderr, "Invalid cropping rectangle specified.\n");
     63 		exit(1);
     64 	}
     65 
     66 	dst = imlib_create_image(w, h);
     67 	if (!dst) {
     68 		fprintf(stderr, "Unable to create cropped image.\n");
     69 		exit(1);
     70 	}
     71 	imlib_context_set_image(dst);
     72 	imlib_image_set_has_alpha(alpha);
     73 	imlib_blend_image_onto_image(src, 1, x, y, w, h, 0, 0, w, h);
     74 
     75 	imlib_image_set_format(format);
     76 	imlib_save_image_with_error_return(dst_filename, &error);
     77 	if (error) {
     78 		fprintf(stderr, "Unable to save cropped image.\n");
     79 		exit(1);
     80 	}
     81 
     82 	imlib_free_image();
     83 	imlib_context_set_image(src);
     84 	imlib_free_image();
     85 	free(format);
     86 
     87 	return 0;
     88 }