Use concrete types instead of auto when convenient and not redundant (#1757)

This commit is contained in:
Rangi
2025-07-17 14:59:51 -04:00
committed by GitHub
parent 9dddd87893
commit 0c96234532
16 changed files with 67 additions and 68 deletions

View File

@@ -692,7 +692,7 @@ static void verboseOutputConfig() {
fputs("\t[\n", stderr);
for (auto const &pal : options.palSpec) {
fputs("\t\t", stderr);
for (auto &color : pal) {
for (auto const &color : pal) {
if (color) {
fprintf(stderr, "#%06x, ", color->toCSS() >> 8);
} else {
@@ -757,14 +757,14 @@ int main(int argc, char *argv[]) {
// We need to allocate a new arg pool for each at-file, so as not to invalidate pointers
// previous at-files may have generated to their own arg pools.
// But for the same reason, the arg pool must also outlive the at-file's stack entry!
auto &argPool = argPools.emplace_back();
std::vector<char> &argPool = argPools.emplace_back();
// Copy `argv[0]` for error reporting, and because option parsing skips it
AtFileStackEntry &stackEntry =
atFileStack.emplace_back(musl_optind, std::vector{atFileName});
// It would be nice to compute the char pointers on the fly, but reallocs don't allow
// that; so we must compute the offsets after the pool is fixed
auto offsets = readAtFile(&musl_optarg[1], argPool);
std::vector<size_t> offsets = readAtFile(&musl_optarg[1], argPool);
stackEntry.argv.reserve(offsets.size() + 2); // Avoid a bunch of reallocs
for (size_t ofs : offsets) {
stackEntry.argv.push_back(&argPool.data()[ofs]);
@@ -797,7 +797,7 @@ int main(int argc, char *argv[]) {
curArgc = argc;
curArgv = argv;
} else {
auto &vec = atFileStack.back().argv;
std::vector<char *> &vec = atFileStack.back().argv;
curArgc = vec.size();
curArgv = vec.data();
}
@@ -816,7 +816,7 @@ int main(int argc, char *argv[]) {
auto autoOutPath = [](bool autoOptEnabled, std::string &path, char const *extension) {
if (autoOptEnabled) {
auto &image = localOptions.groupOutputs ? options.output : options.input;
std::string &image = localOptions.groupOutputs ? options.output : options.input;
if (image.empty()) {
fatalWithUsage(
"No %s specified",

View File

@@ -178,7 +178,7 @@ public:
// Returns the number of distinct colors
size_t volume() const { return uniqueColors().size(); }
bool canFit(ProtoPalette const &protoPal) const {
auto &colors = uniqueColors();
std::unordered_set<uint16_t> &colors = uniqueColors();
colors.insert(RANGE(protoPal));
return colors.size() <= options.maxOpaqueColors();
}
@@ -206,7 +206,8 @@ public:
uint32_t relSize = 0;
for (uint16_t color : protoPal) {
auto multiplicity = // How many of our proto-palettes does this color also belong to?
// How many of our proto-palettes does this color also belong to?
uint32_t multiplicity =
std::count_if(RANGE(*this), [this, &color](ProtoPalAttrs const &attrs) {
ProtoPalette const &pal = (*_protoPals)[attrs.protoPalIndex];
return std::find(RANGE(pal), color) != pal.end();
@@ -225,21 +226,36 @@ public:
// Computes the "relative size" of a set of proto-palettes on this palette
template<typename Iter>
auto combinedVolume(Iter &&begin, Iter const &end, std::vector<ProtoPalette> const &protoPals)
size_t combinedVolume(Iter &&begin, Iter const &end, std::vector<ProtoPalette> const &protoPals)
const {
auto &colors = uniqueColors();
std::unordered_set<uint16_t> &colors = uniqueColors();
addUniqueColors(colors, std::forward<Iter>(begin), end, protoPals);
return colors.size();
}
// Computes the "relative size" of a set of colors on this palette
template<typename Iter>
auto combinedVolume(Iter &&begin, Iter &&end) const {
auto &colors = uniqueColors();
size_t combinedVolume(Iter &&begin, Iter &&end) const {
std::unordered_set<uint16_t> &colors = uniqueColors();
colors.insert(std::forward<Iter>(begin), std::forward<Iter>(end));
return colors.size();
}
};
static void verboseOutputAssignments(
std::vector<AssignedProtos> const &assignments, std::vector<ProtoPalette> const &protoPalettes
) {
for (AssignedProtos const &assignment : assignments) {
fputs("{ ", stderr);
for (ProtoPalAttrs const &attrs : assignment) {
fprintf(stderr, "[%zu] ", attrs.protoPalIndex);
for (uint16_t colorIndex : protoPalettes[attrs.protoPalIndex]) {
fprintf(stderr, "%04" PRIx16 ", ", colorIndex);
}
}
fprintf(stderr, "} (volume = %zu)\n", assignment.volume());
}
}
static void decant(
std::vector<AssignedProtos> &assignments, std::vector<ProtoPalette> const &protoPalettes
) {
@@ -423,7 +439,7 @@ std::tuple<std::vector<size_t>, size_t>
attrs.protoPalIndex,
bestPalIndex
);
auto &bestPal = assignments[bestPalIndex];
AssignedProtos &bestPal = assignments[bestPalIndex];
// Add the color to that palette
bestPal.assign(std::move(attrs));
@@ -550,16 +566,7 @@ std::tuple<std::vector<size_t>, size_t>
// LCOV_EXCL_START
if (options.verbosity >= Options::VERB_INTERM) {
for (auto &&assignment : assignments) {
fputs("{ ", stderr);
for (auto &&attrs : assignment) {
fprintf(stderr, "[%zu] ", attrs.protoPalIndex);
for (auto &&colorIndex : protoPalettes[attrs.protoPalIndex]) {
fprintf(stderr, "%04" PRIx16 ", ", colorIndex);
}
}
fprintf(stderr, "} (volume = %zu)\n", assignment.volume());
}
verboseOutputAssignments(assignments, protoPalettes);
}
// LCOV_EXCL_STOP
@@ -569,16 +576,7 @@ std::tuple<std::vector<size_t>, size_t>
// LCOV_EXCL_START
if (options.verbosity >= Options::VERB_INTERM) {
for (auto &&assignment : assignments) {
fputs("{ ", stderr);
for (auto &&attrs : assignment) {
fprintf(stderr, "[%zu] ", attrs.protoPalIndex);
for (auto &&colorIndex : protoPalettes[attrs.protoPalIndex]) {
fprintf(stderr, "%04" PRIx16 ", ", colorIndex);
}
}
fprintf(stderr, "} (volume = %zu)\n", assignment.volume());
}
verboseOutputAssignments(assignments, protoPalettes);
}
// LCOV_EXCL_STOP

View File

@@ -18,7 +18,7 @@ void sortIndexed(
options.verbosePrint(Options::VERB_LOG_ACT, "Sorting palettes using embedded palette...\n");
auto pngToRgb = [&palRGB, &palAlphaSize, &palAlpha](int index) {
auto const &c = palRGB[index];
png_color const &c = palRGB[index];
return Rgba(
c.red, c.green, c.blue, palAlpha && index < palAlphaSize ? palAlpha[index] : 0xFF
);

View File

@@ -26,6 +26,8 @@
using namespace std::string_view_literals;
static char const *hexDigits = "0123456789ABCDEFabcdef";
template<typename Str> // Should be std::string or std::string_view
static void skipWhitespace(Str const &str, size_t &pos) {
pos = std::min(str.find_first_not_of(" \t"sv, pos), str.length());
@@ -70,10 +72,10 @@ void parseInlinePalSpec(char const * const rawArg) {
" ",
rawArg
);
for (auto i = ofs; i; --i) {
for (size_t i = ofs; i; --i) {
putc(' ', stderr);
}
for (auto i = len; i; --i) {
for (size_t i = len; i; --i) {
putc('^', stderr);
}
putc('\n', stderr);
@@ -93,7 +95,7 @@ void parseInlinePalSpec(char const * const rawArg) {
color = {};
n += literal_strlen("none");
} else {
auto pos = std::min(arg.find_first_not_of("0123456789ABCDEFabcdef"sv, n), arg.length());
size_t pos = std::min(arg.find_first_not_of(hexDigits, n), arg.length());
switch (pos - n) {
case 3:
color = Rgba(
@@ -196,7 +198,7 @@ static T readLE(U const *bytes) {
static bool readLine(std::filebuf &file, std::string &buffer) {
assume(buffer.empty());
for (;;) {
auto c = file.sbumpc();
int c = file.sbumpc();
if (c == std::filebuf::traits_type::eof()) {
return !buffer.empty();
}
@@ -396,8 +398,7 @@ static void parseHEXFile(std::filebuf &file) {
continue;
}
if (line.length() != 6
|| line.find_first_not_of("0123456789ABCDEFabcdef"sv) != std::string::npos) {
if (line.length() != 6 || line.find_first_not_of(hexDigits) != std::string::npos) {
error(
"Failed to parse color #%d (\"%s\"): invalid \"rrggbb\" line",
nbColors + 1,
@@ -432,7 +433,7 @@ static void parseACTFile(std::filebuf &file) {
// https://www.adobe.com/devnet-apps/photoshop/fileformatashtml/#50577411_pgfId-1070626
std::array<char, 772> buf{};
auto len = file.sgetn(buf.data(), buf.size());
size_t len = file.sgetn(buf.data(), buf.size());
uint16_t nbColors = 256;
if (len == 772) {
@@ -555,8 +556,7 @@ static void parseGBCFile(std::filebuf &file) {
for (;;) {
char buf[2 * 4];
auto len = file.sgetn(buf, sizeof(buf));
if (len == 0) {
if (size_t len = file.sgetn(buf, sizeof(buf)); len == 0) {
break;
} else if (len != sizeof(buf)) {
error(
@@ -625,8 +625,7 @@ void parseDmgPalSpec(char const * const rawArg) {
std::string_view arg(rawArg);
if (arg.length() != 2
|| arg.find_first_not_of("0123456789ABCDEFabcdef"sv) != std::string_view::npos) {
if (arg.length() != 2 || arg.find_first_not_of(hexDigits) != std::string_view::npos) {
error("Unknown DMG palette specification \"%s\"", rawArg);
return;
}
@@ -660,7 +659,7 @@ void parseBackgroundPalSpec(char const *arg) {
return;
}
size_t size = strspn(&arg[1], "0123456789ABCDEFabcdef");
size_t size = strspn(&arg[1], hexDigits);
switch (size) {
case 3:
options.bgColor = Rgba(singleToHex(arg[1]), singleToHex(arg[2]), singleToHex(arg[3]), 0xFF);

View File

@@ -68,12 +68,12 @@ static void pngWarning(png_structp png, char const *msg) {
}
static void writePng(png_structp png, png_bytep data, size_t length) {
auto &pngFile = *static_cast<File *>(png_get_io_ptr(png));
File &pngFile = *static_cast<File *>(png_get_io_ptr(png));
pngFile->sputn(reinterpret_cast<char *>(data), length);
}
static void flushPng(png_structp png) {
auto &pngFile = *static_cast<File *>(png_get_io_ptr(png));
File &pngFile = *static_cast<File *>(png_get_io_ptr(png));
pngFile->pubsync();
}
@@ -128,7 +128,7 @@ void reverse() {
}
options.verbosePrint(Options::VERB_LOG_ACT, "Reading tiles...\n");
auto const tiles = readInto(options.output);
std::vector<uint8_t> const tiles = readInto(options.output);
uint8_t tileSize = 8 * options.bitDepth;
if (tiles.size() % tileSize != 0) {
fatal(