# HG changeset patch # User John Tsiombikas # Date 1500960179 -10800 # Node ID aea67378e657ced4d4374c59016d427697f11d9a # Parent f5422c7609c1f4b6626f15bb8c64fc1e197f9ce1 crop diff -r f5422c7609c1 -r aea67378e657 src/main.c --- a/src/main.c Tue Jul 25 06:29:33 2017 +0300 +++ b/src/main.c Tue Jul 25 08:22:59 2017 +0300 @@ -22,6 +22,9 @@ static int pcol_bits = 8; static int verbose = 0; +static int crop_x, crop_y; +static int crop_width, crop_height; + int main(int argc, char **argv) { int i; @@ -54,6 +57,10 @@ verbose = 1; break; + case 'c': + sscanf(argv[++i], "%dx%d+%d+%d", &crop_width, &crop_height, &crop_x, &crop_y); + break; + case 'h': printf("usage: %s [options] ... \n", argv[0]); printf("Options:\n"); @@ -62,6 +69,7 @@ printf(" -i: output image in interleaved mode\n"); printf(" -8: output palette with 8bit per color channel\n"); printf(" -4: output palette with 4bit per color channel\n"); + printf(" -c WxH+X+Y: crop image\n"); printf(" -v: verbose output\n"); printf(" -h: print usage and exit\n"); return 0; @@ -84,7 +92,7 @@ static int proc_image(const char *fname) { - int i; + int i, cropping = 0; struct image img; char *outfname, *suffix; FILE *fp; @@ -94,6 +102,13 @@ return -1; } + if(crop_width <= 0) { + crop_width = img.width - crop_x; + crop_height = img.height - crop_y; + } else { + cropping = 1; + } + if(!(outfname = malloc(strlen(fname) + 4))) { perror("failed to allocate output filename"); return -1; @@ -125,6 +140,9 @@ img.range[i].rate, modestr(img.range[i].cmode)); } } + if(cropping) { + printf(" crop: %dx%d+%d+%d\n", crop_width, crop_height, crop_x, crop_y); + } } switch(mode) { @@ -179,11 +197,11 @@ int i, x, y; unsigned char acc = 0; int acc_bit = 7; - unsigned char *pptr = img->pixels; + unsigned char *pptr = img->pixels + crop_y * img->width + crop_x; for(i=0; ibpp; i++) { - for(y=0; yheight; y++) { - for(x=0; xwidth; x++) { + for(y=0; ybpp - i - 1; acc |= ((pptr[i] >> bit) & 1) << acc_bit; if(acc_bit-- == 0) { @@ -193,6 +211,7 @@ pptr += img->bpp; } } + pptr += img->width - crop_width; } } } @@ -202,11 +221,11 @@ int i, x, y; unsigned char acc = 0; int acc_bit = 7; - unsigned char *pptr = img->pixels; + unsigned char *pptr = img->pixels + crop_y * img->width + crop_x; - for(y=0; yheight; y++) { + for(y=0; ybpp; i++) { - for(x=0; xwidth; x++) { + for(x=0; xbpp - i - 1; acc |= ((pptr[i] >> bit) & 1) << acc_bit; if(acc_bit-- == 0) { @@ -216,6 +235,7 @@ pptr += img->bpp; } } + pptr += img->width - crop_width; } } } @@ -225,10 +245,11 @@ int i, j; unsigned char *pptr = img->pixels; - for(i=0; iheight; i++) { - for(j=0; jwidth; j++) { + for(i=0; iwidth - crop_width; } }