amiga_imgv

changeset 6:ae0ada629b03

wohooo it works :)
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 27 Oct 2017 15:42:58 +0300
parents 0d3d7b020e6a
children 4c36d0f44aa6
files src/amiga/gfx.c src/image.c src/logger.c src/sdl/gfx.c tools/convhammer/main.c
diffstat 5 files changed, 151 insertions(+), 17 deletions(-) [+]
line diff
     1.1 --- a/src/amiga/gfx.c	Fri Oct 27 12:32:24 2017 +0300
     1.2 +++ b/src/amiga/gfx.c	Fri Oct 27 15:42:58 2017 +0300
     1.3 @@ -1,4 +1,5 @@
     1.4  #include <stdio.h>
     1.5 +#include <assert.h>
     1.6  #include <proto/exec.h>
     1.7  #include "gfx.h"
     1.8  #include "copper.h"
     1.9 @@ -32,7 +33,7 @@
    1.10  	prev_intreq = REG_INTREQR;
    1.11  	prev_adkcon = REG_ADKCONR;
    1.12  
    1.13 -	if(init_copper(0, 0) == -1) {
    1.14 +	if(init_copper(1400, 0) == -1) {
    1.15  		return -1;
    1.16  	}
    1.17  
    1.18 @@ -225,17 +226,19 @@
    1.19  		add_copper(COPPER_MOVE(REGN_COLOR(i), img->palette[i]));
    1.20  		logmsg("copper palette[%d]: %x\n", i, (unsigned int)img->palette[i]);
    1.21  	}
    1.22 -#if 0
    1.23  	/* add copper instructions for palette changes according to the image changelist */
    1.24  	prev_line = -1;
    1.25  	while(chg) {
    1.26  		assert(chg->line >= prev_line);
    1.27  		if(chg->line != prev_line) {
    1.28 +			if(chg->line > 255 && prev_line < 255) {
    1.29 +				add_copper(COPPER_VWAIT(255));
    1.30 +			}
    1.31  			prev_line = chg->line;
    1.32  			add_copper(COPPER_VWAIT(chg->line));
    1.33  		}
    1.34  		add_copper(COPPER_MOVE(REGN_COLOR(chg->entry >> 12), chg->entry & 0xfff));
    1.35 +		chg = chg->next;
    1.36  	}
    1.37 -#endif
    1.38  	add_copper(COPPER_END);
    1.39  }
     2.1 --- a/src/image.c	Fri Oct 27 12:32:24 2017 +0300
     2.2 +++ b/src/image.c	Fri Oct 27 15:42:58 2017 +0300
     2.3 @@ -15,13 +15,20 @@
     2.4  static uint16_t (*ntohs)(uint16_t);
     2.5  #endif
     2.6  
     2.7 +
     2.8 +#ifdef __GNUC__
     2.9 +#define PACKED	__attribute__((packed))
    2.10 +#else
    2.11 +#define PACKED
    2.12 +#endif
    2.13 +
    2.14  struct ham_image_header {
    2.15  	char magic[4];
    2.16  	uint16_t width, height;
    2.17  	unsigned char nbitplanes, padding;
    2.18  	uint16_t nchg;
    2.19  	uint16_t palette[16];
    2.20 -};
    2.21 +} PACKED;
    2.22  
    2.23  struct ham_image *load_ham_image(const char *fname)
    2.24  {
     3.1 --- a/src/logger.c	Fri Oct 27 12:32:24 2017 +0300
     3.2 +++ b/src/logger.c	Fri Oct 27 15:42:58 2017 +0300
     3.3 @@ -7,11 +7,15 @@
     3.4  
     3.5  static void init(void)
     3.6  {
     3.7 +#ifdef __unix__
     3.8 +	fp = stdout;
     3.9 +#else
    3.10  	if(!(fp = fopen("logfile", "w"))) {
    3.11  		printf("failed to open logfile\n");
    3.12  		abort();
    3.13  	}
    3.14  	setvbuf(fp, 0, _IONBF, 0);
    3.15 +#endif
    3.16  }
    3.17  
    3.18  void logmsg(const char *fmt, ...)
     4.1 --- a/src/sdl/gfx.c	Fri Oct 27 12:32:24 2017 +0300
     4.2 +++ b/src/sdl/gfx.c	Fri Oct 27 15:42:58 2017 +0300
     4.3 @@ -123,20 +123,24 @@
     4.4  #define GMASK		(fbsurf->format->Gmask)
     4.5  #define BMASK		(fbsurf->format->Bmask)
     4.6  
     4.7 +#define PACKRGB(r, g, b)	((((r) << RSHIFT) & RMASK) | \
     4.8 +		(((g) << GSHIFT) & GMASK) | \
     4.9 +		(((b) << BSHIFT) & BMASK))
    4.10 +
    4.11  void gfx_show_image(struct ham_image *img)
    4.12  {
    4.13  	int i, j, k;
    4.14 -	uint32_t palette[16];
    4.15 +	uint32_t color, palette[16];
    4.16  	uint32_t *dest;
    4.17  	unsigned char *src;
    4.18 +	struct palchange *chg = img->chglist;
    4.19  
    4.20  	for(i=0; i<16; i++) {
    4.21  		uint16_t pcol = img->palette[i];
    4.22  		int red = ARED(pcol);
    4.23  		int green = AGREEN(pcol);
    4.24  		int blue = ABLUE(pcol);
    4.25 -		palette[i] = (red << RSHIFT) | (green << GSHIFT) | (blue << BSHIFT);
    4.26 -		printf("palette[%d]: %d %d %d\n", i, red, green, blue);
    4.27 +		palette[i] = PACKRGB(red, green, blue);
    4.28  	}
    4.29  
    4.30  	if(SDL_MUSTLOCK(fbsurf)) {
    4.31 @@ -146,8 +150,16 @@
    4.32  	dest = fbsurf->pixels;
    4.33  	src = img->pixels;
    4.34  	for(i=0; i<img->height; i++) {
    4.35 +		while(chg && chg->line <= i) {
    4.36 +			int idx = (chg->entry & 0xf000) >> 12;
    4.37 +			int red = ARED(chg->entry);
    4.38 +			int green = AGREEN(chg->entry);
    4.39 +			int blue = ABLUE(chg->entry);
    4.40 +			palette[idx] = PACKRGB(red, green, blue);
    4.41 +			chg = chg->next;
    4.42 +		}
    4.43 +
    4.44  		for(j=0; j<img->width; j++) {
    4.45 -			uint32_t color;
    4.46  			unsigned char idx = 0;
    4.47  			unsigned char ham;
    4.48  			int bit = 7 - (j & 7);
    4.49 @@ -156,23 +168,20 @@
    4.50  				idx |= (((*(src + k * img->width / 8) >> bit) & 1) << k);
    4.51  			}
    4.52  
    4.53 -			printf("%d ", idx);
    4.54 -			color = palette[idx];
    4.55  			ham = (idx >> 4) & 3;
    4.56 -			color = (i & j) ? dest[-1] : 0;
    4.57  
    4.58  			switch(ham) {
    4.59  			case 0:
    4.60  				color = palette[idx];
    4.61  				break;
    4.62  			case 1:
    4.63 -				color = (color & ~BMASK) | (((uint32_t)idx & 0xf) << BSHIFT);
    4.64 +				color = (color & ~BMASK) | (((uint32_t)ABLUE(idx)) << BSHIFT);
    4.65  				break;
    4.66  			case 2:
    4.67 -				color = (color & ~RMASK) | (((uint32_t)idx & 0xf) << RSHIFT);
    4.68 +				color = (color & ~RMASK) | (((uint32_t)ABLUE(idx)) << RSHIFT);
    4.69  				break;
    4.70  			case 3:
    4.71 -				color = (color & ~GMASK) | (((uint32_t)idx & 0xf) << GSHIFT);
    4.72 +				color = (color & ~GMASK) | (((uint32_t)ABLUE(idx)) << GSHIFT);
    4.73  			}
    4.74  
    4.75  			*dest++ = color;
    4.76 @@ -182,7 +191,6 @@
    4.77  		}
    4.78  		src += img->width / 8 * (img->nbitplanes - 1);
    4.79  	}
    4.80 -	putchar('\n');
    4.81  
    4.82  	if(SDL_MUSTLOCK(fbsurf)) {
    4.83  		SDL_UnlockSurface(fbsurf);
     5.1 --- a/tools/convhammer/main.c	Fri Oct 27 12:32:24 2017 +0300
     5.2 +++ b/tools/convhammer/main.c	Fri Oct 27 15:42:58 2017 +0300
     5.3 @@ -30,6 +30,7 @@
     5.4  
     5.5  struct ham_image *load_hammer(const char *fname);
     5.6  int save_ham(struct ham_image *img, const char *fname);
     5.7 +int save_ppm(struct ham_image *img, const char *fname);
     5.8  void free_image(struct ham_image *img);
     5.9  
    5.10  int main(int argc, char **argv)
    5.11 @@ -60,6 +61,17 @@
    5.12  			free_image(img);
    5.13  			continue;
    5.14  		}
    5.15 +
    5.16 +		if((dot = strrchr(name, '.'))) {
    5.17 +			*dot = 0;
    5.18 +		}
    5.19 +		strcat(name, ".ppm");
    5.20 +
    5.21 +		if(save_ppm(img, name) == -1) {
    5.22 +			fprintf(stderr, "failed to save ppm image: %s\n", name);
    5.23 +			free_image(img);
    5.24 +			continue;
    5.25 +		}
    5.26  	}
    5.27  	return 0;
    5.28  }
    5.29 @@ -106,6 +118,7 @@
    5.30  		fprintf(stderr, "invalid changes-per-line value: %u\n", (unsigned int)cps);
    5.31  		goto err;
    5.32  	}
    5.33 +	printf(" image needs %d palette changes/line\n", (int)cps);
    5.34  
    5.35  	/* read palette changes and palette */
    5.36  
    5.37 @@ -151,6 +164,28 @@
    5.38  		goto err;
    5.39  	}
    5.40  
    5.41 +	/* correct HAM codes */
    5.42 +	srcptr = inpixels;
    5.43 +	for(i=0; i<numpix; i++) {
    5.44 +		unsigned char idx = *srcptr;
    5.45 +		int ham = (idx >> 4) & 3;
    5.46 +
    5.47 +		switch(ham) {
    5.48 +		case 1:
    5.49 +			ham = 2;
    5.50 +			break;
    5.51 +		case 2:
    5.52 +			ham = 3;
    5.53 +			break;
    5.54 +		case 3:
    5.55 +			ham = 1;
    5.56 +		default:
    5.57 +			break;
    5.58 +		}
    5.59 +
    5.60 +		*srcptr++ = (idx & 0xf) | (ham << 4);
    5.61 +	}
    5.62 +
    5.63  	for(i=0; i<img->nbitplanes; i++) {
    5.64  		srcptr = inpixels;
    5.65  		destptr = img->pixels + i * img->width / 8;
    5.66 @@ -234,11 +269,88 @@
    5.67  	pptr = img->pixels;
    5.68  	num = img->width * img->height / 8 * img->nbitplanes;
    5.69  	for(i=0; i<num; i++) {
    5.70 -		val = htons(*pptr);
    5.71 +		fwrite(pptr, 1, 1, fp);
    5.72  		++pptr;
    5.73 +	}
    5.74 +	fclose(fp);
    5.75 +	return 0;
    5.76 +}
    5.77  
    5.78 -		fwrite(&val, 2, 1, fp);
    5.79 +#define ARED(x)		((((x) & 0xf00) >> 4) | (((x) & 0xf00) >> 8))
    5.80 +#define AGREEN(x)	(((x) & 0xf0) | (((x) & 0xf0) >> 4))
    5.81 +#define ABLUE(x)	((((x) & 0xf) << 4) | ((x) & 0xf))
    5.82 +
    5.83 +int save_ppm(struct ham_image *img, const char *fname)
    5.84 +{
    5.85 +	int i, j, k;
    5.86 +	unsigned char palette[16][3];
    5.87 +	unsigned char color[3] = {0, 0, 0};
    5.88 +	unsigned char *src;
    5.89 +	FILE *fp;
    5.90 +	struct palchange *chg = img->chglist;
    5.91 +
    5.92 +	if(!(fp = fopen(fname, "wb"))) {
    5.93 +		fprintf(stderr, "failed to open: %s for writing: %s\n", fname, strerror(errno));
    5.94 +		return -1;
    5.95  	}
    5.96 +	fprintf(fp, "P6\n%d %d\n255\n", img->width, img->height);
    5.97 +
    5.98 +	for(i=0; i<16; i++) {
    5.99 +		uint16_t pcol = img->palette[i];
   5.100 +		palette[i][0] = ARED(pcol);
   5.101 +		palette[i][1] = AGREEN(pcol);
   5.102 +		palette[i][2] = ABLUE(pcol);
   5.103 +	}
   5.104 +
   5.105 +	src = img->pixels;
   5.106 +	for(i=0; i<img->height; i++) {
   5.107 +		while(chg && chg->line <= i) {
   5.108 +			int idx = (chg->entry & 0xf000) >> 12;
   5.109 +			/*printf("line %d chg idx: %d -> %d %d %d\n", i, idx, ARED(chg->entry), AGREEN(chg->entry), ABLUE(chg->entry));*/
   5.110 +			palette[idx][0] = ARED(chg->entry);
   5.111 +			palette[idx][1] = AGREEN(chg->entry);
   5.112 +			palette[idx][2] = ABLUE(chg->entry);
   5.113 +			chg = chg->next;
   5.114 +		}
   5.115 +
   5.116 +		for(j=0; j<img->width; j++) {
   5.117 +			unsigned char idx = 0;
   5.118 +			unsigned char ham;
   5.119 +			int bit = 7 - (j & 7);
   5.120 +
   5.121 +			for(k=0; k<img->nbitplanes; k++) {
   5.122 +				idx |= (((*(src + k * img->width / 8) >> bit) & 1) << k);
   5.123 +			}
   5.124 +
   5.125 +			ham = (idx >> 4) & 3;
   5.126 +
   5.127 +			switch(ham) {
   5.128 +			case 0:
   5.129 +				color[0] = palette[idx][0];
   5.130 +				color[1] = palette[idx][1];
   5.131 +				color[2] = palette[idx][2];
   5.132 +				break;
   5.133 +			case 1:
   5.134 +				color[2] = ABLUE(idx);
   5.135 +				break;
   5.136 +			case 2:
   5.137 +				color[0] = ABLUE(idx);
   5.138 +				break;
   5.139 +			case 3:
   5.140 +				color[1] = ABLUE(idx);
   5.141 +			}
   5.142 +
   5.143 +			fputc(color[0], fp);
   5.144 +			fputc(color[1], fp);
   5.145 +			fputc(color[2], fp);
   5.146 +
   5.147 +			if(!bit) {
   5.148 +				++src;
   5.149 +			}
   5.150 +		}
   5.151 +		src += img->width / 8 * (img->nbitplanes - 1);
   5.152 +	}
   5.153 +
   5.154  	fclose(fp);
   5.155  	return 0;
   5.156  }