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

Author Topic: read image from string[solved]  (Read 3472 times)

0 Members and 1 Guest are viewing this topic.

Mortal

  • Sr. Member
  • ****
  • Posts: 284
    • View Profile
read image from string[solved]
« on: January 06, 2016, 07:13:29 pm »
hello

i was working on sf::texture to load image from memory by encoding the source image in base46 and decode it back to be able to load it in sf::texture. i have create encoder.cpp and decoder.cpp to accomplish this as shown in these files

the encoder is simply encode the given image and create .pin file as binary
here encoder.cpp :  https://gist.github.com/MORTAL2000/05f5b100166203ed3cc7

the file being created like this. sfml-logo-small.pin
content : https://gist.github.com/MORTAL2000/4206d168740a8af7b295

the decoder is convert sfml-logo-small.pin to be readable in sf::Texture.
here decoder.cpp : https://gist.github.com/MORTAL2000/af02e3ac4212924a30e7

source image is found here:


if i loaded the encoded image "sfml-logo-small.pin", which is simply a string, from file, it works fine but if i copy and paste it into std::string object in encoder.cpp to read it directly instead of read it from file, i keep getting error says corrupt PNG. how to fix this.

P.S: i have to split the std::string object into two string like this:
static const std::string data0 = "copy and paste from sfml-logo-small.pin ... very long encoded string";
static const std::string data1 = "copy and paste from sfml-logo-small.pin ... very long encoded string";
otherwise the VC14 will complain about it as string is too big

and use it like this:
std::string decoded = base64_decode({ data0 + data1 });

sf::Texture texture;
if (!texture.loadFromMemory(decoded.c_str(), decoded.length()))
{
        return 1;
}
« Last Edit: January 06, 2016, 09:05:06 pm by MORTAL »

Mr_Blame

  • Full Member
  • ***
  • Posts: 192
    • View Profile
    • Email
Re: read image from string
« Reply #1 on: January 06, 2016, 08:44:41 pm »
Std::string is using
char
type as base and image data is stored in bytes aka
unsigned char
char range is from -128 to 127 and unsigned char range is from 0 to 255.
Also
sf::Texture::loadFromMemory
takes not pixel data but not decoed image data.
« Last Edit: January 06, 2016, 08:49:49 pm by Mr_Blame »

Mr_Blame

  • Full Member
  • ***
  • Posts: 192
    • View Profile
    • Email
Re: read image from string
« Reply #2 on: January 06, 2016, 08:49:20 pm »
And what does
base64_decode
function does?

Mortal

  • Sr. Member
  • ****
  • Posts: 284
    • View Profile
Re: read image from string
« Reply #3 on: January 06, 2016, 09:02:39 pm »
@Mr_Blame
thanks a lot for your help, i really appreciate it

i solved it, it turns out that when i copy and paste the string from sfml-logo-small.pin into std::string objects "data0 and data1" , IDE somehow modifies the string content and added a whitepaces in it.

i delete those extra whitespaces like this:
data1.erase(std::remove(data1.begin(), data1.end(), ' '), data1.end());
data0.erase(std::remove(data0.begin(), data0.end(), ' '), data0.end());

here complete example, it works finally:
https://gist.github.com/MORTAL2000/0cf97f0bcae366ede6c0

And what does
base64_decode
function does?
it dencodes given string in term of base64 to retrieve the original string "binary data" that sf::texture can load it
« Last Edit: January 06, 2016, 11:05:42 pm by MORTAL »

Mario

  • SFML Team
  • Hero Member
  • *****
  • Posts: 878
    • View Profile
Re: read image from string[solved]
« Reply #4 on: January 06, 2016, 09:28:22 pm »
Maybe it's just me, but you shouldn't use std::string to store binary data. It might work in your implementation, but others might screw it up (null termination and such).

If you want to use a STL container, use std::vector<unsigned char> instead.

Mortal

  • Sr. Member
  • ****
  • Posts: 284
    • View Profile
Re: read image from string[solved]
« Reply #5 on: January 06, 2016, 09:43:06 pm »
Maybe it's just me, but you shouldn't use std::string to store binary data. It might work in your implementation, but others might screw it up (null termination and such).

If you want to use a STL container, use std::vector<unsigned char> instead.
good point, i will try that

 

anything