SaveImage module (mojo2)

MikeHart

Administrator
Joined
Jun 19, 2017
Location
Germany
This little module will let you save the content of a canvas to either a PNG, JPG or BMP file.
A little test programm is basically self explaining. About its usage. The relevant part is this:

Cerberus X:
Import saveImage

'...

Local file := RequestFile("Save file....", "Image Files:png,jpg,bmp;All Files:*", True)
Local ext:=filepath.ExtractExt(file).ToLower()
Select ext
    Case "png"
        SavePNG(file, canvas)
    Case "bmp"
        SaveBMP(file,canvas)
    Case "jpg"              ' SaveJPG needs a quality value with a range of 1-100
        SaveJPG(file,canvas,50)
End
Just copy the content into your mojo2 powered game.

If you find any issues, please let me know in this thread.

Disclaimer: Tested only on Windows.
 

Attachments

Paul59

Active Member
CX Code Contributor
Joined
Dec 13, 2018
Location
UK
Thanks Mike! :D

I'll test it later/tomorrow on Linux.
 

Paul59

Active Member
CX Code Contributor
Joined
Dec 13, 2018
Location
UK
Am I right in thinking saveImage.cxs and /native should be in /modules_ext? If so, I'm getting this error:

Cerberus X:
g++ -O0 -Wno-int-to-pointer-cast -Wno-free-nonheap-object -Wno-unused-result -I../glfw3/include -I../openal/include -I../stb -I../glfw3/deps -I../glfw3/deps/vulkan -D_GLFW_HAS_GLXGETPROCADDRESS -pthread  -c -o build/Debug/main.o ../main.cpp
../main.cpp: In function ‘void _savejpg(String, int, int, BBDataBuffer*, int)’:
../main.cpp:5312:5: error: ‘stbi_write_jpg’ was not declared in this scope
     stbi_write_jpg(C_STR(filename), w, h, 4, &colorBuffer[0],q);
     ^~~~~~~~~~~~~~
../main.cpp:5312:5: note: suggested alternative: ‘stbi_write_png’
     stbi_write_jpg(C_STR(filename), w, h, 4, &colorBuffer[0],q);
     ^~~~~~~~~~~~~~
     stbi_write_png
Makefile:62: recipe for target 'build/Debug/main.o' failed
TRANS FAILED: Error executing 'make CCOPTS="  -O0" LDOPTS=" " LIBOPTS=" -lopenal" OUT="Debug/CerberusGame"', return code=512
make: *** [build/Debug/main.o] Error 1
Done.
 

Paul59

Active Member
CX Code Contributor
Joined
Dec 13, 2018
Location
UK
Turns out my "later" was several days later but happy to report is works perfectly - at least PNG does, I haven't tried other formats yet.

One thing I can't figure out though is what if the path to the save file is not explicitly defined, eg if code is:
Code:
SavePNG("test.png", imageCanvas)
rather than:
Code:
SavePNG("/home/paul/test.png", imageCanvas)
Where does the image get saved (if at all) in the first case? I can't seem to find it LOL!

Can I suggest renaming module to saveimage.cxs so the import is lower case, the same as all other default modules? :D
 

Rich

Active Member
3rd Party Module Dev
3rd Party Tool Dev
Joined
Sep 9, 2017
ive not played with this module yet, Im still on the temporary savepng that @MikeHart gave me in the early days of FontDu
In that version, if there is no path, then the image is saved in the root of the compiled app, where the exe is.
 

Paul59

Active Member
CX Code Contributor
Joined
Dec 13, 2018
Location
UK
...if there is no path, then the image is saved in the root of the compiled app, where the exe is.
Yup, seems to be working now, maybe did something silly earlier - not an unusual occurrence!
 
Top Bottom