Skip to content

automatic array casting in C++ does not work

I try to read .png images in uint8_t, regardless of their actual internal representation, i.e., 8 or 16 bit, in C++. In the current way, when I try to read a uint16 .png image using bob::io::image::read_color_image I get:

cannot efficiently retrieve blitz::Array<uint8,3> from buffer of type 'dtype: uint16 (2); shape: [3,606,544]; size: 1977984 bytes'

After reading code a little bit, I found that in https://gitlab.idiap.ch/bob/bob.io.image/blob/master/bob/io/image/include/bob.io.image/png.h#L99 I could simply replace:

png.read<T,N>(0);

with:

png.cast<T,N>(0);

Theoretically, this should read the data in any format by resetting the image buffer to the type inside the image (uint16): https://gitlab.idiap.ch/bob/bob.io.image/blob/master/bob/io/image/cpp/png.cpp#L499 read the data, and then cast it back to the desired type: https://gitlab.idiap.ch/bob/bob.io.base/blob/master/bob/io/base/include/bob.io.base/blitz_array.h#L246

Unfortunately, this doesn't compile, it gives me the error:

bob/io/base/include/bob.io.base/blitz_array.h:247:49: error: no matching function for call to ‘cast(const bob::io::base::array::blitz_array&)’

Again, reading more code, I figured out that this function doesn't exists in that namespace, but that:

return bob::core::array::cast<T,N>(*this);

in https://gitlab.idiap.ch/bob/bob.io.base/blob/master/bob/io/base/include/bob.io.base/blitz_array.h#L247 should rather be:

return bob::io::base::array::cast<T,N>(*this);

Now, this doesn't compile either, it gives me the error:

bob/core/include/bob.core/cast.h:23:30: error: invalid static_cast from type ‘const std::complex<float>’ to type ‘unsigned char’

(https://gitlab.idiap.ch/bob/bob.core/blob/master/bob/core/include/bob.core/cast.h#L23) which, in the first place looks weired, as I don't deal with complex numbers at all. However, when reading the backtrace, the code here: https://gitlab.idiap.ch/bob/bob.io.base/blob/master/bob/io/base/include/bob.io.base/array_utils.h#L117 tries to cast anything into anything, which in my case is complex<float> into uint8_t, which raises the compiler error from above.

So, my question is: was this array casting being tested in any way, and has it been working before? Has anyone ever tried to use the bob::io::base::array::cast function?