JFJochFrameSerializer: Generalize 2D arrays for floats
This commit is contained in:
@@ -78,41 +78,43 @@ void CBOR_ENC_COMPRESSED(CborEncoder &encoder,
|
||||
}
|
||||
}
|
||||
|
||||
inline void CBOR_ENC_MULTIDIM_TYPED_ARRAY(CborEncoder &encoder, const char* key,
|
||||
const void *image, size_t image_size,
|
||||
size_t xpixel, size_t ypixel, CompressionAlgorithm algorithm,
|
||||
size_t elem_size, bool elem_sign) {
|
||||
inline void CBOR_ENC_2D_TYPED_ARRAY(CborEncoder &encoder, const CBORImage& image) {
|
||||
//if ((algorithm == CompressionAlgorithm::NO_COMPRESSION) && (xpixel * ypixel != image_size / elem_size))
|
||||
// throw JFJochException(JFJochExceptionCategory::CBORError, "Mismatch in array size");
|
||||
|
||||
CborEncoder arrayEncoder, arrayEncoder_2;
|
||||
cborErr(cbor_encode_text_stringz(&encoder, key));
|
||||
cborErr(cbor_encode_text_stringz(&encoder, image.channel.c_str()));
|
||||
|
||||
cbor_encode_tag(&encoder, TagMultiDimArray);
|
||||
|
||||
cborErr(cbor_encoder_create_array(&encoder, &arrayEncoder, 2));
|
||||
cborErr(cbor_encoder_create_array(&arrayEncoder, &arrayEncoder_2, 2));
|
||||
cborErr(cbor_encode_uint(&arrayEncoder_2, ypixel));
|
||||
cborErr(cbor_encode_uint(&arrayEncoder_2, xpixel));
|
||||
cborErr(cbor_encode_uint(&arrayEncoder_2, image.ypixel));
|
||||
cborErr(cbor_encode_uint(&arrayEncoder_2, image.xpixel));
|
||||
cborErr(cbor_encoder_close_container(&arrayEncoder, &arrayEncoder_2));
|
||||
|
||||
CborTag typed_array_tag;
|
||||
|
||||
if (elem_sign) {
|
||||
if (elem_size == 4)
|
||||
if (image.pixel_is_float) {
|
||||
if (image.pixel_depth_bytes == 4)
|
||||
typed_array_tag = TagFloatLE;
|
||||
else
|
||||
throw JFJochException(JFJochExceptionCategory::CBORError, "Array size not supported");
|
||||
} else if (image.pixel_is_signed) {
|
||||
if (image.pixel_depth_bytes == 4)
|
||||
typed_array_tag = TagSignedInt32BitLE;
|
||||
else if (elem_size == 2)
|
||||
else if (image.pixel_depth_bytes == 2)
|
||||
typed_array_tag = TagSignedInt16BitLE;
|
||||
else if (elem_size == 1)
|
||||
else if (image.pixel_depth_bytes == 1)
|
||||
typed_array_tag = TagSignedInt8Bit;
|
||||
else
|
||||
throw JFJochException(JFJochExceptionCategory::CBORError, "Array size not supported");
|
||||
} else {
|
||||
if (elem_size == 4)
|
||||
if (image.pixel_depth_bytes == 4)
|
||||
typed_array_tag = TagUnsignedInt32BitLE;
|
||||
else if (elem_size == 2)
|
||||
else if (image.pixel_depth_bytes == 2)
|
||||
typed_array_tag = TagUnsignedInt16BitLE;
|
||||
else if (elem_size == 1)
|
||||
else if (image.pixel_depth_bytes == 1)
|
||||
typed_array_tag = TagUnsignedInt8Bit;
|
||||
else
|
||||
throw JFJochException(JFJochExceptionCategory::CBORError, "Array size not supported");
|
||||
@@ -120,7 +122,7 @@ inline void CBOR_ENC_MULTIDIM_TYPED_ARRAY(CborEncoder &encoder, const char* key,
|
||||
|
||||
cbor_encode_tag(&arrayEncoder, typed_array_tag);
|
||||
|
||||
CBOR_ENC_COMPRESSED(arrayEncoder, image, image_size, algorithm, elem_size);
|
||||
CBOR_ENC_COMPRESSED(arrayEncoder, image.data, image.size, image.algorithm, image.pixel_depth_bytes);
|
||||
cborErr(cbor_encoder_close_container(&encoder, &arrayEncoder));
|
||||
}
|
||||
|
||||
@@ -130,15 +132,20 @@ inline void CBOR_ENC_PIXEL_MASK(CborEncoder &encoder, const char* key,
|
||||
CborEncoder mapEncoder;
|
||||
cborErr(cbor_encode_text_stringz(&encoder, key));
|
||||
cborErr(cbor_encoder_create_map(&encoder, &mapEncoder, pixel_mask.size()));
|
||||
for (auto &[pixel_mask_key, pixel_mask_array]: pixel_mask)
|
||||
CBOR_ENC_MULTIDIM_TYPED_ARRAY(mapEncoder,
|
||||
pixel_mask_key.c_str(),
|
||||
pixel_mask_array.data(),
|
||||
pixel_mask_array.size() * sizeof(uint32_t),
|
||||
xpixel, ypixel,
|
||||
CompressionAlgorithm::NO_COMPRESSION,
|
||||
sizeof(uint32_t),
|
||||
false);
|
||||
for (auto &[pixel_mask_key, pixel_mask_array]: pixel_mask) {
|
||||
CBORImage image{
|
||||
.data = reinterpret_cast<const uint8_t *>(pixel_mask_array.data()),
|
||||
.size = pixel_mask_array.size() * sizeof(uint32_t),
|
||||
.xpixel = xpixel,
|
||||
.ypixel = ypixel,
|
||||
.pixel_depth_bytes = sizeof(uint32_t),
|
||||
.pixel_is_signed = false,
|
||||
.pixel_is_float = false,
|
||||
.algorithm = CompressionAlgorithm::NO_COMPRESSION,
|
||||
.channel = pixel_mask_key
|
||||
};
|
||||
CBOR_ENC_2D_TYPED_ARRAY(mapEncoder, image);
|
||||
}
|
||||
|
||||
cborErr(cbor_encoder_close_container(&encoder, &mapEncoder));
|
||||
}
|
||||
@@ -195,16 +202,7 @@ inline void CBOR_ENC(CborEncoder &encoder, const char* key, const CBORImage& mes
|
||||
|
||||
cborErr(cbor_encode_text_stringz(&encoder, key));
|
||||
cborErr(cbor_encoder_create_map(&encoder, &mapEncoder, 1));
|
||||
CBOR_ENC_MULTIDIM_TYPED_ARRAY(mapEncoder,
|
||||
message.channel.c_str(),
|
||||
(uint8_t *) message.data,
|
||||
message.size,
|
||||
message.xpixel,
|
||||
message.ypixel,
|
||||
message.algorithm,
|
||||
message.pixel_depth_bytes,
|
||||
message.pixel_is_signed);
|
||||
|
||||
CBOR_ENC_2D_TYPED_ARRAY(mapEncoder, message);
|
||||
cborErr(cbor_encoder_close_container(&encoder, &mapEncoder));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user