char *argdup(char *src, char *sep, int arg, int opt) { char *dst; int l; l = arglen(src, sep, arg, opt); if(l < 0) return(NULL); dst = malloc(++l); if(!dst) return(NULL); argncpy(dst, l, src, sep, arg, opt); return(dst); }
gdImage *fx_flip(gdImage *src, char *options) { int i; char d[32]; i = 0; while(!argncpy(d, 32, options, ", \t", i++, 0)) { if(*d == 'v') { int y, h; gdImage *line; MSG("Flipping image vertically."); line = gdImageCreateTrueColor(gdImageSX(src), 1); h = gdImageSY(src) / 2; for(y = 0; y < h; y++) { /* Copy bottom line into buffer. */ gdImageCopy(line, src, 0, 0, 0, gdImageSY(src) - y - 1, gdImageSX(src), 1); /* Copy the top line onto the bottom. */ gdImageCopy(src, src, 0, gdImageSY(src) - y - 1, 0, y, gdImageSX(src), 1); /* Copy the buffer into the top. */ gdImageCopy(src, line, 0, y, 0, 0, gdImageSX(src), 1); } gdImageDestroy(line); } else if(*d == 'h') { int x, w; gdImage *line; MSG("Flipping image horizontally."); line = gdImageCreateTrueColor(1, gdImageSY(src)); w = gdImageSX(src) / 2; for(x = 0; x < w; x++) { /* Copy right line into buffer. */ gdImageCopy(line, src, 0, 0, gdImageSX(src) - x - 1, 0, 1, gdImageSY(src)); /* Copy the left line onto the right. */ gdImageCopy(src, src, gdImageSX(src) - x - 1, 0, x, 0, 1, gdImageSY(src)); /* Copy the buffer into the left. */ gdImageCopy(src, line, x, 0, 0, 0, 1, gdImageSY(src)); } gdImageDestroy(line); } else WARN("Unknown flip direction: %s", d); } return(src); }
gdImage *fx_crop(gdImage *src, char *options) { char arg[32]; int w, h, x, y; gdImage *im; if(argncpy(arg, 32, options, ", \t", 0, 0)) { WARN("Invalid area to crop: %s", arg); return(src); } w = argtol(arg, "x ", 0, 0, 10); h = argtol(arg, "x ", 1, 0, 10); if(w < 0 || h < 0) { WARN("Invalid area to crop: %s", arg); return(src); } /* Make sure crop area resolution is smaller than the source image. */ if(w > gdImageSX(src) || h > gdImageSY(src)) { WARN("Crop area is larger than the image!"); return(src); } /* Get the offset. */ x = -1; y = -1; if(!argncpy(arg, 32, options, ", \t", 1, 0)) { x = argtol(arg, "x ", 0, 0, 10); y = argtol(arg, "x ", 1, 0, 10); } if(x < 0 || y < 0) { /* By default crop the center of the image. */ x = (gdImageSX(src) - w) / 2; y = (gdImageSY(src) - h) / 2; } MSG("Cropping image from %ix%i [offset: %ix%i] -> %ix%i.", gdImageSX(src), gdImageSY(src), x, y, w, h); im = gdImageCreateTrueColor(w, h); if(!im) { WARN("Out of memory."); return(src); } gdImageCopy(im, src, 0, 0, x, y, w, h); gdImageDestroy(src); return(im); }