gnome-nds-thumbnailer r11 - trunk



Author: hadess
Date: Thu May  1 16:29:33 2008
New Revision: 11
URL: http://svn.gnome.org/viewvc/gnome-nds-thumbnailer?rev=11&view=rev

Log:
2008-05-01  Bastien Nocera  <hadess hadess net>

	* gnome-nds-thumbnailer.c (main): And port to GIO
	proper



Modified:
   trunk/ChangeLog
   trunk/gnome-nds-thumbnailer.c

Modified: trunk/gnome-nds-thumbnailer.c
==============================================================================
--- trunk/gnome-nds-thumbnailer.c	(original)
+++ trunk/gnome-nds-thumbnailer.c	Thu May  1 16:29:33 2008
@@ -28,13 +28,19 @@
 /* From specs at http://www.bottledlight.com/ds/index.php/FileFormats/NDSFormat
  * and code at http://www.kde-apps.org/content/show.php?content=39247 */
 
-#define CHECK_BOUND(x) {											\
-	if (x >= g_mapped_file_get_length (map) || x < 0) {							\
+#define BOUND_ERROR(x) {											\
+	if (error != NULL) {											\
+		g_warning ("Couldn't access file data at 0x%x, probably not a NDS ROM: %s", x, error->message);	\
+		g_error_free (error);										\
+	} else													\
 		g_warning ("Couldn't access file data at 0x%x, probably not a NDS ROM", x);			\
-		return 1;											\
-	}													\
+	if (stream != NULL)											\
+		g_object_unref (stream);									\
+	return 1;												\
 }
 
+#define LOGO_OFFSET_OFFSET 0x068
+#define BANNER_LENGTH 2112
 #define TILE_DATA_OFFSET 32
 #define TILE_DATA_LENGTH 512
 #define PALETTE_DATA_OFFSET TILE_DATA_OFFSET + 512
@@ -124,8 +130,12 @@
 	GdkPixbuf *pixbuf, *scaled;
 	GError *error = NULL;
 	GOptionContext *context;
+	GFile *input;
 	const char *output;
-	char *input;
+	GFileInputStream *stream;
+
+	guint32 logo_offset[4];
+	char *banner_data;
 
 	/* Options parsing */
 	context = g_option_context_new ("Thumbnail Nintendo DS ROMs");
@@ -152,64 +162,59 @@
 		return 1;
 	}
 
-	{
-		GFile *file;
-
-		file = g_file_new_for_commandline_arg (filenames[0]);
-		input = g_file_get_path (file);
-		g_object_unref (file);
-	}
-
-	if (input == NULL) {
-		g_print ("Only local files are supported\n");
-		return 1;
-	}
-
+	input = g_file_new_for_commandline_arg (filenames[0]);
 	output = filenames[1];
 
-	map = g_mapped_file_new (input, FALSE, &error);
-	if (!map) {
-		g_warning ("Couldn't map %s: %s", input, error->message);
-		g_free (input);
+	/* Open the file for reading */
+	stream = g_file_read (input, NULL, &error);
+	g_object_unref (input);
+
+	if (stream == NULL) {
+		g_warning ("Couldn't open '%s': %s", filenames[0], error->message);
 		g_error_free (error);
 		return 1;
 	}
 
-	base = g_mapped_file_get_contents (map);
-
 	/* Get the address of the logo */
-	CHECK_BOUND(0x068);
-	offset = GUINT32_FROM_LE(*((guint32 *) (base + 0x068)));
-	CHECK_BOUND(offset);
+	if (g_input_stream_skip (G_INPUT_STREAM (stream), LOGO_OFFSET_OFFSET, NULL, &error) == FALSE)
+		BOUND_ERROR(LOGO_OFFSET_OFFSET);
+	if (g_input_stream_read (G_INPUT_STREAM (stream), &logo_offset, sizeof(guint32), NULL, &error) == FALSE)
+		BOUND_ERROR(LOGO_OFFSET_OFFSET);
+	offset = GUINT32_FROM_LE(*logo_offset) - g_seekable_tell (G_SEEKABLE (stream));
+
+	/* Get the icon data */
+	if (g_input_stream_skip (G_INPUT_STREAM (stream), offset, NULL, &error) != offset)
+		BOUND_ERROR(offset);
+	banner_data = g_malloc0(BANNER_LENGTH);
+	if (g_input_stream_read (G_INPUT_STREAM (stream), banner_data, BANNER_LENGTH, NULL, &error) != BANNER_LENGTH)
+		BOUND_ERROR(LOGO_OFFSET_OFFSET);
+
+	g_input_stream_close (G_INPUT_STREAM (stream), NULL, NULL);
+	g_object_unref (stream);
 
 	/* Check the version is version 1 */
-	if (base[offset] != 0x1 || base[offset + 1] != 0x0) {
+	if (banner_data[0] != 0x1 || banner_data[1] != 0x0) {
+		g_free (banner_data);
 		g_warning ("Unsupported icon version, probably not an NDS file");
-		g_free (input);
 		return 1;
 	}
 
 	/* Get the tile and palette data for the logo */
-	CHECK_BOUND((int) (offset + TILE_DATA_OFFSET + TILE_DATA_LENGTH));
-	tile_data = g_memdup (base + offset + TILE_DATA_OFFSET, TILE_DATA_LENGTH);
-	CHECK_BOUND((int) (offset + PALETTE_DATA_OFFSET + PALETTE_DATA_LENGTH));
-	palette_data = g_memdup (base + offset + PALETTE_DATA_OFFSET, PALETTE_DATA_LENGTH);
+	tile_data = g_memdup (banner_data + TILE_DATA_OFFSET, TILE_DATA_LENGTH);
+	palette_data = g_memdup (banner_data + PALETTE_DATA_OFFSET, PALETTE_DATA_LENGTH);
+	g_free (banner_data);
 	pixbuf = load_icon (tile_data, palette_data);
 	g_free (palette_data);
 	g_free (tile_data);
 
-	g_mapped_file_free (map);
-
 	scaled = gdk_pixbuf_scale_simple (pixbuf, output_size, output_size, 0);
 	g_object_unref (pixbuf);
 	if (gdk_pixbuf_save (scaled, output, "png", &error, NULL) == FALSE) {
-		g_warning ("Couldn't save the thumbnail '%s' for file '%s': %s", output, input, error->message);
-		g_free (input);
+		g_warning ("Couldn't save the thumbnail '%s' for file '%s': %s", output, filenames[0], error->message);
 		g_error_free (error);
 		return 1;
 	}
 
-	g_free (input);
 	g_object_unref (scaled);
 
 	return 0;



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