| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
#include "config.h" |
| 31 |
#include <glib/gi18n-lib.h> |
| 32 |
#include <string.h> |
| 33 |
#ifdef HAVE_UNISTD_H |
| 34 |
#include <unistd.h> |
| 35 |
#endif |
| 36 |
#include <glib/gstdio.h> |
| 37 |
#include <gtk/gtk.h> |
| 38 |
#include <gwy.h> |
| 39 |
|
| 40 |
#include "err.h" |
| 41 |
|
| 42 |
#define EXTENSION ".txt" |
| 43 |
|
| 44 |
enum { |
| 45 |
PARAM_ADD_COMMENT, |
| 46 |
PARAM_ENGLISH_COMMENT, |
| 47 |
PARAM_DECIMAL_DOT, |
| 48 |
PARAM_CONCAT_ALL, |
| 49 |
PARAM_PRECISION, |
| 50 |
}; |
| 51 |
|
| 52 |
typedef struct { |
| 53 |
GwyParams *params; |
| 54 |
} ModuleArgs; |
| 55 |
|
| 56 |
typedef struct { |
| 57 |
ModuleArgs *args; |
| 58 |
GtkWidget *dialog; |
| 59 |
GwyParamTable *table; |
| 60 |
} ModuleGUI; |
| 61 |
|
| 62 |
static gboolean module_register (void); |
| 63 |
static GwyParamDef* define_module_params(void); |
| 64 |
static gint detect_file (const GwyFileDetectInfo *fileinfo, |
| 65 |
gboolean only_name); |
| 66 |
static GwyFile* load_file (const gchar *filename, |
| 67 |
GwyRunModeFlags mode, |
| 68 |
GError **error); |
| 69 |
static gboolean export_file (GwyFile *data, |
| 70 |
const gchar *filename, |
| 71 |
GwyRunModeFlags mode, |
| 72 |
GError **error); |
| 73 |
static GwyDialogOutcome run_gui (ModuleArgs *args); |
| 74 |
static void param_changed (ModuleGUI *gui, |
| 75 |
gint id); |
| 76 |
static gboolean export_one_channel (GwyFile *data, |
| 77 |
gint id, |
| 78 |
ModuleArgs *args, |
| 79 |
FILE *fh); |
| 80 |
|
| 81 |
static GwyModuleInfo module_info = { |
| 82 |
GWY_MODULE_ABI_VERSION, |
| 83 |
&module_register, |
| 84 |
N_("Exports data as simple ASCII matrix and reads back this format."), |
| 85 |
"Yeti <[email protected]>", |
| 86 |
"4.0", |
| 87 |
"David Nečas (Yeti)", |
| 88 |
"2004", |
| 89 |
}; |
| 90 |
|
| 91 |
GWY_MODULE_QUERY2(module_info, asciiexport) |
| 92 |
|
| 93 |
static gboolean |
| 94 |
module_register(void) |
| 95 |
{ |
| 96 |
gwy_file_func_register("asciiexport", |
| 97 |
N_("ASCII data matrix (.txt)"), |
| 98 |
detect_file, load_file, NULL, &export_file); |
| 99 |
|
| 100 |
return TRUE; |
| 101 |
} |
| 102 |
|
| 103 |
static GwyParamDef* |
| 104 |
define_module_params(void) |
| 105 |
{ |
| 106 |
static GwyParamDef *paramdef = NULL; |
| 107 |
|
| 108 |
if (paramdef) |
| 109 |
return paramdef; |
| 110 |
|
| 111 |
paramdef = gwy_param_def_new(); |
| 112 |
gwy_param_def_set_function_name(paramdef, gwy_file_func_current()); |
| 113 |
gwy_param_def_add_boolean(paramdef, PARAM_ADD_COMMENT, "add-comment", _("Add _informational comment header"), |
| 114 |
FALSE); |
| 115 |
gwy_param_def_add_boolean(paramdef, PARAM_ENGLISH_COMMENT, "english-comment", _("Keep comment in English"), |
| 116 |
FALSE); |
| 117 |
gwy_param_def_add_boolean(paramdef, PARAM_DECIMAL_DOT, "decimal-dot", _("Use _dot as decimal separator"), TRUE); |
| 118 |
gwy_param_def_add_boolean(paramdef, PARAM_CONCAT_ALL, "concat-all", _("Conca_tenate exports of all images"), FALSE); |
| 119 |
gwy_param_def_add_int(paramdef, PARAM_PRECISION, "precision", _("_Precision"), 0, 16, 5); |
| 120 |
|
| 121 |
return paramdef; |
| 122 |
} |
| 123 |
|
| 124 |
static inline const gchar* |
| 125 |
to_end_of_line(const gchar *s) |
| 126 |
{ |
| 127 |
while (*s && *s != '\n' && *s != '\r') |
| 128 |
s++; |
| 129 |
return s; |
| 130 |
} |
| 131 |
|
| 132 |
static const gchar* |
| 133 |
check_header_field(const gchar **s, const gchar **labels, guint nlabels) |
| 134 |
{ |
| 135 |
guint i, n; |
| 136 |
|
| 137 |
if (strncmp(*s, "# ", 2)) |
| 138 |
return NULL; |
| 139 |
|
| 140 |
*s += 2; |
| 141 |
for (i = 0; i < nlabels; i++) { |
| 142 |
n = strlen(labels[i]); |
| 143 |
if (!strncmp(*s, labels[i], n)) { |
| 144 |
*s += n; |
| 145 |
if (**s == ' ') { |
| 146 |
const gchar *retval = *s+1; |
| 147 |
*s = to_end_of_line(*s + 1); |
| 148 |
if (!**s) |
| 149 |
return NULL; |
| 150 |
while (**s == '\n' || **s == '\r') |
| 151 |
(*s)++; |
| 152 |
return retval; |
| 153 |
} |
| 154 |
return NULL; |
| 155 |
} |
| 156 |
} |
| 157 |
return NULL; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
static gboolean |
| 163 |
parse_header(const gchar **s, |
| 164 |
const gchar **channel, const gchar **width, const gchar **height, const gchar **zunit) |
| 165 |
{ |
| 166 |
static const gchar *key_channel[] = { |
| 167 |
"Channel:", "Kanál:", "Kanal:", "Canal:", "Canal :", "Canale:", "チャネル:", "채널:", "Canal:", "Канал:", |
| 168 |
}; |
| 169 |
static const gchar *key_width[] = { |
| 170 |
"Width:", "Šířka:", "Breite:", "Anchura:", "Largeur :", "Larghezza:", "幅:", "폭:", "Largura:", "Ширина:", |
| 171 |
}; |
| 172 |
static const gchar *key_height[] = { |
| 173 |
"Height:", "Výška:", "Höhe:", "Altura:", "Hauteur :", "Altezza:", "高さ:", "높이:", "Высота:", |
| 174 |
}; |
| 175 |
static const gchar *key_zunit[] = { |
| 176 |
"Value units:", "Jednotky hodnot:", "Einheiten:", "Unités :", "unità valore:", "値の単位:", "값 단위:", |
| 177 |
"Unidades de valor:", "Единицы измерения:", |
| 178 |
}; |
| 179 |
|
| 180 |
gwy_debug("trying to parse header"); |
| 181 |
if (!(*channel = check_header_field(s, key_channel, G_N_ELEMENTS(key_channel)))) |
| 182 |
return FALSE; |
| 183 |
gwy_debug("recognised Channel"); |
| 184 |
if (!(*width = check_header_field(s, key_width, G_N_ELEMENTS(key_width)))) |
| 185 |
return FALSE; |
| 186 |
gwy_debug("recognised Width"); |
| 187 |
if (!(*height = check_header_field(s, key_height, G_N_ELEMENTS(key_height)))) |
| 188 |
return FALSE; |
| 189 |
gwy_debug("recognised Height"); |
| 190 |
if (!(*zunit = check_header_field(s, key_zunit, G_N_ELEMENTS(key_zunit)))) |
| 191 |
return FALSE; |
| 192 |
gwy_debug("recognised Value units"); |
| 193 |
return TRUE; |
| 194 |
} |
| 195 |
|
| 196 |
static gint |
| 197 |
detect_file(const GwyFileDetectInfo *fileinfo, gboolean only_name) |
| 198 |
{ |
| 199 |
const gchar *channel, *width, *height, *zunit, *p; |
| 200 |
|
| 201 |
if (only_name) |
| 202 |
return g_str_has_suffix(fileinfo->name_lowercase, EXTENSION) ? 20 : 0; |
| 203 |
|
| 204 |
p = fileinfo->head; |
| 205 |
if (!parse_header(&p, &channel, &width, &height, &zunit)) |
| 206 |
return 0; |
| 207 |
|
| 208 |
return 50; |
| 209 |
} |
| 210 |
|
| 211 |
static GwyFile* |
| 212 |
load_file(const gchar *filename, |
| 213 |
G_GNUC_UNUSED GwyRunModeFlags mode, |
| 214 |
GError **error) |
| 215 |
{ |
| 216 |
GwyFile *file = NULL; |
| 217 |
gchar *buffer, *s, *end; |
| 218 |
gsize size; |
| 219 |
const gchar *channel, *width, *height, *zunit, *p; |
| 220 |
gboolean found_header = FALSE, moved; |
| 221 |
GwyField *field; |
| 222 |
GError *err = NULL; |
| 223 |
gint xres, yres, i, id, power10; |
| 224 |
gdouble r; |
| 225 |
|
| 226 |
if (!g_file_get_contents(filename, &buffer, &size, &err)) { |
| 227 |
err_GET_FILE_CONTENTS(error, &err); |
| 228 |
return NULL; |
| 229 |
} |
| 230 |
p = buffer; |
| 231 |
id = 0; |
| 232 |
while (parse_header(&p, &channel, &width, &height, &zunit)) { |
| 233 |
found_header = TRUE; |
| 234 |
i = 0; |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
xres = 0; |
| 239 |
while (p[i] && p[i] != '\n' && p[i] != '\r') { |
| 240 |
while (p[i] == ' ' || p[i] == '\t') |
| 241 |
i++; |
| 242 |
moved = FALSE; |
| 243 |
while (p[i] && p[i] != ' ' && p[i] != '\t' && p[i] != '\n' && p[i] != '\r') { |
| 244 |
moved = TRUE; |
| 245 |
i++; |
| 246 |
} |
| 247 |
if (moved) |
| 248 |
xres++; |
| 249 |
} |
| 250 |
|
| 251 |
yres = 0; |
| 252 |
while (p[i] && p[i] != '#') { |
| 253 |
if (p[i] == '\n' || p[i] == '\r') { |
| 254 |
while (p[i] == '\n' || p[i] == '\r' || p[i] == ' ' || p[i] == '\t') |
| 255 |
i++; |
| 256 |
yres++; |
| 257 |
} |
| 258 |
else |
| 259 |
i++; |
| 260 |
} |
| 261 |
|
| 262 |
if (err_DIMENSION(error, xres) || err_DIMENSION(error, yres)) { |
| 263 |
g_clear_object(&file); |
| 264 |
break; |
| 265 |
} |
| 266 |
|
| 267 |
field = gwy_field_new(xres, yres, 1.0, 1.0, FALSE); |
| 268 |
if (!gwy_parse_doubles(p, gwy_field_get_data(field), 0, &yres, &xres, NULL, error)) { |
| 269 |
g_clear_object(&field); |
| 270 |
g_clear_object(&file); |
| 271 |
break; |
| 272 |
} |
| 273 |
|
| 274 |
if (!file) |
| 275 |
file = gwy_file_new_in_construction(); |
| 276 |
|
| 277 |
|
| 278 |
s = g_strndup(zunit, to_end_of_line(zunit) - zunit); |
| 279 |
power10 = gwy_unit_set_from_string(gwy_field_get_unit_z(field), s); |
| 280 |
g_free(s); |
| 281 |
gwy_field_multiply(field, gwy_exp10(power10)); |
| 282 |
|
| 283 |
|
| 284 |
r = g_strtod(height, &end); |
| 285 |
sanitise_real_size(&r, "y size"); |
| 286 |
s = g_strndup(end, to_end_of_line(end) - end); |
| 287 |
power10 = gwy_unit_set_from_string(gwy_field_get_unit_xy(field), s); |
| 288 |
gwy_field_set_yreal(field, r*gwy_exp10(power10)); |
| 289 |
g_free(s); |
| 290 |
|
| 291 |
r = g_strtod(width, &end); |
| 292 |
sanitise_real_size(&r, "x size"); |
| 293 |
s = g_strndup(end, to_end_of_line(end) - end); |
| 294 |
power10 = gwy_unit_set_from_string(gwy_field_get_unit_xy(field), s); |
| 295 |
gwy_field_set_xreal(field, r*gwy_exp10(power10)); |
| 296 |
g_free(s); |
| 297 |
|
| 298 |
gwy_file_pass_image(file, id, field); |
| 299 |
|
| 300 |
|
| 301 |
s = g_strstrip(g_strndup(channel, to_end_of_line(channel) - channel)); |
| 302 |
gwy_file_pass_title(file, GWY_FILE_IMAGE, id, s); |
| 303 |
|
| 304 |
id++; |
| 305 |
p += i; |
| 306 |
} |
| 307 |
|
| 308 |
if (!found_header) { |
| 309 |
err_FILE_TYPE(error, "ASCII export"); |
| 310 |
} |
| 311 |
|
| 312 |
g_free(buffer); |
| 313 |
|
| 314 |
return file; |
| 315 |
} |
| 316 |
|
| 317 |
static gboolean |
| 318 |
export_file(GwyFile *data, |
| 319 |
const gchar *filename, |
| 320 |
GwyRunModeFlags mode, |
| 321 |
GError **error) |
| 322 |
{ |
| 323 |
ModuleArgs args; |
| 324 |
gint i, id, *ids; |
| 325 |
GwyDialogOutcome outcome; |
| 326 |
FILE *fh = NULL; |
| 327 |
gboolean ok = FALSE; |
| 328 |
|
| 329 |
gwy_clear1(args); |
| 330 |
gwy_data_browser_get_current(GWY_APP_FIELD_ID, &id, 0); |
| 331 |
if (id < 0) { |
| 332 |
err_NO_CHANNEL_EXPORT(error); |
| 333 |
return FALSE; |
| 334 |
} |
| 335 |
|
| 336 |
args.params = gwy_params_new_from_settings(define_module_params()); |
| 337 |
if (mode == GWY_RUN_INTERACTIVE) { |
| 338 |
outcome = run_gui(&args); |
| 339 |
gwy_params_save_to_settings(args.params); |
| 340 |
if (outcome == GWY_DIALOG_CANCEL) { |
| 341 |
err_CANCELLED(error); |
| 342 |
goto fail; |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
if (!(fh = gwy_fopen(filename, "w"))) { |
| 347 |
err_OPEN_WRITE(error); |
| 348 |
goto fail; |
| 349 |
} |
| 350 |
|
| 351 |
if (gwy_params_get_boolean(args.params, PARAM_CONCAT_ALL)) { |
| 352 |
ids = gwy_file_get_ids(data, GWY_FILE_IMAGE); |
| 353 |
for (i = 0; ids[i] >= 0; i++) { |
| 354 |
if (!export_one_channel(data, ids[i], &args, fh) || gwy_fprintf(fh, "\n") < 0) { |
| 355 |
err_WRITE(error); |
| 356 |
goto fail; |
| 357 |
} |
| 358 |
} |
| 359 |
} |
| 360 |
else { |
| 361 |
if (!export_one_channel(data, id, &args, fh)) { |
| 362 |
err_WRITE(error); |
| 363 |
goto fail; |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
ok = TRUE; |
| 368 |
|
| 369 |
fail: |
| 370 |
if (fh) |
| 371 |
fclose(fh); |
| 372 |
if (!ok) |
| 373 |
g_unlink(filename); |
| 374 |
|
| 375 |
g_object_unref(args.params); |
| 376 |
|
| 377 |
return ok; |
| 378 |
} |
| 379 |
|
| 380 |
static GwyDialogOutcome |
| 381 |
run_gui(ModuleArgs *args) |
| 382 |
{ |
| 383 |
gboolean needs_decimal_dot_option; |
| 384 |
GwyDialog *dialog; |
| 385 |
GwyParamTable *table; |
| 386 |
ModuleGUI gui; |
| 387 |
|
| 388 |
gui.args = args; |
| 389 |
needs_decimal_dot_option = !gwy_strequal(gwy_get_decimal_separator(), "."); |
| 390 |
|
| 391 |
gui.dialog = gwy_dialog_new(_("Export Text")); |
| 392 |
dialog = GWY_DIALOG(gui.dialog); |
| 393 |
gwy_dialog_add_stock_buttons(dialog, GWY_STOCK_CANCEL, GWY_STOCK_OK, 0); |
| 394 |
|
| 395 |
table = gui.table = gwy_param_table_new(args->params); |
| 396 |
gwy_param_table_append_header(table, -1, _("Options")); |
| 397 |
if (needs_decimal_dot_option) |
| 398 |
gwy_param_table_append_checkbox(table, PARAM_DECIMAL_DOT); |
| 399 |
gwy_param_table_append_checkbox(table, PARAM_ADD_COMMENT); |
| 400 |
gwy_param_table_append_checkbox(table, PARAM_ENGLISH_COMMENT); |
| 401 |
gwy_param_table_append_checkbox(table, PARAM_CONCAT_ALL); |
| 402 |
gwy_param_table_append_slider(table, PARAM_PRECISION); |
| 403 |
gwy_param_table_slider_set_mapping(table, PARAM_PRECISION, GWY_SCALE_MAPPING_LINEAR); |
| 404 |
gwy_dialog_add_content(dialog, gwy_param_table_widget(table), FALSE, FALSE, 0); |
| 405 |
|
| 406 |
g_signal_connect_swapped(table, "param-changed", G_CALLBACK(param_changed), &gui); |
| 407 |
|
| 408 |
return gwy_dialog_run(dialog); |
| 409 |
} |
| 410 |
|
| 411 |
static void |
| 412 |
param_changed(ModuleGUI *gui, gint id) |
| 413 |
{ |
| 414 |
GwyParams *params = gui->args->params; |
| 415 |
|
| 416 |
if (id < 0 || id == PARAM_ADD_COMMENT) { |
| 417 |
gwy_param_table_set_sensitive(gui->table, PARAM_ENGLISH_COMMENT, |
| 418 |
gwy_params_get_boolean(params, PARAM_ADD_COMMENT)); |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
static gboolean |
| 423 |
export_one_channel(GwyFile *data, gint id, ModuleArgs *args, FILE *fh) |
| 424 |
{ |
| 425 |
GwyField *field = gwy_file_get_image(data, id); |
| 426 |
gboolean decimal_dot = gwy_params_get_boolean(args->params, PARAM_DECIMAL_DOT); |
| 427 |
gint precision = gwy_params_get_int(args->params, PARAM_PRECISION); |
| 428 |
gboolean add_comment = gwy_params_get_boolean(args->params, PARAM_ADD_COMMENT); |
| 429 |
gboolean english_comment = gwy_params_get_boolean(args->params, PARAM_ENGLISH_COMMENT); |
| 430 |
gint xres, yres, i; |
| 431 |
const gdouble *d; |
| 432 |
GString *str; |
| 433 |
|
| 434 |
g_return_val_if_fail(GWY_IS_FIELD(field), FALSE); |
| 435 |
xres = gwy_field_get_xres(field); |
| 436 |
yres = gwy_field_get_yres(field); |
| 437 |
d = gwy_field_get_data_const(field); |
| 438 |
|
| 439 |
str = g_string_new(NULL); |
| 440 |
if (add_comment) { |
| 441 |
gdouble xreal = gwy_field_get_xreal(field), yreal = gwy_field_get_yreal(field); |
| 442 |
GwyValueFormat *vf = NULL; |
| 443 |
gdouble v; |
| 444 |
|
| 445 |
gchar *s = gwy_file_get_display_title(data, GWY_FILE_IMAGE, id); |
| 446 |
g_string_printf(str, "# %s %s\n", english_comment ? "Channel:" : _("Channel:"), s); |
| 447 |
g_free(s); |
| 448 |
|
| 449 |
vf = gwy_field_get_value_format_xy(field, GWY_UNIT_FORMAT_VFMARKUP, vf); |
| 450 |
|
| 451 |
g_string_append_printf(str, "# %s ", english_comment ? "Width:" : _("Width:")); |
| 452 |
v = xreal/vf->magnitude; |
| 453 |
gwy_append_doubles_to_gstring(str, &v, 1, precision, "", decimal_dot); |
| 454 |
g_string_append_printf(str, " %s\n", vf->units); |
| 455 |
|
| 456 |
g_string_append_printf(str, "# %s ", english_comment ? "Height:" : _("Height:")); |
| 457 |
v = yreal/vf->magnitude; |
| 458 |
gwy_append_doubles_to_gstring(str, &v, 1, precision, "", decimal_dot); |
| 459 |
g_string_append_printf(str, " %s\n", vf->units); |
| 460 |
|
| 461 |
GwyUnit *units = gwy_field_get_unit_z(field); |
| 462 |
s = gwy_unit_get_string(units, GWY_UNIT_FORMAT_VFMARKUP); |
| 463 |
g_string_append_printf(str, "# %s %s\n", english_comment ? "Value units:" : _("Value units:"), s); |
| 464 |
g_free(s); |
| 465 |
|
| 466 |
fputs(str->str, fh); |
| 467 |
|
| 468 |
gwy_value_format_free(vf); |
| 469 |
} |
| 470 |
|
| 471 |
for (i = 0; i < yres; i++) { |
| 472 |
g_string_truncate(str, 0); |
| 473 |
gwy_append_doubles_to_gstring(str, d + i*xres, xres, precision, "\t", decimal_dot); |
| 474 |
g_string_append_c(str, '\n'); |
| 475 |
if (fputs(str->str, fh) == EOF) { |
| 476 |
g_string_free(str, TRUE); |
| 477 |
return FALSE; |
| 478 |
} |
| 479 |
} |
| 480 |
g_string_free(str, TRUE); |
| 481 |
|
| 482 |
return TRUE; |
| 483 |
} |
| 484 |
|
| 485 |
|