IIIF/iiif.io — pull request #250 by zimeon (2014-06-28 00:30:51+00:00)
Title: Add implementation note about calculation of region and size for tiles

Implementation note I promised for #225

I don't recall whether we'd thought of doing this in the same document or as a separate one, I have done it inline and I think it is OK.

There are choices to be made in implementation (e.g. what size image do you return for a 11x11 image at scale 2? Is it 5x5 or 6x6? Seems that "tradition" might dictate 6x6 but that is just one possible choice) so this isn't the only way to do the calculation. In order to be clearcut I have an integer-only algorithm expressed as Python code which I've tested quite carefully. One could make this shorter using Python's ternary/one-line "if" but I think that is less clear, especially for non-Python folks:

```python
# Calculate region parameters /xr,yr,wr,hr/
xr = n*tw*s
yr = m*th*s
wr = (tw*s) if (xr + tw*s <= width) else (width - xr)
hr = (th*s) if (yr + th*s <= height) else (height - yr)
# Calculate size parameters /ws,hs/
s2 = s/2 #use to implement rounding with integer arithmetic
ws = tw if (xr + tw*s <= width) else (width - xr + s2) / s
hs = th if (yr + th*s <= height) else (height - yr + s2) / s
```

The other issue is that where is image API is implemented following the SHOULD of truncating if a region goes over the edge, then one doesn't really need to calculate special region parameters. However, the numbers are needed for the size parameter calculation and by calculating region also one has something that should work with all implementations.