#include <stdint.h>
#include <stdio.h>

#include <png.h>

/* Maximum admitted PNG pixels with deterministic, poorly compressible RGB. */
int
main(int argc, char **argv)
{
	png_structp png;
	png_infop info;
	FILE *image, *raw;
	unsigned char row[2048 * 3];
	uint32_t state = 1;
	size_t x, y;
	volatile int rc = 1;

	if (argc != 3)
		return 2;
	image = fopen(argv[1], "wb");
	if (image == NULL)
		return 1;
	raw = fopen(argv[2], "wb");
	if (raw == NULL) {
		fclose(image);
		return 1;
	}
	png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
	if (png == NULL)
		goto out;
	info = png_create_info_struct(png);
	if (info == NULL) {
		png_destroy_write_struct(&png, NULL);
		goto out;
	}
	if (setjmp(png_jmpbuf(png))) {
		png_destroy_write_struct(&png, &info);
		goto out;
	}
	png_init_io(png, image);
	png_set_IHDR(png, info, 2048, 2048, 8, PNG_COLOR_TYPE_RGB,
	    PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
	png_set_filter(png, PNG_FILTER_TYPE_BASE, PNG_FILTER_NONE);
	png_write_info(png, info);
	for (y = 0; y < 2048; y++) {
		for (x = 0; x < sizeof(row); x++) {
			state ^= state << 13;
			state ^= state >> 17;
			state ^= state << 5;
			row[x] = (unsigned char)state;
		}
		png_write_row(png, row);
		if (fwrite(row, 1, sizeof(row), raw) != sizeof(row)) {
			png_destroy_write_struct(&png, &info);
			goto out;
		}
	}
	png_write_end(png, NULL);
	png_destroy_write_struct(&png, &info);
	rc = 0;
out:
	if (fclose(image) != 0)
		rc = 1;
	if (fclose(raw) != 0)
		rc = 1;
	return rc;
}
