Skip to content

Improve TFD decoder and add BC1 support#3

Open
StakensJ wants to merge 3 commits intoTestfrom
codex-assisted/improve-mdl-viewer-with-sokol-renderer-tg05gb
Open

Improve TFD decoder and add BC1 support#3
StakensJ wants to merge 3 commits intoTestfrom
codex-assisted/improve-mdl-viewer-with-sokol-renderer-tg05gb

Conversation

@StakensJ
Copy link
Copy Markdown
Owner

@StakensJ StakensJ commented Sep 6, 2025

Summary

  • infer texture dimensions and format from TFD length, with TFH hints as fallback
  • support BC1 decoding and guard against tile-compressed TFD streams

Testing

  • cargo check

https://chatgpt.com/codex/tasks/task_e_68bc3ce187408320a6f3afb32b97fd14

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codex Review: Here are some suggestions.

Reply with @codex fix comments to fix any unresolved comments.

About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you open a pull request for review, mark a draft as ready, or comment "@codex review". If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex fix this CI failure" or "@codex address that feedback".

Comment thread main/src/tfd.rs
Comment on lines +92 to +109
fn decode_bc3_top_mip_to_rgba(src: &[u8], w: usize, h: usize) -> Result<Vec<u8>> {
let bw = (w + 3) / 4;
let bh = (h + 3) / 4;
let mut rgba = vec![0u8; w * h * 4];
let pitch = w * 4;
let bpb = 16usize;
for y in 0..bh {
for x in 0..bw {
let off = (y * bw + x) * bpb;
let block = &src[off..off + bpb];
let mut tmp = [0u8; 4 * 4 * 4];
bcdec_rs::bc3(block, &mut tmp, 4 * 4);
for row in 0..4 {
let dst = (y * 4 + row) * pitch + x * 4 * 4;
let src_row = row * 4 * 4;
rgba[dst..dst + 4 * 4]
.copy_from_slice(&tmp[src_row..src_row + 4 * 4]);
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P1] Handle partial BC3 blocks to avoid slice overflow

decode_bc3_top_mip_to_rgba writes a full 4×4 block into rgba for every block index without checking whether the block overlaps the image edge. For textures whose width or height is not divisible by four (valid BC3 data is typically padded but may represent, e.g., 100×50 UI textures), the computed range rgba[dst..dst + 16] exceeds the w * h * 4 buffer, causing a panic when the last partial block is processed. The BC1 decoder below guards each pixel with if x < w && y < h; BC3 needs similar bounds checks or cropping before copying rows.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant