Re: inline pixbufs



Darin Adler <darin@eazel.com> writes: 
> Are you sure it's really a 5-minute task? Bonobo requires a flatten as well
> as an unflatten. And there's also the [admittedly tiny] issue of
> CORBA_sequence_CORBA_octet_allocbuf vs. malloc.
> 

It might be 10-15 minutes, plus seeing if it compiles. ;-) Anyway I'm
sure it won't be too bad.
 
> I look forward to helping out by reviewing your code to see if I can spot
> any missing cases, since there are many interactions between the various
> parameters.
> 

Please do. My approach is to just reject anything that isn't RGB or
RGBA, and barf if width > rowstride, and verify that the length is at
least rowstride*height. I think the interactions don't get too nasty
until we support another format or two.

Havoc

Index: ChangeLog
===================================================================
RCS file: /cvs/gnome/gtk+/gdk-pixbuf/ChangeLog,v
retrieving revision 1.242
diff -u -u -r1.242 ChangeLog
--- ChangeLog	2000/07/22 23:50:18	1.242
+++ ChangeLog	2000/07/26 18:58:12
@@ -1,3 +1,20 @@
+2000-06-26  Havoc Pennington  <hp@redhat.com>
+
+	* gdk-pixbuf-private.h (GdkPixbufInlineFormat): include an 
+	enum here for the known formats of inlined pixbufs.
+	Also, #define our file magic here.
+
+2000-06-23  Havoc Pennington  <hp@redhat.com>
+
+	* make-inline-pixbuf.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-07-23  Tor Lillqvist  <tml@iki.fi>
 
 	Win32 build setup:
@@ -58,7 +75,7 @@
 
 	* Makefile.am (libgdk_pixbuf_la_LDFLAGS): Use GTK+ version
 	soname scheme for gdk-pixbuf.
-
+	
 2000-06-21  Havoc Pennington  <hp@pobox.com>
 
 	* gdk-pixbuf.c: Convert GdkPixbuf to GObject, leaving it opaque
Index: Makefile.am
===================================================================
RCS file: /cvs/gnome/gtk+/gdk-pixbuf/Makefile.am,v
retrieving revision 1.73
diff -u -u -r1.73 Makefile.am
--- Makefile.am	2000/07/22 23:50:18	1.73
+++ Makefile.am	2000/07/26 18:58:12
@@ -138,6 +138,12 @@
 
 test_gdk_pixbuf_LDADD = $(LDADDS)
 
+bin_PROGRAMS=make-inline-pixbuf
+
+make_inline_pixbuf_SOURCES=make-inline-pixbuf.c
+
+make_inline_pixbuf_LDADD = $(LDADDS) -lgmodule
+
 GDK_PIXBUF_LIBS = $(GLIB_LIBS)
 
 #
Index: gdk-pixbuf-data.c
===================================================================
RCS file: /cvs/gnome/gtk+/gdk-pixbuf/gdk-pixbuf-data.c,v
retrieving revision 1.12
diff -u -u -r1.12 gdk-pixbuf-data.c
--- gdk-pixbuf-data.c	2000/07/26 11:32:38	1.12
+++ gdk-pixbuf-data.c	2000/07/26 18:58:12
@@ -23,6 +23,8 @@
 #include <config.h>
 #include "gdk-pixbuf.h"
 #include "gdk-pixbuf-private.h"
+#include <stdlib.h>
+#include <string.h>
 
 
 
@@ -75,3 +77,167 @@
 
 	return pixbuf;
 }
+
+static guint32
+read_int (const guchar **p)
+{
+        guint32 num;
+
+        /* Note most significant bytes are first in the byte stream */
+        num =
+          (*p)[3]         |
+          ((*p)[2] << 8)  |
+          ((*p)[1] << 16) |
+          ((*p)[0] << 24);
+
+        *p += 4;
+
+        return num;
+}
+
+static gboolean
+read_bool (const guchar **p)
+{
+        gboolean val = **p != 0;
+        
+        ++(*p);
+        
+        return val;
+}
+
+static GdkPixbuf*
+read_raw_inline (const guchar *data, gboolean copy_pixels, int length)
+{
+        GdkPixbuf *pixbuf;
+        const guchar *p = data;
+        guint32 rowstride, width, height, colorspace,
+                n_channels, bits_per_sample;
+        gboolean has_alpha;
+        
+        if (length >= 0 && length < 12) {
+                /* Not enough buffer to hold the width/height/rowstride */
+                return NULL;
+        }
+
+        rowstride = read_int (&p);
+        width = read_int (&p);
+        height = read_int (&p);
+
+        if (rowstride < width)
+                return NULL; /* bad data from untrusted source. */
+
+        /* rowstride >= width, so we can trust width */
+        
+        length -= 12;
+        
+        if (length >= 0 &&
+            length < (height * rowstride + 13)) {
+                /* Not enough buffer to hold the remaining header
+                 * information plus the data.
+                 */
+                
+                return NULL;
+        }
+        
+        /* Read the remaining 13 bytes of header information */
+            
+        has_alpha = read_bool (&p);
+        colorspace = read_int (&p);
+        n_channels = read_int (&p);
+        bits_per_sample = read_int (&p);
+
+        if (colorspace != GDK_COLORSPACE_RGB)
+                return NULL;
+
+        if (bits_per_sample != 8)
+                return NULL;
+
+        if (n_channels < 3 || n_channels > 4)
+                return NULL;
+        
+        if (copy_pixels) {
+                guchar *pixels;
+                gint dest_rowstride;
+                gint row;
+                
+                pixbuf = gdk_pixbuf_new (colorspace,
+                                         has_alpha, bits_per_sample,
+                                         width, height);
+
+                pixels = gdk_pixbuf_get_pixels (pixbuf);
+                dest_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
+	
+                for (row = 0; row < height; row++) {
+                        memcpy (pixels, p, rowstride);
+                        pixels += dest_rowstride;
+                        p += rowstride;
+                }
+        } else {
+                pixbuf = gdk_pixbuf_new_from_data (p,
+                                                   colorspace,
+                                                   has_alpha,
+                                                   bits_per_sample,
+                                                   width, height,
+                                                   rowstride,
+                                                   NULL, NULL);
+        }
+
+        return pixbuf;
+}
+
+/**
+ * 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,
+                              int           length)
+{
+        const guchar *p;
+        GdkPixbuf *pixbuf;
+        GdkPixbufInlineFormat format;
+
+        if (length >= 0 && length < 8) {
+                /* not enough bytes to contain even the magic number
+                 * and format code.
+                 */
+                return NULL;
+        }
+        
+        p = inline_pixbuf;
+
+        if (read_int (&p) != GDK_PIXBUF_INLINE_MAGIC_NUMBER) {
+                return NULL;
+        }
+
+        format = read_int (&p);
+
+        switch (format)
+        {
+        case GDK_PIXBUF_INLINE_RAW:
+                pixbuf = read_raw_inline (p, copy_pixels, length - 8);
+                break;
+
+        default:
+                return NULL;
+        }
+
+        return pixbuf;
+}
+
Index: gdk-pixbuf-private.h
===================================================================
RCS file: /cvs/gnome/gtk+/gdk-pixbuf/gdk-pixbuf-private.h,v
retrieving revision 1.4
diff -u -u -r1.4 gdk-pixbuf-private.h
--- gdk-pixbuf-private.h	2000/07/26 11:32:39	1.4
+++ gdk-pixbuf-private.h	2000/07/26 18:58:12
@@ -117,4 +117,14 @@
 
 
 
+#define GDK_PIXBUF_INLINE_MAGIC_NUMBER 0x47646B50 /* 'GdkP' */
+
+typedef enum
+{
+  GDK_PIXBUF_INLINE_RAW = 0,
+  GDK_PIXBUF_INLINE_RLE = 1
+} GdkPixbufInlineFormat;
+
+
+
 #endif
Index: gdk-pixbuf.h
===================================================================
RCS file: /cvs/gnome/gtk+/gdk-pixbuf/gdk-pixbuf.h,v
retrieving revision 1.44
diff -u -u -r1.44 gdk-pixbuf.h
--- gdk-pixbuf.h	2000/07/26 11:32:40	1.44
+++ gdk-pixbuf.h	2000/07/26 18:58:12
@@ -103,6 +103,11 @@
 
 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,
+                                         int           length);
+
 /* Adding an alpha channel */
 GdkPixbuf *gdk_pixbuf_add_alpha (const GdkPixbuf *pixbuf, gboolean substitute_color,
 				 guchar r, guchar g, guchar b);
Index: make-inline-pixbuf.c
===================================================================
RCS file: make-inline-pixbuf.c
diff -N make-inline-pixbuf.c
--- /dev/null	Tue May  5 16:32:27 1998
+++ make-inline-pixbuf.c	Wed Jul 26 14:58:12 2000
@@ -0,0 +1,212 @@
+/* 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-private.h"
+#include <stdio.h>
+#include <errno.h>
+#include <string.h>
+#include <stdlib.h>
+
+void
+output_int (FILE *outfile, guint32 num, const char *comment)
+{
+  guchar bytes[4];
+
+  g_assert (sizeof (bytes) == sizeof (num));
+
+  /* Note, most significant bytes first */
+  bytes[0] = num >> 24;
+  bytes[1] = num >> 16;
+  bytes[2] = num >> 8;
+  bytes[3] = 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, GDK_PIXBUF_INLINE_MAGIC_NUMBER, "File magic");
+  output_int (outfile, GDK_PIXBUF_INLINE_RAW, "Format of following stuff");
+  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: make-inline-pixbuf [--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;
+}





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