imgref-1.12.0/.cargo_vcs_info.json0000644000000001360000000000100123660ustar { "git": { "sha1": "c987cc5333e2cf89e4e88e7d302bd2f2d9074f8f" }, "path_in_vcs": "" }imgref-1.12.0/.github/workflows/cargo.yml000064400000000000000000000013741046102023000163760ustar 00000000000000name: CI on: push: pull_request: branches: # Branches from forks have the form 'user:branch-name' so we only run # this job on pull_request events for branches that look like fork # branches. Without this we would end up running this job twice for non # forked PRs, once for the push and then once for opening the PR. - '**:**' jobs: test: name: Test strategy: matrix: rust: - stable - nightly runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - uses: actions-rs/toolchain@v1 with: profile: minimal toolchain: ${{ matrix.rust }} override: true - uses: actions-rs/cargo@v1 with: command: test imgref-1.12.0/.gitignore000064400000000000000000000000221046102023000131400ustar 00000000000000target Cargo.lock imgref-1.12.0/Cargo.lock0000644000000002270000000000100103420ustar # This file is automatically @generated by Cargo. # It is not intended for manual editing. version = 4 [[package]] name = "imgref" version = "1.12.0" imgref-1.12.0/Cargo.toml0000644000000027370000000000100103750ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2021" rust-version = "1.61" name = "imgref" version = "1.12.0" authors = ["Kornel Lesiński "] build = false autolib = false autobins = false autoexamples = false autotests = false autobenches = false description = "A basic 2-dimensional slice for safe and convenient handling of pixel buffers with width, height & stride" homepage = "https://lib.rs/crates/imgref" documentation = "https://docs.rs/imgref/" readme = "README.md" keywords = [ "interoperability", "stride", "plane", "frame", "vec2d", ] categories = [ "rust-patterns", "multimedia::images", ] license = "CC0-1.0 OR Apache-2.0" repository = "https://github.com/kornelski/imgref" [package.metadata.docs.rs] targets = ["x86_64-unknown-linux-gnu"] rustdoc-args = ["--generate-link-to-definition"] [badges.maintenance] status = "actively-developed" [features] default = ["deprecated"] deprecated = [] [lib] name = "imgref" path = "src/lib.rs" [[bench]] name = "iterbench" path = "benches/iterbench.rs" imgref-1.12.0/Cargo.toml.orig000064400000000000000000000014371046102023000140520ustar 00000000000000[package] authors = ["Kornel Lesiński "] categories = ["rust-patterns", "multimedia::images"] description = "A basic 2-dimensional slice for safe and convenient handling of pixel buffers with width, height & stride" documentation = "https://docs.rs/imgref/" homepage = "https://lib.rs/crates/imgref" keywords = ["interoperability", "stride", "plane", "frame", "vec2d"] license = "CC0-1.0 OR Apache-2.0" name = "imgref" readme = "README.md" repository = "https://github.com/kornelski/imgref" version = "1.12.0" edition = "2021" rust-version = "1.61" [features] default = ["deprecated"] deprecated = [] [badges] maintenance = { status = "actively-developed" } [package.metadata.docs.rs] targets = ["x86_64-unknown-linux-gnu"] rustdoc-args = ["--generate-link-to-definition"] imgref-1.12.0/LICENSE-APACHE000064400000000000000000000261351046102023000131110ustar 00000000000000 Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. imgref-1.12.0/LICENSE-CC0000064400000000000000000000156101046102023000125310ustar 00000000000000Creative Commons Legal Code CC0 1.0 Universal CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED HEREUNDER. Statement of Purpose The laws of most jurisdictions throughout the world automatically confer exclusive Copyright and Related Rights (defined below) upon the creator and subsequent owner(s) (each and all, an "owner") of an original work of authorship and/or a database (each, a "Work"). Certain owners wish to permanently relinquish those rights to a Work for the purpose of contributing to a commons of creative, cultural and scientific works ("Commons") that the public can reliably and without fear of later claims of infringement build upon, modify, incorporate in other works, reuse and redistribute as freely as possible in any form whatsoever and for any purposes, including without limitation commercial purposes. These owners may contribute to the Commons to promote the ideal of a free culture and the further production of creative, cultural and scientific works, or to gain reputation or greater distribution for their Work in part through the use and efforts of others. For these and/or other purposes and motivations, and without any expectation of additional consideration or compensation, the person associating CC0 with a Work (the "Affirmer"), to the extent that he or she is an owner of Copyright and Related Rights in the Work, voluntarily elects to apply CC0 to the Work and publicly distribute the Work under its terms, with knowledge of his or her Copyright and Related Rights in the Work and the meaning and intended legal effect of CC0 on those rights. 1. Copyright and Related Rights. A Work made available under CC0 may be protected by copyright and related or neighboring rights ("Copyright and Related Rights"). Copyright and Related Rights include, but are not limited to, the following: i. the right to reproduce, adapt, distribute, perform, display, communicate, and translate a Work; ii. moral rights retained by the original author(s) and/or performer(s); iii. publicity and privacy rights pertaining to a person's image or likeness depicted in a Work; iv. rights protecting against unfair competition in regards to a Work, subject to the limitations in paragraph 4(a), below; v. rights protecting the extraction, dissemination, use and reuse of data in a Work; vi. database rights (such as those arising under Directive 96/9/EC of the European Parliament and of the Council of 11 March 1996 on the legal protection of databases, and under any national implementation thereof, including any amended or successor version of such directive); and vii. other similar, equivalent or corresponding rights throughout the world based on applicable law or treaty, and any national implementations thereof. 2. Waiver. To the greatest extent permitted by, but not in contravention of, applicable law, Affirmer hereby overtly, fully, permanently, irrevocably and unconditionally waives, abandons, and surrenders all of Affirmer's Copyright and Related Rights and associated claims and causes of action, whether now known or unknown (including existing as well as future claims and causes of action), in the Work (i) in all territories worldwide, (ii) for the maximum duration provided by applicable law or treaty (including future time extensions), (iii) in any current or future medium and for any number of copies, and (iv) for any purpose whatsoever, including without limitation commercial, advertising or promotional purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each member of the public at large and to the detriment of Affirmer's heirs and successors, fully intending that such Waiver shall not be subject to revocation, rescission, cancellation, termination, or any other legal or equitable action to disrupt the quiet enjoyment of the Work by the public as contemplated by Affirmer's express Statement of Purpose. 3. Public License Fallback. Should any part of the Waiver for any reason be judged legally invalid or ineffective under applicable law, then the Waiver shall be preserved to the maximum extent permitted taking into account Affirmer's express Statement of Purpose. In addition, to the extent the Waiver is so judged Affirmer hereby grants to each affected person a royalty-free, non transferable, non sublicensable, non exclusive, irrevocable and unconditional license to exercise Affirmer's Copyright and Related Rights in the Work (i) in all territories worldwide, (ii) for the maximum duration provided by applicable law or treaty (including future time extensions), (iii) in any current or future medium and for any number of copies, and (iv) for any purpose whatsoever, including without limitation commercial, advertising or promotional purposes (the "License"). The License shall be deemed effective as of the date CC0 was applied by Affirmer to the Work. Should any part of the License for any reason be judged legally invalid or ineffective under applicable law, such partial invalidity or ineffectiveness shall not invalidate the remainder of the License, and in such case Affirmer hereby affirms that he or she will not (i) exercise any of his or her remaining Copyright and Related Rights in the Work or (ii) assert any associated claims and causes of action with respect to the Work, in either case contrary to Affirmer's express Statement of Purpose. 4. Limitations and Disclaimers. a. No trademark or patent rights held by Affirmer are waived, abandoned, surrendered, licensed or otherwise affected by this document. b. Affirmer offers the Work as-is and makes no representations or warranties of any kind concerning the Work, express, implied, statutory or otherwise, including without limitation warranties of title, merchantability, fitness for a particular purpose, non infringement, or the absence of latent or other defects, accuracy, or the present or absence of errors, whether or not discoverable, all to the greatest extent permissible under applicable law. c. Affirmer disclaims responsibility for clearing rights of other persons that may apply to the Work or any use thereof, including without limitation any person's Copyright and Related Rights in the Work. Further, Affirmer disclaims responsibility for obtaining any necessary consents, permissions or other rights required for any use of the Work. d. Affirmer understands and acknowledges that Creative Commons is not a party to this document and has no duty or obligation with respect to this CC0 or use of the Work. imgref-1.12.0/README.md000064400000000000000000000056771046102023000124540ustar 00000000000000# 2D slice of a an image This is a minimal struct that represents a 2-dimensional vector and rectangular slices of it. It's intended for describing image data, and regions of images (AKA bitmap descriptors, frames with a pitch or stride, planes). It's useful when working with image data, and can be used as the common struct for exchanging image pixels between crates. * [API Reference](https://docs.rs/imgref) * [Installation](https://crates.io/crates/imgref) In graphics code it's very common to pass `width` and `height` along with a `Vec` or a slice of pixels — all as separate arguments. This gets very repetitive, and can lead to errors. Additionally, some graphics code requires use of `stride`, which allows defining sub-regions of images without copying. This is useful for slicing and tiling, and to support bitmaps that require padding (e.g. in video formats that round frame sizes to a multiple of a block size). This crate is a simple struct that adds dimensions to the underlying pixel buffer. This makes it easier to correctly keep track of the image size and allows passing images with just one function argument instead three or four. For convenience, it also implements efficient iterators for pixels/rows and indexing with `img[(x,y)]`. ```rust use imgref::*; fn main() { let img = Img::new(vec![0; 1000], 50, 20); // 1000 pixels of a 50×20 image let new_image = some_image_processing_function(img.as_ref()); // Use imgvec.as_ref() instead of &imgvec for better efficiency println!("New size is {}×{}", new_image.width(), new_image.height()); println!("And the top left pixel is {:?}", new_image[(0u32,0u32)]); let first_row_slice = &new_image[0]; for row in new_image.rows() { … } for px in new_image.pixels() { … } // slice (x, y, width, height) by reference - no copy! let fragment = img.sub_image(5, 5, 15, 15); // create a vec of pixels without stride, for compatibility with software // that expects pixels without any "gaps" let (vec, width, height) = fragment.to_contiguous_buf(); } ``` ## Type aliases Illustration: stride is width of the whole buffer. These are described in [more detail in the reference](https://docs.rs/imgref). ### `ImgVec` It owns its pixels (held in a `Vec`). It's analogous to a 2-dimensional `Vec`. Use this type to create and return new images from functions. Don't use `&ImgVec`. Instead call `ImgVec.as_ref()` to get a reference (`ImgRef`) from it (explicit `.as_ref()` call is required, because Rust doesn't support [custom conversions](https://github.com/rust-lang/rfcs/pull/1524) yet.) ### `ImgRef` `ImgRef` is a reference to pixels owned by some other `ImgVec` or a slice. It's analogous to a 2-dimensional `&[]`. Use this type to accept read-only images as arguments in functions. Note that `ImgRef` is a `Copy` type. Pass `ImgRef`, and *not* `&ImgRef`. ### Requirements * Latest stable Rust (1.42+) imgref-1.12.0/benches/iterbench.rs000064400000000000000000000017201046102023000150760ustar 00000000000000#![feature(test)] extern crate test; use imgref::*; use test::Bencher; #[bench] fn iter_count(bench: &mut Bencher) { let img = Img::new_stride(vec![0x11223344u32; 802*600], 800, 600, 802); bench.iter(|| { img.pixels().count() }); } #[bench] fn iter_sum(bench: &mut Bencher) { let img = Img::new_stride(vec![0x11223344u32; 802*600], 800, 600, 802); bench.iter(|| { img.pixels().map(|p| p as usize).sum::() }); } #[bench] fn stride_ignorant_sum(bench: &mut Bencher) { let img = Img::new_stride(vec![0x11223344u32; 802*600], 800, 600, 802); bench.iter(|| { img.buf().iter().copied().map(|p| p as usize).sum::() }); } #[bench] fn chunked_sum(bench: &mut Bencher) { let img = Img::new_stride(vec![0x11223344u32; 802*600], 800, 600, 802); bench.iter(|| { img.buf().chunks(img.stride()).flat_map(|row| row[0..img.width()].iter()).copied().map(|p| p as usize).sum::() }); } imgref-1.12.0/imgref.png000064400000000000000000001275731046102023000131540ustar 00000000000000PNG  IHDR|`^PLTEGpL___(((SSSxsttpppGGGCCCjjjNNN333wwwytssw===ffftttwYYYs"""tttts...裣uvs\\\ bbb~~~r888və5* #wζO 5̭m!`N? ˣ F0Kknj^Ǔ˱](αYÁfޤvw~܁_tRNS7zX "o bC+N#h152"'3$3,.)6;F8Z^KWO>RVSH<\8TD<:[OI@[Y;L7G]IB?^6@CKYNEQOSAdJ8nPf΁=\hu@n bVwB#E,DKyJxkD?ĩ{]Tɒph+F.IDATxUP @2Qu"!pXCL';k Ԓ @1) 0IY"NW-`Iq r/$~/rPAg|uL%g=,9g~']?{ y휑ѱ'lz?O'Z<0jSttrB:ŧr_W2[yA/ţ̊0/D,]_*/d2n~[nQѓNm rixb?_^7 i厞 W,frT9|Cϔ+I't tݴ^C\-bsrEMZԺiYUn<#|xH^(7~鰦Pt3|ҫZA@MGEn>E.?'t d6־Q_RHdJnl>jfO-j _yoPzum#~95݀4Mr!u'2" XβF.b"ؔ (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]6X_{w]OLX_w M>V |H}zrHϪƨgui`@˓Ʌ(%ͼG%oDNMB;zĩS0B+Eng|caAl] Vb\**؎M&?,m?F[cbU9aL.tOm>Ҁ.Q&x@>G~^ƀc 읷"nXU~ OG?.DzOI OJ d<tҘkxm@a.@vv . =$H$eRjOj}opp Z΁mZS&4>|7 qqۍplS½?!C%c%VtVa b<@~}ᛖvNO ^.[j?nj/El,Xg=?[>_r|1@l:S}jpN/ L.qA"XT*Kfg!՝:^r%[=q/Ggh|SB>TcAlۓ<831 $!O>/W^q 9@㯒÷{X'4 <'{B.G{)Xӭ~ m@mju0N1|H˕זü5ZK-W7(q:F,WWoһūhYnGkF}o>f_XNSlBON῱Ņ[AǚUEF=|Ay|Ȓoe^N/ޅPEpM#.3 |Ƭ!-9q$a+>6:E84͆) ڣuSoizC.>>v|٧jƮ KfwFY__oA0iƺ7Q{_FsܜPK!l;qgL6o,m;<F}}_{|[2+M >rnC6TG,Bx}y^6Qӎm L}DC50ǜU *BRi9@%}(f)+ UsZhh.-mlBMlzIENDB`imgref-1.12.0/src/iter.rs000064400000000000000000000230131046102023000132550ustar 00000000000000use core::iter::FusedIterator; use core::marker::PhantomData; use core::num::NonZeroUsize; use core::slice; #[cfg(test)] use alloc::vec; /// Rows of the image. Call `Img.rows()` to create it. /// /// Each element is a slice `width` pixels wide. Ignores padding, if there's any. #[derive(Debug)] #[must_use] pub struct RowsIter<'a, T> { pub(crate) inner: slice::Chunks<'a, T>, pub(crate) width: usize, } impl<'a, T: 'a> Iterator for RowsIter<'a, T> { type Item = &'a [T]; #[inline] fn next(&mut self) -> Option { match self.inner.next() { Some(s) => { // guaranteed during creation of chunks iterator debug_assert!(s.len() >= self.width); unsafe { Some(s.get_unchecked(0..self.width)) } }, None => None, } } #[inline] fn size_hint(&self) -> (usize, Option) { self.inner.size_hint() } #[inline] fn nth(&mut self, n: usize) -> Option { match self.inner.nth(n) { Some(s) => { // guaranteed during creation of chunks iterator debug_assert!(s.len() >= self.width); unsafe { Some(s.get_unchecked(0..self.width)) } }, None => None, } } #[inline] fn count(self) -> usize { self.inner.count() } } impl ExactSizeIterator for RowsIter<'_, T> { #[inline] fn len(&self) -> usize { self.inner.len() } } impl FusedIterator for RowsIter<'_, T> {} impl<'a, T: 'a> DoubleEndedIterator for RowsIter<'a, T> { #[inline] fn next_back(&mut self) -> Option { match self.inner.next_back() { Some(s) => { // guaranteed during creation of chunks iterator debug_assert!(s.len() >= self.width); unsafe { Some(s.get_unchecked(0..self.width)) } }, None => None, } } } /// Rows of the image. Call `Img.rows_mut()` to create it. /// /// Each element is a slice `width` pixels wide. Ignores padding, if there's any. #[derive(Debug)] #[must_use] pub struct RowsIterMut<'a, T> { pub(crate) width: usize, pub(crate) inner: slice::ChunksMut<'a, T>, } impl<'a, T: 'a> Iterator for RowsIterMut<'a, T> { type Item = &'a mut [T]; #[inline] fn next(&mut self) -> Option { match self.inner.next() { Some(s) => Some(&mut s[0..self.width]), None => None, } } #[inline] fn size_hint(&self) -> (usize, Option) { self.inner.size_hint() } #[inline] fn nth(&mut self, n: usize) -> Option { match self.inner.nth(n) { Some(s) => Some(&mut s[0..self.width]), None => None, } } #[inline] fn count(self) -> usize { self.inner.count() } } impl ExactSizeIterator for RowsIterMut<'_, T> {} impl FusedIterator for RowsIterMut<'_, T> {} impl<'a, T: 'a> DoubleEndedIterator for RowsIterMut<'a, T> { #[inline] fn next_back(&mut self) -> Option { match self.inner.next_back() { Some(s) => Some(&mut s[0..self.width]), None => None, } } } /// Iterates over pixels in the (sub)image. Call `Img.pixels()` to create it. /// /// Ignores padding, if there's any. #[must_use] pub struct PixelsIter<'a, T: Copy> { inner: PixelsRefIter<'a, T>, } impl<'a, T: Copy + 'a> PixelsIter<'a, T> { #[inline(always)] #[track_caller] pub(crate) fn new(img: super::ImgRef<'a, T>) -> Self { Self { inner: PixelsRefIter::new(img) } } } impl<'a, T: Copy + 'a> Iterator for PixelsIter<'a, T> { type Item = T; #[inline(always)] fn next(&mut self) -> Option { self.inner.next().copied() } } impl ExactSizeIterator for PixelsIter<'_, T> { #[inline] fn len(&self) -> usize { self.inner.len() } } /// Iterates over pixels in the (sub)image. Call `Img.pixels_ref()` to create it. /// /// Ignores padding, if there's any. #[derive(Debug)] #[must_use] pub struct PixelsRefIter<'a, T> { current: *const T, current_line_end: *const T, rows_left: usize, width: NonZeroUsize, pad: usize, _dat: PhantomData<&'a [T]>, } unsafe impl Send for PixelsRefIter<'_, T> where T: Send {} unsafe impl Sync for PixelsRefIter<'_, T> where T: Sync {} impl<'a, T: 'a> PixelsRefIter<'a, T> { #[inline] #[track_caller] pub(crate) fn new(img: super::ImgRef<'a, T>) -> Self { let width = NonZeroUsize::new(img.width()).expect("width > 0"); let height = img.height(); let stride = img.stride(); assert!(stride >= width.get()); let pad = stride - width.get(); debug_assert!(img.buf().len() + stride >= stride * height + width.get(), "buffer len {} is less than {} (({}+{})x{})", img.buf().len(), stride * height - pad, width, pad, height); Self { current: img.buf().as_ptr(), current_line_end: img.buf()[width.get()..].as_ptr(), width, rows_left: height, pad, _dat: PhantomData, } } } impl<'a, T: 'a> Iterator for PixelsRefIter<'a, T> { type Item = &'a T; #[inline(always)] fn next(&mut self) -> Option { unsafe { if self.current >= self.current_line_end { if self.rows_left <= 1 { return None; } self.rows_left -= 1; self.current = self.current_line_end.add(self.pad); self.current_line_end = self.current.add(self.width.get()); } let px = &*self.current; self.current = self.current.add(1); Some(px) } } #[inline] #[cfg_attr(debug_assertions, track_caller)] fn size_hint(&self) -> (usize, Option) { let this_line = unsafe { self.current_line_end.offset_from(self.current) }; debug_assert!(this_line >= 0); let len = this_line as usize + (self.rows_left - 1) * self.width.get(); (len, Some(len)) } } impl ExactSizeIterator for PixelsRefIter<'_, T> { } /// Iterates over pixels in the (sub)image. Call `Img.pixels_mut()` to create it. /// /// Ignores padding, if there's any. #[derive(Debug)] #[must_use] pub struct PixelsIterMut<'a, T> { current: *mut T, current_line_end: *mut T, y: usize, width: NonZeroUsize, pad: usize, _dat: PhantomData<&'a mut [T]>, } unsafe impl Send for PixelsIterMut<'_, T> where T: Send {} unsafe impl Sync for PixelsIterMut<'_, T> where T: Sync {} impl<'a, T: 'a> PixelsIterMut<'a, T> { #[inline] #[track_caller] pub(crate) fn new(img: &mut super::ImgRefMut<'a, T>) -> Self { let width = NonZeroUsize::new(img.width()).expect("width > 0"); let stride = img.stride(); debug_assert!(!img.buf().is_empty() && img.buf().len() + stride >= stride * img.height() + width.get()); Self { current: img.buf_mut().as_mut_ptr(), current_line_end: img.buf_mut()[width.get()..].as_mut_ptr(), width, y: img.height(), pad: stride - width.get(), _dat: PhantomData, } } } impl<'a, T: 'a> Iterator for PixelsIterMut<'a, T> { type Item = &'a mut T; #[inline(always)] fn next(&mut self) -> Option { unsafe { if self.current >= self.current_line_end { self.y -= 1; if self.y == 0 { return None; } self.current = self.current_line_end.add(self.pad); self.current_line_end = self.current.add(self.width.get()); } let px = &mut *self.current; self.current = self.current.add(1); Some(px) } } } #[test] fn iter() { let img = super::Img::new(vec![1u8, 2], 1, 2); let mut it = img.pixels(); assert_eq!(Some(1), it.next()); assert_eq!(Some(2), it.next()); assert_eq!(None, it.next()); let buf = [1u8; (16 + 3) * (8 + 1)]; for width in 1..16 { for height in 1..8 { for pad in 0..3 { let stride = width + pad; let img = super::Img::new_stride(&buf[..stride * height + stride - width], width, height, stride); assert_eq!(width * height, img.pixels().map(|a| a as usize).sum(), "{width}x{height}"); assert_eq!(width * height, img.pixels().count(), "{width}x{height}"); assert_eq!(height, img.rows().count()); let mut iter1 = img.pixels(); let mut left = width * height; while let Some(_px) = iter1.next() { left -= 1; assert_eq!(left, iter1.len()); } assert_eq!(0, iter1.len()); iter1.next(); assert_eq!(0, iter1.len()); let mut iter2 = img.rows(); match iter2.next() { Some(_) => { assert_eq!(height - 1, iter2.size_hint().0); assert_eq!(height - 1, iter2.filter(|_| true).count()); }, None => { assert_eq!(height, 0); }, }; } } } } imgref-1.12.0/src/lib.rs000064400000000000000000001003021046102023000130550ustar 00000000000000//! In graphics code it's very common to pass `width` and `height` along with a `Vec` of pixels, //! all as separate arguments. This is tedious, and can lead to errors. //! //! This crate is a simple struct that adds dimensions to the underlying buffer. This makes it easier to correctly keep track //! of the image size and allows passing images with just one function argument instead three or four. //! //! Additionally, it has a concept of a `stride`, which allows defining sub-regions of images without copying, //! as well as handling padding (e.g. buffers for video frames may require to be a multiple of 8, regardless of logical image size). //! //! For convenience, there are iterators over rows or all pixels of a (sub)image and //! pixel-based indexing directly with `img[(x,y)]` (where `x`/`y` can be `u32` as well as `usize`). //! //! `Img` type has aliases for common uses: //! //! * Owned: `ImgVec` → `Img>` (use it in `struct`s and return types) //! * Reference: `ImgRef` → `Img<&[T]>` (use it in function arguments) //! * Mutable reference: `ImgRefMut` → `Img<&mut [T]>` //! //! It is assumed that the container is [one element per pixel](https://crates.io/crates/rgb/), e.g. `Vec`, //! and _not_ a `Vec` where 4 `u8` elements are interpreted as one pixel. //! //! //! ```rust //! use imgref::*; //! # fn some_image_processing_function(img: ImgRef) -> ImgVec { img.new_buf(img.buf().to_vec()) } //! //! fn main() { //! let img = Img::new(vec![0; 1000], 50, 20); // 1000 pixels of a 50×20 image //! //! let new_image = some_image_processing_function(img.as_ref()); // Use imgvec.as_ref() instead of &imgvec for better efficiency //! //! println!("New size is {}×{}", new_image.width(), new_image.height()); //! println!("And the top left pixel is {:?}", new_image[(0u32,0u32)]); //! //! let first_row_slice = &new_image[0]; //! //! for row in new_image.rows() { //! // … //! } //! for px in new_image.pixels() { //! // … //! } //! //! // slice (x, y, width, height) by reference - no copy! //! let fragment = img.sub_image(5, 5, 15, 15); //! //! // //! let (vec, width, height) = fragment.to_contiguous_buf(); //! } //! ``` #![no_std] extern crate alloc; #[cfg(test)] extern crate std; use alloc::borrow::{Cow, ToOwned}; use alloc::vec::Vec; use core::slice; mod traits; mod iter; mod ops; pub use iter::*; /// Image owning its pixels. /// /// A 2D array of pixels. The pixels are oriented top-left first and rows are `stride` pixels wide. /// /// If size of the `buf` is larger than `width`*`height`, then any excess space is a padding (see `width_padded()`/`height_padded()`). pub type ImgVec = Img>; /// Reference to pixels inside another image. /// Pass this structure by value (i.e. `ImgRef`, not `&ImgRef`). /// /// Only `width` of pixels of every `stride` can be modified. The `buf` may be longer than `height`*`stride`, but the extra space should be ignored. pub type ImgRef<'slice, Pixel> = Img<&'slice [Pixel]>; /// Same as `ImgRef`, but mutable /// Pass this structure by value (i.e. `ImgRef`, not `&ImgRef`). /// pub type ImgRefMut<'slice, Pixel> = Img<&'slice mut [Pixel]>; /// Additional methods that depend on buffer size /// /// To use these methods you need: /// /// ```rust /// use imgref::*; /// ``` pub trait ImgExt { /// Maximum possible width of the data, including the stride. /// /// # Panics /// /// This method may panic if the underlying buffer is not at least `height()*stride()` pixels large. #[cfg(feature = "deprecated")] fn width_padded(&self) -> usize; /// Height in number of full strides. /// If the underlying buffer is not an even multiple of strides, the last row is ignored. /// /// # Panics /// /// This method may panic if the underlying buffer is not at least `height()*stride()` pixels large. #[cfg(feature = "deprecated")] fn height_padded(&self) -> usize; /// Iterate over the entire buffer as rows, including all padding /// /// Rows will have up to `stride` width, but the last row may be shorter. #[cfg(feature = "deprecated")] fn rows_padded(&self) -> slice::Chunks<'_, Pixel>; /// Borrow the container fn as_ref(&self) -> ImgRef<'_, Pixel>; } /// Additional methods that depend on buffer size /// /// To use these methods you need: /// /// ```rust /// use imgref::*; /// ``` pub trait ImgExtMut { /// Iterate over the entire buffer as rows, including all padding /// /// Rows will have up to `stride` width, but the last row may be shorter. #[cfg(feature = "deprecated")] fn rows_padded_mut(&mut self) -> slice::ChunksMut<'_, Pixel>; /// Borrow the container mutably fn as_mut(&mut self) -> ImgRefMut<'_, Pixel>; } /// Basic struct used for both owned (alias `ImgVec`) and borrowed (alias `ImgRef`) image fragments. /// /// Note: the fields are `pub` only because of borrow checker limitations. Please consider them as read-only. #[derive(Debug, Copy, Clone)] pub struct Img { /// Storage for the pixels. Usually `Vec` or `&[Pixel]`. See `ImgVec` and `ImgRef`. /// /// Note that future version will make this field private. Use `.rows()` and `.pixels()` iterators where possible, or `buf()`/`buf_mut()`/`into_buf()`. #[deprecated(note = "Don't access struct fields directly. Use buf(), buf_mut() or into_buf()")] #[cfg(feature = "deprecated")] pub buf: Container, #[cfg(not(feature = "deprecated"))] buf: Container, /// Number of pixels to skip in the container to advance to the next row. /// /// Note: pixels between `width` and `stride` may not be usable, and may not even exist in the last row. #[deprecated(note = "Don't access struct fields directly. Use stride()")] #[cfg(feature = "deprecated")] pub stride: usize, #[cfg(not(feature = "deprecated"))] stride: usize, /// Width of the image in pixels. /// /// Note that this isn't same as the width of the row in the `buf`, see `stride` #[deprecated(note = "Don't access struct fields directly. Use width()")] #[cfg(feature = "deprecated")] pub width: u32, #[cfg(not(feature = "deprecated"))] width: u32, /// Height of the image in pixels. #[deprecated(note = "Don't access struct fields directly. Use height()")] #[cfg(feature = "deprecated")] pub height: u32, #[cfg(not(feature = "deprecated"))] height: u32, } impl Img { /// Width of the image in pixels. /// /// Note that this isn't same as the width of the row in image data, see `stride()` #[inline(always)] #[allow(deprecated)] pub const fn width(&self) -> usize { self.width as usize } /// Height of the image in pixels. #[inline(always)] #[allow(deprecated)] pub const fn height(&self) -> usize { self.height as usize } /// Number of _pixels_ to skip in the container to advance to the next row. /// /// Note the last row may have fewer pixels than the stride. /// Some APIs use number of *bytes* for a stride. You may need to multiply this one by number of pixels. #[inline(always)] #[allow(deprecated)] pub const fn stride(&self) -> usize { self.stride } /// Immutable reference to the pixel storage. Warning: exposes stride. Use `pixels()` or `rows()` instead. /// /// See also `into_contiguous_buf()`. #[inline(always)] #[allow(deprecated)] pub const fn buf(&self) -> &Container { &self.buf } /// Mutable reference to the pixel storage. Warning: exposes stride. Use `pixels_mut()` or `rows_mut()` instead. /// /// See also `into_contiguous_buf()`. #[inline(always)] #[allow(deprecated)] pub fn buf_mut(&mut self) -> &mut Container { &mut self.buf } /// Get the pixel storage by consuming the image. Be careful about stride — see `into_contiguous_buf()` for a safe version. #[inline(always)] #[allow(deprecated)] pub fn into_buf(self) -> Container { self.buf } #[deprecated(note = "this was meant to be private, use new_buf() and/or rows()")] #[cfg(feature = "deprecated")] #[doc(hidden)] pub fn rows_buf<'buf, T: 'buf>(&self, buf: &'buf [T]) -> RowsIter<'buf, T> { self.rows_buf_internal(buf) } #[inline] #[track_caller] fn rows_buf_internal<'buf, T: 'buf>(&self, buf: &'buf [T]) -> RowsIter<'buf, T> { let stride = self.stride(); debug_assert!(self.width() <= self.stride()); debug_assert!(buf.len() >= self.width() * self.height()); assert!(stride > 0); let non_padded = &buf[0..stride * self.height() + self.width() - stride]; RowsIter { width: self.width(), inner: non_padded.chunks(stride), } } } impl ImgExt for Img where Container: AsRef<[Pixel]> { #[inline(always)] #[cfg(feature = "deprecated")] fn width_padded(&self) -> usize { self.stride() } #[inline(always)] #[cfg(feature = "deprecated")] fn height_padded(&self) -> usize { let len = self.buf().as_ref().len(); assert_eq!(0, len % self.stride()); len / self.stride() } /// Iterate over the entire buffer as rows, including all padding /// /// Rows will have up to `stride` width, but the last row may be shorter. #[inline(always)] #[cfg(feature = "deprecated")] fn rows_padded(&self) -> slice::Chunks<'_, Pixel> { self.buf().as_ref().chunks(self.stride()) } #[inline(always)] #[allow(deprecated)] fn as_ref(&self) -> ImgRef<'_, Pixel> { Img { buf: self.buf.as_ref(), width: self.width, height: self.height, stride: self.stride, } } } impl ImgExtMut for Img where Container: AsMut<[Pixel]> { /// Iterate over the entire buffer as rows, including all padding /// /// Rows will have up to `stride` width, but the last row may be shorter. /// /// # Panics /// /// If stride is 0 #[cfg(feature = "deprecated")] fn rows_padded_mut(&mut self) -> slice::ChunksMut<'_, Pixel> { let stride = self.stride(); self.buf_mut().as_mut().chunks_mut(stride) } #[inline(always)] #[allow(deprecated)] fn as_mut(&mut self) -> ImgRefMut<'_, Pixel> { Img { buf: self.buf.as_mut(), width: self.width, height: self.height, stride: self.stride, } } } #[inline] fn sub_image(left: usize, top: usize, width: usize, height: usize, stride: usize, buf_len: usize) -> (usize, usize, usize) { let start = stride * top + left; let full_strides_end = start + stride * height; // when left > 0 and height is full, the last line is shorter than the stride let end = if buf_len >= full_strides_end { full_strides_end } else { debug_assert!(height > 0); let min_strides_len = full_strides_end + width - stride; debug_assert!(buf_len >= min_strides_len, "the buffer is too small to fit the subimage"); // if can't use full buffer, then shrink to min required (last line having exact width) min_strides_len }; (start, end, stride) } impl<'slice, T> ImgRef<'slice, T> { /// Make a reference for a part of the image, without copying any pixels. /// /// # Panics /// /// It will panic if `sub_image` is outside of the image area /// (left + width must be <= container width, etc.) #[inline] #[must_use] #[track_caller] pub fn sub_image(&self, left: usize, top: usize, width: usize, height: usize) -> Self { assert!(top + height <= self.height()); assert!(left + width <= self.width()); let (start, end, stride) = sub_image(left, top, width, height, self.stride(), self.buf().len()); let buf = &self.buf()[start..end]; Self::new_stride(buf, width, height, stride) } #[inline] /// Iterate over whole rows of pixels as slices /// /// # Panics /// /// If stride is 0 /// /// See also `pixels()` pub fn rows(&self) -> RowsIter<'slice, T> { self.rows_buf_internal(self.buf()) } /// Deprecated /// /// Note: it iterates **all** pixels in the underlying buffer, not just limited by width/height. #[deprecated(note = "Size of this buffer is unpredictable. Use .rows() instead")] #[cfg(feature = "deprecated")] #[doc(hidden)] pub fn iter(&self) -> slice::Iter<'slice, T> { self.buf().iter() } } impl<'a, T: Clone> ImgRef<'a, T> { /// Returns a reference to the buffer, width, height. Guarantees that the buffer is contiguous, /// i.e. it's `width*height` elements long, and `[x + y*width]` addresses each pixel. /// /// It will create a copy if the buffer isn't contiguous (width != stride). /// For a more efficient version, see `into_contiguous_buf()` #[allow(deprecated)] #[must_use] pub fn to_contiguous_buf(&self) -> (Cow<'a, [T]>, usize, usize) { let width = self.width(); let height = self.height(); let stride = self.stride(); if width == stride { return (Cow::Borrowed(self.buf), width, height); } let mut buf = Vec::with_capacity(width * height); for row in self.rows() { buf.extend_from_slice(row); } (Cow::Owned(buf), width, height) } } impl<'slice, T> ImgRefMut<'slice, T> { /// Turn this into immutable reference, and slice a subregion of it #[inline] #[allow(deprecated)] #[must_use] pub fn sub_image(&'slice self, left: usize, top: usize, width: usize, height: usize) -> ImgRef<'slice, T> { self.as_ref().sub_image(left, top, width, height) } /// Slices this image reference to produce another reference to a subregion of it. /// /// Note that mutable borrows are exclusive, so it's not possible to have more than /// one mutable subimage at a time. /// /// ## Panics /// /// If the coordinates are out of bounds #[inline] #[allow(deprecated)] #[must_use] #[track_caller] pub fn sub_image_mut(&mut self, left: usize, top: usize, width: usize, height: usize) -> ImgRefMut<'_, T> { assert!(top + height <= self.height()); assert!(left + width <= self.width()); let (start, end, stride) = sub_image(left, top, width, height, self.stride(), self.buf.len()); let buf = &mut self.buf[start..end]; ImgRefMut::new_stride(buf, width, height, stride) } /// Transforms this image reference to refer to a subregion. /// /// This is identical in behavior to [`ImgRefMut::sub_image_mut()`], except that it returns an /// [`ImgRefMut`] with the same lifetime, rather than a reborrow with a shorter lifetime. /// /// ## Panics /// /// If the coordinates are out of bounds #[allow(deprecated)] #[must_use] #[track_caller] pub fn into_sub_image_mut(self, left: usize, top: usize, width: usize, height: usize) -> Self { assert!(top + height <= self.height()); assert!(left + width <= self.width()); let (start, end, stride) = sub_image(left, top, width, height, self.stride(), self.buf.len()); let buf = &mut self.buf[start..end]; ImgRefMut::new_stride(buf, width, height, stride) } /// Make mutable reference immutable #[inline] #[must_use] pub fn as_ref(&self) -> ImgRef<'_, T> { self.new_buf(self.buf().as_ref()) } } impl<'slice, T: Copy> ImgRef<'slice, T> { /// Iterate `width*height` pixels in the `Img`, ignoring padding area /// /// If you want to iterate in parallel, parallelize `rows()` instead. /// /// # Panics /// /// if width is 0 #[inline] #[track_caller] pub fn pixels(&self) -> PixelsIter<'slice, T> { PixelsIter::new(*self) } } impl<'slice, T> ImgRef<'slice, T> { /// Iterate `width*height` pixels in the `Img`, by reference, ignoring padding area /// /// If you want to iterate in parallel, parallelize `rows()` instead. /// /// # Panics /// /// if width is 0 #[inline] pub fn pixels_ref(&self) -> PixelsRefIter<'slice, T> { PixelsRefIter::new(*self) } } impl ImgRefMut<'_, T> { /// # Panics /// /// If you want to iterate in parallel, parallelize `rows()` instead. /// /// if width is 0 #[inline] pub fn pixels(&self) -> PixelsIter<'_, T> { PixelsIter::new(self.as_ref()) } } impl ImgRefMut<'_, T> { /// If you want to iterate in parallel, parallelize `rows()` instead. /// # Panics /// /// if width is 0 #[inline] pub fn pixels_mut(&mut self) -> PixelsIterMut<'_, T> { PixelsIterMut::new(self) } } impl ImgVec { /// If you want to iterate in parallel, parallelize `rows()` instead. /// # Panics /// /// if width is 0 #[inline] pub fn pixels(&self) -> PixelsIter<'_, T> { PixelsIter::new(self.as_ref()) } } impl ImgVec { /// If you want to iterate in parallel, parallelize `rows()` instead. /// # Panics /// /// if width is 0 #[inline] pub fn pixels_mut(&mut self) -> PixelsIterMut<'_, T> { PixelsIterMut::new(&mut self.as_mut()) } } impl ImgRefMut<'_, T> { /// Iterate over whole rows as slices /// /// # Panics /// /// if stride is 0 #[inline] pub fn rows(&self) -> RowsIter<'_, T> { self.rows_buf_internal(&self.buf()[..]) } /// Iterate over whole rows as slices /// /// # Panics /// /// if stride is 0 #[inline] #[allow(deprecated)] pub fn rows_mut(&mut self) -> RowsIterMut<'_, T> { let stride = self.stride(); let width = self.width(); let height = self.height(); let non_padded = &mut self.buf[0..stride * height + width - stride]; RowsIterMut { width, inner: non_padded.chunks_mut(stride), } } } /// Deprecated. Use .`rows()` or .`pixels()` iterators which are more predictable #[cfg(feature = "deprecated")] impl IntoIterator for Img where Container: IntoIterator { type Item = Container::Item; type IntoIter = Container::IntoIter; /// Deprecated. Use .`rows()` or .`pixels()` iterators which are more predictable fn into_iter(self) -> Container::IntoIter { self.into_buf().into_iter() } } impl ImgVec { /// Create a mutable view into a region within the image. See `sub_image()` for read-only views. /// /// ## Panics /// /// If the coordinates are out of bounds #[allow(deprecated)] #[must_use] #[track_caller] pub fn sub_image_mut(&mut self, left: usize, top: usize, width: usize, height: usize) -> ImgRefMut<'_, T> { assert!(top + height <= self.height()); assert!(left + width <= self.width()); let start = self.stride * top + left; let min_buf_size = if self.height > 0 { self.stride * height + width - self.stride } else {0}; let buf = &mut self.buf[start .. start + min_buf_size]; Img::new_stride(buf, width, height, self.stride) } #[inline] #[must_use] /// Make a reference for a part of the image, without copying any pixels. pub fn sub_image(&self, left: usize, top: usize, width: usize, height: usize) -> ImgRef<'_, T> { self.as_ref().sub_image(left, top, width, height) } /// Make a reference to this image to pass it to functions without giving up ownership /// /// The reference should be passed by value (`ImgRef`, not `&ImgRef`). /// /// If you need a mutable reference, see `as_mut()` and `sub_image_mut()` #[inline] #[must_use] pub fn as_ref(&self) -> ImgRef<'_, T> { self.new_buf(self.buf().as_ref()) } /// Make a mutable reference to the entire image /// /// The reference should be passed by value (`ImgRefMut`, not `&mut ImgRefMut`). /// /// See also `sub_image_mut()` and `rows_mut()` #[inline] pub fn as_mut(&mut self) -> ImgRefMut<'_, T> { let width = self.width(); let height = self.height(); let stride = self.stride(); Img::new_stride(self.buf_mut().as_mut(), width, height, stride) } #[deprecated(note = "Size of this buffer may be unpredictable. Use .rows() instead")] #[cfg(feature = "deprecated")] #[doc(hidden)] pub fn iter(&self) -> slice::Iter<'_, T> { self.buf().iter() } /// Iterate over rows of the image as slices /// /// Each slice is guaranteed to be exactly `width` pixels wide. /// /// This iterator is a good candidate for parallelization (e.g. rayon's `par_bridge()`) #[inline] pub fn rows(&self) -> RowsIter<'_, T> { self.rows_buf_internal(self.buf()) } /// Iterate over rows of the image as mutable slices /// /// Each slice is guaranteed to be exactly `width` pixels wide. /// /// This iterator is a good candidate for parallelization (e.g. rayon's `par_bridge()`) #[inline] #[allow(deprecated)] pub fn rows_mut(&mut self) -> RowsIterMut<'_, T> { let stride = self.stride(); let width = self.width(); let height = self.height(); let non_padded = &mut self.buf[0..stride * height + width - stride]; RowsIterMut { width, inner: non_padded.chunks_mut(stride), } } } impl Img { /// Same as `new()`, except each row is located `stride` number of pixels after the previous one. /// /// Stride can be equal to `width` or larger. If it's larger, then pixels between end of previous row and start of the next are considered a padding, and may be ignored. /// /// The `Container` is usually a `Vec` or a slice. /// /// ## Panics /// /// If stride is 0. #[inline] #[allow(deprecated)] #[track_caller] pub fn new_stride(buf: Container, width: usize, height: usize, stride: usize) -> Self { assert!(stride > 0); assert!(stride >= width); debug_assert!(height < u32::MAX as usize); debug_assert!(width < u32::MAX as usize); Self { buf, width: width as u32, height: height as u32, stride, } } /// Create new image with `Container` (which can be `Vec`, `&[]` or something else) with given `width` and `height` in pixels. /// /// Assumes the pixels in container are contiguous, layed out row by row with `width` pixels per row and at least `height` rows. /// /// If the container is larger than `width`×`height` pixels, the extra rows are a considered a padding and may be ignored. #[inline] pub fn new(buf: Container, width: usize, height: usize) -> Self { Self::new_stride(buf, width, height, width) } } impl Img> { /// Returns the buffer, width, height. Guarantees that the buffer is contiguous, /// i.e. it's `width*height` elements long, and `[x + y*width]` addresses each pixel. /// /// Efficiently performs operation in-place. For other containers use `pixels().collect()`. #[allow(deprecated)] #[must_use] pub fn into_contiguous_buf(mut self) -> (Vec, usize, usize) { let (_, w, h) = self.as_contiguous_buf(); (self.buf, w, h) } /// Returns a reference to the buffer, width, height. Guarantees that the buffer is contiguous, /// i.e. it's `width*height` elements long, and `[x + y*width]` addresses each pixel. /// /// Efficiently performs operation in-place. For other containers use `pixels().collect()`. #[allow(deprecated)] #[must_use] pub fn as_contiguous_buf(&mut self) -> (&[T], usize, usize) { let width = self.width(); let height = self.height(); let stride = self.stride(); if width != stride { for row in 1..height { self.buf.copy_within(row * stride .. row * stride + width, row * width); } } self.buf.truncate(width * height); (&mut self.buf, width, height) } } impl Img { /// A convenience method for creating an image of the same size and stride, but with a new buffer. #[inline] #[track_caller] pub fn map_buf(self, callback: F) -> Img where NewContainer: AsRef<[NewPixel]>, OldContainer: AsRef<[OldPixel]>, F: FnOnce(OldContainer) -> NewContainer { let width = self.width(); let height = self.height(); let stride = self.stride(); let old_buf_len = self.buf().as_ref().len(); #[allow(deprecated)] let new_buf = callback(self.buf); assert_eq!(old_buf_len, new_buf.as_ref().len()); Img::new_stride(new_buf, width, height, stride) } /// A convenience method for creating an image of the same size and stride, but with a new buffer. #[inline] #[track_caller] pub fn new_buf(&self, new_buf: NewContainer) -> Img where NewContainer: AsRef<[NewPixel]>, OldContainer: AsRef<[OldPixel]> { assert_eq!(self.buf().as_ref().len(), new_buf.as_ref().len()); Img::new_stride(new_buf, self.width(), self.height(), self.stride()) } } impl From>> for Img> { #[allow(deprecated)] fn from(img: Img>) -> Self { Self { width: img.width, height: img.height, stride: img.stride, buf: img.buf.into_owned(), } } } impl From> for Img> { #[allow(deprecated)] fn from(img: ImgVec) -> Self { Img { width: img.width, height: img.height, stride: img.stride, buf: img.buf.into(), } } } impl<'a, T: Clone> From> for Img> { #[allow(deprecated)] fn from(img: ImgRef<'a, T>) -> Self { Img { buf: img.buf.into(), width: img.width, height: img.height, stride: img.stride, } } } impl Img> { /// Convert underlying buffer to owned (e.g. slice to vec) /// /// See also `to_contiguous_buf().0.into_owned()` #[allow(deprecated)] #[must_use] pub fn into_owned(self) -> ImgVec { match self.buf { Cow::Borrowed(_) => { let tmp = self.as_ref(); let (buf, w, h) = tmp.to_contiguous_buf(); ImgVec::new(buf.into_owned(), w, h) }, Cow::Owned(buf) => Img { buf, width: self.width, height: self.height, stride: self.stride, }, } } } impl Img where T: ToOwned { /// Convert underlying buffer to owned (e.g. slice to vec) /// /// See also `to_contiguous_buf().0.into_owned()` #[allow(deprecated)] pub fn to_owned(&self) -> Img { Img { buf: self.buf.to_owned(), width: self.width, height: self.height, stride: self.stride, } } } #[cfg(test)] mod tests { use super::*; use alloc::vec; mod with_opinionated_container { use super::*; struct IDontDeriveAnything; #[test] fn compiles() { let _ = Img::new(IDontDeriveAnything, 1, 1); } } #[test] fn with_vec() { let bytes = vec![0u8;20]; let old = Img::new_stride(bytes, 10,2,10); let _ = old.new_buf(vec![6u16;20]); } #[test] fn zero() { let bytes = vec![0u8]; let mut img = Img::new_stride(bytes,0,0,1); let _ = img.sub_image(0,0,0,0); let _ = img.sub_image_mut(0,0,0,0); let _ = img.as_ref(); } #[test] fn zero_width() { let bytes = vec![0u8]; let mut img = Img::new_stride(bytes,0,1,1); let _ = img.sub_image(0,1,0,0); let _ = img.sub_image_mut(0,0,0,1); } #[test] fn zero_height() { let bytes = vec![0u8]; let mut img = Img::new_stride(bytes,1,0,1); assert_eq!(0, img.rows().count()); let _ = img.sub_image(1,0,0,0); let _ = img.sub_image_mut(0,0,1,0); } #[test] #[allow(deprecated)] fn with_slice() { let bytes = vec![0u8;20]; let _ = Img::new_stride(bytes.as_slice(), 10,2,10); let vec = ImgVec::new_stride(bytes, 10,2,10); #[cfg(feature = "deprecated")] for _ in vec.iter() {} assert_eq!(2, vec.rows().count()); for _ in *vec.as_ref().buf() {} #[cfg(feature = "deprecated")] for _ in vec {} } #[test] fn sub() { let img = Img::new_stride(vec![1,2,3,4, 5,6,7,8, 9], 3, 2, 4); assert_eq!(img.buf()[img.stride()], 5); assert_eq!(img.buf()[img.stride() + img.width()-1], 7); assert_eq!(img.pixels().count(), img.width() * img.height()); assert_eq!(img.pixels().sum::(), 24); { let refimg = img.as_ref(); let refimg2 = refimg; // Test is Copy // sub-image with stride hits end of the buffer let s1 = refimg.sub_image(1, 0, refimg.width()-1, refimg.height()); let _ = s1.sub_image(1, 0, s1.width()-1, s1.height()); let subimg = refimg.sub_image(1, 1, 2, 1); assert_eq!(subimg.pixels().count(), subimg.width() * subimg.height()); assert_eq!(subimg.buf()[0], 6); assert_eq!(subimg.stride(), refimg2.stride()); assert!(subimg.stride() * subimg.height() + subimg.width() - subimg.stride() <= subimg.buf().len()); assert_eq!(refimg.buf()[0], 1); assert_eq!(1, subimg.rows().count()); } // 3 different methods for constructing mutable sub-images let mut img_for_mut_1 = img.clone(); let mut img_for_mut_2 = img.clone(); let mut img_for_mut_3 = img; for mut subimg in [ img_for_mut_1.sub_image_mut(1, 1, 2, 1), img_for_mut_2.as_mut().sub_image_mut(1, 1, 2, 1), img_for_mut_3.as_mut().into_sub_image_mut(1, 1, 2, 1), ] { assert_eq!(1, subimg.rows().count()); assert_eq!(1, subimg.rows_mut().count()); assert_eq!(1, subimg.rows_mut().rev().count()); assert_eq!(1, subimg.rows_mut().fuse().rev().count()); assert_eq!(subimg.buf()[0], 6); } } #[test] fn rows() { let img = ImgVec::new_stride(vec![0u8; 10000], 10, 15, 100); assert_eq!(img.height(), img.rows().count()); assert_eq!(img.height(), img.rows().rev().count()); assert_eq!(img.height(), img.rows().fuse().rev().count()); } #[test] fn mut_pixels() { for y in 1..15 { for x in 1..10 { let mut img = ImgVec::new_stride(vec![0u8; 10000], x, y, 100); assert_eq!(x*y, img.pixels_mut().count()); assert_eq!(x*y, img.as_mut().pixels().count()); assert_eq!(x*y, img.as_mut().pixels_mut().count()); assert_eq!(x*y, img.as_mut().as_ref().pixels().count()); } } } #[test] fn into_contiguous_buf() { for in_h in [1, 2, 3, 38, 39, 40, 41].iter().copied() { for in_w in [1, 2, 3, 120, 121].iter().copied() { for stride in [in_w, 121, 122, 166, 242, 243].iter().copied() { let img = ImgVec::new_stride((0..10000).map(|x| x as u8).collect(), in_w, in_h, stride) .map_buf(|x| x); let pixels: Vec<_> = img.pixels().collect(); let (buf, w, h) = img.into_contiguous_buf(); assert_eq!(pixels, buf); assert_eq!(in_w*in_h, buf.len()); assert_eq!(10000, buf.capacity()); assert_eq!(in_w, w); assert_eq!(in_h, h); } } } let img = ImgVec::new((0..55*33).map(|x| x as u8).collect(), 55, 33); let pixels: Vec<_> = img.pixels().collect(); let tmp = img.as_ref(); let (buf, ..) = tmp.to_contiguous_buf(); assert_eq!(&pixels[..], &buf[..]); let (buf, ..) = img.into_contiguous_buf(); assert_eq!(pixels, buf); } } imgref-1.12.0/src/ops.rs000064400000000000000000000110441046102023000131140ustar 00000000000000use super::Img; use alloc::vec::Vec; use core::ops; #[cfg(test)] use alloc::vec; macro_rules! impl_imgref_index { ($container:ty, $index:ty) => { impl<'a, Pixel: Copy> ops::Index<($index, $index)> for Img<$container> { type Output = Pixel; /// Read a pixel at `(x,y)` location (e.g. px = `img[(x,y)]`) /// /// Coordinates may be outside `width`/`height` if the buffer has enough padding. /// The x coordinate can't exceed `stride`. #[inline(always)] #[cfg_attr(debug_assertions, track_caller)] fn index(&self, index: ($index, $index)) -> &Self::Output { let stride = self.stride(); debug_assert_eq!(stride, stride as $index as usize); debug_assert!(index.0 < stride as $index); &self.buf()[(index.1 * (stride as $index) + index.0) as usize] } } }; } macro_rules! impl_imgref_index_mut { ($container:ty, $index:ty) => { impl<'a, Pixel: Copy> ops::IndexMut<($index, $index)> for Img<$container> { /// Write a pixel at `(x,y)` location (e.g. `img[(x,y)] = px`) /// /// Coordinates may be outside `width`/`height` if the buffer has enough padding. /// The x coordinate can't exceed `stride`. #[inline(always)] #[cfg_attr(debug_assertions, track_caller)] fn index_mut(&mut self, index: ($index, $index)) -> &mut Self::Output { let stride = self.stride(); debug_assert_eq!(stride, stride as $index as usize); debug_assert!(index.0 < stride as $index); &mut self.buf_mut()[(index.1 * (stride as $index) + index.0) as usize] } } }; } impl_imgref_index! {&'a [Pixel], usize} impl_imgref_index! {&'a [Pixel], u32} impl_imgref_index! {&'a mut [Pixel], usize} impl_imgref_index! {&'a mut [Pixel], u32} impl_imgref_index_mut! {&'a mut [Pixel], usize} impl_imgref_index_mut! {&'a mut [Pixel], u32} impl_imgref_index! {Vec, usize} impl_imgref_index! {Vec, u32} impl_imgref_index_mut! {Vec, usize} impl_imgref_index_mut! {Vec, u32} #[test] fn index() { let mut img = Img::new_stride(vec![1,2,3,4,5,6,7,8], 2, 2, 3); assert_eq!(1, img[(0u32,0u32)]); assert_eq!(2, img.as_ref()[(1usize,0usize)]); assert_eq!(3, img.as_ref()[(2u32,0u32)]); assert_eq!(4, img[(0usize,1usize)]); assert_eq!(8, img[(1usize,2usize)]); assert_eq!(5, img.sub_image_mut(1,1,1,1)[(0usize,0usize)]); } macro_rules! impl_imgref_row_index { ($container:ty) => { impl<'a, Pixel: Copy> ops::Index for Img<$container> { type Output = [Pixel]; #[inline(always)] /// Take n-th row as a slice. Same as `.rows().nth(n).unwrap()` /// /// Slice length is guaranteed to equal image width. /// Row must be within image height. fn index(&self, row: usize) -> &Self::Output { let stride = self.stride(); let width = self.width(); let start = row * stride; self.buf().get(start .. start + width).unwrap_or_else(|| index_fail(row)) } } }; } macro_rules! impl_imgref_row_index_mut { ($container:ty) => { impl<'a, Pixel: Copy> ops::IndexMut for Img<$container> { #[inline(always)] /// Take n-th row as a mutable slice. Same as `.rows().nth(n).unwrap()` /// /// Slice length is guaranteed to equal image width. /// Row must be within image height. fn index_mut(&mut self, row: usize) -> &mut Self::Output { let stride = self.stride(); let width = self.width(); let start = row * stride; self.buf_mut().get_mut(start .. start + width).unwrap_or_else(|| index_fail(row)) } } }; } impl_imgref_row_index! {&'a [Pixel]} impl_imgref_row_index! {&'a mut [Pixel]} impl_imgref_row_index_mut! {&'a mut [Pixel]} impl_imgref_row_index! {Vec} impl_imgref_row_index_mut! {Vec} #[cold] fn index_fail(row: usize) -> ! { panic!("row {row} is out of range") } #[test] fn index_by_row() { let mut img = Img::new_stride(vec![1,2,3,4,5,6,7,8], 2, 2, 3); assert_eq!(&[1,2], &img[0]); assert_eq!(&[4,5], &img[1]); assert_eq!(&[1,2], &img.as_ref()[0]); assert_eq!(&[4,5], &img.as_ref()[1]); assert_eq!(&[1,2], &img.as_mut()[0]); assert_eq!(&[4,5], &img.as_mut()[1]); } imgref-1.12.0/src/traits.rs000064400000000000000000000075421046102023000136310ustar 00000000000000use crate::{ImgRef, ImgRefMut, ImgVec}; use core::hash::{Hash, Hasher}; impl Hash for ImgRef<'_, T> { #[allow(deprecated)] #[inline] fn hash(&self, state: &mut H) { self.width.hash(state); self.height.hash(state); for row in self.rows() { Hash::hash_slice(row, state); } } } impl Hash for ImgRefMut<'_, T> { #[allow(deprecated)] #[inline] fn hash(&self, state: &mut H) { self.as_ref().hash(state); } } impl Hash for ImgVec { #[inline(always)] fn hash(&self, state: &mut H) { self.as_ref().hash(state); } } impl<'b, T, U> PartialEq> for ImgRef<'_, T> where T: PartialEq { #[allow(deprecated)] #[inline] fn eq(&self, other: &ImgRef<'b, U>) -> bool { self.width == other.width && self.height == other.height && self.rows().zip(other.rows()).all(|(a,b)| a == b) } } impl<'b, T, U> PartialEq> for ImgRefMut<'_, T> where T: PartialEq { #[allow(deprecated)] #[inline] fn eq(&self, other: &ImgRefMut<'b, U>) -> bool { self.as_ref().eq(&other.as_ref()) } } impl PartialEq> for ImgVec where T: PartialEq { #[allow(deprecated)] #[inline(always)] fn eq(&self, other: &ImgVec) -> bool { self.as_ref().eq(&other.as_ref()) } } impl<'a, T, U> PartialEq> for ImgVec where T: PartialEq { #[allow(deprecated)] #[inline(always)] fn eq(&self, other: &ImgRef<'a, U>) -> bool { self.as_ref().eq(other) } } impl PartialEq> for ImgRef<'_, T> where T: PartialEq { #[allow(deprecated)] #[inline(always)] fn eq(&self, other: &ImgVec) -> bool { self.eq(&other.as_ref()) } } impl<'b, T, U> PartialEq> for ImgRefMut<'_, T> where T: PartialEq { #[allow(deprecated)] #[inline(always)] fn eq(&self, other: &ImgRef<'b, U>) -> bool { self.as_ref().eq(other) } } impl<'b, T, U> PartialEq> for ImgRef<'_, T> where T: PartialEq { #[allow(deprecated)] #[inline(always)] fn eq(&self, other: &ImgRefMut<'b, U>) -> bool { self.eq(&other.as_ref()) } } impl Eq for ImgRefMut<'_, T> { } impl Eq for ImgRef<'_, T> { } impl Eq for ImgVec { } #[test] fn test_eq_hash() { use alloc::vec; #[derive(Debug)] struct Comparable(u16); impl PartialEq for Comparable { fn eq(&self, other: &u8) -> bool { self.0 == u16::from(*other) } } let newtype = ImgVec::new(vec![Comparable(0), Comparable(1), Comparable(2), Comparable(3)], 2, 2); let mut img1 = ImgVec::new(vec![0u8, 1, 2, 3], 2, 2); let img_ne = ImgVec::new(vec![0u8, 1, 2, 3], 4, 1); let img2 = ImgVec::new_stride(vec![0u8, 1, 255, 2, 3, 255], 2, 2, 3); let mut img3 = ImgVec::new_stride(vec![0u8, 1, 255, 2, 3], 2, 2, 3); assert_eq!(newtype, img1); equiv(&img1, &img2); equiv(&img2, &img3); equiv(&img1, &img3); assert_ne!(img1, img_ne); assert_eq!(img1.as_ref(), img2); assert_eq!(img2, img3.as_ref()); equiv(&img1.as_ref(), &img3.as_ref()); equiv(&img1.as_mut(), &img3.as_mut()); assert_eq!(img2.as_ref(), img3.as_mut()); let mut map = HashSet::new(); img3[(0usize, 0usize)] = 100; assert_ne!(img1, img3); assert!(map.insert(img1)); assert!(map.insert(img3)); assert!(map.insert(img_ne)); assert!(!map.insert(img2)); } #[cfg(test)] use std::collections::HashSet; #[cfg(test)] use std::fmt::Debug; #[cfg(test)] fn equiv(a: &A, b: &A) where A: Eq + PartialEq + Hash + Debug { assert_eq!(a, b); let mut map = HashSet::new(); assert!(map.insert(a)); assert!(!map.insert(b)); assert!(!map.insert(a)); assert!(map.remove(b)); assert!(map.is_empty()); }