1use std::{
2 ffi::{self, CStr},
3 fmt, io,
4 marker::PhantomData,
5 mem, ptr, result,
6};
7
8use skia_bindings::{self as sb, SkCodec, SkCodec_FrameInfo, SkCodec_Options};
9
10use super::codec_animation;
11use crate::{
12 interop::RustStream, prelude::*, yuva_pixmap_info::SupportedDataTypes, AlphaType, Data,
13 EncodedImageFormat, EncodedOrigin, IRect, ISize, Image, ImageInfo, Pixmap, YUVAPixmapInfo,
14 YUVAPixmaps,
15};
16
17pub use sb::SkCodec_Result as Result;
18variant_name!(Result::IncompleteInput);
19
20pub fn result_to_string(result: Result) -> &'static str {
23 unsafe { CStr::from_ptr(skia_bindings::SkCodec_ResultToString(result)) }
24 .to_str()
25 .unwrap()
26}
27
28pub use sb::SkCodec_SelectionPolicy as SelectionPolicy;
29variant_name!(SelectionPolicy::PreferStillImage);
30
31pub use sb::SkCodec_ZeroInitialized as ZeroInitialized;
32variant_name!(ZeroInitialized::Yes);
33
34#[derive(Copy, Clone, PartialEq, Eq, Debug)]
35pub struct Options {
36 pub zero_initialized: ZeroInitialized,
37 pub subset: Option<IRect>,
38 pub frame_index: usize,
39 pub prior_frame: Option<usize>,
40 pub max_decode_memory: Option<usize>,
41}
42
43impl Default for Options {
44 fn default() -> Self {
45 Self {
46 zero_initialized: ZeroInitialized::No,
47 subset: None,
48 frame_index: 0,
49 prior_frame: None,
50 max_decode_memory: None,
51 }
52 }
53}
54
55pub const NO_FRAME: i32 = sb::SkCodec_kNoFrame;
56
57#[repr(C)]
58#[derive(Copy, Clone, Debug)]
59pub struct FrameInfo {
60 pub required_frame: i32,
61 pub duration: i32,
62 pub fully_received: bool,
63 pub alpha_type: AlphaType,
64 pub has_alpha_within_bounds: bool,
65 pub disposal_method: codec_animation::DisposalMethod,
66 pub blend: codec_animation::Blend,
67 pub rect: IRect,
68}
69
70native_transmutable!(SkCodec_FrameInfo, FrameInfo);
71
72impl Default for FrameInfo {
73 fn default() -> Self {
74 Self::construct(|frame_info| unsafe { sb::C_SkFrameInfo_Construct(frame_info) })
75 }
76}
77
78pub use sb::SkCodec_SkScanlineOrder as ScanlineOrder;
79variant_name!(ScanlineOrder::BottomUp);
80
81pub use sb::SkCodec_IsAnimated as IsAnimated;
82variant_name!(IsAnimated::Yes);
83
84pub struct Codec<'a> {
85 inner: RefHandle<SkCodec>,
86 pd: PhantomData<&'a mut dyn io::Read>,
87}
88
89impl NativeDrop for SkCodec {
90 fn drop(&mut self) {
91 unsafe { sb::C_SkCodec_delete(self) }
92 }
93}
94
95impl fmt::Debug for Codec<'_> {
96 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
97 f.debug_struct("Codec")
98 .field("info", &self.info())
99 .field("dimensions", &self.dimensions())
100 .field("bounds", &self.bounds())
101 .field("origin", &self.origin())
102 .field("encoded_format", &self.encoded_format())
103 .field("scanline_order", &self.scanline_order())
104 .field("next_scanline", &self.next_scanline())
105 .finish()
106 }
107}
108
109impl Codec<'_> {
110 pub fn from_stream<'a, T: io::Read + io::Seek>(
111 stream: &'a mut T,
112 decoders: &[codecs::Decoder],
113 selection_policy: impl Into<Option<SelectionPolicy>>,
114 ) -> result::Result<Codec<'a>, Result> {
115 let stream = RustStream::new_seekable(stream);
116 let mut result = Result::Unimplemented;
117 let codec = unsafe {
118 sb::C_SkCodec_MakeFromStream(
119 stream.into_native(),
121 decoders.as_ptr() as _,
122 decoders.len(),
123 &mut result,
124 selection_policy
125 .into()
126 .unwrap_or(SelectionPolicy::PreferStillImage),
127 )
128 };
129 if result != Result::Success {
130 return Err(result);
131 }
132 Ok(Codec::from_ptr(codec).expect("Codec is null"))
133 }
134
135 pub fn from_data(data: impl Into<Data>) -> Option<Codec<'static>> {
139 Self::from_ptr(unsafe { sb::C_SkCodec_MakeFromData(data.into().into_ptr()) })
140 }
141
142 pub fn from_data_with_decoders(
143 data: impl Into<Data>,
144 decoders: &[codecs::Decoder],
145 ) -> Option<Codec<'static>> {
146 Self::from_ptr(unsafe {
147 sb::C_SkCodec_MakeFromData2(
148 data.into().into_ptr(),
149 decoders.as_ptr() as _,
150 decoders.len(),
151 )
152 })
153 }
154
155 pub fn info(&self) -> ImageInfo {
156 let mut info = ImageInfo::default();
157 unsafe { sb::C_SkCodec_getInfo(self.native(), info.native_mut()) };
158 info
159 }
160
161 pub fn dimensions(&self) -> ISize {
162 ISize::from_native_c(unsafe { sb::C_SkCodec_dimensions(self.native()) })
163 }
164
165 pub fn bounds(&self) -> IRect {
166 IRect::construct(|r| unsafe { sb::C_SkCodec_bounds(self.native(), r) })
167 }
168
169 pub fn has_high_bit_depth_encoded_data(&self) -> bool {
173 unsafe { sb::C_SkCodec_hasHighBitDepthEncodedData(self.native()) }
174 }
175
176 pub fn origin(&self) -> EncodedOrigin {
177 EncodedOrigin::from_native_c(unsafe { sb::C_SkCodec_getOrigin(self.native()) })
178 }
179
180 pub fn get_scaled_dimensions(&self, desired_scale: f32) -> ISize {
181 ISize::from_native_c(unsafe {
182 sb::C_SkCodec_getScaledDimensions(self.native(), desired_scale)
183 })
184 }
185
186 pub fn valid_subset(&self, desired_subset: impl AsRef<IRect>) -> Option<IRect> {
187 let mut desired_subset = *desired_subset.as_ref();
188 unsafe { sb::C_SkCodec_getValidSubset(self.native(), desired_subset.native_mut()) }
189 .then_some(desired_subset)
190 }
191
192 pub fn encoded_format(&self) -> EncodedImageFormat {
193 unsafe { sb::C_SkCodec_getEncodedFormat(self.native()) }
194 }
195
196 pub fn get_pixels_with_options(
197 &mut self,
198 info: &ImageInfo,
199 pixels: &mut [u8],
200 row_bytes: usize,
201 options: Option<&Options>,
202 ) -> Result {
203 assert_eq!(pixels.len(), info.compute_byte_size(row_bytes));
204 unsafe {
205 let native_options = options.map(|options| Self::native_options(options));
206 self.native_mut().getPixels(
207 info.native(),
208 pixels.as_mut_ptr() as *mut _,
209 row_bytes,
210 native_options.as_ptr_or_null(),
211 )
212 }
213 }
214
215 #[deprecated(
216 since = "0.33.1",
217 note = "Use the safe variant get_pixels_with_options()."
218 )]
219 #[allow(clippy::missing_safety_doc)]
220 pub unsafe fn get_pixels(
221 &mut self,
222 info: &ImageInfo,
223 pixels: *mut ffi::c_void,
224 row_bytes: usize,
225 ) -> Result {
226 self.native_mut()
227 .getPixels(info.native(), pixels, row_bytes, ptr::null())
228 }
229
230 #[allow(clippy::missing_safety_doc)]
231 pub unsafe fn get_pixels_to_pixmap(
232 &mut self,
233 pixmap: &Pixmap,
234 options: Option<&Options>,
235 ) -> Result {
236 let native_options = options.map(|options| Self::native_options(options));
237 self.native_mut().getPixels(
238 pixmap.info().native(),
239 pixmap.writable_addr(),
240 pixmap.row_bytes(),
241 native_options.as_ptr_or_null(),
242 )
243 }
244
245 unsafe fn native_options(options: &Options) -> SkCodec_Options {
246 SkCodec_Options {
247 fZeroInitialized: options.zero_initialized,
248 fSubset: options.subset.native().as_ptr_or_null(),
249 fFrameIndex: options.frame_index.try_into().unwrap(),
250 fPriorFrame: match options.prior_frame {
251 None => sb::SkCodec_kNoFrame,
252 Some(frame) => frame.try_into().expect("invalid prior frame"),
253 },
254 fMaxDecodeMemory: options.max_decode_memory.unwrap_or(0),
255 }
256 }
257
258 pub fn get_image<'a>(
259 &mut self,
260 info: impl Into<Option<ImageInfo>>,
261 options: impl Into<Option<&'a Options>>,
262 ) -> std::result::Result<Image, Result> {
263 let info = info.into().unwrap_or_else(|| self.info());
264 let options = options
265 .into()
266 .map(|options| unsafe { Self::native_options(options) });
267 let mut result = Result::InternalError;
268 Image::from_ptr(unsafe {
269 sb::C_SkCodec_getImage(
270 self.native_mut(),
271 info.native(),
272 options.as_ptr_or_null(),
273 &mut result,
274 )
275 })
276 .ok_or(result)
277 }
278
279 pub fn query_yuva_info(
280 &self,
281 supported_data_types: &SupportedDataTypes,
282 ) -> Option<YUVAPixmapInfo> {
283 YUVAPixmapInfo::new_if_valid(|pixmap_info| unsafe {
284 self.native()
285 .queryYUVAInfo(supported_data_types.native(), pixmap_info)
286 })
287 }
288
289 pub fn get_yuva_planes(&mut self, pixmaps: &YUVAPixmaps) -> Result {
290 unsafe { self.native_mut().getYUVAPlanes(pixmaps.native()) }
291 }
292
293 pub fn start_incremental_decode<'a>(
294 &mut self,
295 dst_info: &ImageInfo,
296 dst: &mut [u8],
297 row_bytes: usize,
298 options: impl Into<Option<&'a Options>>,
299 ) -> Result {
300 if !dst_info.valid_pixels(row_bytes, dst) {
301 return Result::InvalidParameters;
302 }
303 let options = options
304 .into()
305 .map(|options| unsafe { Self::native_options(options) });
306 unsafe {
307 self.native_mut().startIncrementalDecode(
308 dst_info.native(),
309 dst.as_mut_ptr() as _,
310 row_bytes,
311 options.as_ptr_or_null(),
312 )
313 }
314 }
315
316 pub fn incremental_decode(&mut self) -> (Result, Option<usize>) {
317 let mut rows_decoded = Default::default();
318 let r = unsafe { sb::C_SkCodec_incrementalDecode(self.native_mut(), &mut rows_decoded) };
319 if r == Result::IncompleteInput {
320 (r, Some(rows_decoded.try_into().unwrap()))
321 } else {
322 (r, None)
323 }
324 }
325
326 pub fn start_scanline_decode<'a>(
327 &mut self,
328 dst_info: &ImageInfo,
329 options: impl Into<Option<&'a Options>>,
330 ) -> Result {
331 let options = options
332 .into()
333 .map(|options| unsafe { Self::native_options(options) });
334 unsafe {
335 self.native_mut()
336 .startScanlineDecode(dst_info.native(), options.as_ptr_or_null())
337 }
338 }
339
340 pub fn get_scanlines(&mut self, dst: &mut [u8], count_lines: usize, row_bytes: usize) -> usize {
341 assert!(mem::size_of_val(dst) >= count_lines * row_bytes);
342 unsafe {
343 self.native_mut().getScanlines(
344 dst.as_mut_ptr() as _,
345 count_lines.try_into().unwrap(),
346 row_bytes,
347 )
348 }
349 .try_into()
350 .unwrap()
351 }
352
353 pub fn skip_scanlines(&mut self, count_lines: usize) -> bool {
354 unsafe {
355 self.native_mut()
356 .skipScanlines(count_lines.try_into().unwrap())
357 }
358 }
359
360 pub fn scanline_order(&self) -> ScanlineOrder {
361 unsafe { sb::C_SkCodec_getScanlineOrder(self.native()) }
362 }
363
364 pub fn next_scanline(&self) -> i32 {
365 unsafe { sb::C_SkCodec_nextScanline(self.native()) }
366 }
367
368 pub fn outbound_scanline(&self, input_scanline: i32) -> i32 {
369 unsafe { self.native().outputScanline(input_scanline) }
370 }
371
372 pub fn get_frame_count(&mut self) -> usize {
373 unsafe { sb::C_SkCodec_getFrameCount(self.native_mut()) }
374 .try_into()
375 .unwrap()
376 }
377
378 pub fn get_frame_info(&mut self, index: usize) -> Option<FrameInfo> {
379 let mut info = FrameInfo::default();
380 unsafe {
381 sb::C_SkCodec_getFrameInfo(
382 self.native_mut(),
383 index.try_into().unwrap(),
384 info.native_mut(),
385 )
386 }
387 .then_some(info)
388 }
389
390 pub fn get_repetition_count(&mut self) -> Option<usize> {
391 const REPETITION_COUNT_INFINITE: i32 = -1;
392 let count = unsafe { sb::C_SkCodec_getRepetitionCount(self.native_mut()) };
393 if count != REPETITION_COUNT_INFINITE {
394 Some(count.try_into().unwrap())
395 } else {
396 None
397 }
398 }
399
400 pub fn is_animated(&mut self) -> IsAnimated {
401 unsafe { sb::C_SkCodec_isAnimated(self.native_mut()) }
402 }
403
404 fn native(&self) -> &SkCodec {
407 self.inner.native()
408 }
409
410 fn native_mut(&mut self) -> &mut SkCodec {
411 self.inner.native_mut()
412 }
413
414 pub(crate) fn from_ptr<'a>(codec: *mut SkCodec) -> Option<Codec<'a>> {
415 RefHandle::from_ptr(codec).map(|inner| Codec {
416 inner,
417 pd: PhantomData,
418 })
419 }
420}
421
422pub mod codecs {
423 use std::{fmt, io, ptr, result, str};
424
425 use skia_bindings::{self as sb, SkCodecs_Decoder};
426
427 use super::Result;
428 use crate::{interop::RustStream, prelude::*, AlphaType, Codec, Image};
429
430 pub type Decoder = Handle<SkCodecs_Decoder>;
431 unsafe_send_sync!(Decoder);
432
433 impl fmt::Debug for Decoder {
434 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
435 f.debug_struct("Decoder").field("id", &self.id()).finish()
436 }
437 }
438
439 impl NativeDrop for SkCodecs_Decoder {
440 fn drop(&mut self) {
441 unsafe { sb::C_SkCodecs_Decoder_destruct(self) }
442 }
443 }
444
445 impl NativeClone for SkCodecs_Decoder {
446 fn clone(&self) -> Self {
447 construct(|d| unsafe { sb::C_SkCodecs_Decoder_CopyConstruct(d, self) })
448 }
449 }
450
451 impl Decoder {
452 pub fn id(&self) -> &'static str {
453 let mut len: usize = 0;
454 let ptr = unsafe { sb::C_SkCodecs_Decoder_getId(self.native(), &mut len) };
455 let chars = unsafe { safer::from_raw_parts(ptr as _, len) };
456 str::from_utf8(chars).expect("Invalid UTF-8 decoder id")
457 }
458
459 pub fn is_format(&self, data: &[u8]) -> bool {
460 unsafe {
461 (self.native().isFormat.expect("Decoder::isFormat is null"))(
462 data.as_ptr() as _,
463 data.len(),
464 )
465 }
466 }
467
468 pub fn from_stream<'a>(
469 &self,
470 stream: &'a mut impl io::Read,
471 ) -> result::Result<Codec<'a>, Result> {
472 let stream = RustStream::new(stream);
473 let mut result = Result::Unimplemented;
474 let codec = unsafe {
475 sb::C_SkCodecs_Decoder_MakeFromStream(
476 self.native(),
477 stream.into_native(),
479 &mut result,
480 ptr::null_mut(),
481 )
482 };
483 if result != Result::Success {
484 return Err(result);
485 }
486 Ok(Codec::from_ptr(codec).expect("Codec is null"))
487 }
488 }
489
490 pub fn deferred_image(
493 codec: Codec<'_>,
494 alpha_type: impl Into<Option<AlphaType>>,
495 ) -> Option<Borrows<'_, Image>> {
496 let alpha_type: Option<AlphaType> = alpha_type.into();
497 Image::from_ptr(unsafe {
500 sb::C_SkCodecs_DeferredImage(codec.inner.into_ptr(), alpha_type.as_ptr_or_null())
501 })
502 .map(|h| unsafe { Borrows::unchecked_new(h) })
503 }
504}