amiga_imgv

diff tools/convhammer/main.c @ 1:6320a20f17c5

file formats in comments
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 25 Oct 2017 21:05:24 +0300
parents a4788c959918
children f75893a33234
line diff
     1.1 --- a/tools/convhammer/main.c	Wed Oct 25 19:34:53 2017 +0300
     1.2 +++ b/tools/convhammer/main.c	Wed Oct 25 21:05:24 2017 +0300
     1.3 @@ -1,10 +1,99 @@
     1.4  #include <stdio.h>
     1.5  #include <stdlib.h>
     1.6 +#include <string.h>
     1.7 +#include <arpa/inet.h>
     1.8  #include "image.h"
     1.9  
    1.10 +/* hammer dump format (input)
    1.11 + * byte order: little endian
    1.12 + *
    1.13 + * 4 bytes: palette changes per scanline (cps)
    1.14 + * 256 * cps * 2 bytes: [ idx | r | g | b ]
    1.15 + * 320 * 256 bytes: linear HAM pixels
    1.16 + */
    1.17 +
    1.18 +/* interleaved ham format (output)
    1.19 + * byte order: big endian
    1.20 + *
    1.21 + * 4 bytes: magic: "HAAM"
    1.22 + * 2 bytes: width
    1.23 + * 2 bytes: height
    1.24 + * 1 byte: number of bitplanes
    1.25 + * 1 byte: padding
    1.26 + * 2 bytes: number of palette change descriptors (nchg)
    1.27 + * 16 * 2 bytes: initial palette [ x | r | g | b ]
    1.28 + * nchg * 4 bytes: [ scanline ] [ idx | r | g | b ]
    1.29 + * width * height / 8 * bitplanes bytes: pixels interleaved scanlines
    1.30 + *     [ row0 bpl0 ] ... [ row0 bplN ] [ row1 bpl0 ] ... [ row1 bplN ] ...
    1.31 + */
    1.32 +
    1.33  struct ham_image *load_hammer(const char *fname);
    1.34  int save_ham(struct ham_image *img, const char *fname);
    1.35 +void free_image(struct ham_image *img);
    1.36  
    1.37  int main(int argc, char **argv)
    1.38  {
    1.39 +	int i;
    1.40 +	struct ham_image *img;
    1.41 +	char *name, *dot;
    1.42 +
    1.43 +	for(i=1; i<argc; i++) {
    1.44 +		if(!(img = load_hammer(argv[i]))) {
    1.45 +			fprintf(stderr, "failed to load hammer dump: %s\n", argv[i]);
    1.46 +			continue;
    1.47 +		}
    1.48 +
    1.49 +		if(!(name = malloc(strlen(argv[i] + 8)))) {
    1.50 +			perror("failed to allocate output name buffer");
    1.51 +			abort();
    1.52 +		}
    1.53 +		strcpy(name, argv[i]);
    1.54 +
    1.55 +		if((dot = strrchr(name, '.'))) {
    1.56 +			*dot = 0;
    1.57 +		}
    1.58 +		strcat(name, ".ham");
    1.59 +
    1.60 +		if(save_ham(name, img) == -1) {
    1.61 +			fprintf(stderr, "failed to save ham image: %s\n", name);
    1.62 +			free_image(img);
    1.63 +			continue;
    1.64 +		}
    1.65 +	}
    1.66 +	return 0;
    1.67  }
    1.68 +
    1.69 +struct ham_image *load_hammer(const char *fname)
    1.70 +{
    1.71 +}
    1.72 +
    1.73 +int save_ham(struct ham_image *img, const char *fname)
    1.74 +{
    1.75 +	uint16_t val;
    1.76 +	FILE *fp;
    1.77 +
    1.78 +	if(!(fp = fopen(fname, "wb"))) {
    1.79 +		fprintf(stderr, "failed to open %s for writing: %s\n", fname, strerror(errno));
    1.80 +		return -1;
    1.81 +	}
    1.82 +	fprintf(fp, "HAAM");
    1.83 +
    1.84 +	val = htons(img->width);
    1.85 +	fwrite(fp, 2, 1, &val);
    1.86 +	val = htons(img->height);
    1.87 +	fwrite(fp, 2, 1, &val);
    1.88 +	/* TODO: continue */
    1.89 +}
    1.90 +
    1.91 +void free_image(struct ham_image *img)
    1.92 +{
    1.93 +	if(img) {
    1.94 +		free(img->pixels);
    1.95 +		while(img->chglist) {
    1.96 +			void *tmp = img->chglist;
    1.97 +			img->chglist = img->chglist->next;
    1.98 +			free(tmp);
    1.99 +		}
   1.100 +		free(img);
   1.101 +	}
   1.102 +}