Files
Muse 7ba66af59d Restructure: move XFrame tree under widgets/ (combined IDE, part 1)
CWidgets library (components, core, palette, layout, common, icons,
tools, test-results, test-driver, tests, screenshots) moves under
widgets/ so the repo root can hold the combined IDE: designer/ +
widgets/ + docs/. Pure git mv, no content changes.
2026-11-25 15:45:12 -05:00

102 lines
3.5 KiB
C++

// common/ico.h — minimal BMP-format ICO decoder + XImage loader.
// The icons generated by tools/gen_icons.py are 16x16, 32bpp BMP ICOs
// (uncompressed), so the palette needs no image library to draw them.
#pragma once
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
namespace cw {
// Decode a 16x16 BMP-format ICO file into RGBA pixels (row-major, top-down).
// Returns false on any parse problem.
inline bool load_ico_rgba(const std::string& path,
std::vector<uint8_t>& rgba, int& w, int& h) {
FILE* f = fopen(path.c_str(), "rb");
if (!f) return false;
fseek(f, 0, SEEK_END);
long len = ftell(f);
fseek(f, 0, SEEK_SET);
std::vector<uint8_t> data((size_t)len);
if (len < 6 || fread(data.data(), 1, (size_t)len, f) != (size_t)len) {
fclose(f);
return false;
}
fclose(f);
auto u16 = [&](size_t o) -> uint16_t {
return (uint16_t)(data[o] | (data[o + 1] << 8));
};
auto u32 = [&](size_t o) -> uint32_t {
return (uint32_t)data[o] | ((uint32_t)data[o + 1] << 8) |
((uint32_t)data[o + 2] << 16) | ((uint32_t)data[o + 3] << 24);
};
if (len < 22 || data[0] != 0 || data[1] != 0 || u16(2) != 1 || u16(4) != 1)
return false;
int iw = data[6] ? data[6] : 256;
int ih = data[7] ? data[7] : 256;
// ICONDIRENTRY: wPlanes @10, wBitCount @12, dwBytesInRes @14,
// dwImageOffset @18.
int bpp = u16(12);
uint32_t size = u32(14);
uint32_t off = u32(18);
if (iw != 16 || ih != 16 || (bpp != 24 && bpp != 32)) return false;
if (off + 40 > data.size() || off + size > data.size()) return false;
uint32_t hdr = u32(off);
int32_t bw = (int32_t)u32(off + 4);
int32_t bh = (int32_t)u32(off + 8);
if (hdr != 40 || bw != 16 || bh != 32) return false; // XOR+AND masks
int bytes = bpp / 8;
const uint8_t* px = data.data() + off + 40;
rgba.assign(16 * 16 * 4, 255);
for (int y = 0; y < 16; ++y) { // BMP rows are bottom-up
for (int x = 0; x < 16; ++x) {
const uint8_t* p = px + ((15 - y) * 16 + x) * bytes;
uint8_t* o = &rgba[(y * 16 + x) * 4];
o[0] = p[2];
o[1] = p[1];
o[2] = p[0];
o[3] = (bytes == 4) ? p[3] : 255;
}
}
w = 16;
h = 16;
return true;
}
// Upload RGBA pixels as an XImage on the given drawable's depth.
// Opaque icons (alpha 255) — the generator draws on a white background.
inline XImage* rgba_to_ximage(Display* d, Visual* vis, int depth,
const std::vector<uint8_t>& rgba, int w, int h) {
// 24-bit visuals take 32-bit padded XImages.
char* buf = new char[(size_t)w * h * 4]();
for (int i = 0; i < w * h; ++i) {
uint32_t* p = (uint32_t*)(buf + i * 4);
*p = ((uint32_t)rgba[i * 4 + 0] << 16) |
((uint32_t)rgba[i * 4 + 1] << 8) |
((uint32_t)rgba[i * 4 + 2]);
}
XImage* img = XCreateImage(d, vis, (unsigned)depth, ZPixmap, 0, buf,
(unsigned)w, (unsigned)h, 32, 0);
if (!img) delete[] buf;
return img;
}
inline unsigned long alloc_color(Display* d, int screen, int r, int g, int b) {
XColor c;
c.red = (unsigned short)(r * 257);
c.green = (unsigned short)(g * 257);
c.blue = (unsigned short)(b * 257);
c.flags = DoRed | DoGreen | DoBlue;
if (!XAllocColor(d, DefaultColormap(d, screen), &c))
return BlackPixel(d, screen);
return c.pixel;
}
} // namespace cw