amiga_imgv

diff tools/img2ham/main.c @ 12:20c7238c60be

img2ham unfinished
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 20 Nov 2017 08:36:16 +0200
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/tools/img2ham/main.c	Mon Nov 20 08:36:16 2017 +0200
     1.3 @@ -0,0 +1,226 @@
     1.4 +#include <stdio.h>
     1.5 +#include <stdlib.h>
     1.6 +#include <string.h>
     1.7 +#include <limits.h>
     1.8 +#include <errno.h>
     1.9 +#include <imago2.h>
    1.10 +#include <arpa/inet.h>
    1.11 +#include "image.h"
    1.12 +
    1.13 +static int convert(struct ham_image *img, unsigned char *pixels, int xsz, int ysz);
    1.14 +static int save_ham(struct ham_image *img, const char *fname);
    1.15 +static void destroy_image(struct ham_image *img);
    1.16 +
    1.17 +int main(int argc, char **argv)
    1.18 +{
    1.19 +	int i, width, height, len;
    1.20 +	unsigned char *pixels;
    1.21 +	char *out_fname, *suffix;
    1.22 +	struct ham_image img;
    1.23 +
    1.24 +	for(i=1; i<argc; i++) {
    1.25 +		if(!(pixels = img_load_pixels(argv[i], &width, &height, IMG_FMT_RGB24))) {
    1.26 +			fprintf(stderr, "failed to load image: %s\n", argv[i]);
    1.27 +			continue;
    1.28 +		}
    1.29 +		convert(&img, pixels, width, height);
    1.30 +
    1.31 +		len = strlen(argv[i]) + 4;
    1.32 +		if(!(out_fname = malloc(len + 1))) {
    1.33 +			perror("failed to allocate memory");
    1.34 +			abort();
    1.35 +		}
    1.36 +		memcpy(out_fname, argv[i], len + 1);
    1.37 +		if(!(suffix = strrchr(out_fname, '.'))) {
    1.38 +			suffix = out_fname + len;
    1.39 +		}
    1.40 +		memcpy(suffix, ".ham", 5);
    1.41 +
    1.42 +		save_ham(&img, out_fname);
    1.43 +		destroy_image(&img);
    1.44 +
    1.45 +		free(out_fname);
    1.46 +	}
    1.47 +
    1.48 +	return 0;
    1.49 +}
    1.50 +
    1.51 +struct fringe {
    1.52 +	int pixel, score;
    1.53 +};
    1.54 +
    1.55 +static int detect_fringes(int *flist, int num, unsigned char *scanline, int width, int pr, int pg, int pb)
    1.56 +{
    1.57 +	int i, j, fcount = 0;
    1.58 +	struct fringe fringes[16] = {{0, 0}};
    1.59 +
    1.60 +	if(num > 16) num = 16;
    1.61 +
    1.62 +	for(i=0; i<width; i++) {
    1.63 +		int r = *scanline++;
    1.64 +		int g = *scanline++;
    1.65 +		int b = *scanline++;
    1.66 +		int dr = r - pr;
    1.67 +		int dg = g - pg;
    1.68 +		int db = b - pb;
    1.69 +		int max = dr > dg ? (dr > db ? dr : db) : (dg > db ? dg : db);
    1.70 +		int score = dr + dg + db - max;
    1.71 +
    1.72 +		if(score > 0) {
    1.73 +			int min = fringes[0].score, min_idx = 0;
    1.74 +			for(j=1; j<fcount; j++) {
    1.75 +				if(fringes[j].score < min) {
    1.76 +					min = fringes[j].score;
    1.77 +					min_idx = j;
    1.78 +				}
    1.79 +			}
    1.80 +			if(score > min) {
    1.81 +				fringes[min_idx].score = score;
    1.82 +				fringes[min_idx].pixel = i;
    1.83 +			}
    1.84 +		}
    1.85 +	}
    1.86 +}
    1.87 +
    1.88 +static int convert(struct ham_image *img, unsigned char *pixels, int xsz, int ysz)
    1.89 +{
    1.90 +	int i, j, x, y, pstart, numpix = xsz * ysz;
    1.91 +	FILE *fp = 0;
    1.92 +	uint32_t cps, npalent;
    1.93 +	struct palchange *tail = 0;
    1.94 +	unsigned char *inpixels = 0;
    1.95 +	unsigned char *srcptr, *destptr;
    1.96 +
    1.97 +	/* convert RGB to HAM in-place */
    1.98 +	for(i=0; i<ysz; i++) {
    1.99 +		for(j=0; j<xsz; j++) {
   1.100 +		}
   1.101 +	}
   1.102 +
   1.103 +	img->width = xsz;
   1.104 +	img->height = ysz;
   1.105 +	img->nbitplanes = 6;
   1.106 +	img->ham = 1;
   1.107 +	img->chglist = 0;
   1.108 +
   1.109 +	if(!(img->pixels = malloc(img->width * img->height / 8 * img->nbitplanes))) {
   1.110 +		perror("failed to allocate pixels");
   1.111 +		goto err;
   1.112 +	}
   1.113 +
   1.114 +	for(i=0; i<img->nbitplanes; i++) {
   1.115 +		srcptr = inpixels;
   1.116 +		destptr = img->pixels + i * img->width / 8;
   1.117 +		for(y=0; y<img->height; y++) {
   1.118 +			unsigned char pixval = 0;
   1.119 +			for(x=0; x<img->width; x++) {
   1.120 +				pixval = (pixval << 1) | ((*srcptr++ >> i) & 1);
   1.121 +				if((x & 7) == 7) {
   1.122 +					*destptr++ = pixval;
   1.123 +					pixval = 0;
   1.124 +				}
   1.125 +			}
   1.126 +			destptr += img->width / 8 * (img->nbitplanes - 1);
   1.127 +		}
   1.128 +	}
   1.129 +
   1.130 +	free(inpixels);
   1.131 +	fclose(fp);
   1.132 +	return img;
   1.133 +
   1.134 +err:
   1.135 +	free(inpixels);
   1.136 +	if(img) {
   1.137 +		while(img->chglist) {
   1.138 +			void *tmp = img->chglist;
   1.139 +			img->chglist = img->chglist->next;
   1.140 +			free(tmp);
   1.141 +		}
   1.142 +		free(img->pixels);
   1.143 +		free(img);
   1.144 +	}
   1.145 +	if(fp) fclose(fp);
   1.146 +	return 0;
   1.147 +}
   1.148 +
   1.149 +
   1.150 +/* interleaved ham format (output)
   1.151 + * byte order: big endian
   1.152 + *
   1.153 + * 4 bytes: magic: "HAAM"
   1.154 + * 2 bytes: width
   1.155 + * 2 bytes: height
   1.156 + * 1 byte: number of bitplanes
   1.157 + * 1 byte: padding
   1.158 + * 2 bytes: number of palette change descriptors (nchg)
   1.159 + * 16 * 2 bytes: initial palette [ x | r | g | b ]
   1.160 + * nchg * 4 bytes: [ scanline ] [ idx | r | g | b ]
   1.161 + * width * height / 8 * bitplanes bytes: pixels interleaved scanlines
   1.162 + *     [ row0 bpl0 ] ... [ row0 bplN ] [ row1 bpl0 ] ... [ row1 bplN ] ...
   1.163 + */
   1.164 +static int save_ham(struct ham_image *img, const char *fname)
   1.165 +{
   1.166 +	int i, num;
   1.167 +	unsigned char c, *pptr;
   1.168 +	uint16_t val;
   1.169 +	FILE *fp;
   1.170 +	struct palchange *chg;
   1.171 +
   1.172 +	if(!(fp = fopen(fname, "wb"))) {
   1.173 +		fprintf(stderr, "failed to open %s for writing: %s\n", fname, strerror(errno));
   1.174 +		return -1;
   1.175 +	}
   1.176 +	fprintf(fp, "HAAM");
   1.177 +
   1.178 +	val = htons(img->width);
   1.179 +	fwrite(&val, 2, 1, fp);
   1.180 +	val = htons(img->height);
   1.181 +	fwrite(&val, 2, 1, fp);
   1.182 +	c = img->nbitplanes;
   1.183 +	fwrite(&c, 1, 1, fp);
   1.184 +	fwrite(&c, 1, 1, fp);	/* padding */
   1.185 +
   1.186 +	num = 0;
   1.187 +	chg = img->chglist;
   1.188 +	while(chg) {
   1.189 +		++num;
   1.190 +		chg = chg->next;
   1.191 +	}
   1.192 +	num = htons(num);
   1.193 +	fwrite(&num, 2, 1, fp);
   1.194 +
   1.195 +	for(i=0; i<16; i++) {
   1.196 +		val = htons(img->palette[i]);
   1.197 +		fwrite(&val, 2, 1, fp);
   1.198 +	}
   1.199 +
   1.200 +	chg = img->chglist;
   1.201 +	while(chg) {
   1.202 +		val = htons(chg->line);
   1.203 +		fwrite(&val, 2, 1, fp);
   1.204 +		val = htons(chg->entry);
   1.205 +		fwrite(&val, 2, 1, fp);
   1.206 +		chg = chg->next;
   1.207 +	}
   1.208 +
   1.209 +	pptr = img->pixels;
   1.210 +	num = img->width * img->height / 8 * img->nbitplanes;
   1.211 +	for(i=0; i<num; i++) {
   1.212 +		fwrite(pptr, 1, 1, fp);
   1.213 +		++pptr;
   1.214 +	}
   1.215 +	fclose(fp);
   1.216 +	return 0;
   1.217 +}
   1.218 +
   1.219 +void destroy_image(struct ham_image *img)
   1.220 +{
   1.221 +	if(img) {
   1.222 +		free(img->pixels);
   1.223 +		while(img->chglist) {
   1.224 +			void *tmp = img->chglist;
   1.225 +			img->chglist = img->chglist->next;
   1.226 +			free(tmp);
   1.227 +		}
   1.228 +	}
   1.229 +}