diff options
author | Vincent Torri <vincent.torri@gmail.com> | 2011-05-19 12:29:34 +0000 |
---|---|---|
committer | Vincent Torri <vincent.torri@gmail.com> | 2011-05-19 12:29:34 +0000 |
commit | 1de11a62db44ea7cebc7e4af92b4044656f4da9e (patch) | |
tree | f432f37d312e866c3435e9a9fb648a464d5b1fb8 | |
parent | 8dd255f071237a4bcde9c930eb0ea4b401a7abfa (diff) | |
download | evas_generic_loaders-1de11a62db44ea7cebc7e4af92b4044656f4da9e.tar.gz |
Add PostScript loader (using libspectre)
SVN revision: 59532
-rw-r--r-- | configure.ac | 5 | ||||
-rw-r--r-- | src/bin/Makefile.am | 4 | ||||
-rw-r--r-- | src/bin/ps/Makefile.am | 22 | ||||
-rw-r--r-- | src/bin/ps/main.c | 265 |
4 files changed, 296 insertions, 0 deletions
diff --git a/configure.ac b/configure.ac index da8e13d..18b513b 100644 --- a/configure.ac +++ b/configure.ac @@ -62,7 +62,10 @@ PKG_CHECK_MODULES(EINA, [eina >= 1.0.0]) PKG_CHECK_MODULES([POPPLER], [poppler >= 0.12], [have_poppler="yes"], [have_poppler="no"]) +PKG_CHECK_MODULES([SPECTRE], [libspectre], [have_ps="yes"], [have_ps="no"]) + AM_CONDITIONAL([HAVE_PDF], [test "x${have_poppler}" = "xyes"]) +AM_CONDITIONAL([HAVE_PS], [test "x${have_ps}" = "xyes"]) ### Checks for header files @@ -114,6 +117,7 @@ src/bin/Makefile src/bin/common/Makefile src/bin/xcf/Makefile src/bin/pdf/Makefile +src/bin/ps/Makefile ]) AC_OUTPUT @@ -134,6 +138,7 @@ echo echo "Loaders:" echo " XCF..................: yes" echo " PDF..................: ${have_poppler}" +echo " PS...................: ${have_ps}" echo echo "Compilation............: make (or gmake)" echo " CPPFLAGS.............: $CPPFLAGS" diff --git a/src/bin/Makefile.am b/src/bin/Makefile.am index 313e122..3ef298a 100644 --- a/src/bin/Makefile.am +++ b/src/bin/Makefile.am @@ -6,3 +6,7 @@ xcf if HAVE_PDF SUBDIRS += pdf endif + +if HAVE_PS +SUBDIRS += ps +endif diff --git a/src/bin/ps/Makefile.am b/src/bin/ps/Makefile.am new file mode 100644 index 0000000..f0c3ba5 --- /dev/null +++ b/src/bin/ps/Makefile.am @@ -0,0 +1,22 @@ +MAINTAINERCLEANFILES = Makefile.in + +AM_CPPFLAGS = \ +-I$(top_srcdir) \ +-I$(top_srcdir)/src \ +-I$(top_srcdir)/src/bin \ +-I$(top_srcdir)/src/bin/common \ +-I$(top_srcdir)/src/bin/ps \ +-DPACKAGE_BIN_DIR=\"$(bindir)\" \ +-DPACKAGE_LIB_DIR=\"$(libdir)\" \ +-DPACKAGE_DATA_DIR=\"$(datadir)/$(PACKAGE)\" \ +@EINA_CFLAGS@ \ +@SPECTRE_CFLAGS@ + +bin_PROGRAMS = evas_image_loader.ps + +evas_image_loader_ps_SOURCES = \ +main.c \ +$(top_srcdir)/src/bin/common/shmfile.c +evas_image_loader_ps_CFLAGS = +evas_image_loader_ps_LDADD = @SPECTRE_LIBS@ @EINA_LIBS@ @SHM_OPEN_LIBS@ +evas_image_loader_ps_LDFLAGS = diff --git a/src/bin/ps/main.c b/src/bin/ps/main.c new file mode 100644 index 0000000..8960aef --- /dev/null +++ b/src/bin/ps/main.c @@ -0,0 +1,265 @@ +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <sys/mman.h> +#include <fcntl.h> + +#include <libspectre/spectre.h> + +#include <Eina.h> + +#include "shmfile.h" + +#define DATA32 unsigned int + +#define PS_DBG + +#ifdef PS_DBG +#define D(fmt, args...) fprintf(stderr, fmt, ## args) +#else +#define D(fmt, args...) +#endif + + +SpectreDocument *psdoc; +unsigned int page_count; + +SpectrePage *page; + +int width = 0; +int height = 0; +void *data = NULL; +double dpi = -1.0; + +#define DEF_DPI 72.0 + +static Eina_Bool +_spectre_init(const char *file, int page_nbr, int size_w, int size_h) +{ + double w, h; + int ww, hh; + SpectreOrientation rot; + SpectreStatus status; + + if (!file || !*file) + return EINA_FALSE; + + if (page_nbr < 0) + return EINA_FALSE; + + if (!eina_init()) + return EINA_FALSE; + + psdoc = spectre_document_new(); + if (!psdoc) + goto shutdown_eina; + + spectre_document_load(psdoc, file); + status = spectre_document_status (psdoc); + if (status != SPECTRE_STATUS_SUCCESS) + { + D("[ps] %s\n", spectre_status_to_string(status)); + goto free_psdoc; + } + + page_count = spectre_document_get_n_pages(psdoc); + status = spectre_document_status(psdoc); + if (status != SPECTRE_STATUS_SUCCESS) + { + D("[eps] %s\n", spectre_status_to_string (status)); + goto free_psdoc; + } + + if (page_nbr >= page_count) + goto free_psdoc; + + /* load the page */ + + page = spectre_document_get_page(psdoc, page_nbr); + status = spectre_document_status(psdoc); + if (status != SPECTRE_STATUS_SUCCESS) + { + D("[eps] %s\n", spectre_status_to_string (status)); + goto free_psdoc; + } + + spectre_page_get_size(page, &ww, &hh); + w = ww; + h = hh; + rot = spectre_page_get_orientation(page); + + if ((rot == SPECTRE_ORIENTATION_LANDSCAPE) || (rot == SPECTRE_ORIENTATION_REVERSE_LANDSCAPE)) + { + double t; + // swap width & height + t = w; w = h; h = t; + } + + if ((size_w > 0) || (size_h > 0)) + { + double w2 = w, h2 = h; + + w2 = size_w; + h2 = (size_w * h) / w; + if (h2 > size_h) + { + h2 = size_h; + w2 = (size_h * w) / h; + } + D("XXXXXXXXXXXXXXXXXXXXx %3.3fx%3.3f\n", w2, h2); + if (w2 > h2) dpi = (w2 * DEF_DPI) / w; + else dpi = (h2 * DEF_DPI) / h; + } + + if (dpi > 0.0) + { + w = (w * dpi) / DEF_DPI; + h = (h * dpi) / DEF_DPI; + } + width = w; + height = h; + + return EINA_TRUE; + + free_page: + spectre_page_free(page); + free_psdoc: + spectre_document_free(psdoc); + shutdown_eina: + eina_shutdown(); + + return EINA_FALSE; +} + +static void +_spectre_shutdown() +{ + spectre_page_free(page); + spectre_document_free(psdoc); + eina_shutdown(); +} + +static void +_spectre_load_image(int size_w, int size_h) +{ + SpectreRenderContext *rc; + unsigned char *psdata; + int stride; + DATA32 *src; + DATA32 *dst; + int yy; + SpectreStatus status; + + rc = spectre_render_context_new(); + if (!rc) + return; + + spectre_page_render(page, rc, &psdata, &stride); + spectre_render_context_set_page_size (rc, width, height); + status = spectre_page_status(page); + if (status != SPECTRE_STATUS_SUCCESS) + { + D("[eps] %s\n", spectre_status_to_string (status)); + return; + } + + shm_alloc(width * height * sizeof(DATA32)); + if (!shm_addr) return; + data = shm_addr; + + if (stride == 4 * width) + memcpy(data, psdata, height * stride); + else + { + src = (DATA32 *)psdata; + dst = (DATA32 *)data; + for (yy = 0; yy < height; src += stride, dst += width, ++yy) + memcpy (dst, src, width * 4); + } + + spectre_render_context_free(rc); +} + +int +main(int argc, char **argv) +{ + char *file; + int i; + int size_w = 0, size_h = 0; + int head_only = 0; + int page = 0; + + if (argc < 2) return -1; + // file is ALWAYS first arg, other options come after + file = argv[1]; + for (i = 2; i < argc; i++) + { + if (!strcmp(argv[i], "-head")) + // asked to only load header, not body/data + head_only = 1; + else if (!strcmp(argv[i], "-key")) + { + i++; + page = atoi(argv[i]); + i++; + } + else if (!strcmp(argv[i], "-opt-scale-down-by")) + { // not used by ps loader + i++; + // int scale_down = atoi(argv[i]); + } + else if (!strcmp(argv[i], "-opt-dpi")) + { + i++; + dpi = ((double)atoi(argv[i])) / 1000.0; // dpi is an int multiplied by 1000 (so 72dpi is 72000) + i++; + } + else if (!strcmp(argv[i], "-opt-size")) + { // not used by ps loader + i++; + size_w = atoi(argv[i]); + i++; + size_h = atoi(argv[i]); + } + } + + D("_spectre_init_file\n"); + D("dpi....: %f\n", dpi); + D("page...: %d\n", page); + + if (!_spectre_init(file, page, size_w, size_h)) + return -1; + D("poppler_file_init done\n"); + + D("dpi2...: %f\n", dpi); + if (!head_only) + { + _spectre_load_image(size_w, size_h); + } + + D("size...: %ix%i\n", width, height); + D("alpha..: 1\n"); + + printf("size %i %i\n", width, height); + printf("alpha 0\n"); + + if (!head_only) + { + if (shm_fd >= 0) printf("shmfile %s\n", shmfile); + else + { + // could also to "tmpfile %s\n" like shmfile but just + // a mmaped tmp file on the system + printf("data\n"); + fwrite(data, width * height * sizeof(DATA32), 1, stdout); + } + shm_free(); + } + else + printf("done\n"); + + _spectre_shutdown(); + + return 0; +} |