meillo@1: /* meillo@7: * resize-gd - resizes images using the gd-library meillo@7: * meillo@7: * Copyright 2008 by markus schnalke meillo@7: * meillo@1: * build-depends: libgd2-noxpm-dev | libgd2-dev meillo@1: * depends: libgd2-noxpm | libgd2-xpm meillo@1: */ meillo@0: meillo@0: #include meillo@7: #include meillo@0: #include meillo@1: #include meillo@7: #include "gd.h" meillo@0: meillo@5: #define PROGRAM "resize-gd" meillo@10: #define VERSION "0.2" meillo@0: meillo@0: enum { meillo@0: Png, meillo@3: Jpg meillo@0: }; meillo@0: meillo@2: struct size { meillo@2: int w; meillo@2: int h; meillo@2: }; meillo@0: meillo@2: meillo@2: void meillo@5: version() { meillo@5: printf("%s version: %s\n", PROGRAM, VERSION); meillo@5: } meillo@5: meillo@5: meillo@5: void meillo@2: usage() { meillo@5: printf("\ meillo@5: usage: %s [...]\n\ meillo@2: (keeps aspect ratio, does not enlarge images)\n\ meillo@5: %s x [...]\n\ meillo@2: (resizes to that size, enlarges if needed)\n\ meillo@5: ", PROGRAM, PROGRAM); meillo@2: } meillo@2: meillo@2: meillo@2: struct size meillo@2: calcsize(struct size opt, struct size img, int enlarge) { meillo@2: struct size result; meillo@2: meillo@2: if (opt.h <= 0) { meillo@2: /* keep aspect ratio */ meillo@2: if (!enlarge && opt.w > img.w && opt.w > img.h) { meillo@2: opt.w = (img.w > img.h) ? img.w : img.h; meillo@2: } meillo@2: if (img.w > img.h) { meillo@2: result.w = opt.w; meillo@2: result.h = opt.w * (1.0 * img.h / img.w); meillo@2: } else { meillo@2: result.h = opt.w; meillo@2: result.w = opt.w * (1.0 * img.w / img.h); meillo@2: } meillo@2: } else { meillo@2: result = opt; meillo@2: } meillo@2: return result; meillo@2: } meillo@2: meillo@2: meillo@2: int meillo@2: validsize(char* sp) { meillo@2: while (isdigit(*sp)) { meillo@2: sp++; meillo@2: } meillo@2: if (*sp == 'x' && isdigit(*(sp+1))) { meillo@2: sp++; meillo@2: while (isdigit(*sp)) { meillo@2: sp++; meillo@2: } meillo@2: } meillo@2: if (*sp != '\0') { meillo@2: return 0; meillo@2: } meillo@2: return 1; meillo@2: } meillo@2: meillo@2: meillo@2: int meillo@2: main(int argc, char* argv[]) { meillo@0: int i; meillo@0: int type; meillo@15: int success = 0; meillo@0: gdImagePtr im_in; meillo@0: gdImagePtr im_out; meillo@0: FILE* in; meillo@0: FILE* out; meillo@2: char* c; meillo@2: struct size sizeopt, sizeimg, sizedest; meillo@0: meillo@5: if (argc == 2 && strcmp(argv[1], "--version") == 0) { meillo@5: version(); meillo@5: exit(0); meillo@5: } meillo@5: if (argc == 2 && strcmp(argv[1], "--help") == 0) { meillo@5: version(); meillo@5: usage(); meillo@5: exit(0); meillo@5: } meillo@0: if (argc < 3) { meillo@2: usage(); meillo@0: exit(1); meillo@0: } meillo@0: meillo@2: if (!validsize(argv[1])) { meillo@1: fprintf(stderr, "Invalid form of size. Has to be or x.\n"); meillo@2: usage(); meillo@5: exit(2); meillo@1: } meillo@1: meillo@2: /* parse width and height */ meillo@2: sizeopt.w = atoi(argv[1]); meillo@14: c = strchr(argv[1], 'x'); meillo@2: if (c && c[1] != '\0') { meillo@2: sizeopt.h = atoi(c + 1); meillo@1: } else { meillo@2: sizeopt.h = -1; /* keep aspect ratio */ meillo@1: } meillo@1: meillo@0: meillo@0: /* process images */ meillo@0: for (i = 2; i < argc; i++) { meillo@10: /* printf("processing file '%s'\n", argv[i]); */ meillo@0: meillo@14: c = strrchr(argv[i], '.'); meillo@14: if (strcmp(c, ".png") == 0 || strcmp(c, ".PNG") == 0) { meillo@0: type = Png; meillo@14: } else if (strcmp(c, ".jpg") == 0 || strcmp(c, ".JPG") == 0 || strcmp(c, ".jpeg") == 0 || strcmp(c, ".JPEG") == 0) { meillo@0: type = Jpg; meillo@0: } else { meillo@14: fprintf(stderr, "'%s' has unknown filetype. Filename must end with '.jpg', '.jpeg' or '.png'.\n", argv[i]); meillo@1: continue; meillo@0: } meillo@7: meillo@1: /* load image */ meillo@1: in = fopen(argv[i], "rb"); meillo@10: if (in == NULL) { meillo@10: fprintf(stderr, "unable to open '%s' for reading\n", argv[i]); meillo@10: continue; meillo@10: } meillo@0: if (type == Png) { meillo@0: im_in = gdImageCreateFromPng(in); meillo@0: } else if (type == Jpg) { meillo@0: im_in = gdImageCreateFromJpeg(in); meillo@0: } meillo@10: if (im_in == NULL) { meillo@10: fprintf(stderr, "unable to load image '%s'\n", argv[i]); meillo@10: continue; meillo@10: } meillo@0: fclose(in); meillo@0: meillo@2: /* calculate target size */ meillo@2: sizeimg.w = im_in->sx; meillo@2: sizeimg.h = im_in->sy; meillo@2: sizedest = calcsize(sizeopt, sizeimg, 0); meillo@1: meillo@5: /* skip images that dont need to be resized */ meillo@5: if (sizedest.w == sizeimg.w && sizedest.h == sizeimg.h) { meillo@15: success++; meillo@5: gdImageDestroy(im_in); meillo@5: continue; meillo@5: } meillo@5: meillo@1: /* copy-resize the image */ meillo@2: im_out = gdImageCreateTrueColor(sizedest.w, sizedest.h); meillo@10: if (im_out == NULL) { meillo@10: fprintf(stderr, "unable to allocate memory for resized image\n"); meillo@10: exit(3); meillo@10: } meillo@0: gdImageCopyResampled(im_out, im_in, 0, 0, 0, 0, im_out->sx, im_out->sy, im_in->sx, im_in->sy); meillo@0: meillo@1: /* write image */ meillo@0: out = fopen(argv[i], "wb"); meillo@10: if (out == NULL) { meillo@10: fprintf(stderr, "unable to open '%s' for writing\n", argv[i]); meillo@10: continue; meillo@10: } meillo@0: if (type == Png) { meillo@0: gdImagePng(im_out, out); meillo@0: } else if (type == Jpg) { meillo@0: gdImageJpeg(im_out, out, -1); meillo@0: } meillo@0: fclose(out); meillo@0: meillo@15: success++; meillo@15: meillo@0: gdImageDestroy(im_in); meillo@0: gdImageDestroy(im_out); meillo@0: } meillo@0: meillo@15: if (success == 0) { meillo@15: return 4; /* no resizing was successful */ meillo@15: } else { meillo@15: return 0; meillo@15: } meillo@0: }