/*** negate.cpp  $Revision:   1.0  $  $Date:   30 Aug 1989  9:33:24  $
*
*   convert pixels from v to 1-v
*
***/
#include <stdio.h>
#include "image.h"


void negate_image(IMAGE *src, IMAGE *dst);

int main(int argc, char *argv[])
{
	IMAGE *fp,*gp;
	char *filename;
	
	if (argc<2) {
		printf("Usage: negate image_file\n");
		return -1;
	}
	filename = argv[1];
	fp = open_image(filename);
	if (!fp) return -1;
	printf("image file: %s\n",filename);
	printf("image size: %d x %d\n",fp->hlen,fp->vlen);
	negate_image(fp,fp);
	return 0;
}

void negate_image(IMAGE *src, IMAGE *dst)
{
	pixel *buf;
	float *fbuf;
	pixel color;
	int i, j;
	int r, g, b;
	PIXEL_TYPE type;
	buf = make_buffer(src);
	fbuf = (float *) buf;
	type = src->type;
	for (j=0; j<dst->vlen; j++) {
		get_line(src,j,buf,type);
		for (i=0; i<dst->hlen; i++) {
			switch (type) {
			case PIX_BYTE:
				buf[i] = 255 - buf[i];
				break;
			case PIX_FLOAT:
				fbuf[i] = 1.0 - fbuf[i];
				break;
			case PIX_RGB:
				color = buf[i];
				r = GetRValue(color);
				g = GetGValue(color);
				b = GetBValue(color);
				buf[i] = RGB(255-r,255-g,255-b);
				break;
			}
		}
		put_line(dst,j,buf,type);
	}
	free_buffer(buf);
}
