Skip to content

Commit 2cdaedc

Browse files
authored
fix(heif): Fix incorrect tracking of current subimage (#5166)
Oh dear, somehow our heif reader never correctly overloaded the current_subimage method, instead inheriting the one from the base class that always returns 0. That's very bad for a file type that supports multiple subimages. Along the way, simplified how we deal with the fact that the "primary" subimage doesn't need to be first in the file, but we always want to present it as if it were first. There was a point where I thought that was related to the bug so I was rewriting it; it turned out to not be the problem, but I'd still like to keep the cleanup I had done. Signed-off-by: Larry Gritz <lg@larrygritz.com>
1 parent 409621b commit 2cdaedc

1 file changed

Lines changed: 10 additions & 7 deletions

File tree

src/heif.imageio/heifinput.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ class HeifInput final : public ImageInput {
7979
bool open(const std::string& name, ImageSpec& newspec,
8080
const ImageSpec& config) override;
8181
bool close() override;
82+
int current_subimage(void) const override { return m_subimage; }
8283
bool seek_subimage(int subimage, int miplevel) override;
8384
bool read_native_scanline(int subimage, int miplevel, int y, int z,
8485
void* data) override;
@@ -188,16 +189,18 @@ HeifInput::open(const std::string& name, ImageSpec& newspec,
188189
try {
189190
m_ctx->read_from_reader(*m_reader);
190191

191-
m_item_ids = m_ctx->get_list_of_top_level_image_IDs();
192+
// Get the item IDs for each subimage, and force the "primary" one to
193+
// have index 0.
192194
m_primary_id = m_ctx->get_primary_image_ID();
193-
for (size_t i = 0; i < m_item_ids.size(); ++i)
194-
if (m_item_ids[i] == m_primary_id) {
195-
m_item_ids.erase(m_item_ids.begin() + i);
196-
break;
197-
}
195+
m_item_ids.resize(0);
196+
m_item_ids.push_back(m_primary_id);
197+
for (auto id : m_ctx->get_list_of_top_level_image_IDs()) {
198+
if (id != m_primary_id)
199+
m_item_ids.push_back(id);
200+
}
198201
// std::cout << " primary id: " << m_primary_id << "\n";
199202
// std::cout << " item ids: " << Strutil::join(m_item_ids, ", ") << "\n";
200-
m_num_subimages = 1 + int(m_item_ids.size());
203+
m_num_subimages = int(m_item_ids.size());
201204

202205
} catch (const heif::Error& err) {
203206
std::string e = err.get_message();

0 commit comments

Comments
 (0)