Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 441f0bb

Browse files
vmobilisstduhpf
authored andcommittedFeb 6, 2025
feat: support JPEG compression (leejet#583)
1 parent a14a8a8 commit 441f0bb

File tree

2 files changed

+49
-10
lines changed

2 files changed

+49
-10
lines changed
 

‎examples/cli/main.cpp

+31-6
Original file line numberDiff line numberDiff line change
@@ -1071,16 +1071,41 @@ int main(int argc, const char* argv[]) {
10711071
}
10721072
}
10731073

1074-
size_t last = params.output_path.find_last_of(".");
1075-
std::string dummy_name = last != std::string::npos ? params.output_path.substr(0, last) : params.output_path;
1074+
std::string dummy_name, ext, lc_ext;
1075+
bool is_jpg;
1076+
size_t last = params.output_path.find_last_of(".");
1077+
size_t last_path = std::min(params.output_path.find_last_of("/"),
1078+
params.output_path.find_last_of("\\"));
1079+
if (last != std::string::npos // filename has extension
1080+
&& (last_path == std::string::npos || last > last_path)) {
1081+
dummy_name = params.output_path.substr(0, last);
1082+
ext = lc_ext = params.output_path.substr(last);
1083+
std::transform(ext.begin(), ext.end(), lc_ext.begin(), ::tolower);
1084+
is_jpg = lc_ext == ".jpg" || lc_ext == ".jpeg" || lc_ext == ".jpe";
1085+
} else {
1086+
dummy_name = params.output_path;
1087+
ext = lc_ext = "";
1088+
is_jpg = false;
1089+
}
1090+
// appending ".png" to absent or unknown extension
1091+
if (!is_jpg && lc_ext != ".png") {
1092+
dummy_name += ext;
1093+
ext = ".png";
1094+
}
10761095
for (int i = 0; i < params.batch_count; i++) {
10771096
if (results[i].data == NULL) {
10781097
continue;
10791098
}
1080-
std::string final_image_path = i > 0 ? dummy_name + "_" + std::to_string(i + 1) + ".png" : dummy_name + ".png";
1081-
stbi_write_png(final_image_path.c_str(), results[i].width, results[i].height, results[i].channel,
1082-
results[i].data, 0, get_image_params(params, params.seed + i).c_str());
1083-
printf("save result image to '%s'\n", final_image_path.c_str());
1099+
std::string final_image_path = i > 0 ? dummy_name + "_" + std::to_string(i + 1) + ext : dummy_name + ext;
1100+
if(is_jpg) {
1101+
stbi_write_jpg(final_image_path.c_str(), results[i].width, results[i].height, results[i].channel,
1102+
results[i].data, 90, get_image_params(params, params.seed + i).c_str());
1103+
printf("save result JPEG image to '%s'\n", final_image_path.c_str());
1104+
} else {
1105+
stbi_write_png(final_image_path.c_str(), results[i].width, results[i].height, results[i].channel,
1106+
results[i].data, 0, get_image_params(params, params.seed + i).c_str());
1107+
printf("save result PNG image to '%s'\n", final_image_path.c_str());
1108+
}
10841109
free(results[i].data);
10851110
results[i].data = NULL;
10861111
}

‎thirdparty/stb_image_write.h

+18-4
Original file line numberDiff line numberDiff line change
@@ -1412,7 +1412,7 @@ static int stbiw__jpg_processDU(stbi__write_context *s, int *bitBuf, int *bitCnt
14121412
return DU[0];
14131413
}
14141414

1415-
static int stbi_write_jpg_core(stbi__write_context *s, int width, int height, int comp, const void* data, int quality) {
1415+
static int stbi_write_jpg_core(stbi__write_context *s, int width, int height, int comp, const void* data, int quality, const char* parameters) {
14161416
// Constants that don't pollute global namespace
14171417
static const unsigned char std_dc_luminance_nrcodes[] = {0,0,1,5,1,1,1,1,1,1,0,0,0,0,0,0,0};
14181418
static const unsigned char std_dc_luminance_values[] = {0,1,2,3,4,5,6,7,8,9,10,11};
@@ -1521,6 +1521,20 @@ static int stbi_write_jpg_core(stbi__write_context *s, int width, int height, in
15211521
s->func(s->context, (void*)YTable, sizeof(YTable));
15221522
stbiw__putc(s, 1);
15231523
s->func(s->context, UVTable, sizeof(UVTable));
1524+
1525+
// comment block with parameters of generation
1526+
if(parameters != NULL) {
1527+
stbiw__putc(s, 0xFF /* comnent */ );
1528+
stbiw__putc(s, 0xFE /* marker */ );
1529+
size_t param_length = std::min(2 + strlen("parameters") + 1 + strlen(parameters) + 1, (size_t) 0xFFFF);
1530+
stbiw__putc(s, param_length >> 8); // no need to mask, length < 65536
1531+
stbiw__putc(s, param_length & 0xFF);
1532+
s->func(s->context, (void*)"parameters", strlen("parameters") + 1); // std::string is zero-terminated
1533+
s->func(s->context, (void*)parameters, std::min(param_length, (size_t) 65534) - 2 - strlen("parameters") - 1);
1534+
if(param_length > 65534) stbiw__putc(s, 0); // always zero-terminate for safety
1535+
if(param_length & 1) stbiw__putc(s, 0xFF); // pad to even length
1536+
}
1537+
15241538
s->func(s->context, (void*)head1, sizeof(head1));
15251539
s->func(s->context, (void*)(std_dc_luminance_nrcodes+1), sizeof(std_dc_luminance_nrcodes)-1);
15261540
s->func(s->context, (void*)std_dc_luminance_values, sizeof(std_dc_luminance_values));
@@ -1625,16 +1639,16 @@ STBIWDEF int stbi_write_jpg_to_func(stbi_write_func *func, void *context, int x,
16251639
{
16261640
stbi__write_context s = { 0 };
16271641
stbi__start_write_callbacks(&s, func, context);
1628-
return stbi_write_jpg_core(&s, x, y, comp, (void *) data, quality);
1642+
return stbi_write_jpg_core(&s, x, y, comp, (void *) data, quality, NULL);
16291643
}
16301644

16311645

16321646
#ifndef STBI_WRITE_NO_STDIO
1633-
STBIWDEF int stbi_write_jpg(char const *filename, int x, int y, int comp, const void *data, int quality)
1647+
STBIWDEF int stbi_write_jpg(char const *filename, int x, int y, int comp, const void *data, int quality, const char* parameters)
16341648
{
16351649
stbi__write_context s = { 0 };
16361650
if (stbi__start_write_file(&s,filename)) {
1637-
int r = stbi_write_jpg_core(&s, x, y, comp, data, quality);
1651+
int r = stbi_write_jpg_core(&s, x, y, comp, data, quality, parameters);
16381652
stbi__end_write_file(&s);
16391653
return r;
16401654
} else

0 commit comments

Comments
 (0)
Please sign in to comment.