Skip to content

App Icon Generator — C++ source

Turn one image into the full web app icon set — favicon, apple-touch-icon and manifest sizes as crisp PNGs. Letterboxed contain-fit, optional white background — runs entirely in your browser.

This is the C++ implementation — the same logic the interactive tool runs, in a shareable, citable form.

// App Icon Generator — contain-fit, centered export geometry for the
// 16/32/180/192/512 icon set (C++17, stdlib only). Port of
// src/tool-sources/app-icon-generator/typescript.ts (the authored canvas snippet).
#include <algorithm>
#include <cmath>
#include <cstddef>
#include <vector>

namespace icon_gen {

constexpr int kDefaultSizes[] = {16, 32, 180, 192, 512};

struct IconRect {
    int size;  // canvas edge (px x px) this rect was fitted into
    int x, y;  // top-left offset: centers the fitted image
    int w, h;  // fitted image size, at least 1x1
};

// The deterministic half of the tool: per export size, the contain-fit rect
// (scale = min(px/w, px/h), clamped to >= 1) plus the centered draw offset —
// the geometry the TS pipeline feeds ctx.drawImage before canvas.toBlob
// PNG-encodes each square. std::lround matches JS Math.round (half away from
// zero). Decoding the source bitmap and PNG encoding stay with the host.
std::vector<IconRect> icon_set_plan(int src_w, int src_h,
                                    const std::vector<int>& sizes =
                                        std::vector<int>(std::begin(kDefaultSizes),
                                                         std::end(kDefaultSizes))) {
    std::vector<IconRect> plan;
    plan.reserve(sizes.size());
    for (int px : sizes) {
        const double scale = std::min(px / double(src_w), px / double(src_h));
        const int w = std::max(1, int(std::lround(src_w * scale)));
        const int h = std::max(1, int(std::lround(src_h * scale)));
        plan.push_back(IconRect{
            px,
            int(std::lround((px - w) / 2.0)),  // letterbox on transparency
            int(std::lround((px - h) / 2.0)),
            w,
            h,
        });
    }
    return plan;
}

}  // namespace icon_gen

Also available in 8 other languages

Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →