Hello.
I've been trying to manipulate image file in memblock, and then produce a new image file from said memblock.
But the image file produced can't be read, and had strange size (4.1mb) while the original file is only 53kb.
What I try to do:
- load image
- Create memblock from said image
- Copy each memblock bytes to an array of same size
- Copy bytes from the array to a NEW memblock
- CreateFileFromMemblock to produce a new jpeg image file
Now, I've also created a new image with CreateImageFromMemblock from the NEW memblock, and can texture a sprite with it and display it.
But I just seems to fail to create a new image file from the same NEW memblock.
The resulting file seems to be corrupted or something. It doesn't matter if I create the new image file using CreateImageFromMemblock or the 'traditional' way of openToWrite to a new file, the result is the same. I didn't even modify the memblock.
What did I do wrong?
Here's the example project. Thanks in advance.
+ Code Snippet// Project: TEST
// Created: 2022-09-20
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "TEST" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetVirtualResolution( 1024, 768 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
// Create memblock
inputImg = LoadImage("login.jpg")
rawMemblockID = CreateMemblockFromImage( inputImg )
//rawMemblockID = CreateMemblockFromFile("inputImg")
rawMemblockSize# = GetMemblockSize( rawMemblockID )
// Make array to store image data
GLOBAL imgArray AS INTEGER[]
FOR x = 0 TO (rawMemblockSize#-1)
imgArray.insert( GetMemblockByte(rawMemblockID,x) )
NEXT x
// Make new image memblock
newMemblockID = CreateMemblock( rawMemblockSize# )
FOR x = 0 TO (rawMemblockSize#-1)
SetMemblockByte( newMemblockID , x , imgArray[x] )
NEXT x
ImgID = CreateImageFromMemblock( newMemblockID )
//imgID = CreateImageFromMemblock( rawMemblockID )
spriteID = CreateSprite( imgID )
SetSpriteSize( spriteID , GetImageWidth( imgID )*0.5 , GetImageHeight( imgID )*0.5 )
//CreateFileFromMemblock( "raw:" + GetReadPath() + "savefile/" + "test.jpg" , newMemblockID )
//REMSTART
// Make new image file from array
fileID = OpenToWrite( "raw:" + GetReadPath() + "savefile/" + "test.jpg" )
FOR x = 0 TO (rawMemblockSize#-1)
WriteByte( fileID , imgArray[x] )
NEXt x
CloseFile( fileID )
//REMEND
do
Print( GetReadPath() )
Sync()
loop