gint export_image(gchar *filename, GHashTable *cache, RSOutput *output, RSFilter *filter, gint snapshot, double exposure, gchar *outputname, gint boundingbox, RSFilter *resample) { RS_PHOTO *photo = NULL; if (cache) { photo = (RS_PHOTO *) g_hash_table_lookup(cache, filename); if (!photo) { photo = rs_photo_load_from_file(filename); g_hash_table_insert(cache, filename, photo); printf("Adding %s to cache\n", filename); } } else photo = rs_photo_load_from_file(filename); if (photo) { rs_metadata_load_from_file(photo->metadata, filename); rs_cache_load(photo); GList *filters = g_list_append(NULL, filter); rs_photo_set_exposure(photo, 0, exposure); rs_photo_apply_to_filters(photo, filters, snapshot); if (boundingbox > 0) { rs_filter_set_enabled(resample, TRUE); rs_filter_set_recursive(filter, "image", photo->input_response, "filename", photo->filename, "bounding-box", TRUE, "width", boundingbox, "height", boundingbox, NULL); } else { rs_filter_set_enabled(resample, FALSE); rs_filter_set_recursive(filter, "image", photo->input_response, "filename", photo->filename, NULL); } if (g_object_class_find_property(G_OBJECT_GET_CLASS(output), "filename")) g_object_set(output, "filename", outputname, NULL); rs_output_set_from_conf(output, "batch"); rs_output_execute(output, filter); gint value = calculate_lightness(filter); printf("%s: %d\n", filename, value); g_list_free(filters); return value; } else return -1; }
gboolean rs_external_editor_gimp(RS_PHOTO *photo, RSFilter *prior_to_resample, guint snapshot) { #ifdef WIN32 return FALSE; #else RSOutput *output = NULL; g_assert(RS_IS_PHOTO(photo)); // We need at least GIMP 2.4.0 to export photo if (!rs_has_gimp(2,4,0)) { return FALSE; } DBusConnection *bus; DBusMessage *message, *reply; GString *filename; bus = dbus_bus_get (DBUS_BUS_SESSION, NULL); gchar* org_name = g_path_get_basename(photo->filename); gchar* org_name_noext = g_utf8_strchr(org_name, -1, '.'); /* Terminate string there */ if (NULL != org_name_noext) org_name_noext[0] = 0; filename = g_string_new(""); g_string_printf(filename, "%s/%s-rawstudio_%.0f.png",g_get_tmp_dir(), org_name, g_random_double()*10000); g_free(org_name); /* Setup our filter chain for saving */ RSFilter *ftransform_input = rs_filter_new("RSColorspaceTransform", prior_to_resample); RSFilter *fdcp = rs_filter_new("RSDcp", ftransform_input); RSFilter *fdenoise= rs_filter_new("RSDenoise", fdcp); RSFilter *ftransform_display = rs_filter_new("RSColorspaceTransform", fdenoise); RSFilter *fend = ftransform_display; GList *filters = g_list_append(NULL, fend); rs_photo_apply_to_filters(photo, filters, snapshot); g_list_free(filters); output = rs_output_new("RSPngfile"); g_object_set(output, "filename", filename->str, NULL); g_object_set(output, "save16bit", FALSE, NULL); g_object_set(output, "copy-metadata", TRUE, NULL); rs_output_execute(output, fend); g_object_unref(output); g_object_unref(ftransform_input); g_object_unref(ftransform_display); g_object_unref(fdenoise); g_object_unref(fdcp); message = dbus_message_new_method_call("org.gimp.GIMP.UI", "/org/gimp/GIMP/UI", "org.gimp.GIMP.UI", "OpenAsNew"); dbus_message_append_args (message, DBUS_TYPE_STRING, &filename->str, DBUS_TYPE_INVALID); /* Send DBus message to GIMP */ reply = dbus_connection_send_with_reply_and_block (bus, message, -1, NULL); /* If we didn't get a reply from GIMP - we try to start it and resend the message */ if (!reply) { gint retval = system("gimp &"); if (retval != 0) { g_warning("system(\"gimp &\") returned: %d\n", retval); g_unlink(filename->str); g_string_free(filename, TRUE); dbus_message_unref (message); return FALSE; } } /* Allow GIMP to start - we send the message every one second */ while (!reply) { gint i = 0; if (i > EXPORT_TO_GIMP_TIMEOUT_SECONDS) { g_warning("Never got a reply from GIMP - deleting temporary file"); g_unlink(filename->str); g_string_free(filename, TRUE); dbus_message_unref (message); return FALSE; } sleep(1); i++; reply = dbus_connection_send_with_reply_and_block (bus, message, -1, NULL); } dbus_message_unref (message); /* Depends on GIMP DBus signal: 'Opened' */ if (rs_has_gimp(2,6,2)) { /* Connect to GIMP and listen for "Opened" signal */ dbus_bus_add_match (bus, "type='signal',interface='org.gimp.GIMP.UI'", NULL); dbus_connection_add_filter(bus, dbus_gimp_opened, filename->str , NULL); g_string_free(filename, FALSE); } else { /* Old sad way - GIMP doesn't let us know that it has opened the photo */ g_warning("You have an old version of GIMP and we suggest that you upgrade to at least 2.6.2"); g_warning("Rawstudio will stop responding for 10 seconds while it waits for GIMP to open the file"); sleep(10); g_unlink(filename->str); g_string_free(filename, TRUE); } return TRUE; #endif }