i want to rendering image in godot each frame from unsigned char *pixels data

void render(int width, int height, const unsigned char *pixels)

	//	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA,
	//			GL_UNSIGNED_BYTE, pixels);

i know how to do this in imgui,though imgui.Image(textureid) function,but i dont know how to work with godot.i try to use set_pixel ,but dont work

	int size = sizeof(unsigned char);
	Ref<Image> image = new Image(width, height, false, Image::FORMAT_RGBA8);
	for (int x = 0; x < width; x++) {
		for (int y = 0; y < height; y++) {
			// call im->set_pixel to create the image
//			printf("%d\n", );
			image->set_pixel(x, y, Color(*(pixels + x * y * size*4)/255
										   , *(pixels + x * y * size*4+1)/255,
										   *(pixels + x * y * size*4+2)/255,
										  0.5));
		}
		//		image->create_from_data(width, height, false, Image::FORMAT_RGBA8, vec);
	}
  • Apparently Godot has no constructor for an image from raw data, which is a pity.

    One problem in the code is that the position calculation is off:

    unsigned char *pixels;
    int row_stride = 4 * sizeof(*pixels);
    int column_stride = 4 * width * sizeof(*pixels);
    // ... and then, assuming pixels has correctly been allocated:
    for( int y = 0; y < height; ++y ) {
        for( int x = 0; x < width; ++x ) {
            int position = y*column_stride + x*row_stride;
            // Do things with pixels[position], pixels[position+1], ...
        }
    }

    I also suggest to avoid implicit conversions (I didn't here), and to calculate the stride based on the actual size of an element, sizeof(*pixels).

    And I second @cybereality that this way of making an image every frame is terribly slow.

Apparently Godot has no constructor for an image from raw data, which is a pity.

One problem in the code is that the position calculation is off:

unsigned char *pixels;
int row_stride = 4 * sizeof(*pixels);
int column_stride = 4 * width * sizeof(*pixels);
// ... and then, assuming pixels has correctly been allocated:
for( int y = 0; y < height; ++y ) {
    for( int x = 0; x < width; ++x ) {
        int position = y*column_stride + x*row_stride;
        // Do things with pixels[position], pixels[position+1], ...
    }
}

I also suggest to avoid implicit conversions (I didn't here), and to calculate the stride based on the actual size of an element, sizeof(*pixels).

And I second @cybereality that this way of making an image every frame is terribly slow.

    But do test your code thoroughly ! Never trust a random guy on the internet ;-)