#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <X11/Xlib.h>

struct color
{
	int	red;
	int	green;
	int	blue;
};


main(int argc, char **argv)
{
	struct color	tc;
	int		tindex = -1;
	
	int		i, n;
	unsigned char	*data;

	FILE	*fp;
	int	width;
	int	height;
	XColor	colors[256];
	int	transparent;

	unsigned char	red[256];
	unsigned char	green[256];
	unsigned char	blue[256];

	if (argc != 4)
	{
		fprintf(stderr, "usage: tgif red green blue\n");
		exit(1);
	}

	tc.red = atoi(argv[1]);
	tc.green = atoi(argv[2]);
	tc.blue = atoi(argv[3]);
	
	data = (unsigned char *)ReadGIF(stdin, &width, &height, colors, &transparent);
	fprintf(stderr, "image: %dx%d\n", width, height);

	for (i = 0; i < 256; i++)
	{
		red[i] = colors[i].red;
		green[i] = colors[i].green;
		blue[i] = colors[i].blue;

		if (red[i] == tc.red && green[i] == tc.green && blue[i] == tc.blue)
		{
			tindex = i;
		}
	}

	if (tindex == -1)
	{
		fprintf(stderr, "transparent color not found.\n");
		exit(1);
	}
	else
	{
		fprintf(stderr, "transparent index = %d\n", tindex);
	}
	
	n = WriteGIF(stdout, data, /* ptype */0, width, height, red, green, blue,
		/* # of colors */256, /* colorstyle */0,
		/* comment */0,
		/* transparent */ tindex >= 0 ? tindex : -1);
	
	printf("n=%d\n", n);
	fclose(fp);

	exit(0);
}
