#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <sys/stat.h>
#include <libps/pslib.h>
#ifdef MEMORY_DEBUGGING
#include <libps/pslib-mp.h>
#endif

void errorhandler(PSDoc *p, int error, const char *str, void *data) {
	fprintf(stderr, "PSLib: %s\n", str);
}

size_t writeproc(PSDoc *p, void *data, size_t size) {
	return fwrite(data, 1, size, stdout);
}

#define LEFT_BORDER 50
#define EXAMPLE_BOX_HEIGHT 190
#define EXAMPLE_BOX_WIDTH  150
#define EXAMPLE_BOX_TITLE_HEIGHT  40

int main() {
	PSDoc *ps;
	int psfont;
	int psimage;
	int pstemplate;
	int x, y, i, j;
	float height, scale;

	PS_boot();
#ifdef MEMORY_DEBUGGING
	PS_mp_init();
#endif

#ifdef MEMORY_DEBUGGING
	ps = PS_new2(errorhandler, PS_mp_malloc, PS_mp_realloc, PS_mp_free, NULL);
#else
	ps = PS_new();
#endif

	if (0 > PS_open_file(ps, "imagereuse.ps")) {
		printf("Cannot open PostScript file\n");
		exit(1);
	}

	PS_set_parameter(ps, "warning", "true");
	PS_set_parameter(ps, "imageencoding", "hex");
	PS_set_parameter(ps, "imagereuse", "true");

	PS_set_info(ps, "Creator", __FILE__);
	PS_set_info(ps, "Author", "Uwe Steinmann");
	PS_set_info(ps, "Title", "Image Reuse Example");
	PS_set_info(ps, "Keywords", "Image Picture PNG Jpeg Reuse");

	psimage = PS_open_image_file(ps, "png", "debian.png", NULL, 0);
	pstemplate = PS_begin_template(ps, 30.0, 30.0);
	PS_place_image(ps, psimage, 0, 0, 0.5);
	PS_moveto(ps, 0, 0);
	PS_lineto(ps, 30, 30);
	PS_moveto(ps, 0, 30);
	PS_lineto(ps, 30, 0);
	PS_stroke(ps);
	PS_end_template(ps);
	PS_close_image(ps, psimage);

	PS_begin_page(ps, 596, 842);
	psfont = PS_findfont(ps, "Helvetica", "", 0);
	PS_setfont(ps, psfont, 8.0);

	psimage = PS_open_image_file(ps, "eps", "picture.eps", NULL, 0);
	height = PS_get_value(ps, "imageheight", (float) psimage);
	y = 0;
	for(i=0; i<10; i++) {
		scale = 1.0/(i+1);
		PS_place_image(ps, psimage, 30, y+30, scale);
		y += scale*height + 20;
	}
	PS_close_image(ps, psimage);

	psimage = PS_open_image_file(ps, "png", "debian.png", NULL, 0);
	height = PS_get_value(ps, "imageheight", (float) psimage);
	y = 0;
	for(i=0; i<10; i++) {
		scale = 3.0/(i+1);
		PS_place_image(ps, psimage, 350, y+30, scale);
		y += scale*height + 20;
	}
	PS_close_image(ps, psimage);


	PS_end_page(ps);

	PS_set_parameter(ps, "imagereuse", "false");

	PS_begin_page(ps, 596, 842);

	psfont = PS_findfont(ps, "Helvetica", "", 0);
	PS_setfont(ps, psfont, 18.0);
	PS_show_xy(ps, "This page is created with imagereuse = false", 20, 815);

	psimage = PS_open_image_file(ps, "png", "debian.png", NULL, 0);
	x = 2;
	for(j=0; j<30; j++) {
		y = 2;
		for(i=0; i<40; i++) {
			PS_place_image(ps, psimage, x, y, 0.2);
			y += 20;
		}
		x += 20;
	}
	PS_close_image(ps, psimage);

	PS_end_page(ps);

	PS_set_parameter(ps, "imagereuse", "true");

	PS_begin_page(ps, 596, 842);

	psfont = PS_findfont(ps, "Helvetica", "", 0);
	PS_setfont(ps, psfont, 18.0);
	PS_show_xy(ps, "This page is created with imagereuse = true", 20, 815);
	psimage = PS_open_image_file(ps, "png", "debian.png", NULL, 0);
	x = 2;
	for(j=0; j<30; j++) {
		y = 2;
		for(i=0; i<40; i++) {
			PS_place_image(ps, psimage, x, y, 0.2);
			y += 20;
		}
		x += 20;
	}
	PS_close_image(ps, psimage);

	PS_end_page(ps);

	PS_begin_page(ps, 596, 842);

	PS_place_image(ps, pstemplate, 20.0, 20.0, 1.0);

	PS_end_page(ps);

	PS_close(ps);
	PS_delete(ps);

#ifdef MEMORY_DEBUGGING 
	PS_mp_list_unfreed();
#endif  

	PS_shutdown();
	exit(0);
}
