Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
main ▾

Modern-Apps.git / third_party / om-file-format-sys / c / include / om_common.h

om_common.h117 lines · 4.8 KiBRaw
1//
2// om_common.h
3// OpenMeteoApi
4//
5// Created by Patrick Zippenfenig on 29.10.2024.
6//
7
8#ifndef OM_COMMON_H
9#define OM_COMMON_H
10
11#include <stddef.h>
12#include <stdint.h>
13#include <stdbool.h>
14
15/// Number of look-up-table addresses to individual data chunks that will be compressed.
16/// Effectively only the first address will be stored and the next addresses are delta and fixed-bytes-encoded.
17/// A larger number reduces file size marginally if a lot of chunks are used. However, read performance decreases.
18#define LUT_CHUNK_COUNT 64
19
20typedef enum {
21 ERROR_OK = 0, // not an error
22 ERROR_INVALID_COMPRESSION_TYPE = 1,
23 ERROR_INVALID_DATA_TYPE = 2,
24 ERROR_OUT_OF_BOUND_READ = 3,
25 ERROR_NOT_AN_OM_FILE = 4,
26 ERROR_DEFLATED_SIZE_MISMATCH = 5,
27 ERROR_INVALID_DIMENSIONS = 6,
28 ERROR_INVALID_CHUNK_DIMENSIONS = 7,
29 ERROR_INVALID_READ_OFFSET = 8,
30 ERROR_INVALID_READ_COUNT = 9,
31 ERROR_INVALID_CUBE_OFFSET = 10,
32} OmError_t;
33
34const char* om_error_string(OmError_t error);
35
36/// Data types
37typedef enum {
38 DATA_TYPE_NONE = 0,
39 DATA_TYPE_INT8 = 1,
40 DATA_TYPE_UINT8 = 2,
41 DATA_TYPE_INT16 = 3,
42 DATA_TYPE_UINT16 = 4,
43 DATA_TYPE_INT32 = 5,
44 DATA_TYPE_UINT32 = 6,
45 DATA_TYPE_INT64 = 7,
46 DATA_TYPE_UINT64 = 8,
47 DATA_TYPE_FLOAT = 9,
48 DATA_TYPE_DOUBLE = 10,
49 DATA_TYPE_STRING = 11,
50 DATA_TYPE_INT8_ARRAY = 12,
51 DATA_TYPE_UINT8_ARRAY = 13,
52 DATA_TYPE_INT16_ARRAY = 14,
53 DATA_TYPE_UINT16_ARRAY = 15,
54 DATA_TYPE_INT32_ARRAY = 16,
55 DATA_TYPE_UINT32_ARRAY = 17,
56 DATA_TYPE_INT64_ARRAY = 18,
57 DATA_TYPE_UINT64_ARRAY = 19,
58 DATA_TYPE_FLOAT_ARRAY = 20,
59 DATA_TYPE_DOUBLE_ARRAY = 21,
60 DATA_TYPE_STRING_ARRAY = 22
61} OmDataType_t;
62
63/// Compression types
64typedef enum {
65 COMPRESSION_PFOR_DELTA2D_INT16 = 0, // Lossy compression using 2D delta coding and scale-factor. Only supports float and scales to 16-bit signed integer.
66 COMPRESSION_FPX_XOR2D = 1, // Lossless float/double compression using 2D xor coding.
67 COMPRESSION_PFOR_DELTA2D = 2, // PFor integer compression. Floating point values are scaled to 32 bit signed integers. Doubles are scaled to 64 bit signed integers.
68 COMPRESSION_PFOR_DELTA2D_INT16_LOGARITHMIC = 3, // Similar to `COMPRESSION_PFOR_DELTA2D_INT16` but applies `log10(1+x)` before.
69 COMPRESSION_NONE = 4
70} OmCompression_t;
71
72/// Get the number of bytes per element.
73/// This function will set an error if called for an invalid data type.
74/// It only supports array types.
75uint8_t om_get_bytes_per_element(OmDataType_t data_type, OmError_t* error);
76
77/// Get the number of bytes per element after compression.
78/// This function will set an error if called for an invalid data type.
79/// It only supports array types.
80uint8_t om_get_bytes_per_element_compressed(OmDataType_t data_type, OmCompression_t compression, OmError_t* error);
81
82static inline uint64_t divide_rounded_up(uint64_t dividend, uint64_t divisor) {
83 return (dividend + divisor - 1) / divisor;
84}
85
86static inline uint64_t om_min(uint64_t a, uint64_t b) { return a < b ? a : b; }
87static inline uint64_t om_max(uint64_t a, uint64_t b) { return a > b ? a : b; }
88
89/// Copy 16 bit integer array and convert to float
90void om_common_copy_float_to_int16(uint64_t length, float scale_factor, float add_offset, const void* src, void* dst);
91void om_common_copy_float_to_int32(uint64_t length, float scale_factor, float add_offset, const void* src, void* dst);
92void om_common_copy_double_to_int64(uint64_t length, float scale_factor, float add_offset, const void* src, void* dst);
93
94/// Copy 16 bit integer array and convert to float and scale log10
95void om_common_copy_float_to_int16_log10(uint64_t length, float scale_factor, const void* src, void* dst);
96
97/// Convert int16 and scale to float
98void om_common_copy_int16_to_float(uint64_t length, float scale_factor, float add_offset, const void* src, void* dst);
99void om_common_copy_int32_to_float(uint64_t length, float scale_factor, float add_offset, const void* src, void* dst);
100void om_common_copy_int64_to_double(uint64_t length, float scale_factor, float add_offset, const void* src, void* dst);
101
102/// Convert int16 and scale to float with log10
103void om_common_copy_int16_to_float_log10(uint64_t length, float scale_factor, const void* src, void* dst);
104
105void om_common_copy8(uint64_t length, const void* src, void* dst);
106void om_common_copy16(uint64_t length, const void* src, void* dst);
107void om_common_copy32(uint64_t length, const void* src, void* dst);
108void om_common_copy64(uint64_t length, const void* src, void* dst);
109
110uint64_t om_common_compress_fpxenc32(const void* src, uint64_t length, void* dst);
111uint64_t om_common_compress_fpxenc64(const void* src, uint64_t length, void* dst);
112uint64_t om_common_decompress_fpxdec32(const void* src, uint64_t length, void* dst);
113uint64_t om_common_decompress_fpxdec64(const void* src, uint64_t length, void* dst);
114
115
116
117#endif // OM_COMMON_H