Re: patch for gdk-pixbuf to read and write PNG tEXt chunks
- From: Sven Neumann <sven gimp org>
- To: Owen Taylor <otaylor redhat com>
- Cc: Gtk+ Developers <gtk-devel-list gnome org>, Jens Finke <jens triq net>
- Subject: Re: patch for gdk-pixbuf to read and write PNG tEXt chunks
- Date: 05 Oct 2001 19:25:21 +0200
Hi,
here's a new patch that tries to address all the outstanding issues
discussed lately. I hope you like it.
Salut, Sven
Index: demos/testpixbuf-save.c
===================================================================
RCS file: /cvs/gnome/gtk+/demos/testpixbuf-save.c,v
retrieving revision 1.7
diff -u -r1.7 testpixbuf-save.c
--- demos/testpixbuf-save.c 2001/09/07 21:49:33 1.7
+++ demos/testpixbuf-save.c 2001/10/05 17:02:26
@@ -35,7 +35,10 @@
return;
}
- if (!gdk_pixbuf_save (pixbuf, "foo.png", "png", &err, NULL)) {
+ if (!gdk_pixbuf_save (pixbuf, "foo.png", "png",
+ &err,
+ "tEXt::Software", "testpixbuf-save",
+ NULL)) {
fprintf (stderr, "%s", err->message);
g_error_free (err);
}
Index: demos/testpixbuf-scale.c
===================================================================
RCS file: /cvs/gnome/gtk+/demos/testpixbuf-scale.c,v
retrieving revision 1.11
diff -u -r1.11 testpixbuf-scale.c
--- demos/testpixbuf-scale.c 2001/08/23 15:26:44 1.11
+++ demos/testpixbuf-scale.c 2001/10/05 17:02:26
@@ -63,6 +63,7 @@
GtkWidget *hbox, *label, *hscale;
GtkAdjustment *adjustment;
GtkRequisition scratch_requisition;
+ const gchar *creator;
GError *error;
pixbuf_init ();
@@ -82,6 +83,10 @@
g_error_free (error);
exit(1);
}
+
+ creator = gdk_pixbuf_get_option (pixbuf, "tEXt::Software");
+ if (creator)
+ g_print ("%s was created by '%s'\n", argv[1], creator);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_signal_connect (GTK_OBJECT (window), "destroy",
Index: docs/reference/gdk-pixbuf/tmpl/gdk-pixbuf.sgml
===================================================================
RCS file: /cvs/gnome/gtk+/docs/reference/gdk-pixbuf/tmpl/gdk-pixbuf.sgml,v
retrieving revision 1.18
diff -u -r1.18 gdk-pixbuf.sgml
--- docs/reference/gdk-pixbuf/tmpl/gdk-pixbuf.sgml 2001/06/29 01:58:57 1.18
+++ docs/reference/gdk-pixbuf/tmpl/gdk-pixbuf.sgml 2001/10/05 17:02:26
@@ -27,6 +27,7 @@
@GDK_PIXBUF_ERROR_UNKNOWN_FORMAT:
@GDK_PIXBUF_ERROR_CORRUPT_IMAGE: An image file was broken somehow.
@GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY: Not enough memory.
+ GDK_PIXBUF_ERROR_BAD_OPTION_KEY:
@GDK_PIXBUF_ERROR_BAD_OPTION_VALUE: An option passed to
gdk_pixbuf_save() had a bad value.
@GDK_PIXBUF_ERROR_UNKNOWN_TYPE: Unknown image type.
Index: gdk-pixbuf/gdk-pixbuf-io.c
===================================================================
RCS file: /cvs/gnome/gtk+/gdk-pixbuf/gdk-pixbuf-io.c,v
retrieving revision 1.60
diff -u -r1.60 gdk-pixbuf-io.c
--- gdk-pixbuf/gdk-pixbuf-io.c 2001/09/14 22:04:55 1.60
+++ gdk-pixbuf/gdk-pixbuf-io.c 2001/10/05 17:02:26
@@ -743,8 +743,13 @@
* "quality", "100", NULL);
* </programlisting>
*
- * The only save parameter that currently exists is the "quality" field
- * for JPEG images; its value should be in the range [0,100].
+ * Currently only few parameters exist. JPEG images can be saved with a
+ * "quality" parameter; its value should be in the range [0,100].
+ * Text chunks can be attached to PNG images by specifying parameters of
+ * the form "tEXt::key", where key is an ASCII string of length 1-79.
+ * The values are UTF-8 encoded strings. Note however that PNG text
+ * chunks are stored in ISO-8859-1 encoding, so you can only set texts
+ * that can be represented in this encoding.
*
* Return value: whether an error was set
**/
Index: gdk-pixbuf/gdk-pixbuf-private.h
===================================================================
RCS file: /cvs/gnome/gtk+/gdk-pixbuf/gdk-pixbuf-private.h,v
retrieving revision 1.9
diff -u -r1.9 gdk-pixbuf-private.h
--- gdk-pixbuf/gdk-pixbuf-private.h 2001/06/26 20:56:32 1.9
+++ gdk-pixbuf/gdk-pixbuf-private.h 2001/10/05 17:02:26
@@ -128,8 +128,16 @@
const GTimeVal *current_time);
};
-
GdkPixbufAnimation* gdk_pixbuf_non_anim_new (GdkPixbuf *pixbuf);
+
+
+
+/* key/value pairs that can be attached by the pixbuf loader */
+
+gboolean gdk_pixbuf_set_option (GdkPixbuf *pixbuf,
+ const gchar *key,
+ const gchar *value);
+
#endif
Index: gdk-pixbuf/gdk-pixbuf.c
===================================================================
RCS file: /cvs/gnome/gtk+/gdk-pixbuf/gdk-pixbuf.c,v
retrieving revision 1.45
diff -u -r1.45 gdk-pixbuf.c
--- gdk-pixbuf/gdk-pixbuf.c 2001/08/07 17:49:09 1.45
+++ gdk-pixbuf/gdk-pixbuf.c 2001/10/05 17:02:26
@@ -1,3 +1,4 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
/* GdkPixbuf library - Basic memory management
*
* Copyright (C) 1999 The Free Software Foundation
@@ -479,6 +480,93 @@
}
}
+
+
+/**
+ * gdk_pixbuf_get_option:
+ * @pixbuf: a #GdkPixbuf
+ * @key: a nul-terminated string.
+ *
+ * Looks up @key in the list of options that may have been attached to the
+ * @pixbuf when it was loaded.
+ *
+ * Return value: the value associated with @key. This is a nul-terminated
+ * string that should not be freed or %NULL if @key was not found.
+ **/
+G_CONST_RETURN gchar *
+gdk_pixbuf_get_option (GdkPixbuf *pixbuf,
+ const gchar *key)
+{
+ gchar **options;
+ gint i;
+
+ g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
+ g_return_val_if_fail (key != NULL, NULL);
+
+ options = g_object_get_qdata (G_OBJECT (pixbuf),
+ g_quark_from_static_string ("gdk_pixbuf_options"));
+ if (options) {
+ for (i = 0; options[2*i]; i++) {
+ if (strcmp (options[2*i], key) == 0)
+ return options[2*i+1];
+ }
+ }
+
+ return NULL;
+}
+
+/**
+ * gdk_pixbuf_set_option:
+ * @pixbuf: a #GdkPixbuf
+ * @key: a nul-terminated string.
+ * @value: a nul-terminated string.
+ *
+ * Attaches a key/value pair as an option to a #GdkPixbuf. If %key already
+ * exists in the list of options attached to @pixbuf, the new value is
+ * ignored and %FALSE is returned.
+ *
+ * Return value: %TRUE on success.
+ **/
+gboolean
+gdk_pixbuf_set_option (GdkPixbuf *pixbuf,
+ const gchar *key,
+ const gchar *value)
+{
+ GQuark quark;
+ gchar **options;
+ gint n = 0;
+
+ g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), FALSE);
+ g_return_val_if_fail (key != NULL, FALSE);
+ g_return_val_if_fail (value != NULL, FALSE);
+
+ quark = g_quark_from_static_string ("gdk_pixbuf_options");
+
+ options = g_object_get_qdata (G_OBJECT (pixbuf), quark);
+
+ if (options) {
+ for (n = 0; options[2*n]; n++) {
+ if (strcmp (options[2*n], key) == 0)
+ return FALSE;
+ }
+
+ g_object_steal_qdata (G_OBJECT (pixbuf), quark);
+ options = g_renew (gchar *, options, 2*(n+1) + 1);
+ } else {
+ options = g_new (gchar *, 3);
+ }
+
+ options[2*n] = g_strdup (key);
+ options[2*n+1] = g_strdup (value);
+ options[2*n+2] = NULL;
+
+ g_object_set_qdata_full (G_OBJECT (pixbuf), quark,
+ options, (GDestroyNotify) g_strfreev);
+
+ return TRUE;
+}
+
+
/* Include the marshallers */
#include <glib-object.h>
Index: gdk-pixbuf/gdk-pixbuf.h
===================================================================
RCS file: /cvs/gnome/gtk+/gdk-pixbuf/gdk-pixbuf.h,v
retrieving revision 1.62
diff -u -r1.62 gdk-pixbuf.h
--- gdk-pixbuf/gdk-pixbuf.h 2001/09/16 23:54:37 1.62
+++ gdk-pixbuf/gdk-pixbuf.h 2001/10/05 17:02:26
@@ -85,6 +85,8 @@
GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
/* no mem to load image */
GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
+ /* bad option key passed to save routine */
+ GDK_PIXBUF_ERROR_BAD_OPTION_KEY,
/* bad option value passed to save routine */
GDK_PIXBUF_ERROR_BAD_OPTION_VALUE,
/* unsupported image type (sort of an ENOSYS) */
@@ -286,6 +288,12 @@
const GTimeVal *current_time);
+
+
+G_CONST_RETURN gchar * gdk_pixbuf_get_option (GdkPixbuf *pixbuf,
+ const gchar *key);
+
+
#include <gdk-pixbuf/gdk-pixbuf-loader.h>
#include <gdk-pixbuf/gdk-pixbuf-enum-types.h>
Index: gdk-pixbuf/io-png.c
===================================================================
RCS file: /cvs/gnome/gtk+/gdk-pixbuf/io-png.c,v
retrieving revision 1.41
diff -u -r1.41 io-png.c
--- gdk-pixbuf/io-png.c 2001/08/30 07:21:13 1.41
+++ gdk-pixbuf/io-png.c 2001/10/05 17:02:26
@@ -182,17 +182,39 @@
g_free (pixels);
}
+static gboolean
+png_text_to_pixbuf_option (png_text text_ptr,
+ gchar **key,
+ gchar **value)
+{
+ *value = g_convert (text_ptr.text, -1,
+ "UTF-8", "ISO-8859-1",
+ NULL, NULL, NULL);
+ if (*value) {
+ *key = g_strconcat ("tEXt::", text_ptr.key, NULL);
+ return TRUE;
+ } else {
+ g_warning ("Couldn't convert tEXt chunk value to UTF-8.");
+ *key = NULL;
+ return FALSE;
+ }
+}
+
/* Shared library entry point */
static GdkPixbuf *
gdk_pixbuf__png_image_load (FILE *f, GError **error)
{
+ GdkPixbuf *pixbuf;
png_structp png_ptr;
png_infop info_ptr, end_info;
+ png_textp text_ptr;
gboolean failed = FALSE;
gint i, ctype, bpp;
png_uint_32 w, h;
png_bytepp rows;
guchar *pixels;
+ gint num_texts;
+ gchar **options = NULL;
png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING,
error,
@@ -255,17 +277,42 @@
rows[i] = pixels + i * w * bpp;
png_read_image (png_ptr, rows);
+
+ if (png_get_text (png_ptr, info_ptr, &text_ptr, &num_texts)) {
+ options = g_new (gchar *, num_texts * 2);
+ for (i = 0; i < num_texts; i++) {
+ png_text_to_pixbuf_option (text_ptr[i],
+ options + 2*i,
+ options + 2*i + 1);
+ }
+ }
png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
g_free (rows);
if (ctype & PNG_COLOR_MASK_ALPHA)
- return gdk_pixbuf_new_from_data (pixels, GDK_COLORSPACE_RGB, TRUE, 8,
- w, h, w * 4,
- free_buffer, NULL);
+ pixbuf = gdk_pixbuf_new_from_data (pixels, GDK_COLORSPACE_RGB, TRUE, 8,
+ w, h, w * 4,
+ free_buffer, NULL);
else
- return gdk_pixbuf_new_from_data (pixels, GDK_COLORSPACE_RGB, FALSE, 8,
- w, h, w * 3,
- free_buffer, NULL);
+ pixbuf = gdk_pixbuf_new_from_data (pixels, GDK_COLORSPACE_RGB, FALSE, 8,
+ w, h, w * 3,
+ free_buffer, NULL);
+
+ if (options) {
+ for (i = 0; i < num_texts; i++) {
+ if (pixbuf) {
+ if (!gdk_pixbuf_set_option (pixbuf,
+ options[2*i],
+ options[2*i+1]))
+ g_warning ("Got multiple tEXt chunks for the same key.");
+ }
+ g_free (options[2*i]);
+ g_free (options[2*i+1]);
+ }
+ g_free (options);
+ }
+
+ return pixbuf;
}
/* I wish these avoided the setjmp()/longjmp() crap in libpng instead
@@ -501,6 +548,8 @@
{
LoadContext* lc;
png_uint_32 width, height;
+ png_textp png_text_ptr;
+ int i, num_texts;
int color_type;
gboolean have_alpha = FALSE;
gboolean failed = FALSE;
@@ -538,7 +587,22 @@
}
return;
}
+
+ /* Extract tEXt chunks and attach them as pixbuf options */
+ if (png_get_text (png_read_ptr, png_info_ptr, &png_text_ptr, &num_texts)) {
+ for (i = 0; i < num_texts; i++) {
+ gchar *key, *value;
+
+ if (png_text_to_pixbuf_option (png_text_ptr[i],
+ &key, &value)) {
+ gdk_pixbuf_set_option (lc->pixbuf, key, value);
+ g_free (key);
+ g_free (value);
+ }
+ }
+ }
+
/* Notify the client that we are ready to go */
if (lc->prepare_func)
@@ -653,32 +717,76 @@
{
png_structp png_ptr;
png_infop info_ptr;
+ png_textp text_ptr = NULL;
guchar *ptr;
guchar *pixels;
- int x, y, j;
+ int x, y;
+ int i, j;
png_bytep row_ptr;
png_bytep data;
png_color_8 sig_bit;
int w, h, rowstride;
int has_alpha;
int bpc;
+ int num_keys;
+
+ num_keys = 0;
if (keys && *keys) {
- g_warning ("Bad option name '%s' passed to PNG saver",
- *keys);
- return FALSE;
-#if 0
- gchar **kiter = keys;
- gchar **viter = values;
-
-
- while (*kiter) {
-
- ++kiter;
- ++viter;
+ gchar **kiter;
+ gchar *key;
+ int len;
+
+ for (kiter = keys; *kiter; kiter++) {
+ if (strncmp (*kiter, "tEXt::", 6) != 0) {
+ g_warning ("Bad option name '%s' passed to PNG saver", *kiter);
+ return FALSE;
+ }
+ key = *kiter + 6;
+ len = strlen (key);
+ if (len <= 1 || len > 79) {
+ g_set_error (error,
+ GDK_PIXBUF_ERROR,
+ GDK_PIXBUF_ERROR_BAD_OPTION_KEY,
+ _("Keys for PNG tEXt chunks must have at least 1 and at most 79 characters."));
+ return FALSE;
+ }
+ for (i = 0; i < len; i++) {
+ if ((guchar) key[i] > 127) {
+ g_set_error (error,
+ GDK_PIXBUF_ERROR,
+ GDK_PIXBUF_ERROR_BAD_OPTION_KEY,
+ _("Keys for PNG tEXt chunks must be ASCII characters."));
+ return FALSE;
+ }
+ }
+ num_keys++;
}
-#endif
}
+
+ if (num_keys > 0) {
+ text_ptr = g_new0 (png_text, num_keys);
+ for (i = 0; i < num_keys; i++) {
+ text_ptr[i].compression = PNG_TEXT_COMPRESSION_NONE;
+ text_ptr[i].key = keys[i] + 6;
+ text_ptr[i].text = g_convert (values[i], -1,
+ "ISO-8859-1", "UTF-8",
+ NULL, &text_ptr[i].text_length,
+ NULL);
+ if (!text_ptr[i].text) {
+ g_set_error (error,
+ GDK_PIXBUF_ERROR,
+ GDK_PIXBUF_ERROR_BAD_OPTION_VALUE,
+ _("Value for PNG tEXt chunk can not be converted to ISO-8859-1 encoding."));
+ num_keys = i;
+ for (i = 0; i < num_keys; i++)
+ g_free (text_ptr[i].text);
+ g_free (text_ptr);
+ return FALSE;
+ }
+ }
+ }
+
data = NULL;
bpc = gdk_pixbuf_get_bits_per_sample (pixbuf);
@@ -704,7 +812,13 @@
png_destroy_write_struct (&png_ptr, (png_infopp) NULL);
return FALSE;
}
+
+ if (num_keys > 0) {
+ png_set_text (png_ptr, info_ptr, text_ptr, num_keys);
+ }
+
png_init_io (png_ptr, f);
+
if (has_alpha) {
png_set_IHDR (png_ptr, info_ptr, w, h, bpc,
PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
@@ -762,6 +876,12 @@
png_write_end (png_ptr, info_ptr);
png_destroy_write_struct (&png_ptr, (png_infopp) NULL);
+
+ if (num_keys > 0) {
+ for (i = 0; i < num_keys; i++)
+ g_free (text_ptr[i].text);
+ g_free (text_ptr);
+ }
return TRUE;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]