In a mobile art-museum app, how do you let a visitor zoom a 20,000 x 15,000-pixel painting down to its brushstrokes without running out of memory?
answer
- memory should scale with the screen
- about 1.2 GB if decoded whole
- levels that halve, cut into tiles
- only the tiles in the viewport
- a coarse overview underneath
basics
~20 sDecoded whole the painting needs about 1.2 GB, so never decode it whole. Show a screen-sized overview, then decode only the tiles on screen at the resolution the zoom needs, from server-cut tiles or a region decoder.
solid answer
~50 sAt four bytes a pixel the painting is 300 million pixels, about 1.2 GB decoded - far beyond what an older phone gives one app. The screen shows only about its own pixel count at a time, so memory should scale with the screen, not the image. Split the image into a pyramid of levels, each half the resolution of the one below, cut into fixed-size tiles. Pick the level whose resolution matches the current zoom, decode only the tiles that intersect the viewport plus a small margin, and keep a coarse overview underneath so panning never shows blank. The tiles come either from a server that pre-cut the pyramid - the better fit on weak gallery signal, since only visible tiles download - or from a decoder that can read one region of a local file. Cancel tiles that leave the viewport and bound the tile cache.
code
pseudocode · 9 linesfunction tilesToShow(viewport, zoom, pyramid):
level = coarsestLevelWithAtLeastOnePixelPerScreenPixel(pyramid, zoom)
area = expand(viewport, by = pyramid.tileSize) # one tile of margin
wanted = tilesIntersecting(level, area)
sortByDistanceFromCentre(wanted, viewport)
for tile in tileCache.keys():
if tile not in wanted and tile.level != pyramid.overviewLevel:
pipeline.lowerPriorityOrCancel(tile)
return wantedgo deeper
Recall that a very large image cannot be decoded whole on a phone, and that zoomable viewers show only the part of the image on screen.
Explain the pyramid: levels that halve in resolution, fixed tiles, and choosing the level that gives about one image pixel per screen pixel.
Do the arithmetic, show why tile memory stays constant across zoom, and choose between server-cut tiles and on-device region decoding for the network the visitors actually have.
Decide whether the pyramid is a shared capability every large-image surface uses, and who owns producing the tiles, rather than each feature building its own zoom viewer.
## Why the painting cannot be decoded whole A decoded image costs `width x height x bytes per pixel`. A 20,000 x 15,000 scan of a painting is **300 million pixels**; at the common four bytes per pixel that is about **1.2 GB**. No phone gives one app that much, and an older phone's limit is a small fraction of it. Decoding the whole file and then zooming the view is therefore not an option, and neither is downsampling the whole image to screen size: that fits in memory but throws away exactly the detail - the brushstrokes - the visitor zooms in to see. The way out is to notice that a phone screen shows only about its own number of pixels at once. A screen of roughly 1,080 x 2,400 pixels (this scenario's assumption) holds about 2.6 million pixels, about 10 MB decoded. If memory follows what is on screen rather than the size of the source, the problem becomes ordinary. ## The tile pyramid A **tile pyramid** stores the image at several resolutions, each level half the width and height of the one below, and cuts every level into fixed-size **tiles**: | Level | Size in pixels | Decoded whole at 4 bytes per pixel | |---|---|---| | 0 (full detail) | 20,000 x 15,000 | ~1.2 GB | | 1 | 10,000 x 7,500 | ~300 MB | | 2 | 5,000 x 3,750 | ~75 MB | | 3 | 2,500 x 1,875 | ~19 MB | | 4 | 1,250 x 938 | ~4.7 MB | Level 4 is small enough to decode whole and serves as the **overview** shown when the painting fits the screen. Every deeper level is only ever decoded a tile grid's worth at a time, never whole. ## Choosing a level and the tiles On every zoom or pan, the viewer does the same few steps: 1. **Pick the level.** Choose the coarsest level that still gives at least one image pixel per screen pixel at the current zoom. Fitted to the screen's width, level 4 suffices; at full zoom, level 0 is needed. 2. **Find the visible tiles.** Intersect the viewport, plus a margin of about one tile, with that level's tile grid. 3. **Request what is missing.** Ask the pipeline for each tile not already in the tile cache, nearest the centre of the screen first. 4. **Draw coarse under fine.** While a tile loads, draw the matching part of a coarser level scaled up, so the visitor sees a blurry patch that sharpens, never a hole. 5. **Let go of what left.** Cancel or demote requests for tiles that scrolled out, and evict the tiles furthest from the viewport when the tile cache is full. With 512-pixel tiles (an assumption; 256 is also common), a 1,080 x 2,400 viewport at full zoom on level 0 touches at most about 4 x 6 = 24 tiles of about 1 MB each, roughly 25 MB, and a one-tile margin on every side roughly doubles that; add the 4.7 MB overview. Between two levels the chosen one can carry up to two image pixels per screen pixel in each direction, so the count can rise up to about fourfold before the next level takes over - some viewers switch levels earlier and accept slight upscaling to keep it lower. Either way, zooming deeper changes which level is used, not the scale of the problem: **tile memory is bounded by the viewport at every zoom, never by the size of the image**. ## Where the tiles come from | Source | How it works | Fits when | |---|---|---| | **Server-cut pyramid** | The server prepares every level and tile once; the client downloads only the tiles it shows | The image is hosted and the network is weak or metered, as in a gallery | | **Region decode on the phone** | A decoder that supports it reads one rectangle of a local file at a chosen subsample | The image is already on the phone, such as a downloaded or locally captured file | For the museum app on weak signal, the server-cut pyramid is the natural choice: a full-detail scan is likely to be tens of megabytes as a file (an assumption about this collection's scans), and the visitor who zooms into one corner should download that corner. Region decoding needs the whole file locally first and a format and decoder that can read a region efficiently. ## Keeping it smooth - **Decode tiles off the UI thread.** Only drawing a finished tile happens on it. - **Bound the tile cache in bytes.** Count tiles by decoded size, not by number, so a policy change in tile size cannot quietly double memory. - **Prioritise by distance from the centre.** The middle of the screen is where the visitor is looking. - **Do not prefetch the deepest level.** Fetching every full-detail tile up front costs the 1.2 GB problem in data instead of memory. - **Release tiles on memory pressure.** Keep the overview and the tiles on screen; everything else can be rebuilt.
- Why keep the coarse overview in memory while full-detail tiles are showing?It is the fallback beneath every tile. During a fast pan or a zoom jump, new tiles take time to arrive and decode; drawing the overview's matching patch scaled up keeps the screen filled with a blurry painting instead of blank squares. At about 4.7 MB for this painting it is cheap insurance.
- When would you decode regions on the phone instead of fetching pre-cut tiles?When the large image is already on the phone - an offline download or a photo the user captured - so there is no server pyramid to fetch and no network to save. Region decoding still needs the whole file locally and a format and decoder that can read a rectangle efficiently; it trades download size for local storage and decode work.
- How would you choose the tile size?It is a trade-off. Smaller tiles waste less memory at the viewport's edges and arrive sooner one by one, but mean more requests and more per-tile overhead; larger tiles mean fewer requests but more pixels decoded outside the screen. Sizes of 256 or 512 pixels are common practice; measure on the oldest supported phone and the weakest expected network.
A tile pyramid works like a printed atlas: one page shows the whole country, deeper pages show each city in detail, and you only ever have open the pages for the place you are looking at, however thick the atlas is.
- Decode the painting at full size once, then zoom by scaling the view.
- Downsample the whole image to screen size; zooming in just magnifies it.
- Tile memory keeps growing as the visitor zooms deeper, since deeper levels hold more pixels.
- Prefetch every full-detail tile up front so zooming feels instant.