#include <blosc2.h>
#include <b2nd.h>
int main() {
blosc2_init();
const int32_t width = 4 * 512;
const int32_t height = 4 * 272;
const int64_t buffershape[] = {1, height, width};
const int64_t buffersize = width * height * (int64_t)sizeof(uint16_t);
uint16_t* image = malloc(buffersize);
int64_t N_images = 10;
blosc2_cparams cparams = BLOSC2_CPARAMS_DEFAULTS;
cparams.typesize = sizeof(uint16_t);
cparams.compcode = BLOSC_BLOSCLZ;
cparams.clevel = 5;
cparams.nthreads = 4;
blosc2_storage storage = BLOSC2_STORAGE_DEFAULTS;
storage.contiguous = true; char *urlpath = "example_stack_images_set_slice.b2nd";
blosc2_remove_urlpath(urlpath);
storage.urlpath = urlpath;
storage.cparams = &cparams;
int64_t shape[] = {N_images, height, width};
int32_t chunkshape[] = {1, height, width};
int32_t blockshape[] = {1, height, width};
b2nd_context_t *ctx = b2nd_create_ctx(&storage, 3,
shape, chunkshape, blockshape,
"|u2", DTYPE_NUMPY_FORMAT,
NULL, 0);
b2nd_array_t *src;
if (b2nd_empty(ctx, &src) < 0) {
printf("Error in b2nd_empty\n");
return -1;
}
printf("Saving images (set_slice version)...\n");
for (int64_t i = 0; i < N_images; i++) {
int64_t start[] = {i, 0, 0};
int64_t stop[] = {i + 1, height, width};
for (int j = 0; j < width * height; j++) {
image[j] = rand() % 65536; }
if (b2nd_set_slice_cbuffer(image, buffershape, buffersize, start, stop, src) < 0) {
printf("Error in b2nd_set_slice_cbuffer\n");
return -1;
}
}
printf("Adding vlmetalayer data\n");
uint8_t msgpack[1024];
char *content = "Using b2nd_set_slice_cbuffer()";
msgpack[0] = 0xd9;
msgpack[1] = strlen(content);
memcpy(msgpack + 2, content, strlen(content) + 1);
int metalen = blosc2_vlmeta_add(src->sc, "method",
msgpack, strlen(content) + 2, NULL);
if (metalen < 0) {
printf("Cannot write vlmetalayer");
return metalen;
}
b2nd_free_ctx(ctx);
printf("Images saved successfully in %s\n", urlpath);
urlpath = "example_stack_images_append.b2nd";
blosc2_remove_urlpath(urlpath);
storage.urlpath = urlpath;
int64_t shape2[] = {0, height, width};
ctx = b2nd_create_ctx(&storage, 3,
shape2, chunkshape, blockshape,
"|u2", DTYPE_NUMPY_FORMAT,
NULL, 0);
b2nd_free(src);
if (b2nd_empty(ctx, &src) < 0) {
printf("Error in b2nd_empty\n");
return -1;
}
printf("Saving images (append version)...\n");
for (int64_t i = 0; i < N_images; i++) {
for (int j = 0; j < width * height; j++) {
image[j] = rand() % 65536; }
if (b2nd_append(src, image, buffersize, 0) < 0) {
printf("Error in b2nd_append\n");
return -1;
}
}
printf("Adding vlmetalayer data\n");
content = "Using b2nd_append()";
msgpack[0] = 0xd9;
msgpack[1] = strlen(content);
memcpy(msgpack + 2, content, strlen(content) + 1);
metalen = blosc2_vlmeta_add(src->sc, "method",
msgpack, strlen(content) + 2, NULL);
if (metalen < 0) {
printf("Cannot write vlmetalayer");
return metalen;
}
printf("Images saved successfully in %s\n", urlpath);
b2nd_free(src);
b2nd_free_ctx(ctx);
blosc2_destroy();
free(image);
return 0;
}