static void gwy_data_field_mark_facets(GwyDataField *dtheta, GwyDataField *dphi, gdouble theta0, gdouble phi0, gdouble tolerance, GwyDataField *mask) { gdouble cr, cth0, sth0, cro; const gdouble *xd, *yd; gdouble *md; gint i; cr = cos(tolerance); cth0 = cos(theta0); sth0 = sin(theta0); xd = gwy_data_field_get_data_const(dtheta); yd = gwy_data_field_get_data_const(dphi); md = gwy_data_field_get_data(mask); for (i = gwy_data_field_get_xres(dtheta)*gwy_data_field_get_yres(dtheta); i; i--, xd++, yd++, md++) { cro = cth0*cos(*xd) + sth0*sin(*xd)*cos(*yd - phi0); *md = (cro >= cr); } }
/* Note this is not a correlation score since we care also about absolute * differences and try to suppress the influence of outliers. */ static gdouble get_row_difference(GwyDataField *dfield1, gint col1, gint row1, GwyDataField *dfield2, gint col2, gint row2, gint width, gint height) { gint xres1, yres1, xres2, yres2, i, j; const gdouble *data1, *data2; gdouble *row; gdouble s = 0.0; g_return_val_if_fail(width > 0, G_MAXDOUBLE); g_return_val_if_fail(height > 0, G_MAXDOUBLE); xres1 = gwy_data_field_get_xres(dfield1); yres1 = gwy_data_field_get_yres(dfield1); xres2 = gwy_data_field_get_xres(dfield2); yres2 = gwy_data_field_get_yres(dfield2); data1 = gwy_data_field_get_data_const(dfield1); data2 = gwy_data_field_get_data_const(dfield2); g_return_val_if_fail(col1 + width <= xres1, G_MAXDOUBLE); g_return_val_if_fail(col2 + width <= xres2, G_MAXDOUBLE); g_return_val_if_fail(row1 + height <= yres1, G_MAXDOUBLE); g_return_val_if_fail(row2 + height <= yres2, G_MAXDOUBLE); row = g_new(gdouble, width); for (i = 0; i < height; i++) { const gdouble *d1 = data1 + (row1 + i)*xres1 + col1; const gdouble *d2 = data2 + (row2 + i)*xres2 + col2; gdouble d; for (j = 0; j < width; j++) { d = d1[j] - d2[j]; row[j] = d; } d = gwy_math_median(width, row); s += d*d; } g_free(row); return sqrt(s/height); }
static gdouble get_column_difference(GwyDataField *dfield1, gint col1, gint row1, GwyDataField *dfield2, gint col2, gint row2, gint width, gint height) { gint xres1, yres1, xres2, yres2, i, j; const gdouble *data1, *data2; gdouble *column; gdouble s = 0.0; g_return_val_if_fail(width > 0, G_MAXDOUBLE); g_return_val_if_fail(height > 0, G_MAXDOUBLE); xres1 = gwy_data_field_get_xres(dfield1); yres1 = gwy_data_field_get_yres(dfield1); xres2 = gwy_data_field_get_xres(dfield2); yres2 = gwy_data_field_get_yres(dfield2); data1 = gwy_data_field_get_data_const(dfield1); data2 = gwy_data_field_get_data_const(dfield2); g_return_val_if_fail(col1 + width <= xres1, G_MAXDOUBLE); g_return_val_if_fail(col2 + width <= xres2, G_MAXDOUBLE); g_return_val_if_fail(row1 + height <= yres1, G_MAXDOUBLE); g_return_val_if_fail(row2 + height <= yres2, G_MAXDOUBLE); column = g_new(gdouble, height); for (j = 0; j < width; j++) { const gdouble *d1 = data1 + row1*xres1 + col1 + j; const gdouble *d2 = data2 + row2*xres2 + col2 + j; gdouble d; for (i = 0; i < height; i++) { d = d1[i*xres1] - d2[i*xres2]; column[i] = d; } d = gwy_math_median(height, column); s += d*d; } g_free(column); return sqrt(s/height); }
static void plot_correlated(GwyDataField *retfield, gint xsize, gint ysize, gdouble threshold) { GwyDataField *tmp; gint xres, yres, i, j, col, row, w, h; const gdouble *data; tmp = gwy_data_field_duplicate(retfield); gwy_data_field_clear(retfield); xres = gwy_data_field_get_xres(retfield); yres = gwy_data_field_get_yres(retfield); data = gwy_data_field_get_data_const(tmp); /* FIXME: this is very inefficient */ for (i = 0; i < yres; i++) { row = MAX(i - ysize/2, 0); h = MIN(i + ysize - ysize/2, yres) - row; for (j = 0; j < xres; j++) { if (data[i*xres + j] > threshold) { col = MAX(j - xsize/2, 0); w = MIN(j + xsize - xsize/2, xres) - col; gwy_data_field_area_fill(retfield, col, row, w, h, 1.0); } } } g_object_unref(tmp); }
static void flip_xy(GwyDataField *source, GwyDataField *dest, gboolean minor) { gint xres, yres, i, j; gdouble *dd; const gdouble *sd; xres = gwy_data_field_get_xres(source); yres = gwy_data_field_get_yres(source); gwy_data_field_resample(dest, yres, xres, GWY_INTERPOLATION_NONE); sd = gwy_data_field_get_data_const(source); dd = gwy_data_field_get_data(dest); if (minor) { for (i = 0; i < xres; i++) { for (j = 0; j < yres; j++) { dd[i*yres + j] = sd[j*xres + (xres - 1 - i)]; } } } else { for (i = 0; i < xres; i++) { for (j = 0; j < yres; j++) { dd[i*yres + (yres - 1 - j)] = sd[j*xres + i]; } } } gwy_data_field_set_xreal(dest, gwy_data_field_get_yreal(source)); gwy_data_field_set_yreal(dest, gwy_data_field_get_xreal(source)); }
static void line_correct_step_iter(GwyDataField *dfield, GwyDataField *mask) { const gdouble threshold = 3.0; gint xres, yres, i, j, len; gdouble u, v, w; const gdouble *d, *drow; gdouble *m, *mrow; yres = gwy_data_field_get_yres(dfield); xres = gwy_data_field_get_xres(dfield); d = gwy_data_field_get_data_const(dfield); m = gwy_data_field_get_data(mask); w = 0.0; for (i = 0; i < yres-1; i++) { drow = d + i*xres; for (j = 0; j < xres; j++) { v = drow[j + xres] - drow[j]; w += v*v; } } w = w/(yres-1)/xres; for (i = 0; i < yres-2; i++) { drow = d + i*xres; mrow = m + (i + 1)*xres; for (j = 0; j < xres; j++) { u = drow[xres + j]; v = (u - drow[j])*(u - drow[j + 2*xres]); if (G_UNLIKELY(v > threshold*w)) { if (2*u - drow[j] - drow[j + 2*xres] > 0) mrow[j] = 1.0; else mrow[j] = -1.0; } } len = 1; for (j = 1; j < xres; j++) { if (mrow[j] == mrow[j-1]) len++; else { if (mrow[j-1]) calcualte_segment_correction(drow + j-len, mrow + j-len, xres, len); len = 1; } } if (mrow[j-1]) { calcualte_segment_correction(drow + j-len, mrow + j-len, xres, len); } } gwy_data_field_sum_fields(dfield, dfield, mask); }
static void compute_slopes(GwyDataField *dfield, gint kernel_size, GwyDataField *xder, GwyDataField *yder) { gint xres, yres; xres = gwy_data_field_get_xres(dfield); yres = gwy_data_field_get_yres(dfield); if (kernel_size > 1) { GwyPlaneFitQuantity quantites[] = { GWY_PLANE_FIT_BX, GWY_PLANE_FIT_BY }; GwyDataField *fields[2]; fields[0] = xder; fields[1] = yder; gwy_data_field_fit_local_planes(dfield, kernel_size, 2, quantites, fields); } else { gint col, row; gdouble *xd, *yd; const gdouble *data; gdouble d; data = gwy_data_field_get_data_const(dfield); xd = gwy_data_field_get_data(xder); yd = gwy_data_field_get_data(yder); for (row = 0; row < yres; row++) { for (col = 0; col < xres; col++) { if (!col) d = data[row*xres + col + 1] - data[row*xres + col]; else if (col == xres-1) d = data[row*xres + col] - data[row*xres + col - 1]; else d = (data[row*xres + col + 1] - data[row*xres + col - 1])/2; *(xd++) = d; if (!row) d = data[row*xres + xres + col] - data[row*xres + col]; else if (row == yres-1) d = data[row*xres + col] - data[row*xres - xres + col]; else d = (data[row*xres + xres + col] - data[row*xres - xres + col])/2; *(yd++) = d; } } } gwy_data_field_multiply(xder, xres/gwy_data_field_get_xreal(dfield)); gwy_data_field_multiply(yder, yres/gwy_data_field_get_yreal(dfield)); }
static void calculate_average_angle(GwyDataField *dtheta, GwyDataField *dphi, GwyDataField *mask, gdouble *theta, gdouble *phi) { gdouble sx, sy, sz; const gdouble *td, *pd, *md; gint i, n; td = gwy_data_field_get_data_const(dtheta); pd = gwy_data_field_get_data_const(dphi); md = gwy_data_field_get_data_const(mask); n = 0; sx = sy = sz = 0.0; for (i = gwy_data_field_get_xres(dtheta)*gwy_data_field_get_yres(dtheta); i; i--, td++, pd++, md++) { if (!*md) continue; sx += sin(*td)*cos(*pd); sy += sin(*td)*sin(*pd); sz += cos(*td); n++; } if (!n) n = 1; sx /= n; sy /= n; sz /= n; *theta = atan2(hypot(sx, sy), sz); *phi = atan2(sy, sx); }
static GwyDataField* fake_mask(GwyDataField *dfield, GwyDataField *mask, GwyMaskingType masking) { GwyDataField *masked; gint xres = gwy_data_field_get_xres(dfield); gint yres = gwy_data_field_get_yres(dfield); const gdouble *d, *m; gdouble *md; gint i, n; if (!mask || masking == GWY_MASK_IGNORE) return dfield; gwy_data_field_area_count_in_range(mask, NULL, 0, 0, xres, yres, G_MAXDOUBLE, 1.0, NULL, &n); if (masking == GWY_MASK_EXCLUDE) n = xres*yres - n; if (n == xres*yres) return dfield; masked = gwy_data_field_new(n, 1, n, 1.0, FALSE); md = gwy_data_field_get_data(masked); d = gwy_data_field_get_data_const(dfield); m = gwy_data_field_get_data_const(mask); n = 0; for (i = 0; i < xres*yres; i++) { gboolean mi = (m[i] >= 1.0); if ((mi && masking == GWY_MASK_INCLUDE) || (!mi && masking == GWY_MASK_EXCLUDE)) md[n++] = d[i]; } g_object_unref(dfield); return masked; }
static void immerse_find_maximum(GwyDataField *score, gint *col, gint *row) { const gdouble *d; gint i, n, m; d = gwy_data_field_get_data_const(score); n = gwy_data_field_get_xres(score)*gwy_data_field_get_yres(score); m = 0; for (i = 1; i < n; i++) { if (d[i] > d[m]) m = i; } *row = m/gwy_data_field_get_xres(score); *col = m % gwy_data_field_get_xres(score); }
static GwyDataField* prof_psdf(GwyDataField *dfield, const ProfArgs *args) { GwyDataField *fftre, *fftim; GwySIUnit *zunit; const gdouble *im; gdouble *re; gint xres, yres, i; fftre = gwy_data_field_new_alike(dfield, FALSE); fftim = gwy_data_field_new_alike(dfield, FALSE); gwy_data_field_2dfft(dfield, NULL, fftre, fftim, args->windowing, GWY_TRANSFORM_DIRECTION_FORWARD, GWY_INTERPOLATION_LINEAR, /* ignored */ FALSE, 1); xres = gwy_data_field_get_xres(dfield); yres = gwy_data_field_get_yres(dfield); re = gwy_data_field_get_data(fftre); im = gwy_data_field_get_data_const(fftim); /* Put the PSDF to fftre. */ for (i = 0; i < xres*yres; i++) re[i] = re[i]*re[i] + im[i]*im[i]; g_object_unref(fftim); gwy_data_field_fft_postprocess(fftre, TRUE); zunit = gwy_data_field_get_si_unit_z(fftre); gwy_si_unit_power(gwy_data_field_get_si_unit_z(dfield), 2, zunit); gwy_si_unit_multiply(gwy_data_field_get_si_unit_xy(dfield), zunit, zunit); return fftre; }
static void find_score_maximum(GwyDataField *correlation_score, gint *max_col, gint *max_row) { gint i, n, maxi = 0; gdouble max = -G_MAXDOUBLE; const gdouble *data; n = gwy_data_field_get_xres(correlation_score) *gwy_data_field_get_yres(correlation_score); data = gwy_data_field_get_data_const(correlation_score); for (i = 0; i < n; i++) { if (max < data[i]) { max = data[i]; maxi = i; } } *max_row = (gint)floor(maxi/gwy_data_field_get_xres(correlation_score)); *max_col = maxi - (*max_row)*gwy_data_field_get_xres(correlation_score); }
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 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 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 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 void gwy_data_field_facet_distribution(GwyDataField *dfield, gint kernel_size, GwyContainer *container) { GwyDataField *dtheta, *dphi, *dist; GwySIUnit *siunit; gdouble *xd, *yd, *data; const gdouble *xdc, *ydc; gdouble q, max; gint res, hres, i, j, mi, mj, xres, yres; if (gwy_container_gis_object_by_name(container, "/theta", &dtheta)) g_object_ref(dtheta); else dtheta = gwy_data_field_new_alike(dfield, FALSE); if (gwy_container_gis_object_by_name(container, "/phi", &dphi)) g_object_ref(dphi); else dphi = gwy_data_field_new_alike(dfield, FALSE); compute_slopes(dfield, kernel_size, dtheta, dphi); xres = gwy_data_field_get_xres(dfield); yres = gwy_data_field_get_yres(dfield); xd = gwy_data_field_get_data(dtheta); yd = gwy_data_field_get_data(dphi); for (i = xres*yres; i; i--, xd++, yd++) { gdouble theta, phi; slopes_to_angles(*xd, *yd, &theta, &phi); *xd = theta; *yd = phi; } q = gwy_data_field_get_max(dtheta); q = MIN(q*1.05, G_PI/2.0); q = G_SQRT2/(2.0*sin(q/2.0)); if (gwy_container_gis_object_by_name(container, "/0/data", &dist)) { g_object_ref(dist); gwy_data_field_clear(dist); gwy_data_field_set_xreal(dist, 2.0*G_SQRT2/q); gwy_data_field_set_yreal(dist, 2.0*G_SQRT2/q); } else { dist = gwy_data_field_new(FDATA_RES, FDATA_RES, 2.0*G_SQRT2/q, 2.0*G_SQRT2/q, TRUE); siunit = gwy_si_unit_new(""); gwy_data_field_set_si_unit_z(dist, siunit); g_object_unref(siunit); /* FIXME */ siunit = gwy_si_unit_new(""); gwy_data_field_set_si_unit_xy(dist, siunit); g_object_unref(siunit); } res = FDATA_RES; hres = (res - 1)/2; data = gwy_data_field_get_data(dist); xdc = gwy_data_field_get_data_const(dtheta); ydc = gwy_data_field_get_data_const(dphi); for (i = xres*yres; i; i--, xdc++, ydc++) { gdouble x, y; gint xx, yy; angles_to_xy(*xdc, *ydc, &x, &y); xx = GWY_ROUND(q*x/G_SQRT2*hres) + hres; yy = GWY_ROUND(q*y/G_SQRT2*hres) + hres; data[yy*res + xx] += 1.0; } /* Find maxima */ mi = mj = hres; max = 0; for (i = 1; i+1 < res; i++) { for (j = 1; j+1 < res; j++) { gdouble z; z = data[i*res + j] + 0.3*(data[i*res + j - 1] + data[i*res + j + 1] + data[i*res - res + j] + data[i*res + res + j]) + 0.1*(data[i*res - res + j - 1] + data[i*res - res + j + 1] + data[i*res + res + j - 1] + data[i*res + res + j + 1]); if (G_UNLIKELY(z > max)) { max = z; mi = i; mj = j; } } } for (i = res*res; i; i--, data++) *data = pow(*data, 0.35); gwy_container_set_double_by_name(container, "/q", q); { gdouble x, y, theta, phi; x = (mj - hres)*G_SQRT2/(q*hres); y = (mi - hres)*G_SQRT2/(q*hres); xy_to_angles(x, y, &theta, &phi); gwy_container_set_double_by_name(container, "/theta0", theta); gwy_container_set_double_by_name(container, "/phi0", phi); } gwy_container_set_object_by_name(container, "/0/data", dist); g_object_unref(dist); gwy_container_set_object_by_name(container, "/theta", dtheta); g_object_unref(dtheta); gwy_container_set_object_by_name(container, "/phi", dphi); g_object_unref(dphi); gwy_container_set_string_by_name(container, "/0/base/palette", g_strdup(FVIEW_GRADIENT)); gwy_data_field_data_changed(dist); }
static void merge_boundary(GwyDataField *dfield1, GwyDataField *dfield2, GwyDataField *result, GwyRectangle res_rect, GwyCoord f1_pos, GwyCoord f2_pos, GwyMergeBoundaryType boundary) { gint xres1, xres2, xres, yres1, yres2, col, row; gdouble weight, val1, val2; gint w1top = 0, w1bot = 0, w1left = 0, w1right = 0; gint w2top = 0, w2bot = 0, w2left = 0, w2right = 0; const gdouble *d1, *d2; gdouble *d; xres1 = dfield1->xres; yres1 = dfield1->yres; xres2 = dfield2->xres; yres2 = dfield2->yres; xres = result->xres; gwy_debug("dfield1: %d x %d at (%d, %d)", xres1, yres1, f1_pos.x, f1_pos.y); gwy_debug("dfield2: %d x %d at (%d, %d)", xres2, yres2, f2_pos.x, f2_pos.y); gwy_debug("result: %d x %d", xres, result->yres); gwy_debug("rect in result : %d x %d at (%d,%d)", res_rect.width, res_rect.height, res_rect.x, res_rect.y); assign_edge(res_rect.x, f1_pos.x, f2_pos.x, &w1left, &w2left); gwy_debug("left: %d %d", w1left, w2left); assign_edge(res_rect.y, f1_pos.y, f2_pos.y, &w1top, &w2top); gwy_debug("top: %d %d", w1top, w2top); assign_edge(res_rect.width, xres1 - f1_pos.x, xres2 - f2_pos.x, &w1right, &w2right); gwy_debug("right: %d %d", w1right, w2right); assign_edge(res_rect.height, yres1 - f1_pos.y, yres2 - f2_pos.y, &w1bot, &w2bot); gwy_debug("bot: %d %d", w1bot, w2bot); d1 = gwy_data_field_get_data_const(dfield1); d2 = gwy_data_field_get_data_const(dfield2); d = gwy_data_field_get_data(result); for (row = 0; row < res_rect.height; row++) { gint dtop = row + 1, dbot = res_rect.height - row; for (col = 0; col < res_rect.width; col++) { weight = 0.5; if (boundary == GWY_MERGE_BOUNDARY_INTERPOLATE) { gint dleft = col + 1, dright = res_rect.width - col; gint d1min = G_MAXINT, d2min = G_MAXINT; /* FIXME: This can be probably simplified... */ if (w1top && dtop < d1min) d1min = dtop; if (w1bot && dbot < d1min) d1min = dbot; if (w1left && dleft < d1min) d1min = dleft; if (w1right && dright < d1min) d1min = dright; if (w2top && dtop < d2min) d2min = dtop; if (w2bot && dbot < d2min) d2min = dbot; if (w2left && dleft < d2min) d2min = dleft; if (w2right && dright < d2min) d2min = dright; weight = (gdouble)d2min/(d1min + d2min); } val1 = d1[xres1*(row + f1_pos.y) + (col + f1_pos.x)]; val2 = d2[xres2*(row + f2_pos.y) + (col + f2_pos.x)]; d[xres*(row + res_rect.y) + col + res_rect.x] = (1.0 - weight)*val1 + weight*val2; } } }
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; }