Skip to content

Commit

Permalink
[test] Add ps2png check program using libspectre
Browse files Browse the repository at this point in the history
Add a simple program to compliment pdf2png and svg2png.
  • Loading branch information
Carlos Garcia Campos authored and Chris Wilson committed Oct 9, 2008
1 parent 3e6afb3 commit 8ac8e8c
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 0 deletions.
1 change: 1 addition & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ if test "x$use_ps" = "xyes"; then
fi

AM_CONDITIONAL(CAIRO_CAN_TEST_PS_SURFACE, test "x$test_ps" = "xyes")
AM_CONDITIONAL(BUILD_PS2PNG, test "x$any2ppm_ps" = "xyes")
AC_SUBST(LIBSPECTRE_CFLAGS)
AC_SUBST(LIBSPECTRE_LIBS)

Expand Down
8 changes: 8 additions & 0 deletions test/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -1094,6 +1094,14 @@ svg2png_LDFLAGS = $(CAIRO_TEST_UNDEFINED_LDFLAGS)
svg2png_LDADD = $(LDADD) $(LIBRSVG_LIBS)
endif

if BUILD_PS2PNG
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
Expand Down
113 changes: 113 additions & 0 deletions test/ps2png.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* Copyright © 2008 Carlos Garcia Campos
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* Author: Carlos Garcia Campos <carlosgc@gnome.org>
*/

#include <stdio.h>
#include <stdlib.h>
#include <cairo.h>
#include <libspectre/spectre.h>

#define FAIL(msg) \
do { fprintf (stderr, "FAIL: %s\n", msg); exit (-1); } while (0)

static const char *
_spectre_render_page (const char *filename,
const char *page_label,
cairo_surface_t **surface_out)
{
static const cairo_user_data_key_t key;

SpectreDocument *document;
SpectreStatus status;
int width, height, stride;
unsigned char *pixels;
cairo_surface_t *surface;

document = spectre_document_new ();
spectre_document_load (document, filename);
status = spectre_document_status (document);
if (status) {
spectre_document_free (document);
return spectre_status_to_string (status);
}

if (page_label) {
SpectrePage *page;
SpectreRenderContext *rc;

page = spectre_document_get_page_by_label (document, page_label);
spectre_document_free (document);
if (page == NULL)
return "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);
status = spectre_page_status (page);
spectre_page_free (page);
if (status) {
free (pixels);
return spectre_status_to_string (status);
}
} 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_surface_set_user_data (surface, &key,
pixels, (cairo_destroy_func_t) free);
*surface_out = surface;
return NULL;
}

int main
(int argc, char *argv[])
{
const char *err;
cairo_surface_t *surface = NULL; /* silence compiler warning */
cairo_status_t status;

if (argc < 3 || argc > 4)
FAIL ("usage: ps2png input_file.ps output_file.png [page]");

err = _spectre_render_page (argv[1], argv[3], &surface);
if (err != NULL)
FAIL (err);

status = cairo_surface_write_to_png (surface, argv[2]);
cairo_surface_destroy (surface);

if (status != CAIRO_STATUS_SUCCESS)
FAIL (cairo_status_to_string (status));

return 0;
}

0 comments on commit 8ac8e8c

Please sign in to comment.