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

Containers::Array<Color4ub> from ImageData2D #469

Open
hsdk123 opened this issue Aug 31, 2020 · 1 comment
Open

Containers::Array<Color4ub> from ImageData2D #469

hsdk123 opened this issue Aug 31, 2020 · 1 comment

Comments

@hsdk123
Copy link
Contributor

hsdk123 commented Aug 31, 2020

Looking for a Containers::Array from ImageData2D implementation as per gitter chat.

Use case: when ex. don't need to iterate over all pixels, but just need to fetch one or a certain portion continuously.

@mosra
Copy link
Owner

mosra commented Aug 31, 2020

For the record (also grabbed from Gitter), this is (an untested) snippet that should work for most common image formats:

#include <Corrade/Utility/Algorithms.h> // for copy()

Containers::Array<Color4ub> outputData{std::size_t(image.size().product()), 
    Containers::DirectInit, Color4ub{0, 0, 0, 255}}; // set alpha to 1 by default
Containers::StridedArrayView2D<Color4ub> output{outputData, 
    {std::size_t(image.size().y()), std::size_t(image->size().x())}};
if(image.format() == PixelFormat::RGBA8Unorm) {
    Utility::copy(image.pixels<Color4ub>(), output);
} else if(image.format() == PixelFormat::RGB8Unorm) {
    Utility::copy(image.pixels<Color3ub>(), 
                  Containers::arrayCast<Color3ub>(output)); // alpha stays at 255
} else if(image.format() == PixelFormat::R8Unorm) {
    // broadcasts the red channel to RGB
    Utility::copy(Containers::arrayCast<const UnsignedByte>(image.pixels()).broadcast<2>(3), 
                  Containers::arrayCast<3, UnsignedByte>(output)); // alpha stays at 255
} else Fatal{} << "Oh noes!";

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

No branches or pull requests

2 participants