/* wasp - copyright 1991 by Steven Reiz
 * see wasp.c for further info,
 * ppm.c, 24/7/91, 22/10/91 - 25/10/91
 */

#include "wasp.h"
/* #include "ppm.sh" */

#ifdef __STDC__
read_ppm(void)
#else
read_ppm()
#endif
{
	char c;
	int y;
	long depth;
	char typ[3];

	cread(typ, 3);
	if (typ[0]!='P' || typ[1]<'0' || typ[1]>'9' || typ[2]!='\n') {
		lseek(infd, 0L, 0);
		return 0;
	}
	typ[2]='\0';
	xsz=getn(' ');
	ysz=getn('\n');
	depth=getn('\n')+1;
	printf("PPM/%s input; %ld x %ld, depth: %ld\n", typ, xsz, ysz, depth);
	fflush(stdout);
	if (!outfilename)
		exit(0);
	if (depth!=256) {
		printe("depths other than 256 are not supported\n");
		exit(1);
	}
	rgb=Malloc(ysz*sizeof(u_short *));
	for (y=0; y<ysz; ++y)
	        rgb[y]=Malloc(xsz*sizeof(u_short));
	switch (typ[1]) {
	case '5':
		read_grey8();
	break;
	case '6':
		read_rgb24();
	break;
	default:
		printe("types other than P5 or P6 are not supported\n");
		exit(1);
	break;
	}
	return 1;
}


#ifdef __STDC__
write_ppm(void)
#else
write_ppm()
#endif
{
	printf("PPM/P6 output; %ld x %ld, depth: 256\n", xsz, ysz);
	cwrite("P6\n", 3);
	putn((int)xsz);
	cwrite(" ", 1);
	putn((int)ysz);
	cwrite("\n255\n", 5);
	write_rgb24();
	erase_counter("PPM file written, %ld bytes", lseek(outfd, 0L, 1));
}


#ifdef __STDC__
PRIVATE int getn(char terminator)
#else
PRIVATE int getn(terminator)
char terminator;
#endif
{
	int n;
	char c;

	n=0;
	while (1) {
		cread(&c, 1);
		if (c<'0' || c>'9')
			break;
		n=10*n+c-'0';
	}
	assert(c==terminator);
	return n;
}


#ifdef __STDC__
PRIVATE int putn(int n)
#else
PRIVATE int getn(n)
int n;
#endif
{
	int i;
	char line[20];

	i=19;
	do {
		line[i--]=n%10+'0';
		n/=10;
	} while (n>0);
	cwrite(line+i+1, 19-i);
}


#ifdef __STDC__
read_rgb24(void)
#else
read_rgb24()
#endif
{
	char *buf;
	int width, color;
	int y, x;
	char *bufp;
	u_short *p;
	
	width=xsz*3;
	buf=Malloc(width);
	init_counter(0, (int)ysz, 10, NULL);
	for (y=0; y<ysz; ++y) {
		counter();
		cread(buf, width);
		x=xsz-1;
		bufp=buf;
		p=rgb[y];
		do {
			color=(*bufp++ & 0xf0)<<4;
			color|= *bufp++ & 0xf0;
			color|=(*bufp++ >>4) & 0x0f;
			*p++ =color;
		} while (--x>=0);
	}
	erase_counter(NULL);
	free(buf);
}


#ifdef __STDC__
read_grey8(void)
#else
read_grey8()
#endif
{
	char *buf;
	int color;
	int y, x;
	char *bufp;
	u_short *p;
	
	buf=Malloc((int)xsz);
	init_counter(0, (int)ysz, 10, NULL);
	for (y=0; y<ysz; ++y) {
		counter();
		cread(buf, (int)xsz);
		x=xsz-1;
		bufp=buf;
		p=rgb[y];
		do {
			color= *bufp++ & 0xf0;
			color|=(color<<4)|(color>>4);
			*p++ =color;
		} while (--x>=0);
	}
	erase_counter(NULL);
	free(buf);
}


#ifdef __STDC__
write_rgb24(void)
#else
write_rgb24()
#endif
{
	char *buf;
	int width, color;
	int y, x;
	char *bufp;
	u_short *p;
	
	width=xsz*3;
	buf=Malloc(width);
	init_counter(0, (int)ysz, 10, NULL);
	for (y=0; y<ysz; ++y) {
		counter();
		x=xsz-1;
		bufp=buf;
		p=rgb[y];
		do {
			color= *p++;
			*bufp++ =(color>>4)&0xf0;
			*bufp++ =color&0xf0;
			*bufp++ =color<<4;
		} while (--x>=0);
		cwrite(buf, width);
	}
	free(buf);
}
