SaveJPG

Discussions related to graphics (2D and 3D), animation and games programming
TonyTooth
Posts: 7
Joined: Mon 07 Aug 2023, 18:37

SaveJPG

Post by TonyTooth »

There is a "SaveJPG" or "IMG_SaveJPG" function in the SDL_image library which I want to be able to use, but it appears either it's not available in BBC BASIC or, more likely, I can't fathom the correct syntax.

I've tried all sorts of variations of sys "SDL_SaveBMP", jj%%, fileGS$ to r% but all I get is "No such system call".

Any suggestions anyone?
Hated Moron

Re: SaveJPG

Post by Hated Moron »

TonyTooth wrote: Thu 21 Dec 2023, 20:44 There is a "SaveJPG" or "IMG_SaveJPG" function in the SDL_image library
BBCSDL isn't linked with SDL_image (which is a heavyweight library with several dependencies, that are difficult to satisfy across multiple platforms), instead it's linked with SDL_stbimage (which is a lightweight header-only library).

You could perhaps save the image as a BMP and use an external conversion tool (maybe an online tool) to convert it to JPG. A BMP has the additional advantage that it's lossless.

If you do particularly want to use SDL_image, you should (in principle) be able to load the relevant shared library at run time (.dll in Windows, .dylib in MacOS, .so on most other platforms) if you can find a pre-compiled copy.
TonyTooth
Posts: 7
Joined: Mon 07 Aug 2023, 18:37

Re: SaveJPG

Post by TonyTooth »

Ah OK - I guess then there's no "save image" function in SDL_stbimage? It's easy of course to save an image as a BMP. I suppose I could "bite the bullet" and write my own BMP to JPG converter in ASM (well - I've done it before, including JPG back to BMP, but unfortunately in my own idiosyncratic format with non-standard minimal header to explore image processing in the frequency domain. It worked, but mainly just degraded the image rather than did anything useful, so I abandoned it.)

There appears to be a SaveBMP function in the standard SDL library, but I failed at guessing the syntax if it's available in BBC BASIC.
Hated Moron

Re: SaveJPG

Post by Hated Moron »

TonyTooth wrote: Fri 22 Dec 2023, 05:27 I suppose I could "bite the bullet" and write my own BMP to JPG converter in ASM.
I don't think that helps. As soon as you incorporate assembler code it makes your program platform-specific (unless you are planning to write four versions - for x86-32, x86-64, ARM32 and ARM64 - and that doesn't help in iOS or the in-browser edition) and if it's platform-specific you might as well use the conversion routine provided by the OS.

For example if you don't care that your program is Windows-specific it's a lot easier to adapt the Saving a JPG Image routine documented at the Wiki than to write your own! The recent discussions here about porting COMLIB from BB4W to BBCSDL should tell you all you need to know to do that.

Alternatively use the SDL_image library as I explained; you would need to find a pre-compiled library for the platform(s) you want the program to run on. For example on Windows you would need the 32-bit version of SDL2_image.dll which is available for download from here (this is the official site).
There appears to be a SaveBMP function in the standard SDL library, but I failed at guessing the syntax if it's available in BBC BASIC.
I think SDL_SaveBMP() is a macro, not a function, which will be why you can't find it. Use SDL_SaveBMP_RW() which is definitely available in BBC BASIC (or you could use *GSAVE of course, if the image you want to save is on the screen).

In your position I'd probably try using the SDL2_image library, which is after all what you originally asked about.
TonyTooth
Posts: 7
Joined: Mon 07 Aug 2023, 18:37

Re: SaveJPG

Post by TonyTooth »

I did find and install an SDL_image dll, and I've got as far as getting the IMG_SaveJPG to save a blank JPG to the right place with the right filename. However, no matter what I try, I can't get IMG_Init to successfully load a JPG codec, despite installing several of them. Ready to forget about it now.
Hated Moron

Re: SaveJPG

Post by Hated Moron »

TonyTooth wrote: Fri 22 Dec 2023, 18:55 However, no matter what I try, I can't get IMG_Init to successfully load a JPG codec, despite installing several of them.
You didn't list any code (it always helps enormously to post a Minimal Reproducible Example which illustrates the issue) so I am assuming you have been trying code similar to this:

Code: Select all

      SYS "SDL_LoadObject", @dir$ + "SDL2_image.dll" TO dll%%
      IF dll%% = 0 PRINT "Could not load object SDL2_image.dll" : STOP
      SYS "SDL_LoadFunction", dll%%, "IMG_Init" TO IMG_Init%%
      IF IMG_Init%% = 0 PRINT "Could not load function IMG_Init" : STOP

      IMG_INIT_JPG = 1
      SYS IMG_Init%%, IMG_INIT_JPG TO res%
      PRINT res%
Here that code prints '1' which means the JPG codec has been successfully initialised (I didn't load any codecs explicitly, I assume it is using something already installed in Windows 11). So I wonder what the difference is between my system and yours to explain why it is failing for you.

Of course if you got that far but it's a subsequent step that is failing, we are no further forward. In that case please post your code.
Ready to forget about it now.
So you don't, in fact, want any help? Or is that just an indication of frustration?
TonyTooth
Posts: 7
Joined: Mon 07 Aug 2023, 18:37

Re: SaveJPG

Post by TonyTooth »

Finally got it to work. SDL2_image requires libjpeg-9.dll and zlib1.dll in order for jpg conversion to work. Earlier versions do not work.
Hated Moron

Re: SaveJPG

Post by Hated Moron »

TonyTooth wrote: Sat 23 Dec 2023, 17:13 Finally got it to work.
Please list your working code. I imagine that other people might well be interested in being able to save JPG images from BBCSDL (I might myself!) so this could be a very useful building block. In fact, it would be very helpful if you could write a Wiki article (or extend the existing article about saving JPG files from BB4W) to explain how this is achieved, including how to overcome the pitfalls you encountered.
TonyTooth
Posts: 7
Joined: Mon 07 Aug 2023, 18:37

Re: SaveJPG

Post by TonyTooth »

I was thinking of doing just as you suggest - so will do.

The most frustrating aspect was the vague SDL2 documentation - so vague as to be almost useless on the topic. Next in line was people pointing to the wrong DLLs. Only a single one was correct. Where they found that out in the first place is a mystery to me.

I'll post proper details shortly.
Hated Moron

Re: SaveJPG

Post by Hated Moron »

TonyTooth wrote: Thu 28 Dec 2023, 21:32 The most frustrating aspect was the vague SDL2 documentation
I've never needed to use SDL2_image so I can't comment on that. The core SDL2 documentation itself isn't too bad, in my experience, although it's lacking in detail and sometimes one needs to refer to the function descriptions in the headers/C-code files. Since it's Open Source this is at least a fairly straightforward thing to do (I keep a copy of the SDL2 source on my PC for convenience).

Similarly whilst the Add SDL constants utility (slot 5 in the SDLIDE Utilities menu) will automatically create declarations for the constants used in core SDL2, it doesn't know anything about SDL2_image so won't help you in that regard.