Files
gromandClaude Opus 5 65520a4a58 The map's darkening was a grid of grey boxes, and a 12 px string was emptying the glyph sheet
grom, after a night's play: "make the map look a lot nicer, it would be really nice if the
darkened areas were not just massive squares on the map, and should fit into the actual
edges of the terrian if that makes sense, also I shouldn't see this dark squares on the
fully zoomed out world map, also make it so the world map can't move out of the frame."

**The darkening.** OpenMapClassic washed a never-visited zone through its land mask, and
those masks are Voronoi-dilated by construction — "so that the forty of them tile each
continent with no gap" — so for any zone that is not on a coast the mask *is* the
rectangle. Measured at zoom 2.5 over the Eastern Kingdoms: a grid of flat grey boxes.
Nothing could have been softened into a fix; it was the wrong mask. It was also drawn at
full strength from every zoom, and at the whole-world view the zones' own art is not drawn
at all, so the only thing on the world map was the fog's rectangles.

A land mask for every map now comes from the client's own data. `bake_zone_land` unions a
map's `WorldMapOverlay` alpha — the pieces the game reveals as you explore, cut to the
shape of the ground — into one 256×256 mask; `overlay-land-index.py` writes the list the
addon reads, because Lua cannot test for a file and a *missing* mask is dropped by the
engine so the whole rectangle draws, which is the defect itself. 53 maps have overlays
(the 40 stock zones, Hyjal, Alterac Valley and 11 of the realm's own); 76 have none and
keep a wide feather. `clip-zone-masks-to-coast.py` then takes the open sea off the dilated
sheet masks — the same dilation was painting parchment out on the ocean — by closing the
continent's own land by 500 yards (above the 466-yard inradius of the widest enclosed gap,
so seams fill and the sea does not) and keeping a 300-yard coastal margin. Twelve
landlocked zones lose 0.0%; Darkshore loses 53%, all of it water.

`tga.rs` gains the writer that needed, `script::uimap` and `ui_world_map::build_catalog`
become public so an example can reach the uiMapID graph, and `WorldMapUiData` grows the
accessor so the VM push and an outside caller cannot build different lists.

**The glyph sheet.** His four-minute session reset it 27 times, each dropping every cached
glyph. The producer is Atlas-CFM's cursor coordinate readout: a 12 px FontString on
`WorldMapDetailFrame`, rewritten every frame, and the canvas zooms by scaling that frame —
140 device px a digit at a canvas scale of 6.9. Nothing about the addon is wrong, so the
fix is ours: `raster_ppem` rasterizes exactly up to 64 device px and snaps to a
third-octave ladder above it, with `cell()` scaling the cached bitmap and metrics left
keyed on the true ppem, so advances and `GetStringWidth` are bit-for-bit unchanged. Same
1,200-frame reproduction: 3 resets and 6,623 rasters before, **0 and 1,424** after, with
occupancy climbing once to 30% and stopping. A reset no longer clears the metric cache
either, and `note_loud_cell` names the string that asked for an outsized cell.

**Four more from the same log.** `SetOwner`'s unknown-anchor line named a forwarding
wrapper rather than the addon that chose the anchor (two `debugstack` frames now); the nine
reference anchor names are written down with the binary evidence, because `ANCHOR_TOP` is
not one of them. The `CreateFrame(kind, …, inherits=<other kind>)` warning fired in both
directions and only one drops content — `kind_subsumes` makes it directional, and
`type_chain` moves out of `install` so `IsObjectType` and the loader read one table. A
string that is not UTF-8 now names the Lua line that cut it, and both glyph warnings quote
the string the character came out of. The three world-map CVars stay unregistered, with the
argument written down beside `REGISTERED` so it is not re-litigated from the warning.

**`World.zmp`.** The realm's `WorldMapArea` carries a third `areaID == 0` row, `694 World`,
with an all-zero rect; the reference ships no bitmap for it and neither the graph nor the
realm's own `Overrides.lua` lets it reach a player. The miss is a `debug!` line now, and
the `warn!` is kept for a continent that *has* a rectangle and is missing its bitmap.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SgstFWtn1RxEJ5kVrpgYwn
2026-11-04 21:16:20 -07:00

387 lines
16 KiB
Rust
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! TGA decoding, the other half of the reference's UI texture lookup: `TextureCreate` tries
//! `{".tga", ".blp"}` (`0x835248`), and while 1.12 ships its UI art as BLP, addons ship TGAs. The
//! reference's `0x5a3a30` dispatches on `imageType`: 1 and 9 (colour-mapped, raw or RLE) to
//! `0x5a39d0` through the palette at `0x5a3820`, 2 to `0x5a3c80`, 10 to `0x5a3d70`.
//!
//! Deviation: grayscale types 3 and 11 decode where the reference refuses them, because no shipped
//! or addon file is one and removing working code would buy nothing.
//!
//! [`tga_to_rgba`] checks the header before the pixels, so it can serve as the fallback after a
//! failed `BLP2` sniff: TGA has no magic.
use anyhow::{bail, Result};
/// Decode a TGA to RGBA8 `(width, height, pixels)`, rows top-down like [`crate::blp_to_rgba`].
pub fn tga_to_rgba(bytes: &[u8]) -> Result<(u32, u32, Vec<u8>)> {
let Some(header) = bytes.get(..18) else {
bail!("TGA: truncated header ({} bytes)", bytes.len());
};
let id_length = header[0] as usize;
let color_map_type = header[1];
let image_type = header[2];
let color_map_origin = u16::from_le_bytes([header[3], header[4]]) as usize;
let color_map_len = u16::from_le_bytes([header[5], header[6]]) as usize;
let color_map_entry_bits = header[7] as usize;
let width = u16::from_le_bytes([header[12], header[13]]) as u32;
let height = u16::from_le_bytes([header[14], header[15]]) as u32;
let bpp = header[16];
let descriptor = header[17];
let rle = match image_type {
1..=3 => false,
9..=11 => true,
t => bail!("TGA: unknown image type {t}"),
};
let paletted = matches!(image_type, 1 | 9);
let grayscale = matches!(image_type, 3 | 11);
// A colour-mapped pixel is an index, its palette entry a width of its own (`0x5a3b40`).
let bytes_per_pixel = match (bpp, paletted, grayscale) {
(8, true, _) => 1,
(24, false, false) => 3,
(32, false, false) => 4,
(8, false, true) => 1,
_ => bail!("TGA: unsupported depth {bpp} for image type {image_type}"),
};
if paletted && color_map_type != 1 {
bail!("TGA: image type {image_type} is colour-mapped but declares no colour map");
}
let map_entry_bytes = color_map_entry_bits.div_ceil(8);
if paletted && !matches!(color_map_entry_bits, 15 | 16 | 24 | 32) {
bail!("TGA: unsupported colour-map entry width {color_map_entry_bits}");
}
if width == 0 || height == 0 || width > 8192 || height > 8192 {
bail!("TGA: implausible dimensions {width}x{height}");
}
// The pixels start after the id field and any colour map, which a colour-mapped image reads
// on the way (`0x5a3b40`: `entryBytes * colorMapLength + idLength + 0x12`).
let map_bytes = if color_map_type == 1 {
color_map_len * map_entry_bytes
} else {
0
};
let palette = if paletted {
let Some(p) = bytes.get(18 + id_length..18 + id_length + map_bytes) else {
bail!("TGA: truncated colour map");
};
p
} else {
&[][..]
};
let data_start = 18 + id_length + map_bytes;
let Some(data) = bytes.get(data_start..) else {
bail!("TGA: truncated before pixel data");
};
let pixel_count = (width as usize) * (height as usize);
let mut pixels = Vec::with_capacity(pixel_count * bytes_per_pixel);
if rle {
// An RLE packet's bit 7 marks a run of one pixel, else `count` raw pixels follow; `count`
// is the low 7 bits plus 1.
let mut at = 0usize;
while pixels.len() < pixel_count * bytes_per_pixel {
let Some(&packet) = data.get(at) else {
bail!("TGA: RLE stream truncated");
};
at += 1;
let count = ((packet & 0x7f) as usize) + 1;
if packet & 0x80 != 0 {
let Some(px) = data.get(at..at + bytes_per_pixel) else {
bail!("TGA: RLE run truncated");
};
at += bytes_per_pixel;
for _ in 0..count {
pixels.extend_from_slice(px);
}
} else {
let n = count * bytes_per_pixel;
let Some(px) = data.get(at..at + n) else {
bail!("TGA: RLE raw packet truncated");
};
at += n;
pixels.extend_from_slice(px);
}
}
pixels.truncate(pixel_count * bytes_per_pixel);
} else {
let n = pixel_count * bytes_per_pixel;
let Some(px) = data.get(..n) else {
bail!("TGA: pixel data truncated");
};
pixels.extend_from_slice(px);
}
// BGR(A) or gray → RGBA. Descriptor bit 5 is a top origin, bit 4 a right one; TGA's default
// is bottom-left.
let top_down = descriptor & 0x20 != 0;
let right_first = descriptor & 0x10 != 0;
let mut rgba = vec![0u8; pixel_count * 4];
for y in 0..height as usize {
let src_y = if top_down { y } else { height as usize - 1 - y };
for x in 0..width as usize {
let src_x = if right_first {
width as usize - 1 - x
} else {
x
};
let s = (src_y * width as usize + src_x) * bytes_per_pixel;
let d = (y * width as usize + x) * 4;
if paletted {
// `0x5a3820`: `palette + (index - firstIndex) * entryBytes`, `firstIndex` being
// the header's colour-map origin (almost always 0).
let idx = (pixels[s] as usize).wrapping_sub(color_map_origin);
let e = idx * map_entry_bytes;
let Some(entry) = palette.get(e..e + map_entry_bytes) else {
bail!("TGA: colour index {} is outside the map", pixels[s]);
};
rgba[d..d + 4].copy_from_slice(&match map_entry_bytes {
// 15/16-bit entries are `ARRRRRGG GGGBBBBB` little-endian, the top bit alpha
// only at 16; the high bits replicate down so a full channel reads 0xff.
2 => {
let v = u16::from_le_bytes([entry[0], entry[1]]);
let c = |shift: u32| {
let f = ((v >> shift) & 0x1f) as u8;
(f << 3) | (f >> 2)
};
let a = if color_map_entry_bits == 16 && v & 0x8000 == 0 {
0
} else {
0xff
};
[c(10), c(5), c(0), a]
}
4 => [entry[2], entry[1], entry[0], entry[3]],
_ => [entry[2], entry[1], entry[0], 0xff],
});
continue;
}
match bytes_per_pixel {
1 => {
let g = pixels[s];
rgba[d..d + 4].copy_from_slice(&[g, g, g, 0xff]);
}
3 => {
rgba[d..d + 4].copy_from_slice(&[pixels[s + 2], pixels[s + 1], pixels[s], 0xff])
}
_ => {
rgba[d..d + 4].copy_from_slice(&[
pixels[s + 2],
pixels[s + 1],
pixels[s],
pixels[s + 3],
]);
}
}
}
}
Ok((width, height, rgba))
}
/// The 26-byte TGA 2.0 footer: two zero offsets (no extension area, no developer directory) and
/// the signature. Nothing in this client reads it — [`tga_to_rgba`] stops at the pixels and the
/// reference's `0x5a3a30` never seeks the tail — but the masks already shipped in
/// `OpenMapClassic/Media/Textures` carry it, because Pillow writes it (`scripts/soften-zone-masks.py`),
/// so writing it keeps a baked mask and a softened one the same shape of file.
const FOOTER: &[u8; 26] = b"\x00\x00\x00\x00\x00\x00\x00\x00TRUEVISION-XFILE.\x00";
/// Encode RGBA8 `pixels`, rows **top-down** like [`tga_to_rgba`] returns them, as an uncompressed
/// 32-bit TGA.
///
/// The byte layout is the one the shipped zone masks have, measured off
/// `MapZoneDilate/1429.tga`: `imageType` 2, no colour map, `bpp` 32, descriptor `0x08` — eight
/// alpha bits and TGA's default **bottom-left** origin, so the rows go out reversed. That is what
/// Pillow writes, and the masks this sits beside are Pillow's output, so a re-bake drops into the
/// same folder without the addon seeing two different files.
pub fn rgba_to_tga(width: u32, height: u32, pixels: &[u8]) -> Result<Vec<u8>> {
if width == 0 || height == 0 {
bail!("TGA: cannot write a {width}x{height} image");
}
if width > 0xffff || height > 0xffff {
bail!("TGA: {width}x{height} does not fit the 16-bit header fields");
}
let need = (width as usize) * (height as usize) * 4;
if pixels.len() != need {
bail!(
"TGA: {width}x{height} wants {need} RGBA bytes, got {}",
pixels.len()
);
}
let mut out = Vec::with_capacity(18 + need + FOOTER.len());
out.extend_from_slice(&[0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
out.extend_from_slice(&(width as u16).to_le_bytes());
out.extend_from_slice(&(height as u16).to_le_bytes());
out.push(32);
out.push(0x08);
for y in (0..height as usize).rev() {
for x in 0..width as usize {
let s = (y * width as usize + x) * 4;
// BGRA, the order the decoder above swizzles back.
out.extend_from_slice(&[pixels[s + 2], pixels[s + 1], pixels[s], pixels[s + 3]]);
}
}
out.extend_from_slice(FOOTER);
Ok(out)
}
#[cfg(test)]
mod tests {
use super::*;
/// A minimal colour-mapped TGA: header, palette, index stream.
fn tga_paletted(
image_type: u8,
w: u16,
h: u16,
palette: &[u8],
entry_bits: u8,
idx: &[u8],
) -> Vec<u8> {
let mut out = vec![0u8; 18];
out[1] = 1; // colour-map type: present
out[2] = image_type;
out[5..7]
.copy_from_slice(&((palette.len() / (entry_bits as usize / 8)) as u16).to_le_bytes());
out[7] = entry_bits;
out[12..14].copy_from_slice(&w.to_le_bytes());
out[14..16].copy_from_slice(&h.to_le_bytes());
out[16] = 8; // 8-bit indices
out[17] = 0x20; // top-origin, so the test reads in file order
out.extend_from_slice(palette);
out.extend_from_slice(idx);
out
}
/// Types 1 and 9, which the reference sends to `0x5a39d0`, in the addons' shape: 8-bit
/// indices into a 24-bit palette.
#[test]
fn colour_mapped_images_decode_through_the_palette() {
// Palette as BGR: entry 0 red, entry 1 green, entry 2 blue.
let pal = [
0, 0, 255, /* red */ 0, 255, 0, /* green */ 255, 0, 0, /* blue */
];
let raw = tga_paletted(1, 2, 2, &pal, 24, &[0, 1, 2, 0]);
let (w, h, rgba) = tga_to_rgba(&raw).unwrap();
assert_eq!((w, h), (2, 2));
assert_eq!(
rgba,
vec![
255, 0, 0, 255, 0, 255, 0, 255, //
0, 0, 255, 255, 255, 0, 0, 255,
]
);
// Type 9 is the same image RLE'd; the runs are over index bytes, not expanded pixels.
let rle = tga_paletted(9, 2, 2, &pal, 24, &[0x02, 0, 1, 2, 0x80, 0]);
assert_eq!(tga_to_rgba(&rle).unwrap().2, rgba);
// An index outside the map is an error, not a black pixel.
let bad = tga_paletted(1, 1, 1, &pal, 24, &[9]);
assert!(tga_to_rgba(&bad)
.unwrap_err()
.to_string()
.contains("outside the map"));
}
/// A minimal TGA: an 18-byte header and pixels.
fn tga(image_type: u8, w: u16, h: u16, bpp: u8, descriptor: u8, data: &[u8]) -> Vec<u8> {
let mut out = vec![0u8; 18];
out[2] = image_type;
out[12..14].copy_from_slice(&w.to_le_bytes());
out[14..16].copy_from_slice(&h.to_le_bytes());
out[16] = bpp;
out[17] = descriptor;
out.extend_from_slice(data);
out
}
#[test]
fn uncompressed_32bit_bottom_origin_flips_and_swizzles() {
// Bottom-up rows, as BGRA: blue, green at the bottom; red, half-alpha white on top.
let px = [
255, 0, 0, 255, /* blue */ 0, 255, 0, 255, /* green */
0, 0, 255, 255, /* red */ 255, 255, 255, 128, /* white a=128 */
];
let (w, h, rgba) = tga_to_rgba(&tga(2, 2, 2, 32, 0, &px)).unwrap();
assert_eq!((w, h), (2, 2));
// Top-down output, as RGBA: red, white, then blue, green.
assert_eq!(
rgba,
vec![
255, 0, 0, 255, 255, 255, 255, 128, //
0, 0, 255, 255, 0, 255, 0, 255,
]
);
}
#[test]
fn uncompressed_24bit_top_origin_gets_opaque_alpha() {
let px = [255, 0, 0, /* blue */ 0, 0, 255 /* red */];
let (w, h, rgba) = tga_to_rgba(&tga(2, 2, 1, 24, 0x20, &px)).unwrap();
assert_eq!((w, h), (2, 1));
assert_eq!(rgba, vec![0, 0, 255, 255, 255, 0, 0, 255]);
}
#[test]
fn rle_run_and_raw_packets_expand() {
// 4×1 top-origin: a run of 3 red and a raw packet of 1 green (BGR, 24-bit).
let data = [0x82, 0, 0, 255, 0x00, 0, 255, 0];
let (w, _, rgba) = tga_to_rgba(&tga(10, 4, 1, 24, 0x20, &data)).unwrap();
assert_eq!(w, 4);
assert_eq!(
rgba,
vec![
255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, //
0, 255, 0, 255,
]
);
}
#[test]
fn grayscale_replicates_channels() {
let (_, _, rgba) = tga_to_rgba(&tga(3, 1, 1, 8, 0x20, &[7])).unwrap();
assert_eq!(rgba, vec![7, 7, 7, 255]);
}
#[test]
fn garbage_and_unsupported_types_are_loud_misses() {
assert!(tga_to_rgba(b"BLP2xxxxxxxxxxxxxxxxxx").is_err()); // a BLP is not a TGA
assert!(tga_to_rgba(&[0u8; 4]).is_err()); // truncated header
assert!(tga_to_rgba(&tga(1, 2, 2, 8, 0, &[0; 32])).is_err()); // colour-mapped, no map
assert!(tga_to_rgba(&tga(2, 2, 2, 16, 0, &[0; 32])).is_err()); // 16-bit
assert!(tga_to_rgba(&tga(2, 2, 2, 32, 0, &[0; 4])).is_err()); // truncated pixels
}
/// The writer's bytes are the shape the shipped masks have, and the decoder above reads back
/// exactly what was written — the pair that lets a baked mask be trusted without opening it in
/// another program.
#[test]
fn written_pixels_come_back_unchanged_through_the_decoder() {
// Asymmetric in both axes and in alpha, so a flipped row, a flipped column or a dropped
// alpha all fail rather than one of the three.
let w = 3u32;
let h = 2u32;
let src: Vec<u8> = vec![
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, //
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
];
let file = rgba_to_tga(w, h, &src).unwrap();
// Header: uncompressed true-colour, 32-bit, eight alpha bits and a bottom-left origin.
assert_eq!(file[2], 2);
assert_eq!(file[16], 32);
assert_eq!(file[17], 0x08);
assert_eq!(file.len(), 18 + (w * h * 4) as usize + 26);
assert_eq!(&file[file.len() - 18..], b"TRUEVISION-XFILE.\x00");
let (gw, gh, back) = tga_to_rgba(&file).unwrap();
assert_eq!((gw, gh), (w, h));
assert_eq!(back, src);
}
#[test]
fn a_pixel_count_that_does_not_match_the_header_is_refused() {
assert!(rgba_to_tga(2, 2, &[0u8; 12]).is_err());
assert!(rgba_to_tga(0, 4, &[]).is_err());
}
}