lbm2bin

changeset 8:aea67378e657 tip

crop
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 25 Jul 2017 08:22:59 +0300
parents f5422c7609c1
children
files src/main.c
diffstat 1 files changed, 30 insertions(+), 9 deletions(-) [+]
line diff
     1.1 --- a/src/main.c	Tue Jul 25 06:29:33 2017 +0300
     1.2 +++ b/src/main.c	Tue Jul 25 08:22:59 2017 +0300
     1.3 @@ -22,6 +22,9 @@
     1.4  static int pcol_bits = 8;
     1.5  static int verbose = 0;
     1.6  
     1.7 +static int crop_x, crop_y;
     1.8 +static int crop_width, crop_height;
     1.9 +
    1.10  int main(int argc, char **argv)
    1.11  {
    1.12  	int i;
    1.13 @@ -54,6 +57,10 @@
    1.14  					verbose = 1;
    1.15  					break;
    1.16  
    1.17 +				case 'c':
    1.18 +					sscanf(argv[++i], "%dx%d+%d+%d", &crop_width, &crop_height, &crop_x, &crop_y);
    1.19 +					break;
    1.20 +
    1.21  				case 'h':
    1.22  					printf("usage: %s [options] <file 1> <file 2> ... <file n>\n", argv[0]);
    1.23  					printf("Options:\n");
    1.24 @@ -62,6 +69,7 @@
    1.25  					printf(" -i: output image in interleaved mode\n");
    1.26  					printf(" -8: output palette with 8bit per color channel\n");
    1.27  					printf(" -4: output palette with 4bit per color channel\n");
    1.28 +					printf(" -c WxH+X+Y: crop image\n");
    1.29  					printf(" -v: verbose output\n");
    1.30  					printf(" -h: print usage and exit\n");
    1.31  					return 0;
    1.32 @@ -84,7 +92,7 @@
    1.33  
    1.34  static int proc_image(const char *fname)
    1.35  {
    1.36 -	int i;
    1.37 +	int i, cropping = 0;
    1.38  	struct image img;
    1.39  	char *outfname, *suffix;
    1.40  	FILE *fp;
    1.41 @@ -94,6 +102,13 @@
    1.42  		return -1;
    1.43  	}
    1.44  
    1.45 +	if(crop_width <= 0) {
    1.46 +		crop_width = img.width - crop_x;
    1.47 +		crop_height = img.height - crop_y;
    1.48 +	} else {
    1.49 +		cropping = 1;
    1.50 +	}
    1.51 +
    1.52  	if(!(outfname = malloc(strlen(fname) + 4))) {
    1.53  		perror("failed to allocate output filename");
    1.54  		return -1;
    1.55 @@ -125,6 +140,9 @@
    1.56  						img.range[i].rate, modestr(img.range[i].cmode));
    1.57  			}
    1.58  		}
    1.59 +		if(cropping) {
    1.60 +			printf("  crop: %dx%d+%d+%d\n", crop_width, crop_height, crop_x, crop_y);
    1.61 +		}
    1.62  	}
    1.63  
    1.64  	switch(mode) {
    1.65 @@ -179,11 +197,11 @@
    1.66  	int i, x, y;
    1.67  	unsigned char acc = 0;
    1.68  	int acc_bit = 7;
    1.69 -	unsigned char *pptr = img->pixels;
    1.70 +	unsigned char *pptr = img->pixels + crop_y * img->width + crop_x;
    1.71  
    1.72  	for(i=0; i<img->bpp; i++) {
    1.73 -		for(y=0; y<img->height; y++) {
    1.74 -			for(x=0; x<img->width; x++) {
    1.75 +		for(y=0; y<crop_height; y++) {
    1.76 +			for(x=0; x<crop_width; x++) {
    1.77  				int bit = img->bpp - i - 1;
    1.78  				acc |= ((pptr[i] >> bit) & 1) << acc_bit;
    1.79  				if(acc_bit-- == 0) {
    1.80 @@ -193,6 +211,7 @@
    1.81  					pptr += img->bpp;
    1.82  				}
    1.83  			}
    1.84 +			pptr += img->width - crop_width;
    1.85  		}
    1.86  	}
    1.87  }
    1.88 @@ -202,11 +221,11 @@
    1.89  	int i, x, y;
    1.90  	unsigned char acc = 0;
    1.91  	int acc_bit = 7;
    1.92 -	unsigned char *pptr = img->pixels;
    1.93 +	unsigned char *pptr = img->pixels + crop_y * img->width + crop_x;
    1.94  
    1.95 -	for(y=0; y<img->height; y++) {
    1.96 +	for(y=0; y<crop_height; y++) {
    1.97  		for(i=0; i<img->bpp; i++) {
    1.98 -			for(x=0; x<img->width; x++) {
    1.99 +			for(x=0; x<crop_width; x++) {
   1.100  				int bit = img->bpp - i - 1;
   1.101  				acc |= ((pptr[i] >> bit) & 1) << acc_bit;
   1.102  				if(acc_bit-- == 0) {
   1.103 @@ -216,6 +235,7 @@
   1.104  					pptr += img->bpp;
   1.105  				}
   1.106  			}
   1.107 +			pptr += img->width - crop_width;
   1.108  		}
   1.109  	}
   1.110  }
   1.111 @@ -225,10 +245,11 @@
   1.112  	int i, j;
   1.113  	unsigned char *pptr = img->pixels;
   1.114  
   1.115 -	for(i=0; i<img->height; i++) {
   1.116 -		for(j=0; j<img->width; j++) {
   1.117 +	for(i=0; i<crop_height; i++) {
   1.118 +		for(j=0; j<crop_width; j++) {
   1.119  			fputc(*pptr++, fp);
   1.120  		}
   1.121 +		pptr += img->width - crop_width;
   1.122  	}
   1.123  }
   1.124