Compiler warning in pixel.hpp
- Dominant language
- C++
- Stars
- 199
- Forks
- 171
- Avg merge
- 1d 14h
- Merged PRs (30d)
- 10
Description
### Actual behavior
I get the following compiler warnings from pixel.hpp when using some gil functions:
```
3> C:\Colony\VS2015\x64\boost_1_72_0\boost\gil\pixel.hpp(214,31): warning C4244: 'argument': conversion from 'const Channel' to 'BaseChannelValue', possible loss of data
3> with
3> [
3> Channel=int
3> ]
3> and
3> [
3> BaseChannelValue=float
3> ]
3> C:\Colony\VS2015\x64\boost_1_72_0\boost\gil\pixel.hpp(158): note: see reference to function template instantiation 'void boost::gil::pixel::assign(const Channel &,std::false_type)' being compiled
3> with
3> [
3> Pixel=int,
3> Channel=int
3> ]
3> C:\Colony\VS2015\x64\boost_1_72_0\boost\gil\pixel.hpp(158): note: see reference to function template instantiation 'void boost::gil::pixel::assign(const Channel &,std::false_type)' being compiled
3> with
3> [
3> Pixel=int,
3> Channel=int
3> ]
3> C:\Colony\VS2015\x64\boost_1_72_0\boost\gil\image_processing\numeric.hpp(68): note: see reference to function template instantiation 'boost::gil::pixel &boost::gil::pixel::operator =(const Pixel &)' being compiled
3> with
3> [
3> Pixel=int
3> ]
3> C:\Colony\VS2015\x64\boost_1_72_0\boost\gil\image_processing\numeric.hpp(68): note: see reference to function template instantiation 'boost::gil::pixel &boost::gil::pixel::operator =(const Pixel &)' being compiled
3> with
3> [
3> Pixel=int
3> ]
```
This is in the grayscale pixel assign function, where it's assigning the int channel value directly to the float grayscale value without an explicit cast. Since not every int has a representation as a float, this is a loss of data, hence the warning. I guess the fix would be to add an explicit `static_cast<>` in there like the following?
```c++
template
void assign(Channel const& channel, std::false_type)
{
check_gray();
gil::at_c<0>(*this) = static_cast( channel );
}
```
### Expected behavior
No warning
### C++ Minimal Working Example
```cpp
#include
#include
#include
#include
int main()
{
int width = 640;
int height = 480;
unsigned char* pImageData = new unsigned char[width*height*4];
boost::gil::rgba8c_view_t imageView = boost::gil::interleaved_view( width, height, ( const boost::gil::rgba8c_pixel_t* )pImageData, width * 4 );
try
{
boost::gil::write_view( "test.png", imageView, boost::gil::png_tag{});
}
catch ( ... )
{
return false;
}
}
```
### Environment
Compiled in Visual Studio 2019 using MSVC with language standard set to latest (C++20).
Contributor guide
Research direction
Start in boost/gil/pixel.hpp at the grayscale pixel assign function referenced around line 214, then inspect boost/gil/image_processing/numeric.hpp where the assignment is instantiated. Reproduce the warning with the provided C++ example under MSVC and verify that the warning is gone without changing the intended conversion behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- computer-vision
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 42/100