skia_safe/codec/
encoded_origin.rs

1use crate::{prelude::*, ISize, Matrix};
2use skia_bindings::{self as sb, SkEncodedOrigin};
3
4// Even though possible, we are not using the original SkEncodedOrigin enum, because of the
5// `to_matrix()` implementation below, which needs an `ISize` and so can not be implemented in the
6// skia-bindings crate.
7
8/// These values match the orientation www.exif.org/Exif2-2.PDF.
9#[repr(i32)]
10#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, Default)]
11pub enum EncodedOrigin {
12    /// Default
13    #[default]
14    TopLeft = SkEncodedOrigin::TopLeft as _,
15    /// Reflected across y-axis
16    TopRight = SkEncodedOrigin::TopRight as _,
17    /// Rotated 180
18    BottomRight = SkEncodedOrigin::BottomRight as _,
19    /// Reflected across x-axis
20    BottomLeft = SkEncodedOrigin::BottomLeft as _,
21    /// Reflected across x-axis, Rotated 90 CCW
22    LeftTop = SkEncodedOrigin::LeftTop as _,
23    /// Rotated 90 CW
24    RightTop = SkEncodedOrigin::RightTop as _,
25    /// Reflected across x-axis, Rotated 90 CW
26    RightBottom = SkEncodedOrigin::RightBottom as _,
27    /// Rotated 90 CCW
28    LeftBottom = SkEncodedOrigin::LeftBottom as _,
29}
30
31native_transmutable!(SkEncodedOrigin, EncodedOrigin, encoded_origin_layout);
32
33impl EncodedOrigin {
34    pub const LAST: Self = EncodedOrigin::LeftBottom;
35    pub const DEFAULT: Self = EncodedOrigin::TopLeft;
36
37    /// Given an width and height of the source data, returns a matrix that transforms the source
38    /// rectangle with upper left corner at `[0, 0]` and origin to a correctly oriented destination
39    /// rectangle of `[0, 0, w, h]`.
40    pub fn to_matrix(self, size: impl Into<ISize>) -> Matrix {
41        let size = size.into();
42        let mut m = Matrix::default();
43        unsafe {
44            sb::C_SkEncodedOriginToMatrix(
45                self.into_native(),
46                size.width,
47                size.height,
48                m.native_mut(),
49            )
50        };
51        m
52    }
53
54    /// Return `true` if the encoded origin includes a 90 degree rotation, in which case the width
55    /// and height of the source data are swapped relative to a correctly oriented destination.
56    pub fn swaps_width_height(self) -> bool {
57        (self as i32) >= EncodedOrigin::LeftTop as i32
58    }
59}