Skip to content

Fix nchannels fallback for wrapped images (e.g., Adjoint / PermutedDimsArray) - #194

Merged
tlnagy merged 2 commits into
tlnagy:masterfrom
marcosdanieldasilva:dev
Mar 5, 2026
Merged

Fix nchannels fallback for wrapped images (e.g., Adjoint / PermutedDimsArray)#194
tlnagy merged 2 commits into
tlnagy:masterfrom
marcosdanieldasilva:dev

Conversation

@marcosdanieldasilva

Copy link
Copy Markdown
Contributor

Description

This PR fixes an issue where calling nchannels() on an image that has been wrapped (such as with adjoint (') or PermutedDimsArray) incorrectly returns the total length of the array (the number of pixels) instead of the actual number of image channels.

The Bug

Currently, if a user loads a multi-channel image and applies an operation that changes the array type (like img'), nchannels falls back to the generic method:
nchannels(x) = _length(x)
Because the wrapped image is evaluated as a generic AbstractArray, _length(x) calculates the total number of elements in the matrix (e.g., rows * cols), leading to wildly incorrect results like returning 4186 for a 2-channel 91x46 image.

The Fix

By adding a dispatch for AbstractArray{<:ColorOrTuple}, we ensure that any wrapped array containing colorants or tuples correctly delegates the nchannels call to its eltype, preserving the expected behavior:

nchannels(img::AbstractArray{<:ColorOrTuple}) = nchannels(eltype(img))

Reproducible Example & Tests

This PR ensures all tests pass, even when the arrays are wrapped with adjoint:

using TiffImages
using Test
using Downloads

_wrap(name) = "https://github.com/tlnagy/exampletiffs/blob/master/$name?raw=true"
get_example(x) = Downloads.download(_wrap(x))

original = TiffImages.load(get_example("shapes_uncompressed.tif"))
hyper = TiffImages.load(get_example("shapes_hyper.tif"))

# Standard tests (Already passed)
@test TiffImages.nchannels(original) == 3
@test TiffImages.nchannels(hyper) == 7

# Wrapped tests (Previously failed, now pass)
@test TiffImages.nchannels(original') == 3
@test TiffImages.nchannels(hyper') == 7

@codecov-commenter

codecov-commenter commented Mar 5, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.48%. Comparing base (5a1ab53) to head (d0a782c).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #194      +/-   ##
==========================================
+ Coverage   93.37%   93.48%   +0.10%     
==========================================
  Files          15       15              
  Lines        1192     1181      -11     
==========================================
- Hits         1113     1104       -9     
+ Misses         79       77       -2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@tlnagy

tlnagy commented Mar 5, 2026

Copy link
Copy Markdown
Owner

I think this is reasonable, thanks for the PR! The failures on the nightlies seem to be related to #193.

@tlnagy
tlnagy merged commit a7074e4 into tlnagy:master Mar 5, 2026
9 of 14 checks passed
@marcosdanieldasilva
marcosdanieldasilva deleted the dev branch March 5, 2026 10:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants