lvandeve / lodepng

PNG encoder and decoder in C and C++.
zlib License
2.08k stars 425 forks source link

How to get a JSON string from the encoded image ? #178

Open joaopfg opened 1 year ago

joaopfg commented 1 year ago

Hi,

I want to send images encoded with this library plus some metadata as strings to another C program. I have some problems:

1) The bytes of the encoded image contains many string null terminator characters ('\0') in the middle. So, the C string libs can't create the string from the bytes array. As a work around, I created my own encoder for this special case, which uses memcpy C lib function to copy the PNG bytes to my string. But this isn't a valid C string. There are many '\0' in the middle.

2) The receiver of the data also can't decode the "string" using C JSON libs because of the same reason (null terminator characters ('\0') in the middle). As a work around, I created my own decoder for this special case, which will look for characters like ',' to identify the end of the PNG bytes and the start of the other message fields.

Is there some better idea ? Maybe there is some lib that implements JSON encoding for the PNG bytes ? I can't find any mention to this in the source code of this lib.

Thanks in advance

cosinekitty commented 1 year ago

I have a general suggestion that is not specific to lodepng. How about using base64 to encode the PNG binary data a string. Then you can embed the string directly in your JSON document. Base64 is supported by Python. You will probably find the encoding is smaller than what you are trying to do now, because you won't have any backslashed escape sequences.

joaopfg commented 1 year ago

@cosinekitty Thanks a lot, this lib solved my problem.

Making a hook, do you know about some C lib to work with images (common operations like resize, rotate etc) ?

cosinekitty commented 1 year ago

Glad I could help. I'm not sure about image operations, but maybe somebody else here will know a good suggestion.