---
title: "Remote OME-NGFF Demo"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Remote OME-NGFF Demo}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

### Setup

We install the `blosc` package to be able to use Blosc as the decompression codec, since the image we want to load is Blosc-compressed.

```r
install.packages("blosc")
```

### Demo

```r
library(pizzarr)

# The path to the root of the OME-NGFF Zarr store.
root <- "https://uk1s3.embassy.ebi.ac.uk/idr/zarr/v0.4/idr0062A/6001240.zarr"

# Open the OME-NGFF as an HttpStore.
store <- HttpStore$new(root)
g <- zarr_open_group(store)

# Using the OME metadata, get the path to the first resolution of the image pyramid.
attrs <- g$get_attrs()$to_list()
resolution_paths <- attrs$multiscales[[1]]$datasets[[1]]$path
first_resolution <- resolution_paths[[1]]

# Load the 4-dimensional array of pixels (as a ZarrArray instance).
zarr_arr <- g$get_item(first_resolution)

print(zarr_arr$get_shape())
# [1]   2 236 275 271

z_index <- 118

# Load a single Z-slice of pixels (as a NestedArray instance).
nested_arr <- zarr_arr$get_item(list(slice(1, 2), slice(z_index, z_index), slice(NA, NA), slice(NA, NA)))

print(nested_arr$shape)
# [1]   2   1 275 271

# Extract the NestedArray contents as a base R array.
arr <- nested_arr$data

# Convert to a pseudo-RGB array to make raster package happy.
rg_arr <- aperm(arr, c(2, 4, 3, 1))[1,,,]
rgb_arr <- array(dim=c(271, 275, 3))
rgb_arr[,,1] <- rg_arr[,,1]
rgb_arr[,,2] <- rg_arr[,,2]
rgb_arr[,,3] <- 0

# Plot the pseudo-RGB image using base R rasterImage().
plot.new()
plot.window(c(0, 271), c(0, 275), asp = 1)
rasterImage(rgb_arr / max(rgb_arr), 0, 0, 271, 275)
```