meillo@1: /* meillo@1: * compile with: gcc -lgd -lpng -lz -ljpeg -lm resize-gd.c meillo@1: * build-depends: libgd2-noxpm-dev | libgd2-dev meillo@1: * depends: libgd2-noxpm | libgd2-xpm meillo@1: * meillo@1: * meillo@1: * meillo@1: * http://www.libgd.org/ImageCreation meillo@1: * http://cpan.uwinnipeg.ca/htdocs/Image-Resize/Image/Resize.pm.html meillo@1: * http://netpbm.sourceforge.net/ meillo@1: * meillo@1: */ meillo@0: meillo@0: #include meillo@0: #include /* for atoi() */ meillo@0: #include meillo@1: #include meillo@0: #include "gd.h" /* Bring in the gd library functions */ meillo@0: 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@2: usage() { meillo@2: puts("\ meillo@2: usage: resize-gd [...]\n\ meillo@2: (keeps aspect ratio, does not enlarge images)\n\ meillo@2: resize-gd x [...]\n\ meillo@2: (resizes to that size, enlarges if needed)\n\ meillo@2: "); 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@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@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@1: exit(3); meillo@1: } meillo@1: meillo@2: /* parse width and height */ meillo@2: sizeopt.w = atoi(argv[1]); meillo@2: c = strstr(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@0: printf("processing file '%s'\n", argv[i]); meillo@0: meillo@1: if (strcmp(argv[i]+strlen(argv[i])-4, ".png") == 0) { meillo@0: type = Png; meillo@1: } else if (strcmp(argv[i]+strlen(argv[i])-4, ".jpg") == 0) { meillo@0: type = Jpg; meillo@0: } else { meillo@1: fprintf(stderr, "'%s' has unknown filetype. Filename must end with (lowercase) '.png' or '.jpg'.\n", argv[i]); meillo@1: continue; meillo@0: } meillo@1: /* load image */ meillo@1: in = fopen(argv[i], "rb"); meillo@0: 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@1: /* meillo@1: if ((im_in = gdImageCreateFromPng(in)) != NULL) { meillo@1: type = Png; meillo@1: } else if ((im_in = gdImageCreateFromJpeg(in)) != NULL) { meillo@1: type = Jpg; meillo@1: } else { meillo@1: fprintf(stderr, "'%s' has unknown filetype. Filename must end with (lowercase) '.png' or '.jpg'.\n", argv[i]); meillo@1: continue; meillo@1: } meillo@1: */ meillo@1: 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@1: /* copy-resize the image */ meillo@2: im_out = gdImageCreateTrueColor(sizedest.w, sizedest.h); 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@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@0: gdImageDestroy(im_in); meillo@0: gdImageDestroy(im_out); meillo@0: } meillo@0: meillo@0: return 0; meillo@0: }