SaveImage module (mojo2)


CX Code Contributor
3rd Party Module Dev
Tutorial Author
3rd Party Target Dev
3rd Party Tool Dev
Jun 19, 2017
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:

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"
    Case "jpg"              ' SaveJPG needs a quality value with a range of 1-100

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.


    535.1 KB · Views: 191
Thanks Mike! :D

I'll test it later/tomorrow on Linux.
Am I right in thinking saveImage.cxs and /native should be in /modules_ext? If so, I'm getting this error:

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);
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
Thanks, I have an idea what I need to do to fix it. Will do ASAP.
Should be fixed on Linux now. Grab the new release!
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:
SavePNG("test.png", imageCanvas)
rather than:
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
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.
...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!
I know this is set up for Mojo2, but I need to save from a different data structure altogether (doing procedural generation).

What pixel format is this expecting, if I call _savepng directly? BGRA?
Looks like RGBA
Top Bottom