lbm2bin

changeset 4:0b96363742d3

added palette export, and fixed a planar pixel output bug
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 16 Jul 2017 21:30:23 +0300
parents c5e5bf1769d8
children 03b2aa7ed478
files .hgignore Makefile src/main.c
diffstat 3 files changed, 86 insertions(+), 16 deletions(-) [+]
line diff
     1.1 --- a/.hgignore	Sat Jul 15 13:32:49 2017 +0300
     1.2 +++ b/.hgignore	Sun Jul 16 21:30:23 2017 +0300
     1.3 @@ -6,3 +6,6 @@
     1.4  \.iff$
     1.5  \.lbm$
     1.6  \.bin$
     1.7 +\.img$
     1.8 +\.pal$
     1.9 +\.cmap$
     2.1 --- a/Makefile	Sat Jul 15 13:32:49 2017 +0300
     2.2 +++ b/Makefile	Sun Jul 16 21:30:23 2017 +0300
     2.3 @@ -3,6 +3,7 @@
     2.4  
     2.5  bin = lbm2bin
     2.6  
     2.7 +CFLAGS = -pedantic -Wall -g
     2.8  LDFLAGS = -lm
     2.9  
    2.10  $(bin): $(obj)
     3.1 --- a/src/main.c	Sat Jul 15 13:32:49 2017 +0300
     3.2 +++ b/src/main.c	Sun Jul 16 21:30:23 2017 +0300
     3.3 @@ -1,15 +1,19 @@
     3.4  #include <stdio.h>
     3.5  #include <stdlib.h>
     3.6  #include <string.h>
     3.7 +#include <assert.h>
     3.8  #include <errno.h>
     3.9  #include "image.h"
    3.10  
    3.11  static int proc_image(const char *fname);
    3.12  static const char *modestr(int cmode);
    3.13  static void output_planar(FILE *fp, struct image *img);
    3.14 -static void output_chunky(FILE *fp, struct image *img);
    3.15 +static void output_linear(FILE *fp, struct image *img);
    3.16 +static void output_palette(FILE *fp, struct image *img, int pcol_bits);
    3.17  
    3.18  static int planar = 0;
    3.19 +static int pcol_bits = 8;
    3.20 +static int verbose = 0;
    3.21  
    3.22  int main(int argc, char **argv)
    3.23  {
    3.24 @@ -19,19 +23,30 @@
    3.25  		if(argv[i][0] == '-') {
    3.26  			if(argv[i][2] == 0) {
    3.27  				switch(argv[i][1]) {
    3.28 +				case 'a':
    3.29 +					planar = 1;
    3.30 +					pcol_bits = 4;
    3.31 +					break;
    3.32 +
    3.33  				case 'p':
    3.34  					planar = 1;
    3.35  					break;
    3.36  
    3.37 -				case 'c':
    3.38 +				case 'l':
    3.39  					planar = 0;
    3.40  					break;
    3.41  
    3.42 +				case 'v':
    3.43 +					verbose = 1;
    3.44 +					break;
    3.45 +
    3.46  				case 'h':
    3.47  					printf("usage: %s [options] <file 1> <file 2> ... <file n>\n", argv[0]);
    3.48  					printf("Options:\n");
    3.49 -					printf(" -p: output image in planar format (default)\n");
    3.50 -					printf(" -c: output image in chunky format\n");
    3.51 +					printf(" -a: amiga mode (4bit palette colors & planar format)\n");
    3.52 +					printf(" -p: output image in planar format\n");
    3.53 +					printf(" -c: output image in linear (chunky) format\n");
    3.54 +					printf(" -v: verbose output\n");
    3.55  					printf(" -h: print usage and exit\n");
    3.56  					return 0;
    3.57  
    3.58 @@ -71,9 +86,11 @@
    3.59  	if(!(suffix = strrchr(outfname, '.'))) {
    3.60  		suffix = outfname + strlen(outfname);
    3.61  	}
    3.62 -	strcpy(suffix, ".bin");
    3.63 +	strcpy(suffix, ".img");
    3.64  
    3.65 -	printf("processing %s -> %s\n", fname, outfname);
    3.66 +	if(verbose) {
    3.67 +		printf("processing %s -> %s\n", fname, outfname);
    3.68 +	}
    3.69  
    3.70  	if(!(fp = fopen(outfname, "wb"))) {
    3.71  		fprintf(stderr, "failed to open output file: %s: %s\n", outfname, strerror(errno));
    3.72 @@ -82,23 +99,48 @@
    3.73  		return -1;
    3.74  	}
    3.75  
    3.76 -	printf("  bits per pixel: %d\n", img.bpp);
    3.77 -	if(img.num_ranges) {
    3.78 -		printf("  color ranges:\n");
    3.79 -		for(i=0; i<img.num_ranges; i++) {
    3.80 -			printf("    [%d]: %d - %d, rate: %u, mode: %s\n", i, img.range[i].low, img.range[i].high,
    3.81 -					img.range[i].rate, modestr(img.range[i].cmode));
    3.82 +	if(verbose) {
    3.83 +		printf("  bits per pixel: %d (%d colors)\n", img.bpp, 1 << img.bpp);
    3.84 +		if(img.num_ranges) {
    3.85 +			printf("  color ranges:\n");
    3.86 +			for(i=0; i<img.num_ranges; i++) {
    3.87 +				printf("    [%d]: %d - %d, rate: %u, mode: %s\n", i, img.range[i].low, img.range[i].high,
    3.88 +						img.range[i].rate, modestr(img.range[i].cmode));
    3.89 +			}
    3.90  		}
    3.91  	}
    3.92  
    3.93  	if(planar) {
    3.94 +		if(verbose) printf("  writing planar pixels\n");
    3.95  		output_planar(fp, &img);
    3.96  	} else {
    3.97 -		output_chunky(fp, &img);
    3.98 +		if(verbose) printf("  writing linear pixels\n");
    3.99 +		output_linear(fp, &img);
   3.100 +	}
   3.101 +	fclose(fp);
   3.102 +
   3.103 +	/* open fname.pal */
   3.104 +	strcpy(outfname, fname);
   3.105 +	if(!(suffix = strrchr(outfname, '.'))) {
   3.106 +		suffix = outfname + strlen(outfname);
   3.107 +	}
   3.108 +	strcpy(suffix, ".pal");
   3.109 +
   3.110 +	if(!(fp = fopen(outfname, "wb"))) {
   3.111 +		fprintf(stderr, "failed to open palette file %s for writing: %s\n",
   3.112 +				outfname, strerror(errno));
   3.113 +		free(outfname);
   3.114 +		destroy_image(&img);
   3.115 +		return -1;
   3.116  	}
   3.117  
   3.118 +	if(verbose) {
   3.119 +		printf("  writing %d bit palette colors\n", pcol_bits);
   3.120 +		output_palette(fp, &img, pcol_bits);
   3.121 +	}
   3.122 +
   3.123 +	fclose(fp);
   3.124  	free(outfname);
   3.125 -	fclose(fp);
   3.126  	destroy_image(&img);
   3.127  	return 0;
   3.128  }
   3.129 @@ -113,18 +155,19 @@
   3.130  	for(i=0; i<img->bpp; i++) {
   3.131  		for(y=0; y<img->height; y++) {
   3.132  			for(x=0; x<img->width; x++) {
   3.133 -				acc |= (((*pptr++ >> i) & 1) << acc_bit);
   3.134 +				acc |= (((*pptr >> i) & 1) << acc_bit);
   3.135  				if(--acc_bit == 0) {
   3.136  					fputc(acc, fp);
   3.137  					acc_bit = 7;
   3.138  					acc = 0;
   3.139 +					++pptr;
   3.140  				}
   3.141  			}
   3.142  		}
   3.143  	}
   3.144  }
   3.145  
   3.146 -static void output_chunky(FILE *fp, struct image *img)
   3.147 +static void output_linear(FILE *fp, struct image *img)
   3.148  {
   3.149  	int i, j;
   3.150  	unsigned char *pptr = img->pixels;
   3.151 @@ -136,6 +179,29 @@
   3.152  	}
   3.153  }
   3.154  
   3.155 +static void output_palette(FILE *fp, struct image *img, int pcol_bits)
   3.156 +{
   3.157 +	int i, num_colors;
   3.158 +
   3.159 +	num_colors = 1 << img->bpp;
   3.160 +
   3.161 +	assert(pcol_bits == 8 || pcol_bits == 4);
   3.162 +
   3.163 +	if(pcol_bits == 8) {
   3.164 +		for(i=0; i<num_colors; i++) {
   3.165 +			fputc(img->palette[i].r, fp);
   3.166 +			fputc(img->palette[i].g, fp);
   3.167 +			fputc(img->palette[i].b, fp);
   3.168 +		}
   3.169 +	} else {
   3.170 +		/* nibble colors */
   3.171 +		for(i=0; i<num_colors; i++) {
   3.172 +			fputc(img->palette[i].b | (img->palette[i].g << 4), fp);
   3.173 +			fputc(img->palette[i].r, fp);
   3.174 +		}
   3.175 +	}
   3.176 +}
   3.177 +
   3.178  static const char *modestr(int cmode)
   3.179  {
   3.180  	switch(cmode) {