libsdfio 1.0.0
Reader and writer for ISO 25178-71 SDF surface data files in C
Loading...
Searching...
No Matches
write_coded.c
/* Write an SDF file with the coded values API. */
#include <sdfio/file.h>
#include <sdfio/sdfio.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define COLUMNS 512
#define ROWS 64
#define DROPOUT_EVERY 97
static void generate_coded_row(int16_t *row, size_t y)
{
const size_t row_offset = y * 31;
for (size_t column = 0; column < COLUMNS; ++column)
{
if ((column + y) % DROPOUT_EVERY == 0)
{
row[column] = SDFIO_BAD_INT16;
}
else
{
row[column] = (int16_t)(((row_offset + column * 7) % 4000) - 2000);
}
}
}
static void init_header(sdfio_header *header)
{
const double z_scale =
sdfio_header_set_dates(header, time(NULL));
sdfio_header_set_columns(header, COLUMNS);
sdfio_header_set_rows(header, ROWS);
sdfio_header_set_x_scale(header, 2e-6);
sdfio_header_set_y_scale(header, 2e-6);
sdfio_header_set_z_scale(header, z_scale);
}
int main(int argc, char **argv)
{
if (argc < 2)
{
fprintf(stderr, "usage: %s output.sdf\n", argv[0]);
return EXIT_FAILURE;
}
sdfio_header *header = NULL;
init_header(header);
sdfio_writer *writer = NULL;
int16_t row[COLUMNS];
sdfio_status status = sdfio_create_file(writer, argv[1], header);
for (size_t y = 0; y < ROWS && status == SDFIO_OK; ++y)
{
generate_coded_row(row, y);
status = sdfio_write_data(writer, &layout, row, 1);
}
if (status == SDFIO_OK)
{
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;
}
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_z_scale(sdfio_header *header, double z_scale)
Sets the z-scale factor.
sdfio_status sdfio_header_set_z_resolution(sdfio_header *header, double z_resolution)
Sets the z-resolution.
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.
double sdfio_suggest_z_scale_for_range(sdfio_data_type type, double lowest, double highest)
Returns the z-scale that uses the range of an integer data type best for heights between lowest and h...
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.
Description of a buffer of the caller for heights or for the values of the data area: the type of its...
Definition layout.h:45
sdfio_status
Result of a function call.
Definition types.h:31
@ SDFIO_OK
The call succeeded.
Definition types.h:33
#define SDFIO_BAD_INT16
The bad value of SDFIO_TYPE_INT16.
Definition types.h:136
const sdfio_allocator * sdfio_default_allocator(void)
Returns the allocator that uses malloc() and free().
@ SDFIO_TYPE_INT16
int16_t.
Definition types.h:118
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.
sdfio_status sdfio_write_data(sdfio_writer *writer, const sdfio_layout *layout, const void *src, size_t rows)
Writes the next rows of the data area.
void sdfio_writer_free(sdfio_writer *writer)
Releases a writer.
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...