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
|
/*
* $Id: csmfile.c 20674 2017-12-18 17:58:47Z yeti-dn $
* Copyright (C) 2011 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.
*/
/* XXX: The file looks like Windows bitmap until you look beyond the nominal
* file end. And the type of data inside is not what the BMP header says at
* all. And the dimensions in the added footer are wrong. => Epic FAIL. */
/**
* [FILE-MAGIC-FREEDESKTOP]
* <mime-type type="application/x-benyuan-csm-spm">
* <comment>Benyuan CSM data</comment>
* <glob pattern="*.csm"/>
* <glob pattern="*.CSM"/>
* </mime-type>
**/
/**
* [FILE-MAGIC-USERGUIDE]
* Benyuan CSM
* .csm
* Read
**/
#include "config.h"
#include <string.h>
#include <stdlib.h>
#include <libgwyddion/gwymacros.h>
#include <libgwyddion/gwymath.h>
#include <libgwyddion/gwyutils.h>
#include <libgwymodule/gwymodule-file.h>
#include <libprocess/datafield.h>
#include <app/gwymoduleutils-file.h>
#include "err.h"
#include "get.h"
#define Nanometre 1e-9
#define MAGIC "Version = CSPM"
#define MAGIC_SIZE (sizeof(MAGIC) - 1)
#define EXTENSION ".csm"
enum {
BMP_HEADER_SIZE = 54
};
static gboolean module_register(void);
static gint csmfile_detect (const GwyFileDetectInfo *fileinfo,
gboolean only_name);
static GwyContainer* csmfile_load (const gchar *filename,
GwyRunType mode,
GError **error);
static gboolean read_bmp_header(const guchar *p,
guint *xres,
guint *yres,
guint *size);
static void store_meta (gpointer key,
gpointer value,
gpointer user_data);
static GwyModuleInfo module_info = {
GWY_MODULE_ABI_VERSION,
&module_register,
N_("Imports Benyuan CSM data files."),
"Yeti <yeti@gwyddion.net>",
"0.3",
"David Nečas (Yeti)",
"2011",
};
GWY_MODULE_QUERY2(module_info, csmfile)
static gboolean
module_register(void)
{
gwy_file_func_register("csmfile",
N_("Benyuan CSM files (.csm)"),
(GwyFileDetectFunc)&csmfile_detect,
(GwyFileLoadFunc)&csmfile_load,
NULL,
NULL);
return TRUE;
}
static gint
csmfile_detect(const GwyFileDetectInfo *fileinfo,
gboolean only_name)
{
guint size, xres, yres;
if (only_name)
return g_str_has_suffix(fileinfo->name_lowercase, EXTENSION) ? 20 : 0;
// Weed out files that do not pretend to be Windows bitmaps quickly.
if (fileinfo->buffer_len < BMP_HEADER_SIZE
|| !read_bmp_header(fileinfo->head, &size, &xres, &yres))
return 0;
// Weed out incorrect Windows bitmaps but also those no extra data beyond
// the nominal end.
if (fileinfo->file_size <= size)
return 0;
// Look for "Version = CSPM" somewhere near the end.
if (!gwy_memmem(fileinfo->tail, fileinfo->buffer_len,
MAGIC, MAGIC_SIZE))
return 0;
return 90;
}
static GwyContainer*
csmfile_load(const gchar *filename,
G_GNUC_UNUSED GwyRunType mode,
GError **error)
{
GwyContainer *meta, *container = NULL;
GHashTable *hash = NULL;
guchar *d24, *buffer = NULL;
gsize size = 0;
GError *err = NULL;
GwyDataField *dfield = NULL;
guint xres, yres, bmpsize, header_size, maxval, i, j;
gdouble real, zmin, zmax, q, z0;
GwyTextHeaderParser parser;
GwySIUnit *unit = NULL;
gchar *value, *end, *header = NULL;
gdouble *data;
gint power10;
if (!gwy_file_get_contents(filename, &buffer, &size, &err)) {
err_GET_FILE_CONTENTS(error, &err);
return NULL;
}
if (size < BMP_HEADER_SIZE) {
err_TOO_SHORT(error);
goto fail;
}
if (!read_bmp_header(buffer, &xres, &yres, &bmpsize)
|| size <= bmpsize) {
err_FILE_TYPE(error, "CSM");
goto fail;
}
header_size = size - bmpsize;
header = g_new(gchar, header_size + 1);
memcpy(header, buffer + bmpsize, header_size);
header[header_size] = '\0';
gwy_clear(&parser, 1);
parser.key_value_separator = "=";
hash = gwy_text_header_parse(header, &parser, NULL, NULL);
/* Do NOT use the fields Image width, Image height from the added footer.
* Even though it is specifically added by Benyuan it can disagree with the
* BMP diemsions and when they disagree the BMP diemsions are apparently
* right. */
if (err_DIMENSION(error, xres))
goto fail;
if (err_DIMENSION(error, yres))
goto fail;
if (!(value = g_hash_table_lookup(hash, "ScanSize"))) {
err_MISSING_FIELD(error, "ScanSize");
goto fail;
}
real = g_ascii_strtod(value, NULL);
/* Use negated positive conditions to catch NaNs */
if (!((real = fabs(real)) > 0)) {
g_warning("Real size is 0.0, fixing to 1.0");
real = 1.0;
}
if (!(value = g_hash_table_lookup(hash, "HeightScale"))) {
err_MISSING_FIELD(error, "HeightScale");
goto fail;
}
zmax = g_ascii_strtod(value, &end);
unit = gwy_si_unit_new_parse(end, &power10);
/* Optional stuff for which we try to fall back. */
if (!(value = g_hash_table_lookup(hash, "StartHeightScale")))
zmin = 0.0;
else
zmin = g_ascii_strtod(value, NULL);
if (!(value = g_hash_table_lookup(hash, "MaxValue")))
maxval = 0xffff;
else
maxval = MAX(atoi(value), 1);
dfield = gwy_data_field_new(xres, yres, real*Nanometre, real*Nanometre,
FALSE);
data = gwy_data_field_get_data(dfield);
d24 = buffer + BMP_HEADER_SIZE;
q = pow10(power10)*(zmax - zmin)/maxval;
z0 = pow10(power10)*zmin;
for (i = 0; i < yres; i++) {
gdouble *row = data + (yres-1 - i)*xres;
for (j = 0; j < xres; j++, row++, d24 += 3)
*row = (d24[0] + 256.0*d24[1])*q + z0;
}
gwy_si_unit_set_from_string(gwy_data_field_get_si_unit_xy(dfield), "m");
gwy_data_field_set_si_unit_z(dfield, unit);
container = gwy_container_new();
gwy_container_set_object_by_name(container, "/0/data", dfield);
g_object_unref(dfield);
meta = gwy_container_new();
g_hash_table_foreach(hash, store_meta, meta);
gwy_container_set_object_by_name(container, "/0/meta", meta);
g_object_unref(meta);
if ((value = g_hash_table_lookup(hash, "sTitle"))
&& g_utf8_validate(value, -1, NULL)) {
gwy_container_set_string_by_name(container, "/0/data/title",
g_strdup(value));
}
else
gwy_app_channel_title_fall_back(container, 0);
gwy_file_channel_import_log_add(container, 0, NULL, filename);
fail:
gwy_file_abandon_contents(buffer, size, NULL);
GWY_OBJECT_UNREF(unit);
if (header)
g_free(header);
if (hash)
g_hash_table_destroy(hash);
return container;
}
/* XXX: Code identical to nxiifile.c */
static gboolean
read_bmp_header(const guchar *p,
guint *xres,
guint *yres,
guint *size)
{
guint x;
if (p[0] != 'B' || p[1] != 'M')
return FALSE;
p += 2;
if ((*size = gwy_get_guint32_le(&p)) < BMP_HEADER_SIZE) /* Size */
return FALSE;
if ((x = gwy_get_guint32_le(&p)) != 0) /* Reserved */
return FALSE;
if ((x = gwy_get_guint32_le(&p)) != 54) /* Offset */
return FALSE;
if ((x = gwy_get_guint32_le(&p)) != 40) /* Header size */
return FALSE;
if ((*xres = gwy_get_guint32_le(&p)) == 0) /* Width */
return FALSE;
if ((*yres = gwy_get_guint32_le(&p)) == 0) /* Height */
return FALSE;
if ((x = gwy_get_guint16_le(&p)) != 1) /* Bit planes */
return FALSE;
if ((x = gwy_get_guint16_le(&p)) != 24) /* BPP */
return FALSE;
if ((x = gwy_get_guint32_le(&p)) != 0) /* Compression */
return FALSE;
if ((x = gwy_get_guint32_le(&p))
&& x + BMP_HEADER_SIZE != *size) /* Compresed size, may be 0 */
return FALSE;
if (3*(*xres)*(*yres) + BMP_HEADER_SIZE != *size)
return FALSE;
return TRUE;
}
static void
store_meta(gpointer key,
gpointer value,
gpointer user_data)
{
GwyContainer *meta = (GwyContainer*)user_data;
if (g_utf8_validate(value, -1, NULL)) {
gwy_container_set_string_by_name(meta, key, g_strdup(value));
}
else {
// FIXME: Is this Windows-locale dependent?
gchar *s = g_convert(value, -1, "UTF-8", "GB2312", NULL, NULL, NULL);
if (s)
gwy_container_set_string_by_name(meta, key, s);
}
}
/* 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 : */
|