/*
 *  Load an image, convert all non-black pixels to transparent pixels.
 *  Save it to a new name.
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "app.h"

int main(int argc, char *argv[])
{
	int i;
	Image *img = NULL;
	Image *img2;
	int x, y;
	Rect area;
	Colour c;
	char *filename;

	for (i = 1; argv[i] != NULL; i++)
	{
		if (img)
			app_del_image(img);
		img = app_read_image(argv[i], 32);
		if (! img)
			continue;
		area = app_get_image_area(img);

		img2 = app_new_image(area.width, area.height, 32);
		if (img2 == NULL)
			continue;

		for (y = 0; y < area.height; y++)
		{
		 for (x = 0; x < area.width; x++)
		 {
			c = img->data32[y][x];
			if (c.red != 0 || c.green != 0 || c.blue != 0)
				c.alpha = 255;
			img2->data32[y][x] = c;
		 }
		}

		filename = app_alloc(strlen(argv[i])+20);
		sprintf(filename, "%s_trans.png", argv[i]);
		if (app_write_image(img2, filename))
			printf("Created image file: %s\n", filename);
		app_free(filename);
	}

	return 0;
}
