+1 vote

hi,
how to change a pixel of an image?
Image::putpixel, getpixel seems to be deprecated.
is there an alternative?
(c++)

(EDITED)

in Engine by (63 points)
edited by

I don't see a clear way of doing it in the current Image class (Godot 3.0), and it's certainly because it can have multiple formats (RGBA 8 bit, RF float, RGB 8 bit...).
So I would like to know what's the best way to set individual pixels on that too.

So no way to edit an image or texture anymore?
Because i used it to manipulate a texture at runtime,
needed this to change my splatmaps alphaTexture in my terrain.

EDIT: I will give it a try tomorrow, maybe i can hack something out of the old one :)
also get_pixel vanquished :(

In theory using GPU painting (basically drawing stuff on a render texture) instead of CPU painting could be a way to paint on the splatmap too, but I'm not sure how easy it is with the current Godot API.

1 Answer

+1 vote
Best answer

ok played around abit,
for all who need get_pixel again until (i hope) its implemented again.

workaround to get the pixels raw data (putpixel can be similar done):
i added a member to image-class (since getpixelb is private ) :

void Image::get_pixel_raw(int p_x, int p_y, uint8_t * p_dst, uint32_t &p_pixelsize)
{

    uint32_t pixel_size = get_format_pixel_size(format);
    if (p_pixelsize != pixel_size)
    {
        ERR_PRINT("Image.get_pixel_raw: pixelSize different");
        ERR_FAIL();
    }

    PoolVector<uint8_t>::Read r = data.read();

    _get_pixelb(p_x, p_y, pixel_size, r.ptr(), p_dst);

}

and use it like this:

uint32_t pixelSize = img.get_format_pixel_size(Image::FORMAT_RGBA8);

    // TODO: create my "GRAY16" format again, to be able to load geoData
    if (img.get_format() != Image::FORMAT_RGBA8)
    {
        ERR_PRINT("Only RGBA8 supported yet")
            ERR_FAIL();
    }

    int width = img.get_width();
    int height = img.get_height();

    union sPixel { struct sPixelRGBA { uint8_t r; uint8_t g; uint8_t b; uint8_t a; } values; uint8_t data[4]; };
    sPixel pixel;

    PoolVector<real_t>::Write w = heights.write();
    for (int y = 0; y < height; y++)
    {
        for (int x = 0; x < width; x++)
        {

            img.get_pixel_raw(x, y,pixel.data, pixelSize);
            w[getVectorIndex(x, y)] = pixel.values.r;
        }
    }

but be aware of: https://github.com/godotengine/godot/issues/8158

by (63 points)
selected by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.