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

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

om_common.c233 lines · 8.1 KiBRaw
1//
2// om_common.c
3// OpenMeteoApi
4//
5// Created by Patrick Zippenfenig on 30.10.2024.
6//
7
8#include "om_common.h"
9#include <math.h>
10#include "vp4.h"
11#include "fp.h"
12#include "conf.h"
13#pragma clang diagnostic ignored "-Wunused-parameter"
14#pragma clang diagnostic warning "-Wbad-function-cast"
15#pragma clang diagnostic error "-Wswitch"
16
17const char* om_error_string(OmError_t error) {
18 switch (error) {
19 case ERROR_OK:
20 return "No error occured";
21 case ERROR_INVALID_COMPRESSION_TYPE:
22 return "Invalid compression type";
23 case ERROR_INVALID_DATA_TYPE:
24 return "Invalid data type";
25 case ERROR_OUT_OF_BOUND_READ:
26 return "Corrupted data with potential out-of-bound read";
27 case ERROR_NOT_AN_OM_FILE:
28 return "Not an OM file";
29 case ERROR_DEFLATED_SIZE_MISMATCH:
30 return "Corrupted data: Deflated size does not match";
31 case ERROR_INVALID_DIMENSIONS:
32 return "Invalid dimensions";
33 case ERROR_INVALID_CHUNK_DIMENSIONS:
34 return "Invalid chunk dimensions";
35 case ERROR_INVALID_READ_OFFSET:
36 return "Invalid read offset dimensions";
37 case ERROR_INVALID_READ_COUNT:
38 return "Invalid read count dimensions";
39 case ERROR_INVALID_CUBE_OFFSET:
40 return "Invalid read cube offset dimensions";
41 }
42 return "";
43}
44
45uint8_t om_get_bytes_per_element(OmDataType_t data_type, OmError_t* error) {
46 switch (data_type) {
47 case DATA_TYPE_INT8_ARRAY:
48 case DATA_TYPE_UINT8_ARRAY:
49 return 1;
50
51 case DATA_TYPE_INT16_ARRAY:
52 case DATA_TYPE_UINT16_ARRAY:
53 return 2;
54
55 case DATA_TYPE_INT32_ARRAY:
56 case DATA_TYPE_UINT32_ARRAY:
57 case DATA_TYPE_FLOAT_ARRAY:
58 return 4;
59
60 case DATA_TYPE_INT64_ARRAY:
61 case DATA_TYPE_UINT64_ARRAY:
62 case DATA_TYPE_DOUBLE_ARRAY:
63 return 8;
64
65 case DATA_TYPE_STRING_ARRAY:
66 // NOTE: STRING_ARRAY is currently not implemented!
67 *error = ERROR_INVALID_DATA_TYPE;
68 return 0;
69 break;
70
71 case DATA_TYPE_INT8:
72 case DATA_TYPE_UINT8:
73 case DATA_TYPE_INT16:
74 case DATA_TYPE_UINT16:
75 case DATA_TYPE_INT32:
76 case DATA_TYPE_UINT32:
77 case DATA_TYPE_FLOAT:
78 case DATA_TYPE_INT64:
79 case DATA_TYPE_UINT64:
80 case DATA_TYPE_DOUBLE:
81 case DATA_TYPE_NONE:
82 case DATA_TYPE_STRING:
83 // NOTE: These datatypes are scalar and should not be used with this function!
84 *error = ERROR_INVALID_DATA_TYPE;
85 return 0;
86 }
87}
88
89uint8_t om_get_bytes_per_element_compressed(OmDataType_t data_type, OmCompression_t compression, OmError_t* error) {
90 // Adjust compressed size based on compression type
91 switch (compression) {
92 case COMPRESSION_PFOR_DELTA2D_INT16:
93 case COMPRESSION_PFOR_DELTA2D_INT16_LOGARITHMIC:
94 if (data_type != DATA_TYPE_FLOAT_ARRAY) {
95 *error = ERROR_INVALID_DATA_TYPE;
96 break;
97 }
98 return 2;
99
100 case COMPRESSION_FPX_XOR2D:
101 if (data_type != DATA_TYPE_FLOAT_ARRAY && data_type != DATA_TYPE_DOUBLE_ARRAY) {
102 *error = ERROR_INVALID_DATA_TYPE;
103 break;
104 }
105 return om_get_bytes_per_element(data_type, error);
106 case COMPRESSION_PFOR_DELTA2D:
107 return om_get_bytes_per_element(data_type, error);
108
109 default:
110 *error = ERROR_INVALID_COMPRESSION_TYPE;
111 }
112 return om_get_bytes_per_element(data_type, error);
113}
114
115void om_common_copy_float_to_int16(uint64_t length, float scale_factor, float add_offset, const void* src, void* dst) {
116 for (uint64_t i = 0; i < length; ++i) {
117 float val = ((float *)src)[i];
118 if (isnan(val)) {
119 ((int16_t *)dst)[i] = INT16_MAX;
120 } else {
121 float scaled = (val + add_offset) * scale_factor;
122 float clamped = fmaxf(INT16_MIN, fminf(INT16_MAX, roundf(scaled)));
123 ((int16_t *)dst)[i] = (int16_t)clamped;
124 }
125 }
126}
127
128void om_common_copy_float_to_int32(uint64_t length, float scale_factor, float add_offset, const void* src, void* dst) {
129 for (uint64_t i = 0; i < length; ++i) {
130 float val = ((float *)src)[i];
131 if (isnan(val)) {
132 ((int32_t *)dst)[i] = INT32_MAX;
133 } else {
134 float scaled = (val + add_offset) * scale_factor;
135 float clamped = fmaxf((float)INT32_MIN, fminf((float)INT32_MAX, roundf(scaled)));
136 ((int32_t *)dst)[i] = (int32_t)clamped;
137 }
138 }
139}
140
141void om_common_copy_double_to_int64(uint64_t length, float scale_factor, float add_offset, const void* src, void* dst) {
142 for (uint64_t i = 0; i < length; ++i) {
143 double val = ((double *)src)[i];
144 if (isnan(val)) {
145 ((int64_t *)dst)[i] = INT64_MAX;
146 } else {
147 double scaled = (val + (double)add_offset) * (double)scale_factor;
148 double clamped = fmax((double)INT64_MIN, fmin((double)INT64_MAX, round(scaled)));
149 ((int64_t *)dst)[i] = (int64_t)clamped;
150 }
151 }
152}
153
154void om_common_copy_float_to_int16_log10(uint64_t length, float scale_factor, const void* src, void* dst) {
155 for (uint64_t i = 0; i < length; ++i) {
156 float val = ((float *)src)[i];
157 if (isnan(val)) {
158 ((int16_t *)dst)[i] = INT16_MAX;
159 } else {
160 float scaled = log10f(1 + val) * scale_factor;
161 float clamped = fmaxf(INT16_MIN, fminf(INT16_MAX, roundf(scaled)));
162 ((int16_t *)dst)[i] = (int16_t)clamped;
163 }
164 }
165}
166
167void om_common_copy_int16_to_float(uint64_t length, float scale_factor, float add_offset, const void* src, void* dst) {
168 for (uint64_t i = 0; i < length; ++i) {
169 int16_t val = ((int16_t *)src)[i];
170 ((float *)dst)[i] = (val == INT16_MAX) ? NAN : (float)val / scale_factor - add_offset;
171 }
172}
173
174void om_common_copy_int32_to_float(uint64_t length, float scale_factor, float add_offset, const void* src, void* dst) {
175 for (uint64_t i = 0; i < length; ++i) {
176 int32_t val = ((int32_t *)src)[i];
177 ((float *)dst)[i] = (val == INT32_MAX) ? NAN : (float)val / scale_factor - add_offset;
178 }
179}
180
181void om_common_copy_int64_to_double(uint64_t length, float scale_factor, float add_offset, const void* src, void* dst) {
182 for (uint64_t i = 0; i < length; ++i) {
183 int64_t val = ((int64_t *)src)[i];
184 ((double *)dst)[i] = (val == INT64_MAX) ? NAN : (double)val / (double)scale_factor - (double)add_offset;
185 }
186}
187
188void om_common_copy_int16_to_float_log10(uint64_t length, float scale_factor, const void* src, void* dst) {
189 for (uint64_t i = 0; i < length; ++i) {
190 int16_t val = ((int16_t *)src)[i];
191 ((float *)dst)[i] = (val == INT16_MAX) ? NAN : powf(10, (float)val / scale_factor) - 1;
192 }
193}
194
195void om_common_copy8(uint64_t length, const void* src, void* dst) {
196 for (uint64_t i = 0; i < length; ++i) {
197 ((int8_t *)dst)[i] = ((int8_t *)src)[i];
198 }
199}
200
201void om_common_copy16(uint64_t length, const void* src, void* dst) {
202 for (uint64_t i = 0; i < length; ++i) {
203 ((int16_t *)dst)[i] = ((int16_t *)src)[i];
204 }
205}
206
207void om_common_copy32(uint64_t length, const void* src, void* dst) {
208 for (uint64_t i = 0; i < length; ++i) {
209 ((int32_t *)dst)[i] = ((int32_t *)src)[i];
210 }
211}
212
213void om_common_copy64(uint64_t length, const void* src, void* dst) {
214 for (uint64_t i = 0; i < length; ++i) {
215 ((int64_t *)dst)[i] = ((int64_t *)src)[i];
216 }
217}
218
219uint64_t om_common_compress_fpxenc32(const void* src, uint64_t length, void* dst) {
220 return fpxenc32((uint32_t*)src, length, (unsigned char *)dst, 0);
221}
222
223uint64_t om_common_compress_fpxenc64(const void* src, uint64_t length, void* dst) {
224 return fpxenc64((uint64_t*)src, length, (unsigned char *)dst, 0);
225}
226
227uint64_t om_common_decompress_fpxdec32(const void* src, uint64_t length, void* dst) {
228 return fpxdec32((unsigned char *)src, length, (uint32_t *)dst, 0);
229}
230
231uint64_t om_common_decompress_fpxdec64(const void* src, uint64_t length, void* dst) {
232 return fpxdec64((unsigned char *)src, length, (uint64_t *)dst, 0);
233}