Skip to content

Commit bfaa0a1

Browse files
authored
Refactor ImageBrush to use generic type for image data and simplify ImageBrushRef implementation (#133)
Making `ImageBrush` generic over `ImageData` allows us to simplify `ImageBrushRef`, because `ImageBrushRef<'a> = ImageBrush<&'a ImageData>`. My main usecase for generic `ImageBrush` is recording: you register image outside of draw command and reference it using newtype (this will be converted to `ImageBrushRef` when actually used). Next version of #122 --------- Signed-off-by: sagudev <[email protected]>
1 parent 3c25161 commit bfaa0a1

File tree

1 file changed

+24
-93
lines changed

1 file changed

+24
-93
lines changed

src/image.rs

Lines changed: 24 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -181,35 +181,19 @@ impl ImageSampler {
181181

182182
/// Describes the image content of a filled or stroked shape.
183183
///
184-
/// See also [`ImageBrushRef`] which can be used to avoid reference counting overhead.
185-
#[derive(Clone, PartialEq, Debug)]
184+
/// This type is generic over the storage used for the image data.
185+
/// By default, the generic parameter is [`ImageData`], which is a shared image with dynamic lifetime.
186+
/// However, different renderers can use different types here, such as a pre-registered id.
187+
#[derive(Copy, Clone, PartialEq, Debug)]
186188
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
187-
pub struct ImageBrush {
189+
pub struct ImageBrush<D = ImageData> {
188190
/// The image to render.
189-
pub image: ImageData,
191+
pub image: D,
190192
/// Parameters which specify how to sample from the image during rendering.
191193
pub sampler: ImageSampler,
192194
}
193195

194-
impl ImageBrush {
195-
/// Creates a new `ImageBrush` for the specified `ImageData` with default `ImageSampler`.
196-
#[must_use]
197-
pub fn new(image: ImageData) -> Self {
198-
Self {
199-
image,
200-
sampler: ImageSampler::default(),
201-
}
202-
}
203-
204-
/// Converts an owned `ImageBrush` into a borrowed `ImageBrushRef`.
205-
#[must_use]
206-
pub fn as_ref(&self) -> ImageBrushRef<'_> {
207-
ImageBrushRef {
208-
image: &self.image,
209-
sampler: self.sampler,
210-
}
211-
}
212-
196+
impl<D> ImageBrush<D> {
213197
/// Builder method for setting the image [extend mode](Extend) in both
214198
/// directions.
215199
#[must_use]
@@ -269,89 +253,36 @@ impl ImageBrush {
269253
}
270254
}
271255

272-
/// Borrowed version of [`ImageBrush`] for avoiding reference counting overhead.
273-
#[derive(Copy, Clone, PartialEq, Debug)]
274-
pub struct ImageBrushRef<'a> {
275-
/// The image to render.
276-
pub image: &'a ImageData,
277-
/// Parameters which specify how to sample from the image during rendering.
278-
pub sampler: ImageSampler,
279-
}
280-
281-
impl ImageBrushRef<'_> {
282-
/// Creates a new image with the given data, [format](ImageFormat) and dimensions.
256+
impl ImageBrush {
257+
/// Creates a new `ImageBrush` for the specified `ImageData` with default `ImageSampler`.
283258
#[must_use]
284-
pub fn new<'a>(image: &'a ImageData) -> ImageBrushRef<'a> {
285-
ImageBrushRef {
259+
pub fn new(image: ImageData) -> Self {
260+
Self {
286261
image,
287262
sampler: ImageSampler::default(),
288263
}
289264
}
290265

291-
/// Converts the `ImageBrushRef` to an owned `ImageBrush`.
266+
/// Converts an owned `ImageBrush` into a borrowed `ImageBrushRef`.
292267
#[must_use]
293-
pub fn to_owned(&self) -> ImageBrush {
268+
pub fn as_ref(&'_ self) -> ImageBrushRef<'_> {
294269
ImageBrush {
295-
image: (*self.image).clone(),
270+
image: &self.image,
296271
sampler: self.sampler,
297272
}
298273
}
274+
}
299275

300-
/// Builder method for setting the image [extend mode](Extend) in both
301-
/// directions.
302-
#[must_use]
303-
pub fn with_extend(mut self, mode: Extend) -> Self {
304-
self.sampler.x_extend = mode;
305-
self.sampler.y_extend = mode;
306-
self
307-
}
308-
309-
/// Builder method for setting the image [extend mode](Extend) in the
310-
/// horizontal direction.
311-
#[must_use]
312-
pub fn with_x_extend(mut self, mode: Extend) -> Self {
313-
self.sampler.x_extend = mode;
314-
self
315-
}
316-
317-
/// Builder method for setting the image [extend mode](Extend) in the
318-
/// vertical direction.
319-
#[must_use]
320-
pub fn with_y_extend(mut self, mode: Extend) -> Self {
321-
self.sampler.y_extend = mode;
322-
self
323-
}
324-
325-
/// Builder method for setting a hint for the desired image [quality](ImageQuality)
326-
/// when rendering.
327-
#[must_use]
328-
pub fn with_quality(mut self, quality: ImageQuality) -> Self {
329-
self.sampler.quality = quality;
330-
self
331-
}
332-
333-
/// Returns the image with the alpha multiplier set to `alpha`.
334-
#[must_use]
335-
#[track_caller]
336-
pub fn with_alpha(mut self, alpha: f32) -> Self {
337-
debug_assert!(
338-
alpha.is_finite() && alpha >= 0.0,
339-
"A non-finite or negative alpha ({alpha}) is meaningless."
340-
);
341-
self.sampler.alpha = alpha;
342-
self
343-
}
276+
/// Borrowed version of [`ImageBrush`] for avoiding reference counting overhead.
277+
pub type ImageBrushRef<'a> = ImageBrush<&'a ImageData>;
344278

345-
/// Returns the image with the alpha multiplier multiplied again by `alpha`.
346-
/// The behaviour of this transformation is undefined if `alpha` is negative.
279+
impl ImageBrushRef<'_> {
280+
/// Converts the `ImageBrushRef` to an owned `ImageBrush`.
347281
#[must_use]
348-
#[track_caller]
349-
pub fn multiply_alpha(mut self, alpha: f32) -> Self {
350-
debug_assert!(
351-
alpha.is_finite() && alpha >= 0.0,
352-
"A non-finite or negative alpha ({alpha}) is meaningless."
353-
);
354-
self.sampler.alpha *= alpha;
355-
self
282+
pub fn to_owned(&self) -> ImageBrush {
283+
ImageBrush {
284+
image: (*self.image).clone(),
285+
sampler: self.sampler,
286+
}
356287
}
357288
}

0 commit comments

Comments
 (0)