[gthumb] added some utilities to resize images



commit 7d180a2862bddb5147c4c280707deb391fa41450
Author: Paolo Bacchilega <paobac src gnome org>
Date:   Tue Sep 4 18:21:15 2012 +0200

    added some utilities to resize images

 gthumb/Makefile.am       |    2 +
 gthumb/gth-image-utils.c |  178 ++++++++++++++++++++++++++++++++++++++++++++++
 gthumb/gth-image-utils.h |   76 ++++++++++++++++++++
 gthumb/gtk-utils.c       |   35 +++++++++-
 gthumb/gtk-utils.h       |    3 +
 gthumb/pixbuf-io.c       |    1 +
 gthumb/pixbuf-utils.c    |   55 --------------
 gthumb/pixbuf-utils.h    |   13 ----
 8 files changed, 294 insertions(+), 69 deletions(-)
---
diff --git a/gthumb/Makefile.am b/gthumb/Makefile.am
index f6b674b..99367c2 100644
--- a/gthumb/Makefile.am
+++ b/gthumb/Makefile.am
@@ -73,6 +73,7 @@ PUBLIC_HEADER_FILES = 					\
 	gth-image-saver.h				\
 	gth-image-selector.h				\
 	gth-image-task.h				\
+	gth-image-utils.h				\
 	gth-image-viewer.h				\
 	gth-image-viewer-tool.h				\
 	gth-info-bar.h					\
@@ -205,6 +206,7 @@ gthumb_SOURCES = 					\
 	gth-image-saver.c				\
 	gth-image-selector.c				\
 	gth-image-task.c				\
+	gth-image-utils.c				\
 	gth-image-viewer.c				\
 	gth-image-viewer-tool.c				\
 	gth-info-bar.c					\
diff --git a/gthumb/gth-image-utils.c b/gthumb/gth-image-utils.c
new file mode 100644
index 0000000..c084847
--- /dev/null
+++ b/gthumb/gth-image-utils.c
@@ -0,0 +1,178 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+
+/*
+ *  GThumb
+ *
+ *  Copyright (C) 2001-2009 The Free Software Foundation, 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <config.h>
+#include <math.h>
+#include <glib.h>
+#include <glib/gi18n.h>
+#include "cairo-scale.h"
+#include "cairo-utils.h"
+#include "glib-utils.h"
+#include "gth-image-utils.h"
+#include "gth-main.h"
+
+
+SizeValue
+ImageSizeValues[IMAGE_SIZE_N] = {
+		{ 320, 200 },
+		{ 320, 320 },
+		{ 640, 480 },
+		{ 640, 640 },
+		{ 800, 600 },
+		{ 800, 800 },
+		{ 1024, 768 },
+		{ 1024, 1024 },
+		{ 1280, 960 },
+		{ 1280, 1280 } };
+
+
+gboolean
+scale_keeping_ratio_min (int      *width,
+			 int      *height,
+			 int       min_width,
+			 int       min_height,
+			 int       max_width,
+			 int       max_height,
+			 gboolean  allow_upscaling)
+{
+	double   w = *width;
+	double   h = *height;
+	double   min_w = min_width;
+	double   min_h = min_height;
+	double   max_w = max_width;
+	double   max_h = max_height;
+	double   factor;
+	int      new_width, new_height;
+	gboolean modified;
+
+	if ((*width < max_width) && (*height < max_height) && ! allow_upscaling)
+		return FALSE;
+
+	if (((*width < min_width) || (*height < min_height)) && ! allow_upscaling)
+		return FALSE;
+
+	factor = MAX (MIN (max_w / w, max_h / h), MAX (min_w / w, min_h / h));
+	new_width  = MAX ((int) floor (w * factor + 0.50), 1);
+	new_height = MAX ((int) floor (h * factor + 0.50), 1);
+
+	modified = (new_width != *width) || (new_height != *height);
+
+	*width = new_width;
+	*height = new_height;
+
+	return modified;
+}
+
+
+gboolean
+scale_keeping_ratio (int      *width,
+		     int      *height,
+		     int       max_width,
+		     int       max_height,
+		     gboolean  allow_upscaling)
+{
+	return scale_keeping_ratio_min (width,
+					height,
+					0,
+					0,
+					max_width,
+					max_height,
+					allow_upscaling);
+}
+
+
+gboolean
+_g_buffer_resize_image (void          *buffer,
+		        gsize          count,
+		        GthFileData   *file_data,
+		        int            max_width,
+		        int            max_height,
+		        void         **resized_buffer,
+		        gsize         *resized_count,
+		        GCancellable  *cancellable,
+		        GError       **error)
+{
+	GInputStream       *istream;
+	const char         *mime_type;
+	GthImageLoaderFunc  loader_func;
+	GthImage           *image;
+	int                 width;
+	int                 height;
+	cairo_surface_t    *surface;
+	cairo_surface_t    *scaled;
+	gboolean            result;
+
+	if ((max_width == -1) || (max_height == -1)) {
+		*error = NULL;
+		return FALSE;
+	}
+
+	istream = g_memory_input_stream_new_from_data (buffer, count, NULL);
+	mime_type = _g_content_type_get_from_stream (istream, cancellable, NULL);
+	if (mime_type == NULL) {
+		g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "%s", _("No suitable loader available for this file type"));
+		return FALSE;
+	}
+
+	loader_func = gth_main_get_image_loader_func (mime_type, GTH_IMAGE_FORMAT_CAIRO_SURFACE);
+	if (loader_func == NULL) {
+		g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "%s", _("No suitable loader available for this file type"));
+		g_object_unref (istream);
+		return FALSE;
+	}
+
+	image = loader_func (istream,
+			     NULL,
+			     -1,
+			     &width,
+			     &height,
+			     NULL,
+			     cancellable,
+			     error);
+	if (image == NULL) {
+		g_object_unref (istream);
+		return FALSE;
+	}
+
+	if (! scale_keeping_ratio (&width, &height, max_width, max_height, FALSE)) {
+		error = NULL;
+		g_object_unref (image);
+		g_object_unref (istream);
+		return FALSE;
+	}
+
+	surface = gth_image_get_cairo_surface (image);
+	scaled = _cairo_image_surface_scale (surface, width, height, SCALE_FILTER_BEST, NULL);
+	gth_image_set_cairo_surface (image, scaled);
+	result = gth_image_save_to_buffer (image,
+					   mime_type,
+					   file_data,
+					   (char **) resized_buffer,
+					   resized_count,
+					   error);
+
+	cairo_surface_destroy (scaled);
+	cairo_surface_destroy (surface);
+	g_object_unref (image);
+	g_object_unref (istream);
+
+	return result;
+}
diff --git a/gthumb/gth-image-utils.h b/gthumb/gth-image-utils.h
new file mode 100644
index 0000000..03c0f46
--- /dev/null
+++ b/gthumb/gth-image-utils.h
@@ -0,0 +1,76 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+
+/*
+ *  GThumb
+ *
+ *  Copyright (C) 2001-2012 The Free Software Foundation, 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef GTH_IMAGE_UTILS_H
+#define GTH_IMAGE_UTILS_H
+
+
+#include <glib.h>
+#include "gth-file-data.h"
+
+
+typedef enum /*< skip >*/ {
+	IMAGE_SIZE_320x200,
+	IMAGE_SIZE_320x320,
+	IMAGE_SIZE_640x480,
+	IMAGE_SIZE_640x640,
+	IMAGE_SIZE_800x600,
+	IMAGE_SIZE_800x800,
+	IMAGE_SIZE_1024x768,
+	IMAGE_SIZE_1024x1024,
+	IMAGE_SIZE_1280x960,
+	IMAGE_SIZE_1280x1280,
+	IMAGE_SIZE_N
+} ImageSize;
+
+
+typedef struct {
+	int width;
+	int height;
+} SizeValue;
+
+
+extern SizeValue ImageSizeValues[IMAGE_SIZE_N];
+
+
+gboolean    scale_keeping_ratio_min              (int             *width,
+					          int             *height,
+					          int              min_width,
+					          int              min_height,
+					          int              max_width,
+					          int              max_height,
+					          gboolean         allow_upscaling);
+gboolean    scale_keeping_ratio                  (int             *width,
+					          int             *height,
+					          int              max_width,
+					          int              max_height,
+					          gboolean         allow_upscaling);
+gboolean    _g_buffer_resize_image               (void            *buffer,
+						  gsize            count,
+						  GthFileData     *file_data,
+						  int              max_width,
+						  int              max_height,
+						  void           **resized_buffer,
+						  gsize           *resized_count,
+						  GCancellable    *cancellable,
+						  GError         **error);
+
+#endif /* GTH_IMAGE_UTILS_H */
diff --git a/gthumb/gtk-utils.c b/gthumb/gtk-utils.c
index 224eb8f..7544fef 100644
--- a/gthumb/gtk-utils.c
+++ b/gthumb/gtk-utils.c
@@ -21,8 +21,9 @@
 
 #include <config.h>
 #include <string.h>
+#include "gth-image-utils.h"
 #include "gtk-utils.h"
-#include "pixbuf-utils.h"
+
 
 #define REQUEST_ENTRY_WIDTH_IN_CHARS 40
 
@@ -1028,3 +1029,35 @@ _gtk_widget_get_icon_theme (GtkWidget *widget)
 
 	return gtk_icon_theme_get_for_screen (screen);
 }
+
+
+void
+_gtk_combo_box_add_image_sizes (GtkComboBox *combo_box,
+				int          active_width,
+				int          active_height)
+{
+	GtkListStore *list_store;
+	int           active_index;
+	int           i;
+
+	list_store = GTK_LIST_STORE (gtk_combo_box_get_model (combo_box));
+	active_index = 0;
+	for (i = 1; i < G_N_ELEMENTS (ImageSizeValues); i++) {
+		GtkTreeIter  iter;
+		char        *name;
+
+		gtk_list_store_append (list_store, &iter);
+
+		if ((ImageSizeValues[i].width == active_width) && (ImageSizeValues[i].height == active_height))
+			active_index = i;
+
+		/* Translators: this is an image size, such as 1024 Ã 768 */
+		name = g_strdup_printf (_("%d à %d"), ImageSizeValues[i].width, ImageSizeValues[i].height);
+		gtk_list_store_set (list_store, &iter,
+				    0, name,
+				    -1);
+
+		g_free (name);
+	}
+	gtk_combo_box_set_active (combo_box, active_index);
+}
diff --git a/gthumb/gtk-utils.h b/gthumb/gtk-utils.h
index 4e8a2f3..d6987ab 100644
--- a/gthumb/gtk-utils.h
+++ b/gthumb/gtk-utils.h
@@ -131,6 +131,9 @@ gboolean        _gdk_rgba_darker                           (GdkRGBA          *co
 gboolean        _gdk_rgba_lighter                          (GdkRGBA          *color,
 							    GdkRGBA          *result);
 GtkIconTheme *  _gtk_widget_get_icon_theme                 (GtkWidget        *widget);
+void            _gtk_combo_box_add_image_sizes             (GtkComboBox      *combo_box,
+							    int               active_width,
+							    int               active_height);
 
 G_END_DECLS
 
diff --git a/gthumb/pixbuf-io.c b/gthumb/pixbuf-io.c
index 0af6ef1..3294812 100644
--- a/gthumb/pixbuf-io.c
+++ b/gthumb/pixbuf-io.c
@@ -28,6 +28,7 @@
 #include "gth-error.h"
 #include "gth-hook.h"
 #include "gth-main.h"
+#include "gth-image-utils.h"
 #include "gth-image-saver.h"
 #include "pixbuf-io.h"
 #include "pixbuf-utils.h"
diff --git a/gthumb/pixbuf-utils.c b/gthumb/pixbuf-utils.c
index 54fac12..faadda0 100644
--- a/gthumb/pixbuf-utils.c
+++ b/gthumb/pixbuf-utils.c
@@ -196,58 +196,3 @@ _gdk_pixbuf_get_type_from_mime_type (const char *mime_type)
 	else
 		return g_strdup (mime_type);
 }
-
-
-gboolean
-scale_keeping_ratio_min (int      *width,
-			 int      *height,
-			 int       min_width,
-			 int       min_height,
-			 int       max_width,
-			 int       max_height,
-			 gboolean  allow_upscaling)
-{
-	double   w = *width;
-	double   h = *height;
-	double   min_w = min_width;
-	double   min_h = min_height;
-	double   max_w = max_width;
-	double   max_h = max_height;
-	double   factor;
-	int      new_width, new_height;
-	gboolean modified;
-
-	if ((*width < max_width) && (*height < max_height) && ! allow_upscaling)
-		return FALSE;
-
-	if (((*width < min_width) || (*height < min_height)) && ! allow_upscaling)
-		return FALSE;
-
-	factor = MAX (MIN (max_w / w, max_h / h), MAX (min_w / w, min_h / h));
-	new_width  = MAX ((int) floor (w * factor + 0.50), 1);
-	new_height = MAX ((int) floor (h * factor + 0.50), 1);
-
-	modified = (new_width != *width) || (new_height != *height);
-
-	*width = new_width;
-	*height = new_height;
-
-	return modified;
-}
-
-
-gboolean
-scale_keeping_ratio (int      *width,
-		     int      *height,
-		     int       max_width,
-		     int       max_height,
-		     gboolean  allow_upscaling)
-{
-	return scale_keeping_ratio_min (width,
-					height,
-					0,
-					0,
-					max_width,
-					max_height,
-					allow_upscaling);
-}
diff --git a/gthumb/pixbuf-utils.h b/gthumb/pixbuf-utils.h
index 6d31a42..b51d76e 100644
--- a/gthumb/pixbuf-utils.h
+++ b/gthumb/pixbuf-utils.h
@@ -40,19 +40,6 @@ GdkPixbuf * _gdk_pixbuf_transform                (GdkPixbuf       *src,
 					          GthTransform     transform);
 char *      _gdk_pixbuf_get_type_from_mime_type  (const char      *mime_type);
 
-gboolean    scale_keeping_ratio_min              (int             *width,
-					          int             *height,
-					          int              min_width,
-					          int              min_height,
-					          int              max_width,
-					          int              max_height,
-					          gboolean         allow_upscaling);
-gboolean    scale_keeping_ratio                  (int             *width,
-					          int             *height,
-					          int              max_width,
-					          int              max_height,
-					          gboolean         allow_upscaling);
-
 G_END_DECLS
 
 #endif



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