0 votes

I'm having some issues with Godot 4.0 Beta 6 and the load*from_buffer methods of the Image class.
When I call the method the Image object is de-referenced and the variable points to null. This may be a bug, or something I'm missing.

Here is a sample code that I'm using. The image file is within a zip file. But using a regular file produced the same result. (The PackedByteArray data retrieved from the zip file was verified!)

Also, the ZIPReader reads png and jpg, but not bmp files!

Thanks in advance!

var resourceFile = "res://ZIP/testImage2.zip"

var zipSource = ZIPReader.new()
var loaded = zipSource.open(resourceFile)
if loaded!=OK:
    return ERR_FILE_CANT_OPEN

var filesInZip = zipSource.get_files()

print(filesInZip)

var zippedGraphic : PackedByteArray

for file in filesInZip:
    zippedGraphic = []
    var imageExtensions = ["PNG", "BMP", "JPG", "JPEG", "TGA", "WEBP"]
    var imageExtension = file.to_upper().split(".")[-1]
    ### Only process zippedGraphic images
    if (("spriteSheets/" in file) and (file!="spriteSheets/") and
        imageExtension in imageExtensions):
        $Panel/filefound.text = file
        print("this is the file: "+file)

        zippedGraphic = zipSource.read_file(file)
        if (zippedGraphic==null) or (zippedGraphic.is_empty()):
            $Panel/result.text = "File Can't Read"
            break

        ### Store the uncompressed stream. This was used to validate the buffer data.
        if true:
            var imgFilename = "user://last-image_import.%s" % imageExtension.to_lower()
            var imgfile = FileAccess.open(imgFilename, FileAccess.WRITE)
            if !imgfile.is_open():
                $Panel/result.text = "File Can't Write to User"
            else:
                imgfile.store_buffer(zippedGraphic)
                imgfile = null
                print("Stored image to:" + imgFilename);        


        ### Image to be passed to Texture and Texture to be saved as Resource
        var loadedImage: Image = Image.new()
        var builtTexture: ImageTexture = ImageTexture.new()
        var img_error_code
        # Create the image according to extension
        match imageExtension:
            "PNG":
                img_error_code=loadedImage.load_png_from_buffer(zippedGraphic) # Set loadedImage to null!
            "BMP":
                img_error_code=loadedImage.load_bmp_from_buffer(zippedGraphic)
            "JPG":
                img_error_code=loadedImage.load_jpg_from_buffer(zippedGraphic)
            "JPEG":
                img_error_code=loadedImage.load_jpg_from_buffer(zippedGraphic)
            "TGA":
                img_error_code=loadedImage.load_tga_from_buffer(zippedGraphic)
            "WEBP":
                img_error_code=loadedImage.load_webp_from_buffer(zippedGraphic)

        if img_error_code!=OK:
            $Panel/result.text = "File Can't Read"
            break


        builtTexture.create_from_image(loadedImage)
Godot version 4.0Dev (Beta 6)
in Engine by (18 points)

Please log in or register to answer this question.

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 [email protected] with your username.