-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathnanoscope.c
1800 lines (1600 loc) · 60.3 KB
/
nanoscope.c
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
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* @(#) $Id$
* Copyright (C) 2004-2015 David Necas (Yeti), Petr Klapetek.
* E-mail: yeti@gwyddion.net, klapetek@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.
*/
/**
* [FILE-MAGIC-FREEDESKTOP]
* <mime-type type="application/x-nanoscope-iii-spm">
* <comment>Nanoscope III SPM data</comment>
* <magic priority="80">
* <match type="string" offset="0" value="\\*File list\r\n"/>
* <match type="string" offset="0" value="?*File list\r\n"/>
* </magic>
* </mime-type>
**/
/**
* [FILE-MAGIC-FILEMAGIC]
* # Nanoscope III
* # Two header variants.
* 0 string \\*File\ list\x0d\x0a Nanoscope III SPM binary data
* 0 string ?*File\ list\x0d\x0a Nanoscope III SPM text data
**/
/**
* [FILE-MAGIC-USERGUIDE]
* Veeco Nanoscope III
* .001 .002 etc.
* Read SPS:Limited[1] Volume
* [1] Spectra curves are imported as graphs, positional information is lost.
**/
#include "config.h"
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <libgwyddion/gwymacros.h>
#include <libgwyddion/gwyutils.h>
#include <libgwyddion/gwymath.h>
#include <libprocess/datafield.h>
#include <libgwydgets/gwygraphmodel.h>
#include <libgwymodule/gwymodule-file.h>
#include <app/gwymoduleutils-file.h>
#include <app/data-browser.h>
#include "err.h"
#define MAGIC_BIN "\\*File list\r\n"
#define MAGIC_TXT "?*File list\r\n"
#define MAGIC_SIZE (sizeof(MAGIC_TXT)-1)
#define MAGIC_BIN_PARTIAL "\\*File list"
#define MAGIC_TXT_PARTIAL "?*File list"
#define MAGIC_SIZE_PARTIAL (sizeof(MAGIC_TXT_PARTIAL)-1)
#define MAGIC_FORCE_BIN "\\*Force file list\r\n"
#define MAGIC_FORCE_SIZE (sizeof(MAGIC_FORCE_BIN)-1)
typedef enum {
NANOSCOPE_FILE_TYPE_NONE = 0,
NANOSCOPE_FILE_TYPE_BIN,
NANOSCOPE_FILE_TYPE_BIN32,
NANOSCOPE_FILE_TYPE_TXT,
NANOSCOPE_FILE_TYPE_FORCE_BIN,
NANOSCOPE_FILE_TYPE_FORCE_BIN32,
NANOSCOPE_FILE_TYPE_FORCE_VOLUME,
NANOSCOPE_FILE_TYPE_FORCE_VOLUME32,
NANOSCOPE_FILE_TYPE_BROKEN
} NanoscopeFileType;
typedef enum {
NANOSCOPE_VALUE_OLD = 0,
NANOSCOPE_VALUE_VALUE,
NANOSCOPE_VALUE_SCALE,
NANOSCOPE_VALUE_SELECT
} NanoscopeValueType;
typedef enum {
NANOSCOPE_SPECTRA_IV,
NANOSCOPE_SPECTRA_FZ,
} NanoscopeSpectraType;
/*
* Old-style record is
* \Foo: HardValue (HardScale)
* where HardScale is optional.
*
* New-style record is
* \@Bar: V [SoftScale] (HardScale) HardValue
* where SoftScale and HardScale are optional.
*/
typedef struct {
NanoscopeValueType type;
const gchar *soft_scale;
gdouble hard_scale;
const gchar *hard_scale_units;
gdouble hard_value;
const gchar *hard_value_str;
const gchar *hard_value_units;
} NanoscopeValue;
typedef struct {
GHashTable *hash;
GwyDataField *data_field;
GwyGraphModel *graph_model;
GwyBrick *brick;
GwyBrick *second_brick;
} NanoscopeData;
static gboolean module_register (void);
static gint nanoscope_detect (const GwyFileDetectInfo *fileinfo,
gboolean only_name);
static GwyContainer* nanoscope_load (const gchar *filename,
GwyRunType mode,
GError **error);
static GwyDataField* hash_to_data_field (GHashTable *hash,
GHashTable *scannerlist,
GHashTable *scanlist,
GHashTable *contrlist,
NanoscopeFileType file_type,
guint bufsize,
gchar *buffer,
gint gxres,
gint gyres,
gboolean gnonsquare_aspect,
gchar **p,
GError **error);
static GwyGraphModel* hash_to_curve (GHashTable *hash,
GHashTable *forcelist,
GHashTable *scanlist,
GHashTable *scannerlist,
NanoscopeFileType file_type,
guint bufsize,
gchar *buffer,
gint gxres,
GError **error);
static GwyBrick* hash_to_brick (GHashTable *hash,
GHashTable *forcelist,
GHashTable *scanlist,
GHashTable *scannerlist,
NanoscopeFileType file_type,
guint bufsize,
gchar *buffer,
GwyBrick **second_brick,
GError **error);
static gboolean read_text_data (guint n,
gdouble *data,
gchar **buffer,
gint bpp,
GError **error);
static gboolean read_binary_data (gint n,
gdouble *data,
gchar *buffer,
gint bpp,
gint qbpp,
GError **error);
static GHashTable* read_hash (gchar **buffer,
GError **error);
static void get_scan_list_res (GHashTable *hash,
gint *xres,
gint *yres);
static gboolean has_nonsquare_aspect (GHashTable *hash);
static GwySIUnit* get_physical_scale (GHashTable *hash,
GHashTable *scannerlist,
GHashTable *scanlist,
GHashTable *contrlist,
gdouble *scale,
GError **error);
static GwySIUnit* get_spec_ordinate_scale(GHashTable *hash,
GHashTable *scanlist,
gdouble *scale,
gboolean *convert_to_force,
GError **error);
static GwySIUnit* get_spec_abscissa_scale(GHashTable *hash,
GHashTable *forcelist,
GHashTable *scannerlist,
GHashTable *scanlist,
gdouble *xreal,
gdouble *xoff,
NanoscopeSpectraType *spectype,
GError **error);
static GwyContainer* nanoscope_get_metadata (GHashTable *hash,
GList *list);
static NanoscopeValue* parse_value (const gchar *key,
gchar *line);
static GwyDataField* preview_from_brick (GwyBrick *brick);
static GwyModuleInfo module_info = {
GWY_MODULE_ABI_VERSION,
&module_register,
N_("Imports Veeco (Digital Instruments) Nanoscope data files, "
"version 3 or newer."),
"Yeti <yeti@gwyddion.net>",
"0.34",
"David Nečas (Yeti) & Petr Klapetek",
"2004",
};
GWY_MODULE_QUERY(module_info)
static gboolean
module_register(void)
{
gwy_file_func_register("nanoscope",
N_("Nanoscope III files"),
(GwyFileDetectFunc)&nanoscope_detect,
(GwyFileLoadFunc)&nanoscope_load,
NULL,
NULL);
return TRUE;
}
static gint
nanoscope_detect(const GwyFileDetectInfo *fileinfo,
gboolean only_name)
{
gint score = 0;
if (only_name)
return 0;
if (fileinfo->buffer_len > MAGIC_SIZE
&& (!memcmp(fileinfo->head, MAGIC_TXT_PARTIAL, MAGIC_SIZE_PARTIAL)
|| !memcmp(fileinfo->head, MAGIC_BIN_PARTIAL, MAGIC_SIZE_PARTIAL)
|| !memcmp(fileinfo->head, MAGIC_FORCE_BIN, MAGIC_FORCE_SIZE)))
score = 100;
return score;
}
static GwyContainer*
nanoscope_load(const gchar *filename,
G_GNUC_UNUSED GwyRunType mode,
GError **error)
{
GwyContainer *meta, *container = NULL;
GError *err = NULL;
gchar *buffer = NULL;
gchar *p;
const gchar *self;
gsize size = 0;
NanoscopeFileType file_type;
NanoscopeData *ndata;
NanoscopeValue *val;
GHashTable *hash, *scannerlist = NULL, *scanlist = NULL, *forcelist = NULL,
*contrlist = NULL, *equipmentlist = NULL;
GList *l, *list = NULL;
gint i, xres = 0, yres = 0;
gboolean nonsquare_aspect = FALSE, ok;
GwyDataField *preview;
gchar *prevkey, *titlekey;
if (!g_file_get_contents(filename, &buffer, &size, &err)) {
err_GET_FILE_CONTENTS(error, &err);
return NULL;
}
file_type = NANOSCOPE_FILE_TYPE_NONE;
if (size > MAGIC_SIZE) {
if (!memcmp(buffer, MAGIC_TXT, MAGIC_SIZE))
file_type = NANOSCOPE_FILE_TYPE_TXT;
else if (!memcmp(buffer, MAGIC_BIN, MAGIC_SIZE))
file_type = NANOSCOPE_FILE_TYPE_BIN;
else if (!memcmp(buffer, MAGIC_FORCE_BIN, MAGIC_FORCE_SIZE))
file_type = NANOSCOPE_FILE_TYPE_FORCE_BIN;
else if (!memcmp(buffer, MAGIC_TXT_PARTIAL, MAGIC_SIZE_PARTIAL)
|| !memcmp(buffer, MAGIC_BIN_PARTIAL, MAGIC_SIZE_PARTIAL))
file_type = NANOSCOPE_FILE_TYPE_BROKEN;
}
if (!file_type) {
g_set_error(error, GWY_MODULE_FILE_ERROR, GWY_MODULE_FILE_ERROR_DATA,
_("File is not a Nanoscope file, "
"or it is a unknown subtype."));
g_free(buffer);
return NULL;
}
if (file_type == NANOSCOPE_FILE_TYPE_BROKEN) {
g_set_error(error, GWY_MODULE_FILE_ERROR, GWY_MODULE_FILE_ERROR_DATA,
_("File has been damaged by change of line endings, "
"resulting in corruption of the binary part of the file."
"\n\n"
"Typically, this occurs if the file is treated as text "
"when sent by e-mail uncompressed, sent by FTP in ascii "
"mode (use binary), compressed by ‘Send to compressed "
"folder’ in some versions of MS Windows, or any other "
"file transfer that attempts to store text "
"platform-independently."));
g_free(buffer);
return NULL;
}
gwy_debug("File type: %d", file_type);
/* as we already know file_type, fix the first char for hash reading */
*buffer = '\\';
p = buffer;
while ((hash = read_hash(&p, &err))) {
ndata = g_new0(NanoscopeData, 1);
ndata->hash = hash;
list = g_list_append(list, ndata);
val = g_hash_table_lookup(hash, "Start context");
if (val && gwy_strequal(val->hard_value_str, "FVOL"))
file_type = NANOSCOPE_FILE_TYPE_FORCE_VOLUME;
/* In version 9.2 all data magically became 32bit. */
self = g_hash_table_lookup(hash, "#self");
if (self
&& gwy_strequal(self, "File list")
&& (val = g_hash_table_lookup(hash, "Version"))) {
gulong version = strtol(val->hard_value_str, NULL, 16);
if (version >= 0x09200000) {
gwy_debug("version is 0x%lx, assuming 32bit data", version);
if (file_type == NANOSCOPE_FILE_TYPE_BIN)
file_type = NANOSCOPE_FILE_TYPE_BIN32;
else if (file_type == NANOSCOPE_FILE_TYPE_FORCE_BIN)
file_type = NANOSCOPE_FILE_TYPE_FORCE_BIN32;
else if (file_type == NANOSCOPE_FILE_TYPE_FORCE_VOLUME)
file_type = NANOSCOPE_FILE_TYPE_FORCE_VOLUME32;
}
}
}
if (err) {
g_propagate_error(error, err);
ok = FALSE;
}
else
ok = TRUE;
for (l = list; ok && l; l = g_list_next(l)) {
ndata = (NanoscopeData*)l->data;
hash = ndata->hash;
self = g_hash_table_lookup(hash, "#self");
/* The alternate names were found in files written by some beast
* called Nanoscope E software */
if (gwy_strequal(self, "Scanner list")
|| gwy_strequal(self, "Microscope list")) {
scannerlist = hash;
continue;
}
if (gwy_strequal(self, "Equipment list")) {
equipmentlist = hash;
continue;
}
if (gwy_strequal(self, "File list")) {
continue;
}
if (gwy_strequal(self, "Controller list")) {
contrlist = hash;
continue;
}
if (gwy_stramong(self, "Ciao scan list", "Afm list", "Stm list",
"NC Afm list", NULL)) {
get_scan_list_res(hash, &xres, &yres);
nonsquare_aspect = has_nonsquare_aspect(hash);
scanlist = hash;
}
if (gwy_stramong(self, "Ciao force list", NULL)) {
get_scan_list_res(hash, &xres, &yres);
nonsquare_aspect = has_nonsquare_aspect(hash);
forcelist = hash;
}
if (!gwy_stramong(self, "AFM image list", "Ciao image list",
"STM image list", "NCAFM image list",
"Ciao force image list", "Image list", NULL))
continue;
gwy_debug("processing hash %s", self);
if (file_type == NANOSCOPE_FILE_TYPE_FORCE_BIN) {
ndata->graph_model = hash_to_curve(hash, forcelist, scanlist,
scannerlist,
file_type,
size, buffer,
xres,
error);
ok = ok && ndata->graph_model;
}
else if (file_type == NANOSCOPE_FILE_TYPE_FORCE_VOLUME) {
if (!gwy_strequal(self, "Ciao force image list"))
continue;
ndata->brick = hash_to_brick(hash, forcelist, scanlist,
equipmentlist,
file_type,
size, buffer,
&ndata->second_brick,
error);
ok = ok && ndata->brick;
}
else {
ndata->data_field = hash_to_data_field(hash, scannerlist, scanlist,
contrlist,
file_type,
size, buffer,
xres, yres, nonsquare_aspect,
&p, error);
ok = ok && ndata->data_field;
}
}
if (ok) {
gchar key[40];
i = 0;
container = gwy_container_new();
for (l = list; l; l = g_list_next(l)) {
ndata = (NanoscopeData*)l->data;
if (ndata->data_field) {
const gchar *name;
g_snprintf(key, sizeof(key), "/%d/data", i);
gwy_container_set_object_by_name(container, key,
ndata->data_field);
g_snprintf(key, sizeof(key), "/%d/data/title", i);
name = NULL;
if ((val = g_hash_table_lookup(ndata->hash,
"@2:Image Data"))
|| (val = g_hash_table_lookup(ndata->hash,
"@3:Image Data"))) {
if (val->soft_scale)
name = val->soft_scale;
else if (val->hard_value_str)
name = val->hard_value_str;
}
else if ((val = g_hash_table_lookup(ndata->hash, "Image data")))
name = val->hard_value_str;
if (name)
gwy_container_set_string_by_name(container, key,
g_strdup(name));
meta = nanoscope_get_metadata(ndata->hash, list);
g_snprintf(key, sizeof(key), "/%d/meta", i);
gwy_container_set_object_by_name(container, key, meta);
g_object_unref(meta);
gwy_app_channel_check_nonsquare(container, i);
gwy_file_channel_import_log_add(container, i, NULL,
filename);
i++;
}
else if (ndata->graph_model) {
GQuark quark = gwy_app_get_graph_key_for_id(i+1);
gwy_container_set_object(container, quark, ndata->graph_model);
i++;
}
else if (ndata->brick) {
GQuark quark = gwy_app_get_brick_key_for_id(i+1);
preview = preview_from_brick(ndata->brick);
prevkey = g_strconcat(g_quark_to_string(quark), "/preview", NULL);
titlekey = g_strconcat(g_quark_to_string(quark), "/title", NULL);
gwy_container_set_object(container, quark, ndata->brick);
gwy_container_set_object(container, g_quark_from_string(prevkey), preview);
gwy_container_set_string(container, g_quark_from_string(titlekey), g_strdup("Approach"));
i++;
if (ndata->second_brick) {
quark = gwy_app_get_brick_key_for_id(i+1);
preview = preview_from_brick(ndata->second_brick);
prevkey = g_strconcat(g_quark_to_string(quark), "/preview", NULL);
titlekey = g_strconcat(g_quark_to_string(quark), "/title", NULL);
gwy_container_set_object(container, quark, ndata->second_brick);
gwy_container_set_object(container, g_quark_from_string(prevkey), preview);
gwy_container_set_string(container, g_quark_from_string(titlekey), g_strdup("Retract"));
i++;
}
}
}
if (!i)
GWY_OBJECT_UNREF(container);
}
for (l = list; l; l = g_list_next(l)) {
ndata = (NanoscopeData*)l->data;
GWY_OBJECT_UNREF(ndata->data_field);
GWY_OBJECT_UNREF(ndata->graph_model);
if (ndata->hash)
g_hash_table_destroy(ndata->hash);
g_free(ndata);
}
g_free(buffer);
g_list_free(list);
if (!container && ok)
err_NO_DATA(error);
return container;
}
static void
get_scan_list_res(GHashTable *hash,
gint *xres, gint *yres)
{
NanoscopeValue *val;
/* XXX: Some observed files contained correct dimensions only in
* a global section, sizes in `image list' sections were bogus.
* Version: 0x05300001 */
if ((val = g_hash_table_lookup(hash, "Samps/line")))
*xres = GWY_ROUND(val->hard_value);
if ((val = g_hash_table_lookup(hash, "Lines")))
*yres = GWY_ROUND(val->hard_value);
gwy_debug("Global xres, yres = %d, %d", *xres, *yres);
}
static gboolean
has_nonsquare_aspect(GHashTable *hash)
{
NanoscopeValue *val;
gdouble ar;
val = g_hash_table_lookup(hash, "Aspect ratio");
if (!val || gwy_strequal(val->hard_value_str, "1:1"))
return FALSE;
ar = g_ascii_strtod(val->hard_value_str, NULL);
if (ar > 0.0 && ar != 1.0)
return TRUE;
return FALSE;
}
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);
}
/* FIXME: This is a bit simplistic */
static GwyContainer*
nanoscope_get_metadata(GHashTable *hash,
GList *list)
{
static const gchar *hashes[] = {
"File list", "Scanner list", "Equipment list", "Ciao scan list",
};
GwyContainer *meta;
GList *l;
guint i;
meta = gwy_container_new();
for (l = list; l; l = g_list_next(l)) {
GHashTable *h = ((NanoscopeData*)l->data)->hash;
for (i = 0; i < G_N_ELEMENTS(hashes); i++) {
if (gwy_strequal(g_hash_table_lookup(h, "#self"), hashes[i])) {
g_hash_table_foreach(h, add_metadata, meta);
break;
}
}
}
g_hash_table_foreach(hash, add_metadata, meta);
return meta;
}
static GwyDataField*
hash_to_data_field(GHashTable *hash,
GHashTable *scannerlist,
GHashTable *scanlist,
GHashTable *contrlist,
NanoscopeFileType file_type,
guint bufsize,
gchar *buffer,
gint gxres,
gint gyres,
gboolean gnonsquare_aspect,
gchar **p,
GError **error)
{
NanoscopeValue *val;
GwyDataField *dfield;
GwySIUnit *unitz, *unitxy;
gchar *s, *end;
gchar un[5];
gint xres, yres, bpp, qbpp, offset, size, power10;
gdouble xreal, yreal, q;
gdouble *data;
gboolean size_ok, use_global, nonsquare_aspect;
if (!require_keys(hash, error, "Samps/line", "Number of lines",
"Scan size", "Data offset", "Data length", NULL))
return NULL;
val = g_hash_table_lookup(hash, "Samps/line");
xres = GWY_ROUND(val->hard_value);
val = g_hash_table_lookup(hash, "Number of lines");
yres = GWY_ROUND(val->hard_value);
/* Bytes/pixel determines the scaling factor, not actual raw data type.
* This is what Bruker people say. */
val = g_hash_table_lookup(hash, "Bytes/pixel");
qbpp = val ? GWY_ROUND(val->hard_value) : 2;
bpp = 2;
if (file_type == NANOSCOPE_FILE_TYPE_BIN32)
bpp = 4;
nonsquare_aspect = has_nonsquare_aspect(hash);
gwy_debug("xres %d, yres %d", xres, yres);
gwy_debug("gxres %d, gyres %d", gxres, gyres);
/* scan size */
val = g_hash_table_lookup(hash, "Scan size");
xreal = g_ascii_strtod(val->hard_value_str, &end);
if (errno || *end != ' ') {
g_set_error(error, GWY_MODULE_FILE_ERROR, GWY_MODULE_FILE_ERROR_DATA,
_("Cannot parse `Scan size' field."));
return NULL;
}
gwy_debug("xreal = %g", xreal);
s = end+1;
yreal = g_ascii_strtod(s, &end);
if (errno || *end != ' ') {
/* Old files don't have two numbers here, assume equal dimensions */
yreal = xreal;
end = s;
}
gwy_debug("yreal = %g", yreal);
while (g_ascii_isspace(*end))
end++;
if (sscanf(end, "%4s", un) != 1) {
g_set_error(error, GWY_MODULE_FILE_ERROR, GWY_MODULE_FILE_ERROR_DATA,
_("Cannot parse `Scan size' field."));
return NULL;
}
gwy_debug("xy unit: <%s>", un);
unitxy = gwy_si_unit_new_parse(un, &power10);
q = pow10(power10);
xreal *= q;
yreal *= q;
/* Prevents possible division by 0 when gxres and gyres are not set for
* whatever reason. */
if (!gxres)
gxres = xres;
if (!gyres)
gyres = xres;
offset = size = 0;
if (file_type == NANOSCOPE_FILE_TYPE_BIN
|| file_type == NANOSCOPE_FILE_TYPE_BIN32) {
val = g_hash_table_lookup(hash, "Data offset");
offset = GWY_ROUND(val->hard_value);
val = g_hash_table_lookup(hash, "Data length");
size = GWY_ROUND(val->hard_value);
size_ok = FALSE;
use_global = FALSE;
/* Try channel size and local size */
if (!size_ok && size == bpp*xres*yres)
size_ok = TRUE;
if (!size_ok && size == bpp*gxres*gyres) {
size_ok = TRUE;
use_global = TRUE;
}
/* If they don't match exactly, try whether they at least fit inside */
if (!size_ok && size > bpp*MAX(xres*yres, gxres*gyres)) {
size_ok = TRUE;
use_global = (xres*yres < gxres*gyres);
}
if (!size_ok && size > bpp*MIN(xres*yres, gxres*gyres)) {
size_ok = TRUE;
use_global = (xres*yres > gxres*gyres);
}
if (!size_ok) {
err_SIZE_MISMATCH(error, size, bpp*xres*yres, TRUE);
return NULL;
}
if (use_global) {
if (gxres) {
xreal *= (gdouble)gxres/xres;
xres = gxres;
}
if (gyres) {
yreal *= (gdouble)gyres/yres;
yres = gyres;
}
}
else if (nonsquare_aspect) {
gwy_debug("nonsquare_aspect");
if (gnonsquare_aspect) {
gwy_debug("gnonsquare_aspect");
/* Reported by Peter Eaton. Not sure if we detect it
* correctly. */
yreal *= yres;
yreal /= xres;
}
else {
/* This seems to be the common case. */
yreal *= yres;
yreal /= gyres;
}
}
if (err_DIMENSION(error, xres) || err_DIMENSION(error, yres))
return NULL;
if (err_SIZE_MISMATCH(error, offset + size, bufsize, FALSE))
return NULL;
/* Use negated positive conditions to catch NaNs */
if (!((xreal = fabs(xreal)) > 0)) {
g_warning("Real x size is 0.0, fixing to 1.0");
xreal = 1.0;
}
if (!((yreal = fabs(yreal)) > 0)) {
g_warning("Real y size is 0.0, fixing to 1.0");
yreal = 1.0;
}
}
q = 1.0;
unitz = get_physical_scale(hash, scannerlist, scanlist, contrlist, &q, error);
if (!unitz)
return NULL;
dfield = gwy_data_field_new(xres, yres, xreal, yreal, FALSE);
data = gwy_data_field_get_data(dfield);
switch (file_type) {
case NANOSCOPE_FILE_TYPE_TXT:
if (!read_text_data(xres*yres, data, p, qbpp, error)) {
g_object_unref(dfield);
return NULL;
}
break;
case NANOSCOPE_FILE_TYPE_BIN:
case NANOSCOPE_FILE_TYPE_BIN32:
if (!read_binary_data(xres*yres, data, buffer + offset, bpp, qbpp,
error)) {
g_object_unref(dfield);
return NULL;
}
break;
default:
g_assert_not_reached();
break;
}
gwy_data_field_multiply(dfield, q);
gwy_data_field_invert(dfield, TRUE, FALSE, FALSE);
gwy_data_field_set_si_unit_z(dfield, unitz);
g_object_unref(unitz);
gwy_data_field_set_si_unit_xy(dfield, unitxy);
g_object_unref(unitxy);
return dfield;
}
static GwyBrick*
hash_to_brick(GHashTable *hash,
GHashTable *forcelist,
GHashTable *scanlist,
G_GNUC_UNUSED GHashTable *scannerlist,
NanoscopeFileType file_type,
guint bufsize,
gchar *buffer,
GwyBrick **second_brick,
GError **error)
{
GwyBrick *brick;
NanoscopeValue *val;
gdouble *adata, *rdata;
guint xres, yres, zres, offset, length, bpp, qbpp, i, j, l, st;
gdouble q;
gdouble *storage;
gdouble newl;
gdouble *abuf, *rbuf;
gint floorv;
gboolean continuous = FALSE;
gdouble rest;
gwy_debug("Loading brick");
if (!require_keys(hash, error, "Samps/line", "Data offset", "Data length",
NULL))
return NULL;
if (!require_keys(forcelist, error, "force/line", NULL))
return NULL;
if (!require_keys(scanlist, error, "Scan size", NULL))
return NULL;
if ((val = g_hash_table_lookup(scanlist, "Capture Mode"))) {
if (strstr(val->hard_value_str, "Continuous")) {
continuous = TRUE;
}
}
/* scan size */
val = g_hash_table_lookup(hash, "Data offset");
offset = GWY_ROUND(val->hard_value);
gwy_debug("data offset %u", offset);
val = g_hash_table_lookup(hash, "Data length");
length = GWY_ROUND(val->hard_value);
gwy_debug("data length %u", length);
val = g_hash_table_lookup(hash, "Samps/line");
zres = GWY_ROUND(val->hard_value);
gwy_debug("samples in force line %u", zres);
val = g_hash_table_lookup(forcelist, "force/line");
xres = GWY_ROUND(val->hard_value);
gwy_debug("force curves per line %u", xres);
/* Bytes/pixel determines the scaling factor, not actual raw data type.
* This is what Bruker people say. */
val = g_hash_table_lookup(hash, "Bytes/pixel");
qbpp = val ? GWY_ROUND(val->hard_value) : 2;
bpp = 2;
if (file_type == NANOSCOPE_FILE_TYPE_FORCE_VOLUME32)
bpp = 4;
/* FIXME: It is not clear how we should get yres. Just divide the total
* data size with all the known sizes and hope for the best. */
yres = length/(xres*zres*bpp*2);
if (err_DIMENSION(error, xres)
|| err_DIMENSION(error, yres)
|| err_DIMENSION(error, zres))
return NULL;
if (err_SIZE_MISMATCH(error, offset + length, bufsize, FALSE))
return NULL;
brick = gwy_brick_new(xres, yres, zres, xres, yres, zres, 0);
adata = gwy_brick_get_data(brick);
/*up to now it seems that second brick is always present*/
*second_brick = gwy_brick_new(xres, yres, zres, xres, yres, zres, 0);
rdata = gwy_brick_get_data(*second_brick);
//gwy_debug("brick size expected to be %dx%dx%d (two bricks loaded), data length %d, expected %d", xres, yres, zres, length, xres*yres*zres*bpp*2);
q = pow(1.0/256.0, qbpp);
storage = (gdouble*) malloc((xres*yres*zres*2 + 100) * sizeof(gdouble));
st = 0;
if (bpp == 4) {
gint32 *d = (gint32*)(buffer + offset);
for (i = 0; i < yres; i++) {
for (j = 0; j < xres; j++) {
/* Approach curves */
for (l = 0; l < zres; l++) {
storage[st++] = q*GINT32_FROM_LE(*d);
d++;
}
/* Retract curves */
for (l = 0; l < zres; l++) {
storage[st + zres-l-1] = q*GINT32_FROM_LE(*d);
d++;
}
st += zres;
}
}
}
else {
gint16 *d = (gint16*)(buffer + offset);
for (i = 0; i < yres; i++) {
for (j = 0; j < xres; j++) {
/* Approach curves */
for (l = 0; l < zres; l++) {
storage[st++] = q*GINT16_FROM_LE(*d);
d++;
}
/* Retract curves */
for (l = 0; l < zres; l++) {
storage[st + zres-l-1] = q*GINT16_FROM_LE(*d);
d++;
}
st += zres;
}
}
}
if (continuous) {
st = 47; //ad hoc fix, this should be detected automaticallz
} else
st = 0;
/*split data again with this strange shift*/
for (i = 0; i < yres; i++) {
for (j = 0; j < xres; j++) {
// Approach curves
for (l = 0; l < zres; l++) {
adata[(l*yres + i)*xres + j] = storage[st++];
if (adata[(l*yres + i)*xres + j]<-0.45) adata[(l*yres + i)*xres + j] = storage[st-2]; //strange PF/QNM value?
}
// Retract curves
for (l = 0; l < zres; l++) {
rdata[((zres-l-1)*yres + i)*xres + j] = storage[st++];
}
}
}
if (continuous) {
/*remove sine distortion from the data*/
abuf = g_new(gdouble, zres);
rbuf = g_new(gdouble, zres);
for (i = 0; i < yres; i++) {
for (j = 0; j < xres; j++) {
for (l = 0; l < zres; l++) {
newl = zres*(asin(2.0*(gdouble)l/(gdouble)zres-1.0)+G_PI/2)/G_PI;
floorv = floor(newl);
rest = newl - floorv;
abuf[l] = (1.0 - rest)*adata[(floorv*yres + i)*xres + j] + rest*adata[((floorv+1)*yres + i)*xres + j];
rbuf[l] = (1.0 - rest)*rdata[(floorv*yres + i)*xres + j] + rest*rdata[((floorv+1)*yres + i)*xres + j];
}
for (l = 0; l < zres; l++) {
adata[(l*yres + i)*xres + j] = abuf[l];
rdata[(l*yres + i)*xres + j] = rbuf[l];
}
}
}
g_free(abuf);
g_free(rbuf);
}
g_free(storage);
return brick;
}
static GwyDataField*
preview_from_brick(GwyBrick *brick)
{
GwyDataField *dfield = gwy_data_field_new(10, 10, 10, 10, FALSE);
gwy_brick_sum_plane(brick, dfield, 0, 0, 0, gwy_brick_get_xres(brick),
gwy_brick_get_yres(brick), -1, FALSE);
return dfield;
}
#define CHECK_AND_APPLY(op, hash, key) \
if (!(val = g_hash_table_lookup((hash), (key)))) { \
err_MISSING_FIELD(error, (key)); \
return NULL; \
} \
*scale op val->hard_value