Skip to content

Commit b92fbaa

Browse files
committed
Polish interface for efficient copy
Since conversion is fallible, using `to_*` is more appropriate. Backport-Of: 41046d3
1 parent 65264b7 commit b92fbaa

File tree

4 files changed

+22
-13
lines changed

4 files changed

+22
-13
lines changed

src/images/buffer.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::color::{FromColor, FromPrimitive, Luma, LumaA, Rgb, Rgba};
1010
use crate::error::{
1111
ImageResult, ParameterError, ParameterErrorKind, UnsupportedError, UnsupportedErrorKind,
1212
};
13-
use crate::flat::{FlatSamples, SampleLayout, View};
13+
use crate::flat::{FlatSamples, SampleLayout, ViewOfPixel};
1414
use crate::math::Rect;
1515
use crate::metadata::cicp::{CicpApplicable, CicpPixelCast, CicpRgb, ColorComponentForCicp};
1616
use crate::traits::{EncodableLayout, Pixel, PixelWithColorType};
@@ -1238,7 +1238,7 @@ where
12381238
*self.get_pixel(x, y)
12391239
}
12401240

1241-
fn as_samples(&self) -> Option<View<&[<Self::Pixel as Pixel>::Subpixel], Self::Pixel>> {
1241+
fn to_pixel_view(&self) -> Option<ViewOfPixel<'_, Self::Pixel>> {
12421242
let samples = FlatSamples {
12431243
samples: &*self.data,
12441244
layout: self.sample_layout(),
@@ -1292,7 +1292,7 @@ where
12921292

12931293
fn copy_from_samples(
12941294
&mut self,
1295-
view: View<&[<Self::Pixel as Pixel>::Subpixel], Self::Pixel>,
1295+
view: ViewOfPixel<'_, Self::Pixel>,
12961296
x: u32,
12971297
y: u32,
12981298
) -> ImageResult<()> {

src/images/flat.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,7 +1437,7 @@ where
14371437
*P::from_slice(&buffer[..channels])
14381438
}
14391439

1440-
fn as_samples(&self) -> Option<View<&[<Self::Pixel as Pixel>::Subpixel], Self::Pixel>> {
1440+
fn to_pixel_view(&self) -> Option<ViewOfPixel<'_, Self::Pixel>> {
14411441
Some(View {
14421442
inner: FlatSamples {
14431443
samples: self.inner.samples.as_ref(),
@@ -1481,7 +1481,7 @@ where
14811481
*P::from_slice(&buffer[..channels])
14821482
}
14831483

1484-
fn as_samples(&self) -> Option<View<&[<Self::Pixel as Pixel>::Subpixel], Self::Pixel>> {
1484+
fn to_pixel_view(&self) -> Option<ViewOfPixel<'_, Self::Pixel>> {
14851485
Some(View {
14861486
inner: FlatSamples {
14871487
samples: self.inner.samples.as_ref(),

src/images/generic_image.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::error::{ImageError, ImageResult};
2-
use crate::flat::{View, ViewOfPixel};
2+
use crate::flat::ViewOfPixel;
33
use crate::math::Rect;
44
use crate::traits::Pixel;
55
use crate::{ImageBuffer, SubImage};
@@ -140,8 +140,17 @@ pub trait GenericImageView {
140140
ImageBuffer::new(width, height)
141141
}
142142

143-
/// If the buffer has a fitting layout, return a view of the samples descriptor.
144-
fn as_samples(&self) -> Option<ViewOfPixel<'_, Self::Pixel>> {
143+
/// If the buffer has a fitting layout, return a canonical view of the samples.
144+
///
145+
/// This is the basis of optimization and by default return `None`. It lets consumers of
146+
/// generic images access the sample data through a canonical descriptor of its layout directly
147+
/// instead of pixel-by-pixel. This provides more efficient forms of access that the
148+
/// [`GenericImageView`] trait itself does not demand from all its implementations.
149+
///
150+
/// Implementation of this method should be cheap to call.
151+
///
152+
/// If implemented, a [`SubImage`] proxy of this image will provide a sample view as well.
153+
fn to_pixel_view(&self) -> Option<ViewOfPixel<'_, Self::Pixel>> {
145154
None
146155
}
147156
}
@@ -254,7 +263,7 @@ pub trait GenericImage: GenericImageView {
254263
where
255264
O: GenericImageView<Pixel = Self::Pixel>,
256265
{
257-
if let Some(flat) = other.as_samples() {
266+
if let Some(flat) = other.to_pixel_view() {
258267
return self.copy_from_samples(flat, x, y);
259268
}
260269

@@ -275,7 +284,7 @@ pub trait GenericImage: GenericImageView {
275284
/// Copy pixels from a regular strided matrix of pixels.
276285
fn copy_from_samples(
277286
&mut self,
278-
samples: View<&[<Self::Pixel as Pixel>::Subpixel], Self::Pixel>,
287+
samples: ViewOfPixel<'_, Self::Pixel>,
279288
x: u32,
280289
y: u32,
281290
) -> ImageResult<()> {

src/images/sub_image.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{flat::View, GenericImage, GenericImageView, ImageBuffer, Pixel};
1+
use crate::{flat::ViewOfPixel, GenericImage, GenericImageView, ImageBuffer, Pixel};
22
use std::ops::{Deref, DerefMut};
33

44
/// A View into another image
@@ -209,8 +209,8 @@ where
209209
self.image.buffer_with_dimensions(width, height)
210210
}
211211

212-
fn as_samples(&self) -> Option<View<&'_ [<Self::Pixel as Pixel>::Subpixel], Self::Pixel>> {
213-
let inner = self.image.as_samples()?;
212+
fn to_pixel_view(&self) -> Option<ViewOfPixel<'_, Self::Pixel>> {
213+
let inner = self.image.to_pixel_view()?;
214214

215215
// Now pivot the inner descriptor.
216216
let mut descriptor = inner.into_inner();

0 commit comments

Comments
 (0)