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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
|
/*
* $Id: aistfile.c 24136 2021-09-15 12:21:10Z yeti-dn $
* Copyright (C) 2009-2021 David Necas (Yeti).
* E-mail: yeti@gwyddion.net.
*
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with this program; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/*
* Warning: Madness ahead.
*
* Files are saved and loaded using Qt serialization facilities but inconsistently so, data stored by the Qt
* serialization layer are in big-endian byte order (because people at TrollTech think it is more portable or
* something) while AIST does not go that far as to think about byte order so they dump stuff to QByteArrays in the
* native order which happens to be little-endian because they run it on x86. Bite me.
*/
/**
* [FILE-MAGIC-FREEDESKTOP]
* <mime-type type="application/x-aist-nt-spm">
* <comment>AIST-NT SPM data</comment>
* <glob pattern="*.aist"/>
* <glob pattern="*.AIST"/>
* </mime-type>
**/
/**
* [FILE-MAGIC-USERGUIDE]
* AIST-NT
* .aist
* Read
**/
#include "config.h"
#include <string.h>
#include <stdlib.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <libgwyddion/gwymacros.h>
#include <libgwyddion/gwyutils.h>
#include <libgwyddion/gwymath.h>
#include <libprocess/datafield.h>
#include <libgwymodule/gwymodule-file.h>
#include <app/data-browser.h>
#include <app/gwymoduleutils-file.h>
#include "err.h"
#define EXTENSION ".aist"
typedef struct {
guint id;
guint index;
gchar *name;
gchar *description;
} AistCommon;
typedef struct {
AistCommon common;
guint xres, yres;
gdouble left, right, top, bottom;
gchar *xunits, *yunits, *zunits;
} AistRaster;
typedef struct {
AistCommon common;
guint res;
gchar *xunits, *yunits;
} AistCurve;
typedef struct {
GwyContainer *container;
const gchar *filename;
gint channel_id;
gint graph_id;
} AistContext;
static gboolean module_register (void);
static gint aist_detect (const GwyFileDetectInfo *fileinfo,
gboolean only_name);
static GwyContainer* aist_load (const gchar *filename,
GwyRunType mode,
GError **error);
static gboolean read_qt_bool (const guchar **p,
gsize *size,
gboolean *value);
static gboolean read_qt_byte (const guchar **p,
gsize *size,
guint *value);
static gboolean read_qt_int (const guchar **p,
gsize *size,
gint *value);
static gboolean read_qt_double (const guchar **p,
gsize *size,
gdouble *value);
static gboolean read_qt_string (const guchar **p,
gsize *size,
gchar **value);
static gboolean read_qt_byte_array(const guchar **p,
gsize *size,
guint *len,
const guchar **value);
static GwyModuleInfo module_info = {
GWY_MODULE_ABI_VERSION,
&module_register,
N_("Imports AIST-NT data files."),
"Yeti <yeti@gwyddion.net>",
"0.5.3",
"David Nečas (Yeti)",
"2009",
};
GWY_MODULE_QUERY2(module_info, aistfile)
static gboolean
module_register(void)
{
gwy_file_func_register("aistfile",
N_("AIST-NT files (.aist)"),
(GwyFileDetectFunc)&aist_detect,
(GwyFileLoadFunc)&aist_load,
NULL,
NULL);
return TRUE;
}
static gint
aist_detect(const GwyFileDetectInfo *fileinfo,
gboolean only_name)
{
gint score = 0;
const guchar *buf = fileinfo->head;
gsize size = fileinfo->buffer_len;
guint is_data_node;
if (only_name)
return (g_str_has_suffix(fileinfo->name_lowercase, EXTENSION)) ? 10 : 0;
/* The first byte is a boolean, it must be 0 or 1. This is unfortunately the only quick means to filter-out
* non-AIST files. */
if (!read_qt_byte(&buf, &size, &is_data_node) || is_data_node > 1)
return 0;
/* A silly detection method. Try to start reading and check if the stuff looks like the nodes we expect. Reading
* the strings looks dangerous. Fortnately we know buffer_len is small so it won't try to allocate 2GB of memory
* for the UTF-8 representation. */
if (is_data_node) {
gchar *type = NULL;
guint len;
if (read_qt_string(&buf, &size, &type) && read_qt_int(&buf, &size, &len)) {
if (gwy_stramong(type, "raster", "curve", "settings", NULL))
score = 85;
}
g_free(type);
}
else {
gchar *p, *name = NULL;
guint nchildren, i, len, nascii;
gunichar c;
/* This is really weak. Return a low score unless the string at least
* looks reasonable stringy. */
if (read_qt_string(&buf, &size, &name)
&& read_qt_int(&buf, &size, &nchildren)
&& read_qt_byte(&buf, &size, &is_data_node)) {
if (is_data_node <= 1 && nchildren <= 0xff && strlen(name) <= 0xff) {
score = 40;
len = g_utf8_strlen(name, -1);
nascii = 0;
p = name;
for (i = 0; i < MIN(len, 20); i++) {
c = g_utf8_get_char(p);
nascii += (c >= 0x20 && c < 0x7f);
}
score += 30*nascii/(nascii + 1);
}
}
g_free(name);
}
return score;
}
static void
free_aist_common(AistCommon *common)
{
g_free(common->name);
g_free(common->description);
}
static gboolean
read_aist_common(const guchar **p, gsize *size,
AistCommon *common)
{
if (!read_qt_int(p, size, &common->id)
|| !read_qt_string(p, size, &common->name)
|| !read_qt_string(p, size, &common->description)
|| !read_qt_int(p, size, &common->index)) {
free_aist_common(common);
return FALSE;
}
return TRUE;
}
/* Lateral units are a weird thing but they contain the actual units in bracket somewhere. If there are no brackes,
* try to interpret the entire string as units -- I've been told this is the right thing. */
static GwySIUnit*
extract_units(const gchar *label,
gdouble *q)
{
GwySIUnit *unit;
gint power10;
const gchar *s;
gchar *t;
if ((s = strchr(label, '['))) {
t = g_strdup(s + 1);
if (strchr(t, ']'))
*strchr(t, ']') = '\0';
unit = gwy_si_unit_new_parse(t, &power10);
g_free(t);
}
else
unit = gwy_si_unit_new_parse(label, &power10);
*q = pow10(power10);
return unit;
}
static GwyDataField*
make_mask_field(GwyDataField *dfield,
const guchar *data)
{
GwyDataField *mfield;
guint xres, yres, i, j, rowstride;
const guchar *datarow;
gdouble *d;
xres = gwy_data_field_get_xres(dfield);
yres = gwy_data_field_get_yres(dfield);
rowstride = xres;
/* Do not create the mask if it is all zero. */
for (i = 0; i < rowstride*yres; i++) {
if (!data[i])
break;
}
if (i == rowstride*yres)
return NULL;
mfield = gwy_data_field_new_alike(dfield, FALSE);
d = gwy_data_field_get_data(mfield);
for (i = 0; i < yres; i++) {
datarow = data + (yres-1 - i)*rowstride;
for (j = 0; j < xres; j++) {
*(d++) = datarow[j];
}
}
return mfield;
}
static gboolean
read_aist_raster(const guchar **p, gsize *size, AistContext *context)
{
AistRaster raster;
GwyDataField *dfield, *mfield;
GwySIUnit *xyunit, *zunit;
gboolean ok = FALSE;
guint i, j, n, len;
const guchar *data;
gchar *s;
gdouble *d;
gdouble qxy, qz;
gwy_clear(&raster, 1);
gwy_debug("reading common");
if (!read_aist_common(p, size, &raster.common))
goto fail;
gwy_debug("reading raster");
if (!read_qt_int(p, size, &raster.xres)
|| !read_qt_int(p, size, &raster.yres)
|| !read_qt_double(p, size, &raster.left)
|| !read_qt_double(p, size, &raster.right)
|| !read_qt_double(p, size, &raster.top)
|| !read_qt_double(p, size, &raster.bottom)
|| !read_qt_string(p, size, &raster.xunits)
|| !read_qt_string(p, size, &raster.yunits)
|| !read_qt_string(p, size, &raster.zunits))
goto fail;
if (!read_qt_byte_array(p, size, &len, &data))
goto fail;
n = raster.xres * raster.yres;
if (len != n*sizeof(gdouble))
goto fail;
xyunit = extract_units(raster.xunits, &qxy);
zunit = extract_units(raster.zunits, &qz);
dfield = gwy_data_field_new(raster.xres, raster.yres,
qxy*fabs(raster.right - raster.left), qxy*fabs(raster.bottom - raster.top),
FALSE);
gwy_data_field_set_si_unit_xy(dfield, xyunit);
gwy_data_field_set_si_unit_z(dfield, zunit);
/* Apparently this is not generally wanted.
gwy_data_field_set_xoffset(dfield, qxy*MIN(raster.left, raster.right));
gwy_data_field_set_yoffset(dfield, qxy*MIN(raster.top, raster.bottom));
*/
g_object_unref(xyunit);
g_object_unref(zunit);
d = gwy_data_field_get_data(dfield);
for (i = 0; i < raster.yres; i++) {
for (j = 0; j < raster.xres; j++) {
d[(raster.yres-1 - i)*raster.xres + j]
= qz*gwy_get_gdouble_le(&data);
}
}
gwy_container_set_object(context->container, gwy_app_get_data_key_for_id(context->channel_id), dfield);
g_object_unref(dfield);
gwy_file_channel_import_log_add(context->container, context->channel_id, NULL, context->filename);
if ((s = strchr(raster.common.name, '[')))
s = g_strchomp(g_strndup(raster.common.name, s - raster.common.name));
else
s = g_strdup(raster.common.name);
gwy_container_set_string(context->container, gwy_app_get_data_title_key_for_id(context->channel_id), s);
/* At this moment we consider the loading successful. */
ok = TRUE;
/* The mask raster is byte-valued. It contains nonzeroes in points that were measured (the opposite of how we
* normally create masks upon import). */
if (read_qt_byte_array(p, size, &len, &data)) {
if (len == raster.xres*raster.yres) {
mfield = make_mask_field(dfield, data);
if (mfield) {
gwy_app_channel_remove_bad_data(dfield, mfield);
gwy_container_set_object(context->container, gwy_app_get_mask_key_for_id(context->channel_id), mfield);
g_object_unref(mfield);
}
}
/* Here's something called view data. It means the data was processed (by whatever means) hence we are not
* interesed in it. */
read_qt_byte_array(p, size, &len, &data);
}
context->channel_id++;
fail:
free_aist_common(&raster.common);
g_free(raster.xunits);
g_free(raster.yunits);
g_free(raster.zunits);
return ok;
}
static gboolean
read_aist_curve(const guchar **p, gsize *size, AistContext *context)
{
AistCurve curve;
GwyGraphModel *gmodel;
GwyGraphCurveModel *gcmodel;
GwySIUnit *xunit, *yunit;
gboolean ok = FALSE;
guint len, viewlen, i;
const guchar *data, *viewdata;
const gdouble *xdata, *ydata;
gdouble *xdatacal, *ydatacal;
gdouble *must_free = NULL;
gdouble qx, qy;
GQuark quark;
gwy_clear(&curve, 1);
gwy_debug("reading common");
if (!read_aist_common(p, size, &curve.common))
goto fail;
gwy_debug("reading curve");
if (!read_qt_int(p, size, &curve.res))
goto fail;
if (!read_qt_byte_array(p, size, &len, &data))
goto fail;
if (len != 2*curve.res*sizeof(gdouble))
goto fail;
/* Again something called view data. Skip it. The units follow. */
if (!read_qt_byte_array(p, size, &viewlen, &viewdata))
goto fail;
if (!read_qt_string(p, size, &curve.xunits) || !read_qt_string(p, size, &curve.yunits))
goto fail;
xunit = extract_units(curve.xunits, &qx);
yunit = extract_units(curve.yunits, &qy);
/* The data are already stored as doubles in the correct order, so save work if also the endianess matches. */
if (G_BYTE_ORDER == G_BIG_ENDIAN) {
must_free = g_new(gdouble, 2*curve.res);
xdata = must_free;
ydata = xdata + curve.res;
gwy_memcpy_byte_swap(data, (guchar*)must_free, 8, 2*curve.res, 7);
}
else if (G_BYTE_ORDER == G_LITTLE_ENDIAN) {
xdata = (const gdouble *)data;
ydata = xdata + curve.res;
}
xdatacal = g_new(gdouble, curve.res);
ydatacal = g_new(gdouble, curve.res);
for (i = 0; i < curve.res; i++) {
xdatacal[i] = xdata[i]*qx;
ydatacal[i] = ydata[i]*qy;
}
gcmodel = gwy_graph_curve_model_new();
gwy_graph_curve_model_set_data(gcmodel, xdatacal, ydatacal, curve.res);
g_object_set(gcmodel,
"mode", GWY_GRAPH_CURVE_LINE,
"description", curve.common.description,
NULL);
g_free(must_free);
g_free(xdatacal);
g_free(ydatacal);
gmodel = gwy_graph_model_new();
gwy_graph_model_add_curve(gmodel, gcmodel);
g_object_unref(gcmodel);
g_object_set(gmodel,
"title", curve.common.name,
"si-unit-x", xunit,
"si-unit-y", yunit,
NULL);
g_object_unref(xunit);
g_object_unref(yunit);
quark = gwy_app_get_graph_key_for_id(context->graph_id+1);
gwy_container_set_object(context->container, quark, gmodel);
g_object_unref(gmodel);
context->graph_id++;
ok = TRUE;
fail:
free_aist_common(&curve.common);
g_free(curve.xunits);
g_free(curve.yunits);
return ok;
}
static gboolean
read_aist_data(const guchar **p, gsize *size, AistContext *context)
{
gchar *type = NULL;
gboolean ok = FALSE;
const guchar *datap;
gsize datasize;
guint len;
if (!read_qt_string(p, size, &type) || !read_qt_int(p, size, &len))
goto fail;
if (*size < len)
goto fail;
datasize = len;
datap = *p;
/* Do this even if we fail, data are skippable. */
*size -= datasize;
*p += datasize;
if (gwy_strequal(type, "raster")) {
gwy_debug("raster data");
if (!read_aist_raster(&datap, &datasize, context))
goto fail;
}
else if (gwy_strequal(type, "curve")) {
gwy_debug("curve data");
if (!read_aist_curve(&datap, &datasize, context))
goto fail;
}
ok = TRUE;
fail:
g_free(type);
return ok;
}
static gboolean
read_aist_tree(const guchar **p, gsize *size, AistContext *context)
{
gboolean is_data_node;
gchar *name = NULL;
guint i, nchildren;
gboolean ok = FALSE;
if (!read_qt_bool(p, size, &is_data_node))
goto fail;
if (is_data_node) {
gwy_debug("reading data");
if (!read_aist_data(p, size, context))
goto fail;
}
if (!read_qt_string(p, size, &name) || !read_qt_int(p, size, &nchildren))
goto fail;
for (i = 0; i < nchildren; i++) {
gwy_debug("recursing");
ok = read_aist_tree(p, size, context);
gwy_debug("returned");
if (!ok)
goto fail;
}
ok = TRUE;
fail:
g_free(name);
return ok;
}
static GwyContainer*
aist_load(const gchar *filename,
G_GNUC_UNUSED GwyRunType mode,
GError **error)
{
AistContext context;
guchar *buffer = NULL;
const guchar *p;
gsize remaining, size = 0;
GError *err = NULL;
if (!gwy_file_get_contents(filename, &buffer, &size, &err)) {
err_GET_FILE_CONTENTS(error, &err);
return NULL;
}
p = buffer;
remaining = size;
context.container = gwy_container_new();
context.filename = filename;
context.channel_id = 0;
context.graph_id = 0;
read_aist_tree(&p, &remaining, &context);
gwy_file_abandon_contents(buffer, size, NULL);
if ((context.channel_id == 0) && (context.graph_id == 0)) {
g_object_unref(context.container);
context.container = NULL;
err_NO_DATA(error);
}
return context.container;
}
static gboolean
read_qt_string(const guchar **p, gsize *size, gchar **value)
{
guint len;
*value = NULL;
if (*size < sizeof(guint32))
return FALSE;
len = gwy_get_guint32_be(p);
*size -= sizeof(guint32);
gwy_debug("QString length: %u", len);
if (*size < len || len % sizeof(gunichar2))
return FALSE;
*value = gwy_utf16_to_utf8((const gunichar2*)*p, len/sizeof(gunichar2), GWY_BYTE_ORDER_BIG_ENDIAN);
gwy_debug("QString data: <%s>", *value);
*size -= len;
*p += len;
return !!*value;
}
static gboolean
read_qt_int(const guchar **p, gsize *size, gint *value)
{
*value = 0;
if (*size < sizeof(gint32))
return FALSE;
*value = gwy_get_gint32_be(p);
gwy_debug("QInt data: %u", *value);
*size -= sizeof(gint32);
return TRUE;
}
static gboolean
read_qt_double(const guchar **p, gsize *size, gdouble *value)
{
*value = 0;
if (*size < sizeof(gdouble))
return FALSE;
*value = gwy_get_gdouble_be(p);
gwy_debug("QDouble data: %g", *value);
*size -= sizeof(gdouble);
return TRUE;
}
static gboolean
read_qt_bool(const guchar **p, gsize *size, gboolean *value)
{
*value = FALSE;
if (*size < sizeof(guchar))
return FALSE;
*value = !!**p;
gwy_debug("QBool data: %d", *value);
*size -= sizeof(guchar);
(*p)++;
return TRUE;
}
static gboolean
read_qt_byte(const guchar **p, gsize *size, guint *value)
{
*value = FALSE;
if (*size < sizeof(guchar))
return FALSE;
*value = **p;
gwy_debug("QByte data: %d", *value);
*size -= sizeof(guchar);
(*p)++;
return TRUE;
}
/* NB: Returned value is a direct pointer to the input buffer. Do not free! */
static gboolean
read_qt_byte_array(const guchar **p, gsize *size, guint *len, const guchar **value)
{
*value = NULL;
if (!read_qt_int(p, size, len))
return FALSE;
/* There can be empty array with length of -1 */
if (-1 == *len)
return TRUE;
if (*size < *len)
return FALSE;
gwy_debug("QByteArray of length %u", *len);
*value = *p;
*size -= *len;
(*p) += *len;
return TRUE;
}
/* vim: set cin columns=120 tw=118 et ts=4 sw=4 cino=>1s,e0,n0,f0,{0,}0,^0,\:1s,=0,g1s,h0,t0,+1s,c3,(0,u0 : */
|