[gnome-photos/wip/rishi/misc-fixes: 25/25] Add a sink to guess the size of a GeglBuffer after JPEG compression



commit 498ad592473390094d7e70197a84e44ebcb657be
Author: Debarshi Ray <debarshir gnome org>
Date:   Wed Dec 23 11:10:42 2015 +0100

    Add a sink to guess the size of a GeglBuffer after JPEG compression

 src/photos-operation-jpg-guess-sizes.c |  287 ++++++++++++++++++++++++++++++++
 src/photos-operation-jpg-guess-sizes.h |   45 +++++
 2 files changed, 332 insertions(+), 0 deletions(-)
---
diff --git a/src/photos-operation-jpg-guess-sizes.c b/src/photos-operation-jpg-guess-sizes.c
new file mode 100644
index 0000000..d8dcd19
--- /dev/null
+++ b/src/photos-operation-jpg-guess-sizes.c
@@ -0,0 +1,287 @@
+/*
+ * Photos - access, organize and share your photos on GNOME
+ * Copyright © 2015 Red Hat, Inc.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+
+#include "config.h"
+
+#include <babl/babl.h>
+#include <gegl.h>
+#include <gegl-plugin.h>
+#include <jpeglib.h>
+
+#include "photos-operation-jpg-guess-sizes.h"
+
+
+struct _PhotosOperationJpgGuessSizes
+{
+  GeglOperationSink parent_instance;
+  gboolean progressive;
+  gint quality;
+};
+
+struct _PhotosOperationJpgGuessSizesClass
+{
+  GeglOperationSinkClass parent_class;
+};
+
+enum
+{
+  PROP_0,
+  PROP_PROGRESSIVE,
+  PROP_QUALITY
+};
+
+
+G_DEFINE_TYPE (PhotosOperationJpgGuessSizes, photos_operation_jpg_guess_sizes, GEGL_TYPE_OPERATION_SINK);
+
+
+typedef struct _PhotosOperationJpgGuessSizeDestMgr PhotosOperationJpgGuessSizeDestMgr;
+
+struct _PhotosOperationJpgGuessSizeDestMgr
+{
+  struct jpeg_destination_mgr parent;
+};
+
+static JOCTET dummy_buffer[1];
+
+
+static void
+photos_operation_jpg_guess_sizes_count_dest (j_compress_ptr cinfo)
+{
+  PhotosOperationJpgGuessSizeDestMgr *dest;
+
+  if (cinfo->dest == NULL)
+    {
+      cinfo->dest
+        = (struct jpeg_destination_mgr *) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo,
+                                                                      JPOOL_PERMANENT,
+                                                                      sizeof 
(PhotosOperationJpgGuessSizeDestMgr));
+    }
+
+  dest = (PhotosOperationJpgGuessSizeDestMgr *) cinfo->dest;
+  dest->parent.init_description = ;
+  dest->parent.empty_output_buffer = ;
+  dest->parent.term_destination = ;
+  dest->parent.next_output_byte = dummy_buffer;
+  dest->parent.free_in_buffer = G_N_ELEMENTS (dummy_buffer);
+}
+
+
+static gsize
+gegl_buffer_export_jpg (GeglBuffer  *gegl_buffer,
+                        const gchar *path,
+                        gint         quality,
+                        gint         smoothing,
+                        gboolean     optimize,
+                        gboolean     progressive,
+                        gboolean     grayscale,
+                        gint         src_x,
+                        gint         src_y,
+                        gint         width,
+                        gint         height)
+{
+  struct jpeg_compress_struct cinfo;
+  struct jpeg_error_mgr jerr;
+  JSAMPROW row_pointer[1];
+  const Babl *format;
+
+  cinfo.err = jpeg_std_error (&jerr);
+  jpeg_create_compress (&cinfo);
+
+  jpeg_stdio_dest (&cinfo, fp);
+
+  cinfo.image_width = width;
+  cinfo.image_height = height;
+
+  if (!grayscale)
+    {
+      cinfo.input_components = 3;
+      cinfo.in_color_space = JCS_RGB;
+    }
+  else
+    {
+      cinfo.input_components = 1;
+      cinfo.in_color_space = JCS_GRAYSCALE;
+    }
+
+  jpeg_set_defaults (&cinfo);
+  jpeg_set_quality (&cinfo, quality, TRUE);
+  cinfo.smoothing_factor = smoothing;
+  cinfo.optimize_coding = optimize;
+  if (progressive)
+    jpeg_simple_progression (&cinfo);
+
+  /* Use 1x1,1x1,1x1 MCUs and no subsampling */
+  cinfo.comp_info[0].h_samp_factor = 1;
+  cinfo.comp_info[0].v_samp_factor = 1;
+
+  if (!grayscale)
+    {
+      cinfo.comp_info[1].h_samp_factor = 1;
+      cinfo.comp_info[1].v_samp_factor = 1;
+      cinfo.comp_info[2].h_samp_factor = 1;
+      cinfo.comp_info[2].v_samp_factor = 1;
+    }
+
+  /* No restart markers */
+  cinfo.restart_interval = 0;
+  cinfo.restart_in_rows = 0;
+
+  jpeg_start_compress (&cinfo, TRUE);
+
+  if (!grayscale)
+    {
+      format = babl_format ("R'G'B' u8");
+      row_pointer[0] = g_malloc (width * 3);
+    }
+  else
+    {
+      format = babl_format ("Y' u8");
+      row_pointer[0] = g_malloc (width);
+    }
+
+  while (cinfo.next_scanline < cinfo.image_height) {
+    GeglRectangle rect;
+
+    rect.x = src_x;
+    rect.y = src_y + cinfo.next_scanline;
+    rect.width = width;
+    rect.height = 1;
+
+    gegl_buffer_get (gegl_buffer, &rect, 1.0, format,
+                     row_pointer[0], GEGL_AUTO_ROWSTRIDE,
+                     GEGL_ABYSS_NONE);
+
+    jpeg_write_scanlines (&cinfo, row_pointer, 1);
+  }
+
+  jpeg_finish_compress (&cinfo);
+  jpeg_destroy_compress (&cinfo);
+
+  g_free (row_pointer[0]);
+
+  if (stdout != fp)
+    fclose (fp);
+
+  return 0;
+}
+
+
+static gboolean
+photos_operation_jpg_guess_sizes_process (GeglOperation *operation,
+                                          GeglBuffer *input,
+                                          const GeglRectangle *roi,
+                                          gint level)
+{
+  return TRUE;
+}
+
+
+static void
+photos_operation_jpg_guess_sizes_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec 
*pspec)
+{
+  PhotosOperationJpgGuessSizes *self = PHOTOS_OPERATION_JPG_GUESS_SIZES (object);
+
+  switch (prop_id)
+    {
+    case PROP_PROGRESSIVE:
+      g_value_set_boolean (value, self->progressive);
+      break;
+
+    case PROP_QUALITY:
+      g_value_set_int (value, self->quality);
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+    }
+}
+
+
+static void
+photos_operation_jpg_guess_sizes_set_property (GObject *object,
+                                               guint prop_id,
+                                               const GValue *value,
+                                               GParamSpec *pspec)
+{
+  PhotosOperationJpgGuessSizes *self = PHOTOS_OPERATION_JPG_GUESS_SIZES (object);
+
+  switch (prop_id)
+    {
+    case PROP_PROGRESSIVE:
+      self->progressive = g_value_get_boolean (value);
+      break;
+
+    case PROP_QUALITY:
+      self->quality = g_value_get_int (value);
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+    }
+}
+
+
+static void
+photos_operation_jpg_guess_sizes_init (PhotosOperationJpgGuessSizes *self)
+{
+}
+
+
+static void
+photos_operation_jpg_guess_sizes_class_init (PhotosOperationJpgGuessSizesClass *class)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (class);
+  GeglOperationClass *operation_class = GEGL_OPERATION_CLASS (class);
+  GeglOperationSinkClass *sink_class = GEGL_OPERATION_SINK_CLASS (class);
+
+  operation_class->opencl_support = FALSE;
+  sink_class->needs_full = TRUE;
+
+  object_class->get_property = photos_operation_jpg_guess_sizes_get_property;
+  object_class->set_property = photos_operation_jpg_guess_sizes_set_property;
+  sink_class->process = photos_operation_jpg_guess_sizes_process;
+
+  g_object_class_install_property (object_class,
+                                   PROP_PROGRESSIVE,
+                                   g_param_spec_int ("progressive",
+                                                     "Progressive",
+                                                     "Create progressive JPEG images",
+                                                     TRUE,
+                                                     G_PARAM_CONSTRUCT | G_PARAM_READWRITE));
+
+  g_object_class_install_property (object_class,
+                                   PROP_QUALITY,
+                                   g_param_spec_int ("quality",
+                                                     "Quality",
+                                                     "JPEG compression quality (between 1 and 100)",
+                                                     1,
+                                                     100,
+                                                     90,
+                                                     G_PARAM_CONSTRUCT | G_PARAM_READWRITE));
+
+  gegl_operation_class_set_keys (operation_class,
+                                 "name", "photos:jpg-guess-sizes",
+                                 "title", "JPEG Guess Sizes",
+                                 "description", "Guesses the size of a GeglBuffer after applying JPEG 
compression",
+                                 NULL);
+}
diff --git a/src/photos-operation-jpg-guess-sizes.h b/src/photos-operation-jpg-guess-sizes.h
new file mode 100644
index 0000000..1259c65
--- /dev/null
+++ b/src/photos-operation-jpg-guess-sizes.h
@@ -0,0 +1,45 @@
+/*
+ * Photos - access, organize and share your photos on GNOME
+ * Copyright © 2015 Red Hat, Inc.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+#ifndef PHOTOS_OPERATION_JPG_GUESS_SIZES_H
+#define PHOTOS_OPERATION_JPG_GUESS_SIZES_H
+
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+#define PHOTOS_TYPE_OPERATION_JPG_GUESS_SIZES (photos_operation_jpg_guess_sizes_get_type ())
+
+#define PHOTOS_OPERATION_JPG_GUESS_SIZES(obj) \
+  (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
+   PHOTOS_TYPE_OPERATION_JPG_GUESS_SIZES, PhotosOperationJpgGuessSizes))
+
+#define PHOTOS_IS_OPERATION_JPG_GUESS_SIZES(obj) \
+  (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
+   PHOTOS_TYPE_OPERATION_JPG_GUESS_SIZES))
+
+typedef struct _PhotosOperationJpgGuessSizes      PhotosOperationJpgGuessSizes;
+typedef struct _PhotosOperationJpgGuessSizesClass PhotosOperationJpgGuessSizesClass;
+
+GType            photos_operation_jpg_guess_sizes_get_type       (void) G_GNUC_CONST;
+
+G_END_DECLS
+
+#endif /* PHOTOS_OPERATION_JPG_GUESS_SIZES_H */


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