Repository: image-rs/image-tiff
Pull request #208 (2023-07-23 10:45:42)

add CMYKA
As described in #207, this PR adds the CMYKA8 color type.


Patch:
diff --git a/README.md b/README.md
index 2ab4d86..c116ac8 100644
--- a/README.md
+++ b/README.md
@@ -24,6 +24,7 @@ This table lists photometric interpretations and sample formats which are suppor
 | `RGBPalette`                |                                         |                           |
 | `Mask`                      |                                         |                           |
 | `CMYK`                      | CMYK(8\|16\|32\|64)                     | CMYK(32\|64)              |
+| `CMYKA`                     | CMYKA(8)                                |                           |
 | `YCbCr`                     |                                         |                           |
 | `CIELab`                    |                                         |                           |
 
diff --git a/src/decoder/image.rs b/src/decoder/image.rs
index f1c5294..ddfd624 100644
--- a/src/decoder/image.rs
+++ b/src/decoder/image.rs
@@ -330,6 +330,7 @@ impl Image {
             },
             PhotometricInterpretation::CMYK => match self.samples {
                 4 => Ok(ColorType::CMYK(self.bits_per_sample)),
+                5 => Ok(ColorType::CMYKA(self.bits_per_sample)),
                 _ => Err(TiffError::UnsupportedError(
                     TiffUnsupportedError::InterpretationWithBits(
                         self.photometric_interpretation,
@@ -567,6 +568,7 @@ impl Image {
             ColorType::RGB(n)
             | ColorType::RGBA(n)
             | ColorType::CMYK(n)
+            | ColorType::CMYKA(n)
             | ColorType::YCbCr(n)
             | ColorType::Gray(n)
             | ColorType::Multiband {
diff --git a/src/encoder/colortype.rs b/src/encoder/colortype.rs
index 462cf62..7bb45d0 100644
--- a/src/encoder/colortype.rs
+++ b/src/encoder/colortype.rs
@@ -341,3 +341,13 @@ impl ColorType for YCbCr8 {
 
     integer_horizontal_predict!();
 }
+
+pub struct CMYKA8;
+impl ColorType for CMYKA8 {
+    type Inner = u8;
+    const TIFF_VALUE: PhotometricInterpretation = PhotometricInterpretation::CMYK;
+    const BITS_PER_SAMPLE: &'static [u16] = &[8, 8, 8, 8, 8];
+    const SAMPLE_FORMAT: &'static [SampleFormat] = &[SampleFormat::Uint; 5];
+
+    integer_horizontal_predict!();
+}
diff --git a/src/lib.rs b/src/lib.rs
index fb48a84..d199461 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -16,6 +16,7 @@ pub use self::error::{TiffError, TiffFormatError, TiffResult, TiffUnsupportedErr
 
 /// An enumeration over supported color types and their bit depths
 #[derive(Copy, PartialEq, Eq, Debug, Clone, Hash)]
+#[non_exhaustive]
 pub enum ColorType {
     /// Pixel is grayscale
     Gray(u8),
@@ -35,6 +36,9 @@ pub enum ColorType {
     /// Pixel is CMYK
     CMYK(u8),
 
+    /// Pixel is CMYK with an alpha channel
+    CMYKA(u8),
+
     /// Pixel is YCbCr
     YCbCr(u8),
 
@@ -50,6 +54,7 @@ impl ColorType {
             | ColorType::GrayA(b)
             | ColorType::RGBA(b)
             | ColorType::CMYK(b)
+            | ColorType::CMYKA(b)
             | ColorType::YCbCr(b)
             | ColorType::Multiband { bit_depth: b, .. } => b,
         }


Test patch:
diff --git a/tests/encode_images.rs b/tests/encode_images.rs
index a5b4091..2f17d7d 100644
--- a/tests/encode_images.rs
+++ b/tests/encode_images.rs
@@ -298,6 +298,11 @@ fn test_cmyk_u8_roundtrip() {
     test_u8_roundtrip::<colortype::CMYK8>("cmyk-3c-8b.tiff", ColorType::CMYK(8));
 }
 
+#[test]
+fn test_cmyka_u8_roundtrip() {
+    test_u8_roundtrip::<colortype::CMYKA8>("cmyk-4c-8b.tiff", ColorType::CMYKA(8));
+}
+
 #[test]
 fn test_gray_u16_roundtrip() {
     test_u16_roundtrip::<colortype::Gray16>("minisblack-1c-16b.tiff", ColorType::Gray(16));