Recent

Author Topic: [Solved] Linux - Any bindings/header translations for stb_image or similar?  (Read 914 times)

SuperSathanas

  • New Member
  • *
  • Posts: 16
This is very similar to my other post regarding font glyph rendering.

So, with Delphi on Windows, I used DelphiStb (https://github.com/neslib/DelphiStb) to load images from disk to memory buffers that I could then use with OpenGL. There doesn't seem to be an equivalent or similar library/binding that works with FPC on Linux.

I'd really like to avoid having to create a TBitmap or other object/component, have it load the image, then grab the data from it to stick into another memory buffer. I'm looking for something that will just read the image from disk and shove that raw pixel data right into a memory buffer.

I went as far as writing my own functions for various types of bitmaps and PNG, but I'd also like to not have to keep dealing with edge cases or continuing to have to learn how to decompress more image formats to make the code more practical.

Things, like SFML and SLD2 exist, but I'm also going to be using my own Windowing and OpenGL code, and I'd like to be able to just load the image directly into memory where I can do with it what I please from there, instead of having to incur the overhead or abstractions of those other libraries/frameworks.

Edit: it turns out that creating the shared object library for the stb headers I wanted and then altering the Neslib.Stb units to load the library was extremely painless, though considering that the last commit on the Neslib repo was 8 years, ago, I could scan over the latest stb headers to see what needs to be added or changed. I'll end up either uploading the shared object library and the units to my own repo, or see if I can't get get the changes merged to the original Neslib repo.
« Last Edit: May 31, 2024, 04:36:46 pm by SuperSathanas »

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Linux - Any bindings/header translations for stb_image or similar?
« Reply #1 on: May 27, 2024, 03:31:28 am »
Take a look at BGRABitmap and BGRAControls packages, its offering GL and some wrappers including to load images plus the extras from the BGRA core. If it works in the way you want it to be you will find out by watching sources.
RayLib could be also a possibility, offering many wrappers for many things but again, you will need to checkout sources on your own if it meet your need.

As by now I am more than clueless what you are trying to achieve, how about you attach an image that your delphi app created so we can better understand your Font and Image issues with OpenGL.

And for the experts it would be more than useful if you tell what GL binding you "must" use, since each distribution may differ.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

SuperSathanas

  • New Member
  • *
  • Posts: 16
Re: Linux - Any bindings/header translations for stb_image or similar?
« Reply #2 on: May 27, 2024, 05:49:31 am »
I had looked at BGRABitmap, but it seems to entail more than what I'm looking for. For the images, I simply want to be able to load an image from disk to a memory buffer, and save images in memory to disk, which is exactly what the stb_image.h and stb_image_write.h headers allow me to do. With the shared library and the header translation, loading an image to memory and using with OpenGL is as simple as

Code: Pascal  [Select][+][-]
  1. procedure NeatExample();
  2. var
  3. W,H,C: Integer; // receive width, height and number of channels in loaded image
  4. Img: PByte;
  5. Tex: Cardinal;
  6.   begin
  7.      // load image from file_path, requesting that the buffer returned has 4 color channels
  8.     Img := stbi_load(PAnsiChar('file_path'), W, H, C, 4);
  9.  
  10.     // create gl texture from data in Img
  11.     glGenTextures(1, @Tex);
  12.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, W, H, 0, GL_RGBA, GL_UNSIGNED_BYTE, Img);
  13.  
  14.     // save Img to disk
  15.     stbi_write_png(PAnsiChar('new_file_path'), W, H, C, 0);
  16.  
  17.     stbi_image_free(Img); // free Img's memory
  18.   end;
  19.  

I want just a simple, straightforward way to load images to memory and save them to file. no more than that, no having to instantiate objects that wrap the functionality, just right to the point, load and save.

The bindings I'm using for OpenGL aren't really relevant to what I'm wanting, I just mentioned that I'm using OpenGL to help make the point that I really just want the raw image data and nothing more. I just want to be able to work with the raw image data with OpenGL, not manipulate it with other libraries like BGRABitamp.

In any case, I guess I went ahead and solved my own problem, because I compiled the stb headers into a shared object library and made some surprisingly simple edits to the Neslib units and it's all working as expected. I don't think it's going to be so simple with the FreeType headers, though.

cdbc

  • Hero Member
  • *****
  • Posts: 1340
    • http://www.cdbc.dk
Re: Linux - Any bindings/header translations for stb_image or similar?
« Reply #3 on: May 27, 2024, 08:47:40 am »
Hi
Hmmm, there's allways 'FPImage' in FPC's RTL, I think it could do what you're looking for, with not much /Frills/, you should have a look....
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Linux - Any bindings/header translations for stb_image or similar?
« Reply #4 on: May 27, 2024, 10:28:29 am »
I had looked at BGRABitmap, but it seems to entail more than what I'm looking for.
Yes, mostly any GL binding offer more than you would use/need, so whatever we would suggest, its out of your scope since they are capable to do more than load an image into memory.....
loading an image to memory and using with OpenGL is as simple as
BGRABitmap in GL mode -> "SomeIBGLTextureVariable := BGLTexture('filename.jpg');" but it offers more so its a no-go.

To me it seems that you are limited to your delphi units and not open for new things.

So why not convert your units to make them FPC compatible and all your GL problems are solved?
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

SuperSathanas

  • New Member
  • *
  • Posts: 16
Re: Linux - Any bindings/header translations for stb_image or similar?
« Reply #5 on: May 27, 2024, 02:42:56 pm »
To me it seems that you are limited to your delphi units and not open for new things.

I get the feeling that you're making assumptions about what I want and why I want it. I don't want OpenGL bindings. I have my own OpenGL code that I was using with Delphi on Windows, and now I'm trying to get it ported to FPC on Linux. As I said in my last reply, I went ahead and compiled the relevant headers from the stb library into a shared object library and made the few necessary changes to the units. I decided to just try solving the issue while waiting to see if anyone would recommend something I was unaware of. It turned out to be way easier than I assumed it would be.

The only thing I was looking for was an existing unit (or units) that could load an image from disk into a simple memory buffer of the raw pixel data, no wrapping it up in classes or additional functionality, no higher abstractions, because that basic functionality that I want is easy to integrate into not just what I'm doing currently, but really anything else needing image loading/saving in the future.

But again, it's solved. I'll be putting the shared object library and the units up somewhere for other people to use should they want it after I'm done bringing the units up to date with the current versions of the headers. After that, I guess I'm moving on to making some complete and functional FreeType2 bindings for FPC. because what ships with FPC is incomplete and broken.

 

TinyPortal © 2005-2018