Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
DZI syntax to IIIF
# Take params from the DZI syntax and turn them into an IIIF request
#
# Copyright (C) 2009 CodePlex Foundation
# Copyright (C) 2010-2013 OpenSeadragon contributors
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# - Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# - Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# - Neither the name of CodePlex Foundation nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
import math
# would-be params from dzi syntax, i.e. `/level/x_y.jpg`
level = 9
x = 0
y = 0
# would-be values from iiif info service
img_tile_width = 256
img_tile_height = 256
img_width = 2717
img_height = 3600
# constants
IIIF_ROTATION = '0'
IIIF_QUALITY = 'native.jpg'
## get the scale (level as a decimal)
max_img_dimension = max(img_width, img_height)
max_level = int(math.ceil(math.log(max_img_dimension, 2)))
scale = math.pow(0.5, max_level - level)
## get iiif size
iiif_pct_size = scale * 100
iiif_size = str('pct:' + str(iiif_pct_size))
## iiif region
iiif_region = ''
iiif_tile_size_width = int(math.ceil(img_tile_width / scale))
iiif_tile_size_height = int(math.ceil(img_tile_height / scale))
# image dimensions at this level
level_width = int(math.ceil(img_width * scale))
level_height = int(math.ceil(img_height * scale))
if level_width < img_tile_width or level_height < img_tile_height:
iiif_region = 'full'
else:
iiif_tile_x = x * iiif_tile_size_width + x
iiif_tile_y = y * iiif_tile_size_height + y
iiif_tile_w = min(iiif_tile_size_width, img_width - iiif_tile_x)
iiif_tile_h = min(iiif_tile_size_height, img_height - iiif_tile_y)
bounds = (iiif_tile_x, iiif_tile_y, iiif_tile_w, iiif_tile_h)
iiif_region = ','.join(map(str, bounds))
print '/'.join((iiif_region, iiif_size, IIIF_ROTATION, IIIF_QUALITY))
import math
# DZI to IIIF 2.0
# constants
IIIF_ROTATION = '0'
IIIF_QUALITY = 'default.jpg'
def dzi_to_iiif(level, x_idx, y_idx, img_w, img_h, img_tile_w, image_tile_h):
## get the scale (level as a decimal)
long_dimension = max(img_w, img_h)
max_level = int(math.ceil(math.log(long_dimension, 2)))
print max_level
scale = math.pow(0.5, max_level - level)
## get iiif size
iiif_size = '%d,' % int(math.ceil(img_w * scale))
## iiif region
iiif_region = ''
iiif_tile_size_width = int(math.ceil(img_tile_w / scale))
iiif_tile_size_height = int(math.ceil(img_tile_h / scale))
# image dimensions at this level
level_width = int(math.ceil(img_w * scale))
level_height = int(math.ceil(img_h * scale))
if level_width < img_tile_w or level_height < img_tile_h:
iiif_region = 'full'
else:
iiif_tile_x = x_idx * iiif_tile_size_width + x_idx
iiif_tile_y = y_idx * iiif_tile_size_height + y_idx
iiif_tile_w = min(iiif_tile_size_width, img_w - iiif_tile_x)
iiif_tile_h = min(iiif_tile_size_height, img_h - iiif_tile_y)
bounds = (iiif_tile_x, iiif_tile_y, iiif_tile_w, iiif_tile_h)
iiif_region = ','.join(map(str, bounds))
return '/'.join((iiif_region, iiif_size, IIIF_ROTATION, IIIF_QUALITY))
# would-be params from dzi syntax, i.e. `/level/x_y.jpg`
level = 10
x = 0
y = 0
# would-be values from iiif info service
img_tile_w = 1024
img_tile_h = 1024
img_w = 5434
img_h = 7200
print dzi_to_iiif(level, x, y, img_w, img_h, img_tile_w, img_tile_h)

@jpstroop, we're using this code in the OpenSeadragon project at:

https://github.com/openseadragon/openseadragon/blob/master/src/iiiftilesource.js

We're in the process of making sure all of our code is properly licensed. This version of your code doesn't appear to have any license, which won't work for us. Have you published it elsewhere with an opensource license? If not, would you be willing to add a license here?

Thank you.

If you'd prefer, you can just post a comment in openseadragon/openseadragon#81 giving us permission to distribute this code under the New BSD license.

Thanks.

Owner

jpstroop commented May 13, 2013

@jpstroop, thank you for the license addition, and thanks in the first place for your contribution to OpenSeadragon!

Sorry I didn't respond sooner to this thread... GitHub hadn't informed me that you'd responded. :-(

@jpstroop: I'm sooo sorry, I gave you the wrong link. We're trying to switch to the version in https://github.com/bgilbert/openseadragon/blob/license/LICENSE.txt, which has very slightly different wording. Would you mind updating? We want to make sure we have proper permission from all our contributors.

Thanks so much. 😓

Owner

jpstroop commented May 14, 2013

@bgilbert, @iangilman: no problem, I've changed it up top. I'm not sure that the CodePlex line needs to be in there...but just used the text verbatim. Let me know if I should take it out.

Looks good. Thanks again.

Owner

jpstroop commented Dec 4, 2013

Note: while this served as the basis for the initial OpenSeadragon/IIIF Image API implementation, both OSd and the Image API have changed. Leaving this around for the sake of history, but that's all it is...history.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment