1use crate::{Error, Header, ImageFormat, Result};
2
3#[derive(Debug, Clone, Copy)]
4pub struct RawImage<'a> {
5 format: ImageFormat,
6 width: u16,
7 height: u16,
8 data: &'a [u8],
9}
10
11impl<'a> RawImage<'a> {
12 pub(crate) const fn new(format: ImageFormat, width: u16, height: u16, data: &'a [u8]) -> Self {
13 Self {
14 format,
15 width,
16 height,
17 data,
18 }
19 }
20
21 pub const fn format(&self) -> ImageFormat {
22 self.format
23 }
24
25 pub const fn width(&self) -> u16 {
26 self.width
27 }
28
29 pub const fn height(&self) -> u16 {
30 self.height
31 }
32
33 pub const fn data(&self) -> &'a [u8] {
34 self.data
35 }
36
37 pub const fn len(&self) -> usize {
38 self.data.len()
39 }
40
41 pub const fn is_empty(&self) -> bool {
42 self.data.is_empty()
43 }
44}
45
46pub(crate) fn image_data_len(header: &Header) -> Result<usize> {
47 let mut total = 0usize;
48 for mip in 0..header.mip_count() {
49 let (width, height, depth) = mip_dimensions(header, mip);
50 let one_slice = header.image_format().byte_len(width, height)?;
51 let mip_length = checked_mul(one_slice, usize::from(depth), "mip depth byte length")?;
52 let mip_length = checked_mul(
53 mip_length,
54 usize::from(header.face_count()),
55 "mip face byte length",
56 )?;
57 let mip_length = checked_mul(
58 mip_length,
59 usize::from(header.frame_count()),
60 "mip frame byte length",
61 )?;
62 total = total
63 .checked_add(mip_length)
64 .ok_or(Error::ArithmeticOverflow {
65 context: "complete image data byte length",
66 })?;
67 }
68 Ok(total)
69}
70
71pub(crate) fn locate(
72 header: &Header,
73 mip: u8,
74 frame: u16,
75 face: u8,
76 slice: u16,
77) -> Result<(usize, usize, u16, u16)> {
78 check_index("mip", u32::from(mip), u32::from(header.mip_count()))?;
79 check_index("frame", u32::from(frame), u32::from(header.frame_count()))?;
80 check_index("face", u32::from(face), u32::from(header.face_count()))?;
81
82 let (width, height, depth) = mip_dimensions(header, mip);
83 check_index("depth slice", u32::from(slice), u32::from(depth))?;
84
85 let mut offset = 0usize;
86
87 for previous_mip in (mip + 1)..header.mip_count() {
90 let (previous_width, previous_height, previous_depth) =
91 mip_dimensions(header, previous_mip);
92 let one_slice = header
93 .image_format()
94 .byte_len(previous_width, previous_height)?;
95 let mip_length = checked_mul(
96 one_slice,
97 usize::from(previous_depth),
98 "preceding mip depth byte length",
99 )?;
100 let mip_length = checked_mul(
101 mip_length,
102 usize::from(header.face_count()),
103 "preceding mip face byte length",
104 )?;
105 let mip_length = checked_mul(
106 mip_length,
107 usize::from(header.frame_count()),
108 "preceding mip frame byte length",
109 )?;
110 offset = offset
111 .checked_add(mip_length)
112 .ok_or(Error::ArithmeticOverflow {
113 context: "subresource mip offset",
114 })?;
115 }
116
117 let one_slice = header.image_format().byte_len(width, height)?;
118 let frame_index = checked_mul(
119 usize::from(frame),
120 usize::from(header.face_count()),
121 "subresource frame index",
122 )?;
123 let face_index =
124 frame_index
125 .checked_add(usize::from(face))
126 .ok_or(Error::ArithmeticOverflow {
127 context: "subresource face index",
128 })?;
129 let slice_index = checked_mul(face_index, usize::from(depth), "subresource depth index")?
130 .checked_add(usize::from(slice))
131 .ok_or(Error::ArithmeticOverflow {
132 context: "subresource slice index",
133 })?;
134 offset = offset
135 .checked_add(checked_mul(
136 slice_index,
137 one_slice,
138 "subresource byte offset",
139 )?)
140 .ok_or(Error::ArithmeticOverflow {
141 context: "subresource byte offset",
142 })?;
143
144 Ok((offset, one_slice, width, height))
145}
146
147pub(crate) fn mip_dimensions(header: &Header, mip: u8) -> (u16, u16, u16) {
148 (
149 mip_dimension(header.width(), mip),
150 mip_dimension(header.height(), mip),
151 mip_dimension(header.depth(), mip),
152 )
153}
154
155fn mip_dimension(base: u16, mip: u8) -> u16 {
156 base.checked_shr(u32::from(mip)).unwrap_or(0).max(1)
157}
158
159fn checked_mul(left: usize, right: usize, context: &'static str) -> Result<usize> {
160 left.checked_mul(right)
161 .ok_or(Error::ArithmeticOverflow { context })
162}
163
164fn check_index(axis: &'static str, index: u32, count: u32) -> Result<()> {
165 if index >= count {
166 return Err(Error::SubresourceOutOfRange { axis, index, count });
167 }
168 Ok(())
169}