Skip to content

Commit d46ed5e

Browse files
authored
feat: support JPEG compression (leejet#583)
1 parent 2535ad5 commit d46ed5e

File tree

2 files changed

+49
-10
lines changed

2 files changed

+49
-10
lines changed

examples/cli/main.cpp

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,16 +1057,41 @@ int main(int argc, const char* argv[]) {
10571057
}
10581058
}
10591059

1060-
size_t last = params.output_path.find_last_of(".");
1061-
std::string dummy_name = last != std::string::npos ? params.output_path.substr(0, last) : params.output_path;
1060+
std::string dummy_name, ext, lc_ext;
1061+
bool is_jpg;
1062+
size_t last = params.output_path.find_last_of(".");
1063+
size_t last_path = std::min(params.output_path.find_last_of("/"),
1064+
params.output_path.find_last_of("\\"));
1065+
if (last != std::string::npos // filename has extension
1066+
&& (last_path == std::string::npos || last > last_path)) {
1067+
dummy_name = params.output_path.substr(0, last);
1068+
ext = lc_ext = params.output_path.substr(last);
1069+
std::transform(ext.begin(), ext.end(), lc_ext.begin(), ::tolower);
1070+
is_jpg = lc_ext == ".jpg" || lc_ext == ".jpeg" || lc_ext == ".jpe";
1071+
} else {
1072+
dummy_name = params.output_path;
1073+
ext = lc_ext = "";
1074+
is_jpg = false;
1075+
}
1076+
// appending ".png" to absent or unknown extension
1077+
if (!is_jpg && lc_ext != ".png") {
1078+
dummy_name += ext;
1079+
ext = ".png";
1080+
}
10621081
for (int i = 0; i < params.batch_count; i++) {
10631082
if (results[i].data == NULL) {
10641083
continue;
10651084
}
1066-
std::string final_image_path = i > 0 ? dummy_name + "_" + std::to_string(i + 1) + ".png" : dummy_name + ".png";
1067-
stbi_write_png(final_image_path.c_str(), results[i].width, results[i].height, results[i].channel,
1068-
results[i].data, 0, get_image_params(params, params.seed + i).c_str());
1069-
printf("save result image to '%s'\n", final_image_path.c_str());
1085+
std::string final_image_path = i > 0 ? dummy_name + "_" + std::to_string(i + 1) + ext : dummy_name + ext;
1086+
if(is_jpg) {
1087+
stbi_write_jpg(final_image_path.c_str(), results[i].width, results[i].height, results[i].channel,
1088+
results[i].data, 90, get_image_params(params, params.seed + i).c_str());
1089+
printf("save result JPEG image to '%s'\n", final_image_path.c_str());
1090+
} else {
1091+
stbi_write_png(final_image_path.c_str(), results[i].width, results[i].height, results[i].channel,
1092+
results[i].data, 0, get_image_params(params, params.seed + i).c_str());
1093+
printf("save result PNG image to '%s'\n", final_image_path.c_str());
1094+
}
10701095
free(results[i].data);
10711096
results[i].data = NULL;
10721097
}

thirdparty/stb_image_write.h

Lines changed: 18 additions & 4 deletions
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)