amiga_imgv

diff tools/convhammer/main.c @ 2:f75893a33234

hammer -> ham image converter
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 26 Oct 2017 10:04:29 +0300
parents 6320a20f17c5
children 663471a80c21
line diff
     1.1 --- a/tools/convhammer/main.c	Wed Oct 25 21:05:24 2017 +0300
     1.2 +++ b/tools/convhammer/main.c	Thu Oct 26 10:04:29 2017 +0300
     1.3 @@ -1,6 +1,7 @@
     1.4  #include <stdio.h>
     1.5  #include <stdlib.h>
     1.6  #include <string.h>
     1.7 +#include <errno.h>
     1.8  #include <arpa/inet.h>
     1.9  #include "image.h"
    1.10  
    1.11 @@ -54,7 +55,7 @@
    1.12  		}
    1.13  		strcat(name, ".ham");
    1.14  
    1.15 -		if(save_ham(name, img) == -1) {
    1.16 +		if(save_ham(img, name) == -1) {
    1.17  			fprintf(stderr, "failed to save ham image: %s\n", name);
    1.18  			free_image(img);
    1.19  			continue;
    1.20 @@ -63,14 +64,133 @@
    1.21  	return 0;
    1.22  }
    1.23  
    1.24 +static uint16_t nops(uint16_t x) { return x; }
    1.25 +static uint16_t swaps(uint16_t x) { return (x >> 8) | (x << 8); }
    1.26 +
    1.27  struct ham_image *load_hammer(const char *fname)
    1.28  {
    1.29 +	int i, x, y, pstart, numpix = 320 * 256;
    1.30 +	FILE *fp = 0;
    1.31 +	uint32_t cps, npalent;
    1.32 +	struct ham_image *img = 0;
    1.33 +	struct palchange *tail = 0;
    1.34 +	unsigned char *inpixels = 0;
    1.35 +	unsigned char *srcptr, *destptr;
    1.36 +	uint16_t endtest = 0xaabb;
    1.37 +	uint16_t (*letohs)(uint16_t);
    1.38 +
    1.39 +	letohs = (*(unsigned char*)&endtest == 0xaa) ? swaps : nops;
    1.40 +
    1.41 +	if(!(fp = fopen(fname, "rb"))) {
    1.42 +		fprintf(stderr, "failed to open %s: %s\n", fname, strerror(errno));
    1.43 +		return 0;
    1.44 +	}
    1.45 +
    1.46 +	if(!(img = malloc(sizeof *img))) {
    1.47 +		perror("failed to allocate image");
    1.48 +		goto err;
    1.49 +	}
    1.50 +	img->width = 320;
    1.51 +	img->height = 256;
    1.52 +	img->nbitplanes = 6;
    1.53 +	img->chglist = 0;
    1.54 +
    1.55 +	if(!(img->pixels = malloc(img->width * img->height / 8 * img->nbitplanes))) {
    1.56 +		perror("failed to allocate pixels");
    1.57 +		goto err;
    1.58 +	}
    1.59 +
    1.60 +	if(fread(&cps, 4, 1, fp) < 1) goto err;
    1.61 +	cps = letohs(cps);
    1.62 +	if(cps <= 0 || cps > 16) {
    1.63 +		fprintf(stderr, "invalid changes-per-line value: %u\n", (unsigned int)cps);
    1.64 +		goto err;
    1.65 +	}
    1.66 +
    1.67 +	/* read palette changes and palette */
    1.68 +
    1.69 +	npalent = cps * 256;
    1.70 +	pstart = npalent - 16;
    1.71 +
    1.72 +	for(i=0; i<npalent; i++) {
    1.73 +		uint16_t palent;
    1.74 +		struct palchange *chg;
    1.75 +
    1.76 +		if(fread(&palent, 2, 1, fp) < 1) {
    1.77 +			perror("unexpected end of file");
    1.78 +			goto err;
    1.79 +		}
    1.80 +		palent = letohs(palent);
    1.81 +
    1.82 +		if(!(chg = malloc(sizeof *chg))) {
    1.83 +			perror("failed to allocate palchange node");
    1.84 +			goto err;
    1.85 +		}
    1.86 +		chg->line = i / cps;
    1.87 +		chg->entry = palent;
    1.88 +		chg->next = 0;
    1.89 +		if(tail) {
    1.90 +			tail->next = chg;
    1.91 +			tail = chg;
    1.92 +		} else {
    1.93 +			img->chglist = tail = chg;
    1.94 +		}
    1.95 +
    1.96 +		if(i >= pstart) {
    1.97 +			img->palette[i - pstart] = palent & 0xfff;
    1.98 +		}
    1.99 +	}
   1.100 +
   1.101 +	/* read pixels */
   1.102 +	if(!(inpixels = malloc(numpix))) {
   1.103 +		perror("failed to allocate pixel buffer");
   1.104 +		goto err;
   1.105 +	}
   1.106 +	if(fread(inpixels, 1, numpix, fp) < numpix) {
   1.107 +		perror("unexpected end of file while reading pixels");
   1.108 +		goto err;
   1.109 +	}
   1.110 +
   1.111 +	for(i=0; i<img->nbitplanes; i++) {
   1.112 +		srcptr = inpixels;
   1.113 +		destptr = img->pixels + i * img->width / 8;
   1.114 +		for(y=0; y<img->height; y++) {
   1.115 +			unsigned char pixval = 0;
   1.116 +			for(x=0; x<img->width; x++) {
   1.117 +				pixval = (pixval << 1) | ((*srcptr++ >> i) & 1);
   1.118 +				if((x & 7) == 7) {
   1.119 +					*destptr++ = pixval;
   1.120 +					pixval = 0;
   1.121 +				}
   1.122 +			}
   1.123 +			destptr += img->width / 8 * (img->nbitplanes - 1);
   1.124 +		}
   1.125 +	}
   1.126 +
   1.127 +	return img;
   1.128 +
   1.129 +err:
   1.130 +	free(inpixels);
   1.131 +	if(img) {
   1.132 +		while(img->chglist) {
   1.133 +			void *tmp = img->chglist;
   1.134 +			img->chglist = img->chglist->next;
   1.135 +			free(tmp);
   1.136 +		}
   1.137 +		free(img->pixels);
   1.138 +		free(img);
   1.139 +	}
   1.140 +	if(fp) fclose(fp);
   1.141 +	return 0;
   1.142  }
   1.143  
   1.144  int save_ham(struct ham_image *img, const char *fname)
   1.145  {
   1.146 +	int i, num;
   1.147 +	unsigned char c, *pptr;
   1.148  	uint16_t val;
   1.149  	FILE *fp;
   1.150 +	struct palchange *chg;
   1.151  
   1.152  	if(!(fp = fopen(fname, "wb"))) {
   1.153  		fprintf(stderr, "failed to open %s for writing: %s\n", fname, strerror(errno));
   1.154 @@ -79,10 +199,41 @@
   1.155  	fprintf(fp, "HAAM");
   1.156  
   1.157  	val = htons(img->width);
   1.158 -	fwrite(fp, 2, 1, &val);
   1.159 +	fwrite(&val, 2, 1, fp);
   1.160  	val = htons(img->height);
   1.161 -	fwrite(fp, 2, 1, &val);
   1.162 -	/* TODO: continue */
   1.163 +	fwrite(&val, 2, 1, fp);
   1.164 +	c = img->nbitplanes;
   1.165 +	fwrite(&c, 1, 1, fp);
   1.166 +	fwrite(&c, 1, 1, fp);	/* padding */
   1.167 +
   1.168 +	num = 0;
   1.169 +	chg = img->chglist;
   1.170 +	while(chg) {
   1.171 +		++num;
   1.172 +		chg = chg->next;
   1.173 +	}
   1.174 +	num = htons(num);
   1.175 +	fwrite(&num, 2, 1, fp);
   1.176 +
   1.177 +	chg = img->chglist;
   1.178 +	while(chg) {
   1.179 +		val = htons(chg->line);
   1.180 +		fwrite(&val, 2, 1, fp);
   1.181 +		val = htons(chg->entry);
   1.182 +		fwrite(&val, 2, 1, fp);
   1.183 +		chg = chg->next;
   1.184 +	}
   1.185 +
   1.186 +	pptr = img->pixels;
   1.187 +	num = img->width * img->height / 8 * img->nbitplanes;
   1.188 +	for(i=0; i<num; i++) {
   1.189 +		val = htons(*pptr);
   1.190 +		++pptr;
   1.191 +
   1.192 +		fwrite(&val, 2, 1, fp);
   1.193 +	}
   1.194 +	fclose(fp);
   1.195 +	return 0;
   1.196  }
   1.197  
   1.198  void free_image(struct ham_image *img)