guint xcf_read_int32 (FILE *fp, guint32 *data, gint count) { guint total = 0; if (count > 0) { total += xcf_read_int8 (fp, (guint8 *) data, count * 4); while (count--) { *data = g_ntohl (*data); data++; } } return total; }
guint xcf_read_string (FILE *fp, gchar **data, gint count) { guint total = 0; gint i; for (i = 0; i < count; i++) { guint32 tmp; total += xcf_read_int32 (fp, &tmp, 1); if (tmp > 0) { gchar *str; str = g_new (gchar, tmp); total += xcf_read_int8 (fp, (guint8*) str, tmp); if (str[tmp - 1] != '\0') str[tmp - 1] = '\0'; data[i] = gimp_any_to_utf8 (str, -1, _("Invalid UTF-8 string in XCF file")); g_free (str); } else { data[i] = NULL; } } return total; }
static GimpValueArray * xcf_load_invoker (GimpProcedure *procedure, Gimp *gimp, GimpContext *context, GimpProgress *progress, const GimpValueArray *args, GError **error) { XcfInfo info; GimpValueArray *return_vals; GimpImage *image = NULL; const gchar *filename; gboolean success = FALSE; gchar id[14]; gimp_set_busy (gimp); filename = g_value_get_string (gimp_value_array_index (args, 1)); info.fp = g_fopen (filename, "rb"); if (info.fp) { info.gimp = gimp; info.progress = progress; info.cp = 0; info.filename = filename; info.tattoo_state = 0; info.active_layer = NULL; info.active_channel = NULL; info.floating_sel_drawable = NULL; info.floating_sel = NULL; info.floating_sel_offset = 0; info.swap_num = 0; info.ref_count = NULL; info.compression = COMPRESS_NONE; if (progress) { gchar *name = g_filename_display_name (filename); gchar *msg = g_strdup_printf (_("Opening '%s'"), name); gimp_progress_start (progress, msg, FALSE); g_free (msg); g_free (name); } success = TRUE; info.cp += xcf_read_int8 (info.fp, (guint8 *) id, 14); if (! g_str_has_prefix (id, "gimp xcf ")) { success = FALSE; } else if (strcmp (id + 9, "file") == 0) { info.file_version = 0; } else if (id[9] == 'v') { info.file_version = atoi (id + 10); } else { success = FALSE; } if (success) { if (info.file_version >= 0 && info.file_version < G_N_ELEMENTS (xcf_loaders)) { image = (*(xcf_loaders[info.file_version])) (gimp, &info, error); if (! image) success = FALSE; } else { g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED, _("XCF error: unsupported XCF file version %d " "encountered"), info.file_version); success = FALSE; } } fclose (info.fp); if (progress) gimp_progress_end (progress); } else { int save_errno = errno; g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (save_errno), _("Could not open '%s' for reading: %s"), gimp_filename_to_utf8 (filename), g_strerror (save_errno)); } return_vals = gimp_procedure_get_return_values (procedure, success, error ? *error : NULL); if (success) gimp_value_set_image (gimp_value_array_index (return_vals, 1), image); gimp_unset_busy (gimp); return return_vals; }
static GimpValueArray * xcf_load_invoker (GimpProcedure *procedure, Gimp *gimp, GimpContext *context, GimpProgress *progress, const GimpValueArray *args, GError **error) { XcfInfo info = { 0, }; GimpValueArray *return_vals; GimpImage *image = NULL; const gchar *uri; gchar *filename; GFile *file; gboolean success = FALSE; gchar id[14]; GError *my_error = NULL; gimp_set_busy (gimp); uri = g_value_get_string (gimp_value_array_index (args, 1)); #ifdef GIO_IS_FIXED file = g_file_new_for_uri (uri); #else file = g_file_new_for_path (uri); #endif filename = g_file_get_parse_name (file); info.input = G_INPUT_STREAM (g_file_read (file, NULL, &my_error)); if (info.input) { info.gimp = gimp; info.seekable = G_SEEKABLE (info.input); info.progress = progress; info.filename = filename; info.compression = COMPRESS_NONE; if (progress) { gchar *name = g_filename_display_name (filename); gchar *msg = g_strdup_printf (_("Opening '%s'"), name); gimp_progress_start (progress, msg, FALSE); g_free (msg); g_free (name); } success = TRUE; info.cp += xcf_read_int8 (info.input, (guint8 *) id, 14); if (! g_str_has_prefix (id, "gimp xcf ")) { success = FALSE; } else if (strcmp (id + 9, "file") == 0) { info.file_version = 0; } else if (id[9] == 'v') { info.file_version = atoi (id + 10); } else { success = FALSE; } if (success) { if (info.file_version >= 0 && info.file_version < G_N_ELEMENTS (xcf_loaders)) { image = (*(xcf_loaders[info.file_version])) (gimp, &info, error); if (! image) success = FALSE; } else { g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED, _("XCF error: unsupported XCF file version %d " "encountered"), info.file_version); success = FALSE; } } g_object_unref (info.input); if (progress) gimp_progress_end (progress); } else { g_propagate_prefixed_error (error, my_error, _("Could not open '%s' for reading: "), filename); } g_free (filename); g_object_unref (file); return_vals = gimp_procedure_get_return_values (procedure, success, error ? *error : NULL); if (success) gimp_value_set_image (gimp_value_array_index (return_vals, 1), image); gimp_unset_busy (gimp); return return_vals; }