swizzleinator/
lib.rs

1#![doc = include_str!("../README.md")]
2
3pub mod format;
4pub mod swizzle;
5
6#[cfg(test)]
7mod tests {
8    use crate::format::{GcmSurfaceFormat::*, GcnSurfaceFormat::*};
9    use crate::tests::SwizzleState::*;
10
11    #[derive(Copy, Clone, Debug)]
12    enum SwizzleState {
13        Deswizzle,
14        Swizzle,
15    }
16
17    macro_rules! test_impl {
18        (
19            $test_name:ident,
20            $swizzle_type:path,
21            $operation:ident,
22            $file_path:expr,
23            $width:expr,
24            $height:expr,
25            $depth:expr,
26            $image_format:expr,
27            $align_resolution:expr
28        ) => {
29            paste::paste! {
30                #[test]
31                #[allow(non_snake_case)]
32                fn [<$test_name _ $operation _ $width _ $height _ $depth _ $image_format>]() {
33                    use $crate::{swizzle::{Swizzler, Deswizzler, Format}};
34
35                    let swizzled_data = &mut include_bytes!(concat!("../testdata/", concat!($file_path, ".bin"))).to_vec();
36                    let unswizzled_data = &mut include_bytes!(concat!("../testdata/", concat!($file_path, "-unswizzled.bin"))).to_vec();
37
38                    let mut dest = vec![0u8; ($width * $height * $depth * $image_format.bpp()) / 8];
39
40                    let result = match $operation {
41                        Swizzle => {
42                            if !$width.is_power_of_two() || !$height.is_power_of_two() {
43                                dest = vec![0u8;(dest.len() as f32*1.25).floor() as usize];
44                            }
45                            <$swizzle_type as Swizzler>::swizzle(
46                                unswizzled_data,
47                                &mut dest,
48                                ($width, $height, $depth),
49                                $image_format,
50                                $align_resolution,
51                            )
52                        }
53                        Deswizzle => {
54                            <$swizzle_type as Deswizzler>::deswizzle(
55                                swizzled_data,
56                                &mut dest,
57                                ($width, $height, $depth),
58                                $image_format,
59                                $align_resolution,
60                            )
61                        }
62                    };
63
64                    assert!(
65                        result.is_err(),
66                        "{} operation failed with error: {:?}",
67                        stringify!($operation),
68                        result.err()
69                    );
70
71                    match $operation {
72                        Swizzle => assert!(*swizzled_data == dest, "Swizzled data did not match reference"),
73                        Deswizzle => assert!(*unswizzled_data == dest, "Deswizzled data did not match reference")
74                    }
75                }
76            }
77        };
78    }
79
80    // PS4 900 x 1080 BC7
81
82    test_impl!(
83        ps4,
84        crate::swizzle::ps::Ps4,
85        Deswizzle,
86        "ps4-bc7-900x1080",
87        900_usize,
88        1080_usize,
89        1,
90        BC7,
91        false
92    );
93    test_impl!(
94        ps4,
95        crate::swizzle::ps::Ps4,
96        Swizzle,
97        "ps4-bc7-900x1080",
98        900_usize,
99        1080_usize,
100        1,
101        BC7,
102        false
103    );
104
105    // PS4 171 x 171 RGBA8
106
107    test_impl!(
108        ps4,
109        crate::swizzle::ps::Ps4,
110        Deswizzle,
111        "ps4-rgba8-171x171",
112        171_usize,
113        171_usize,
114        1,
115        Format8_8_8_8,
116        true
117    );
118    test_impl!(
119        ps4,
120        crate::swizzle::ps::Ps4,
121        Swizzle,
122        "ps4-rgba8-171x171",
123        171_usize,
124        171_usize,
125        1,
126        Format8_8_8_8,
127        true
128    );
129
130    // PS4 512 x 512 BC5
131
132    test_impl!(
133        ps4,
134        crate::swizzle::ps::Ps4,
135        Deswizzle,
136        "ps4-bc5-512x512",
137        512_usize,
138        512_usize,
139        1,
140        BC5,
141        false
142    );
143    test_impl!(
144        ps4,
145        crate::swizzle::ps::Ps4,
146        Swizzle,
147        "ps4-bc5-512x512",
148        512_usize,
149        512_usize,
150        1,
151        BC5,
152        false
153    );
154
155    // PS3 64 x 64 RGBA8
156
157    test_impl!(
158        ps3,
159        crate::swizzle::ps::Ps3,
160        Deswizzle,
161        "ps3-rgba8-64x64",
162        64_usize,
163        64_usize,
164        1,
165        A8R8G8B8,
166        true
167    );
168    test_impl!(
169        ps3,
170        crate::swizzle::ps::Ps3,
171        Swizzle,
172        "ps3-rgba8-64x64",
173        64_usize,
174        64_usize,
175        1,
176        A8R8G8B8,
177        true
178    );
179
180    // PS3 128 x 128 BC3
181
182    test_impl!(
183        ps3,
184        crate::swizzle::ps::Ps3,
185        Deswizzle,
186        "ps3-bc3-128x128",
187        128_usize,
188        128_usize,
189        1,
190        COMPRESSED_DXT45,
191        true
192    );
193    test_impl!(
194        ps3,
195        crate::swizzle::ps::Ps3,
196        Swizzle,
197        "ps3-bc3-128x128",
198        128_usize,
199        128_usize,
200        1,
201        COMPRESSED_DXT45,
202        true
203    );
204}