blob: ba34fa8b434363f202628c165ffb42a9a7bec1b4 [file] [log] [blame]
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="Calculate the region that can be copied from top to bottom."><meta name="keywords" content="rust, rustlang, rust-lang, overlay_bounds"><title>overlay_bounds in image::imageops - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceCodePro-Semibold.ttf.woff2"><link rel="stylesheet" href="../../normalize.css"><link rel="stylesheet" href="../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" href="../../ayu.css" disabled><link rel="stylesheet" href="../../dark.css" disabled><link rel="stylesheet" href="../../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../../storage.js"></script><script defer src="sidebar-items.js"></script><script defer src="../../main.js"></script><noscript><link rel="stylesheet" href="../../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../../favicon.svg"></head><body class="rustdoc fn"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle">&#9776;</button><a class="sidebar-logo" href="../../image/index.html"><div class="logo-container"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></div></a><h2></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../../image/index.html"><div class="logo-container"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></div></a><div class="sidebar-elems"><h2><a href="index.html">In image::imageops</a></h2></div></nav><main><div class="width-limiter"><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><a href="../../help.html">?</a></div><div id="settings-menu" tabindex="-1"><a href="../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../wheel.svg"></a></div></div></form></nav><section id="main-content" class="content"><div class="main-heading"><h1 class="fqn">Function <a href="../index.html">image</a>::<wbr><a href="index.html">imageops</a>::<wbr><a class="fn" href="#">overlay_bounds</a><button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard"><img src="../../clipboard.svg" width="19" height="18" alt="Copy item path"></button></h1><span class="out-of-band"><a class="srclink" href="../../src/image/imageops/mod.rs.html#131-146">source</a> · <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">&#x2212;</span>]</a></span></div><div class="item-decl"><pre class="rust fn"><code>pub fn overlay_bounds(<br>&nbsp;&nbsp;&nbsp;&nbsp;(bottom_width, bottom_height): (<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>, <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>),<br>&nbsp;&nbsp;&nbsp;&nbsp;(top_width, top_height): (<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>, <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>),<br>&nbsp;&nbsp;&nbsp;&nbsp;x: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>,<br>&nbsp;&nbsp;&nbsp;&nbsp;y: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a><br>) -&gt; (<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>, <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>)</code></pre></div><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Calculate the region that can be copied from top to bottom.</p>
<p>Given image size of bottom and top image, and a point at which we want to place the top image
onto the bottom image, how large can we be? Have to wary of the following issues:</p>
<ul>
<li>Top might be larger than bottom</li>
<li>Overflows in the computation</li>
<li>Coordinates could be completely out of bounds</li>
</ul>
<p>The main idea is to make use of inequalities provided by the nature of <code>saturing_add</code> and
<code>saturating_sub</code>. These intrinsically validate that all resulting coordinates will be in bounds
for both images.</p>
<p>We want that all these coordinate accesses are safe:</p>
<ol>
<li><code>bottom.get_pixel(x + [0..x_range), y + [0..y_range))</code></li>
<li><code>top.get_pixel([0..x_range), [0..y_range))</code></li>
</ol>
<p>Proof that the function provides the necessary bounds for width. Note that all unaugmented math
operations are to be read in standard arithmetic, not integer arithmetic. Since no direct
integer arithmetic occurs in the implementation, this is unambiguous.</p>
<div class="example-wrap"><pre class="language-text"><code>Three short notes/lemmata:
- Iff `(a - b) &lt;= 0` then `a.saturating_sub(b) = 0`
- Iff `(a - b) &gt;= 0` then `a.saturating_sub(b) = a - b`
- If `a &lt;= c` then `a.saturating_sub(b) &lt;= c.saturating_sub(b)`
1.1 We show that if `bottom_width &lt;= x`, then `x_range = 0` therefore `x + [0..x_range)` is empty.
x_range
= (top_width.saturating_add(x).min(bottom_width)).saturating_sub(x)
&lt;= bottom_width.saturating_sub(x)
bottom_width &lt;= x
&lt;==&gt; bottom_width - x &lt;= 0
&lt;==&gt; bottom_width.saturating_sub(x) = 0
==&gt; x_range &lt;= 0
==&gt; x_range = 0
1.2 If `x &lt; bottom_width` then `x + x_range &lt; bottom_width`
x + x_range
&lt;= x + bottom_width.saturating_sub(x)
= x + (bottom_width - x)
= bottom_width
2. We show that `x_range &lt;= top_width`
x_range
= (top_width.saturating_add(x).min(bottom_width)).saturating_sub(x)
&lt;= top_width.saturating_add(x).saturating_sub(x)
&lt;= (top_wdith + x).saturating_sub(x)
= top_width (due to `top_width &gt;= 0` and `x &gt;= 0`)</code></pre></div>
<p>Proof is the same for height.</p>
</div></details></section></div></main><div id="rustdoc-vars" data-root-path="../../" data-current-crate="image" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.66.0-nightly (5c8bff74b 2022-10-21)" ></div></body></html>