static gboolean fill_data_fields(SurfFile *surffile, const guchar *buffer, GError **error) { GwySIUnit *siunit; gdouble *data; guint i, j; surffile->dfield = gwy_data_field_new(surffile->xres, surffile->yres, surffile->xres*surffile->dx, surffile->yres*surffile->dy, FALSE); data = gwy_data_field_get_data(surffile->dfield); switch (surffile->pointsize) { case 16: { const gint16 *row, *d16 = (const gint16*)buffer; for (i = 0; i < surffile->xres; i++) { row = d16 + i*surffile->yres; for (j = 0; j < surffile->yres; j++) *(data++) = GINT16_FROM_LE(row[j]) * surffile->dz; } } break; case 32: { const gint32 *row, *d32 = (const gint32*)buffer; for (i = 0; i < surffile->xres; i++) { row = d32 + i*surffile->yres; for (j = 0; j < surffile->yres; j++) *(data++) = GINT32_FROM_LE(row[j]) * surffile->dz; } } break; default: err_BPP(error, surffile->pointsize); return FALSE; break; } siunit = gwy_si_unit_new("m"); gwy_data_field_set_si_unit_xy(surffile->dfield, siunit); g_object_unref(siunit); siunit = gwy_si_unit_new("m"); gwy_data_field_set_si_unit_z(surffile->dfield, siunit); g_object_unref(siunit); return TRUE; }
/* FIXME: We hope Nanoscope files always use little endian, because we only * have seen them on Intel. */ static gboolean read_binary_data(gint n, gdouble *data, gchar *buffer, gint bpp, GError **error) { gint i; gdouble q; q = 1.0/(1 << (8*bpp)); switch (bpp) { case 1: for (i = 0; i < n; i++) data[i] = q*buffer[i]; break; case 2: { gint16 *p = (gint16*)buffer; for (i = 0; i < n; i++) data[i] = q*GINT16_FROM_LE(p[i]); } break; case 4: { gint32 *p = (gint32*)buffer; for (i = 0; i < n; i++) data[i] = q*GINT32_FROM_LE(p[i]); } break; default: err_BPP(error, bpp); return FALSE; break; } return TRUE; }
static gboolean fill_data_fields(SurfFile *surffile, const guchar *buffer, GError **error) { gdouble *data; surffile->dfield = gwy_data_field_new(surffile->xres, surffile->yres, surffile->xres*surffile->dx, surffile->yres*surffile->dy, FALSE); data = gwy_data_field_get_data(surffile->dfield); switch (surffile->pointsize) { case 16: gwy_convert_raw_data(buffer, surffile->xres*surffile->yres, 1, GWY_RAW_DATA_SINT16, GWY_BYTE_ORDER_LITTLE_ENDIAN, data, surffile->dz, 0.0); break; case 32: gwy_convert_raw_data(buffer, surffile->xres*surffile->yres, 1, GWY_RAW_DATA_SINT32, GWY_BYTE_ORDER_LITTLE_ENDIAN, data, surffile->dz, 0.0); break; default: err_BPP(error, surffile->pointsize); return FALSE; break; } gwy_data_field_set_si_unit_xy(surffile->dfield, surffile->xyunit); g_object_unref(surffile->xyunit); gwy_data_field_set_si_unit_z(surffile->dfield, surffile->zunit); g_object_unref(surffile->zunit); return TRUE; }
static gsize pt3file_read_header(const guchar *buffer, gsize size, PicoHarpFile *pt3file, GError **error) { const guchar* (*read_imaging_header)(PicoHarpImagingHeader *header, const guchar *p); PicoHarpInstrument instr; const guchar *p; guint i, expected_size; p = buffer; if (size < HEADER_MIN_SIZE + 2) { err_TOO_SHORT(error); return 0; } get_CHARARRAY(pt3file->ident, &p); get_CHARARRAY(pt3file->format_version, &p); get_CHARARRAY(pt3file->creator_name, &p); get_CHARARRAY(pt3file->creator_version, &p); gwy_debug("<%.*s> <%.*s> <%.*s> <%.*s>", (gint)sizeof(pt3file->ident), pt3file->ident, (gint)sizeof(pt3file->format_version), pt3file->format_version, (gint)sizeof(pt3file->creator_name), pt3file->creator_name, (gint)sizeof(pt3file->creator_version), pt3file->creator_version); get_CHARARRAY(pt3file->file_time, &p); get_CHARARRAY(pt3file->crlf, &p); get_CHARARRAY(pt3file->comment, &p); if (memcmp(pt3file->ident, MAGIC, MAGIC_SIZE) != 0 || pt3file->crlf[0] != '\r' || pt3file->crlf[1] != '\n') { err_FILE_TYPE(error, "PicoHarp"); return 0; } pt3file->number_of_curves = gwy_get_guint32_le(&p); gwy_debug("number_of_curves: %u", pt3file->number_of_curves); pt3file->bits_per_record = gwy_get_guint32_le(&p); gwy_debug("bits_per_record: %u", pt3file->bits_per_record); pt3file->routing_channels = gwy_get_guint32_le(&p); gwy_debug("routing_channels: %u", pt3file->routing_channels); pt3file->number_of_boards = gwy_get_guint32_le(&p); gwy_debug("number_of_boards: %u", pt3file->number_of_boards); if (pt3file->number_of_boards != 1) { g_warning("Number of boards is %u instead of 1. Reading one.", pt3file->number_of_boards); pt3file->number_of_boards = MAX(pt3file->number_of_boards, 1); if (size < HEADER_MIN_SIZE + BOARD_SIZE*pt3file->number_of_boards) { g_set_error(error, GWY_MODULE_FILE_ERROR, GWY_MODULE_FILE_ERROR_DATA, _("File header is truncated.")); return 0; } } pt3file->active_curve = gwy_get_guint32_le(&p); pt3file->measurement_mode = gwy_get_guint32_le(&p); gwy_debug("measurement_mode: %u", pt3file->measurement_mode); pt3file->sub_mode = gwy_get_guint32_le(&p); gwy_debug("sub_mode: %u", pt3file->sub_mode); pt3file->range_no = gwy_get_guint32_le(&p); pt3file->offset = gwy_get_gint32_le(&p); pt3file->acquisition_time = gwy_get_guint32_le(&p); pt3file->stop_at = gwy_get_guint32_le(&p); pt3file->stop_on_ovfl = gwy_get_guint32_le(&p); pt3file->restart = gwy_get_guint32_le(&p); pt3file->display_lin_log = !!gwy_get_guint32_le(&p); pt3file->display_time_axis_from = gwy_get_guint32_le(&p); pt3file->display_time_axis_to = gwy_get_guint32_le(&p); pt3file->display_counts_axis_from = gwy_get_guint32_le(&p); pt3file->display_counts_axis_to = gwy_get_guint32_le(&p); for (i = 0; i < G_N_ELEMENTS(pt3file->display_curve); i++) { pt3file->display_curve[i].map_to = gwy_get_guint32_le(&p); pt3file->display_curve[i].show = gwy_get_guint32_le(&p); } for (i = 0; i < G_N_ELEMENTS(pt3file->auto_param); i++) { pt3file->auto_param[i].start = gwy_get_gfloat_le(&p); pt3file->auto_param[i].step = gwy_get_gfloat_le(&p); pt3file->auto_param[i].end = gwy_get_gfloat_le(&p); } pt3file->repeat_mode = gwy_get_guint32_le(&p); pt3file->repeats_per_curve = gwy_get_guint32_le(&p); pt3file->repeat_time = gwy_get_guint32_le(&p); pt3file->repeat_wait_time = gwy_get_guint32_le(&p); get_CHARARRAY(pt3file->script_name, &p); p = pt3file_read_board(&pt3file->board, p); p += BOARD_SIZE*(pt3file->number_of_boards - 1); pt3file->ext_devices = gwy_get_guint32_le(&p); pt3file->reserved1 = gwy_get_guint32_le(&p); pt3file->reserved2 = gwy_get_guint32_le(&p); pt3file->input0_rate = gwy_get_guint32_le(&p); pt3file->input1_rate = gwy_get_guint32_le(&p); pt3file->stop_after = gwy_get_guint32_le(&p); pt3file->stop_reason = gwy_get_guint32_le(&p); pt3file->number_of_records = gwy_get_guint32_le(&p); gwy_debug("number_of_records: %u", pt3file->number_of_records); pt3file->spec_header_length = 4*gwy_get_guint32_le(&p); gwy_debug("spec_header_length: %u", pt3file->spec_header_length); gwy_debug("now at pos 0x%0lx", (gulong)(p - buffer)); if (pt3file->measurement_mode != 2 && pt3file->measurement_mode != 3) { g_set_error(error, GWY_MODULE_FILE_ERROR, GWY_MODULE_FILE_ERROR_DATA, _("Measurement mode must be 2 or 3; %u is invalid."), pt3file->measurement_mode); return 0; } if (pt3file->sub_mode != PICO_HARP_IMAGE) { g_set_error(error, GWY_MODULE_FILE_ERROR, GWY_MODULE_FILE_ERROR_DATA, _("Only area imaging files are supported.")); return 0; } if (pt3file->bits_per_record != 32) { err_BPP(error, pt3file->bits_per_record); return 0; } pt3file->imaging.common.dimensions = gwy_get_guint32_le(&p); gwy_debug("imaging dimensions: %u", pt3file->imaging.common.dimensions); if (pt3file->imaging.common.dimensions != 3) { g_set_error(error, GWY_MODULE_FILE_ERROR, GWY_MODULE_FILE_ERROR_DATA, _("Only area imaging files are supported.")); return 0; } pt3file->imaging.common.instrument = instr = gwy_get_guint32_le(&p); gwy_debug("imaging instrument: %u", pt3file->imaging.common.instrument); if (instr == PICO_HARP_PIE710) { expected_size = IMAGING_PIE710_SIZE; read_imaging_header = &read_pie710_imaging_header; } else if (instr == PICO_HARP_KDT180) { expected_size = IMAGING_KDT180_SIZE; read_imaging_header = &read_kdt180_imaging_header; } else if (instr == PICO_HARP_LSM) { expected_size = IMAGING_LSM_SIZE; read_imaging_header = &read_lsm_imaging_header; } else { g_set_error(error, GWY_MODULE_FILE_ERROR, GWY_MODULE_FILE_ERROR_DATA, _("Unknown instrument number %u."), instr); return 0; } if (pt3file->spec_header_length != expected_size) { g_set_error(error, GWY_MODULE_FILE_ERROR, GWY_MODULE_FILE_ERROR_DATA, _("Wrong imaging header size: %u instead of %u."), pt3file->spec_header_length, expected_size); return 0; } if ((p - buffer) + expected_size > size) { g_set_error(error, GWY_MODULE_FILE_ERROR, GWY_MODULE_FILE_ERROR_DATA, _("File header is truncated.")); return 0; } p = read_imaging_header(&pt3file->imaging, p); gwy_debug("xres: %u", pt3file->imaging.common.xres); gwy_debug("yres: %u", pt3file->imaging.common.yres); if (err_DIMENSION(error, pt3file->imaging.common.xres) || err_DIMENSION(error, pt3file->imaging.common.xres)) return 0; return (gsize)(p - buffer); }
static GwyContainer* burleigh_exp_load(const gchar *filename, G_GNUC_UNUSED GwyRunType mode, GError **error) { GwyContainer *container = NULL; gchar *buffer = NULL; BurleighExpHeader header; gsize size = 0; GError *err = NULL; GwyDataField *dfield; gdouble *data; guint i, n; if (!g_file_get_contents(filename, &buffer, &size, &err)) { err_GET_FILE_CONTENTS(error, &err); return NULL; } if (size < MIN_FILE_SIZE + 2) { err_TOO_SHORT(error); g_free(buffer); return NULL; } if (!burleigh_exp_read_header(&header, buffer, error)) goto fail; n = header.xres * header.yres; if (header.binary) { if (header.bpp != 16) { err_BPP(error, header.bpp); goto fail; } else if (err_SIZE_MISMATCH(error, header.length + 2*n, size, TRUE)) goto fail; } dfield = gwy_data_field_new(header.xres, header.yres, header.xscale, header.yscale, FALSE); data = gwy_data_field_get_data(dfield); if (header.binary) { const gint16 *d16 = (const gint16*)(buffer + header.length); for (i = 0; i < n; i++) data[i] = GINT16_FROM_LE(d16[i]); } else { gchar *p = buffer + header.length; for (i = 0; i < n; i++) data[i] = strtol(p, &p, 10); } gwy_data_field_multiply(dfield, header.zscale/32768.0); /* Units references released in free_header() */ gwy_data_field_set_si_unit_xy(dfield, header.xyunits); gwy_data_field_set_si_unit_z(dfield, header.zunits); container = gwy_container_new(); gwy_container_set_object(container, gwy_app_get_data_key_for_id(0), dfield); g_object_unref(dfield); gwy_app_channel_title_fall_back(container, 0); gwy_file_channel_import_log_add(container, 0, NULL, filename); fail: free_header(&header); g_free(buffer); return container; }
static GwyContainer* jeol_load(const gchar *filename, G_GNUC_UNUSED GwyRunType mode, GError **error) { JEOLImageHeader image_header; GwyContainer *meta, *container = NULL; guchar *buffer = NULL; gsize expected_size, size = 0; GError *err = NULL; GwyDataField *dfield = NULL; const gchar *title; gchar *s; if (!gwy_file_get_contents(filename, &buffer, &size, &err)) { err_GET_FILE_CONTENTS(error, &err); return NULL; } if (size < JEOL_DATA_START) { err_TOO_SHORT(error); goto fail; } if (memcmp(buffer, MAGIC, MAGIC_SIZE) != 0) { err_FILE_TYPE(error, "JEOL"); goto fail; } jeol_read_image_header(buffer, &image_header); /* Elementrary sanity */ if (image_header.bpp != 16) { err_BPP(error, image_header.bpp); goto fail; } if (err_DIMENSION(error, image_header.xres) || err_DIMENSION(error, image_header.yres)) goto fail; expected_size = image_header.bpp/8 * image_header.xres*image_header.yres; if (err_SIZE_MISMATCH(error, JEOL_DATA_START + expected_size, size, FALSE)) goto fail; if (image_header.image_type != JEOL_IMAGE || image_header.compressed) { err_NO_DATA(error); goto fail; } /* Use negated positive conditions to catch NaNs */ if (!((image_header.xreal = fabs(image_header.xreal)) > 0)) { g_warning("Real x size is 0.0, fixing to 1.0"); image_header.xreal = 1.0; } if (!((image_header.yreal = fabs(image_header.yreal)) > 0)) { g_warning("Real y size is 0.0, fixing to 1.0"); image_header.yreal = 1.0; } dfield = jeol_read_data_field(buffer + JEOL_DATA_START, &image_header); if (!dfield) { g_set_error(error, GWY_MODULE_FILE_ERROR, GWY_MODULE_FILE_ERROR_DATA, _("The type of data is unknown. " "Please report it to the developers.")); goto fail; } container = gwy_container_new(); gwy_container_set_object_by_name(container, "/0/data", dfield); g_object_unref(dfield); /* Title */ s = g_convert(image_header.internal_filename, -1, "iso-8859-1", "utf-8", NULL, NULL, NULL); if (s) g_strstrip(s); if (s && *s) gwy_container_set_string_by_name(container, "/0/data/title", s); else { title = gwy_flat_enum_to_string(image_header.data_source, G_N_ELEMENTS(data_sources), data_sources, data_sources_name); if (title) gwy_container_set_string_by_name(container, "/0/data/title", g_strdup(title)); } /* Meta */ meta = jeol_get_metadata(&image_header); gwy_container_set_object_by_name(container, "/0/meta", meta); g_object_unref(meta); gwy_file_channel_import_log_add(container, 0, NULL, filename); fail: gwy_file_abandon_contents(buffer, size, NULL); return container; }
static GwyDataField* read_datafield(const guchar *buffer, guint size, const StmprgFile *stmprgfile, GError **error) { gint xres, yres, bpp; gdouble xreal, yreal, q; GwyDataField *dfield; gdouble *data; GwySIUnit *unit; bpp = 2; /* words, always */ xres = stmprgfile->mainfield.points; yres = stmprgfile->mainfield.lines; xreal = Angstrom * stmprgfile->mainfield.field_x; yreal = Angstrom * stmprgfile->mainfield.field_y; q = stmprgfile->mainfield.sol_z * 1.0e-5; /* 5 ?? */ /* resolution of z value in angstrom/bit, 1.0e-10 */ if (err_SIZE_MISMATCH(error, bpp*xres*yres, size, FALSE)) return NULL; dfield = gwy_data_field_new(xres, yres, xreal, yreal, FALSE); data = gwy_data_field_get_data(dfield); if (!read_binary_ubedata(xres * yres, data, buffer, bpp)) { err_BPP(error, bpp); g_object_unref(dfield); return NULL; } gwy_data_field_multiply(dfield, q); unit = gwy_si_unit_new("m"); gwy_data_field_set_si_unit_xy(dfield, unit); g_object_unref(unit); /* Assuming we are reading channel1... */ switch (stmprgfile->control.channel1) { case STMPRG_CHANNEL_OFF: g_set_error(error, GWY_MODULE_FILE_ERROR, GWY_MODULE_FILE_ERROR_DATA, _("First channel is switched off.")); return NULL; case STMPRG_CHANNEL_Z: unit = gwy_si_unit_new("m"); break; case STMPRG_CHANNEL_I: case STMPRG_CHANNEL_I_I0: case STMPRG_CHANNEL_I0: unit = gwy_si_unit_new("A"); break; case STMPRG_CHANNEL_EXT1: case STMPRG_CHANNEL_EXT2: case STMPRG_CHANNEL_U0: unit = gwy_si_unit_new("V"); break; default: g_assert_not_reached(); break; } gwy_data_field_set_si_unit_z(dfield, unit); g_object_unref(unit); return dfield; }