Skip to content

Commit

Permalink
add jxl support to imglab
Browse files Browse the repository at this point in the history
  • Loading branch information
arrufat committed Feb 28, 2024
1 parent 43a1699 commit fbd6e19
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 7 deletions.
11 changes: 10 additions & 1 deletion tools/imglab/src/cluster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,16 @@ int cluster_dataset(
if (assignments[i].c == c)
temp.push_back(images[assignments[i].idx]);
}

#ifdef DLIB_JXL_SUPPORT
if (parser.option("jxl"))
{
string outfile = "cluster_"+pad_int_with_zeros(c+1, 3) + ".jxl";
cout << "Saving " << outfile << endl;
const float jxl_quality = std::stof(parser.option("jxl").argument());
save_jxl(tile_images(temp), outfile, jxl_quality);
}
else
#endif
#ifdef DLIB_WEBP_SUPPORT
if (parser.option("webp"))
{
Expand Down
12 changes: 12 additions & 0 deletions tools/imglab/src/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ std::string to_jpg_name (const std::string& filename)

// ----------------------------------------------------------------------------------------

#ifdef DLIB_JXL_SUPPORT
std::string to_jxl_name (const std::string& filename)
{
std::string::size_type pos = filename.find_last_of(".");
if (pos == std::string::npos)
throw dlib::error("invalid filename: " + filename);
return filename.substr(0,pos) + ".jxl";
}
#endif

// ----------------------------------------------------------------------------------------

#ifdef DLIB_WEBP_SUPPORT
std::string to_webp_name (const std::string& filename)
{
Expand Down
3 changes: 3 additions & 0 deletions tools/imglab/src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ void make_empty_file (

std::string to_png_name (const std::string& filename);
std::string to_jpg_name (const std::string& filename);
#ifdef DLIB_JXL_SUPPORT
std::string to_jxl_name (const std::string& filename);
#endif
#ifdef DLIB_WEBP_SUPPORT
std::string to_webp_name (const std::string& filename);
#endif
Expand Down
8 changes: 8 additions & 0 deletions tools/imglab/src/flip_dataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,14 @@ void flip_dataset(const command_line_parser& parser)
filename = to_jpg_name(filename);
save_jpeg(temp, filename,JPEG_QUALITY);
}
#ifdef DLIB_JXL_SUPPORT
else if (parser.option("jxl"))
{
filename = to_jxl_name(filename);
const float jxl_quality = std::stof(parser.option("jxl").argument());
save_webp(temp, filename, jxl_quality);
}
#endif
#ifdef DLIB_WEBP_SUPPORT
else if (parser.option("webp"))
{
Expand Down
62 changes: 56 additions & 6 deletions tools/imglab/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include <dlib/dir_nav.h>


const char* VERSION = "1.20";
const char* VERSION = "1.21";



Expand Down Expand Up @@ -61,8 +61,8 @@ void create_new_dataset (
// then parser[i] should be a directory

std::vector<file> files = get_files_in_directory_tree(parser[i],
match_endings(".png .PNG .jpeg .JPEG .jpg .JPG .bmp .BMP .dng .DNG .gif .GIF .webp .WEBP"),
depth);
match_endings(".png .PNG .jpeg .JPEG .jpg .JPG .bmp .BMP .dng .DNG .gif .GIF .jxl .JXL .webp .WEBP"),
depth);
sort(files.begin(), files.end());

for (unsigned long j = 0; j < files.size(); ++j)
Expand Down Expand Up @@ -339,6 +339,14 @@ void rotate_dataset(const command_line_parser& parser)
filename = to_jpg_name(filename);
save_jpeg(temp, filename,JPEG_QUALITY);
}
#ifdef DLIB_JXL_SUPPORT
else if (parser.option("jxl"))
{
filename = to_jxl_name(filename);
const float jxl_quality = std::stof(parser.option("jxl").argument());
save_jxl(temp, filename, jxl_quality);
}
#endif
#ifdef DLIB_WEBP_SUPPORT
else if (parser.option("webp"))
{
Expand Down Expand Up @@ -491,6 +499,14 @@ int resample_dataset(const command_line_parser& parser)
dimg.filename = to_jpg_name(dimg.filename);
save_jpeg(chip,dimg.filename, JPEG_QUALITY);
}
#ifdef DLIB_JXL_SUPPORT
else if (parser.option("jxl"))
{
dimg.filename = to_jxl_name(dimg.filename);
const float jxl_quality = std::stof(parser.option("jxl").argument());
save_jxl(chip, dimg.filename, jxl_quality);
}
#endif
#ifdef DLIB_WEBP_SUPPORT
else if (parser.option("webp"))
{
Expand Down Expand Up @@ -524,9 +540,23 @@ int tile_dataset(const command_line_parser& parser)

string out_image = parser.option("tile").argument();
string ext = right_substr(out_image,".");
if (ext != "png" && ext != "jpg" && ext != "webp")
if (ext != "png" && ext != "jpg"
#if DLIB_JXL_SUPPORT
&& ext != "jxl"
#endif
#if DLIB_WEBP_SUPPORT
&& ext != "webp"
#endif
)
{
cerr << "The output image file must have either .png, .jpg or .webp extension." << endl;
cerr << "The output image file must have one of these extensions: .png, .jpg" << endl;
#if DLIB_JXL_SUPPORT
cerr << ", .jxl";
#endif
#if DLIB_WEBP_SUPPORT
cerr << ", .webp";
#endif
cerr << ".";
return EXIT_FAILURE;
}

Expand Down Expand Up @@ -571,6 +601,14 @@ int tile_dataset(const command_line_parser& parser)
{
save_png(tile_images(images), out_image);
}
#ifdef DLIB_JXL_SUPPORT
else if (ext == "jxl")
{
// Lossless by default
const float jxl_quality = get_option(parser, "jxl", 100.f);
save_jxl(tile_images(images), out_image, jxl_quality);
}
#endif
#ifdef DLIB_WEBP_SUPPORT
else if (ext == "webp")
{
Expand Down Expand Up @@ -657,8 +695,11 @@ int main(int argc, char** argv)
parser.add_option("rmignore","Remove all boxes marked ignore and save the results to a new XML file.");
parser.add_option("rm-if-overlaps","Remove all boxes labeled <arg> if they overlap any box not labeled <arg> and save the results to a new XML file.",1);
parser.add_option("jpg", "When saving images to disk, write them as jpg files instead of png.");
#ifdef DLIB_JXL_SUPPORT
parser.add_option("jxl", "When saving images to disk, write them as jxl files instead of png, using <arg> as the quality factor.", 1);
#endif
#ifdef DLIB_WEBP_SUPPORT
parser.add_option("webp", "When saving images to disk, write them as webp files instead of png or jpg, using <arg> as the quality factor.", 1);
parser.add_option("webp", "When saving images to disk, write them as webp files instead of png, using <arg> as the quality factor.", 1);
#endif

parser.set_group_name("Cropping sub images");
Expand Down Expand Up @@ -775,8 +816,14 @@ int main(int argc, char** argv)
parser.check_incompatible_options("rmtrunc", "ignore");
parser.check_incompatible_options("box-images", "rename");
parser.check_incompatible_options("box-images", "ignore");
#ifdef DLIB_JXL_SUPPORT
parser.check_incompatible_options("jpg", "jxl");
#endif
#ifdef DLIB_WEBP_SUPPORT
parser.check_incompatible_options("jpg", "webp");
#endif
#if DLIB_JXL_SUPPORT && DLIB_WEBP_SUPPORT
parser.check_incompatible_options("jxl", "webp");
#endif
const char* convert_args[] = {"pascal-xml","pascal-v1","idl"};
parser.check_option_arg_range("convert", convert_args);
Expand All @@ -787,6 +834,9 @@ int main(int argc, char** argv)
parser.check_option_arg_range("cropped-object-size", 4, 10000*10000);
parser.check_option_arg_range("crop-size", 1.0, 100.0);
parser.check_option_arg_range("split-train-test", 0.0, 1.0);
#ifdef DLIB_JXL_SUPPORT
parser.check_option_arg_range("jxl", 0.f, 100.f);
#endif
#ifdef DLIB_WEBP_SUPPORT
parser.check_option_arg_range("webp", 0.f, std::numeric_limits<float>::max());
#endif
Expand Down

0 comments on commit fbd6e19

Please sign in to comment.