lbm2bin

changeset 7:f5422c7609c1

implemented interleaved output, and *I think* I fixed the planar output, needs testing
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 25 Jul 2017 06:29:33 +0300
parents 89fd62196739
children aea67378e657
files src/main.c
diffstat 1 files changed, 65 insertions(+), 13 deletions(-) [+]
line diff
     1.1 --- a/src/main.c	Thu Jul 20 07:42:52 2017 +0300
     1.2 +++ b/src/main.c	Tue Jul 25 06:29:33 2017 +0300
     1.3 @@ -9,9 +9,16 @@
     1.4  static const char *modestr(int cmode);
     1.5  static void output_planar(FILE *fp, struct image *img);
     1.6  static void output_linear(FILE *fp, struct image *img);
     1.7 +static void output_interleaved(FILE *fp, struct image *img);
     1.8  static void output_palette(FILE *fp, struct image *img, int pcol_bits);
     1.9  
    1.10 -static int planar = 0;
    1.11 +enum img_mode {
    1.12 +	IMG_LINEAR,
    1.13 +	IMG_PLANAR,
    1.14 +	IMG_INTERLEAVED
    1.15 +};
    1.16 +
    1.17 +static enum img_mode mode = IMG_LINEAR;
    1.18  static int pcol_bits = 8;
    1.19  static int verbose = 0;
    1.20  
    1.21 @@ -23,17 +30,24 @@
    1.22  		if(argv[i][0] == '-') {
    1.23  			if(argv[i][2] == 0) {
    1.24  				switch(argv[i][1]) {
    1.25 -				case 'a':
    1.26 -					planar = 1;
    1.27 +				case '4':
    1.28  					pcol_bits = 4;
    1.29  					break;
    1.30  
    1.31 +				case '8':
    1.32 +					pcol_bits = 8;
    1.33 +					break;
    1.34 +
    1.35  				case 'p':
    1.36 -					planar = 1;
    1.37 +					mode = IMG_PLANAR;
    1.38  					break;
    1.39  
    1.40  				case 'l':
    1.41 -					planar = 0;
    1.42 +					mode = IMG_LINEAR;
    1.43 +					break;
    1.44 +
    1.45 +				case 'i':
    1.46 +					mode = IMG_INTERLEAVED;
    1.47  					break;
    1.48  
    1.49  				case 'v':
    1.50 @@ -43,9 +57,11 @@
    1.51  				case 'h':
    1.52  					printf("usage: %s [options] <file 1> <file 2> ... <file n>\n", argv[0]);
    1.53  					printf("Options:\n");
    1.54 -					printf(" -a: amiga mode (4bit palette colors & planar format)\n");
    1.55 -					printf(" -p: output image in planar format\n");
    1.56 -					printf(" -c: output image in linear (chunky) format\n");
    1.57 +					printf(" -p: output image in planar mode\n");
    1.58 +					printf(" -l: output image in linear mode\n");
    1.59 +					printf(" -i: output image in interleaved mode\n");
    1.60 +					printf(" -8: output palette with 8bit per color channel\n");
    1.61 +					printf(" -4: output palette with 4bit per color channel\n");
    1.62  					printf(" -v: verbose output\n");
    1.63  					printf(" -h: print usage and exit\n");
    1.64  					return 0;
    1.65 @@ -111,12 +127,24 @@
    1.66  		}
    1.67  	}
    1.68  
    1.69 -	if(planar) {
    1.70 +	switch(mode) {
    1.71 +	case IMG_PLANAR:
    1.72  		if(verbose) printf("  writing planar pixels\n");
    1.73  		output_planar(fp, &img);
    1.74 -	} else {
    1.75 +		break;
    1.76 +
    1.77 +	case IMG_LINEAR:
    1.78  		if(verbose) printf("  writing linear pixels\n");
    1.79  		output_linear(fp, &img);
    1.80 +		break;
    1.81 +
    1.82 +	case IMG_INTERLEAVED:
    1.83 +		if(verbose) printf("  writing interleaved pixels\n");
    1.84 +		output_interleaved(fp, &img);
    1.85 +		break;
    1.86 +
    1.87 +	default:
    1.88 +		break;
    1.89  	}
    1.90  	fclose(fp);
    1.91  
    1.92 @@ -156,12 +184,36 @@
    1.93  	for(i=0; i<img->bpp; i++) {
    1.94  		for(y=0; y<img->height; y++) {
    1.95  			for(x=0; x<img->width; x++) {
    1.96 -				acc |= (((*pptr >> i) & 1) << acc_bit);
    1.97 -				if(--acc_bit == 0) {
    1.98 +				int bit = img->bpp - i - 1;
    1.99 +				acc |= ((pptr[i] >> bit) & 1) << acc_bit;
   1.100 +				if(acc_bit-- == 0) {
   1.101  					fputc(acc, fp);
   1.102  					acc_bit = 7;
   1.103  					acc = 0;
   1.104 -					++pptr;
   1.105 +					pptr += img->bpp;
   1.106 +				}
   1.107 +			}
   1.108 +		}
   1.109 +	}
   1.110 +}
   1.111 +
   1.112 +static void output_interleaved(FILE *fp, struct image *img)
   1.113 +{
   1.114 +	int i, x, y;
   1.115 +	unsigned char acc = 0;
   1.116 +	int acc_bit = 7;
   1.117 +	unsigned char *pptr = img->pixels;
   1.118 +
   1.119 +	for(y=0; y<img->height; y++) {
   1.120 +		for(i=0; i<img->bpp; i++) {
   1.121 +			for(x=0; x<img->width; x++) {
   1.122 +				int bit = img->bpp - i - 1;
   1.123 +				acc |= ((pptr[i] >> bit) & 1) << acc_bit;
   1.124 +				if(acc_bit-- == 0) {
   1.125 +					fputc(acc, fp);
   1.126 +					acc_bit = 7;
   1.127 +					acc = 0;
   1.128 +					pptr += img->bpp;
   1.129  				}
   1.130  			}
   1.131  		}