|
1 | | -use crate::{decoder::ifd, encoder::TiffValue}; |
| 1 | +use crate::encoder::TiffValue; |
2 | 2 |
|
3 | 3 | macro_rules! tags { |
4 | 4 | { |
@@ -403,111 +403,6 @@ impl ValueBuffer { |
403 | 403 | } |
404 | 404 | } |
405 | 405 |
|
406 | | -/// Create a value with native endian byte order. |
407 | | -impl TryFrom<ifd::Value> for ValueBuffer { |
408 | | - type Error = crate::TiffError; |
409 | | - |
410 | | - fn try_from(value: ifd::Value) -> Result<Self, Self::Error> { |
411 | | - Self::try_from(&value) |
412 | | - } |
413 | | -} |
414 | | - |
415 | | -/// Create a value with native endian byte order. |
416 | | -impl TryFrom<&'_ ifd::Value> for ValueBuffer { |
417 | | - type Error = crate::TiffError; |
418 | | - |
419 | | - fn try_from(value: &'_ ifd::Value) -> Result<Self, Self::Error> { |
420 | | - use std::borrow::Cow::{self, Borrowed, Owned}; |
421 | | - |
422 | | - fn bytes_of_value( |
423 | | - value: &'_ ifd::Value, |
424 | | - ) -> Result<(Cow<'_, [u8]>, Type), crate::TiffError> { |
425 | | - Ok(match value { |
426 | | - ifd::Value::List(values) => { |
427 | | - let mut type_ = None; |
428 | | - let mut bytes = vec![]; |
429 | | - |
430 | | - // This is treated separately, also we rely on the caller being faithful in their |
431 | | - // use of the ifd::Value enum. |
432 | | - for item in values { |
433 | | - let (value, newty) = bytes_of_value(item)?; |
434 | | - |
435 | | - if type_.map_or(false, |ty| ty != newty) { |
436 | | - return Err(crate::TiffError::UnsupportedError( |
437 | | - crate::error::TiffUnsupportedError::UnsupportedDataType, |
438 | | - )); |
439 | | - } |
440 | | - |
441 | | - type_ = Some(newty); |
442 | | - bytes.extend_from_slice(&value); |
443 | | - } |
444 | | - |
445 | | - let Some(ty) = type_ else { |
446 | | - return Err(crate::TiffError::UnsupportedError( |
447 | | - crate::error::TiffUnsupportedError::UnsupportedDataType, |
448 | | - )); |
449 | | - }; |
450 | | - |
451 | | - (Owned(bytes), ty) |
452 | | - } |
453 | | - ifd::Value::Byte(val) => (Borrowed(bytemuck::bytes_of(val)), Type::BYTE), |
454 | | - ifd::Value::Short(val) => (Borrowed(bytemuck::bytes_of(val)), Type::SHORT), |
455 | | - ifd::Value::SignedByte(val) => (Borrowed(bytemuck::bytes_of(val)), Type::SBYTE), |
456 | | - ifd::Value::SignedShort(val) => (Borrowed(bytemuck::bytes_of(val)), Type::SSHORT), |
457 | | - ifd::Value::Signed(val) => (Borrowed(bytemuck::bytes_of(val)), Type::SLONG), |
458 | | - ifd::Value::SignedBig(val) => (Borrowed(bytemuck::bytes_of(val)), Type::SLONG8), |
459 | | - ifd::Value::Unsigned(val) => (Borrowed(bytemuck::bytes_of(val)), Type::LONG), |
460 | | - ifd::Value::UnsignedBig(val) => (Borrowed(bytemuck::bytes_of(val)), Type::LONG8), |
461 | | - ifd::Value::Float(val) => (Borrowed(bytemuck::bytes_of(val)), Type::FLOAT), |
462 | | - ifd::Value::Double(val) => (Borrowed(bytemuck::bytes_of(val)), Type::DOUBLE), |
463 | | - ifd::Value::Rational(num, denom) => ( |
464 | | - Owned([bytemuck::bytes_of(num), bytemuck::bytes_of(denom)].concat()), |
465 | | - Type::RATIONAL, |
466 | | - ), |
467 | | - #[expect(deprecated)] |
468 | | - ifd::Value::RationalBig(..) | ifd::Value::SRationalBig(..) => { |
469 | | - return Err(crate::TiffError::UnsupportedError( |
470 | | - crate::error::TiffUnsupportedError::UnsupportedDataType, |
471 | | - )); |
472 | | - } |
473 | | - ifd::Value::SRational(num, denom) => ( |
474 | | - Owned([bytemuck::bytes_of(num), bytemuck::bytes_of(denom)].concat()), |
475 | | - Type::SRATIONAL, |
476 | | - ), |
477 | | - ifd::Value::Ascii(st) => (Borrowed(st.as_bytes()), Type::ASCII), |
478 | | - ifd::Value::Ifd(val) => (Borrowed(bytemuck::bytes_of(val)), Type::IFD), |
479 | | - ifd::Value::IfdBig(val) => (Borrowed(bytemuck::bytes_of(val)), Type::IFD8), |
480 | | - }) |
481 | | - } |
482 | | - |
483 | | - let byte_order = ByteOrder::native(); |
484 | | - |
485 | | - let (bytes, ty) = bytes_of_value(value)?; |
486 | | - let bytes = bytes.into_owned(); |
487 | | - |
488 | | - // Check the count can be represented. |
489 | | - if let ifd::Value::List(values) = value { |
490 | | - let count = |
491 | | - u64::try_from(values.len()).map_err(|_| crate::TiffError::LimitsExceeded)?; |
492 | | - let expected = ty.value_bytes(count)?; |
493 | | - |
494 | | - if bytes.len() != expected as usize { |
495 | | - return Err(crate::TiffError::UnsupportedError( |
496 | | - crate::TiffUnsupportedError::UnsupportedDataType, |
497 | | - )); |
498 | | - } |
499 | | - } else { |
500 | | - debug_assert_eq!(bytes.len(), ty.byte_len().into()); |
501 | | - } |
502 | | - |
503 | | - Ok(ValueBuffer { |
504 | | - bytes, |
505 | | - byte_order, |
506 | | - ty, |
507 | | - }) |
508 | | - } |
509 | | -} |
510 | | - |
511 | 406 | /// Byte order of the TIFF file. |
512 | 407 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
513 | 408 | pub enum ByteOrder { |
|
0 commit comments