-
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:
(http://www.sfml-dev.org/download/goodies/sfml-logo-small.png)
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;
}
-
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.
-
And what does
base64_decode
function does?
-
@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
-
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.
-
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