-
Notifications
You must be signed in to change notification settings - Fork 357
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
Support JPEG compression #583
Conversation
with parameters of generation
thirdparty/stb_image_write.h
Outdated
stbiw__putc(s, param_length >> 8); // no need to mask, length < 65536 | ||
stbiw__putc(s, param_length & 0xFF); | ||
s->func(s->context, (void*)"parameters", strlen("parameters") + 1); // std::string is zero-terminated | ||
s->func(s->context, (void*)parameters, param_length - 2 - strlen("parameters") - 1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be missing a zero-termination for parameters with length above 65522 (0xFFFF - strlen("parameters") - 1 - 1)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@idostyle, thanks for review.
JPEG comment block:
comment_marker { 0xFF, 0xFE /* comment marker */ }
comment_body {
uint16be comment_body.length(),
"comment" /* std::string terminates with 0 for compatibility with C strings */
}
Comment length can be 2..65535 bytes, according to https://www.w3.org/Graphics/JPEG/itu-t81.pdf, "Table B.8 – Comment segment parameter sizes and values".
size_t param_length = std::min(2 + strlen("parameters") + 1 + strlen(parameters) + 1, (size_t) 0xFFFF);
// maker_length + len('parameters') + 0 + params.length() + 0,
// truncated to 65535 bytes, so comment can be 65535 - 2 - 10 - 1 - 1 = 65521 bytes.
s->func(s->context, (void*)parameters, param_length - 2 - strlen("parameters") - 1);
// 65535 - 2 /* marker*/ - 10 - 1 /* 'parameters' + 0 */
Second 0, terminating params, is not substracted, therefore it will be written.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@idostyle, I finally got it, you're right, despite that length of comment block is limited, it should always be terminated with 0, for safety. Fixed.
Nice. I'm surprised stb_image doesn't support metadata by default. |
Thank you for your contribution. |
JPEG quality is simply set to 90, to not complicate command options.
And thanks for the program! 🙂