gdk-pixbuf-csource.c 7.35 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* Gdk-Pixbuf-CSource - GdkPixbuf based image CSource generator
 * Copyright (C) 1999, 2001 Tim Janik
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser 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"

21
#include "../gtk/gtkversion.h"	/* versioning */
22 23 24
#include "gdk-pixbuf.h"
#include "gdk-pixdata.h"
#include <stdio.h>
25
#include <stdlib.h>
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
#include <string.h>


/* --- defines --- */
#undef	G_LOG_DOMAIN
#define	G_LOG_DOMAIN	"Gdk-Pixbuf-CSource"
#define PRG_NAME        "gdk-pixbuf-csource"
#define PKG_NAME        "Gtk+"
#define PKG_HTTP_HOME   "http://www.gtk.org"


/* --- prototypes --- */
static void	parse_args	(gint    *argc_p,
				 gchar ***argv_p);
static void	print_blurb	(FILE    *bout,
				 gboolean print_help);


/* --- variables --- */
static guint    gen_type = GDK_PIXDATA_DUMP_PIXDATA_STREAM;
static guint    gen_ctype = GDK_PIXDATA_DUMP_GTYPES | GDK_PIXDATA_DUMP_STATIC | GDK_PIXDATA_DUMP_CONST;
static gboolean use_rle = TRUE;
static gboolean with_decoder = FALSE;
static gchar   *image_name = "my_pixbuf";
static gboolean	build_list = FALSE;


/* --- functions --- */
static void
print_csource (FILE *f_out,
	       GdkPixbuf *pixbuf)
{
  GdkPixdata pixdata;
  gpointer free_me;
  GString *gstring;

  free_me = gdk_pixdata_from_pixbuf (&pixdata, pixbuf, use_rle);
  gstring = gdk_pixdata_to_csource (&pixdata, image_name,
				    gen_type | gen_ctype |
				    (with_decoder ? GDK_PIXDATA_DUMP_RLE_DECODER : 0));

  fprintf (f_out, "%s\n", gstring->str);

  g_free (free_me);
}

int
main (int   argc,
      char *argv[])
{
  GdkPixbuf *pixbuf;
  GError *error = NULL;

  /* initialize glib/GdkPixbuf */
Owen Taylor's avatar
Owen Taylor committed
80
  g_type_init ();
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103

  /* parse args and do fast exits */
  parse_args (&argc, &argv);

  if (!build_list)
    {
      if (argc != 2)
	{
	  print_blurb (stderr, TRUE);
	  return 1;
	}
      
      pixbuf = gdk_pixbuf_new_from_file (argv[1], &error);
      if (!pixbuf)
	{
	  fprintf (stderr, "failed to load \"%s\": %s\n",
		   argv[1],
		   error->message);
	  g_error_free (error);
	  return 1;
	}
      
      print_csource (stdout, pixbuf);
104
      g_object_unref (pixbuf);
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
    }
  else /* parse name, file pairs */
    {
      gchar **p = argv + 1;
      guint j = argc - 1;
      gboolean toggle = FALSE;

      while (j--)
	{
	  if (!toggle)
	    image_name = *p++;
	  else
	    {
	      pixbuf = gdk_pixbuf_new_from_file (*p, &error);
	      if (!pixbuf)
		{
		  fprintf (stderr, "failed to load \"%s\": %s\n",
			   *p,
			   error->message);
		  g_error_free (error);
		  return 1;
		}
	      print_csource (stdout, pixbuf);
128
	      g_object_unref (pixbuf);
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
	      p++;
	    }
	  toggle = !toggle;
	}
    }
  
  return 0;
}

static void
parse_args (gint    *argc_p,
	    gchar ***argv_p)
{
  guint argc = *argc_p;
  gchar **argv = *argv_p;
  guint i, e;

  for (i = 1; i < argc; i++)
    {
      if (strcmp ("--macros", argv[i]) == 0)
	{
	  gen_type = GDK_PIXDATA_DUMP_MACROS;
	  argv[i] = NULL;
	}
      else if (strcmp ("--struct", argv[i]) == 0)
	{
	  gen_type = GDK_PIXDATA_DUMP_PIXDATA_STRUCT;
	  argv[i] = NULL;
	}
      else if (strcmp ("--stream", argv[i]) == 0)
	{
	  gen_type = GDK_PIXDATA_DUMP_PIXDATA_STREAM;
	  argv[i] = NULL;
	}
      else if (strcmp ("--rle", argv[i]) == 0)
	{
	  use_rle = TRUE;
	  argv[i] = NULL;
	}
      else if (strcmp ("--raw", argv[i]) == 0)
	{
	  use_rle = FALSE;
	  argv[i] = NULL;
	}
      else if (strcmp ("--extern", argv[i]) == 0)
	{
	  gen_ctype &= ~GDK_PIXDATA_DUMP_STATIC;
	  argv[i] = NULL;
	}
      else if (strcmp ("--static", argv[i]) == 0)
	{
	  gen_ctype |= GDK_PIXDATA_DUMP_STATIC;
	  argv[i] = NULL;
	}
      else if (strcmp ("--decoder", argv[i]) == 0)
	{
	  with_decoder = TRUE;
	  argv[i] = NULL;
	}
      else if ((strcmp ("--name", argv[i]) == 0) ||
	       (strncmp ("--name=", argv[i], 7) == 0))
	{
	  gchar *equal = argv[i] + 6;

	  if (*equal == '=')
	    image_name = g_strdup (equal + 1);
	  else if (i + 1 < argc)
	    {
	      image_name = g_strdup (argv[i + 1]);
	      argv[i] = NULL;
	      i += 1;
	    }
	  argv[i] = NULL;
	}
      else if (strcmp ("--build-list", argv[i]) == 0)
	{
	  build_list = TRUE;
	  argv[i] = NULL;
	}
      else if (strcmp ("-h", argv[i]) == 0 ||
	       strcmp ("--help", argv[i]) == 0)
	{
	  print_blurb (stderr, TRUE);
	  argv[i] = NULL;
	  exit (0);
	}
      else if (strcmp ("-v", argv[i]) == 0 ||
	       strcmp ("--version", argv[i]) == 0)
	{
	  print_blurb (stderr, FALSE);
	  argv[i] = NULL;
	  exit (0);
	}
      else if (strcmp (argv[i], "--g-fatal-warnings") == 0)
	{
	  GLogLevelFlags fatal_mask;

	  fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
	  fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
	  g_log_set_always_fatal (fatal_mask);

	  argv[i] = NULL;
	}
    }

  e = 0;
  for (i = 1; i < argc; i++)
    {
      if (e)
	{
	  if (argv[i])
	    {
	      argv[e++] = argv[i];
	      argv[i] = NULL;
	    }
	}
      else if (!argv[i])
	e = i;
    }
  if (e)
    *argc_p = e;
}

static void
print_blurb (FILE    *bout,
	     gboolean print_help)
{
  if (!print_help)
    {
      fprintf (bout, "%s version ", PRG_NAME);
      fprintf (bout, "%u.%u.%u", GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION);
      fprintf (bout, "\n");
      fprintf (bout, "%s comes with ABSOLUTELY NO WARRANTY.\n", PRG_NAME);
      fprintf (bout, "You may redistribute copies of %s under the terms of\n", PRG_NAME);
      fprintf (bout, "the GNU Lesser General Public License which can be found in the\n");
      fprintf (bout, "%s source package. Sources, examples and contact\n", PKG_NAME);
      fprintf (bout, "information are available at %s\n", PKG_HTTP_HOME);
    }
  else
    {
      fprintf (bout, "Usage: %s [options] [image]\n", PRG_NAME);
      fprintf (bout, "       %s [options] --build-list [[name image]...]\n", PRG_NAME);
      fprintf (bout, "  --stream                   generate pixbuf data stream\n");
      fprintf (bout, "  --struct                   generate GdkPixdata structure\n");
      fprintf (bout, "  --macros                   generate image size/pixel macros\n");
      fprintf (bout, "  --rle                      use one byte run-length-encoding\n");
      fprintf (bout, "  --raw                      provide raw image data copy\n");
      fprintf (bout, "  --extern                   generate extern symbols\n");
      fprintf (bout, "  --static                   generate static symbols\n");
      fprintf (bout, "  --decoder                  provide rle decoder\n");
      fprintf (bout, "  --name=identifier          C macro/variable name\n");
      fprintf (bout, "  --build-list		   parse (name, image) pairs\n");
      fprintf (bout, "  -h, --help                 show this help message\n");
      fprintf (bout, "  -v, --version              print version informations\n");
      fprintf (bout, "  --g-fatal-warnings         make warnings fatal (abort)\n");
    }
}