-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #87 from curationexperts/kakadu
Kakadu support
- Loading branch information
Showing
48 changed files
with
1,038 additions
and
267 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
source 'https://rubygems.org' | ||
|
||
gem 'byebug' | ||
# Specify your gem's dependencies in riiif.gemspec | ||
gemspec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
module Riiif | ||
# Represents a IIIF request | ||
class Transformation | ||
attr_reader :crop, :size, :quality, :rotation, :format | ||
def initialize(crop, size, quality, rotation, format) | ||
@crop = crop | ||
@size = size | ||
@quality = quality | ||
@rotation = rotation | ||
@format = format | ||
end | ||
|
||
# Create a clone of this Transformation, scaled by the factor | ||
# @param [Integer] factor the scale for the new transformation | ||
# @return [Transformation] a new transformation, scaled by factor | ||
def reduce(factor) | ||
Transformation.new(crop.dup, | ||
size.reduce(factor), | ||
quality, | ||
rotation, | ||
format) | ||
end | ||
|
||
# Create a clone of this Transformation, without the crop | ||
# @return [Transformation] a new transformation | ||
# TODO: it would be nice if we didn't need image_info | ||
def without_crop(image_info) | ||
Transformation.new(Region::Full.new(image_info), | ||
size.dup, | ||
quality, | ||
rotation, | ||
format) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
module Riiif | ||
# Represents a cropping operation | ||
class Crop | ||
attr_reader :image_info | ||
|
||
# @return [String] a region for imagemagick to decode | ||
# (appropriate for passing to the -crop parameter) | ||
def to_imagemagick | ||
"#{width}x#{height}+#{offset_x}+#{offset_y}" | ||
end | ||
|
||
# @return [String] a region for kakadu to decode | ||
# (appropriate for passing to the -region parameter) | ||
def to_kakadu | ||
"\{#{decimal_offset_y},#{decimal_offset_x}\},\{#{decimal_height},#{decimal_width}\}" | ||
end | ||
|
||
attr_reader :offset_x | ||
|
||
attr_reader :offset_y | ||
|
||
# @return [Integer] the height in pixels | ||
def height | ||
image_info.height | ||
end | ||
|
||
# @return [Integer] the width in pixels | ||
def width | ||
image_info.width | ||
end | ||
|
||
# @return [Float] the fractional height with respect to the original size | ||
def decimal_height(n = height) | ||
n.to_f / image_info.height | ||
end | ||
|
||
# @return [Float] the fractional width with respect to the original size | ||
def decimal_width(n = width) | ||
n.to_f / image_info.width | ||
end | ||
|
||
def decimal_offset_x | ||
offset_x.to_f / image_info.width | ||
end | ||
|
||
def decimal_offset_y | ||
offset_y.to_f / image_info.height | ||
end | ||
|
||
def maintain_aspect_ratio? | ||
(height / width) == (image_info.height / image_info.width) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module Riiif | ||
# Transforms an image using Imagemagick | ||
class ImagemagickTransformer < AbstractTransformer | ||
def command_factory | ||
ImagemagickCommandFactory | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# frozen_string_literal: true | ||
|
||
module Riiif | ||
# Builds a command to run a transformation using Kakadu | ||
class KakaduCommandFactory | ||
class_attribute :external_command | ||
self.external_command = 'kdu_expand' | ||
|
||
# A helper method to instantiate and invoke build | ||
# @param [String] path the location of the file | ||
# @param info [ImageInformation] information about the source | ||
# @param [Transformation] transformation | ||
def initialize(path, info, transformation) | ||
@path = path | ||
@info = info | ||
@transformation = transformation | ||
end | ||
|
||
attr_reader :path, :info, :transformation | ||
|
||
# @param tmp_file [String] the path to the temporary file | ||
# @return [String] a command for running kdu_expand to produce the requested output | ||
def command(tmp_file) | ||
[external_command, quiet, input, threads, region, reduce, output(tmp_file)].join | ||
end | ||
|
||
def reduction_factor | ||
@reduction_factor ||= transformation.size.reduction_factor | ||
end | ||
|
||
private | ||
|
||
def input | ||
" -i #{path}" | ||
end | ||
|
||
def output(output_filename) | ||
" -o #{output_filename}" | ||
end | ||
|
||
def threads | ||
' -num_threads 4' | ||
end | ||
|
||
def quiet | ||
' -quiet' | ||
end | ||
|
||
def region | ||
region_arg = transformation.crop.to_kakadu | ||
" -region \"#{region_arg}\"" if region_arg | ||
end | ||
|
||
# kdu_expand is not capable of arbitrary scaling, but it does | ||
# offer a -reduce argument which is capable of downscaling by | ||
# factors of 2, significantly speeding decompression. We can | ||
# use it if either the percent is <=50, or the height/width | ||
# are <=50% of full size. | ||
def reduce | ||
" -reduce #{reduction_factor}" if reduction_factor && reduction_factor != 0 | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module Riiif | ||
# Creates names for a temporary file | ||
class LinkNameService | ||
def self.create | ||
::File.join(Dir.tmpdir, SecureRandom.uuid) + '.bmp' | ||
end | ||
end | ||
end |
Oops, something went wrong.