odin-lang / Odin

Odin Programming Language
https://odin-lang.org
BSD 3-Clause "New" or "Revised" License
7k stars 620 forks source link

JPG support in raylib #4220

Open paramita opened 2 months ago

paramita commented 2 months ago

It seems that raylib only supports PNG/GIF/QOI/DDS by default: https://github.com/raysan5/raylib/blob/master/src/config.h#L162 It was recently discussed at https://discord.com/channels/568138951836172421/568871298428698645/1263162072557162496

I started to learn odin and wanted to build the image viewer app, so I immediately faced that issue. I hope it's possible to widen the supported image formats. For example, enabling SVG support would be beneficial for building UI elements since the vector image can be scaled to any size.

The following warning is shown when loading jpg as Image or Texture2D:

INFO: FILEIO: [cat.jpg] File loaded successfully
WARNING: IMAGE: Data format not supported
WARNING: IMAGE: Failed to load image data
Texture{id = 0, width = 0, height = 0, mipmaps = 0, format = "UNKNOWN"}

Operating System & Odin Version:

Odin:    dev-2024-09-nightly:dd1f151
OS:      Windows 10 Unknown Edition (000000bf) (version: 21H2), build 19044.4780
CPU:     13th Gen Intel(R) Core(TM) i5-13600K
RAM:     32541 MiB
Backend: LLVM 18.1.8
Feoramund commented 2 months ago

I haven't used our raylib bindings yet, but I imagine you could compile raylib with the configurations you want set to enabled and use the resulting library object instead of the one shipped with Odin. That might be a good workaround for now.

Vonixxx commented 2 months ago

I also needed JPG support. I simply recompiled Raylib with -DCUSTOMIZE_BUILD=ON, -DSUPPORT_FILEFORMAT_JPG=ON and -Draylib_USE_STATIC_LIBS=ON to enable it, then I copied the resulting libraylib.a into the appropriate Odin directory, and recompiled Odin as well.

laytan commented 2 months ago

I don't think we should really deviate from the default Raylib build, like others said you can recompile with support and switch out the library.

An idea might be to better document (or provide scripts for) this process of recompilation.

Kelimion commented 2 months ago

You could also petition Raylib upstream to have JPG included by default. I don't believe there's any patent encumbrance in JPG since the patent on arithmetic coding expired. And even while that patent was still alive, most JPG implementations (including Photoshop) went with Huffman coding instead of arithmetic coding because of that patent, so there was no reason to exclude JPG support that I can see.

JulienLecoq commented 2 months ago

@paramita I just faced the same issue with SVG, it's very annoying from a user point of view. You're like, oh cool, I can just call LoadImageSvg() and you do it and it explode at you because in reality it's not there by default and you don't have any explanation of what to do to enable it.

JulienLecoq commented 2 months ago

@Vonixxx Could you explain quickly how to recompile Raylib? I don't see much stuff in Odin/vendor/raylib. I don't have a strong background on compilation and so on.

Vonixxx commented 2 months ago

@Vonixxx Could you explain quickly how to recompile Raylib? I don't see much stuff in Odin/vendor/raylib. I don't have a strong background on compilation and so on.

I see you're on Windows, so I cannot assist with that. I use Linux.

You have to visit Raylib's own Github page for that anyhow, here's a link explaining how to build for Windows.

JulienLecoq commented 2 months ago

@Vonixxx Could you explain quickly how to recompile Raylib? I don't see much stuff in Odin/vendor/raylib. I don't have a strong background on compilation and so on.

I see you're on Windows, so I cannot assist with that. I use Linux.

You have to visit Raylib's own Github page for that anyhow, here's a link explaining how to build for Windows.

I'm on MacOS. Thanks, I'll look into that for MacOS.

JulienLecoq commented 2 months ago

Mhh I'm a bit lost, where is raylib even installed when we install Odin? How can we find that per OS?

flysand7 commented 2 months ago

It's in the vendor folder inside the Odin repository

JulienLecoq commented 2 months ago

I was asking for the code but I noticed that it does not install the code itself šŸ‘šŸ»

@paramita I don't know if you succeeded to compile it with JPG enabled but if not, here is how I did it on MacOS on Apple Silicon architecture (idk the Windows equivalent):

  1. Install cmake with homebrew (requires homebrew to be installed): brew install cmake
  2. Clone raylib: git clone git@github.com:raysan5/raylib.git
  3. Go to the raylib directory: cd raylib
  4. Open the file CMakeLists.txt
  5. Add the line add_compile_definitions(SUPPORT_FILEFORMAT_JPG) just below the line include(CMakeOptions.txt)
  6. Save & quit the file and create a directory called build: mkdir build
  7. Go to the build directory: cd build
  8. Run the command: cmake ..
  9. Run the command: make
  10. Copy the file from raylib/build/raylib/libraylib.a to Odin/vendor/raylib/macos-arm64
  11. Load the file in your program and run your program, it should works šŸ™‚