[libgxps] tools: Add xpstojpeg tool



commit f2bca61794f94998d4774eca9f6977c31732e046
Author: Carlos Garcia Campos <carlosgc gnome org>
Date:   Sun Oct 30 14:32:00 2011 +0100

    tools: Add xpstojpeg tool

 configure.ac                |    1 +
 tools/Makefile.am           |   23 +++++++
 tools/gxps-jpeg-converter.c |   65 +++++++++++++++++++
 tools/gxps-jpeg-converter.h |   41 ++++++++++++
 tools/gxps-jpeg-writer.c    |  148 +++++++++++++++++++++++++++++++++++++++++++
 tools/gxps-jpeg-writer.h    |   44 +++++++++++++
 6 files changed, 322 insertions(+), 0 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index e48335a..8364b23 100644
--- a/configure.ac
+++ b/configure.ac
@@ -117,6 +117,7 @@ if test x$with_libjpeg != xno && test -z "$LIBJPEG"; then
   fi
 fi
 
+AM_CONDITIONAL(HAVE_LIBJPEG, test ! -z "$LIBJPEG")
 AC_SUBST(LIBJPEG)
 
 dnl libtiff
diff --git a/tools/Makefile.am b/tools/Makefile.am
index 777d69c..ccb7e05 100644
--- a/tools/Makefile.am
+++ b/tools/Makefile.am
@@ -57,6 +57,29 @@ xpstopng_LDADD = \
 	$(LIBPNG_LIBS)
 endif # HAVE_LIBPNG
 
+if HAVE_LIBJPEG
+bin_PROGRAMS += xpstojpeg
+
+xpstojpeg_SOURCES = \
+	gxps-converter-main.c	\
+	gxps-jpeg-converter.c	\
+	gxps-jpeg-converter.h	\
+	gxps-jpeg-writer.c	\
+	gxps-jpeg-writer.h
+
+xpstojpeg_CPPFLAGS = \
+	$(tools_cppflags)				\
+	-DCONVERTER_TYPE=GXPS_TYPE_JPEG_CONVERTER	\
+	-DCONVERTER_HEADER=gxps-jpeg-converter.h
+
+xpstojpeg_CFLAGS = \
+	$(tools_cflags)
+
+xpstojpeg_LDADD = \
+	libgxpstools.la		\
+	$(LIBJPEG)
+endif # HAVE_LIBJPEG
+
 if HAVE_CAIRO_PDF
 bin_PROGRAMS += xpstopdf
 
diff --git a/tools/gxps-jpeg-converter.c b/tools/gxps-jpeg-converter.c
new file mode 100644
index 0000000..0f29e83
--- /dev/null
+++ b/tools/gxps-jpeg-converter.c
@@ -0,0 +1,65 @@
+/* GXPSJpegConverter
+ *
+ * Copyright (C) 2011  Carlos Garcia Campos <carlosgc gnome org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#include <config.h>
+
+#include "gxps-jpeg-converter.h"
+#include "gxps-jpeg-writer.h"
+#include <libgxps/gxps.h>
+
+struct _GXPSJpegConverter {
+	GXPSImageConverter parent;
+};
+
+struct _GXPSJpegConverterClass {
+	GXPSImageConverterClass parent_class;
+};
+
+G_DEFINE_TYPE (GXPSJpegConverter, gxps_jpeg_converter, GXPS_TYPE_IMAGE_CONVERTER)
+
+static const gchar *
+gxps_jpeg_converter_get_extension (GXPSConverter *converter)
+{
+        return "jpg";
+}
+
+static void
+gxps_jpeg_converter_end_page (GXPSConverter *converter)
+{
+        GXPSImageConverter *image_converter = GXPS_IMAGE_CONVERTER (converter);
+
+        if (!image_converter->writer)
+                image_converter->writer = gxps_jpeg_writer_new ();
+
+        GXPS_CONVERTER_CLASS (gxps_jpeg_converter_parent_class)->end_page (converter);
+}
+
+static void
+gxps_jpeg_converter_init (GXPSJpegConverter *converter)
+{
+}
+
+static void
+gxps_jpeg_converter_class_init (GXPSJpegConverterClass *klass)
+{
+        GXPSConverterClass *converter_class = GXPS_CONVERTER_CLASS (klass);
+
+        converter_class->get_extension = gxps_jpeg_converter_get_extension;
+        converter_class->end_page = gxps_jpeg_converter_end_page;
+}
diff --git a/tools/gxps-jpeg-converter.h b/tools/gxps-jpeg-converter.h
new file mode 100644
index 0000000..142da70
--- /dev/null
+++ b/tools/gxps-jpeg-converter.h
@@ -0,0 +1,41 @@
+/* GXPSJpegConverter
+ *
+ * Copyright (C) 2011  Carlos Garcia Campos <carlosgc gnome org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#ifndef __GXPS_JPEG_CONVERTER_H__
+#define __GXPS_JPEG_CONVERTER_H__
+
+#include "gxps-image-converter.h"
+
+G_BEGIN_DECLS
+
+#define GXPS_TYPE_JPEG_CONVERTER           (gxps_jpeg_converter_get_type ())
+#define GXPS_JPEG_CONVERTER(obj)           (G_TYPE_CHECK_INSTANCE_CAST (obj, GXPS_TYPE_JPEG_CONVERTER, GXPSJpegConverter))
+#define GXPS_JPEG_CONVERTER_CLASS(cls)     (G_TYPE_CHECK_CLASS_CAST (cls, GXPS_TYPE_JPEG_CONVERTER, GXPSJpegConverterClass))
+#define GXPS_IS_JPEG_CONVERTER(obj)        (G_TYPE_CHECK_INSTANCE_TYPE (obj, GXPS_TYPE_JPEG_CONVERTER))
+#define GXPS_IS_JPEG_CONVERTER_CLASS(obj)  (G_TYPE_CHECK_CLASS_TYPE (obj, GXPS_TYPE_JPEG_CONVERTER))
+#define GXPS_JPEG_CONVERTER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GXPS_TYPE_JPEG_CONVERTER, GXPSJpegConverterClass))
+
+typedef struct _GXPSJpegConverter        GXPSJpegConverter;
+typedef struct _GXPSJpegConverterClass   GXPSJpegConverterClass;
+
+GType gxps_jpeg_converter_get_type (void);
+
+G_END_DECLS
+
+#endif /* __GXPS_JPEG_CONVERTER_H__ */
diff --git a/tools/gxps-jpeg-writer.c b/tools/gxps-jpeg-writer.c
new file mode 100644
index 0000000..4dbda45
--- /dev/null
+++ b/tools/gxps-jpeg-writer.c
@@ -0,0 +1,148 @@
+/* GXPSJpegWriter
+ *
+ * Copyright (C) 2011  Carlos Garcia Campos <carlosgc gnome org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#include <config.h>
+
+#include "gxps-jpeg-writer.h"
+#include <jpeglib.h>
+#include <setjmp.h>
+#include <stdint.h>
+#include <string.h>
+
+struct _GXPSJpegWriter {
+	GObject parent;
+
+        guchar                     *row_buffer;
+        struct jpeg_compress_struct cinfo;
+        struct jpeg_error_mgr       error_mgr;
+};
+
+struct _GXPSJpegWriterClass {
+	GObjectClass parent_class;
+};
+
+static void gxps_jpeg_writer_image_writer_iface_init (GXPSImageWriterInterface *iface);
+
+G_DEFINE_TYPE_WITH_CODE (GXPSJpegWriter, gxps_jpeg_writer, G_TYPE_OBJECT,
+                         G_IMPLEMENT_INTERFACE (GXPS_TYPE_IMAGE_WRITER,
+                                                gxps_jpeg_writer_image_writer_iface_init))
+
+static void
+gxps_jpeg_writer_init (GXPSJpegWriter *jpeg_writer)
+{
+}
+
+static void
+gxps_jpeg_writer_class_init (GXPSJpegWriterClass *klass)
+{
+}
+
+GXPSImageWriter *
+gxps_jpeg_writer_new (void)
+{
+        return GXPS_IMAGE_WRITER (g_object_new (GXPS_TYPE_JPEG_WRITER, NULL));
+}
+
+static void
+jpeg_output_message (j_common_ptr cinfo)
+{
+        char buffer[JMSG_LENGTH_MAX];
+
+        /* Create the message */
+        (*cinfo->err->format_message) (cinfo, buffer);
+        g_printerr ("%s\n", buffer);
+}
+
+static gboolean
+gxps_jpeg_writer_image_writer_init (GXPSImageWriter *image_writer,
+                                    FILE            *fd,
+                                    guint            width,
+                                    guint            height,
+                                    guint            x_resolution,
+                                    guint            y_resolution)
+{
+        GXPSJpegWriter *jpeg_writer = GXPS_JPEG_WRITER (image_writer);
+
+        jpeg_writer->row_buffer = (guchar *) g_malloc (width * 4);
+
+        jpeg_std_error (&jpeg_writer->error_mgr);
+        jpeg_writer->error_mgr.output_message = jpeg_output_message;
+
+        jpeg_create_compress (&jpeg_writer->cinfo);
+        jpeg_writer->cinfo.err = &jpeg_writer->error_mgr;
+
+        jpeg_stdio_dest (&jpeg_writer->cinfo, fd);
+
+        jpeg_writer->cinfo.image_width = width;
+        jpeg_writer->cinfo.image_height = height;
+        jpeg_writer->cinfo.density_unit = 1; /* dots per inch */
+        jpeg_writer->cinfo.X_density = x_resolution;
+        jpeg_writer->cinfo.Y_density = y_resolution;
+        jpeg_writer->cinfo.in_color_space = JCS_RGB; /* colorspace of input image */
+        jpeg_writer->cinfo.input_components = 3; /* color components per pixel */
+        jpeg_set_defaults (&jpeg_writer->cinfo);
+
+        jpeg_start_compress (&jpeg_writer->cinfo, TRUE);
+
+        return TRUE;
+}
+
+static gboolean
+gxps_jpeg_writer_image_writer_write (GXPSImageWriter *image_writer,
+                                     guchar          *row)
+{
+        GXPSJpegWriter *jpeg_writer = GXPS_JPEG_WRITER (image_writer);
+        guint           image_width = jpeg_writer->cinfo.image_width;
+        uint32_t       *pixel = (uint32_t *)row;
+        guchar         *rowp;
+        guint           i;
+
+        rowp = jpeg_writer->row_buffer;
+
+        for (i = 0; i < image_width; i++, pixel++) {
+                *rowp++ = (*pixel & 0xff0000) >> 16;
+                *rowp++ = (*pixel & 0x00ff00) >>  8;
+                *rowp++ = (*pixel & 0x0000ff) >>  0;
+        }
+
+        jpeg_write_scanlines (&jpeg_writer->cinfo, &jpeg_writer->row_buffer, 1);
+
+        return TRUE;
+}
+
+static gboolean
+gxps_jpeg_writer_image_writer_finish (GXPSImageWriter *image_writer)
+{
+        GXPSJpegWriter *jpeg_writer = GXPS_JPEG_WRITER (image_writer);
+
+        jpeg_finish_compress (&jpeg_writer->cinfo);
+
+        g_free (jpeg_writer->row_buffer);
+        jpeg_writer->row_buffer = NULL;
+
+        return TRUE;
+}
+
+static void
+gxps_jpeg_writer_image_writer_iface_init (GXPSImageWriterInterface *iface)
+{
+        iface->init = gxps_jpeg_writer_image_writer_init;
+        iface->write = gxps_jpeg_writer_image_writer_write;
+        iface->finish = gxps_jpeg_writer_image_writer_finish;
+}
diff --git a/tools/gxps-jpeg-writer.h b/tools/gxps-jpeg-writer.h
new file mode 100644
index 0000000..ca21891
--- /dev/null
+++ b/tools/gxps-jpeg-writer.h
@@ -0,0 +1,44 @@
+/* GXPSJpegWriter
+ *
+ * Copyright (C) 2011  Carlos Garcia Campos <carlosgc gnome org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#ifndef __GXPS_JPEG_WRITER_H__
+#define __GXPS_JPEG_WRITER_H__
+
+#include <glib-object.h>
+#include "gxps-image-writer.h"
+
+G_BEGIN_DECLS
+
+#define GXPS_TYPE_JPEG_WRITER           (gxps_jpeg_writer_get_type ())
+#define GXPS_JPEG_WRITER(obj)           (G_TYPE_CHECK_INSTANCE_CAST (obj, GXPS_TYPE_JPEG_WRITER, GXPSJpegWriter))
+#define GXPS_JPEG_WRITER_CLASS(cls)     (G_TYPE_CHECK_CLASS_CAST (cls, GXPS_TYPE_JPEG_WRITER, GXPSJpegWriterClass))
+#define GXPS_IS_JPEG_WRITER(obj)        (G_TYPE_CHECK_INSTANCE_TYPE (obj, GXPS_TYPE_JPEG_WRITER))
+#define GXPS_IS_JPEG_WRITER_CLASS(obj)  (G_TYPE_CHECK_CLASS_TYPE (obj, GXPS_TYPE_JPEG_WRITER))
+#define GXPS_JPEG_WRITER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GXPS_TYPE_JPEG_WRITER, GXPSJpegWriterClass))
+
+typedef struct _GXPSJpegWriter        GXPSJpegWriter;
+typedef struct _GXPSJpegWriterClass   GXPSJpegWriterClass;
+
+GType            gxps_jpeg_writer_get_type (void);
+GXPSImageWriter *gxps_jpeg_writer_new      (void);
+
+
+G_END_DECLS
+
+#endif /* __GXPS_JPEG_WRITER_H__ */



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]