Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unify channel index type in nth_channel_view and image_view::num_channels #373

Open
mloskot opened this issue Aug 8, 2019 · 6 comments · May be fixed by #659
Open

Unify channel index type in nth_channel_view and image_view::num_channels #373

mloskot opened this issue Aug 8, 2019 · 6 comments · May be fixed by #659
Labels
cat/annoyance Not a bug, not a feature, but something that should be improved core boost/gil

Comments

@mloskot
Copy link
Member

mloskot commented Aug 8, 2019

Actual behavior

  • image_view::num_channels() returns size_t
  • nth_channel_view(View, int n) expects int for index

Apart from signed and unsigned mix-up causing the annoying compilation warnings,
if no rationale of the mix is provided, it's a sloppy design of the interface.

Expected behavior

Use of common type for indexing of image_view channels.

C++ Minimal Working Example

#include <boost/gil.hpp>
namespace gil = boost::gil;
int main
{
    gil::rgb8_image_t img;
    auto v = gil::view(img);
    for (std::size_t i = 0; i < v.num_channels(); i++)
        auto _ = nth_channel_view(v, i);
}
@mloskot mloskot added cat/annoyance Not a bug, not a feature, but something that should be improved core boost/gil labels Aug 8, 2019
striezel added a commit to striezel-stash/gil that referenced this issue May 4, 2022
This unifies the channel index type in nth_channel_view and
image_view::num_channels - the latter one already uses std::size_t.

Fixes boostorg#373.
@striezel striezel linked a pull request May 4, 2022 that will close this issue
2 tasks
@sdebionne
Copy link
Contributor

A rationale could be that int is used almost everywhere to identify the channel. I think it helps writing compile-time recursion for per-channel operation but if it' not the case we might want to extend "unification" to color base.

@mloskot
Copy link
Member Author

mloskot commented May 4, 2022

It has been a bit of a mess.
If we think of the image in terms of 3D: X, Y and Z, then currently we define X and Y as ptrdiff_t and the X as int or size_t.

I'm recalling #305 where some of us, @stefanseefeld, were preferring size_t to express the image-in-memory dimensions. I've been cultivating a more conservative approach to stick to ptrdiff_t as it is what the original authors of GIL (folks at Adobe) decided to use. Looks like it may be a good to review the current practice and decide on the ptrdiff_t vs size_t once for all :)

My suggestion is to

  • use ptrdiff_t for sizes of abstract, virtual and physical sizes of image/band dimensions
  • use size_t for sizes in memory
  • if image dimension is used as size in memory, then static_cast<size_t> it, and vice versa.

Please, have your say @sdebionne @striezel @stefanseefeld @lpranam @chhenning @simmplecoder ...

@striezel
Copy link
Contributor

striezel commented May 5, 2022

A rationale could be that int is used almost everywhere to identify the channel.

Everywhere - except where it is not used. While nth_channel_view still uses int (unless #659 is approved, which would change that to std::size_t), many places that are using nth_channel_view are using std::size_t as channel index already. For example:

Seems to me like most of the image processing stuff agreed that channels should be a std::size_t.

It has been a bit of a mess.

Yeah, seems like it, but we can fix that - one PR at a time.

I'm recalling #305 where some of us, @stefanseefeld, were preferring size_t to express the image-in-memory dimensions. I've been cultivating a more conservative approach to stick to ptrdiff_t as it is what the original authors of GIL (folks at Adobe) decided to use. Looks like it may be a good to review the current practice and decide on the ptrdiff_t vs size_t once for all :)

Basically I am with Stefan Seefeld on this one. std::ptrdiff_t allows for negative values, but there is no negative channel index. That's why I think std::size_t (or any unsigned type, for that matter) is the better choice here, because it conveys the notion that channel indices start at zero and cannot be negative. As a side effect, any compiler warning about signed / unsigned mixup with regards to channels would then be an indication for potential trouble. After all, passing a possibly negative int or std::ptrdiff_t as channel index is most likely not what people want to do anyway.

@sdebionne
Copy link
Contributor

Everywhere - except where it is not used.

Sorry, I meant "everywhere" in core. Image processing is a recent addition to the library (that I dont know much about TBH).

I think there is a consensus that unsigned integers are error prone and that using unsigned type for size_t was a mistake.

OpenMP does not allow a for loop index to be unsigned (until recent iteration of the standard).

@striezel
Copy link
Contributor

striezel commented May 6, 2022

I was not really expecting that people do arithmetic operations on the number of channels, but if that is a concern, then some signed type may indeed be better. In that case it should probably be ptrdiff_t instead, as was suggested earlier (#373 (comment)). I just do not fancy the name ptrdiff_t for a type that is used to represent the number of channels. The name ptrdiff_t may invoke the idea that this is dealing with pointers, which it is not. So maybe there should be a name like boost::gil::channel_index_t or boost::gil::index_t that then is aliased to ptrdiff_t.

@mloskot
Copy link
Member Author

mloskot commented May 17, 2022

@striezel

maybe there should be a name like boost::gil::channel_index_t or boost::gil::index_t that then is aliased to ptrdiff_t

I don't like the ptrdiff_t name particularly.
I don't say the name is clear across all contexts in GIL.
I don't feel comfortable with s/ptrdiff_t/size_t/g across GIL as such seemingly trivial change may turn not as benign as it seems.
I assume Lubomir Bourdev and Hailin Jin had reasons to choose ptrdiff_t which, unfortunately, remain undocumented.

I do like the idea of readable and self-descriptive names, so I like the idea of aliasing ptrdiff_t as boost::gil::index_t and use it in all indexing situations in GIL (channel, pixel, x, y, etc.), and fix any existing mismatch to match index_t.
Worth it to you too @sdebionne ?

striezel added a commit to striezel-stash/gil that referenced this issue May 31, 2022
This unifies the channel index type in nth_channel_view and
image_view::num_channels - the latter one already uses std::size_t.

Fixes boostorg#373.
striezel added a commit to striezel-stash/gil that referenced this issue Jun 28, 2022
This unifies the channel index type in nth_channel_view and
image_view::num_channels - the latter one already uses std::size_t.

Fixes boostorg#373.
striezel added a commit to striezel-stash/gil that referenced this issue Jun 28, 2022
This unifies the channel index type in nth_channel_view and
image_view::num_channels - the latter one already uses std::size_t.

Fixes boostorg#373.
striezel added a commit to striezel-stash/gil that referenced this issue Sep 1, 2022
This unifies the channel index type in nth_channel_view and
image_view::num_channels - the latter one already uses std::size_t.

Fixes boostorg#373.
striezel added a commit to striezel-stash/gil that referenced this issue May 5, 2024
This unifies the channel index type in nth_channel_view and
image_view::num_channels - the latter one already uses std::size_t.

Fixes boostorg#373.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cat/annoyance Not a bug, not a feature, but something that should be improved core boost/gil
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants