static gboolean surffile_save(GwyContainer *data, const gchar *filename, G_GNUC_UNUSED GwyRunType mode, GError **error) { FILE *fh; gboolean ok = TRUE; SurfWriter surf; gfloat uintmax = 1073741824.0; gdouble zmaxreal; gdouble zminreal; gdouble xreal; gdouble yreal; gchar *dxunittmp; gchar *dyunittmp; gchar *dzunittmp; GwySIUnit *xysi; GwySIUnit *zsi; GwyContainer *current_data; int power = 0; int k = 0; GwyDataField *dfield; const gdouble *points; gint32 *integer_values; strncpy(surf.signature, "DIGITAL SURF", 12); surf.format = 0; surf.nobjects = 1; surf.version = 1; surf.type = 1; strncpy(surf.object_name, "SCRATCH", 30); strncpy(surf.operator_name, "csm", 30); surf.material_code = 0; surf.acquisition = 0; surf.range = 1; surf.special_points = 0; surf.absolute = 1; strncpy(surf.reserved, " ", 8); surf.pointsize = 32; surf.zmin = 0; surf.zmax = 1073741824.0; strncpy(surf.xaxis, "X", 16); strncpy(surf.yaxis, "Y", 16); strncpy(surf.zaxis, "Z", 16); surf.xunit_ratio = 1; surf.yunit_ratio = 1; surf.zunit_ratio = 1; surf.imprint = 1; surf.inversion = 0; surf.leveling = 0; strncpy(surf.obsolete, " ", 12); surf.seconds = 0; surf.minutes = 0; surf.hours = 0; surf.day = 5; surf.month = 1; surf.year = 2001; surf.dayof = 0; surf.measurement_duration = 1.0; strncpy(surf.obsolete2, " ", 10); surf.comment_size = 0; surf.private_size = 0; strncpy(surf.client_zone," ", 128); surf.XOffset = 0.0; surf.YOffset = 0.0; surf.ZOffset = 0.0; strncpy(surf.reservedzone, " ", 34); if (!(fh = gwy_fopen( filename, "wb"))) { err_OPEN_WRITE(error); return FALSE; } gwy_app_data_browser_get_current(GWY_APP_CONTAINER, ¤t_data, 0); if (!current_data) { err_NO_CHANNEL_EXPORT(error); return FALSE; } if (data != current_data) { return FALSE; } gwy_app_data_browser_get_current(GWY_APP_DATA_FIELD, &dfield, 0); if (!dfield) { err_NO_CHANNEL_EXPORT(error); return FALSE; } /*header values*/ xysi = gwy_data_field_get_si_unit_xy(dfield); zsi = gwy_data_field_get_si_unit_z(dfield); dxunittmp = gwy_si_unit_get_string(xysi, GWY_SI_UNIT_FORMAT_PLAIN); dyunittmp = gwy_si_unit_get_string (xysi, GWY_SI_UNIT_FORMAT_PLAIN); dzunittmp = gwy_si_unit_get_string (zsi, GWY_SI_UNIT_FORMAT_PLAIN); strncpy(surf.dx_unit, dxunittmp, 16); strncpy(surf.dy_unit, dyunittmp, 16); strncpy(surf.dz_unit, dzunittmp, 16); g_free(dxunittmp); g_free(dyunittmp); g_free(dzunittmp); /*extrema*/ zmaxreal = gwy_data_field_get_max(dfield); zminreal = gwy_data_field_get_min(dfield); surf.xres = gwy_data_field_get_xres(dfield); surf.yres = gwy_data_field_get_yres(dfield); surf.nofpoints = surf.xres * surf.yres; xreal = gwy_data_field_get_xreal(dfield); yreal = gwy_data_field_get_yreal(dfield); /*units*/ power = 0; surf.dx = (gfloat)(xreal / surf.xres); get_unit(surf.dx_unit, &power, xreal); surf.dx *= pow10(power); strncpy(surf.xlength_unit, surf.dx_unit, 16); power = 0; surf.dy = (gfloat)(yreal / surf.yres); get_unit(surf.dy_unit, &power, yreal); surf.dy *= pow10(power); strncpy(surf.ylength_unit, surf.dy_unit, 16); power = 0; if (zmaxreal > zminreal) { get_unit(surf.dz_unit, &power, (zmaxreal - zminreal)); } strncpy(surf.zlength_unit, surf.dz_unit, 16); zmaxreal *= pow10(power); zminreal *= pow10(power); surf.dz = (zmaxreal - zminreal) / uintmax; surf.ZOffset = zminreal; /*convert data into integer32*/ integer_values = g_new(gint32, surf.nofpoints); points = gwy_data_field_get_data_const(dfield); if (zminreal != zmaxreal) { for (k = 0; k < surf.nofpoints; k++) { // * pow10( power ) to convert in the dz_unit integer_values[k] = floor(uintmax * (points[k] * pow10(power) - zminreal) / (zmaxreal - zminreal)); } } else { for (k = 0; k < surf.nofpoints; k++) { integer_values[k] = 0; } } /* byte order*/ surf.format = GINT16_TO_LE(surf.format); surf.nobjects = GUINT16_TO_LE(surf.nobjects); surf.version = GINT16_TO_LE(surf.version); surf.type = GINT16_TO_LE(surf.type); surf.material_code = GINT16_TO_LE(surf.material_code); surf.acquisition = GINT16_TO_LE(surf.acquisition); surf.range = GINT16_TO_LE(surf.range); surf.special_points = GINT16_TO_LE(surf.special_points); surf.absolute = GINT16_TO_LE(surf.absolute); surf.pointsize = GINT16_TO_LE(surf.pointsize); surf.zmin = GINT32_TO_LE(surf.zmin); surf.zmax = GINT32_TO_LE(surf.zmax); surf.xres = GINT32_TO_LE(surf.xres); surf.yres = GINT32_TO_LE(surf.yres); surf.nofpoints = GINT32_TO_LE(surf.nofpoints); surf.dx = GFLOAT_TO_LE(surf.dx); surf.dy = GFLOAT_TO_LE(surf.dy); surf.dz = GFLOAT_TO_LE(surf.dz); surf.xunit_ratio = GFLOAT_TO_LE(surf.xunit_ratio); surf.yunit_ratio = GFLOAT_TO_LE(surf.yunit_ratio); surf.zunit_ratio = GFLOAT_TO_LE(surf.zunit_ratio); surf.imprint = GINT16_TO_LE(surf.imprint); surf.inversion = GINT16_TO_LE(surf.inversion); surf.leveling = GINT16_TO_LE(surf.leveling); surf.seconds = GINT16_TO_LE(surf.seconds); surf.minutes = GINT16_TO_LE(surf.minutes); surf.hours = GINT16_TO_LE(surf.hours); surf.day = GINT16_TO_LE(surf.day); surf.month = GINT16_TO_LE(surf.month); surf.year = GINT16_TO_LE(surf.year); surf.dayof = GINT16_TO_LE(surf.dayof); surf.measurement_duration = GFLOAT_TO_LE(surf.measurement_duration); surf.comment_size = GINT16_TO_LE(surf.comment_size); surf.private_size = GINT16_TO_LE(surf.private_size); surf.XOffset = GFLOAT_TO_LE(surf.XOffset); surf.YOffset = GFLOAT_TO_LE(surf.YOffset); surf.ZOffset = GFLOAT_TO_LE(surf.ZOffset); for (k = 0; k < surf.nofpoints; k++) { integer_values[k] = GINT32_TO_LE(integer_values[k]); } //write // fwrite(&surf, sizeof( SurfWriter ), 1, fh) bad struct align if( fwrite(&surf.signature, sizeof(char), 12, fh) != 12 || fwrite(&surf.format, sizeof(gint16), 1, fh) != 1 || fwrite(&surf.nobjects, sizeof(guint16), 1, fh) != 1 || fwrite(&surf.version, sizeof(gint16), 1, fh) != 1 || fwrite(&surf.type, sizeof(gint16), 1, fh) != 1 || fwrite(&surf.object_name, sizeof(char), 30, fh) != 30 || fwrite(&surf.operator_name, sizeof(char), 30, fh) != 30 || fwrite(&surf.material_code, sizeof(gint16), 1, fh) != 1 || fwrite(&surf.acquisition, sizeof(gint16), 1, fh) != 1 || fwrite(&surf.range, sizeof(gint16), 1, fh) != 1 || fwrite(&surf.special_points, sizeof(gint16), 1, fh) != 1 || fwrite(&surf.absolute, sizeof(gint16), 1, fh) != 1 || fwrite(&surf.reserved, sizeof(char), 8, fh) != 8 || fwrite(&surf.pointsize, sizeof(gint16), 1, fh) != 1 || fwrite(&surf.zmin, sizeof(gint32), 1, fh) != 1 || fwrite(&surf.zmax, sizeof(gint32), 1, fh) != 1 || fwrite(&surf.xres, sizeof(gint32), 1, fh) != 1 || fwrite(&surf.yres, sizeof(gint32), 1, fh) != 1 || fwrite(&surf.nofpoints, sizeof(gint32), 1, fh) != 1 || fwrite(&surf.dx, sizeof(gfloat), 1, fh) != 1 || fwrite(&surf.dy, sizeof(gfloat), 1, fh) != 1 || fwrite(&surf.dz, sizeof(gfloat), 1, fh) != 1 || fwrite(&surf.xaxis, sizeof(char), 16, fh) != 16 || fwrite(&surf.yaxis, sizeof(char), 16, fh) != 16 || fwrite(&surf.zaxis, sizeof(char), 16, fh) != 16 || fwrite(&surf.dx_unit, sizeof(char), 16, fh) != 16 || fwrite(&surf.dy_unit, sizeof(char), 16, fh) != 16 || fwrite(&surf.dz_unit, sizeof(char), 16, fh) != 16 || fwrite(&surf.xlength_unit, sizeof(char), 16, fh) != 16 || fwrite(&surf.ylength_unit, sizeof(char), 16, fh) != 16 || fwrite(&surf.zlength_unit, sizeof(char), 16, fh) != 16 || fwrite(&surf.xunit_ratio, sizeof(gfloat), 1, fh) != 1 || fwrite(&surf.yunit_ratio, sizeof(gfloat), 1, fh) != 1 || fwrite(&surf.zunit_ratio, sizeof(gfloat), 1, fh) != 1 || fwrite(&surf.imprint, sizeof(gint16), 1, fh) != 1 || fwrite(&surf.inversion, sizeof(gint16), 1, fh) != 1 || fwrite(&surf.leveling, sizeof(gint16), 1, fh) != 1 || fwrite(&surf.obsolete, sizeof(char), 12, fh) != 12 || fwrite(&surf.seconds, sizeof(gint16), 1, fh) != 1 || fwrite(&surf.minutes, sizeof(gint16), 1, fh) != 1 || fwrite(&surf.hours, sizeof(gint16), 1, fh) != 1 || fwrite(&surf.day, sizeof(gint16), 1, fh) != 1 || fwrite(&surf.month, sizeof(gint16), 1, fh) != 1 || fwrite(&surf.year, sizeof(gint16), 1, fh) != 1 || fwrite(&surf.dayof, sizeof(gint16),1,fh) != 1 || fwrite(&surf.measurement_duration, sizeof(gfloat), 1, fh) != 1 || fwrite(&surf.obsolete2, sizeof(char), 10, fh) != 10 || fwrite(&surf.comment_size, sizeof(gint16), 1, fh) != 1 || fwrite(&surf.private_size, sizeof(gint16), 1, fh) != 1 || fwrite(&surf.client_zone, sizeof(char), 128, fh) != 128 || fwrite(&surf.XOffset, sizeof(gfloat), 1, fh) != 1 || fwrite(&surf.YOffset, sizeof(gfloat), 1, fh) != 1 || fwrite(&surf.ZOffset, sizeof(gfloat), 1, fh) != 1 || fwrite(&surf.reservedzone, sizeof(char), 34, fh)!= 34 || fwrite(integer_values, sizeof(gint32), surf.nofpoints, fh) != surf.nofpoints ) { err_WRITE(error); ok = FALSE; g_unlink(filename); } g_free(integer_values); fclose( fh ); return ok; }
static gboolean asc_export(GwyContainer *data, const gchar *filename, G_GNUC_UNUSED GwyRunType mode, GError **error) { GwyDataField *dfield; guint xres, i, n; gchar *header; const gdouble *d; gboolean zunit_is_nm; FILE *fh; gwy_app_data_browser_get_current(GWY_APP_DATA_FIELD, &dfield, 0); if (!dfield) { err_NO_CHANNEL_EXPORT(error); return FALSE; } if (!(fh = gwy_fopen(filename, "w"))) { err_OPEN_WRITE(error); return FALSE; } header = asc_format_header(data, dfield, &zunit_is_nm); if (fputs(header, fh) == EOF) goto fail; d = gwy_data_field_get_data_const(dfield); xres = gwy_data_field_get_xres(dfield); n = xres*gwy_data_field_get_yres(dfield); for (i = 0; i < n; i++) { gchar buf[G_ASCII_DTOSTR_BUF_SIZE]; gchar c; if (zunit_is_nm) g_ascii_dtostr(buf, G_ASCII_DTOSTR_BUF_SIZE, d[i]/Nanometer); else g_ascii_dtostr(buf, G_ASCII_DTOSTR_BUF_SIZE, d[i]); if (fputs(buf, fh) == EOF) goto fail; c = (i % xres == xres-1) ? '\n' : '\t'; if (fputc(c, fh) == EOF) goto fail; } fclose(fh); g_free(header); return TRUE; fail: err_WRITE(error); fclose(fh); g_free(header); g_unlink(filename); return FALSE; }
static gboolean aafm_export(G_GNUC_UNUSED GwyContainer *data, const gchar *filename, G_GNUC_UNUSED GwyRunType mode, GError **error) { union { guchar pp[4]; float f; } z; guint16 res, r; gint16 *x; gint16 v; gint i, j, xres, yres, n; GwyDataField *dfield; const gdouble *d; gdouble min, max, q, z0; FILE *fh; gboolean ok = TRUE; gwy_app_data_browser_get_current(GWY_APP_DATA_FIELD, &dfield, 0); if (!dfield) { err_NO_CHANNEL_EXPORT(error); return FALSE; } if (!(fh = gwy_fopen(filename, "wb"))) { err_OPEN_WRITE(error); return FALSE; } d = gwy_data_field_get_data_const(dfield); xres = gwy_data_field_get_xres(dfield); yres = gwy_data_field_get_yres(dfield); res = MIN(MIN(xres, yres), 32767); n = (gint)res*(gint)res; r = GUINT16_TO_LE(res); fwrite(&res, 1, sizeof(r), fh); gwy_data_field_get_min_max(dfield, &min, &max); if (min == max) { q = 0.0; z0 = 0.0; } else { q = 65533.0/(max - min); z0 = -32766.5*(max + min)/(max - min); } z.f = MIN(gwy_data_field_get_xreal(dfield), gwy_data_field_get_yreal(dfield))/Angstrom; #if (G_BYTE_ORDER == G_BIG_ENDIAN) GWY_SWAP(guchar, z.pp[0], z.pp[3]); GWY_SWAP(guchar, z.pp[1], z.pp[2]); #endif fwrite(&z, 1, sizeof(z), fh); x = g_new(gint16, n); for (i = 0; i < res; i++) { for (j = 0; j < res; j++) { v = GWY_ROUND(d[(res-1 - j)*res + i]*q + z0); x[i*res + j] = GINT16_TO_LE(v); } } if (!(ok = (fwrite(x, 1, 2*n, fh) == 2*n))) { err_WRITE(error); g_unlink(filename); } else { z.f = (max - min)/Angstrom; #if (G_BYTE_ORDER == G_BIG_ENDIAN) GWY_SWAP(guchar, z.pp[0], z.pp[3]); GWY_SWAP(guchar, z.pp[1], z.pp[2]); #endif fwrite(&z, 1, sizeof(z), fh); } fclose(fh); g_free(x); return ok; }
static gboolean gsf_export(GwyContainer *container, const gchar *filename, G_GNUC_UNUSED GwyRunType mode, GError **error) { static const gchar zeroes[4] = { 0, 0, 0, 0 }; GString *header = NULL; gfloat *dfl = NULL; guint i, xres, yres, padding; gint id; GwyDataField *dfield; const gdouble *d; gdouble v; gchar *s; GwySIUnit *unit, *emptyunit; FILE *fh; gwy_app_data_browser_get_current(GWY_APP_DATA_FIELD, &dfield, GWY_APP_DATA_FIELD_ID, &id, 0); if (!dfield) { err_NO_CHANNEL_EXPORT(error); return FALSE; } if (!(fh = g_fopen(filename, "wb"))) { err_OPEN_WRITE(error); return FALSE; } xres = gwy_data_field_get_xres(dfield); yres = gwy_data_field_get_yres(dfield); header = g_string_new(MAGIC); g_string_append_printf(header, "XRes = %u\n", xres); g_string_append_printf(header, "YRes = %u\n", yres); append_num(header, "XReal", gwy_data_field_get_xreal(dfield)); append_num(header, "YReal", gwy_data_field_get_yreal(dfield)); if ((v = gwy_data_field_get_xoffset(dfield))) append_num(header, "XOffset", v); if ((v = gwy_data_field_get_yoffset(dfield))) append_num(header, "YOffset", v); emptyunit = gwy_si_unit_new(NULL); unit = gwy_data_field_get_si_unit_xy(dfield); if (!gwy_si_unit_equal(unit, emptyunit)) { s = gwy_si_unit_get_string(unit, GWY_SI_UNIT_FORMAT_PLAIN); g_string_append_printf(header, "XYUnits = %s\n", s); g_free(s); } unit = gwy_data_field_get_si_unit_z(dfield); if (!gwy_si_unit_equal(unit, emptyunit)) { s = gwy_si_unit_get_string(unit, GWY_SI_UNIT_FORMAT_PLAIN); g_string_append_printf(header, "ZUnits = %s\n", s); g_free(s); } g_object_unref(emptyunit); s = gwy_app_get_data_field_title(container, id); g_string_append_printf(header, "Title = %s\n", s); g_free(s); if (fwrite(header->str, 1, header->len, fh) != header->len) { err_WRITE(error); goto fail; } padding = 4 - (header->len % 4); if (fwrite(zeroes, 1, padding, fh) != padding) { err_WRITE(error); goto fail; } g_string_free(header, TRUE); header = NULL; dfl = g_new(gfloat, xres*yres); d = gwy_data_field_get_data_const(dfield); for (i = 0; i < xres*yres; i++) { union { guchar pp[4]; float f; } z; z.f = d[i]; #if (G_BYTE_ORDER == G_BIG_ENDIAN) GWY_SWAP(guchar, z.pp[0], z.pp[3]); GWY_SWAP(guchar, z.pp[1], z.pp[2]); #endif dfl[i] = z.f; } if (fwrite(dfl, sizeof(gfloat), xres*yres, fh) != xres*yres) { err_WRITE(error); goto fail; } g_free(dfl); fclose(fh); return TRUE; fail: if (fh) fclose(fh); g_unlink(filename); if (header) g_string_free(header, TRUE); g_free(dfl); return FALSE; }
static gboolean sdfile_export_text(G_GNUC_UNUSED GwyContainer *data, const gchar *filename, G_GNUC_UNUSED GwyRunType mode, GError **error) { enum { SCALE = 65535 }; static const gchar header_format[] = "aBCR-0.0\n" "ManufacID = Gwyddion\n" "CreateDate = %02u%02u%04u%02u%02u\n" "ModDate = %02u%02u%04u%02u%02u\n" "NumPoints = %d\n" "NumProfiles = %d\n" "Xscale = %e\n" "Yscale = %e\n" "Zscale = %e\n" "Zresolution = -1\n" /* FIXME: Dunno */ "Compression = 0\n" "DataType = %d\n" "CheckType = 0\n" "NumDataSet = 1\n" "NanPresent = 0\n" "*\n"; GwyDataField *dfield; const gdouble *d; gint xres, yres, i; const struct tm *ttm; gchar buf[24]; time_t t; FILE *fh; gwy_app_data_browser_get_current(GWY_APP_DATA_FIELD, &dfield, 0); if (!dfield) { err_NO_CHANNEL_EXPORT(error); return FALSE; } if (!(fh = g_fopen(filename, "w"))) { err_OPEN_WRITE(error); return FALSE; } d = gwy_data_field_get_data_const(dfield); xres = gwy_data_field_get_xres(dfield); yres = gwy_data_field_get_yres(dfield); /* We do not keep date information, just use the current time */ time(&t); ttm = localtime(&t); fprintf(fh, header_format, ttm->tm_mday, ttm->tm_mon, ttm->tm_year, ttm->tm_hour, ttm->tm_min, ttm->tm_mday, ttm->tm_mon, ttm->tm_year, ttm->tm_hour, ttm->tm_min, xres, yres, gwy_data_field_get_xmeasure(dfield), gwy_data_field_get_ymeasure(dfield), 1.0, SDF_FLOAT); for (i = 0; i < xres*yres; i++) { g_ascii_formatd(buf, sizeof(buf), "%g", d[i]); fputs(buf, fh); fputc('\n', fh); } fclose(fh); return TRUE; }