Inlining pixbufs
- From: Havoc Pennington <hp redhat com>
- To: gtk-devel-list gnome org
- Subject: Inlining pixbufs
- Date: 23 Jun 2000 19:15:52 -0400
Hi,
We need an equivalent to 'convertrgb' in gnome-libs/tools in order to
inline our stock icons, and since convertrgb is crufty and this is a
generally useful feature, I wrote a new program to handle it. The
program gets installed, so other people can use it.
The idea is:
- we create a program image-to-inline that loads image files and
can create a file full of C variable declarations
- we use this to create the inline variables for GTK
- gdk-pixbuf.h now contains gdk_pixbuf_new_from_inline() that
reads the stuff image-to-inline outputs.
Patch appended.
Havoc
Index: ChangeLog
===================================================================
RCS file: /cvs/gnome/gtk+/ChangeLog,v
retrieving revision 1.1295
diff -u -r1.1295 ChangeLog
--- ChangeLog 2000/06/23 19:41:55 1.1295
+++ ChangeLog 2000/06/23 22:58:14
@@ -1,5 +1,14 @@
2000-06-23 Havoc Pennington <hp@redhat.com>
+ * demos/Makefile.am (test-inline-pixbufs.h): Add
+ test-inline-pixbufs.h to BUILT_SOURCES, this is autogenerated by
+ the image-to-inline command we build in gdk-pixbuf
+
+ * demos/testpixbuf.c (main): Add test cases for reading
+ inline data
+
+2000-06-23 Havoc Pennington <hp@redhat.com>
+
* gtk/gtktextview.c (gtk_text_view_set_buffer): Use anonymous mark
instead of making up a bogus name for first_para_mark
Index: gdk-pixbuf/ChangeLog
===================================================================
RCS file: /cvs/gnome/gtk+/gdk-pixbuf/ChangeLog,v
retrieving revision 1.237
diff -u -r1.237 ChangeLog
--- gdk-pixbuf/ChangeLog 2000/06/22 15:36:11 1.237
+++ gdk-pixbuf/ChangeLog 2000/06/23 22:58:14
@@ -1,3 +1,14 @@
+2000-06-23 Havoc Pennington <hp@redhat.com>
+
+ * image-to-inline.c: Small program that creates C variable
+ declarations of inline pixbuf data. This can be read
+ by gdk_pixbuf_new_from_inline.
+
+ * gdk-pixbuf.h (gdk_pixbuf_new_from_inline): New function to read
+ inline pixbuf data and create a pixbuf from it.
+
+ * gdk-pixbuf-data.c (gdk_pixbuf_new_from_inline): implement here
+
2000-06-21 Havoc Pennington <hp@pobox.com>
* gdk-pixbuf.c: Convert GdkPixbuf to GObject, leaving it opaque
Index: gdk-pixbuf/Makefile.am
===================================================================
RCS file: /cvs/gnome/gtk+/gdk-pixbuf/Makefile.am,v
retrieving revision 1.69
diff -u -r1.69 Makefile.am
--- gdk-pixbuf/Makefile.am 2000/06/21 20:47:17 1.69
+++ gdk-pixbuf/Makefile.am 2000/06/23 22:58:14
@@ -138,6 +138,12 @@
test_gdk_pixbuf_LDADD = $(LDADDS) -lgmodule
+bin_PROGRAMS=image-to-inline
+
+image_to_inline_SOURCES=image-to-inline.c
+
+image_to_inline_LDADD = $(LDADDS) -lgmodule
+
GDK_PIXBUF_LIBS = $(GLIB_LIBS)
#
Index: gdk-pixbuf/gdk-pixbuf-data.c
===================================================================
RCS file: /cvs/gnome/gtk+/gdk-pixbuf/gdk-pixbuf-data.c,v
retrieving revision 1.10
diff -u -r1.10 gdk-pixbuf-data.c
--- gdk-pixbuf/gdk-pixbuf-data.c 2000/06/22 15:36:11 1.10
+++ gdk-pixbuf/gdk-pixbuf-data.c 2000/06/23 22:58:14
@@ -75,3 +75,98 @@
return pixbuf;
}
+
+int
+read_int (const guchar **p)
+{
+ guint32 num;
+
+ num = g_ntohl (* (guint32*) *p);
+
+ *p += 4;
+
+ return num;
+}
+
+gboolean
+read_bool (const guchar **p)
+{
+ gboolean val = **p != 0;
+
+ ++(*p);
+
+ return val;
+}
+
+/* sync with image-to-inline.c */
+#define MAGIC_NUMBER 1804289383
+
+static void
+free_buffer (guchar *pixels, gpointer data)
+{
+ free (pixels);
+}
+
+/**
+ * gdk_pixbuf_new_from_inline:
+ * @data: An inlined GdkPixbuf
+ * @copy_pixels: whether to copy the pixels out of the inline data, or to use them in-place
+ *
+ * Create a #GdkPixbuf from a custom format invented to store pixbuf
+ * data in C program code. This library comes with a program called "image-to-inline"
+ * that can write out a variable definition containing an inlined pixbuf.
+ * This is useful if you want to ship a program with images, but
+ * don't want to depend on any external files.
+ *
+ * The inline data format contains the pixels in #GdkPixbuf's native format.
+ * Since the inline pixbuf is static data, you don't really need to copy it.
+ * However it's typically in read-only memory, so if you plan to modify
+ * it you must copy it.
+ *
+ * Return value: A newly-created #GdkPixbuf structure with a reference count of
+ * 1.
+ **/
+GdkPixbuf*
+gdk_pixbuf_new_from_inline (const guchar *inline_pixbuf,
+ gboolean copy_pixels)
+{
+ const guchar *p;
+ GdkPixbuf *pixbuf;
+
+ p = inline_pixbuf;
+
+ if (read_int (&p) != MAGIC_NUMBER) {
+ g_warning ("Bad inline data; wrong magic number");
+ return NULL;
+ }
+
+ pixbuf = GDK_PIXBUF (g_type_create_instance (GDK_TYPE_PIXBUF));
+
+ pixbuf->rowstride = read_int (&p);
+ pixbuf->width = read_int (&p);
+ pixbuf->height = read_int (&p);
+ pixbuf->has_alpha = read_bool (&p);
+ pixbuf->colorspace = read_int (&p);
+ pixbuf->n_channels = read_int (&p);
+ pixbuf->bits_per_sample = read_int (&p);
+
+ if (copy_pixels) {
+ pixbuf->pixels = malloc (pixbuf->height * pixbuf->rowstride);
+
+ if (pixbuf->pixels == NULL) {
+ g_object_unref (G_OBJECT (pixbuf));
+ return NULL;
+ }
+
+ pixbuf->destroy_fn = free_buffer;
+ pixbuf->destroy_fn_data = NULL;
+
+ memcpy (pixbuf->pixels, p, pixbuf->height * pixbuf->rowstride);
+ } else {
+ pixbuf->pixels = (guchar *) p;
+ }
+
+
+ return pixbuf;
+}
+
Index: gdk-pixbuf/gdk-pixbuf.h
===================================================================
RCS file: /cvs/gnome/gtk+/gdk-pixbuf/gdk-pixbuf.h,v
retrieving revision 1.42
diff -u -r1.42 gdk-pixbuf.h
--- gdk-pixbuf/gdk-pixbuf.h 2000/06/22 15:36:12 1.42
+++ gdk-pixbuf/gdk-pixbuf.h 2000/06/23 22:58:14
@@ -103,6 +103,10 @@
GdkPixbuf *gdk_pixbuf_new_from_xpm_data (const char **data);
+/* Read an inline pixbuf */
+GdkPixbuf *gdk_pixbuf_new_from_inline (const guchar *inline_pixbuf,
+ gboolean copy_pixels);
+
/* Adding an alpha channel */
GdkPixbuf *gdk_pixbuf_add_alpha (const GdkPixbuf *pixbuf, gboolean substitute_color,
guchar r, guchar g, guchar b);
Index: gdk-pixbuf/image-to-inline.c
===================================================================
RCS file: image-to-inline.c
diff -N image-to-inline.c
--- /dev/null Tue May 5 16:32:27 1998
+++ image-to-inline.c Fri Jun 23 18:58:14 2000
@@ -0,0 +1,211 @@
+/* Program to convert an image file to inline C data
+ *
+ * Copyright (C) 2000 Red Hat, Inc.
+ *
+ * Developed by Havoc Pennington <hp@redhat.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 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
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include <config.h>
+#include "gdk-pixbuf.h"
+#include <stdio.h>
+#include <errno.h>
+#include <string.h>
+
+/* sync with gdk-pixbuf-data.c */
+#define MAGIC_NUMBER 1804289383
+
+void
+output_int (FILE *outfile, int number, const char *comment)
+{
+ guchar bytes[4];
+ guint32 num;
+
+ g_assert (sizeof (bytes) == sizeof (num));
+
+ num = number;
+ *((guint32*)bytes) = g_htonl (num);
+
+ fprintf(outfile, " /* %s (decimal: %u) */\n 0x%.2x, 0x%.2x, 0x%.2x, 0x%.2x,\n",
+ comment, num,
+ bytes[0], bytes[1], bytes[2], bytes[3]);
+}
+
+void
+output_bool (FILE *outfile, gboolean val, const char *comment)
+{
+ fprintf(outfile, " /* %s (%s) */\n 0x%.2x,\n",
+ comment,
+ val ? "TRUE" : "FALSE",
+ val ? 1 : 0);
+}
+
+void
+output_pixbuf (FILE *outfile, gboolean ext_symbols,
+ const char *varname,
+ GdkPixbuf *pixbuf)
+{
+ const char *modifier;
+ const guchar *p;
+ const guchar *end;
+ gboolean has_alpha;
+ int column;
+
+ modifier = "static ";
+ if (ext_symbols)
+ modifier = "";
+
+ fprintf (outfile, "%sconst guchar ", modifier);
+ fputs (varname, outfile);
+ fputs (" [] =\n", outfile);
+ fputs ("{\n", outfile);
+
+ p = gdk_pixbuf_get_pixels (pixbuf);
+ end = p + gdk_pixbuf_get_rowstride (pixbuf) * gdk_pixbuf_get_height (pixbuf);
+ has_alpha = gdk_pixbuf_get_has_alpha (pixbuf);
+
+ /* Sync the order of writing with the order of reading in
+ * gdk-pixbuf-data.c
+ */
+ output_int (outfile, MAGIC_NUMBER, "File magic");
+ output_int (outfile, gdk_pixbuf_get_rowstride (pixbuf), "Rowstride");
+ output_int (outfile, gdk_pixbuf_get_width (pixbuf), "Width");
+ output_int (outfile, gdk_pixbuf_get_height (pixbuf), "Height");
+
+ output_bool (outfile, has_alpha, "Has an alpha channel");
+
+ output_int (outfile, gdk_pixbuf_get_colorspace (pixbuf), "Colorspace (0 == RGB, no other options implemented)");
+
+ output_int (outfile, gdk_pixbuf_get_n_channels (pixbuf), "Number of channels");
+
+ output_int (outfile, gdk_pixbuf_get_bits_per_sample (pixbuf), "Bits per sample");
+
+ fputs (" /* Image data */\n", outfile);
+
+ /* Copy the data in the pixbuf */
+ column = 0;
+ while (p != end)
+ {
+ guchar r, g, b, a;
+
+ r = *p;
+ ++p;
+ g = *p;
+ ++p;
+ b = *p;
+ ++p;
+ if (has_alpha)
+ {
+ a = *p;
+ ++p;
+ }
+ else
+ a = 0;
+
+
+ if (has_alpha)
+ fprintf(outfile, " 0x%.2x, 0x%.2x, 0x%.2x, 0x%.2x", r, g, b, a);
+ else
+ fprintf(outfile, " 0x%.2x, 0x%.2x, 0x%.2x", r, g, b);
+
+ if (p != end)
+ fputs (",", outfile);
+ else
+ fputs ("\n", outfile);
+
+ ++column;
+
+ if (column > 2)
+ {
+ fputs ("\n", outfile);
+ column = 0;
+ }
+ }
+
+ fputs ("};\n\n", outfile);
+}
+
+void
+usage (void)
+{
+ fprintf (stderr, "Usage: image-to-inline [--extern-symbols] OUTFILE varname1 imagefile1 varname2 imagefile2 ...\n");
+ exit (1);
+}
+
+int
+main (int argc, char **argv)
+{
+ gboolean ext_symbols = FALSE;
+ FILE *outfile;
+ int i;
+
+ gdk_pixbuf_init ();
+
+ if (argc < 4)
+ usage ();
+
+ i = 1;
+ if (strcmp (argv[i], "--extern-symbols") == 0)
+ {
+ ext_symbols = TRUE;
+ ++i;
+ if (argc < 5)
+ usage ();
+ }
+
+ outfile = fopen (argv[i], "w");
+ if (outfile == NULL)
+ {
+ fprintf (stderr, "Failed to open output file `%s': %s\n",
+ argv[i], strerror (errno));
+ exit (1);
+ }
+
+ ++i;
+
+ fputs ("/* This file was automatically generated by the image-to-inline program.\n"
+ " * It contains inline RGB image data.\n"
+ " */\n\n", outfile);
+
+ while (i < argc)
+ {
+ GdkPixbuf *pixbuf;
+
+ if ((i + 1) == argc)
+ usage ();
+
+ pixbuf = gdk_pixbuf_new_from_file (argv[i+1]);
+
+ if (pixbuf == NULL)
+ {
+ fprintf (stderr, "Failed to open image file `%s': %s\n",
+ argv[i+1], strerror (errno));
+
+ exit (1);
+ }
+
+ output_pixbuf (outfile, ext_symbols, argv[i], pixbuf);
+
+ gdk_pixbuf_unref (pixbuf);
+
+ i += 2;
+ }
+
+ fclose (outfile);
+
+ return 0;
+}
Index: demos/Makefile.am
===================================================================
RCS file: /cvs/gnome/gtk+/demos/Makefile.am,v
retrieving revision 1.2
diff -u -r1.2 Makefile.am
--- demos/Makefile.am 2000/06/22 00:05:44 1.2
+++ demos/Makefile.am 2000/06/23 22:58:14
@@ -37,6 +37,11 @@
testpixbuf-scale \
pixbuf-demo
+BUILT_SOURCES=test-inline-pixbufs.h
+
+test-inline-pixbufs.h: $(top_builddir)/gdk-pixbuf/image-to-inline apple-red.png gnome-foot.png
+ GDK_PIXBUF_MODULEDIR=$(top_builddir)/gdk-pixbuf/.libs $(top_builddir)/gdk-pixbuf/image-to-inline test-inline-pixbufs.h apple_red apple-red.png gnome_foot gnome-foot.png
+
testpixbuf_DEPENDENCIES = $(DEPS)
testpixbuf_drawable_DEPENDENCIES = $(DEPS)
testpixbuf_scale_DEPENDENCIES = $(DEPS)
Index: demos/testpixbuf.c
===================================================================
RCS file: /cvs/gnome/gtk+/demos/testpixbuf.c,v
retrieving revision 1.36
diff -u -r1.36 testpixbuf.c
--- demos/testpixbuf.c 2000/06/22 15:36:09 1.36
+++ demos/testpixbuf.c 2000/06/23 22:58:14
@@ -25,6 +25,8 @@
#include <gtk/gtk.h>
#include <gtk/gdk-pixbuf-loader.h>
+#include "test-inline-pixbufs.h"
+
typedef struct {
FILE *imagefile;
GdkPixbufLoader *loader;
@@ -375,7 +377,7 @@
window = gtk_widget_new (gtk_window_get_type (),
"GtkObject::user_data", NULL,
"GtkWindow::type", GTK_WINDOW_TOPLEVEL,
- "GtkWindow::title", "testrgb",
+ "GtkWindow::title", title ? title : "testrgb",
"GtkWindow::allow_shrink", TRUE,
NULL);
gtk_signal_connect (GTK_OBJECT (window), "destroy",
@@ -536,6 +538,13 @@
new_testrgb_window (pixbuf, NULL);
++xpmp;
}
+
+ /* Test loading from inline data. */
+ pixbuf = gdk_pixbuf_new_from_inline (apple_red, FALSE);
+ new_testrgb_window (pixbuf, "Red apple from inline data");
+
+ pixbuf = gdk_pixbuf_new_from_inline (gnome_foot, TRUE);
+ new_testrgb_window (pixbuf, "Foot from inline data");
found_valid = TRUE;
} else {
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]