From 96e1ff487d8991b82edc8bd0d966f89d14c20b4f Mon Sep 17 00:00:00 2001 From: Carlos Garcia Campos Date: Sun, 21 Sep 2008 14:29:30 +0200 Subject: [PATCH] [test] Add ps2png check program using libspectre --- test/Makefile.am | 8 +++++ test/ps2png.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 0 deletions(-) create mode 100644 test/ps2png.c diff --git a/test/Makefile.am b/test/Makefile.am index 8faf1fd..d8500e2 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -898,6 +898,14 @@ svg2png_LDFLAGS = $(CAIRO_TEST_UNDEFINED_LDFLAGS) svg2png_LDADD = $(LDADD) $(LIBRSVG_LIBS) endif +if CAIRO_CAN_TEST_PS_SURFACE +check_PROGRAMS += ps2png +ps2png_CFLAGS = $(LIBSPECTRE_CFLAGS) +# add LDADD, so ps2png uses "our" cairo +ps2png_LDFLAGS = $(CAIRO_TEST_UNDEFINED_LDFLAGS) +ps2png_LDADD = $(LDADD) $(LIBSPECTRE_LIBS) +endif + EXTRA_PROGRAMS += $(TESTS) $(DISABLED_TESTS) # Do a funny transition of CAIRO_TEST_TARGET through TARGETS such that diff --git a/test/ps2png.c b/test/ps2png.c new file mode 100644 index 0000000..1172e59 --- /dev/null +++ b/test/ps2png.c @@ -0,0 +1,76 @@ +#include +#include +#include +#include + +#define FAIL(msg) \ + do { fprintf (stderr, "FAIL: %s\n", msg); exit (-1); } while (0) + +int main (int argc, char *argv[]) +{ + SpectreDocument *document; + int width, height, stride; + unsigned char *pixels; + cairo_surface_t *surface; + cairo_status_t cairo_status; + const char *filename = argv[1]; + const char *output_filename = argv[2]; + + if (argc < 3 || argc > 4) + FAIL ("usage: ps2png input_file.ps output_file.png [page]"); + + document = spectre_document_new (); + spectre_document_load (document, filename); + if (spectre_document_status (document)) { + const char *err; + + err = spectre_status_to_string (spectre_document_status (document)); + spectre_document_free (document); + FAIL (err); + } + + if (argc == 4) { + SpectrePage *page; + SpectreRenderContext *rc; + const char *page_label = argv[3]; + + page = spectre_document_get_page_by_label (document, page_label); + spectre_document_free (document); + if (page == NULL) + FAIL ("page not found"); + + spectre_page_get_size (page, &width, &height); + rc = spectre_render_context_new (); + spectre_render_context_set_page_size (rc, width, height); + spectre_page_render (page, rc, &pixels, &stride); + spectre_render_context_free (rc); + if (spectre_page_status (page)) { + const char *err; + + err = spectre_status_to_string (spectre_page_status (page)); + free (pixels); + spectre_page_free (page); + + FAIL (err); + } + + spectre_page_free (page); + } else { + spectre_document_get_page_size (document, &width, &height); + spectre_document_render (document, &pixels, &stride); + spectre_document_free (document); + } + + surface = cairo_image_surface_create_for_data (pixels, + CAIRO_FORMAT_RGB24, + width, height, + stride); + cairo_status = cairo_surface_write_to_png (surface, output_filename); + cairo_surface_destroy (surface); + free (pixels); + + if (cairo_status) + FAIL (cairo_status_to_string (cairo_status)); + + return 0; +} -- 1.5.6.3