Files
phy/image/trait.ImageDecoder.html
Orion Kindel 0ce894e6b0 doc
2025-03-18 10:30:23 -05:00

51 lines
22 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!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="The trait that all decoders implement"><title>ImageDecoder in image - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-6b053e98.ttf.woff2,FiraSans-Regular-0fe48ade.woff2,FiraSans-Medium-e1aa3f0a.woff2,SourceCodePro-Regular-8badfe75.ttf.woff2,SourceCodePro-Semibold-aa29a496.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../static.files/normalize-9960930a.css"><link rel="stylesheet" href="../static.files/rustdoc-42caa33d.css"><meta name="rustdoc-vars" data-root-path="../" data-static-root-path="../static.files/" data-current-crate="image" data-themes="" data-resource-suffix="" data-rustdoc-version="1.84.0 (9fc6b4312 2025-01-07)" data-channel="1.84.0" data-search-js="search-92e6798f.js" data-settings-js="settings-0f613d39.js" ><script src="../static.files/storage-59e33391.js"></script><script defer src="sidebar-items.js"></script><script defer src="../static.files/main-5f194d8c.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-893ab5e7.css"></noscript><link rel="alternate icon" type="image/png" href="../static.files/favicon-32x32-6580c154.png"><link rel="icon" type="image/svg+xml" href="../static.files/favicon-044be391.svg"></head><body class="rustdoc trait"><!--[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" title="show sidebar"></button></nav><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../image/index.html">image</a><span class="version">0.25.2</span></h2></div><div class="sidebar-elems"><section id="rustdoc-toc"><h2 class="location"><a href="#">Image<wbr>Decoder</a></h2><h3><a href="#required-methods">Required Methods</a></h3><ul class="block"><li><a href="#tymethod.color_type" title="color_type">color_type</a></li><li><a href="#tymethod.dimensions" title="dimensions">dimensions</a></li><li><a href="#tymethod.read_image" title="read_image">read_image</a></li><li><a href="#tymethod.read_image_boxed" title="read_image_boxed">read_image_boxed</a></li></ul><h3><a href="#provided-methods">Provided Methods</a></h3><ul class="block"><li><a href="#method.icc_profile" title="icc_profile">icc_profile</a></li><li><a href="#method.original_color_type" title="original_color_type">original_color_type</a></li><li><a href="#method.set_limits" title="set_limits">set_limits</a></li><li><a href="#method.total_bytes" title="total_bytes">total_bytes</a></li></ul><h3><a href="#foreign-impls">Implementations on Foreign Types</a></h3><ul class="block"><li><a href="#impl-ImageDecoder-for-Box%3CT%3E" title="Box&#60;T&#62;">Box&#60;T&#62;</a></li></ul><h3><a href="#implementors">Implementors</a></h3></section><div id="rustdoc-modnav"><h2 class="in-crate"><a href="index.html">In crate image</a></h2></div></div></nav><div class="sidebar-resizer"></div><main><div class="width-limiter"><rustdoc-search></rustdoc-search><section id="main-content" class="content"><div class="main-heading"><span class="rustdoc-breadcrumbs"><a href="index.html">image</a></span><h1>Trait <span class="trait">ImageDecoder</span><button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><rustdoc-toolbar></rustdoc-toolbar><span class="sub-heading"><a class="src" href="../src/image/image.rs.html#612-698">Source</a> </span></div><pre class="rust item-decl"><code>pub trait ImageDecoder {
// Required methods
fn <a href="#tymethod.dimensions" class="fn">dimensions</a>(&amp;self) -&gt; (<a class="primitive" href="https://doc.rust-lang.org/1.84.0/std/primitive.u32.html">u32</a>, <a class="primitive" href="https://doc.rust-lang.org/1.84.0/std/primitive.u32.html">u32</a>);
<span class="item-spacer"></span> fn <a href="#tymethod.color_type" class="fn">color_type</a>(&amp;self) -&gt; <a class="enum" href="enum.ColorType.html" title="enum image::ColorType">ColorType</a>;
<span class="item-spacer"></span> fn <a href="#tymethod.read_image" class="fn">read_image</a>(self, buf: &amp;mut [<a class="primitive" href="https://doc.rust-lang.org/1.84.0/std/primitive.u8.html">u8</a>]) -&gt; <a class="type" href="error/type.ImageResult.html" title="type image::error::ImageResult">ImageResult</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/1.84.0/std/primitive.unit.html">()</a>&gt;
<span class="where">where Self: <a class="trait" href="https://doc.rust-lang.org/1.84.0/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a></span>;
<span class="item-spacer"></span> fn <a href="#tymethod.read_image_boxed" class="fn">read_image_boxed</a>(self: <a class="struct" href="https://doc.rust-lang.org/1.84.0/alloc/boxed/struct.Box.html" title="struct alloc::boxed::Box">Box</a>&lt;Self&gt;, buf: &amp;mut [<a class="primitive" href="https://doc.rust-lang.org/1.84.0/std/primitive.u8.html">u8</a>]) -&gt; <a class="type" href="error/type.ImageResult.html" title="type image::error::ImageResult">ImageResult</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/1.84.0/std/primitive.unit.html">()</a>&gt;;
// Provided methods
fn <a href="#method.original_color_type" class="fn">original_color_type</a>(&amp;self) -&gt; <a class="enum" href="enum.ExtendedColorType.html" title="enum image::ExtendedColorType">ExtendedColorType</a> { ... }
<span class="item-spacer"></span> fn <a href="#method.icc_profile" class="fn">icc_profile</a>(&amp;mut self) -&gt; <a class="type" href="error/type.ImageResult.html" title="type image::error::ImageResult">ImageResult</a>&lt;<a class="enum" href="https://doc.rust-lang.org/1.84.0/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;<a class="struct" href="https://doc.rust-lang.org/1.84.0/alloc/vec/struct.Vec.html" title="struct alloc::vec::Vec">Vec</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/1.84.0/std/primitive.u8.html">u8</a>&gt;&gt;&gt; { ... }
<span class="item-spacer"></span> fn <a href="#method.total_bytes" class="fn">total_bytes</a>(&amp;self) -&gt; <a class="primitive" href="https://doc.rust-lang.org/1.84.0/std/primitive.u64.html">u64</a> { ... }
<span class="item-spacer"></span> fn <a href="#method.set_limits" class="fn">set_limits</a>(&amp;mut self, limits: <a class="struct" href="struct.Limits.html" title="struct image::Limits">Limits</a>) -&gt; <a class="type" href="error/type.ImageResult.html" title="type image::error::ImageResult">ImageResult</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/1.84.0/std/primitive.unit.html">()</a>&gt; { ... }
}</code></pre><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>The trait that all decoders implement</p>
</div></details><h2 id="required-methods" class="section-header">Required Methods<a href="#required-methods" class="anchor">§</a></h2><div class="methods"><details class="toggle method-toggle" open><summary><section id="tymethod.dimensions" class="method"><a class="src rightside" href="../src/image/image.rs.html#614">Source</a><h4 class="code-header">fn <a href="#tymethod.dimensions" class="fn">dimensions</a>(&amp;self) -&gt; (<a class="primitive" href="https://doc.rust-lang.org/1.84.0/std/primitive.u32.html">u32</a>, <a class="primitive" href="https://doc.rust-lang.org/1.84.0/std/primitive.u32.html">u32</a>)</h4></section></summary><div class="docblock"><p>Returns a tuple containing the width and height of the image</p>
</div></details><details class="toggle method-toggle" open><summary><section id="tymethod.color_type" class="method"><a class="src rightside" href="../src/image/image.rs.html#617">Source</a><h4 class="code-header">fn <a href="#tymethod.color_type" class="fn">color_type</a>(&amp;self) -&gt; <a class="enum" href="enum.ColorType.html" title="enum image::ColorType">ColorType</a></h4></section></summary><div class="docblock"><p>Returns the color type of the image data produced by this decoder</p>
</div></details><details class="toggle method-toggle" open><summary><section id="tymethod.read_image" class="method"><a class="src rightside" href="../src/image/image.rs.html#665-667">Source</a><h4 class="code-header">fn <a href="#tymethod.read_image" class="fn">read_image</a>(self, buf: &amp;mut [<a class="primitive" href="https://doc.rust-lang.org/1.84.0/std/primitive.u8.html">u8</a>]) -&gt; <a class="type" href="error/type.ImageResult.html" title="type image::error::ImageResult">ImageResult</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/1.84.0/std/primitive.unit.html">()</a>&gt;<div class="where">where
Self: <a class="trait" href="https://doc.rust-lang.org/1.84.0/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,</div></h4></section></summary><div class="docblock"><p>Returns all the bytes in the image.</p>
<p>This function takes a slice of bytes and writes the pixel data of the image into it.
Although not required, for certain color types callers may want to pass buffers which are
aligned to 2 or 4 byte boundaries to the slice can be cast to a <a href="https://doc.rust-lang.org/1.84.0/std/primitive.u16.html" title="primitive u16">u16</a> or <a href="https://doc.rust-lang.org/1.84.0/std/primitive.u32.html" title="primitive u32">u32</a>. To accommodate
such casts, the returned contents will always be in native endian.</p>
<h5 id="panics"><a class="doc-anchor" href="#panics">§</a>Panics</h5>
<p>This function panics if <code>buf.len() != self.total_bytes()</code>.</p>
<h5 id="examples"><a class="doc-anchor" href="#examples">§</a>Examples</h5><div class="example-wrap"><pre class="language-no_build"><code>use zerocopy::{AsBytes, FromBytes};
fn read_16bit_image(decoder: impl ImageDecoder) -&gt; Vec&lt;16&gt; {
let mut buf: Vec&lt;u16&gt; = vec![0; decoder.total_bytes()/2];
decoder.read_image(buf.as_bytes());
buf
}</code></pre></div></div></details><details class="toggle method-toggle" open><summary><section id="tymethod.read_image_boxed" class="method"><a class="src rightside" href="../src/image/image.rs.html#697">Source</a><h4 class="code-header">fn <a href="#tymethod.read_image_boxed" class="fn">read_image_boxed</a>(self: <a class="struct" href="https://doc.rust-lang.org/1.84.0/alloc/boxed/struct.Box.html" title="struct alloc::boxed::Box">Box</a>&lt;Self&gt;, buf: &amp;mut [<a class="primitive" href="https://doc.rust-lang.org/1.84.0/std/primitive.u8.html">u8</a>]) -&gt; <a class="type" href="error/type.ImageResult.html" title="type image::error::ImageResult">ImageResult</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/1.84.0/std/primitive.unit.html">()</a>&gt;</h4></section></summary><div class="docblock"><p>Use <code>read_image</code> instead; this method is an implementation detail needed so the trait can
be object safe.</p>
<p>Note to implementors: This method should be implemented by calling <code>read_image</code> on
the boxed decoder…</p>
<div class="example-wrap"><pre class="language-no_build"><code> fn read_image_boxed(self: Box&lt;Self&gt;, buf: &amp;mut [u8]) -&gt; ImageResult&lt;()&gt; {
(*self).read_image(buf)
}</code></pre></div></div></details></div><h2 id="provided-methods" class="section-header">Provided Methods<a href="#provided-methods" class="anchor">§</a></h2><div class="methods"><details class="toggle method-toggle" open><summary><section id="method.original_color_type" class="method"><a class="src rightside" href="../src/image/image.rs.html#620-622">Source</a><h4 class="code-header">fn <a href="#method.original_color_type" class="fn">original_color_type</a>(&amp;self) -&gt; <a class="enum" href="enum.ExtendedColorType.html" title="enum image::ExtendedColorType">ExtendedColorType</a></h4></section></summary><div class="docblock"><p>Returns the color type of the image file before decoding</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.icc_profile" class="method"><a class="src rightside" href="../src/image/image.rs.html#627-629">Source</a><h4 class="code-header">fn <a href="#method.icc_profile" class="fn">icc_profile</a>(&amp;mut self) -&gt; <a class="type" href="error/type.ImageResult.html" title="type image::error::ImageResult">ImageResult</a>&lt;<a class="enum" href="https://doc.rust-lang.org/1.84.0/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;<a class="struct" href="https://doc.rust-lang.org/1.84.0/alloc/vec/struct.Vec.html" title="struct alloc::vec::Vec">Vec</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/1.84.0/std/primitive.u8.html">u8</a>&gt;&gt;&gt;</h4></section></summary><div class="docblock"><p>Returns the ICC color profile embedded in the image, or <code>Ok(None)</code> if the image does not have one.</p>
<p>For formats that dont support embedded profiles this function should always return <code>Ok(None)</code>.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.total_bytes" class="method"><a class="src rightside" href="../src/image/image.rs.html#637-642">Source</a><h4 class="code-header">fn <a href="#method.total_bytes" class="fn">total_bytes</a>(&amp;self) -&gt; <a class="primitive" href="https://doc.rust-lang.org/1.84.0/std/primitive.u64.html">u64</a></h4></section></summary><div class="docblock"><p>Returns the total number of bytes in the decoded image.</p>
<p>This is the size of the buffer that must be passed to <code>read_image</code> or
<code>read_image_with_progress</code>. The returned value may exceed <code>usize::MAX</code>, in
which case it isnt actually possible to construct a buffer to decode all the image data
into. If, however, the size does not fit in a u64 then <code>u64::MAX</code> is returned.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.set_limits" class="method"><a class="src rightside" href="../src/image/image.rs.html#680-685">Source</a><h4 class="code-header">fn <a href="#method.set_limits" class="fn">set_limits</a>(&amp;mut self, limits: <a class="struct" href="struct.Limits.html" title="struct image::Limits">Limits</a>) -&gt; <a class="type" href="error/type.ImageResult.html" title="type image::error::ImageResult">ImageResult</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/1.84.0/std/primitive.unit.html">()</a>&gt;</h4></section></summary><div class="docblock"><p>Set the decoder to have the specified limits. See <a href="./io/struct.Limits.html"><code>Limits</code></a> for the different kinds of
limits that is possible to set.</p>
<p>Note to implementors: make sure you call <a href="./io/struct.Limits.html#method.check_support"><code>Limits::check_support</code></a> so that
decoding fails if any unsupported strict limits are set. Also make sure
you call <a href="./io/struct.Limits.html#method.check_dimensions"><code>Limits::check_dimensions</code></a> to check the <code>max_image_width</code> and
<code>max_image_height</code> limits.</p>
</div></details></div><h2 id="foreign-impls" class="section-header">Implementations on Foreign Types<a href="#foreign-impls" class="anchor">§</a></h2><details class="toggle implementors-toggle"><summary><section id="impl-ImageDecoder-for-Box%3CT%3E" class="impl"><a class="src rightside" href="../src/image/image.rs.html#700-728">Source</a><a href="#impl-ImageDecoder-for-Box%3CT%3E" class="anchor">§</a><h3 class="code-header">impl&lt;T: ?<a class="trait" href="https://doc.rust-lang.org/1.84.0/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a> + <a class="trait" href="trait.ImageDecoder.html" title="trait image::ImageDecoder">ImageDecoder</a>&gt; <a class="trait" href="trait.ImageDecoder.html" title="trait image::ImageDecoder">ImageDecoder</a> for <a class="struct" href="https://doc.rust-lang.org/1.84.0/alloc/boxed/struct.Box.html" title="struct alloc::boxed::Box">Box</a>&lt;T&gt;</h3></section></summary><div class="impl-items"><section id="method.dimensions" class="method trait-impl"><a class="src rightside" href="../src/image/image.rs.html#701-703">Source</a><a href="#method.dimensions" class="anchor">§</a><h4 class="code-header">fn <a href="#tymethod.dimensions" class="fn">dimensions</a>(&amp;self) -&gt; (<a class="primitive" href="https://doc.rust-lang.org/1.84.0/std/primitive.u32.html">u32</a>, <a class="primitive" href="https://doc.rust-lang.org/1.84.0/std/primitive.u32.html">u32</a>)</h4></section><section id="method.color_type" class="method trait-impl"><a class="src rightside" href="../src/image/image.rs.html#704-706">Source</a><a href="#method.color_type" class="anchor">§</a><h4 class="code-header">fn <a href="#tymethod.color_type" class="fn">color_type</a>(&amp;self) -&gt; <a class="enum" href="enum.ColorType.html" title="enum image::ColorType">ColorType</a></h4></section><section id="method.original_color_type-1" class="method trait-impl"><a class="src rightside" href="../src/image/image.rs.html#707-709">Source</a><a href="#method.original_color_type-1" class="anchor">§</a><h4 class="code-header">fn <a href="#method.original_color_type" class="fn">original_color_type</a>(&amp;self) -&gt; <a class="enum" href="enum.ExtendedColorType.html" title="enum image::ExtendedColorType">ExtendedColorType</a></h4></section><section id="method.icc_profile-1" class="method trait-impl"><a class="src rightside" href="../src/image/image.rs.html#710-712">Source</a><a href="#method.icc_profile-1" class="anchor">§</a><h4 class="code-header">fn <a href="#method.icc_profile" class="fn">icc_profile</a>(&amp;mut self) -&gt; <a class="type" href="error/type.ImageResult.html" title="type image::error::ImageResult">ImageResult</a>&lt;<a class="enum" href="https://doc.rust-lang.org/1.84.0/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;<a class="struct" href="https://doc.rust-lang.org/1.84.0/alloc/vec/struct.Vec.html" title="struct alloc::vec::Vec">Vec</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/1.84.0/std/primitive.u8.html">u8</a>&gt;&gt;&gt;</h4></section><section id="method.total_bytes-1" class="method trait-impl"><a class="src rightside" href="../src/image/image.rs.html#713-715">Source</a><a href="#method.total_bytes-1" class="anchor">§</a><h4 class="code-header">fn <a href="#method.total_bytes" class="fn">total_bytes</a>(&amp;self) -&gt; <a class="primitive" href="https://doc.rust-lang.org/1.84.0/std/primitive.u64.html">u64</a></h4></section><section id="method.read_image" class="method trait-impl"><a class="src rightside" href="../src/image/image.rs.html#716-721">Source</a><a href="#method.read_image" class="anchor">§</a><h4 class="code-header">fn <a href="#tymethod.read_image" class="fn">read_image</a>(self, buf: &amp;mut [<a class="primitive" href="https://doc.rust-lang.org/1.84.0/std/primitive.u8.html">u8</a>]) -&gt; <a class="type" href="error/type.ImageResult.html" title="type image::error::ImageResult">ImageResult</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/1.84.0/std/primitive.unit.html">()</a>&gt;<div class="where">where
Self: <a class="trait" href="https://doc.rust-lang.org/1.84.0/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,</div></h4></section><section id="method.read_image_boxed" class="method trait-impl"><a class="src rightside" href="../src/image/image.rs.html#722-724">Source</a><a href="#method.read_image_boxed" class="anchor">§</a><h4 class="code-header">fn <a href="#tymethod.read_image_boxed" class="fn">read_image_boxed</a>(self: <a class="struct" href="https://doc.rust-lang.org/1.84.0/alloc/boxed/struct.Box.html" title="struct alloc::boxed::Box">Box</a>&lt;Self&gt;, buf: &amp;mut [<a class="primitive" href="https://doc.rust-lang.org/1.84.0/std/primitive.u8.html">u8</a>]) -&gt; <a class="type" href="error/type.ImageResult.html" title="type image::error::ImageResult">ImageResult</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/1.84.0/std/primitive.unit.html">()</a>&gt;</h4></section><section id="method.set_limits-1" class="method trait-impl"><a class="src rightside" href="../src/image/image.rs.html#725-727">Source</a><a href="#method.set_limits-1" class="anchor">§</a><h4 class="code-header">fn <a href="#method.set_limits" class="fn">set_limits</a>(&amp;mut self, limits: <a class="struct" href="struct.Limits.html" title="struct image::Limits">Limits</a>) -&gt; <a class="type" href="error/type.ImageResult.html" title="type image::error::ImageResult">ImageResult</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/1.84.0/std/primitive.unit.html">()</a>&gt;</h4></section></div></details><h2 id="implementors" class="section-header">Implementors<a href="#implementors" class="anchor">§</a></h2><div id="implementors-list"><section id="impl-ImageDecoder-for-PngDecoder%3CR%3E" class="impl"><a class="src rightside" href="../src/image/codecs/png.rs.html#167-215">Source</a><a href="#impl-ImageDecoder-for-PngDecoder%3CR%3E" class="anchor">§</a><h3 class="code-header">impl&lt;R: <a class="trait" href="https://doc.rust-lang.org/1.84.0/std/io/trait.BufRead.html" title="trait std::io::BufRead">BufRead</a> + <a class="trait" href="https://doc.rust-lang.org/1.84.0/std/io/trait.Seek.html" title="trait std::io::Seek">Seek</a>&gt; <a class="trait" href="trait.ImageDecoder.html" title="trait image::ImageDecoder">ImageDecoder</a> for <a class="struct" href="codecs/png/struct.PngDecoder.html" title="struct image::codecs::png::PngDecoder">PngDecoder</a>&lt;R&gt;</h3></section><section id="impl-ImageDecoder-for-HdrDecoder%3CR%3E" class="impl"><a class="src rightside" href="../src/image/codecs/hdr/decoder.rs.html#296-321">Source</a><a href="#impl-ImageDecoder-for-HdrDecoder%3CR%3E" class="anchor">§</a><h3 class="code-header">impl&lt;R: <a class="trait" href="https://doc.rust-lang.org/1.84.0/std/io/trait.Read.html" title="trait std::io::Read">Read</a>&gt; <a class="trait" href="trait.ImageDecoder.html" title="trait image::ImageDecoder">ImageDecoder</a> for <a class="struct" href="codecs/hdr/struct.HdrDecoder.html" title="struct image::codecs::hdr::HdrDecoder">HdrDecoder</a>&lt;R&gt;</h3></section></div><script src="../trait.impl/image/image/trait.ImageDecoder.js" data-ignore-extern-crates="alloc" async></script></section></div></main></body></html>