Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
joaopfg opened this issue Dec 9, 2022 · 3 comments
Open

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

joaopfg opened this issue Dec 9, 2022 · 3 comments

Comments

@joaopfg
Copy link

joaopfg commented Dec 9, 2022

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

@joaopfg joaopfg changed the title How to send the encoded image as a JSON string ? How to get a JSON string from the encoded image ? Dec 9, 2022
@joaopfg joaopfg changed the title How to get a JSON string from the encoded image ? How to get a string from the encoded image ? Dec 9, 2022
@joaopfg joaopfg changed the title How to get a string from the encoded image ? How to get a JSON string from the encoded image ? Dec 9, 2022
@cosinekitty
Copy link
Contributor

cosinekitty commented Dec 9, 2022

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
Copy link
Author

joaopfg commented Dec 9, 2022

@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
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants