1use std::io::Cursor;
2use std::path::{Path, PathBuf};
3use std::time::Duration;
4
5use image::codecs::gif::{GifDecoder, GifEncoder, Repeat};
6use image::{AnimationDecoder, Delay, DynamicImage, Frame, ImageFormat, ImageOutputFormat};
7use image::{ImageBuffer, Luma, Rgb, Rgba, RgbaImage};
8use runmat_builtins::{
9 BuiltinCompletionPolicy, BuiltinDescriptor, BuiltinErrorDescriptor, BuiltinOutputMode,
10 BuiltinParamArity, BuiltinParamDescriptor, BuiltinParamType, BuiltinSignatureDescriptor,
11 LogicalArray, NumericDType, Tensor, Value,
12};
13use runmat_macros::runtime_builtin;
14
15use crate::builtins::common::spec::{
16 BroadcastSemantics, BuiltinFusionSpec, BuiltinGpuSpec, ConstantStrategy, GpuOpKind,
17 ReductionNaN, ResidencyPolicy, ShapeRequirements,
18};
19use crate::builtins::common::tensor;
20use crate::builtins::image::type_resolvers::imwrite_type;
21use crate::{build_runtime_error, gather_if_needed_async, BuiltinResult, RuntimeError};
22
23const BUILTIN_NAME: &str = "imwrite";
24
25const IMWRITE_INPUTS_IMAGE_FILENAME: [BuiltinParamDescriptor; 2] = [
26 BuiltinParamDescriptor {
27 name: "A",
28 ty: BuiltinParamType::NumericArray,
29 arity: BuiltinParamArity::Required,
30 default: None,
31 description: "Grayscale, truecolor, or RGBA image data.",
32 },
33 BuiltinParamDescriptor {
34 name: "filename",
35 ty: BuiltinParamType::StringScalar,
36 arity: BuiltinParamArity::Required,
37 default: None,
38 description: "Output image path.",
39 },
40];
41
42const IMWRITE_INPUTS_INDEXED: [BuiltinParamDescriptor; 3] = [
43 BuiltinParamDescriptor {
44 name: "X",
45 ty: BuiltinParamType::NumericArray,
46 arity: BuiltinParamArity::Required,
47 default: None,
48 description: "Indexed image data.",
49 },
50 BuiltinParamDescriptor {
51 name: "map",
52 ty: BuiltinParamType::NumericArray,
53 arity: BuiltinParamArity::Required,
54 default: None,
55 description: "Nx3 colormap.",
56 },
57 BuiltinParamDescriptor {
58 name: "filename",
59 ty: BuiltinParamType::StringScalar,
60 arity: BuiltinParamArity::Required,
61 default: None,
62 description: "Output image path.",
63 },
64];
65
66const IMWRITE_INPUTS_OPTIONS: [BuiltinParamDescriptor; 4] = [
67 BuiltinParamDescriptor {
68 name: "A",
69 ty: BuiltinParamType::NumericArray,
70 arity: BuiltinParamArity::Required,
71 default: None,
72 description: "Image data.",
73 },
74 BuiltinParamDescriptor {
75 name: "filename",
76 ty: BuiltinParamType::StringScalar,
77 arity: BuiltinParamArity::Required,
78 default: None,
79 description: "Output image path.",
80 },
81 BuiltinParamDescriptor {
82 name: "name",
83 ty: BuiltinParamType::StringScalar,
84 arity: BuiltinParamArity::Variadic,
85 default: None,
86 description: "Name-value option.",
87 },
88 BuiltinParamDescriptor {
89 name: "value",
90 ty: BuiltinParamType::Any,
91 arity: BuiltinParamArity::Variadic,
92 default: None,
93 description: "Name-value option value.",
94 },
95];
96
97const IMWRITE_SIGNATURES: [BuiltinSignatureDescriptor; 4] = [
98 BuiltinSignatureDescriptor {
99 label: "imwrite(A, filename)",
100 inputs: &IMWRITE_INPUTS_IMAGE_FILENAME,
101 outputs: &[],
102 },
103 BuiltinSignatureDescriptor {
104 label: "imwrite(A, filename, fmt)",
105 inputs: &IMWRITE_INPUTS_OPTIONS,
106 outputs: &[],
107 },
108 BuiltinSignatureDescriptor {
109 label: "imwrite(A, filename, name, value, ...)",
110 inputs: &IMWRITE_INPUTS_OPTIONS,
111 outputs: &[],
112 },
113 BuiltinSignatureDescriptor {
114 label: "imwrite(X, map, filename, ...)",
115 inputs: &IMWRITE_INPUTS_INDEXED,
116 outputs: &[],
117 },
118];
119
120const IMWRITE_ERROR_INVALID_ARGUMENT: BuiltinErrorDescriptor = BuiltinErrorDescriptor {
121 code: "RM.IMWRITE.INVALID_ARGUMENT",
122 identifier: Some("RunMat:imwrite:InvalidArgument"),
123 when: "Arguments do not match a supported imwrite form.",
124 message: "imwrite: invalid argument",
125};
126const IMWRITE_ERROR_INVALID_FILENAME: BuiltinErrorDescriptor = BuiltinErrorDescriptor {
127 code: "RM.IMWRITE.INVALID_FILENAME",
128 identifier: Some("RunMat:imwrite:InvalidFilename"),
129 when: "Filename is missing or empty.",
130 message: "imwrite: invalid filename",
131};
132const IMWRITE_ERROR_INVALID_FORMAT: BuiltinErrorDescriptor = BuiltinErrorDescriptor {
133 code: "RM.IMWRITE.INVALID_FORMAT",
134 identifier: Some("RunMat:imwrite:InvalidFormat"),
135 when: "Image format cannot be inferred or is unsupported.",
136 message: "imwrite: invalid image format",
137};
138const IMWRITE_ERROR_INVALID_IMAGE: BuiltinErrorDescriptor = BuiltinErrorDescriptor {
139 code: "RM.IMWRITE.INVALID_IMAGE",
140 identifier: Some("RunMat:imwrite:InvalidImage"),
141 when: "Image data has unsupported type, shape, or values.",
142 message: "imwrite: invalid image data",
143};
144const IMWRITE_ERROR_INVALID_COLORMAP: BuiltinErrorDescriptor = BuiltinErrorDescriptor {
145 code: "RM.IMWRITE.INVALID_COLORMAP",
146 identifier: Some("RunMat:imwrite:InvalidColormap"),
147 when: "Indexed-image colormap is not an Nx3 numeric array.",
148 message: "imwrite: invalid colormap",
149};
150const IMWRITE_ERROR_INVALID_OPTION: BuiltinErrorDescriptor = BuiltinErrorDescriptor {
151 code: "RM.IMWRITE.INVALID_OPTION",
152 identifier: Some("RunMat:imwrite:InvalidOption"),
153 when: "Name-value option is malformed or unsupported for the requested format.",
154 message: "imwrite: invalid option",
155};
156const IMWRITE_ERROR_ENCODE: BuiltinErrorDescriptor = BuiltinErrorDescriptor {
157 code: "RM.IMWRITE.ENCODE",
158 identifier: Some("RunMat:imwrite:EncodeError"),
159 when: "Image data cannot be encoded.",
160 message: "imwrite: encode error",
161};
162const IMWRITE_ERROR_IO: BuiltinErrorDescriptor = BuiltinErrorDescriptor {
163 code: "RM.IMWRITE.IO",
164 identifier: Some("RunMat:imwrite:Io"),
165 when: "Image file cannot be read for append or written.",
166 message: "imwrite: file I/O error",
167};
168
169const IMWRITE_ERRORS: [BuiltinErrorDescriptor; 8] = [
170 IMWRITE_ERROR_INVALID_ARGUMENT,
171 IMWRITE_ERROR_INVALID_FILENAME,
172 IMWRITE_ERROR_INVALID_FORMAT,
173 IMWRITE_ERROR_INVALID_IMAGE,
174 IMWRITE_ERROR_INVALID_COLORMAP,
175 IMWRITE_ERROR_INVALID_OPTION,
176 IMWRITE_ERROR_ENCODE,
177 IMWRITE_ERROR_IO,
178];
179
180pub const IMWRITE_DESCRIPTOR: BuiltinDescriptor = BuiltinDescriptor {
181 signatures: &IMWRITE_SIGNATURES,
182 output_mode: BuiltinOutputMode::Fixed,
183 completion_policy: BuiltinCompletionPolicy::Public,
184 errors: &IMWRITE_ERRORS,
185};
186
187#[runmat_macros::register_gpu_spec(builtin_path = "crate::builtins::image::imwrite")]
188pub const GPU_SPEC: BuiltinGpuSpec = BuiltinGpuSpec {
189 name: "imwrite",
190 op_kind: GpuOpKind::Custom("image-imwrite"),
191 supported_precisions: &[],
192 broadcast: BroadcastSemantics::None,
193 provider_hooks: &[],
194 constant_strategy: ConstantStrategy::InlineLiteral,
195 residency: ResidencyPolicy::GatherImmediately,
196 nan_mode: ReductionNaN::Include,
197 two_pass_threshold: None,
198 workgroup_size: None,
199 accepts_nan_mode: false,
200 notes: "Host image encoder sink; gpuArray inputs are gathered before writing.",
201};
202
203#[runmat_macros::register_fusion_spec(builtin_path = "crate::builtins::image::imwrite")]
204pub const FUSION_SPEC: BuiltinFusionSpec = BuiltinFusionSpec {
205 name: "imwrite",
206 shape: ShapeRequirements::Any,
207 constant_strategy: ConstantStrategy::InlineLiteral,
208 elementwise: None,
209 reduction: None,
210 emits_nan: false,
211 notes: "File I/O is not eligible for fusion.",
212};
213
214#[runtime_builtin(
215 name = "imwrite",
216 category = "image/io",
217 summary = "Write image data to a file.",
218 keywords = "image,write,imwrite,png,jpeg,gif,bmp,tiff",
219 sink = true,
220 suppress_auto_output = true,
221 type_resolver(imwrite_type),
222 descriptor(crate::builtins::image::imwrite::IMWRITE_DESCRIPTOR),
223 builtin_path = "crate::builtins::image::imwrite"
224)]
225async fn imwrite_builtin(args: Vec<Value>) -> BuiltinResult<Value> {
226 if let Some(n) = crate::output_count::current_output_count() {
227 if n > 0 {
228 return Err(imwrite_error_with_detail(
229 &IMWRITE_ERROR_INVALID_ARGUMENT,
230 "imwrite does not return output arguments",
231 ));
232 }
233 }
234
235 let mut host_args = Vec::with_capacity(args.len());
236 for arg in &args {
237 host_args.push(gather_if_needed_async(arg).await?);
238 }
239
240 let invocation = parse_invocation(&host_args)?;
241 let image = materialize_image(
242 &invocation.image,
243 invocation.map.as_ref(),
244 invocation.alpha.as_ref(),
245 )?;
246 let bytes = encode_image(&image, &invocation).await?;
247 runmat_filesystem::write_async(&invocation.path, &bytes)
248 .await
249 .map_err(|err| {
250 imwrite_error_with_detail(
251 &IMWRITE_ERROR_IO,
252 format!("failed to write '{}': {err}", invocation.path.display()),
253 )
254 })?;
255
256 Ok(Value::OutputList(Vec::new()))
257}
258
259#[derive(Clone, Copy, Debug, PartialEq, Eq)]
260enum WriteMode {
261 Overwrite,
262 Append,
263}
264
265#[derive(Debug)]
266struct ImwriteOptions {
267 quality: u8,
268 delay_time: Option<f64>,
269 loop_count: Option<f64>,
270 write_mode: WriteMode,
271}
272
273impl Default for ImwriteOptions {
274 fn default() -> Self {
275 Self {
276 quality: 75,
277 delay_time: None,
278 loop_count: None,
279 write_mode: WriteMode::Overwrite,
280 }
281 }
282}
283
284#[derive(Debug)]
285struct Invocation {
286 image: Value,
287 map: Option<Value>,
288 alpha: Option<Tensor>,
289 path: PathBuf,
290 format: ImageFormat,
291 options: ImwriteOptions,
292}
293
294#[derive(Clone)]
295struct MaterializedImage {
296 rows: usize,
297 cols: usize,
298 channels: usize,
299 data: PixelData,
300}
301
302#[derive(Clone)]
303enum PixelData {
304 U8(Vec<u8>),
305 U16(Vec<u16>),
306}
307
308fn parse_invocation(args: &[Value]) -> BuiltinResult<Invocation> {
309 if args.len() < 2 {
310 return Err(imwrite_error_with_detail(
311 &IMWRITE_ERROR_INVALID_ARGUMENT,
312 "expected image data and filename",
313 ));
314 }
315
316 let (image, map, filename_index) = if is_string_like(&args[1]) {
317 (args[0].clone(), None, 1usize)
318 } else {
319 if args.len() < 3 {
320 return Err(imwrite_error_with_detail(
321 &IMWRITE_ERROR_INVALID_ARGUMENT,
322 "indexed images require X, map, and filename",
323 ));
324 }
325 (args[0].clone(), Some(args[1].clone()), 2usize)
326 };
327
328 let filename = string_arg(
329 "filename",
330 &args[filename_index],
331 &IMWRITE_ERROR_INVALID_FILENAME,
332 )?;
333 if filename.trim().is_empty() {
334 return Err(imwrite_error_with_detail(
335 &IMWRITE_ERROR_INVALID_FILENAME,
336 "filename must not be empty",
337 ));
338 }
339 let path = PathBuf::from(filename);
340 let mut idx = filename_index + 1;
341
342 let mut explicit_format = None;
343 if idx < args.len() {
344 if let Some(text) = tensor::value_to_string(&args[idx]) {
345 if !is_option_name(&text) {
346 explicit_format = Some(parse_format_hint(&text)?);
347 idx += 1;
348 }
349 }
350 }
351
352 let mut options = ImwriteOptions::default();
353 let mut alpha = None;
354 while idx < args.len() {
355 let name = string_arg("option name", &args[idx], &IMWRITE_ERROR_INVALID_OPTION)?;
356 idx += 1;
357 if idx >= args.len() {
358 return Err(imwrite_error_with_detail(
359 &IMWRITE_ERROR_INVALID_OPTION,
360 format!("option '{name}' requires a value"),
361 ));
362 }
363 let value = &args[idx];
364 idx += 1;
365
366 match canonical_option_name(&name).as_str() {
367 "alpha" => alpha = Some(tensor_from_numeric_like(value, "Alpha")?),
368 "quality" => {
369 let q = numeric_scalar(value, "Quality")?;
370 if !q.is_finite() || !(0.0..=100.0).contains(&q) {
371 return Err(imwrite_error_with_detail(
372 &IMWRITE_ERROR_INVALID_OPTION,
373 "Quality must be a scalar from 0 to 100",
374 ));
375 }
376 options.quality = q.round() as u8;
377 }
378 "writemode" => {
379 let mode = string_arg("WriteMode", value, &IMWRITE_ERROR_INVALID_OPTION)?;
380 options.write_mode = match mode.trim().to_ascii_lowercase().as_str() {
381 "overwrite" => WriteMode::Overwrite,
382 "append" => WriteMode::Append,
383 _ => {
384 return Err(imwrite_error_with_detail(
385 &IMWRITE_ERROR_INVALID_OPTION,
386 "WriteMode must be 'overwrite' or 'append'",
387 ))
388 }
389 };
390 }
391 "delaytime" => {
392 let delay = numeric_scalar(value, "DelayTime")?;
393 if !delay.is_finite() || delay < 0.0 {
394 return Err(imwrite_error_with_detail(
395 &IMWRITE_ERROR_INVALID_OPTION,
396 "DelayTime must be a finite non-negative scalar in seconds",
397 ));
398 }
399 options.delay_time = Some(delay);
400 }
401 "loopcount" => {
402 let count = numeric_scalar(value, "LoopCount")?;
403 if count.is_nan() || count < 0.0 {
404 return Err(imwrite_error_with_detail(
405 &IMWRITE_ERROR_INVALID_OPTION,
406 "LoopCount must be non-negative or Inf",
407 ));
408 }
409 options.loop_count = Some(count);
410 }
411 "compression" | "bitdepth" | "mode" | "disposalmethod" | "backgroundcolor"
412 | "comment" | "transparentcolor" => {
413 return Err(imwrite_error_with_detail(
414 &IMWRITE_ERROR_INVALID_OPTION,
415 format!("option '{name}' is not supported yet"),
416 ));
417 }
418 _ => {
419 return Err(imwrite_error_with_detail(
420 &IMWRITE_ERROR_INVALID_OPTION,
421 format!("unsupported option '{name}'"),
422 ))
423 }
424 }
425 }
426
427 let format = match explicit_format {
428 Some(format) => format,
429 None => infer_format_from_path(&path)?,
430 };
431
432 Ok(Invocation {
433 image,
434 map,
435 alpha,
436 path,
437 format,
438 options,
439 })
440}
441
442fn is_string_like(value: &Value) -> bool {
443 tensor::value_to_string(value).is_some()
444}
445
446fn string_arg(
447 label: &str,
448 value: &Value,
449 error: &'static BuiltinErrorDescriptor,
450) -> BuiltinResult<String> {
451 tensor::value_to_string(value).ok_or_else(|| {
452 imwrite_error_with_detail(
453 error,
454 format!("{label} must be a string scalar or char vector"),
455 )
456 })
457}
458
459fn numeric_scalar(value: &Value, label: &str) -> BuiltinResult<f64> {
460 match value {
461 Value::Num(n) => Ok(*n),
462 Value::Int(i) => Ok(i.to_f64()),
463 Value::Bool(b) => Ok(if *b { 1.0 } else { 0.0 }),
464 Value::Tensor(t) if t.data.len() == 1 => Ok(t.data[0]),
465 Value::LogicalArray(a) if a.data.len() == 1 => Ok(if a.data[0] != 0 { 1.0 } else { 0.0 }),
466 _ => Err(imwrite_error_with_detail(
467 &IMWRITE_ERROR_INVALID_OPTION,
468 format!("{label} must be a numeric scalar"),
469 )),
470 }
471}
472
473fn canonical_option_name(name: &str) -> String {
474 name.chars()
475 .filter(|ch| !ch.is_whitespace() && *ch != '_' && *ch != '-')
476 .flat_map(char::to_lowercase)
477 .collect()
478}
479
480fn is_option_name(name: &str) -> bool {
481 matches!(
482 canonical_option_name(name).as_str(),
483 "alpha"
484 | "quality"
485 | "writemode"
486 | "delaytime"
487 | "loopcount"
488 | "compression"
489 | "bitdepth"
490 | "mode"
491 | "disposalmethod"
492 | "backgroundcolor"
493 | "comment"
494 | "transparentcolor"
495 )
496}
497
498fn parse_format_hint(value: &str) -> BuiltinResult<ImageFormat> {
499 let label = value.trim().trim_start_matches('.').to_ascii_lowercase();
500 if label.is_empty() {
501 return Err(imwrite_error_with_detail(
502 &IMWRITE_ERROR_INVALID_FORMAT,
503 "format hint must not be empty",
504 ));
505 }
506 match label.as_str() {
507 "jpg" | "jpeg" | "jpe" => Ok(ImageFormat::Jpeg),
508 "png" => Ok(ImageFormat::Png),
509 "bmp" => Ok(ImageFormat::Bmp),
510 "gif" => Ok(ImageFormat::Gif),
511 "tif" | "tiff" => Ok(ImageFormat::Tiff),
512 other => ImageFormat::from_extension(other)
513 .filter(is_supported_format)
514 .ok_or_else(|| {
515 imwrite_error_with_detail(
516 &IMWRITE_ERROR_INVALID_FORMAT,
517 format!("unsupported image format '{other}'"),
518 )
519 }),
520 }
521}
522
523fn infer_format_from_path(path: &Path) -> BuiltinResult<ImageFormat> {
524 ImageFormat::from_path(path)
525 .ok()
526 .filter(is_supported_format)
527 .ok_or_else(|| {
528 imwrite_error_with_detail(
529 &IMWRITE_ERROR_INVALID_FORMAT,
530 format!(
531 "could not infer supported image format from '{}'",
532 path.display()
533 ),
534 )
535 })
536}
537
538fn is_supported_format(format: &ImageFormat) -> bool {
539 matches!(
540 format,
541 ImageFormat::Png
542 | ImageFormat::Jpeg
543 | ImageFormat::Bmp
544 | ImageFormat::Gif
545 | ImageFormat::Tiff
546 )
547}
548
549fn tensor_from_numeric_like(value: &Value, label: &str) -> BuiltinResult<Tensor> {
550 match value {
551 Value::Tensor(t) => Ok(t.clone()),
552 Value::LogicalArray(a) => logical_to_tensor(a),
553 Value::Num(n) => Tensor::new(vec![*n], vec![1, 1]).map_err(|err| {
554 imwrite_error_with_detail(&IMWRITE_ERROR_INVALID_IMAGE, format!("{label}: {err}"))
555 }),
556 Value::Int(i) => Tensor::new(vec![i.to_f64()], vec![1, 1]).map_err(|err| {
557 imwrite_error_with_detail(&IMWRITE_ERROR_INVALID_IMAGE, format!("{label}: {err}"))
558 }),
559 Value::Bool(b) => {
560 Tensor::new(vec![if *b { 1.0 } else { 0.0 }], vec![1, 1]).map_err(|err| {
561 imwrite_error_with_detail(&IMWRITE_ERROR_INVALID_IMAGE, format!("{label}: {err}"))
562 })
563 }
564 _ => Err(imwrite_error_with_detail(
565 &IMWRITE_ERROR_INVALID_IMAGE,
566 format!("{label} must be numeric or logical"),
567 )),
568 }
569}
570
571fn logical_to_tensor(value: &LogicalArray) -> BuiltinResult<Tensor> {
572 let data = value
573 .data
574 .iter()
575 .map(|&b| if b != 0 { 1.0 } else { 0.0 })
576 .collect::<Vec<_>>();
577 Tensor::new(data, value.shape.clone())
578 .map_err(|err| imwrite_error_with_detail(&IMWRITE_ERROR_INVALID_IMAGE, err))
579}
580
581fn materialize_image(
582 image: &Value,
583 map: Option<&Value>,
584 alpha: Option<&Tensor>,
585) -> BuiltinResult<MaterializedImage> {
586 let tensor = tensor_from_numeric_like(image, "image")?;
587 let mut out = if let Some(map_value) = map {
588 materialize_indexed_image(&tensor, &tensor_from_numeric_like(map_value, "map")?)?
589 } else {
590 materialize_direct_image(&tensor)?
591 };
592
593 if let Some(alpha) = alpha {
594 apply_alpha(&mut out, alpha)?;
595 }
596 Ok(out)
597}
598
599fn image_dimensions(tensor: &Tensor) -> BuiltinResult<(usize, usize, usize)> {
600 match tensor.shape.len() {
601 0 => Ok((1, 1, 1)),
602 1 => Ok((1, tensor.shape[0], 1)),
603 2 => Ok((tensor.shape[0], tensor.shape[1], 1)),
604 3 if matches!(tensor.shape[2], 1 | 3 | 4) => {
605 Ok((tensor.shape[0], tensor.shape[1], tensor.shape[2]))
606 }
607 _ => Err(imwrite_error_with_detail(
608 &IMWRITE_ERROR_INVALID_IMAGE,
609 "image must be MxN, MxNx3, or MxNx4",
610 )),
611 }
612}
613
614fn materialize_direct_image(tensor: &Tensor) -> BuiltinResult<MaterializedImage> {
615 let (rows, cols, channels) = image_dimensions(tensor)?;
616 let pixels = rows.checked_mul(cols).ok_or_else(|| {
617 imwrite_error_with_detail(&IMWRITE_ERROR_INVALID_IMAGE, "image dimensions overflow")
618 })?;
619 if tensor.data.len() != pixels * channels {
620 return Err(imwrite_error_with_detail(
621 &IMWRITE_ERROR_INVALID_IMAGE,
622 "image data length does not match shape",
623 ));
624 }
625
626 let mut data = if tensor.dtype == NumericDType::U16 {
627 PixelData::U16(vec![0u16; pixels * channels])
628 } else {
629 PixelData::U8(vec![0u8; pixels * channels])
630 };
631 for row in 0..rows {
632 for col in 0..cols {
633 for channel in 0..channels {
634 let src = row + rows * col + pixels * channel;
635 let dst = (row * cols + col) * channels + channel;
636 match &mut data {
637 PixelData::U8(data) => data[dst] = value_to_u8(tensor.data[src], tensor.dtype),
638 PixelData::U16(data) => {
639 data[dst] = value_to_u16(tensor.data[src], tensor.dtype)
640 }
641 }
642 }
643 }
644 }
645 Ok(MaterializedImage {
646 rows,
647 cols,
648 channels,
649 data,
650 })
651}
652
653fn materialize_indexed_image(indexed: &Tensor, map: &Tensor) -> BuiltinResult<MaterializedImage> {
654 let (rows, cols, channels) = image_dimensions(indexed)?;
655 if channels != 1 {
656 return Err(imwrite_error_with_detail(
657 &IMWRITE_ERROR_INVALID_IMAGE,
658 "indexed image X must be a 2-D array",
659 ));
660 }
661 if map.shape.len() != 2 || map.shape[1] != 3 || map.shape[0] == 0 {
662 return Err(imwrite_error_with_detail(
663 &IMWRITE_ERROR_INVALID_COLORMAP,
664 "map must be an Nx3 colormap",
665 ));
666 }
667
668 let pixels = rows.checked_mul(cols).ok_or_else(|| {
669 imwrite_error_with_detail(&IMWRITE_ERROR_INVALID_IMAGE, "image dimensions overflow")
670 })?;
671 let byte_len = pixels.checked_mul(3).ok_or_else(|| {
672 imwrite_error_with_detail(&IMWRITE_ERROR_INVALID_IMAGE, "image dimensions overflow")
673 })?;
674 let mut data = vec![0u8; byte_len];
675 for row in 0..rows {
676 for col in 0..cols {
677 let pixel = row + rows * col;
678 let map_idx = map_index(indexed.data[pixel], indexed.dtype, map.shape[0])?;
679 let dst = (row * cols + col) * 3;
680 for channel in 0..3 {
681 let src = map_idx + map.shape[0] * channel;
682 data[dst + channel] = value_to_u8(map.data[src], map.dtype);
683 }
684 }
685 }
686 Ok(MaterializedImage {
687 rows,
688 cols,
689 channels: 3,
690 data: PixelData::U8(data),
691 })
692}
693
694fn map_index(value: f64, dtype: NumericDType, map_rows: usize) -> BuiltinResult<usize> {
695 if !value.is_finite() {
696 return Err(imwrite_error_with_detail(
697 &IMWRITE_ERROR_INVALID_IMAGE,
698 "indexed image values must be finite",
699 ));
700 }
701 let index = if matches!(dtype, NumericDType::U8 | NumericDType::U16) {
702 value.round() as isize
703 } else {
704 value.round() as isize - 1
705 };
706 if index < 0 || index as usize >= map_rows {
707 return Err(imwrite_error_with_detail(
708 &IMWRITE_ERROR_INVALID_IMAGE,
709 format!("indexed image value {value} is outside the colormap"),
710 ));
711 }
712 Ok(index as usize)
713}
714
715fn value_to_u8(value: f64, dtype: NumericDType) -> u8 {
716 let scaled = match dtype {
717 NumericDType::U8 => value,
718 NumericDType::U16 => value / 257.0,
719 NumericDType::F64 | NumericDType::F32 => value.clamp(0.0, 1.0) * 255.0,
720 };
721 if scaled.is_nan() {
722 0
723 } else {
724 scaled.round().clamp(0.0, 255.0) as u8
725 }
726}
727
728fn value_to_u16(value: f64, dtype: NumericDType) -> u16 {
729 let scaled = match dtype {
730 NumericDType::U16 => value,
731 NumericDType::U8 => value * 257.0,
732 NumericDType::F64 | NumericDType::F32 => value.clamp(0.0, 1.0) * 65535.0,
733 };
734 if scaled.is_nan() {
735 0
736 } else {
737 scaled.round().clamp(0.0, 65535.0) as u16
738 }
739}
740
741fn apply_alpha(image: &mut MaterializedImage, alpha: &Tensor) -> BuiltinResult<()> {
742 if alpha.shape.len() != 2 || alpha.shape[0] != image.rows || alpha.shape[1] != image.cols {
743 return Err(imwrite_error_with_detail(
744 &IMWRITE_ERROR_INVALID_OPTION,
745 "Alpha must be an MxN array matching the image dimensions",
746 ));
747 }
748 if alpha.data.len() != image.rows * image.cols {
749 return Err(imwrite_error_with_detail(
750 &IMWRITE_ERROR_INVALID_OPTION,
751 "Alpha data length does not match shape",
752 ));
753 }
754
755 let pixels = image.rows * image.cols;
756 image.data = match &image.data {
757 PixelData::U8(data) => {
758 let mut rgba = vec![0u8; pixels * 4];
759 for row in 0..image.rows {
760 for col in 0..image.cols {
761 let pixel = row * image.cols + col;
762 let alpha_idx = row + image.rows * col;
763 let dst = pixel * 4;
764 match image.channels {
765 1 => {
766 let gray = data[pixel];
767 rgba[dst] = gray;
768 rgba[dst + 1] = gray;
769 rgba[dst + 2] = gray;
770 }
771 3 | 4 => {
772 let src = pixel * image.channels;
773 rgba[dst] = data[src];
774 rgba[dst + 1] = data[src + 1];
775 rgba[dst + 2] = data[src + 2];
776 }
777 _ => unreachable!(),
778 }
779 rgba[dst + 3] = value_to_u8(alpha.data[alpha_idx], alpha.dtype);
780 }
781 }
782 PixelData::U8(rgba)
783 }
784 PixelData::U16(data) => {
785 let mut rgba = vec![0u16; pixels * 4];
786 for row in 0..image.rows {
787 for col in 0..image.cols {
788 let pixel = row * image.cols + col;
789 let alpha_idx = row + image.rows * col;
790 let dst = pixel * 4;
791 match image.channels {
792 1 => {
793 let gray = data[pixel];
794 rgba[dst] = gray;
795 rgba[dst + 1] = gray;
796 rgba[dst + 2] = gray;
797 }
798 3 | 4 => {
799 let src = pixel * image.channels;
800 rgba[dst] = data[src];
801 rgba[dst + 1] = data[src + 1];
802 rgba[dst + 2] = data[src + 2];
803 }
804 _ => unreachable!(),
805 }
806 rgba[dst + 3] = value_to_u16(alpha.data[alpha_idx], alpha.dtype);
807 }
808 }
809 PixelData::U16(rgba)
810 }
811 };
812 image.channels = 4;
813 Ok(())
814}
815
816async fn encode_image(
817 image: &MaterializedImage,
818 invocation: &Invocation,
819) -> BuiltinResult<Vec<u8>> {
820 if invocation.options.write_mode == WriteMode::Append && invocation.format != ImageFormat::Gif {
821 return Err(imwrite_error_with_detail(
822 &IMWRITE_ERROR_INVALID_OPTION,
823 "WriteMode 'append' is supported for GIF files only",
824 ));
825 }
826
827 match invocation.format {
828 ImageFormat::Gif => encode_gif(image, invocation).await,
829 ImageFormat::Jpeg => {
830 if image.channels == 4 {
831 return Err(imwrite_error_with_detail(
832 &IMWRITE_ERROR_INVALID_OPTION,
833 "JPEG does not support alpha channels",
834 ));
835 }
836 write_dynamic_image(
837 image_to_dynamic(&image_as_8bit(image), false)?,
838 ImageOutputFormat::Jpeg(invocation.options.quality),
839 )
840 }
841 ImageFormat::Bmp => {
842 if image.channels == 4 {
843 return Err(imwrite_error_with_detail(
844 &IMWRITE_ERROR_INVALID_OPTION,
845 "BMP alpha output is not supported",
846 ));
847 }
848 write_dynamic_image(
849 image_to_dynamic(&image_as_8bit(image), false)?,
850 ImageOutputFormat::Bmp,
851 )
852 }
853 ImageFormat::Png => {
854 write_dynamic_image(image_to_dynamic(image, true)?, ImageOutputFormat::Png)
855 }
856 ImageFormat::Tiff => {
857 write_dynamic_image(image_to_dynamic(image, true)?, ImageOutputFormat::Tiff)
858 }
859 _ => Err(imwrite_error_with_detail(
860 &IMWRITE_ERROR_INVALID_FORMAT,
861 "unsupported image format",
862 )),
863 }
864}
865
866fn image_to_dynamic(image: &MaterializedImage, keep_alpha: bool) -> BuiltinResult<DynamicImage> {
867 let width = u32::try_from(image.cols).map_err(|_| {
868 imwrite_error_with_detail(&IMWRITE_ERROR_INVALID_IMAGE, "image width is too large")
869 })?;
870 let height = u32::try_from(image.rows).map_err(|_| {
871 imwrite_error_with_detail(&IMWRITE_ERROR_INVALID_IMAGE, "image height is too large")
872 })?;
873
874 match image.channels {
875 1 => match &image.data {
876 PixelData::U8(data) => {
877 ImageBuffer::<Luma<u8>, _>::from_raw(width, height, data.clone())
878 .map(DynamicImage::ImageLuma8)
879 .ok_or_else(|| {
880 imwrite_error_with_detail(
881 &IMWRITE_ERROR_INVALID_IMAGE,
882 "invalid grayscale image buffer",
883 )
884 })
885 }
886 PixelData::U16(data) => {
887 ImageBuffer::<Luma<u16>, _>::from_raw(width, height, data.clone())
888 .map(DynamicImage::ImageLuma16)
889 .ok_or_else(|| {
890 imwrite_error_with_detail(
891 &IMWRITE_ERROR_INVALID_IMAGE,
892 "invalid grayscale image buffer",
893 )
894 })
895 }
896 },
897 3 => match &image.data {
898 PixelData::U8(data) => ImageBuffer::<Rgb<u8>, _>::from_raw(width, height, data.clone())
899 .map(DynamicImage::ImageRgb8)
900 .ok_or_else(|| {
901 imwrite_error_with_detail(
902 &IMWRITE_ERROR_INVALID_IMAGE,
903 "invalid RGB image buffer",
904 )
905 }),
906 PixelData::U16(data) => {
907 ImageBuffer::<Rgb<u16>, _>::from_raw(width, height, data.clone())
908 .map(DynamicImage::ImageRgb16)
909 .ok_or_else(|| {
910 imwrite_error_with_detail(
911 &IMWRITE_ERROR_INVALID_IMAGE,
912 "invalid RGB image buffer",
913 )
914 })
915 }
916 },
917 4 if keep_alpha => match &image.data {
918 PixelData::U8(data) => {
919 ImageBuffer::<Rgba<u8>, _>::from_raw(width, height, data.clone())
920 .map(DynamicImage::ImageRgba8)
921 .ok_or_else(|| {
922 imwrite_error_with_detail(
923 &IMWRITE_ERROR_INVALID_IMAGE,
924 "invalid RGBA image buffer",
925 )
926 })
927 }
928 PixelData::U16(data) => {
929 ImageBuffer::<Rgba<u16>, _>::from_raw(width, height, data.clone())
930 .map(DynamicImage::ImageRgba16)
931 .ok_or_else(|| {
932 imwrite_error_with_detail(
933 &IMWRITE_ERROR_INVALID_IMAGE,
934 "invalid RGBA image buffer",
935 )
936 })
937 }
938 },
939 4 => match &image.data {
940 PixelData::U8(data) => {
941 let mut rgb = Vec::with_capacity(image.rows * image.cols * 3);
942 for chunk in data.chunks_exact(4) {
943 rgb.extend_from_slice(&chunk[..3]);
944 }
945 ImageBuffer::<Rgb<u8>, _>::from_raw(width, height, rgb)
946 .map(DynamicImage::ImageRgb8)
947 .ok_or_else(|| {
948 imwrite_error_with_detail(
949 &IMWRITE_ERROR_INVALID_IMAGE,
950 "invalid RGB image buffer",
951 )
952 })
953 }
954 PixelData::U16(data) => {
955 let mut rgb = Vec::with_capacity(image.rows * image.cols * 3);
956 for chunk in data.chunks_exact(4) {
957 rgb.extend_from_slice(&chunk[..3]);
958 }
959 ImageBuffer::<Rgb<u16>, _>::from_raw(width, height, rgb)
960 .map(DynamicImage::ImageRgb16)
961 .ok_or_else(|| {
962 imwrite_error_with_detail(
963 &IMWRITE_ERROR_INVALID_IMAGE,
964 "invalid RGB image buffer",
965 )
966 })
967 }
968 },
969 _ => Err(imwrite_error_with_detail(
970 &IMWRITE_ERROR_INVALID_IMAGE,
971 "image must have 1, 3, or 4 channels",
972 )),
973 }
974}
975
976fn write_dynamic_image(image: DynamicImage, format: ImageOutputFormat) -> BuiltinResult<Vec<u8>> {
977 let mut cursor = Cursor::new(Vec::new());
978 image.write_to(&mut cursor, format).map_err(|err| {
979 imwrite_error_with_detail(
980 &IMWRITE_ERROR_ENCODE,
981 format!("unable to encode image: {err}"),
982 )
983 })?;
984 Ok(cursor.into_inner())
985}
986
987async fn encode_gif(image: &MaterializedImage, invocation: &Invocation) -> BuiltinResult<Vec<u8>> {
988 let mut frames = Vec::new();
989 let mut existing_repeat = None;
990 if invocation.options.write_mode == WriteMode::Append {
991 let existing = runmat_filesystem::read_async(&invocation.path)
994 .await
995 .map_err(|err| {
996 imwrite_error_with_detail(
997 &IMWRITE_ERROR_IO,
998 format!(
999 "failed to read GIF for append '{}': {err}",
1000 invocation.path.display()
1001 ),
1002 )
1003 })?;
1004 existing_repeat = gif_repeat_from_bytes(&existing);
1005 let decoder = GifDecoder::new(Cursor::new(existing)).map_err(|err| {
1006 imwrite_error_with_detail(
1007 &IMWRITE_ERROR_ENCODE,
1008 format!("failed to decode GIF: {err}"),
1009 )
1010 })?;
1011 for frame in decoder.into_frames() {
1012 frames.push(frame.map_err(|err| {
1013 imwrite_error_with_detail(
1014 &IMWRITE_ERROR_ENCODE,
1015 format!("failed to decode GIF frame: {err}"),
1016 )
1017 })?);
1018 }
1019 }
1020 frames.push(gif_frame_from_image(image, invocation.options.delay_time)?);
1021
1022 let mut bytes = Vec::new();
1023 {
1024 let mut encoder = GifEncoder::new(&mut bytes);
1025 let repeat = if let Some(loop_count) = invocation.options.loop_count {
1026 Some(loop_count_to_repeat(loop_count)?)
1027 } else {
1028 existing_repeat
1029 };
1030 if let Some(repeat) = repeat {
1031 encoder.set_repeat(repeat).map_err(|err| {
1032 imwrite_error_with_detail(
1033 &IMWRITE_ERROR_ENCODE,
1034 format!("failed to set GIF repeat: {err}"),
1035 )
1036 })?;
1037 }
1038 for frame in frames {
1039 encoder.encode_frame(frame).map_err(|err| {
1040 imwrite_error_with_detail(
1041 &IMWRITE_ERROR_ENCODE,
1042 format!("failed to encode GIF frame: {err}"),
1043 )
1044 })?;
1045 }
1046 }
1047 Ok(bytes)
1048}
1049
1050fn gif_repeat_from_bytes(bytes: &[u8]) -> Option<Repeat> {
1051 const APP_EXT_PREFIX: &[u8] = b"\x21\xFF\x0BNETSCAPE2.0\x03\x01";
1052 bytes.windows(APP_EXT_PREFIX.len() + 3).find_map(|window| {
1053 if !window.starts_with(APP_EXT_PREFIX) || window[APP_EXT_PREFIX.len() + 2] != 0 {
1054 return None;
1055 }
1056 let lo = window[APP_EXT_PREFIX.len()];
1057 let hi = window[APP_EXT_PREFIX.len() + 1];
1058 let count = u16::from_le_bytes([lo, hi]);
1059 if count == 0 {
1060 Some(Repeat::Infinite)
1061 } else {
1062 Some(Repeat::Finite(count))
1063 }
1064 })
1065}
1066
1067fn loop_count_to_repeat(loop_count: f64) -> BuiltinResult<Repeat> {
1068 if loop_count.is_infinite() {
1069 return Ok(Repeat::Infinite);
1070 }
1071 let rounded = loop_count.round();
1072 if (rounded - loop_count).abs() > 1e-6 || rounded > u16::MAX as f64 {
1073 return Err(imwrite_error_with_detail(
1074 &IMWRITE_ERROR_INVALID_OPTION,
1075 "LoopCount must be an integer between 0 and 65535, or Inf",
1076 ));
1077 }
1078 Ok(Repeat::Finite(rounded as u16))
1079}
1080
1081fn gif_frame_from_image(
1082 image: &MaterializedImage,
1083 delay_time: Option<f64>,
1084) -> BuiltinResult<Frame> {
1085 let width = u32::try_from(image.cols).map_err(|_| {
1086 imwrite_error_with_detail(&IMWRITE_ERROR_INVALID_IMAGE, "image width is too large")
1087 })?;
1088 let height = u32::try_from(image.rows).map_err(|_| {
1089 imwrite_error_with_detail(&IMWRITE_ERROR_INVALID_IMAGE, "image height is too large")
1090 })?;
1091
1092 let mut rgba = vec![0u8; image.rows * image.cols * 4];
1093 let data = image_data_as_u8(image);
1094 for pixel in 0..image.rows * image.cols {
1095 let dst = pixel * 4;
1096 match image.channels {
1097 1 => {
1098 let gray = data[pixel];
1099 rgba[dst] = gray;
1100 rgba[dst + 1] = gray;
1101 rgba[dst + 2] = gray;
1102 rgba[dst + 3] = 255;
1103 }
1104 3 => {
1105 let src = pixel * 3;
1106 rgba[dst..dst + 3].copy_from_slice(&data[src..src + 3]);
1107 rgba[dst + 3] = 255;
1108 }
1109 4 => {
1110 let src = pixel * 4;
1111 rgba[dst..dst + 4].copy_from_slice(&data[src..src + 4]);
1112 }
1113 _ => unreachable!(),
1114 }
1115 }
1116 let image = RgbaImage::from_raw(width, height, rgba).ok_or_else(|| {
1117 imwrite_error_with_detail(&IMWRITE_ERROR_INVALID_IMAGE, "invalid GIF frame buffer")
1118 })?;
1119 let delay = delay_time
1120 .map(|seconds| Delay::from_saturating_duration(Duration::from_secs_f64(seconds)))
1121 .unwrap_or_else(|| Delay::from_numer_denom_ms(0, 1));
1122 Ok(Frame::from_parts(image, 0, 0, delay))
1123}
1124
1125fn image_data_as_u8(image: &MaterializedImage) -> Vec<u8> {
1126 match &image.data {
1127 PixelData::U8(data) => data.clone(),
1128 PixelData::U16(data) => data
1129 .iter()
1130 .map(|value| ((*value as f64) / 257.0).round().clamp(0.0, 255.0) as u8)
1131 .collect(),
1132 }
1133}
1134
1135fn image_as_8bit(image: &MaterializedImage) -> MaterializedImage {
1136 MaterializedImage {
1137 rows: image.rows,
1138 cols: image.cols,
1139 channels: image.channels,
1140 data: PixelData::U8(image_data_as_u8(image)),
1141 }
1142}
1143
1144fn imwrite_error_with_detail(
1145 error: &'static BuiltinErrorDescriptor,
1146 message: impl Into<String>,
1147) -> RuntimeError {
1148 let mut builder = build_runtime_error(message).with_builtin(BUILTIN_NAME);
1149 if let Some(identifier) = error.identifier {
1150 builder = builder.with_identifier(identifier);
1151 }
1152 builder.build()
1153}
1154
1155#[cfg(test)]
1156mod tests {
1157 use super::*;
1158 use futures::executor::block_on;
1159 use image::io::Reader as ImageReader;
1160 use std::fs;
1161 use tempfile::tempdir;
1162
1163 fn tensor(data: Vec<f64>, shape: Vec<usize>, dtype: NumericDType) -> Tensor {
1164 Tensor::new_with_dtype(data, shape, dtype).expect("tensor")
1165 }
1166
1167 fn call(args: Vec<Value>) -> BuiltinResult<Value> {
1168 block_on(imwrite_builtin(args))
1169 }
1170
1171 #[test]
1172 fn writes_png_rgb_and_round_trips_layout() {
1173 let dir = tempdir().unwrap();
1174 let path = dir.path().join("rgb.png");
1175 let rgb = tensor(
1176 vec![255.0, 0.0, 0.0, 0.0, 0.0, 255.0],
1177 vec![1, 2, 3],
1178 NumericDType::U8,
1179 );
1180
1181 call(vec![
1182 Value::Tensor(rgb),
1183 Value::from(path.to_string_lossy().as_ref()),
1184 ])
1185 .unwrap();
1186
1187 let decoded = ImageReader::open(&path)
1188 .unwrap()
1189 .decode()
1190 .unwrap()
1191 .to_rgb8();
1192 assert_eq!(decoded.dimensions(), (2, 1));
1193 assert_eq!(decoded.get_pixel(0, 0).0, [255, 0, 0]);
1194 assert_eq!(decoded.get_pixel(1, 0).0, [0, 0, 255]);
1195 }
1196
1197 #[test]
1198 fn writes_png_alpha_option() {
1199 let dir = tempdir().unwrap();
1200 let path = dir.path().join("alpha.png");
1201 let image = tensor(vec![1.0, 0.0, 0.0], vec![1, 1, 3], NumericDType::F64);
1202 let alpha = tensor(vec![0.5], vec![1, 1], NumericDType::F64);
1203
1204 call(vec![
1205 Value::Tensor(image),
1206 Value::from(path.to_string_lossy().as_ref()),
1207 Value::from("Alpha"),
1208 Value::Tensor(alpha),
1209 ])
1210 .unwrap();
1211
1212 let decoded = ImageReader::open(&path)
1213 .unwrap()
1214 .decode()
1215 .unwrap()
1216 .to_rgba8();
1217 assert_eq!(decoded.get_pixel(0, 0).0, [255, 0, 0, 128]);
1218 }
1219
1220 #[test]
1221 fn writes_uint16_png_without_downcasting() {
1222 let dir = tempdir().unwrap();
1223 let path = dir.path().join("gray16.png");
1224 let image = tensor(
1225 vec![0.0, 65535.0, 12345.0, 40000.0],
1226 vec![2, 2],
1227 NumericDType::U16,
1228 );
1229
1230 call(vec![
1231 Value::Tensor(image),
1232 Value::from(path.to_string_lossy().as_ref()),
1233 ])
1234 .unwrap();
1235
1236 let decoded = ImageReader::open(&path).unwrap().decode().unwrap();
1237 let gray = decoded.as_luma16().expect("expected 16-bit grayscale PNG");
1238 assert_eq!(gray.dimensions(), (2, 2));
1239 assert_eq!(gray.get_pixel(0, 0).0, [0]);
1240 assert_eq!(gray.get_pixel(0, 1).0, [65535]);
1241 assert_eq!(gray.get_pixel(1, 0).0, [12345]);
1242 assert_eq!(gray.get_pixel(1, 1).0, [40000]);
1243 }
1244
1245 #[test]
1246 fn writes_indexed_gif_with_zero_based_uint8_indices() {
1247 let dir = tempdir().unwrap();
1248 let path = dir.path().join("indexed.gif");
1249 let x = tensor(vec![0.0, 1.0], vec![1, 2], NumericDType::U8);
1250 let map = tensor(
1251 vec![255.0, 0.0, 0.0, 0.0, 0.0, 255.0],
1252 vec![2, 3],
1253 NumericDType::U8,
1254 );
1255
1256 call(vec![
1257 Value::Tensor(x),
1258 Value::Tensor(map),
1259 Value::from(path.to_string_lossy().as_ref()),
1260 ])
1261 .unwrap();
1262
1263 let decoded = ImageReader::open(&path)
1264 .unwrap()
1265 .decode()
1266 .unwrap()
1267 .to_rgb8();
1268 assert_eq!(decoded.dimensions(), (2, 1));
1269 assert_eq!(decoded.get_pixel(0, 0).0, [255, 0, 0]);
1270 assert_eq!(decoded.get_pixel(1, 0).0, [0, 0, 255]);
1271 }
1272
1273 #[test]
1274 fn appends_gif_frame() {
1275 let dir = tempdir().unwrap();
1276 let path = dir.path().join("animated.gif");
1277 let first = tensor(vec![1.0, 0.0, 0.0], vec![1, 1, 3], NumericDType::F64);
1278 let second = tensor(vec![0.0, 1.0, 0.0], vec![1, 1, 3], NumericDType::F64);
1279
1280 call(vec![
1281 Value::Tensor(first),
1282 Value::from(path.to_string_lossy().as_ref()),
1283 Value::from("LoopCount"),
1284 Value::Num(f64::INFINITY),
1285 Value::from("DelayTime"),
1286 Value::Num(0.25),
1287 ])
1288 .unwrap();
1289 call(vec![
1290 Value::Tensor(second),
1291 Value::from(path.to_string_lossy().as_ref()),
1292 Value::from("WriteMode"),
1293 Value::from("append"),
1294 Value::from("DelayTime"),
1295 Value::Num(0.25),
1296 ])
1297 .unwrap();
1298
1299 let bytes = fs::read(&path).unwrap();
1300 assert!(matches!(
1301 gif_repeat_from_bytes(&bytes),
1302 Some(Repeat::Infinite)
1303 ));
1304 let decoder = GifDecoder::new(Cursor::new(bytes)).unwrap();
1305 let frames = decoder.into_frames().collect_frames().unwrap();
1306 assert_eq!(frames.len(), 2);
1307 }
1308
1309 #[test]
1310 fn rejects_alpha_for_jpeg() {
1311 let dir = tempdir().unwrap();
1312 let path = dir.path().join("bad.jpg");
1313 let image = tensor(vec![1.0, 0.0, 0.0], vec![1, 1, 3], NumericDType::F64);
1314 let alpha = tensor(vec![1.0], vec![1, 1], NumericDType::F64);
1315
1316 let err = call(vec![
1317 Value::Tensor(image),
1318 Value::from(path.to_string_lossy().as_ref()),
1319 Value::from("Alpha"),
1320 Value::Tensor(alpha),
1321 ])
1322 .unwrap_err();
1323 assert_eq!(err.identifier(), Some("RunMat:imwrite:InvalidOption"));
1324 }
1325
1326 #[test]
1327 fn descriptor_has_stable_errors() {
1328 let codes: Vec<&str> = IMWRITE_DESCRIPTOR
1329 .errors
1330 .iter()
1331 .map(|error| error.code)
1332 .collect();
1333 assert!(codes.contains(&"RM.IMWRITE.INVALID_IMAGE"));
1334 assert!(codes.contains(&"RM.IMWRITE.ENCODE"));
1335 }
1336}