Godot: How to draw a rawarray Image

Created on 12 Mar 2016  路  12Comments  路  Source: godotengine/godot

Right now I have to save my rawarray(image) first but I want to show it without saving to the drive.

var image_as_raw_array = []#is filled with an png image from www
var path = "res://cache.png"

if(f.open( path, File.WRITE )==0):
    f.store_buffer( image_as_raw_array )
    f.close()

    var img = ImageTexture.new()
    img.load( path )

    preview_sprite.set_texture( img )

Is there a way without "img.load( path )" something like img.load( rawarray )?

http://www.godotengine.org/qa/744/how-to-draw-a-rawarray-image

discussion feature proposal core

Most helpful comment

The idea of creating an Image directly from a RawArray looks very useful. At least I can see it working for my case, where I get images from a database. Currently I need to save it to a file locally and load it again as an Image.

All 12 comments

For storing a Image in a file you can use File::store_var and File::get_var.

But if you want create a Image from RawArray now is not pratical. But there's this Image constructor:

Image(int p_width, int p_height, int p_mipmaps, Format p_format, const DVector<uint8_t>& p_data);

I can bind this constructor GDScript, but i will need increase the maximun number of arguments a constructor can have, it's easy and will not afect anything.

Is it possible to read the width and height from the rawarray?
It works with "img.load( path )" so why not with img.load( format, rawarray )

In libgdx I used the Pixmap class to generate and manipulate Image/texture in game. Very fast and very cool to create procedurally generated content.

With new Texture(pixmap) you got the texture to draw.

Perhaps we could have something similarly in godot.

@AlexHolly, oh i'm sorry i misunderstood the question, you can transform transform a RawArray in a Image using bytes2var.

@puppetmaster- Image and ImageTexture. Exposing the additional constructor, maybe, will expand it's possibilities.

I think bytes2var only works if the bytes where created by godot. My rawarray is a .png image from www
I try anyway.

UPDATE:
preview_sprite.set_texture( bytes2var(file_as_raw_array) )

ERROR: decode_variant: Condition ' type>=Variant::VARIANT_MAX ' is true. returned: ERR_INVALID_DATA
At: core/io/marshalls.cpp:47.
ERROR: call: Not enough bytes for decoding..
At: modules/gdscript/gd_functions.cpp:683.

I think this feature is missing...

bytes2var and var2bytes would (de)serialize a godot Texture, not a .png file :smile:

The idea of creating an Image directly from a RawArray looks very useful. At least I can see it working for my case, where I get images from a database. Currently I need to save it to a file locally and load it again as an Image.

Note that this works just fine for C++ (as evidenced by assetlib, even though AL would cache them).

If I could open this up again.
I was able to expose the constructor to gdscript by by editing the variant_call class
I added the ("data", Variant::RAW_ARRAY):
_VariantCall::add_constructor(_VariantCall::Image_init1,Variant::IMAGE,"width",Variant::INT,"height",Variant::INT,"mipmaps",Variant::BOOL,"format",Variant::INT,"data",Variant::RAW_ARRAY);
and adding *p_args[4]:
r_ret=Image(*p_args[0],*p_args[1],*p_args[2],Image::Format(p_args[3]->operator int()),*p_args[4]);

and adding then the argument:
const String& p_name5="", const Variant::Type p_type5=Variant::NIL)
to the add_constructor function.
So now gdscript allows me to use my RawArray from udp to create the Image.

However it errors because the data is coming in as compressed jpg. I cannot find a way to decompress to rgb.

Looking to utilize this as well.

Still not feasible under 3.0.6 ?

@ArthurAttout There is Image::load_{jpg,png}_from_buffer.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zatherz picture zatherz  路  121Comments

RyanBram picture RyanBram  路  101Comments

akien-mga picture akien-mga  路  366Comments

ghost picture ghost  路  161Comments

Angluca picture Angluca  路  100Comments