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

Author Topic: Can someoene help with loadFromMemory()  (Read 2409 times)

0 Members and 1 Guest are viewing this topic.

kaB00M

  • Full Member
  • ***
  • Posts: 101
    • View Profile
    • Caffeware
    • Email
Can someoene help with loadFromMemory()
« on: August 12, 2012, 03:56:30 am »
Can someoene help with  loadFromMemory() ? I mean how to implement and use it.
I tried as a test:

sf::Image *image = new sf::Image();
image->loadFromFile("file.png"); //works

sf::Image image2;
image2.loadFromMemory(image, sizeof(*image)); //doesn't work
 

Doesn't work...

« Last Edit: August 12, 2012, 04:00:39 am by kaB00M »



FRex

  • Hero Member
  • *****
  • Posts: 1845
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Can someoene help with loadFromMemory()
« Reply #1 on: August 12, 2012, 04:57:58 am »
I think that the first argument is supposed to point to array of RGBA pixels not image, image can give you the pointer to pixels it's storing, and second argument, size, is 4*amount of pixels.
So if you want to load from one image into other like that you should do:
sf::Image *image new sf::Image();
image->loadFromFile("file.png");

sf::Image image2;
image2.loadFromMemory(image->getPixelsPtr(),image->getSize().x*image->getSize().y*4);
 
Altho it's somewhat insane to dynamically allocate first image.
getPixelsPtr returns read only pointer but I'm not sure if image2 will make a deep copy for itself so you might want to mess with first image, delete it, ect. and see if the second one still works, or wait for Laurent to clarify that.  :)
Back to C++ gamedev with SFML in May 2023

kaB00M

  • Full Member
  • ***
  • Posts: 101
    • View Profile
    • Caffeware
    • Email
Re: Can someoene help with loadFromMemory()
« Reply #2 on: August 12, 2012, 07:38:57 am »
Its not working.  :'(

I'm getting a "Failed to load from memory. Image not of know type, or corrupt" error.

 :-\ :-\ :-\ :-\ :-\ :-\



Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Can someoene help with loadFromMemory()
« Reply #3 on: August 12, 2012, 10:34:52 am »
Have you read the doc? Everything's clearly explained. If not, please tell me what you don't understand and I'll try to clarify.

And, most important, what is the problem here? You think that you need loadFromMemory, but you don't. So what do you want to do?
Laurent Gomila - SFML developer

kaB00M

  • Full Member
  • ***
  • Posts: 101
    • View Profile
    • Caffeware
    • Email
Re: Can someoene help with loadFromMemory()
« Reply #4 on: August 12, 2012, 05:12:52 pm »
Have you read the doc? Everything's clearly explained. If not, please tell me what you don't understand and I'll try to clarify.

I have read this:

Quote
bool sf::Image::loadFromMemory    (    const void *     data,
      std::size_t     size)       

Load the image from a file in memory.

The supported image formats are bmp, png, tga, jpg, gif, psd, hdr and pic. Some format options are not supported, like progressive jpeg. If this function fails, the image is left unchanged.

Parameters:
    data   Pointer to the file data in memory
    size   Size of the data to load, in bytes

Returns:
    True if loading was successful

See also:
    loadFromFile, loadFromStream


I still don't know. :(

Quote
And, most important, what is the problem here? You think that you need loadFromMemory, but you don't. So what do you want to do?

I'm just trying new things. Sometimes one learns something new and realizes it's better than the old.

Also, is this true.
Quote
Altho it's somewhat insane to dynamically allocate first image.

Thank you for your time Laurent.



Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Can someoene help with loadFromMemory()
« Reply #5 on: August 12, 2012, 06:12:00 pm »
Look, it's written just there :P
Quote
Load the image from a file in memory
A file, not an array of pixels.
Which is even clearer with the next line:
Quote
The supported image formats are bmp, png, tga, jpg, gif, psd, hdr and pic.
So loadFromMemory is the same as loadFromFile, except that the file is in RAM, not on the disk.

Quote
Also, is this true.
Quote
Altho it's somewhat insane to dynamically allocate first image.
It's insane to dynamically allocate something that doesn't need to be dynamically allocated ;)
So the answer is yes, unless you have a very good reason to do so (but in this case you would probably not ask this question).
Laurent Gomila - SFML developer

 

anything