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

Author Topic: loadFromMemory using existing SFML image?  (Read 2816 times)

0 Members and 1 Guest are viewing this topic.

Chili

  • Guest
loadFromMemory using existing SFML image?
« on: March 16, 2013, 11:45:19 pm »
Title more or less explains what I'm trying to do, extract the data and size of a current SFML loaded image so I can load another image with the loadFromMemory function. So how do one exactly extract the data and get the size of an image?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10820
    • View Profile
    • development blog
    • Email
Re: loadFromMemory using existing SFML image?
« Reply #1 on: March 17, 2013, 12:07:39 am »
Hmm... by looking at the magical documentation? ;D

sf::Image img = texture.copyToImage(); // Heavy operation
img.getSize(); // Get size
img.getPixel(); // Get single pixel data
img.getPixelsPtr(); // Get pointer to the pixel array
 

btw why would you want to load SFML images with loadFromMemory, if they're already natively supported? It makes imho no sense at all, or I did understand you wrong. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Chili

  • Guest
Re: loadFromMemory using existing SFML image?
« Reply #2 on: March 17, 2013, 12:09:50 am »
I want to load it from memory since I need to send the image in its current form to a client when it connects to the server through winsock. Converting it to a data array and sending the size of that array before the array itself so the client know how large the buffer needs to be seems like the best option.

Also, one of these days I swear I will understand the documentation better, think this is the second time I missed something so obvious even after looking over it four times. Thanks though!
« Last Edit: March 17, 2013, 12:14:48 am by Chili »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10820
    • View Profile
    • development blog
    • Email
Re: loadFromMemory using existing SFML image?
« Reply #3 on: March 17, 2013, 12:16:00 am »
I want to load it from memory since I need to send an image to a client when it connects to the server through winsock
Oh okay that makes kind of sense then.
If the image isn't generated within the application, but exists already as file on the disk, then it might be better just to send the binary file, rather than loading the file, getting all the pixel data and sending it over.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Chili

  • Guest
Re: loadFromMemory using existing SFML image?
« Reply #4 on: March 17, 2013, 12:26:40 am »
Hmm never considered that, will have to look into that once I iron out a few bottlenecks in the current code.

Edit: Ah! While the image isn't created inside the program it still got some edits done to it that is important that the client receives, of course saving the image in its current format and sending away that would work but would it be faster?
« Last Edit: March 17, 2013, 12:33:25 am by Chili »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10820
    • View Profile
    • development blog
    • Email
AW: loadFromMemory using existing SFML image?
« Reply #5 on: March 17, 2013, 12:44:12 am »
Questions about performance can mostly just be determined by benchmarkings. ;)

If everything is already in memory, you can easily send it.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Chili

  • Guest
Re: loadFromMemory using existing SFML image?
« Reply #6 on: March 17, 2013, 12:50:04 am »
Hmm... by looking at the magical documentation? ;D

sf::Image img = texture.copyToImage(); // Heavy operation
img.getSize(); // Get size
img.getPixel(); // Get single pixel data
img.getPixelsPtr(); // Get pointer to the pixel array
 

btw why would you want to load SFML images with loadFromMemory, if they're already natively supported? It makes imho no sense at all, or I did understand you wrong. ;)
Just finished eating and had time to start working on this, while getSize do get the size of the image it´s not the size I'm looking for(at least I don't think it is), when I said size I meant how many bytes the image is, not the dimension of the image. Also, how do you convert an Uint8 to a char?Type casting it as a char seems to work fine.
« Last Edit: March 17, 2013, 12:56:51 am by Chili »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10820
    • View Profile
    • development blog
    • Email
AW: loadFromMemory using existing SFML image?
« Reply #7 on: March 17, 2013, 07:15:18 am »
If you're going to work on networking, you'll have to know such things. ;)
The size of an image is witdh*height*4*sizeof(sf::Uint8).
That is, each pixel is built from 4 sf::Uint8.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: loadFromMemory using existing SFML image?
« Reply #8 on: March 17, 2013, 09:23:58 am »
Everything that you need to know is written in the documentation, just read it carefully ;)

By the way, loadFromMemory loads an image from a file in memory. To load it from a pixel array you must call the create function.
Laurent Gomila - SFML developer

Chili

  • Guest
Re: AW: loadFromMemory using existing SFML image?
« Reply #9 on: March 17, 2013, 08:23:13 pm »
The size of an image is witdh*height*4*sizeof(sf::Uint8).
Any reason you specified "sizeof(sf::uint8)" seeing how it´s an usigned char?

Everything that you need to know is written in the documentation, just read it carefully ;)

By the way, loadFromMemory loads an image from a file in memory. To load it from a pixel array you must call the create function.
I see, got it to work instantly now. Thanks!

 

anything