Commit 2f59ae3d for libheif

commit 2f59ae3da925dbbddc59fe570d9ec0000d5ab064
Author: Dirk Farin <dirk.farin@gmail.com>
Date:   Thu Mar 12 16:58:37 2026 +0100

    unci: use 32bit component_indices internally

diff --git a/libheif/codecs/uncompressed/unc_boxes.cc b/libheif/codecs/uncompressed/unc_boxes.cc
index 2bd50224..075ff1a3 100644
--- a/libheif/codecs/uncompressed/unc_boxes.cc
+++ b/libheif/codecs/uncompressed/unc_boxes.cc
@@ -401,7 +401,10 @@ Error Box_uncC::write(StreamWriter& writer) const
         return {heif_error_Invalid_input, heif_suberror_Invalid_parameter_value, "component bit-depth out of range [1..256]"};
       }

-      writer.write16(component.component_index);
+      if (component.component_index > 0xFFFF) {
+        return {heif_error_Invalid_input, heif_suberror_Invalid_parameter_value, "component index must be 16 bit"};
+      }
+      writer.write16(static_cast<uint16_t>(component.component_index));
       writer.write8(uint8_t(component.component_bit_depth - 1));
       writer.write8(component.component_format);
       writer.write8(component.component_align_size);
diff --git a/libheif/codecs/uncompressed/unc_boxes.h b/libheif/codecs/uncompressed/unc_boxes.h
index 4ea310f2..1ce7ef48 100644
--- a/libheif/codecs/uncompressed/unc_boxes.h
+++ b/libheif/codecs/uncompressed/unc_boxes.h
@@ -109,7 +109,7 @@ public:

   struct Component
   {
-    uint16_t component_index;
+    uint32_t component_index;
     uint16_t component_bit_depth; // range [1..256]
     uint8_t component_format;
     uint8_t component_align_size;
diff --git a/libheif/codecs/uncompressed/unc_encoder_component_interleave.cc b/libheif/codecs/uncompressed/unc_encoder_component_interleave.cc
index d5e31cc2..cf9bf64c 100644
--- a/libheif/codecs/uncompressed/unc_encoder_component_interleave.cc
+++ b/libheif/codecs/uncompressed/unc_encoder_component_interleave.cc
@@ -72,9 +72,7 @@ unc_encoder_component_interleave::unc_encoder_component_interleave(const std::sh
     auto comp_format = to_unc_component_format(image->get_component_datatype(idx));
     bool aligned = (bpp % 8 == 0);

-    uint16_t cmpd_idx = image->get_component_cmpd_index();
-
-    m_components.push_back({cmpd_idx, ch, comp_type, comp_format, bpp, aligned});
+    m_components.push_back({idx, ch, comp_type, comp_format, bpp, aligned});
   }

   // Build cmpd/uncC boxes
diff --git a/libheif/codecs/uncompressed/unc_encoder_component_interleave.h b/libheif/codecs/uncompressed/unc_encoder_component_interleave.h
index 98ae382d..c5322292 100644
--- a/libheif/codecs/uncompressed/unc_encoder_component_interleave.h
+++ b/libheif/codecs/uncompressed/unc_encoder_component_interleave.h
@@ -40,7 +40,7 @@ public:
 private:
   struct channel_component
   {
-    uint16_t component_idx;
+    uint32_t component_idx;
     heif_channel channel;
     heif_uncompressed_component_type component_type;
     heif_uncompressed_component_format component_format;
diff --git a/libheif/pixelimage.cc b/libheif/pixelimage.cc
index fdecd125..3d0b8cb3 100644
--- a/libheif/pixelimage.cc
+++ b/libheif/pixelimage.cc
@@ -2227,7 +2227,7 @@ uint16_t HeifPixelImage::get_component_type(uint32_t component_idx) const
 }


-std::vector<uint16_t> HeifPixelImage::get_component_cmpd_indices_interleaved() const
+std::vector<uint32_t> HeifPixelImage::get_component_cmpd_indices_interleaved() const
 {
   const ImageComponent* comp = find_component_for_channel(heif_channel_interleaved);
   assert(comp);
@@ -2241,14 +2241,14 @@ Result<uint32_t> HeifPixelImage::add_component(uint32_t width, uint32_t height,
                                                const heif_security_limits* limits)
 {
   // Auto-generate component_index by appending to cmpd table
+  uint32_t component_index = static_cast<uint32_t>(m_cmpd_component_types.size());
   m_cmpd_component_types.push_back(component_type);
-  uint16_t component_index = static_cast<uint16_t>(m_cmpd_component_types.size() - 1);

   ImageComponent plane;
   plane.m_channel = map_uncompressed_component_to_channel(component_type);
   plane.m_component_index = std::vector{component_index};
   if (Error err = plane.alloc(width, height, datatype, bit_depth, 1, limits, m_memory_handle)) {
-    return err;
+    return {err};
   }

   m_planes.push_back(plane);
@@ -2270,7 +2270,7 @@ Result<uint32_t> HeifPixelImage::add_component_for_index(uint32_t component_inde

   ImageComponent plane;
   plane.m_channel = map_uncompressed_component_to_channel(component_type);
-  plane.m_component_index = std::vector{static_cast<uint16_t>(component_index)};
+  plane.m_component_index = std::vector{component_index};
   if (Error err = plane.alloc(width, height, datatype, bit_depth, 1, limits, m_memory_handle)) {
     return err;
   }
diff --git a/libheif/pixelimage.h b/libheif/pixelimage.h
index 002b7b31..6e14471e 100644
--- a/libheif/pixelimage.h
+++ b/libheif/pixelimage.h
@@ -443,9 +443,9 @@ public:

   std::vector<uint16_t> get_cmpd_component_types() const { return m_cmpd_component_types; }

-  std::vector<uint16_t> get_component_cmpd_indices_interleaved() const;
+  std::vector<uint32_t> get_component_cmpd_indices_interleaved() const;

-  uint16_t get_component_cmpd_index() const { assert(m_cmpd_component_types.size()==1); return m_cmpd_component_types[0]; }
+  uint32_t get_component_cmpd_index() const { assert(m_cmpd_component_types.size()==1); return m_cmpd_component_types[0]; }

   // Encoder path: auto-generates component_index by appending to cmpd table.
   Result<uint32_t> add_component(uint32_t width, uint32_t height,
@@ -556,7 +556,7 @@ private:

     // index into the cmpd component definition table
     // Interleaved channels will have a list of indices in the order R,G,B,A
-    std::vector<uint16_t> m_component_index;
+    std::vector<uint32_t> m_component_index;

     // limits=nullptr disables the limits
     Error alloc(uint32_t width, uint32_t height, heif_channel_datatype datatype, int bit_depth,