Skip to content

Compiler warning in pixel.hpp #688

Description

@jsenn

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<boost::gil::float32_t,boost::gil::gray_layout_t>::assign<Pixel>(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<boost::gil::float32_t,boost::gil::gray_layout_t>::assign<Pixel>(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::float32_t,boost::gil::gray_layout_t> &boost::gil::pixel<boost::gil::float32_t,boost::gil::gray_layout_t>::operator =<int>(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::float32_t,boost::gil::gray_layout_t> &boost::gil::pixel<boost::gil::float32_t,boost::gil::gray_layout_t>::operator =<int>(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?

    template <typename Channel>
    void assign(Channel const& channel, std::false_type)
    {
        check_gray();
        gil::at_c<0>(*this) = static_cast<channel_t::base_channel_t>( channel );
    }

Expected behavior

No warning

C++ Minimal Working Example

#include <stdexcept>
#include <boost/gil.hpp>
#include <boost/gil/io/write_view.hpp>
#include <boost/gil/extension/io/png.hpp>

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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions