mosra / magnum

Lightweight and modular C++11 graphics middleware for games and data visualization
https://magnum.graphics/
Other
4.75k stars 439 forks source link

How to create image #581

Closed zaichenhu closed 2 years ago

zaichenhu commented 2 years ago

when I use Image2D to create a new image, the parameter always needs Array Char type, but how to use integer or float as an input, so what's the best practice to create an image with magnum?

Squareys commented 2 years ago

You can allocate the data and then use .pixels<T>() to type cast the char data to some pixel type, e.g.:

    constexpr Vector2i size{2, 2};
    Image2D image{{}, PixelFormat::RGBA32UI, size,
        Containers::Array<char>{NoInit, size.product()*sizeof(UnsignedInt)*4}};
    auto pixels = image.pixels<Vector4ui>();

    const int x = 0;
    const int y = 1:
    pixels[y][x] = Vector4ui{345, 0, 123, 678};

Note that to access a pixel, you pick the row first, then the column in the row, which is why x and y are in a different order than I used to expect.

For some pixel types, you might need to add row padding etc. I think there was a function to compute the required data size for an image dimension and storage parameters 🤔

zaichenhu commented 2 years ago

You can allocate the data and then use .pixels<T>() to type cast the char data to some pixel type, e.g.:

    constexpr Vector2i size{2, 2};
    Image2D image{{}, PixelFormat::RGBA32UI, size,
        Containers::Array<char>{NoInit, size.product()*sizeof(UnsignedInt)*4}};
    auto pixels = image.pixels<Vector4ui>();

    const int x = 0;
    const int y = 1:
    pixels[y][x] = Vector4ui{345, 0, 123, 678};

Note that to access a pixel, you pick the row first, then the column in the row, which is why x and y are in a different order than I used to expect.

For some pixel types, you might need to add row padding etc. I think there was a function to compute the required data size for an image dimension and storage parameters 🤔

Thank you very much!