Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: How to use sf::Image:LoadFromMemory?  (Read 2903 times)

0 Members and 1 Guest are viewing this topic.

kullerhamPster

  • Newbie
  • *
  • Posts: 40
    • View Profile
How to use sf::Image:LoadFromMemory?
« on: February 05, 2017, 07:05:39 pm »
From what I have read in the documentation and some posts I found here in the forum, this function does not expect just an array of RGBA values, but an entire image (file) in memory, and therefore 'data' has to point to the first byte of the file in memory.
But if this is true, I don't know why the second parameter 'size' is needed and how it is interpreted.

If the function expects an entire file, it could determine the size of the image via the file's header, just as loadFromFile probably does (which doesn't expect a size). So I guess I understand something wrong here and hope that someone can explain why this parameter is needed.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
How to use sf::Image:LoadFromMemory?
« Reply #1 on: February 05, 2017, 07:38:19 pm »
The size is not the image size, but the size of the memory block, so the function knows when to stop reading. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

kullerhamPster

  • Newbie
  • *
  • Posts: 40
    • View Profile
Re: How to use sf::Image:LoadFromMemory?
« Reply #2 on: February 05, 2017, 08:49:59 pm »
The size is not the image size, but the size of the memory block, so the function knows when to stop reading. ;)

Thanks for the clarification :)
So it's basically the filesize of the image file in byte?

But I still don't quite understand why it is needed. If the function has a pointer to the start of the file data, it can (and probably must) read the entire file header to find out the image type, the bpp, and the dimensions. With all that information, shouldn't it be possible to calulate the size of the memory block? But perhaps I'm thinking too much in terms of uncompressed BMP images and it's more complicated for compressed image data?!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: How to use sf::Image:LoadFromMemory?
« Reply #3 on: February 05, 2017, 09:13:58 pm »
Regardless if the theoretical size can be deduced from the data, a pointer to an array in C must always go with its real size. Even when the size can be deduced, it's more to be sure that we won't read/write past the buffer provided by the caller. You know, crashes, security vulnerabilities and this kind of stuff... ;)

And yes, I don't think it's safe to assume that every file format stores its size in the headers. And even if it's in the headers at byte N, you can never be sure that buffer has at least those N+1 bytes.
« Last Edit: February 06, 2017, 06:23:08 am by Laurent »
Laurent Gomila - SFML developer

kullerhamPster

  • Newbie
  • *
  • Posts: 40
    • View Profile
Re: How to use sf::Image:LoadFromMemory?
« Reply #4 on: February 05, 2017, 09:28:37 pm »
Thank you for the explanation. :)