libsdfio 1.0.0
Reader and writer for ISO 25178-71 SDF surface data files in C
Loading...
Searching...
No Matches
read_coded.c
/* Read an SDF file with the coded values API. */
#include <sdfio/file.h>
#include <sdfio/sdfio.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
if (argc < 2)
{
fprintf(stderr, "usage: %s input.sdf\n", argv[0]);
return EXIT_FAILURE;
}
sdfio_reader *reader = NULL;
sdfio_status status = sdfio_open_file(reader, argv[1]);
if (status != SDFIO_OK)
{
fprintf(stderr, "%s: %s\n", sdfio_status_string(status),
return EXIT_FAILURE;
}
const sdfio_header *header = sdfio_reader_header(reader);
{
fprintf(stderr, "the file does not contain int16 coded values\n");
return EXIT_FAILURE;
}
size_t row_size = 0;
int16_t *row = sdfio_data_row_size(header, &row_size) == SDFIO_OK
? malloc(row_size)
: NULL;
if (row == NULL)
{
fprintf(stderr, "a row does not fit in memory\n");
return EXIT_FAILURE;
}
size_t not_measured = 0;
size_t measured = 0;
double sum = 0.0;
const size_t columns = sdfio_header_columns(header);
const size_t rows = sdfio_header_rows(header);
for (size_t y = 0; y < rows; ++y)
{
status = sdfio_read_data(reader, &layout, row, 1);
if (status != SDFIO_OK)
{
break;
}
for (size_t x = 0; x < columns; ++x)
{
if (row[x] == SDFIO_BAD_INT16)
{
++not_measured;
}
else
{
sum += row[x];
++measured;
}
}
}
if (status != SDFIO_OK)
{
fprintf(stderr, "%s: %s\n", sdfio_status_string(status),
}
else
{
const double mean = sum / (double)measured;
printf("%s %lu x %lu, %zu points not measured, mean %g coded value = "
"%g m\n",
sdfio_header_revision(header) == SDFIO_ISO_1_0 ? "ISO-1.0"
: "ISO-2.0",
(unsigned long)columns, (unsigned long)rows, not_measured, mean,
mean * sdfio_header_z_scale(header));
}
free(row);
return status == SDFIO_OK ? EXIT_SUCCESS : EXIT_FAILURE;
}
Files by path, and the stream over a C file.
sdfio_status sdfio_open_file(sdfio_reader *reader, const char *path)
Opens the file at a path as the source and reads its header, as sdfio_open_stream() does for a stream...
struct sdfio_header sdfio_header
Header of a file, with the field names of the standard.
Definition header.h:42
sdfio_data_type sdfio_header_data_type(const sdfio_header *header)
Returns the data type of the coded values in the data area.
sdfio_revision sdfio_header_revision(const sdfio_header *header)
Returns the revision of the format.
uint32_t sdfio_header_rows(const sdfio_header *header)
Returns the number of rows N of the data matrix: 1 for a profile.
uint32_t sdfio_header_columns(const sdfio_header *header)
Returns the number of columns M of the data matrix: the points along x.
double sdfio_header_z_scale(const sdfio_header *header)
Returns the z-scale factor: the height in metres that a coded value of 1 in the data area stands for.
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.
sdfio_status sdfio_read_data(sdfio_reader *reader, const sdfio_layout *layout, void *dst, size_t rows)
Reads the next rows of the data area as the file codes them.
sdfio_status sdfio_reader_add_binary(sdfio_reader *reader)
Adds the binary format to the formats that the reader reads, the version numbers bISO-1....
const sdfio_header * sdfio_reader_header(const sdfio_reader *reader)
Returns the header of the opened file, or null if no file is open.
struct sdfio_reader sdfio_reader
Opaque handle of a reader.
Definition reader.h:58
sdfio_status sdfio_reader_create(const sdfio_allocator *allocator, sdfio_reader **reader)
Creates a reader.
const char * sdfio_reader_error_message(const sdfio_reader *reader)
Returns a description of the most recent failure of the reader, or an empty string if none occurred.
void sdfio_reader_free(sdfio_reader *reader)
Releases a reader and closes the file that sdfio_open_file() opened, if any.
Reading and writing of ISO 25178-71 SDF surface data files.
sdfio_status sdfio_data_row_size(const sdfio_header *header, size_t *bytes)
Returns the size in bytes of a buffer for one row of the data area, to read or write the data row by ...
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_ISO_1_0
Revision ISO-1.0.
Definition types.h:74
const char * sdfio_status_string(sdfio_status status)
Returns a constant description of a status.
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