File: princetonspe.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 (460 lines) | stat: -rw-r--r-- 14,575 bytes parent folder | download | duplicates (3)
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
/*
 *  $Id: princetonspe.c 19948 2017-06-22 14:05:41Z yeti-dn $
 *  Copyright (C) 2015 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.
 */

/**
 * [FILE-MAGIC-USERGUIDE]
 * Princeton Instruments camera SPE
 * .spe
 * Read
 **/

#include "config.h"
#include <string.h>
#include <stdlib.h>
#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 TAIL_MAGIC "</SpeFormat>"
#define TAIL_MAGIC_SIZE (sizeof(TAIL_MAGIC)-1)
#define BLOODY_UTF8_BOM "\xef\xbb\xbf"
#define EXTENSION ".spe"

#define STRIDE_KEY "/SpeFormat/DataFormat/DataBlock::stride"

/* The only fields where anything at all seems to be present in newer files. */
enum {
    XRES_CCD_OFFSET  = 0x06,
    YRES_CCD_OFFSET  = 0x12,
    NOSCAN_OFFSET    = 0x22,
    XRES_OFFSET      = 0x2a,
    DATA_TYPE_OFFSET = 0x6c,
    YRES_OFFSET      = 0x290,
    SCRAMBLE_OFFSET  = 0x292,
    LNOSCAN_OFFSET   = 0x298,
    FOOTER_OFFSET    = 0x2a6,
    NUMFRAMES_OFFSET = 0x5a6,
    VERSION_OFFSET   = 0x7c8,
    HEADER_SIZE      = 0x1004,
};

typedef enum {
    PSPE_DATA_FLOAT = 0,
    PSPE_DATA_LONG = 1,
    PSPE_DATA_SHORT = 2,
    PSPE_DATA_USHORT = 3,
    PSPE_DATA_NTYPES
} PSPEDataType;

typedef struct {
    gsize size;
    guchar *buffer;
    guint xres_ccd;
    guint yres_ccd;
    guint xres;
    guint yres;
    PSPEDataType data_type;
    guint scramble;
    guint num_frames;
    guint noscan;
    guint lnoscan;
    guint footer_offset;
    gdouble version;
    /* Derived data. */
    guint stride;
    GwyRawDataType rawtype;
    GString *str;
    GString *path;
    GHashTable *hash;
} PSPEFile;

static gboolean      module_register (void);
static gint          pspe_detect     (const GwyFileDetectInfo *fileinfo,
                                      gboolean only_name);
static GwyContainer* pspe_load       (const gchar *filename,
                                      GwyRunType mode,
                                      GError **error);
static gboolean      pspe_read_header(PSPEFile *pspefile,
                                      const guchar *buffer);
static gboolean      pspe_check_size (PSPEFile *pspefile,
                                      GError **error);
static void          parse_xml_footer(PSPEFile *pspefile);

static GwyModuleInfo module_info = {
    GWY_MODULE_ABI_VERSION,
    &module_register,
    N_("Princeton Instruments camera SPE files."),
    "Yeti <yeti@gwyddion.net>",
    "0.1",
    "David Nečas (Yeti)",
    "2015",
};

GWY_MODULE_QUERY2(module_info, princetonspe)

static gboolean
module_register(void)
{
    gwy_file_func_register("princetonspe",
                           N_("Princeton Instruments SPE files"),
                           (GwyFileDetectFunc)&pspe_detect,
                           (GwyFileLoadFunc)&pspe_load,
                           NULL,
                           NULL);

    return TRUE;
}

static gint
pspe_detect(const GwyFileDetectInfo *fileinfo,
            gboolean only_name)
{
    PSPEFile pspefile;
    gint score = 0;

    if (only_name)
        return g_str_has_suffix(fileinfo->name_lowercase, EXTENSION) ? 10 : 0;

    gwy_clear(&pspefile, 1);
    pspefile.size = fileinfo->file_size;
    if (fileinfo->file_size > HEADER_SIZE
        && fileinfo->buffer_len >= NUMFRAMES_OFFSET + sizeof(guint32)
        && pspe_read_header(&pspefile, fileinfo->head)
        && pspe_check_size(&pspefile, NULL)) {
        score = 90;
        /* New files (3.0) have some XML at the end.  Check that for surer
         * identification. */
        if (gwy_memmem(fileinfo->tail, fileinfo->buffer_len,
                       TAIL_MAGIC, TAIL_MAGIC_SIZE))
            score = 100;
    }

    return score;
}

static GwyContainer*
pspe_load(const gchar *filename,
          G_GNUC_UNUSED GwyRunType mode,
          GError **error)
{
    PSPEFile pspefile;
    GwyContainer *container = NULL;
    GError *err = NULL;
    GwyDataField *dfield = NULL;
    gchar *value;
    GQuark quark;
    guint i, typesize, imagelen, len, nframes, data_size;

    gwy_clear(&pspefile, 1);

    if (!gwy_file_get_contents(filename, &pspefile.buffer, &pspefile.size,
                               &err)) {
        err_GET_FILE_CONTENTS(error, &err);
        return NULL;
    }
    if (pspefile.size < HEADER_SIZE) {
        err_TOO_SHORT(error);
        goto fail;
    }
    if (!pspe_read_header(&pspefile, pspefile.buffer)) {
        err_FILE_TYPE(error, "Princeton Instruments SPE");
        goto fail;
    }
    gwy_debug("ccd %u x %u", pspefile.xres_ccd, pspefile.yres_ccd);
    gwy_debug("res %u x %u", pspefile.xres, pspefile.yres);
    gwy_debug("num frames %u", pspefile.num_frames);
    gwy_debug("data type %u", pspefile.data_type);
    gwy_debug("version %g", pspefile.version);
    gwy_debug("footer offset %u", pspefile.footer_offset);

    if (!pspe_check_size(&pspefile, error))
        goto fail;

    typesize = gwy_raw_data_size(pspefile.rawtype);
    imagelen = typesize * pspefile.xres * pspefile.yres;
    nframes = pspefile.num_frames;

    parse_xml_footer(&pspefile);
    if (pspefile.hash
        && (value = g_hash_table_lookup(pspefile.hash, STRIDE_KEY))) {
        /* Not sure what is stored between images.  I have seen 8 bytes with
         * one double precision number there. */
        data_size = pspefile.footer_offset - HEADER_SIZE;
        len = atoi(value);
        if (len < imagelen) {
            err_INVALID(error, "DataBlock::stride");
            goto fail;
        }
        if (!len || data_size/len != nframes) {
            err_SIZE_MISMATCH(error, data_size, len*nframes, TRUE);
            goto fail;
        }
        imagelen = len;
    }

    container = gwy_container_new();
    for (i = 0; i < nframes; i++) {
        dfield = gwy_data_field_new(pspefile.xres, pspefile.yres,
                                    pspefile.xres, pspefile.yres,
                                    FALSE);
        gwy_convert_raw_data(pspefile.buffer + HEADER_SIZE + imagelen*i,
                             pspefile.xres * pspefile.yres, 1,
                             pspefile.rawtype, GWY_BYTE_ORDER_LITTLE_ENDIAN,
                             gwy_data_field_get_data(dfield), 1.0, 0.0);
        quark = gwy_app_get_data_key_for_id(i);
        gwy_container_set_object(container, quark, dfield);
        g_object_unref(dfield);

        gwy_app_set_data_field_title(container, i, "Intensity");
        gwy_file_channel_import_log_add(container, i, NULL, filename);
    }

fail:
    gwy_file_abandon_contents(pspefile.buffer, pspefile.size, NULL);
    if (pspefile.hash)
        g_hash_table_destroy(pspefile.hash);
    if (pspefile.str)
        g_string_free(pspefile.str, TRUE);

    return container;
}

static gboolean
pspe_read_header(PSPEFile *pspefile, const guchar *buffer)
{
    const guchar *p;

    p = buffer + XRES_CCD_OFFSET;
    pspefile->xres_ccd = gwy_get_guint16_le(&p);
    p = buffer + YRES_CCD_OFFSET;
    pspefile->yres_ccd = gwy_get_guint16_le(&p);

    p = buffer + XRES_OFFSET;
    pspefile->xres = gwy_get_guint16_le(&p);
    p = buffer + YRES_OFFSET;
    pspefile->yres = gwy_get_guint16_le(&p);

    p = buffer + DATA_TYPE_OFFSET;
    pspefile->data_type = gwy_get_guint16_le(&p);
    p = buffer + SCRAMBLE_OFFSET;
    pspefile->scramble = gwy_get_guint16_le(&p);
    p = buffer + NUMFRAMES_OFFSET;
    pspefile->num_frames = gwy_get_guint32_le(&p);

    p = buffer + NOSCAN_OFFSET;
    pspefile->noscan = gwy_get_guint16_le(&p);
    p = buffer + LNOSCAN_OFFSET;
    pspefile->lnoscan = gwy_get_guint32_le(&p);

    p = buffer + VERSION_OFFSET;
    pspefile->version = gwy_get_gfloat_le(&p);
    if (pspefile->version >= 3.0) {
        p = buffer + FOOTER_OFFSET;
        pspefile->footer_offset = gwy_get_guint32_le(&p);
    }

    /* The noscan and lnoscan fields must be filled with one-bits, apparently.
     * The rest is more difficult. */
    return (pspefile->noscan == 0xffff
            && pspefile->lnoscan == 0xffffffff
            && pspefile->scramble == 1
            && pspefile->data_type < PSPE_DATA_NTYPES);
}

static gboolean
pspe_check_size(PSPEFile *pspefile, GError **error)
{
    guint typesize, xres, yres, nframes, size = pspefile->size;

    if (err_DIMENSION(error, pspefile->xres))
        return FALSE;
    if (err_DIMENSION(error, pspefile->yres))
        return FALSE;
    if (!pspefile->num_frames) {
        err_NO_DATA(error);
        return FALSE;
    }

    if (pspefile->data_type == PSPE_DATA_FLOAT)
        pspefile->rawtype = GWY_RAW_DATA_FLOAT;
    else if (pspefile->data_type == PSPE_DATA_LONG)
        pspefile->rawtype = GWY_RAW_DATA_SINT32;
    else if (pspefile->data_type == PSPE_DATA_SHORT)
        pspefile->rawtype = GWY_RAW_DATA_SINT16;
    else if (pspefile->data_type == PSPE_DATA_USHORT)
        pspefile->rawtype = GWY_RAW_DATA_UINT16;
    else {
        err_DATA_TYPE(error, pspefile->data_type);
        return FALSE;
    }

    typesize = gwy_raw_data_size(pspefile->rawtype);
    xres = pspefile->xres;
    yres = pspefile->yres;
    nframes = pspefile->num_frames;
    /* Check the size safely with respect to integer overflows.  But do not
     * bother for the error message: the thing may need 128bit numbers to
     * format properly.  XXX: We may return FALSE with no error set with
     * unlucky numbers. */
    if ((size - HEADER_SIZE)/xres/yres/typesize < nframes) {
        err_SIZE_MISMATCH(error,
                          size - HEADER_SIZE, xres*yres*typesize*nframes,
                          TRUE);
        return FALSE;
    }

    if (pspefile->footer_offset < HEADER_SIZE
        || pspefile->footer_offset < HEADER_SIZE + xres*yres*typesize*nframes) {
        g_set_error(error, GWY_MODULE_FILE_ERROR, GWY_MODULE_FILE_ERROR_DATA,
                    _("XML footer overlaps with data."));
        return FALSE;
    }

    if (pspefile->footer_offset > pspefile->size) {
        g_set_error(error, GWY_MODULE_FILE_ERROR, GWY_MODULE_FILE_ERROR_DATA,
                    _("File is truncated."));
        return FALSE;
    }
    return TRUE;
}

static void
pspe_start_element(G_GNUC_UNUSED GMarkupParseContext *context,
                   const gchar *element_name,
                   const gchar **attribute_names,
                   const gchar **attribute_values,
                   gpointer user_data,
                   G_GNUC_UNUSED GError **error)
{
    PSPEFile *pspefile = (PSPEFile*)user_data;
    GString *str = pspefile->str;
    gchar *path;
    guint i, len;

    g_string_append_c(pspefile->path, '/');
    g_string_append(pspefile->path, element_name);
    path = pspefile->path->str;

    g_string_assign(str, path);
    g_string_append(str, "::");
    len = str->len;
    for (i = 0; attribute_names[i]; i++) {
        if (!strlen(attribute_names[i]) || !strlen(attribute_values[i]))
            continue;
        g_string_append(str, attribute_names[i]);
        gwy_debug("%s <%s>", str->str, attribute_values[i]);
        g_hash_table_insert(pspefile->hash,
                            g_strdup(str->str), g_strdup(attribute_values[i]));
        g_string_truncate(str, len);
    }
}

static void
pspe_end_element(G_GNUC_UNUSED GMarkupParseContext *context,
                 const gchar *element_name,
                 gpointer user_data,
                 G_GNUC_UNUSED GError **error)
{
    PSPEFile *pspefile = (PSPEFile*)user_data;
    guint n, len = pspefile->path->len;
    gchar *path = pspefile->path->str;

    n = strlen(element_name);
    g_return_if_fail(g_str_has_suffix(path, element_name));
    g_return_if_fail(len > n);
    g_return_if_fail(path[len-1 - n] == '/');
    g_string_set_size(pspefile->path, len-1 - n);
}

static void
pspe_text(G_GNUC_UNUSED GMarkupParseContext *context,
          const gchar *text,
          G_GNUC_UNUSED gsize text_len,
          gpointer user_data,
          G_GNUC_UNUSED GError **error)
{
    PSPEFile *pspefile = (PSPEFile*)user_data;
    gchar *path = pspefile->path->str;
    GString *str = pspefile->str;

    if (!strlen(text))
        return;

    g_string_assign(str, text);
    g_strstrip(str->str);
    if (!strlen(str->str))
        return;

    gwy_debug("%s <%s>", path, str->str);
    g_hash_table_insert(pspefile->hash, g_strdup(path), g_strdup(str->str));
}

static void
parse_xml_footer(PSPEFile *pspefile)
{
    GMarkupParser parser = {
        &pspe_start_element,
        &pspe_end_element,
        &pspe_text,
        NULL,
        NULL,
    };
    GMarkupParseContext *context = NULL;
    gchar *xmldata, *s;
    guint xmlsize = pspefile->size - pspefile->footer_offset;

    if (!xmlsize)
        return;

    xmldata = g_new(gchar, xmlsize + 1);
    memcpy(xmldata, pspefile->buffer + pspefile->footer_offset, xmlsize);
    xmldata[xmlsize] = '\0';

    gwy_strkill(xmldata, "\r");
    s = xmldata;
    /* Not seen in the wild but the XML people tend to use BOM in UTF-8... */
    if (g_str_has_prefix(s, BLOODY_UTF8_BOM))
        s += strlen(BLOODY_UTF8_BOM);

    pspefile->hash = g_hash_table_new_full(g_str_hash, g_str_equal,
                                           g_free, g_free);
    pspefile->path = g_string_new(NULL);
    pspefile->str = g_string_new(NULL);
    context = g_markup_parse_context_new(&parser, 0, pspefile, NULL);
    if (!g_markup_parse_context_parse(context, s, -1, NULL)
        || !g_markup_parse_context_end_parse(context, NULL)) {
        g_hash_table_destroy(pspefile->hash);
        pspefile->hash = NULL;
    }

    g_string_free(pspefile->path, TRUE);
    if (context)
        g_markup_parse_context_free(context);
    g_free(xmldata);
}

/* 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 : */