proofsheet 0.1.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,48 @@
1
+ host: "https://example.com"
2
+
3
+ defaults:
4
+ viewport: [800, 500]
5
+ output_dir: "examples/output"
6
+
7
+ shots:
8
+ web_page:
9
+ path: "/"
10
+
11
+ images:
12
+ cat_badge:
13
+ url: "https://placecats.com/300/300"
14
+
15
+ studio_logo:
16
+ path: "examples/assets/logo480.png"
17
+
18
+ compositions:
19
+ showcase:
20
+ canvas: [1200, 750]
21
+ background:
22
+ gradient: ["#1e293b", "#0f172a"]
23
+ angle: 135
24
+ layers:
25
+ - shot: web_page
26
+ x: 80
27
+ y: 100
28
+ width: 680
29
+ radius: 16
30
+ rotate: -2
31
+ shadow: true
32
+
33
+ - image: cat_badge
34
+ x: 740
35
+ y: 180
36
+ width: 360
37
+ radius: 24
38
+ rotate: 3
39
+ shadow:
40
+ x: 4
41
+ y: 20
42
+ blur: 28
43
+ color: "#00000066"
44
+
45
+ - image: studio_logo
46
+ x: 1040
47
+ y: 40
48
+ width: 100
@@ -0,0 +1,12 @@
1
+ <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2
+ <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
3
+ <svg width="100%" height="100%" viewBox="0 0 512 512" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" xmlns:serif="http://www.serif.com/" style="fill-rule:evenodd;clip-rule:evenodd;stroke-linejoin:round;stroke-miterlimit:2;">
4
+ <g id="Logo" transform="matrix(2.056225,0,0,2.056225,-357.783133,-252.915663)">
5
+ <g transform="matrix(0.427835,0,0,0.427835,169.293814,116.582474)">
6
+ <path d="M593,51.375L593,560.625C593,580.701 576.701,597 556.625,597L47.375,597C27.299,597 11,580.701 11,560.625L11,51.375C11,31.299 27.299,15 47.375,15L556.625,15C576.701,15 593,31.299 593,51.375Z" style="fill:rgb(90,165,121);"/>
7
+ </g>
8
+ <g transform="matrix(0.300091,0,0,0.300091,148.942642,125.47191)">
9
+ <path d="M480.43,409.571C482.804,411.34 496.854,411.063 497.503,411.065C536.617,411.232 538.336,410.803 538.813,413.41C542.251,432.23 542.779,478.152 605.501,489.493C622.431,492.554 681.998,483.868 686.819,416.53C687.095,412.676 686.833,411.358 691.5,411.353C704.941,411.338 857.598,411.166 859.518,411.403C861.799,411.685 861.517,418.289 861.115,439.483C861.11,439.758 860.948,443.156 860.929,443.529C859.992,462.066 857.175,485.869 855.291,487.198C851.771,489.68 768.523,477.184 765.595,479.64C763.626,481.293 742.297,540.583 740.283,545.41C737.143,552.936 734.552,553.951 740.013,559.973C754.633,576.093 798.524,618.653 798.191,622.462C798.001,624.623 785.832,643.378 759.754,669.751C755.457,674.096 707.502,722.593 699.788,717.078C696.684,714.859 646.141,649.658 643.627,649.007C641.612,648.486 627.08,656.463 602.71,666.971C599.8,668.227 568.688,681.642 566.854,683.83C566.191,684.621 566.47,684.692 566.147,689.493C565.76,689.862 565.903,694.678 565.928,699.505C566.154,742.37 570.023,773.514 565.634,774.903C564.047,775.405 547.482,776.7 542.518,777.614C537.141,778.605 523.24,778.957 520.997,778.995C520.836,778.913 514.382,779.309 512.5,779.263C487.496,778.649 486.645,779.034 484.496,778.935C459.389,777.777 459.525,777.091 434.549,775.058C427.46,774.481 428.973,770.487 429.41,762.494C429.904,753.456 436.265,699.061 436.909,693.551C437.75,686.362 440.307,684.145 433.472,681.57C428.83,679.821 389.678,665.071 375.397,659.817C356.172,652.743 355.705,651.926 353.727,653.763C345.294,661.595 295.121,720.004 292.493,720.469C289.819,720.943 289.753,720.216 267.506,697.495C232.556,661.799 194.714,625.947 194.933,622.535C195.074,620.349 202.326,614.273 216.41,602.393C235.578,586.223 266.592,564.428 265.713,561.45C264.321,556.742 263.173,557.274 240.81,505.362C231.888,484.651 230.873,481.14 227.465,480.89C215.903,480.039 143.406,487.805 140.959,485.093C137.639,481.414 133.219,413.222 136.531,411.567C136.815,411.425 136.827,411.317 222.5,411.23C299.79,411.152 300.706,411.196 307.498,411.521C314.895,411.875 302.891,441.785 336.913,473.052C366.024,499.805 422.384,493.798 445.086,457.249C465.905,423.732 407.109,328.997 399.089,271.578C398.442,266.949 381.703,272.626 360.589,256.389C344.028,243.654 334.306,219.37 320.518,184.493C317.951,177.998 322.492,179.413 329.46,179.96C371.857,183.285 383.697,217.424 396.841,238.199C402.049,246.431 404.417,195.845 416.933,165.678C466.389,46.47 568.273,63.725 620.454,57.198C644.542,54.186 661.914,53.574 682.917,34.99C689.78,28.917 685.167,52.432 678.1,66.3C632.194,156.379 542.043,161.88 518.51,167.547C494.84,173.247 449.898,171.738 437.246,213.424C429.752,238.116 431.337,273.678 433.497,273.651C437.088,273.605 446.762,238.734 490.466,224.402C502.272,220.53 526.095,220.521 527.076,222.636C527.472,223.49 528.529,257.279 517.05,279.264C493.703,323.977 438.229,298.977 441.171,308.573C443.249,315.353 444.075,314.97 472.98,391.319C479.452,408.415 479.165,408.628 480.43,409.571Z" style="fill:rgb(33,16,2);"/>
10
+ </g>
11
+ </g>
12
+ </svg>
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -84,6 +84,17 @@ module Proofsheet
84
84
  @session.driver.browser.screenshot_as(:png)
85
85
  end
86
86
 
87
+ def image_url_for(selector)
88
+ node = @session.find(selector, match: :first)
89
+ tag_name = node.tag_name.downcase
90
+ raise Error, "#{selector.inspect} selected <#{tag_name}>, expected <img>" unless tag_name == "img"
91
+
92
+ url = @session.evaluate_script("arguments[0].currentSrc || arguments[0].src", node)
93
+ raise Error, "#{selector.inspect} selected an image without a source" if url.nil? || url.empty?
94
+
95
+ url
96
+ end
97
+
87
98
  def rect_for(selector, margin: 0)
88
99
  node = @session.find(selector, match: :first)
89
100
  @session.execute_script(SCROLL_INTO_VIEW, node, margin)
@@ -5,28 +5,59 @@ require "pathname"
5
5
 
6
6
  module Proofsheet
7
7
  class Capturer
8
- def initialize(manifest:, host:, root: Dir.pwd, credentials: nil, browser_class: Browser, out: $stdout)
8
+ def initialize(manifest:, host:, root: Dir.pwd, credentials: nil, out: $stdout, **collaborators)
9
9
  @manifest = manifest
10
10
  @host = host
11
11
  @root = Pathname(root)
12
12
  @credentials = credentials
13
- @browser_class = browser_class
14
13
  @out = out
14
+ @browser_class = collaborators.fetch(:browser_class, Browser)
15
+ @fetcher = collaborators.fetch(:fetcher, Download)
15
16
  end
16
17
 
17
- def capture(names = @manifest.names)
18
- shots = names.map { |name| @manifest.shot(name) }
18
+ def capture(names = @manifest.names + @manifest.image_names)
19
+ shots, images = split(names)
19
20
  username, password = authentication
20
21
 
21
- @browser = @browser_class.new(host: @host, viewport: @manifest.viewport).start
22
- sign_in(username, password) if @manifest.login
23
- shots.each { |shot| capture_shot(shot) }
22
+ images.select(&:url).each { |image| download(image) }
23
+ browse(shots, images.select(&:page), username, password)
24
24
  ensure
25
25
  @browser&.quit
26
26
  end
27
27
 
28
28
  private
29
29
 
30
+ def split(names)
31
+ images, shots = names.partition { |name| @manifest.image_names.include?(name) }
32
+ [shots.map { |name| @manifest.shot(name) }, images.map { |name| @manifest.image(name) }]
33
+ end
34
+
35
+ def browse(shots, pages, username, password)
36
+ return if shots.empty? && pages.empty?
37
+
38
+ @browser = @browser_class.new(host: @host, viewport: @manifest.viewport).start
39
+ sign_in(username, password) if @manifest.login
40
+ shots.each { |shot| capture_shot(shot) }
41
+ pages.each { |image| download_page_image(image) }
42
+ end
43
+
44
+ def download(image, url = image.url)
45
+ @out.puts image.name
46
+ write(image, downloaded_png(image, url))
47
+ end
48
+
49
+ def download_page_image(image)
50
+ @browser.visit(image.page)
51
+ download(image, @browser.image_url_for(image.selector))
52
+ end
53
+
54
+ def downloaded_png(image, url)
55
+ require "vips"
56
+ Vips::Image.new_from_buffer(@fetcher.call(url), "").write_to_buffer(".png")
57
+ rescue Vips::Error => e
58
+ raise Error, "#{image.name}: downloaded data is not a supported image: #{e.message}"
59
+ end
60
+
30
61
  def authentication
31
62
  return [nil, nil] unless @manifest.login
32
63
 
@@ -4,29 +4,30 @@ require "optparse"
4
4
 
5
5
  module Proofsheet
6
6
  class CLI
7
- USAGE = "Usage: proofsheet <capture [SHOT ...] | list> [--config PATH] [--host HOST]"
7
+ USAGE = "Usage: proofsheet <capture [SHOT ...] | compose [COMPOSITION ...] | list> " \
8
+ "[--config PATH] [--host HOST]"
8
9
 
9
- def initialize(argv, out: $stdout, err: $stderr, root: Dir.pwd, manifest_loader: Manifest.method(:load),
10
- capturer_class: Capturer)
10
+ def initialize(argv, out: $stdout, err: $stderr, root: Dir.pwd, manifest_loader: Manifest.method(:load), **classes)
11
11
  @argv = argv.dup
12
12
  @out = out
13
13
  @err = err
14
14
  @root = root
15
15
  @manifest_loader = manifest_loader
16
- @capturer_class = capturer_class
16
+ @capturer_class = classes.fetch(:capturer_class, Capturer)
17
+ @composer_class = classes.fetch(:composer_class, Composer)
17
18
  end
18
19
 
19
20
  def run
20
21
  command = @argv.shift
21
22
  return help(0) if %w[help --help -h].include?(command)
22
23
  return version if %w[--version -v].include?(command)
23
- return help(1) unless %w[capture list].include?(command)
24
+ return help(1) unless %w[capture compose list].include?(command)
24
25
 
25
26
  options = { config: Manifest::DEFAULT_PATH }
26
27
  parser(options).parse!(@argv)
27
28
  manifest = @manifest_loader.call(options[:config])
28
29
 
29
- command == "list" ? list(manifest) : capture(manifest, options)
30
+ dispatch(command, manifest, options)
30
31
  0
31
32
  rescue OptionParser::ParseError, Error, KeyError => e
32
33
  @err.puts "proofsheet: #{e.message}"
@@ -35,6 +36,13 @@ module Proofsheet
35
36
 
36
37
  private
37
38
 
39
+ def dispatch(command, manifest, options)
40
+ return list(manifest) if command == "list"
41
+ return compose(manifest) if command == "compose"
42
+
43
+ capture(manifest, options)
44
+ end
45
+
38
46
  def parser(options)
39
47
  OptionParser.new do |opts|
40
48
  opts.on("-c", "--config PATH") { |path| options[:config] = path }
@@ -55,12 +63,25 @@ module Proofsheet
55
63
  credentials = Credentials.new(manifest.credentials) if manifest.login
56
64
  capturer = @capturer_class.new(manifest: manifest, host: host, root: @root,
57
65
  credentials: credentials, out: @out)
58
- names = @argv.empty? ? manifest.names : @argv
66
+ names = capture_names(manifest)
59
67
  @out.puts "Capturing #{names.size} #{names.size == 1 ? "shot" : "shots"} from #{host}"
60
68
  capturer.capture(names)
61
69
  @out.puts "Done."
62
70
  end
63
71
 
72
+ def capture_names(manifest)
73
+ return @argv unless @argv.empty?
74
+
75
+ manifest.names + manifest.image_names
76
+ end
77
+
78
+ def compose(manifest)
79
+ names = @argv.empty? ? manifest.composition_names : @argv
80
+ @out.puts "Composing #{names.size} #{names.size == 1 ? "image" : "images"}"
81
+ @composer_class.new(manifest: manifest, root: @root, out: @out).compose(names)
82
+ @out.puts "Done."
83
+ end
84
+
64
85
  def help(status)
65
86
  (status.zero? ? @out : @err).puts USAGE
66
87
  status
@@ -0,0 +1,132 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "fileutils"
4
+ require "pathname"
5
+ require "vips"
6
+
7
+ module Proofsheet
8
+ class Composer
9
+ def initialize(manifest:, root: Dir.pwd, out: $stdout)
10
+ @manifest = manifest
11
+ @root = Pathname(root)
12
+ @out = out
13
+ end
14
+
15
+ def compose(names = @manifest.composition_names)
16
+ names.each { |name| compose_one(@manifest.composition(name)) }
17
+ end
18
+
19
+ private
20
+
21
+ def compose_one(composition)
22
+ @out.puts composition.name
23
+ image = background_image(composition)
24
+ composition.layers.each { |layer| image = add_layer(image, layer) }
25
+ write(composition, image)
26
+ end
27
+
28
+ def background_image(composition)
29
+ spec = composition.background
30
+ return solid(composition.width, composition.height, spec.color) if spec.color
31
+ return gradient(composition.width, composition.height, spec.gradient, spec.angle) if spec.gradient
32
+ return cover_image(spec.image, composition.width, composition.height) if spec.image
33
+
34
+ raise Error, "#{composition.name}: background must set color, gradient, or image"
35
+ end
36
+
37
+ def add_layer(canvas, layer)
38
+ image = layer_image(layer)
39
+ canvas = add_shadow(canvas, image, layer) if layer.shadow
40
+ canvas.composite2(image, :over, x: layer.x, y: layer.y)
41
+ end
42
+
43
+ def layer_image(layer)
44
+ image = load_image(@root.join(@manifest.layer_path(layer)))
45
+ image = Cropper.call(image, layer.crop) if layer.crop
46
+ image = image.resize(layer.width.to_f / image.width) if layer.width
47
+ image = RoundedCorners.call(image, layer.radius)
48
+ image = image.rotate(layer.rotate, background: [0, 0, 0, 0]) unless layer.rotate.zero?
49
+ image
50
+ end
51
+
52
+ def add_shadow(canvas, image, layer)
53
+ shadow = layer.shadow
54
+ alpha, padding = shadow_mask(image, shadow.blur)
55
+ overlay = shadow_overlay(alpha, shadow.color)
56
+ canvas.composite2(overlay, :over, x: layer.x + shadow.x - padding,
57
+ y: layer.y + shadow.y - padding)
58
+ end
59
+
60
+ def shadow_mask(image, blur)
61
+ padding = (blur * 3).ceil
62
+ alpha = image.extract_band(3).embed(padding, padding, image.width + (padding * 2),
63
+ image.height + (padding * 2), extend: :black)
64
+ alpha = alpha.gaussblur(blur) if blur.positive?
65
+ [alpha, padding]
66
+ end
67
+
68
+ def shadow_overlay(alpha, value)
69
+ color = parse_color(value)
70
+ opacity = color.pop
71
+ rgb = solid(alpha.width, alpha.height, color).extract_band(0, n: 3)
72
+ rgb.bandjoin(alpha * (opacity / 255.0))
73
+ end
74
+
75
+ def solid(width, height, color)
76
+ channels = color.is_a?(Array) ? color : parse_color(color)
77
+ rgb = Vips::Image.black(width, height).new_from_image(channels.first(3)).copy(interpretation: :srgb)
78
+ rgb.bandjoin(channels.fetch(3, 255))
79
+ end
80
+
81
+ def gradient(width, height, colors, angle)
82
+ raise Error, "gradient must contain exactly two colors" unless colors&.size == 2
83
+
84
+ from, to = colors.map { |color| parse_color(color) }
85
+ amount = gradient_amount(width, height, angle)
86
+ channels = from.zip(to).map { |start, finish| (amount * (finish - start)) + start }
87
+ channels.shift.bandjoin(channels).cast(:uchar).copy(interpretation: :srgb)
88
+ end
89
+
90
+ def gradient_amount(width, height, degrees)
91
+ coordinates = Vips::Image.xyz(width, height)
92
+ radians = degrees * Math::PI / 180
93
+ projection = (coordinates[0] * Math.cos(radians)) + (coordinates[1] * Math.sin(radians))
94
+ minimum = projection.min
95
+ maximum = projection.max
96
+ return Vips::Image.black(width, height) if minimum == maximum
97
+
98
+ (projection - minimum) / (maximum - minimum)
99
+ end
100
+
101
+ def cover_image(path, width, height)
102
+ image = load_image(@root.join(path))
103
+ scale = [width.to_f / image.width, height.to_f / image.height].max
104
+ image = image.resize(scale)
105
+ image.crop((image.width - width) / 2, (image.height - height) / 2, width, height)
106
+ end
107
+
108
+ def load_image(path)
109
+ raise Error, "image not found: #{path}" unless path.exist?
110
+
111
+ image = Vips::Image.new_from_file(path.to_s, access: :sequential)
112
+ image = image.colourspace(:srgb) unless image.interpretation == :srgb
113
+ image.has_alpha? ? image : image.bandjoin(255)
114
+ end
115
+
116
+ def parse_color(value)
117
+ match = /\A#([0-9a-f]{6})([0-9a-f]{2})?\z/i.match(value.to_s)
118
+ raise Error, "invalid color #{value.inspect}; use #RRGGBB or #RRGGBBAA" unless match
119
+
120
+ (match[1].scan(/../) + [match[2] || "ff"]).map { |channel| channel.to_i(16) }
121
+ end
122
+
123
+ def output_path(filename) = @root.join(@manifest.output_dir, filename)
124
+
125
+ def write(composition, image)
126
+ path = output_path(composition.filename)
127
+ FileUtils.mkdir_p(path.dirname)
128
+ image.write_to_file(path.to_s)
129
+ @out.puts " wrote #{path.relative_path_from(@root)}"
130
+ end
131
+ end
132
+ end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Proofsheet
4
+ class CompositionConfig
5
+ Background = Data.define(:color, :gradient, :image, :angle)
6
+ Shadow = Data.define(:x, :y, :blur, :color)
7
+ Crop = Data.define(:x, :y, :width, :height)
8
+ Layer = Data.define(:shot, :image, :x, :y, :crop, :width, :radius, :rotate, :shadow)
9
+ Composition = Data.define(:name, :width, :height, :background, :layers) do
10
+ def filename
11
+ "#{name}.png"
12
+ end
13
+ end
14
+
15
+ def initialize(data, path:)
16
+ @data = data
17
+ @path = path
18
+ end
19
+
20
+ def names
21
+ @data.keys
22
+ end
23
+
24
+ def fetch(name)
25
+ raw = @data[name]
26
+ raise Error, unknown_message(name) if raw.nil?
27
+
28
+ width, height = raw.fetch("canvas")
29
+ Composition.new(name: name, width: width, height: height,
30
+ background: background(raw.fetch("background", "#ffffff")),
31
+ layers: raw.fetch("layers").map { |layer| layer_spec(layer, name) })
32
+ rescue KeyError => e
33
+ raise Error, "#{name}: #{e.message} in #{@path}"
34
+ end
35
+
36
+ private
37
+
38
+ def unknown_message(name)
39
+ "unknown composition #{name.inspect} — known compositions: #{names.join(", ")}"
40
+ end
41
+
42
+ def background(raw)
43
+ return Background.new(color: raw, gradient: nil, image: nil, angle: 0) if raw.is_a?(String)
44
+
45
+ Background.new(color: raw["color"], gradient: raw["gradient"], image: raw["image"],
46
+ angle: raw.fetch("angle", 0))
47
+ end
48
+
49
+ def layer_spec(raw, name)
50
+ source = [raw["shot"], raw["image"]].compact
51
+ raise Error, "#{name}: a layer must set exactly one of shot or image" unless source.one?
52
+
53
+ Layer.new(shot: raw["shot"], image: raw["image"], x: raw.fetch("x", 0), y: raw.fetch("y", 0),
54
+ crop: crop(raw["crop"]), width: raw["width"], radius: raw.fetch("radius", 0),
55
+ rotate: raw.fetch("rotate", 0), shadow: shadow(raw["shadow"]))
56
+ end
57
+
58
+ def crop(raw)
59
+ return if raw.nil?
60
+
61
+ Crop.new(x: raw.fetch("x"), y: raw.fetch("y"),
62
+ width: raw.fetch("width"), height: raw.fetch("height"))
63
+ end
64
+
65
+ def shadow(raw)
66
+ return if raw.nil? || raw == false
67
+
68
+ raw = {} if raw == true
69
+ Shadow.new(x: raw.fetch("x", 0), y: raw.fetch("y", 16), blur: raw.fetch("blur", 24),
70
+ color: raw.fetch("color", "#00000055"))
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Proofsheet
4
+ class Cropper
5
+ def self.call(image, spec)
6
+ new(image, spec).call
7
+ end
8
+
9
+ def initialize(image, spec)
10
+ @image = image
11
+ @spec = spec
12
+ end
13
+
14
+ def call
15
+ raise Error, "crop #{@spec.to_h} exceeds source image #{@image.width}×#{@image.height}" unless valid?
16
+
17
+ @image.crop(@spec.x, @spec.y, @spec.width, @spec.height)
18
+ end
19
+
20
+ private
21
+
22
+ def valid?
23
+ @spec.x >= 0 && @spec.y >= 0 && positive_dimensions? && within_bounds?
24
+ end
25
+
26
+ def positive_dimensions?
27
+ @spec.width.positive? && @spec.height.positive?
28
+ end
29
+
30
+ def within_bounds?
31
+ @spec.x + @spec.width <= @image.width && @spec.y + @spec.height <= @image.height
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "open-uri"
4
+ require "socket"
5
+
6
+ module Proofsheet
7
+ class Download
8
+ FAILURES = [URI::InvalidURIError, OpenURI::HTTPError, RuntimeError, SystemCallError, SocketError].freeze
9
+
10
+ def self.call(url)
11
+ new(url).call
12
+ end
13
+
14
+ def initialize(url)
15
+ @url = url.to_s
16
+ end
17
+
18
+ def call
19
+ uri = URI.parse(@url)
20
+ raise Error, "#{@url.inspect} is not an http or https URL" unless uri.is_a?(URI::HTTP)
21
+
22
+ uri.read
23
+ rescue *FAILURES => e
24
+ raise Error, "could not fetch #{@url}: #{e.message}"
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Proofsheet
4
+ class ImageConfig
5
+ SOURCES = %w[path url page].freeze
6
+
7
+ Image = Data.define(:name, :path, :url, :page, :selector) do
8
+ def filename = "#{name}.png"
9
+ def local? = !path.nil?
10
+ end
11
+
12
+ def initialize(data, path:)
13
+ @data = data
14
+ @path = path
15
+ end
16
+
17
+ def names
18
+ @data.keys
19
+ end
20
+
21
+ def fetch(name)
22
+ raw = @data[name]
23
+ raise Error, "unknown image #{name.inspect} — known images: #{names.join(", ")}" if raw.nil?
24
+
25
+ sources = SOURCES.select { |key| raw[key] }
26
+ raise Error, "#{name}: set exactly one of path, url, or page in #{@path}" unless sources.one?
27
+
28
+ validate_selector(name, raw)
29
+
30
+ Image.new(name: name, path: raw["path"], url: raw["url"], page: raw["page"],
31
+ selector: raw["selector"])
32
+ end
33
+
34
+ private
35
+
36
+ def validate_selector(name, raw)
37
+ raise Error, "#{name}: page requires selector in #{@path}" if raw["page"] && !raw["selector"]
38
+ return unless raw["selector"] && !raw["page"]
39
+
40
+ raise Error, "#{name}: selector can only be used with page in #{@path}"
41
+ end
42
+ end
43
+ end
@@ -73,6 +73,23 @@ module Proofsheet
73
73
  names.map { |name| shot(name) }
74
74
  end
75
75
 
76
+ def image_names = image_config.names
77
+
78
+ def image(name) = image_config.fetch(name)
79
+
80
+ def layer_path(layer)
81
+ return File.join(output_dir, "#{layer.shot}.png") if layer.shot
82
+
83
+ source = image(layer.image)
84
+ source.local? ? source.path : File.join(output_dir, source.filename)
85
+ end
86
+
87
+ def composition_names = composition_config.names
88
+
89
+ def composition(name)
90
+ composition_config.fetch(name)
91
+ end
92
+
76
93
  def shot(name)
77
94
  raw = shot_data[name]
78
95
  raise Error, "unknown shot #{name.inspect} — known shots: #{names.join(", ")}" if raw.nil?
@@ -124,5 +141,22 @@ module Proofsheet
124
141
  rescue KeyError
125
142
  raise Error, "shots is missing from #{@path}"
126
143
  end
144
+
145
+ def image_config
146
+ @image_config ||= ImageConfig.new(@data.fetch("images", {}), path: @path).tap do |config|
147
+ reject_clashes(config.names)
148
+ end
149
+ end
150
+
151
+ def reject_clashes(names)
152
+ clashes = names & shot_data.keys
153
+ return if clashes.empty?
154
+
155
+ raise Error, "#{clashes.join(", ")} named as both a shot and an image in #{@path}"
156
+ end
157
+
158
+ def composition_config
159
+ @composition_config ||= CompositionConfig.new(@data.fetch("compositions", {}), path: @path)
160
+ end
127
161
  end
128
162
  end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Proofsheet
4
+ class RoundedCorners
5
+ def self.call(image, requested_radius)
6
+ return image unless requested_radius.positive?
7
+
8
+ new(image, requested_radius).call
9
+ end
10
+
11
+ def initialize(image, requested_radius)
12
+ @image = image
13
+ @radius = [requested_radius.to_f, image.width / 2.0, image.height / 2.0].min
14
+ end
15
+
16
+ def call
17
+ alpha = (@image.extract_band(3) * (coverage / 255.0)).cast(:uchar)
18
+ @image.extract_band(0, n: 3).bandjoin(alpha)
19
+ end
20
+
21
+ private
22
+
23
+ def coverage
24
+ distance = ((x_distance * x_distance) + (y_distance * y_distance))**0.5
25
+ value = (((distance * -1) + @radius + 0.5) * 255)
26
+ value = value.relational_const(:more, [255]).ifthenelse(255, value)
27
+ value.relational_const(:less, [0]).ifthenelse(0, value)
28
+ end
29
+
30
+ def x_distance
31
+ corner_distance(coordinates[0], @image.width)
32
+ end
33
+
34
+ def y_distance
35
+ corner_distance(coordinates[1], @image.height)
36
+ end
37
+
38
+ def corner_distance(coordinate, length)
39
+ distance = (coordinate - ((length - 1) / 2.0)).abs - ((length / 2.0) - @radius)
40
+ distance.relational_const(:more, [0]).ifthenelse(distance, 0)
41
+ end
42
+
43
+ def coordinates
44
+ @coordinates ||= Vips::Image.xyz(@image.width, @image.height)
45
+ end
46
+ end
47
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Proofsheet
4
- VERSION = "0.1.0"
4
+ VERSION = "0.3.0"
5
5
  end