libsdfio 1.0.0
Reader and writer for ISO 25178-71 SDF surface data files in C
Loading...
Searching...
No Matches
write_heights.c
/* Write SDF files of both revisions with the heights API. */
#include <sdfio/file.h>
#include <sdfio/sdfio.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define COLUMNS 256
#define ROWS 192
static float heights[ROWS * COLUMNS];
/* Generates a ripple of heights in metres, with a region of points that are not
* measured, which are NaN. */
static void generate_heights(void)
{
for (size_t row = 0; row < ROWS; ++row)
{
const bool in_region = row > 100 && row < 110;
const double row_factor = 2e-6 * sin(0.1 * (double)row);
float *line = heights + row * COLUMNS;
for (size_t column = 0; column < COLUMNS; ++column)
{
line[column] =
in_region && column < 40
? NAN
: (float)(row_factor * cos(0.07 * (double)column));
}
}
}
static void init_header(sdfio_header *header, sdfio_revision revision,
{
sdfio_header_set_dates(header, time(NULL));
sdfio_header_set_revision(header, revision);
sdfio_header_set_columns(header, COLUMNS);
sdfio_header_set_rows(header, ROWS);
sdfio_header_set_x_scale(header, 1e-6);
sdfio_header_set_y_scale(header, 1e-6);
}
static int write_surface(const char *path, sdfio_revision revision,
{
sdfio_header *header = NULL;
init_header(header, revision, type);
sdfio_writer *writer = NULL;
sdfio_status status = sdfio_create_file(writer, path, header);
if (status == SDFIO_OK)
{
}
if (status == SDFIO_OK)
{
sdfio_write_trailer_field(writer, "OperatorName", "Jane Doe");
sdfio_write_trailer_field(writer, "Magnification", "20.5");
status = sdfio_end_file(writer);
}
if (status != SDFIO_OK)
{
fprintf(stderr, "%s\n", sdfio_writer_error_message(writer));
}
return status == SDFIO_OK ? EXIT_SUCCESS : EXIT_FAILURE;
}
int main(int argc, char **argv)
{
if (argc < 3)
{
fprintf(stderr, "usage: %s iso2.sdf iso1.sdf\n", argv[0]);
return EXIT_FAILURE;
}
generate_heights();
const int floats = write_surface(argv[1], SDFIO_ISO_2_0, SDFIO_TYPE_FLOAT);
const int integers =
write_surface(argv[2], SDFIO_ISO_1_0, SDFIO_TYPE_INT16);
return floats == EXIT_SUCCESS && integers == EXIT_SUCCESS ? EXIT_SUCCESS
: EXIT_FAILURE;
}
Files by path, and the stream over a C file.
sdfio_status sdfio_create_file(sdfio_writer *writer, const char *path, const sdfio_header *header)
Creates the file at a path and starts it as sdfio_create_stream() does.
struct sdfio_header sdfio_header
Header of a file, with the field names of the standard.
Definition header.h:42
sdfio_status sdfio_header_set_revision(sdfio_header *header, sdfio_revision revision)
Sets the revision of the format.
void sdfio_header_free(sdfio_header *header)
Releases a header that sdfio_header_create() created.
sdfio_status sdfio_header_set_x_scale(sdfio_header *header, double x_scale)
Sets the sampling interval along x in metres.
sdfio_status sdfio_header_create(const sdfio_allocator *allocator, sdfio_header **header)
Creates a header with default values.
sdfio_status sdfio_header_set_rows(sdfio_header *header, uint32_t rows)
Sets the number of rows N of the data matrix.
sdfio_status sdfio_header_set_columns(sdfio_header *header, uint32_t columns)
Sets the number of columns M of the data matrix.
sdfio_status sdfio_header_set_dates(sdfio_header *header, time_t date)
Sets the creation date and the modification date to the same time, rounded down to the minute.
sdfio_status sdfio_header_set_y_scale(sdfio_header *header, double y_scale)
Sets the sampling interval along y in metres.
sdfio_status sdfio_header_set_data_type(sdfio_header *header, sdfio_data_type data_type)
Sets the data type of the coded values in the data area.
const sdfio_layout * sdfio_layout_packed(sdfio_data_type type)
Returns the layout of a plain array: the elements follow each other and the rows follow each other.
Reading and writing of ISO 25178-71 SDF surface data files.
sdfio_revision
Revision of the SDF format that a file follows.
Definition types.h:72
@ SDFIO_ISO_1_0
Revision ISO-1.0.
Definition types.h:74
@ SDFIO_ISO_2_0
Revision ISO-2.0.
Definition types.h:76
sdfio_status
Result of a function call.
Definition types.h:31
@ SDFIO_OK
The call succeeded.
Definition types.h:33
const sdfio_allocator * sdfio_default_allocator(void)
Returns the allocator that uses malloc() and free().
sdfio_data_type
Type of the coded values of the data area of a file, and of the elements of a buffer of heights.
Definition types.h:112
@ SDFIO_TYPE_FLOAT
float: IEEE 754 binary32.
Definition types.h:114
@ SDFIO_TYPE_INT16
int16_t.
Definition types.h:118
const sdfio_time_zone * sdfio_system_time_zone(void)
Returns the time zone of the machine, as its C library knows it.
sdfio_status sdfio_write_trailer_field(sdfio_writer *writer, const char *name, const char *value)
Writes a line field = value of the trailer, which ISO-2.0 expects.
sdfio_status sdfio_writer_add_binary(sdfio_writer *writer)
Adds the binary format to the formats that the writer writes.
sdfio_status sdfio_writer_create(const sdfio_allocator *allocator, sdfio_writer **writer)
Creates a writer.
struct sdfio_writer sdfio_writer
Opaque handle of a writer.
Definition writer.h:69
const char * sdfio_writer_error_message(const sdfio_writer *writer)
Returns a description of the most recent failure of the writer, or an empty string if none occurred.
void sdfio_writer_free(sdfio_writer *writer)
Releases a writer.
sdfio_status sdfio_write_heights(sdfio_writer *writer, const sdfio_layout *layout, const void *src)
Writes the whole data area from a matrix of heights in metres that lies in memory as the layout descr...
sdfio_status sdfio_writer_set_time_zone(sdfio_writer *writer, const sdfio_time_zone *time_zone)
Sets the conversion of the dates of ISO-1.0 files.
sdfio_status sdfio_end_file(sdfio_writer *writer)
Ends the file: closes the trailer, the last record, flushes the stream and, for a file that sdfio_cre...