#include <assert.h>
#include <stdio.h>
#include <string.h>

#include <png.h>

#include "pdfw.h"
#include "util.h"

int
main(int argc, char **argv)
{
	pdfw *p;
	int w, h;
	char opaque[1024], output[1024];
	const unsigned char rgb[] = { 10, 20, 30, 200, 210, 220 };
	const unsigned char rgba[] = { 10, 20, 30, 0, 40, 50, 60, 128, 70, 80, 90, 255 };
	const unsigned char solid[] = { 1, 2, 3, 255, 4, 5, 6, 255, 7, 8, 9, 255, 10, 11, 12, 255 };
	const unsigned char gray[] = { 0, 50, 100, 200, 255 };
	const unsigned char indices[] = { 0, 1, 0, 1, 0, 1 };
	const unsigned char palette[] = { 10, 20, 30, 0, 200, 210, 220, 128 };
	const struct {
		png_uint_32 format;
		const unsigned char *data;
		int alpha;
	} variants[] = {
		{ PNG_FORMAT_RGB, rgb, 0 },
		{ PNG_FORMAT_RGBA, rgba, 1 },
		{ PNG_FORMAT_RGBA, solid, 0 },
		{ PNG_FORMAT_GRAY, gray, 0 },
		{ PNG_FORMAT_RGBA_COLORMAP, indices, 1 }
	};
	size_t i, x;
	FILE *colors, *alpha;
	png_image png;

	assert(argc == 4);
	snprintf(opaque, sizeof(opaque), "%s/asset-0", argv[3]);
	snprintf(output, sizeof(output), "%s/image.pdf", argv[3]);
	assert((p = pdfw_open(output)) != NULL);
	assert(pdfw_page(p, 595.28, 841.89) == 0);
	assert(pdfw_image_size(p, argv[1], &w, &h) == 0 && w == 4 && h == 2);
	assert(copy_file(argv[1], opaque) == 0);
	assert(pdfw_image_size(p, opaque, &w, &h) == 0 &&
	    "extensionless frozen JPEG must retain its format");
	assert(w == 4 && h == 2);
	assert(pdfw_image(p, opaque, 50, 600, 200, 100) == 0);
	/* Same path deliberately has a misleading JPEG suffix for the PNG case. */
	snprintf(opaque, sizeof(opaque), "%s/asset-1.jpg", argv[3]);
	assert(copy_file(argv[2], opaque) == 0);
	assert(pdfw_image_size(p, opaque, &w, &h) == 0 && w == 200 && h == 200);
	assert(pdfw_image(p, opaque, 50, 300, 200, 200) == 0);
	for (i = 0; i < sizeof(variants) / sizeof(variants[0]); i++) {
		memset(&png, 0, sizeof(png));
		png.version = PNG_IMAGE_VERSION;
		png.width = (png_uint_32)i + 2;
		png.height = 1;
		png.format = variants[i].format;
		png.colormap_entries = 2;
		snprintf(opaque, sizeof(opaque), "%s/variant-%zu.png", argv[3], i + 2);
		assert(png_image_write_to_file(&png, opaque, 0, variants[i].data,
		    0, palette) != 0);
		assert(pdfw_image(p, opaque, 10, 10 + i * 20, 100, 10) == 0);
		snprintf(opaque, sizeof(opaque), "%s/variant-%zu.rgb", argv[3], i + 2);
		assert((colors = fopen(opaque, "wb")) != NULL);
		alpha = NULL;
		if (variants[i].alpha) {
			snprintf(opaque, sizeof(opaque), "%s/variant-%zu.alpha", argv[3], i + 2);
			assert((alpha = fopen(opaque, "wb")) != NULL);
		}
		for (x = 0; x < i + 2; x++) {
			const unsigned char *pixel;

			if (png.format & PNG_FORMAT_FLAG_COLORMAP)
				pixel = palette + indices[x] * 4;
			else if (png.format == PNG_FORMAT_GRAY) {
				assert(fputc(gray[x], colors) != EOF);
				assert(fputc(gray[x], colors) != EOF);
				assert(fputc(gray[x], colors) != EOF);
				continue;
			} else
				pixel = variants[i].data + x * (png.format == PNG_FORMAT_RGBA ? 4 : 3);
			assert(fwrite(pixel, 1, 3, colors) == 3);
			if (alpha != NULL)
				assert(fputc(pixel[3], alpha) != EOF);
		}
		assert(fclose(colors) == 0);
		if (alpha != NULL)
			assert(fclose(alpha) == 0);
	}
	assert(pdfw_close(p) == 0);
	puts("image content contract: all passed");
	return 0;
}
