File: dmefile.c

package info (click to toggle)
gwyddion 2.62-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 51,952 kB
  • sloc: ansic: 398,486; python: 7,877; sh: 5,492; makefile: 4,723; xml: 3,883; cpp: 1,969; pascal: 418; perl: 154; ruby: 130
file content (348 lines) | stat: -rw-r--r-- 11,736 bytes parent folder | download
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
/*
 *  $Id: dmefile.c 24040 2021-08-21 20:50:16Z yeti-dn $
 *  Copyright (C) 2006 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-dme-spm">
 *   <comment>DME SPM data</comment>
 *   <magic priority="80">
 *     <match type="string" offset="0" value="RSCOPE"/>
 *   </magic>
 * </mime-type>
 **/

/**
 * [FILE-MAGIC-FILEMAGIC]
 * # DME Rasterscope.
 * # Using just RSCOPE at the beginning is a bit rough, look also for the date
 * # field.
 * 0 string RSCOPE
 * >16 regex [0-9]{2}-[0-9]{2}-[0-9]{2} Danish Micro Engineering Rasterscope SPM data
 **/

/**
 * [FILE-MAGIC-USERGUIDE]
 * DME Rasterscope
 * .img
 * Read
 **/

#include "config.h"
#include <string.h>
#include <libgwyddion/gwymacros.h>
#include <libgwyddion/gwymath.h>
#include <libgwyddion/gwyutils.h>
#include <libprocess/datafield.h>
#include <libgwymodule/gwymodule-file.h>
#include <app/gwymoduleutils-file.h>

#include "err.h"
#include "get.h"

#define EXTENSION ".img"

#define MAGIC "RSCOPE"
#define MAGIC_SIZE (sizeof(MAGIC)-1)

#define Angstrom (1e-10)

enum {
    HEADER_SIZE = 4048
};

typedef struct {
    gchar program_name[6];
    guint32 file_version;
    guint32 program_version;
    guint32 header_size;
    guint32 data_size;
    gchar time[17];
    gchar comment[148 + 1];
    guint data_type;  /* FIXME */
    guint32 xres;
    guint32 yres;
    gdouble xreal;
    gdouble yreal;
    gdouble xoff;
    gdouble yoff;
    gchar title[19 + 1];
    gdouble sample_pause;
    gdouble sample_speed;
    gdouble tunnel_current;
    gdouble bias;
    gdouble loop_gain;
    guint direction;  /* FIXME */
    guint head_type;  /* FIXME */
    gdouble x_calibration;
    gdouble y_calibration;  /* XXX: Missing in the documentation? */
    gdouble z_calibration;
    gdouble min;
    gdouble max;
    gdouble mean;
    gdouble full_scale;
    gdouble scale_offset;
    gdouble x_slope_corr;
    gdouble y_slope_corr;
    gdouble offset_corr;
    gboolean slope_calculated;
    gboolean roughness_valid;
    gdouble ra;
    gdouble rms;
    gdouble ry;
    guint display_form_mode;  /* FIXME */
    guint display_rotated;  /* FIXME */
    guint32 display_angle_polar;
    guint32 display_angle_azimuthal;
    gdouble scale_fraction;
    guint slope_mode;  /* FIXME */
    gdouble height_scale_factor;
} DMEFile;

static gboolean      module_register(void);
static gint          dme_detect     (const GwyFileDetectInfo *fileinfo,
                                     gboolean only_name);
static GwyContainer* dme_load       (const gchar *filename,
                                     GwyRunType mode,
                                     GError **error);
static void          dme_read_header(const guchar *p,
                                     DMEFile *dmefile);

static GwyModuleInfo module_info = {
    GWY_MODULE_ABI_VERSION,
    &module_register,
    N_("Imports Danish Micro Engineering (DME) data files."),
    "Yeti <yeti@gwyddion.net>",
    "0.5.1",
    "David Nečas (Yeti) & Petr Klapetek",
    "2006",
};

GWY_MODULE_QUERY2(module_info, dmefile)

static gboolean
module_register(void)
{
    gwy_file_func_register("dmefile",
                           N_("DME files (.img)"),
                           (GwyFileDetectFunc)&dme_detect,
                           (GwyFileLoadFunc)&dme_load,
                           NULL,
                           NULL);

    return TRUE;
}

static gint
dme_detect(const GwyFileDetectInfo *fileinfo,
           gboolean only_name)
{
    if (only_name)
        return g_str_has_suffix(fileinfo->name_lowercase, EXTENSION) ? 15 : 0;

    if (fileinfo->file_size > HEADER_SIZE
        && memcmp(fileinfo->head, MAGIC, MAGIC_SIZE) == 0)
        return 100;

    return 0;
}

static GwyContainer*
dme_load(const gchar *filename,
         G_GNUC_UNUSED GwyRunType mode,
         GError **error)
{
    GwyContainer *container = NULL;
    guchar *buffer = NULL;
    gsize size = 0;
    GError *err = NULL;
    GwyDataField *dfield = NULL;
    guint i;
    DMEFile dmefile;
    gdouble *data;
    const gint16 *ls16;
    GwySIUnit *siunit;
    gdouble q;

    if (!gwy_file_get_contents(filename, &buffer, &size, &err)) {
        err_GET_FILE_CONTENTS(error, &err);
        return NULL;
    }
    if (size < HEADER_SIZE + 2) {
        err_TOO_SHORT(error);
        gwy_file_abandon_contents(buffer, size, NULL);
        return NULL;
    }

    dme_read_header(buffer, &dmefile);

    if (dmefile.header_size < HEADER_SIZE) {
        g_set_error(error, GWY_MODULE_FILE_ERROR, GWY_MODULE_FILE_ERROR_DATA,
                    _("Header is too short (only %d bytes)."),
                    dmefile.header_size);
        gwy_file_abandon_contents(buffer, size, NULL);
        return NULL;
    }

    if (err_SIZE_MISMATCH(error, dmefile.header_size + dmefile.data_size, size,
                          TRUE)) {
        gwy_file_abandon_contents(buffer, size, NULL);
        return NULL;
    }

    if (dmefile.data_size != 2*(dmefile.xres + 1)*dmefile.yres) {
        g_set_error(error, GWY_MODULE_FILE_ERROR, GWY_MODULE_FILE_ERROR_DATA,
                    _("Data size %u does not match data dimensions (%u×%u)."),
                    dmefile.data_size, dmefile.xres, dmefile.yres);
        gwy_file_abandon_contents(buffer, size, NULL);
        return NULL;
    }

    if (err_DIMENSION(error, dmefile.xres)
        || err_DIMENSION(error, dmefile.yres)) {
        gwy_file_abandon_contents(buffer, size, NULL);
        return NULL;
    }

    /* Use negated positive conditions to catch NaNs */
    if (!((dmefile.xreal = fabs(dmefile.x_calibration * dmefile.xreal)) > 0)) {
        g_warning("Real x size is 0.0, fixing to 1.0");
        dmefile.xreal = 1.0;
    }
    if (!((dmefile.yreal = fabs(dmefile.y_calibration * dmefile.yreal)) > 0)) {
        g_warning("Real y size is 0.0, fixing to 1.0");
        dmefile.yreal = 1.0;
    }

    dfield = gwy_data_field_new(dmefile.xres, dmefile.yres,
                                Angstrom*dmefile.xreal, Angstrom*dmefile.yreal,
                                FALSE);
    data = gwy_data_field_get_data(dfield);
    ls16 = (const gint16*)(buffer + dmefile.header_size)
           + dmefile.xres*dmefile.yres;
    for (i = 0; i < dmefile.yres; i++) {
        q = Angstrom * dmefile.height_scale_factor;
        q *= gwy_powi(2.0, ls16[i] & 0x0f);
        q *= dmefile.z_calibration;
        gwy_convert_raw_data(buffer + dmefile.header_size + 2*i*dmefile.xres,
                             dmefile.xres, 1,
                             GWY_RAW_DATA_SINT16, GWY_BYTE_ORDER_LITTLE_ENDIAN,
                             data + i*dmefile.xres, q, 0.0);
    }
    gwy_data_field_invert(dfield, FALSE, TRUE, FALSE);

    gwy_file_abandon_contents(buffer, size, NULL);

    siunit = gwy_si_unit_new("m");
    gwy_data_field_set_si_unit_xy(dfield, siunit);
    g_object_unref(siunit);

    siunit = gwy_si_unit_new("m");
    gwy_data_field_set_si_unit_z(dfield, siunit);
    g_object_unref(siunit);

    container = gwy_container_new();
    gwy_container_set_object_by_name(container, "/0/data", dfield);
    g_object_unref(dfield);

    if (dmefile.title[0])
        gwy_container_set_string_by_name(container, "/0/data/title",
                                         g_strdup(dmefile.title));
    else
        gwy_container_set_string_by_name(container, "/0/data/title",
                                         g_strdup("Topography"));

    gwy_file_channel_import_log_add(container, 0, NULL, filename);

    return container;
}

static void
dme_read_header(const guchar *p,
                DMEFile *dmefile)
{
    get_CHARARRAY(dmefile->program_name, &p);
    dmefile->file_version = gwy_get_guint16_le(&p);
    dmefile->program_version = gwy_get_guint16_le(&p);
    dmefile->header_size = gwy_get_guint16_le(&p);
    dmefile->data_size = gwy_get_guint32_le(&p);
    gwy_debug("header_size: %u data_size: %u",
              dmefile->header_size, dmefile->data_size);
    get_CHARARRAY(dmefile->time, &p);
    get_PASCAL_CHARARRAY0(dmefile->comment, &p);
    g_strstrip(dmefile->comment);
    dmefile->data_type = *(p++);
    gwy_debug("data_type: %u", dmefile->data_type);
    p += 123;  /* reserved */
    dmefile->xres = gwy_get_guint32_le(&p);
    dmefile->yres = gwy_get_guint32_le(&p);
    gwy_debug("xres: %u, yres: %u", dmefile->xres, dmefile->yres);
    dmefile->xreal = gwy_get_pascal_real_le(&p);
    dmefile->yreal = gwy_get_pascal_real_le(&p);
    gwy_debug("xreal: %g, yreal: %g", dmefile->xreal, dmefile->yreal);
    dmefile->xoff = gwy_get_pascal_real_le(&p);
    dmefile->yoff = gwy_get_pascal_real_le(&p);
    get_PASCAL_CHARARRAY0(dmefile->title, &p);
    g_strstrip(dmefile->title);
    dmefile->sample_pause = gwy_get_pascal_real_le(&p);
    dmefile->sample_speed = gwy_get_pascal_real_le(&p);
    gwy_debug("sample_pause: %g, sample_speed: %g",
              dmefile->sample_pause, dmefile->sample_speed);
    dmefile->tunnel_current = gwy_get_pascal_real_le(&p);
    dmefile->bias = gwy_get_pascal_real_le(&p);
    dmefile->loop_gain = gwy_get_pascal_real_le(&p);
    gwy_debug("tunnel_current: %g, bias: %g, loop_gain: %g",
              dmefile->tunnel_current, dmefile->bias, dmefile->loop_gain);
    dmefile->direction = gwy_get_guint16_le(&p);
    dmefile->head_type = *(p++);
    gwy_debug("direction: %u, head_type: %u",
              dmefile->direction, dmefile->head_type);
    p += 291;  /* reserved */
    dmefile->x_calibration = gwy_get_pascal_real_le(&p);
    dmefile->y_calibration = gwy_get_pascal_real_le(&p);
    dmefile->z_calibration = gwy_get_pascal_real_le(&p);
    p += 120;  /* reserved */
    dmefile->min = gwy_get_pascal_real_le(&p);
    dmefile->max = gwy_get_pascal_real_le(&p);
    dmefile->mean = gwy_get_pascal_real_le(&p);
    dmefile->full_scale = gwy_get_pascal_real_le(&p);
    dmefile->scale_offset = gwy_get_pascal_real_le(&p);
    dmefile->x_slope_corr = gwy_get_pascal_real_le(&p);
    dmefile->y_slope_corr = gwy_get_pascal_real_le(&p);
    dmefile->offset_corr = gwy_get_pascal_real_le(&p);
    dmefile->slope_calculated = gwy_get_gboolean8(&p);
    dmefile->roughness_valid = gwy_get_gboolean8(&p);
    dmefile->ra = gwy_get_pascal_real_le(&p);
    dmefile->rms = gwy_get_pascal_real_le(&p);
    dmefile->ry = gwy_get_pascal_real_le(&p);
    p += 2017;  /* reserved */
    dmefile->display_form_mode = *(p++);
    dmefile->display_rotated = gwy_get_guint16_le(&p);
    dmefile->display_angle_polar = gwy_get_guint16_le(&p);
    dmefile->display_angle_azimuthal = gwy_get_guint16_le(&p);
    dmefile->scale_fraction = gwy_get_pascal_real_le(&p);
    p += 334;  /* reserved */
    dmefile->slope_mode = *(p++);
    p += 38;  /* reserved */
    dmefile->height_scale_factor = gwy_get_pascal_real_le(&p);
    gwy_debug("height_scale_factor: %g", dmefile->height_scale_factor);
}

/* vim: set cin et ts=4 sw=4 cino=>1s,e0,n0,f0,{0,}0,^0,\:1s,=0,g1s,h0,t0,+1s,c3,(0,u0 : */