Debian Codegraph @

modules/file/femtoscan-txt.c

raw symbols SCIP
1 /*
2 * $Id: femtoscan-txt.c 28789 2025-11-04 17:14:03Z yeti-dn $
3 * Copyright (C) 2014-2025 David Necas (Yeti).
4 *
5 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public
6 * License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any
7 * later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
10 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11 * details.
12 *
13 * You should have received a copy of the GNU General Public License along with this program; if not, write to the
14 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15 */
16
17 /**
18 * [FILE-MAGIC-FREEDESKTOP]
19 * <mime-type type="application/x-femtoscan-txt">
20 * <comment>FemtoScan text SPM data</comment>
21 * <magic priority="60">
22 * <match type="string" offset="0" value="\tX,nm\t0\t"/>
23 * </magic>
24 * </mime-type>
25 **/
26
27 /**
28 * [FILE-MAGIC-FILEMAGIC]
29 * # FemtoScan text data
30 * 0 string \tX,nm\t0\t FemtoScan SPM text data
31 **/
32
33 /**
34 * [FILE-MAGIC-USERGUIDE]
35 * FemtoScan text data
36 * .txt
37 * Read[1]
38 * [1] Regular sampling in both X and Y direction is assumed.
39 **/
40
41 #include "config.h"
42 #include <glib/gi18n-lib.h>
43 #include <string.h>
44 #include <stdlib.h>
45 #include <gwy.h>
46
47 #include "err.h"
48
49 /* FIXME: This seems to be a pretty safe bet but the axes may be in principle different than in nm and starting from
50 * 0. */
51 #define MAGIC "\tX,nm\t0\t"
52 #define MAGIC_SIZE (sizeof(MAGIC)-1)
53 #define MAGIC2 "Y,nm\tZ,"
54 #define EXTENSION ".txt"
55
56 #define Nanometre (1e-9)
57
58 static gboolean module_register(void);
59 static gint detect_file (const GwyFileDetectInfo *fileinfo,
60 gboolean only_name);
61 static GwyFile* load_file (const gchar *filename,
62 GwyRunModeFlags mode,
63 GError **error);
64
65 static GwyModuleInfo module_info = {
66 GWY_MODULE_ABI_VERSION,
67 &module_register,
68 N_("Imports FemtoScan TXT files."),
69 "Yeti <[email protected]>",
70 "0.2",
71 "David Nečas (Yeti)",
72 "2014",
73 };
74
75 GWY_MODULE_QUERY2(module_info, femtoscan_txt)
76
77 static gboolean
78 module_register(void)
79 {
80 gwy_file_func_register("femtoscan-txt",
81 N_("FemtoScan text files (.txt)"),
82 detect_file, load_file, NULL, NULL);
83
84 return TRUE;
85 }
86
87 static gint
88 detect_file(const GwyFileDetectInfo *fileinfo, gboolean only_name)
89 {
90 if (only_name)
91 return g_str_has_suffix(fileinfo->name_lowercase, EXTENSION) ? 10 : 0;
92
93 if (fileinfo->file_size < MAGIC_SIZE || memcmp(fileinfo->head, MAGIC, MAGIC_SIZE) != 0)
94 return 0;
95
96 return 100;
97 }
98
99 static guint
100 read_values(GArray *values, gchar *p)
101 {
102 guint oldlen = values->len;
103
104 while (TRUE) {
105 gchar *end;
106 gdouble v;
107
108 v = g_ascii_strtod(p, &end);
109 if (end == p)
110 return values->len - oldlen;
111 g_array_append_val(values, v);
112 p = end;
113 }
114 }
115
116 static GwyFile*
117 load_file(const gchar *filename,
118 G_GNUC_UNUSED GwyRunModeFlags mode,
119 GError **error)
120 {
121 GwyFile *file = NULL;
122 GwyField *dfield = NULL;
123 GArray *xcal = NULL, *ycal = NULL, *data = NULL;
124 GwyUnit *unit = NULL;
125 gchar *line, *p, *buffer = NULL;
126 gsize size;
127 GError *err = NULL;
128 gdouble xreal, yreal;
129 guint xres, yres;
130 gint power10;
131
132 if (!g_file_get_contents(filename, &buffer, &size, &err)) {
133 err_GET_FILE_CONTENTS(error, &err);
134 goto fail;
135 }
136
137 p = buffer;
138 line = gwy_str_next_line(&p);
139 if (!g_str_has_prefix(line, MAGIC)) {
140 err_FILE_TYPE(error, "FemtoScan TXT data");
141 goto fail;
142 }
143
144 xcal = g_array_new(FALSE, FALSE, sizeof(gdouble));
145 read_values(xcal, line + strlen("\tX,nm\t"));
146 if (err_DIMENSION(error, xcal->len))
147 goto fail;
148
149 line = gwy_str_next_line(&p);
150 if (!g_str_has_prefix(line, MAGIC2)) {
151 err_FILE_TYPE(error, "FemtoScan TXT data");
152 goto fail;
153 }
154 unit = gwy_unit_new_parse(line + strlen(MAGIC2), &power10);
155
156 ycal = g_array_new(FALSE, FALSE, sizeof(gdouble));
157 data = g_array_new(FALSE, FALSE, sizeof(gdouble));
158 while ((line = gwy_str_next_line(&p))) {
159 gchar *end;
160 gdouble v;
161 guint n;
162
163 if (!*line)
164 break;
165 if (g_ascii_isspace(line[0])) {
166 g_strstrip(line);
167 if (!*line)
168 break;
169 }
170
171 v = g_ascii_strtod(line, &end);
172 if (end == line) {
173 g_set_error(error, GWY_MODULE_FILE_ERROR, GWY_MODULE_FILE_ERROR_DATA,
174 _("Data line does not start with a Y abscissa."));
175 goto fail;
176 }
177
178 g_array_append_val(ycal, v);
179 if ((n = read_values(data, end)) != xcal->len) {
180 g_set_error(error, GWY_MODULE_FILE_ERROR, GWY_MODULE_FILE_ERROR_DATA,
181 _("Data line length %u does not correspond to the number of X abscissas %u."),
182 n, (guint)xcal->len);
183 goto fail;
184 }
185 }
186 if (err_DIMENSION(error, ycal->len))
187 goto fail;
188
189 xres = xcal->len;
190 yres = ycal->len;
191 /* Linearise brutally all calibration. */
192 xreal = (g_array_index(xcal, gdouble, xres-1) - g_array_index(xcal, gdouble, 0))*xres/(xres - 1.0)*Nanometre;
193 yreal = (g_array_index(ycal, gdouble, yres-1) - g_array_index(ycal, gdouble, 0))*yres/(yres - 1.0)*Nanometre;
194 dfield = gwy_field_new(xres, yres, xreal, yreal, FALSE);
195 gwy_assign(gwy_field_get_data(dfield), data->data, xres*yres);
196
197 gwy_unit_set_from_string(gwy_field_get_unit_xy(dfield), "m");
198 gwy_unit_assign(gwy_field_get_unit_z(dfield), unit);
199 if (power10)
200 gwy_field_multiply(dfield, gwy_exp10(power10));
201
202 file = gwy_file_new_in_construction();
203 gwy_file_set_image(file, 0, dfield);
204 gwy_log_add_import(file, GWY_FILE_IMAGE, 0, NULL, filename);
205
206 fail:
207 g_clear_object(&unit);
208 g_clear_object(&dfield);
209 g_free(buffer);
210 if (xcal)
211 g_array_free(xcal, TRUE);
212 if (ycal)
213 g_array_free(ycal, TRUE);
214 if (data)
215 g_array_free(data, TRUE);
216
217 return file;
218 }
219
220 /* 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 : */