Kelsidavis-WoWee/tools/editor/cli_png_emit.hpp
Kelsi d073f6f608 refactor(editor): extract savePngOrError helper
58 procedural-texture handlers in cli_gen_texture.cpp ended
with the same 6-line stbi_write_png + error-handling block:

    if (!stbi_write_png(outPath.c_str(), W, H, 3,
                        pixels.data(), W * 3)) {
        std::fprintf(stderr,
            "gen-texture-foo: stbi_write_png failed for %s\n",
            outPath.c_str());
        return 1;
    }

Hoist into cli_png_emit.hpp as inline savePngOrError, used as
`if (!savePngOrError(outPath, W, H, pixels, "cmd")) return 1;`.

cli_gen_texture.cpp drops by ~118 lines. Output bytes verified
identical via diff: regenerating moss.png with same args
produces byte-for-byte the same file.

This is the texture-side counterpart to saveWomOrError in
cli_box_emitter.hpp — same write-check-fprintf pattern, same
1-line API.
2026-05-09 11:59:37 -07:00

36 lines
1.1 KiB
C++

#pragma once
#include "stb_image_write.h"
#include <cstdint>
#include <cstdio>
#include <string>
#include <vector>
namespace wowee {
namespace editor {
namespace cli {
// Shared PNG-write wrapper used by every --gen-texture-* handler.
// Calls stbi_write_png with the standard RGB/24-bit/W*3-stride
// arguments and reports a stderr message on failure. Returns true
// on success so the caller can do
// `if (!savePngOrError(...)) return 1;`.
//
// 58 sites in cli_gen_texture.cpp open-coded the same 5-line
// "if write fails, fprintf stderr and return 1" block before
// extraction.
inline bool savePngOrError(const std::string& outPath, int W, int H,
const std::vector<uint8_t>& pixels,
const char* cmdName) {
if (stbi_write_png(outPath.c_str(), W, H, 3,
pixels.data(), W * 3)) {
return true;
}
std::fprintf(stderr, "%s: stbi_write_png failed for %s\n",
cmdName, outPath.c_str());
return false;
}
} // namespace cli
} // namespace editor
} // namespace wowee