implement quiet mode and detailed image load diagnostics

- Add -q/--quiet flag to suppress terminal logs and swatches
- Use stbi_failure_reason() to print detailed decode errors
- Update JSON image path resolution to handle CLI flags correctly
This commit is contained in:
2026-08-08 12:12:06 +03:00
parent 93f5cd0ba4
commit f42059fc61
+37 -20
View File
@@ -24,30 +24,38 @@ struct Bucket {
};
int main(int argc, char *argv[]) {
for (int i = 0; i <= argc - 1; i++) {
bool quiet = false;
std::string imagefile;
for (int i = 1; i < argc; i++) {
std::string arg = argv[i];
if (arg == "--help" || arg == "-h") {
cout <<
"ccolors - extract color palette from images\n\n"
"Usage:\n"
" " << argv[0] << " <image>\n\n"
" " << argv[0] << " <image> [options]\n\n"
"Options:\n"
" -h, --help Show this help page\n\n"
" -h, --help Show this help page\n"
" -q, --quiet Suppress terminal output (JSON is still written)\n\n"
"Output:\n"
" Prints dominant colors as HEX and terminal swatches\n"
" Also writes palette.json\n";
return 0;
} else if (arg == "--quiet" || arg == "-q") {
quiet = true;
} else if (imagefile.empty()) {
imagefile = arg;
} else {
cout << "Unknown argument: " << arg << "\n";
return 1;
}
}
if (argc != 2) {
if (imagefile.empty()) {
cout << "Please specify a image file. " << argv[0] << " imagefile.jpg"
<< "\nTry \"" << argv[0] << " --help\" for more information.";
return 1;
}
std::string imagefile = argv[1];
if (!fs::is_regular_file(imagefile)) {
std::cout << "File not found: " << imagefile << "\n";
return 1;
@@ -70,9 +78,9 @@ int main(int argc, char *argv[]) {
const unsigned char* header = buffer.data();
if (is_png(header)) {
cout << "PNG file detected!\n";
if (!quiet) cout << "PNG file detected!\n";
} else if (is_jpeg(header)) {
cout << "JPEG file detected!\n";
if (!quiet) cout << "JPEG file detected!\n";
} else {
cout << "Unknown image file format!\n";
return 1;
@@ -90,17 +98,24 @@ int main(int argc, char *argv[]) {
);
if (!data) {
cout << "Failed to load image!\n";
const char* reason = stbi_failure_reason();
cout << "Failed to load image";
if (reason != nullptr) {
cout << ": " << reason;
}
cout << "\n";
return 1;
}
cout << "**********\n";
cout << "Loaded image:\n";
cout << "Width: " << width << "\n";
cout << "Height: " << height << "\n";
cout << "Channels: " << channels << "\n";
cout << "**********\n";
if (!quiet) {
cout << "**********\n";
cout << "Loaded image:\n";
cout << "Width: " << width << "\n";
cout << "Height: " << height << "\n";
cout << "Channels: " << channels << "\n";
cout << "**********\n";
}
std::unordered_map<int, Bucket> hist;
@@ -137,7 +152,7 @@ int main(int argc, char *argv[]) {
}
}
cout << "Sampled pixels: " << sampled << "\n";
if (!quiet) cout << "Sampled pixels: " << sampled << "\n";
std::vector<Bucket> buckets;
@@ -155,7 +170,7 @@ int main(int argc, char *argv[]) {
std::ofstream json("palette.json");
json << "{\n";
json << " \"image\": \"" << argv[1] << "\",\n";
json << " \"image\": \"" << imagefile << "\",\n";
json << " \"palette\": [\n";
for (int i = 0; i < paletteSize; i++) {
@@ -164,9 +179,11 @@ int main(int argc, char *argv[]) {
int g = b.g / b.count;
int bcol = b.b / b.count;
printf("\033[48;2;%d;%d;%dm \033[0m #%02X%02X%02X\n",
r, g, bcol,
r, g, bcol);
if (!quiet) {
printf("\033[48;2;%d;%d;%dm \033[0m #%02X%02X%02X\n",
r, g, bcol,
r, g, bcol);
}
json << " { "
<< "\"r\": " << r << ", "