static GwyContainer* gwyfile_load(const gchar *filename, G_GNUC_UNUSED GwyRunType mode, GError **error) { GObject *object; GError *err = NULL; guchar *buffer = NULL; gsize size = 0; gsize pos = 0; if (!gwy_file_get_contents(filename, &buffer, &size, &err)) { err_GET_FILE_CONTENTS(error, &err); return NULL; } if (size < MAGIC_SIZE || (memcmp(buffer, MAGIC, MAGIC_SIZE) && memcmp(buffer, MAGIC2, MAGIC_SIZE))) { err_FILE_TYPE(error, "Gwyddion"); gwy_file_abandon_contents(buffer, size, &err); return NULL; } if (!memcmp(buffer, MAGIC, MAGIC_SIZE)) { object = gwy_container_deserialize_old(buffer + MAGIC_SIZE, size - MAGIC_SIZE, &pos); gwyfile_remove_old_data(object); } else object = gwy_serializable_deserialize(buffer + MAGIC_SIZE, size - MAGIC_SIZE, &pos); gwy_file_abandon_contents(buffer, size, &err); if (!object) { g_set_error(error, GWY_MODULE_FILE_ERROR, GWY_MODULE_FILE_ERROR_DATA, _("Data deserialization failed.")); return NULL; } if (!GWY_IS_CONTAINER(object)) { g_set_error(error, GWY_MODULE_FILE_ERROR, GWY_MODULE_FILE_ERROR_DATA, _("Data deserialization succeeded, but resulted in " "an unexpected object %s."), g_type_name(G_TYPE_FROM_INSTANCE(object))); g_object_unref(object); return NULL; } gwyfile_pack_metadata(GWY_CONTAINER(object)); return GWY_CONTAINER(object); }
static void add_metadata(gpointer hkey, gpointer hvalue, gpointer user_data) { gchar *key = (gchar*)hkey; NanoscopeValue *val = (NanoscopeValue*)hvalue; gchar *v, *w; if (gwy_strequal(key, "#self") || !val->hard_value_str || !val->hard_value_str[0]) return; if (key[0] == '@') key++; v = g_strdup(val->hard_value_str); if (strchr(v, '\272')) { w = gwy_strreplace(v, "\272", "deg", -1); g_free(v); v = w; } if (strchr(v, '~')) { w = gwy_strreplace(v, "~", "µ", -1); g_free(v); v = w; } gwy_container_set_string_by_name(GWY_CONTAINER(user_data), key, v); }
static GwyContainer* rhk_sm3_load(const gchar *filename) { GPtrArray *rhkfile; RHKPage *rhkpage; GwyContainer *container = NULL; guchar *buffer = NULL; gsize size = 0; GError *err = NULL; GwyDataField *dfield = NULL; const guchar *p; guint i, count; if (!gwy_file_get_contents(filename, &buffer, &size, &err)) { g_warning("Cannot read file %s", filename); g_clear_error(&err); return NULL; } if (size < HEADER_SIZE) { g_warning("File %s is not a RHK SM3 file", filename); gwy_file_abandon_contents(buffer, size, NULL); return NULL; } rhkfile = g_ptr_array_new(); p = buffer; count = 0; gwy_debug("position %04x", p - buffer); while ((rhkpage = rhk_sm3_read_page(&p, &size))) { gwy_debug("Page #%u read OK", count); count++; rhkpage->pageno = count; gwy_debug("position %04x", p - buffer); if (rhkpage->type != RHK_TYPE_IMAGE) { gwy_debug("Page is not IMAGE, skipping"); rhk_sm3_page_free(rhkpage); continue; } g_ptr_array_add(rhkfile, rhkpage); } i = select_which_data(rhkfile); if (i != (guint)-1) { rhkpage = g_ptr_array_index(rhkfile, i); container = GWY_CONTAINER(gwy_container_new()); dfield = rhk_sm3_page_to_data_field(rhkpage); gwy_container_set_object_by_name(container, "/0/data", (GObject*)dfield); g_object_unref(dfield); rhk_sm3_store_metadata(rhkpage, container); } gwy_file_abandon_contents(buffer, size, NULL); for (i = 0; i < rhkfile->len; i++) rhk_sm3_page_free(g_ptr_array_index(rhkfile, i)); g_ptr_array_free(rhkfile, TRUE); return container; }
static void add_meta(gpointer hkey, gpointer hvalue, gpointer user_data) { gchar *value = g_convert((const gchar*)hvalue, -1, "UTF-8", "ISO-8859-1", NULL, NULL, NULL); if (value) gwy_container_set_string_by_name(GWY_CONTAINER(user_data), (gchar*)hkey, value); }
static GwyContainer* gwyfile_load(const gchar *filename) { GObject *object, *dfield; GError *err = NULL; guchar *buffer = NULL; gsize size = 0; gsize pos = 0; if (!gwy_file_get_contents(filename, &buffer, &size, &err)) { g_warning("Cannot read file %s", filename); g_clear_error(&err); return NULL; } if (size < MAGIC_SIZE || (memcmp(buffer, MAGIC, MAGIC_SIZE) && memcmp(buffer, MAGIC2, MAGIC_SIZE))) { g_warning("File %s doesn't seem to be a .gwy file", filename); if (!gwy_file_abandon_contents(buffer, size, &err)) { g_critical("%s", err->message); g_clear_error(&err); } return NULL; } if (!memcmp(buffer, MAGIC, MAGIC_SIZE)) object = gwy_serializable_deserialize(buffer + MAGIC_SIZE, size - MAGIC_SIZE, &pos); else object = gwy_container_deserialize2(buffer + MAGIC_SIZE, size - MAGIC_SIZE, &pos); if (!gwy_file_abandon_contents(buffer, size, &err)) { g_critical("%s", err->message); g_clear_error(&err); } if (!object) { g_warning("File %s deserialization failed", filename); return NULL; } if (!GWY_IS_CONTAINER(object)) { g_warning("File %s contains some strange object", filename); g_object_unref(object); return NULL; } dfield = gwy_container_get_object_by_name(GWY_CONTAINER(object), "/0/data"); if (!dfield || !GWY_IS_DATA_FIELD(dfield)) { g_warning("File %s contains no data field", filename); g_object_unref(object); return NULL; } return (GwyContainer*)object; }
static void add_meta(gpointer hkey, gpointer hvalue, gpointer user_data) { if (gwy_stramong((gchar*)hkey, "XRes", "YRes", "XReal", "YReal", "XOffset", "YOffset", "XYUnits", "ZUnits", "Title", NULL)) return; gwy_container_set_string_by_name(GWY_CONTAINER(user_data), (gchar*)hkey, g_strdup((gchar*)hvalue)); }
static void apply(GwyUnitoolState *state) { static const gchar *field_names[] = { "/0/data", "/0/mask", "/0/show" }; GtkWidget *data_window; GwyDataView *data_view; GwyContainer *data; GwyDataField *dfield; gint ximin, yimin, ximax, yimax; gdouble sel[4]; gsize i; if (!gwy_vector_layer_get_selection(state->layer, sel)) return; data_view = GWY_DATA_VIEW(GWY_DATA_VIEW_LAYER(state->layer)->parent); data = gwy_data_view_get_data(data_view); data = GWY_CONTAINER(gwy_serializable_duplicate(G_OBJECT(data))); gwy_app_clean_up_data(data); for (i = 0; i < G_N_ELEMENTS(field_names); i++) { if (!gwy_container_gis_object_by_name(data, field_names[i], (GObject**)&dfield)) continue; ximin = gwy_data_field_rtoj(dfield, sel[0]); yimin = gwy_data_field_rtoi(dfield, sel[1]); ximax = gwy_data_field_rtoj(dfield, sel[2]) + 1; yimax = gwy_data_field_rtoi(dfield, sel[3]) + 1; gwy_data_field_set_xreal(dfield, (ximax - ximin) *gwy_data_field_get_xreal(dfield) /gwy_data_field_get_xres(dfield)); gwy_data_field_set_yreal(dfield, (yimax - yimin) *gwy_data_field_get_yreal(dfield) /gwy_data_field_get_yres(dfield)); gwy_data_field_resize(dfield, ximin, yimin, ximax, yimax); } data_window = gwy_app_data_window_create(data); gwy_app_data_window_set_untitled(GWY_DATA_WINDOW(data_window), NULL); gwy_vector_layer_unselect(state->layer); gwy_data_view_update(data_view); gwy_debug("%d %d", gwy_data_field_get_xres(dfield), gwy_data_field_get_yres(dfield)); }
static void gwyfile_remove_old_data(GObject *object) { GwyContainer *data; GwySelection *rect, *point, *line; if (!object || !GWY_IS_CONTAINER(object)) return; data = GWY_CONTAINER(object); /* Selections */ rect = gwyfile_gather_old_rect_selection(data); point = gwyfile_gather_old_point_selection(data); line = gwyfile_gather_old_line_selection(data); gwy_container_remove_by_prefix(data, "/0/select"); if (rect) { gwy_container_set_object_by_name(data, "/0/select/rectangle", rect); g_object_unref(rect); } if (point) { gwy_container_set_object_by_name(data, "/0/select/point", point); g_object_unref(point); } if (line) { gwy_container_set_object_by_name(data, "/0/select/line", line); g_object_unref(line); } /* 3D */ gwy_container_remove_by_prefix(data, "/0/3d/labels"); gwy_container_remove_by_name(data, "/0/3d/rot_x"); gwy_container_remove_by_name(data, "/0/3d/rot_y"); gwy_container_remove_by_name(data, "/0/3d/view_scale"); gwy_container_remove_by_name(data, "/0/3d/deformation_z"); gwy_container_remove_by_name(data, "/0/3d/light_z"); gwy_container_remove_by_name(data, "/0/3d/light_y"); }
static GwyContainer* burleigh_load(const gchar *filename) { GwySIUnit *unit; GwyContainer *container = NULL; guchar *buffer = NULL; const guchar *p; gsize size = 0; GError *err = NULL; IMGFile imgfile; GwyDataField *dfield; gdouble *data; const gint16 *d; gdouble scale; guint i; if (!gwy_file_get_contents(filename, &buffer, &size, &err)) { g_warning("Cannot get file contents"); g_clear_error(&err); return NULL; } if (size < HEADER_SIZE_MIN + 2) { g_warning("File is too short"); gwy_file_abandon_contents(buffer, size, NULL); return NULL; } memset(&imgfile, 0, sizeof(imgfile)); p = buffer; imgfile.version = get_FLOAT(&p); imgfile.version_int = ROUND(10*imgfile.version); if (imgfile.version_int == 21) { d = burleigh_load_v21(&imgfile, buffer, size); if (!d) { gwy_file_abandon_contents(buffer, size, NULL); return NULL; } } else { g_warning("File format version %.f is not supported", imgfile.version); gwy_file_abandon_contents(buffer, size, NULL); return NULL; } dfield = GWY_DATA_FIELD(gwy_data_field_new(imgfile.xres, imgfile.yres, Angstrom*imgfile.xrange, Angstrom*imgfile.yrange, FALSE)); data = gwy_data_field_get_data(dfield); scale = Angstrom * imgfile.z_gain * imgfile.zrange; for (i = 0; i < imgfile.xres*imgfile.yres; i++) data[i] = scale * GINT16_FROM_LE(d[i]); gwy_file_abandon_contents(buffer, size, NULL); unit = GWY_SI_UNIT(gwy_si_unit_new("m")); gwy_data_field_set_si_unit_xy(dfield, unit); g_object_unref(unit); container = GWY_CONTAINER(gwy_container_new()); switch (imgfile.data_type) { case BURLEIGH_CURRENT: unit = GWY_SI_UNIT(gwy_si_unit_new("A")); gwy_container_set_string_by_name(container, "/filename/title", g_strdup("Current")); break; case BURLEIGH_TOPOGRAPHY: unit = GWY_SI_UNIT(gwy_si_unit_new("m")); gwy_container_set_string_by_name(container, "/filename/title", g_strdup("Topography")); break; default: unit = GWY_SI_UNIT(gwy_si_unit_new("m")); break; } gwy_data_field_set_si_unit_z(dfield, unit); g_object_unref(unit); gwy_container_set_object_by_name(container, "/0/data", (GObject*)dfield); g_object_unref(dfield); return container; }
static GwyContainer* surffile_load(const gchar *filename) { SurfFile surffile; GObject *object = NULL; guchar *buffer = NULL; const guchar *p; gsize size = 0; gsize estsize; GError *err = NULL; gchar signature[12]; gdouble max, min; if (!gwy_file_get_contents(filename, &buffer, &size, &err)) { g_warning("Cannot read file %s", filename); g_clear_error(&err); return NULL; } p = buffer; get_CHARARRAY(signature, &p); if (strncmp(signature, "DIGITAL SURF", 12) != 0) { g_warning("File %s is not a Surf file", filename); gwy_file_abandon_contents(buffer, size, NULL); return NULL; } if (size < 500) { g_warning("File %s is too short to be Surf file", filename); gwy_file_abandon_contents(buffer, size, NULL); return NULL; } surffile.format = get_WORD_LE(&p); surffile.nobjects = get_WORD_LE(&p); surffile.version = get_WORD_LE(&p); surffile.type = get_WORD_LE(&p); get_CHARS0(surffile.object_name, &p, 30); get_CHARS0(surffile.operator_name, &p, 30); surffile.material_code = get_WORD_LE(&p); surffile.acquisition = get_WORD_LE(&p); surffile.range = get_WORD_LE(&p); surffile.special_points = get_WORD_LE(&p); surffile.absolute = get_WORD_LE(&p); /*reserved*/ p += 8; surffile.pointsize = get_WORD_LE(&p); surffile.zmin = get_DWORD(&p); surffile.zmax = get_DWORD(&p); surffile.xres = get_DWORD(&p); surffile.yres = get_DWORD(&p); surffile.nofpoints = get_DWORD(&p); //surffile.xres = 200; surffile.yres = 200; surffile.dx = get_FLOAT_LE(&p); surffile.dy = get_FLOAT_LE(&p); surffile.dz = get_FLOAT_LE(&p); get_CHARS0(surffile.xaxis, &p, 16); get_CHARS0(surffile.yaxis, &p, 16); get_CHARS0(surffile.zaxis, &p, 16); get_CHARS0(surffile.dx_unit, &p, 16); get_CHARS0(surffile.dy_unit, &p, 16); get_CHARS0(surffile.dz_unit, &p, 16); get_CHARS0(surffile.xlength_unit, &p, 16); get_CHARS0(surffile.ylength_unit, &p, 16); get_CHARS0(surffile.zlength_unit, &p, 16); surffile.xunit_ratio = get_FLOAT_LE(&p); surffile.yunit_ratio = get_FLOAT_LE(&p); surffile.zunit_ratio = get_FLOAT_LE(&p); surffile.imprint = get_WORD_LE(&p); surffile.inversion = get_WORD_LE(&p); surffile.leveling = get_WORD_LE(&p); p += 12; surffile.seconds = get_WORD_LE(&p); surffile.minutes = get_WORD_LE(&p); surffile.hours = get_WORD_LE(&p); surffile.day = get_WORD_LE(&p); surffile.month = get_WORD_LE(&p); surffile.year = get_WORD_LE(&p); surffile.measurement_duration = get_WORD_LE(&p); surffile.comment_size = get_WORD_LE(&p); surffile.private_size = get_WORD_LE(&p); get_CHARARRAY(surffile.client_zone, &p); surffile.XOffset = get_FLOAT_LE(&p); surffile.YOffset = get_FLOAT_LE(&p); surffile.ZOffset = get_FLOAT_LE(&p); gwy_debug("fileformat: %d, n_of_objects: %d, version: %d, object_type: %d", surffile.format, surffile.nobjects, surffile.version, surffile.type); gwy_debug("object name: %s", surffile.object_name); gwy_debug("operator name: %s", surffile.operator_name); gwy_debug("material code: %d, acquisition type: %d", surffile.material_code, surffile.acquisition); gwy_debug("range type: %d, special points: %d, absolute: %d", surffile.range, surffile.special_points, (gint)surffile.absolute); gwy_debug("data point size: %d", surffile.pointsize); gwy_debug("zmin: %d, zmax: %d", surffile.zmin, surffile.zmax); gwy_debug("xres: %d, yres: %d (xres*yres = %d)", surffile.xres, surffile.yres, (surffile.xres*surffile.yres)); gwy_debug("total number of points: %d", surffile.nofpoints); gwy_debug("dx: %g, dy: %g, dz: %g", surffile.dx, surffile.dy, surffile.dz); gwy_debug("X axis name: %16s", surffile.xaxis); gwy_debug("Y axis name: %16s", surffile.yaxis); gwy_debug("Z axis name: %16s", surffile.zaxis); gwy_debug("dx unit: %16s", surffile.dx_unit); gwy_debug("dy unit: %16s", surffile.dy_unit); gwy_debug("dz unit: %16s", surffile.dz_unit); gwy_debug("X axis unit: %16s", surffile.xlength_unit); gwy_debug("Y axis unit: %16s", surffile.ylength_unit); gwy_debug("Z axis unit: %16s", surffile.zlength_unit); gwy_debug("xunit_ratio: %g, yunit_ratio: %g, zunit_ratio: %g", surffile.xunit_ratio, surffile.yunit_ratio, surffile.zunit_ratio); gwy_debug("imprint: %d, inversion: %d, leveling: %d", surffile.imprint, surffile.inversion, surffile.leveling); gwy_debug("Time: %d:%d:%d, Date: %d.%d.%d", surffile.hours, surffile.minutes, surffile.seconds, surffile.day, surffile.month, surffile.year); p = buffer + 512; estsize = 512 + surffile.pointsize*surffile.xres*surffile.yres/8; if (size < estsize) { g_warning("File %s is too short to contain Surf data %d %d", filename, (int)size, (int)estsize); gwy_file_abandon_contents(buffer, size, NULL); return NULL; } fill_data_fields(&surffile, p); gwy_file_abandon_contents(buffer, size, NULL); if (surffile.absolute == 0) { max = gwy_data_field_get_max(surffile.dfield); min = gwy_data_field_get_min(surffile.dfield); gwy_data_field_add(surffile.dfield, -min); gwy_data_field_multiply(surffile.dfield, (surffile.zmax - surffile.zmin)/(max-min)); } if (surffile.inversion == 1) gwy_data_field_invert(surffile.dfield, FALSE, FALSE, TRUE); if (surffile.inversion == 2) gwy_data_field_invert(surffile.dfield, FALSE, TRUE, TRUE); if (surffile.inversion == 3) gwy_data_field_invert(surffile.dfield, TRUE, FALSE, TRUE); if (surffile.dfield) { object = gwy_container_new(); gwy_container_set_object_by_name(GWY_CONTAINER(object), "/0/data", G_OBJECT(surffile.dfield)); store_metadata(&surffile, GWY_CONTAINER(object)); } return (GwyContainer*)object; return NULL; }
static void gwy_recent_file_update_thumbnail(GwyRecentFile *rf, GwyContainer *data, gint hint, GdkPixbuf *use_this_pixbuf) { GwyDataField *dfield; GdkPixbuf *pixbuf; GStatBuf st; gchar *fnm; GwySIUnit *siunit; GwySIValueFormat *vf; gdouble xreal, yreal; gchar str_mtime[22]; gchar str_size[22]; gchar str_width[22]; gchar str_height[22]; GError *err = NULL; GQuark quark; gint id; g_return_if_fail(GWY_CONTAINER(data)); if (use_this_pixbuf) { /* If we are given a pixbuf, hint must be the ultimate channel id. * We also ignore the thnumbnail state then. */ g_return_if_fail(GDK_IS_PIXBUF(use_this_pixbuf)); id = hint; pixbuf = g_object_ref(use_this_pixbuf); } else { pixbuf = NULL; id = gwy_recent_file_find_some_channel(data, hint); if (rf->file_state == FILE_STATE_UNKNOWN) gwy_app_recent_file_try_load_thumbnail(rf); } /* Find channel with the lowest id not smaller than hint */ if (id == G_MAXINT) { gwy_debug("There is no channel in the file, cannot make thumbnail."); return; } if (g_stat(rf->file_sys, &st) != 0) { g_warning("File <%s> was just loaded or saved, but it doesn't seem to " "exist any more: %s", rf->file_utf8, g_strerror(errno)); return; } if ((gulong)rf->file_mtime == (gulong)st.st_mtime) return; quark = gwy_app_get_data_key_for_id(id); dfield = GWY_DATA_FIELD(gwy_container_get_object(data, quark)); g_return_if_fail(GWY_IS_DATA_FIELD(dfield)); rf->file_mtime = st.st_mtime; rf->file_size = st.st_size; rf->image_width = gwy_data_field_get_xres(dfield); rf->image_height = gwy_data_field_get_yres(dfield); xreal = gwy_data_field_get_xreal(dfield); yreal = gwy_data_field_get_yreal(dfield); siunit = gwy_data_field_get_si_unit_xy(dfield); vf = gwy_si_unit_get_format(siunit, GWY_SI_UNIT_FORMAT_VFMARKUP, sqrt(xreal*yreal), NULL); g_free(rf->image_real_size); rf->image_real_size = g_strdup_printf("%.*f×%.*f%s%s", vf->precision, xreal/vf->magnitude, vf->precision, yreal/vf->magnitude, (vf->units && *vf->units) ? " " : "", vf->units); rf->file_state = FILE_STATE_OK; gwy_si_unit_value_format_free(vf); if (g_str_has_prefix(rf->file_sys, gwy_recent_file_thumbnail_dir())) { gchar c; c = rf->file_sys[strlen(gwy_recent_file_thumbnail_dir())]; if (!c || G_IS_DIR_SEPARATOR(c)) return; } if (!pixbuf) pixbuf = gwy_app_get_channel_thumbnail(data, id, TMS_NORMAL_THUMB_SIZE, TMS_NORMAL_THUMB_SIZE); g_snprintf(str_mtime, sizeof(str_mtime), "%lu", rf->file_mtime); g_snprintf(str_size, sizeof(str_size), "%lu", rf->file_size); g_snprintf(str_width, sizeof(str_width), "%d", rf->image_width); g_snprintf(str_height, sizeof(str_height), "%d", rf->image_height); /* invent an unique temporary name for atomic save * FIXME: rough, but works on Win32 */ fnm = g_strdup_printf("%s.%u", rf->thumb_sys, getpid()); if (!gdk_pixbuf_save(pixbuf, fnm, "png", &err, KEY_SOFTWARE, PACKAGE_NAME, KEY_THUMB_URI, rf->file_uri, KEY_THUMB_MTIME, str_mtime, KEY_THUMB_FILESIZE, str_size, KEY_THUMB_IMAGE_WIDTH, str_width, KEY_THUMB_IMAGE_HEIGHT, str_height, KEY_THUMB_GWY_REAL_SIZE, rf->image_real_size, NULL)) { g_clear_error(&err); rf->thumb_state = FILE_STATE_FAILED; } #ifndef G_OS_WIN32 chmod(fnm, 0600); #endif g_unlink(rf->thumb_sys); if (g_rename(fnm, rf->thumb_sys) != 0) { g_unlink(fnm); rf->thumb_state = FILE_STATE_FAILED; rf->thumb_mtime = 0; } else { rf->thumb_state = FILE_STATE_UNKNOWN; /* force reload */ rf->thumb_mtime = rf->file_mtime; } g_free(fnm); gwy_object_unref(rf->pixbuf); g_object_unref(pixbuf); }
static gboolean wshed_dialog(WshedArgs *args, GwyContainer *data) { GtkWidget *dialog, *table, *label, *spin, *hbox; WshedControls controls; enum { RESPONSE_RESET = 1, RESPONSE_PREVIEW = 2 }; gint response; gdouble zoomval; GtkObject *layer; GwyDataField *dfield; gint row; dialog = gtk_dialog_new_with_buttons(_("Mark Grains by Watershed"), NULL, 0, _("_Update Preview"), RESPONSE_PREVIEW, _("_Reset"), RESPONSE_RESET, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_OK, GTK_RESPONSE_OK, NULL); gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_OK); gtk_dialog_set_has_separator(GTK_DIALOG(dialog), FALSE); controls.dialog = dialog; hbox = gtk_hbox_new(FALSE, 2); gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), GTK_WIDGET(hbox), FALSE, FALSE, 4); controls.mydata = GWY_CONTAINER(gwy_serializable_duplicate(G_OBJECT(data))); controls.view = gwy_data_view_new(controls.mydata); layer = gwy_layer_basic_new(); gwy_data_view_set_base_layer(GWY_DATA_VIEW(controls.view), GWY_PIXMAP_LAYER(layer)); dfield = GWY_DATA_FIELD(gwy_container_get_object_by_name(controls.mydata, "/0/data")); if (gwy_data_field_get_xres(dfield) >= gwy_data_field_get_yres(dfield)) zoomval = PREVIEW_SIZE/(gdouble)gwy_data_field_get_xres(dfield); else zoomval = PREVIEW_SIZE/(gdouble)gwy_data_field_get_yres(dfield); gwy_data_view_set_zoom(GWY_DATA_VIEW(controls.view), zoomval); gtk_box_pack_start(GTK_BOX(hbox), controls.view, FALSE, FALSE, 4); table = gtk_table_new(9, 4, FALSE); gtk_box_pack_start(GTK_BOX(hbox), table, TRUE, TRUE, 4); row = 0; label = gtk_label_new(NULL); gtk_label_set_markup(GTK_LABEL(label), _("<b>Grain Location</b>")); gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5); gtk_table_attach(GTK_TABLE(table), label, 0, 2, row, row+1, GTK_EXPAND | GTK_FILL, 0, 2, 2); row++; controls.locate_steps = gtk_adjustment_new(args->locate_steps, 1.0, 100.0, 1, 5, 0); gwy_table_attach_hscale(table, row, _("_Number of steps:"), "", controls.locate_steps, 0); g_signal_connect(controls.locate_steps, "value_changed", G_CALLBACK(wshed_invalidate), &controls); row++; controls.locate_dropsize = gtk_adjustment_new(args->locate_dropsize, 0.01, 100.0, 0.1, 5, 0); spin = gwy_table_attach_hscale(table, row, _("_Drop size:"), "%", controls.locate_dropsize, 0); gtk_spin_button_set_digits(GTK_SPIN_BUTTON(spin), 2); g_signal_connect(controls.locate_dropsize, "value_changed", G_CALLBACK(wshed_invalidate), &controls); row++; controls.locate_thresh = gtk_adjustment_new(args->locate_thresh, 0.0, 100.0, 1, 5, 0); gwy_table_attach_hscale(table, row, _("_Threshold:"), "px<sup>2</sup>", controls.locate_thresh, 0); g_signal_connect(controls.locate_thresh, "value_changed", G_CALLBACK(wshed_invalidate), &controls); gtk_table_set_row_spacing(GTK_TABLE(table), row, 8); row++; label = gtk_label_new(NULL); gtk_label_set_markup(GTK_LABEL(label), _("<b>Segmentation</b>")); gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5); gtk_table_attach(GTK_TABLE(table), label, 0, 2, row, row+1, GTK_EXPAND | GTK_FILL, 0, 2, 2); row++; controls.wshed_steps = gtk_adjustment_new(args->wshed_steps, 1.0, 1000.0, 1, 5, 0); gwy_table_attach_hscale(table, row, _("Num_ber of steps:"), "", controls.wshed_steps, 0); g_signal_connect(controls.wshed_steps, "value_changed", G_CALLBACK(wshed_invalidate), &controls); row++; controls.wshed_dropsize = gtk_adjustment_new(args->wshed_dropsize, 0.01, 100.0, 0.1, 5, 0); spin = gwy_table_attach_hscale(table, row, _("Dr_op size:"), "%", controls.wshed_dropsize, 0); gtk_spin_button_set_digits(GTK_SPIN_BUTTON(spin), 2); g_signal_connect(controls.wshed_dropsize, "value_changed", G_CALLBACK(wshed_invalidate), &controls); gtk_table_set_row_spacing(GTK_TABLE(table), row, 8); row++; label = gtk_label_new(NULL); gtk_label_set_markup(GTK_LABEL(label), _("<b>Options</b>")); gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5); gtk_table_attach(GTK_TABLE(table), label, 0, 2, row, row+1, GTK_EXPAND | GTK_FILL, 0, 2, 2); row++; controls.inverted = gtk_check_button_new_with_mnemonic(_("_Invert height")); gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(controls.inverted), args->inverted); gtk_table_attach(GTK_TABLE(table), controls.inverted, 0, 4, row, row+1, GTK_EXPAND | GTK_FILL, 0, 2, 2); g_signal_connect(controls.inverted, "toggled", G_CALLBACK(wshed_invalidate), &controls); row++; controls.color_button = gwy_color_button_new(); gwy_color_button_set_use_alpha(GWY_COLOR_BUTTON(controls.color_button), TRUE); load_mask_color(controls.color_button, gwy_data_view_get_data(GWY_DATA_VIEW(controls.view))); gwy_table_attach_hscale(table, row++, _("_Mask color:"), NULL, GTK_OBJECT(controls.color_button), GWY_HSCALE_WIDGET_NO_EXPAND); g_signal_connect(controls.color_button, "clicked", G_CALLBACK(mask_color_change_cb), &controls); controls.computed = FALSE; gtk_widget_show_all(dialog); do { response = gtk_dialog_run(GTK_DIALOG(dialog)); switch (response) { case GTK_RESPONSE_CANCEL: case GTK_RESPONSE_DELETE_EVENT: wshed_dialog_update_values(&controls, args); gtk_widget_destroy(dialog); case GTK_RESPONSE_NONE: g_object_unref(controls.mydata); return FALSE; break; case GTK_RESPONSE_OK: break; case RESPONSE_RESET: *args = wshed_defaults; wshed_dialog_update_controls(&controls, args); break; case RESPONSE_PREVIEW: wshed_dialog_update_values(&controls, args); preview(&controls, args); break; default: g_assert_not_reached(); break; } } while (response != GTK_RESPONSE_OK); save_mask_color(controls.color_button, data); wshed_dialog_update_values(&controls, args); gtk_widget_destroy(dialog); wshed_ok(&controls, args, data); g_object_unref(controls.mydata); return controls.computed; }
static guint select_which_data(GPtrArray *rhkfile) { RHKControls controls; RHKPage *rhkpage; GtkWidget *dialog, *label, *vbox, *hbox, *align; GwyDataField *dfield; GwyEnum *choices; GwyPixmapLayer *layer; GSList *radio, *rl; guint i, b = (guint)-1; const gchar *s; if (!rhkfile->len) return b; if (rhkfile->len == 1) return 0; controls.file = rhkfile; choices = g_new(GwyEnum, rhkfile->len + 1); for (i = 0; i < rhkfile->len; i++) { rhkpage = g_ptr_array_index(rhkfile, i); choices[i].value = i; s = rhkpage->strings[RHK_STRING_LABEL]; if (s && *s) choices[i].name = g_strdup_printf(_("Page %u (%s)"), rhkpage->pageno, s); else choices[i].name = g_strdup_printf(_("Page %u"), rhkpage->pageno); } rhkpage = g_ptr_array_index(rhkfile, 0); dialog = gtk_dialog_new_with_buttons(_("Select Data"), NULL, 0, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_OK, GTK_RESPONSE_OK, NULL); gtk_dialog_set_has_separator(GTK_DIALOG(dialog), FALSE); gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_OK); hbox = gtk_hbox_new(FALSE, 20); gtk_container_set_border_width(GTK_CONTAINER(hbox), 6); gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox, TRUE, TRUE, 0); align = gtk_alignment_new(0.0, 0.0, 0.0, 0.0); gtk_box_pack_start(GTK_BOX(hbox), align, TRUE, TRUE, 0); vbox = gtk_vbox_new(TRUE, 0); gtk_container_add(GTK_CONTAINER(align), vbox); label = gtk_label_new(_("Data to load:")); gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5); gtk_box_pack_start(GTK_BOX(vbox), label, TRUE, TRUE, 0); radio = gwy_radio_buttons_create(choices, rhkfile->len, "data", G_CALLBACK(selection_changed), &controls, 0); for (i = 0, rl = radio; rl; i++, rl = g_slist_next(rl)) gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(rl->data), TRUE, TRUE, 0); /* preview */ align = gtk_alignment_new(1.0, 0.0, 0.0, 0.0); gtk_box_pack_start(GTK_BOX(hbox), align, TRUE, TRUE, 0); controls.data = GWY_CONTAINER(gwy_container_new()); dfield = rhk_sm3_page_to_data_field(rhkpage); gwy_container_set_object_by_name(controls.data, "/0/data", (GObject*)dfield); g_object_unref(dfield); controls.data_view = gwy_data_view_new(controls.data); g_object_unref(controls.data); gwy_data_view_set_zoom(GWY_DATA_VIEW(controls.data_view), 120.0/MAX(rhkpage->x_size, rhkpage->y_size)); layer = GWY_PIXMAP_LAYER(gwy_layer_basic_new()); gwy_data_view_set_base_layer(GWY_DATA_VIEW(controls.data_view), layer); gtk_container_add(GTK_CONTAINER(align), controls.data_view); gtk_widget_show_all(dialog); gtk_window_present(GTK_WINDOW(dialog)); switch (gtk_dialog_run(GTK_DIALOG(dialog))) { case GTK_RESPONSE_CANCEL: case GTK_RESPONSE_DELETE_EVENT: gtk_widget_destroy(dialog); case GTK_RESPONSE_NONE: break; case GTK_RESPONSE_OK: b = GPOINTER_TO_UINT(gwy_radio_buttons_get_current(radio, "data")); gtk_widget_destroy(dialog); break; default: g_assert_not_reached(); break; } for (i = 0; i < rhkfile->len; i++) g_free((gpointer)choices[i].name); g_free(choices); return b; }