SaveImage module (mojo2)

MikeHart

Administrator
CX Code Contributor
3rd Party Module Dev
3rd Party Target Dev
3rd Party Tool Dev
Joined
Jun 19, 2017
Messages
3,454
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:
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

  • SaveImage.zip
    535.1 KB · Views: 174

Paul59

Active member
CX Code Contributor
Joined
Dec 13, 2018
Messages
384
Thanks Mike! :D

I'll test it later/tomorrow on Linux.
 

Paul59

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

Cerberus:
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.
 

MikeHart

Administrator
CX Code Contributor
3rd Party Module Dev
3rd Party Target Dev
3rd Party Tool Dev
Joined
Jun 19, 2017
Messages
3,454
Thanks, I have an idea what I need to do to fix it. Will do ASAP.
 

MikeHart

Administrator
CX Code Contributor
3rd Party Module Dev
3rd Party Target Dev
3rd Party Tool Dev
Joined
Jun 19, 2017
Messages
3,454
Should be fixed on Linux now. Grab the new release!
 

Paul59

Active member
CX Code Contributor
Joined
Dec 13, 2018
Messages
384
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

Well-known member
CX Code Contributor
3rd Party Module Dev
Tutorial Author
3rd Party Tool Dev
Joined
Sep 9, 2017
Messages
451
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
Messages
384
...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!
 

Raph

New member
Joined
Aug 25, 2017
Messages
23
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?
 

MikeHart

Administrator
CX Code Contributor
3rd Party Module Dev
3rd Party Target Dev
3rd Party Tool Dev
Joined
Jun 19, 2017
Messages
3,454
Looks like RGBA
 
Top Bottom