amiga_imgv

diff tools/convhammer/main.c @ 6:ae0ada629b03

wohooo it works :)
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 27 Oct 2017 15:42:58 +0300
parents 663471a80c21
children
line diff
     1.1 --- a/tools/convhammer/main.c	Fri Oct 27 12:32:24 2017 +0300
     1.2 +++ b/tools/convhammer/main.c	Fri Oct 27 15:42:58 2017 +0300
     1.3 @@ -30,6 +30,7 @@
     1.4  
     1.5  struct ham_image *load_hammer(const char *fname);
     1.6  int save_ham(struct ham_image *img, const char *fname);
     1.7 +int save_ppm(struct ham_image *img, const char *fname);
     1.8  void free_image(struct ham_image *img);
     1.9  
    1.10  int main(int argc, char **argv)
    1.11 @@ -60,6 +61,17 @@
    1.12  			free_image(img);
    1.13  			continue;
    1.14  		}
    1.15 +
    1.16 +		if((dot = strrchr(name, '.'))) {
    1.17 +			*dot = 0;
    1.18 +		}
    1.19 +		strcat(name, ".ppm");
    1.20 +
    1.21 +		if(save_ppm(img, name) == -1) {
    1.22 +			fprintf(stderr, "failed to save ppm image: %s\n", name);
    1.23 +			free_image(img);
    1.24 +			continue;
    1.25 +		}
    1.26  	}
    1.27  	return 0;
    1.28  }
    1.29 @@ -106,6 +118,7 @@
    1.30  		fprintf(stderr, "invalid changes-per-line value: %u\n", (unsigned int)cps);
    1.31  		goto err;
    1.32  	}
    1.33 +	printf(" image needs %d palette changes/line\n", (int)cps);
    1.34  
    1.35  	/* read palette changes and palette */
    1.36  
    1.37 @@ -151,6 +164,28 @@
    1.38  		goto err;
    1.39  	}
    1.40  
    1.41 +	/* correct HAM codes */
    1.42 +	srcptr = inpixels;
    1.43 +	for(i=0; i<numpix; i++) {
    1.44 +		unsigned char idx = *srcptr;
    1.45 +		int ham = (idx >> 4) & 3;
    1.46 +
    1.47 +		switch(ham) {
    1.48 +		case 1:
    1.49 +			ham = 2;
    1.50 +			break;
    1.51 +		case 2:
    1.52 +			ham = 3;
    1.53 +			break;
    1.54 +		case 3:
    1.55 +			ham = 1;
    1.56 +		default:
    1.57 +			break;
    1.58 +		}
    1.59 +
    1.60 +		*srcptr++ = (idx & 0xf) | (ham << 4);
    1.61 +	}
    1.62 +
    1.63  	for(i=0; i<img->nbitplanes; i++) {
    1.64  		srcptr = inpixels;
    1.65  		destptr = img->pixels + i * img->width / 8;
    1.66 @@ -234,11 +269,88 @@
    1.67  	pptr = img->pixels;
    1.68  	num = img->width * img->height / 8 * img->nbitplanes;
    1.69  	for(i=0; i<num; i++) {
    1.70 -		val = htons(*pptr);
    1.71 +		fwrite(pptr, 1, 1, fp);
    1.72  		++pptr;
    1.73 +	}
    1.74 +	fclose(fp);
    1.75 +	return 0;
    1.76 +}
    1.77  
    1.78 -		fwrite(&val, 2, 1, fp);
    1.79 +#define ARED(x)		((((x) & 0xf00) >> 4) | (((x) & 0xf00) >> 8))
    1.80 +#define AGREEN(x)	(((x) & 0xf0) | (((x) & 0xf0) >> 4))
    1.81 +#define ABLUE(x)	((((x) & 0xf) << 4) | ((x) & 0xf))
    1.82 +
    1.83 +int save_ppm(struct ham_image *img, const char *fname)
    1.84 +{
    1.85 +	int i, j, k;
    1.86 +	unsigned char palette[16][3];
    1.87 +	unsigned char color[3] = {0, 0, 0};
    1.88 +	unsigned char *src;
    1.89 +	FILE *fp;
    1.90 +	struct palchange *chg = img->chglist;
    1.91 +
    1.92 +	if(!(fp = fopen(fname, "wb"))) {
    1.93 +		fprintf(stderr, "failed to open: %s for writing: %s\n", fname, strerror(errno));
    1.94 +		return -1;
    1.95  	}
    1.96 +	fprintf(fp, "P6\n%d %d\n255\n", img->width, img->height);
    1.97 +
    1.98 +	for(i=0; i<16; i++) {
    1.99 +		uint16_t pcol = img->palette[i];
   1.100 +		palette[i][0] = ARED(pcol);
   1.101 +		palette[i][1] = AGREEN(pcol);
   1.102 +		palette[i][2] = ABLUE(pcol);
   1.103 +	}
   1.104 +
   1.105 +	src = img->pixels;
   1.106 +	for(i=0; i<img->height; i++) {
   1.107 +		while(chg && chg->line <= i) {
   1.108 +			int idx = (chg->entry & 0xf000) >> 12;
   1.109 +			/*printf("line %d chg idx: %d -> %d %d %d\n", i, idx, ARED(chg->entry), AGREEN(chg->entry), ABLUE(chg->entry));*/
   1.110 +			palette[idx][0] = ARED(chg->entry);
   1.111 +			palette[idx][1] = AGREEN(chg->entry);
   1.112 +			palette[idx][2] = ABLUE(chg->entry);
   1.113 +			chg = chg->next;
   1.114 +		}
   1.115 +
   1.116 +		for(j=0; j<img->width; j++) {
   1.117 +			unsigned char idx = 0;
   1.118 +			unsigned char ham;
   1.119 +			int bit = 7 - (j & 7);
   1.120 +
   1.121 +			for(k=0; k<img->nbitplanes; k++) {
   1.122 +				idx |= (((*(src + k * img->width / 8) >> bit) & 1) << k);
   1.123 +			}
   1.124 +
   1.125 +			ham = (idx >> 4) & 3;
   1.126 +
   1.127 +			switch(ham) {
   1.128 +			case 0:
   1.129 +				color[0] = palette[idx][0];
   1.130 +				color[1] = palette[idx][1];
   1.131 +				color[2] = palette[idx][2];
   1.132 +				break;
   1.133 +			case 1:
   1.134 +				color[2] = ABLUE(idx);
   1.135 +				break;
   1.136 +			case 2:
   1.137 +				color[0] = ABLUE(idx);
   1.138 +				break;
   1.139 +			case 3:
   1.140 +				color[1] = ABLUE(idx);
   1.141 +			}
   1.142 +
   1.143 +			fputc(color[0], fp);
   1.144 +			fputc(color[1], fp);
   1.145 +			fputc(color[2], fp);
   1.146 +
   1.147 +			if(!bit) {
   1.148 +				++src;
   1.149 +			}
   1.150 +		}
   1.151 +		src += img->width / 8 * (img->nbitplanes - 1);
   1.152 +	}
   1.153 +
   1.154  	fclose(fp);
   1.155  	return 0;
   1.156  }