oxihuman-export 0.1.2

Export pipeline for OxiHuman — glTF, COLLADA, STL, and streaming formats
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
// Copyright (C) 2026 COOLJAPAN OU (Team KitaSan)
// SPDX-License-Identifier: Apache-2.0

#![allow(dead_code)]

use std::fs;
use std::io::Read;
use std::path::Path;

/// A simple RGBA image buffer for TGA export.
pub struct TgaImage {
    pub pixels: Vec<u8>, // raw RGBA bytes (4 bytes per pixel)
    pub width: usize,
    pub height: usize,
}

impl TgaImage {
    /// Create a new image filled with a solid color.
    pub fn new(width: usize, height: usize, fill: [u8; 4]) -> Self {
        let mut pixels = Vec::with_capacity(width * height * 4);
        for _ in 0..(width * height) {
            pixels.extend_from_slice(&fill);
        }
        Self {
            pixels,
            width,
            height,
        }
    }

    /// Build from float RGB pixels (values in [0.0, 1.0]).
    pub fn from_rgb_f32(pixels: &[[f32; 3]], width: usize, height: usize) -> Self {
        let mut rgba = Vec::with_capacity(width * height * 4);
        for p in pixels {
            let r = (p[0] * 255.0).clamp(0.0, 255.0) as u8;
            let g = (p[1] * 255.0).clamp(0.0, 255.0) as u8;
            let b = (p[2] * 255.0).clamp(0.0, 255.0) as u8;
            rgba.extend_from_slice(&[r, g, b, 255]);
        }
        Self {
            pixels: rgba,
            width,
            height,
        }
    }

    /// Build from float RGBA pixels (values in [0.0, 1.0]).
    pub fn from_rgba_f32(pixels: &[[f32; 4]], width: usize, height: usize) -> Self {
        let mut rgba = Vec::with_capacity(width * height * 4);
        for p in pixels {
            let r = (p[0] * 255.0).clamp(0.0, 255.0) as u8;
            let g = (p[1] * 255.0).clamp(0.0, 255.0) as u8;
            let b = (p[2] * 255.0).clamp(0.0, 255.0) as u8;
            let a = (p[3] * 255.0).clamp(0.0, 255.0) as u8;
            rgba.extend_from_slice(&[r, g, b, a]);
        }
        Self {
            pixels: rgba,
            width,
            height,
        }
    }

    /// Get a pixel at (x, y).
    pub fn get_pixel(&self, x: usize, y: usize) -> [u8; 4] {
        let i = 4 * (y * self.width + x);
        [
            self.pixels[i],
            self.pixels[i + 1],
            self.pixels[i + 2],
            self.pixels[i + 3],
        ]
    }

    /// Set a pixel at (x, y).
    pub fn set_pixel(&mut self, x: usize, y: usize, rgba: [u8; 4]) {
        let i = 4 * (y * self.width + x);
        self.pixels[i..i + 4].copy_from_slice(&rgba);
    }

    /// Return total number of pixels.
    pub fn pixel_count(&self) -> usize {
        self.width * self.height
    }

    /// Convert to TGA bytes (RGB 24-bit, bottom-left origin).
    pub fn to_tga_bytes_rgb(&self) -> Vec<u8> {
        let w = self.width as u16;
        let h = self.height as u16;
        let mut out = Vec::with_capacity(18 + self.width * self.height * 3);

        // 18-byte header
        out.extend_from_slice(&[
            0, // [0]  ID length
            0, // [1]  color map type
            2, // [2]  image type: uncompressed true-color
            0, 0, // [3-4]  color map origin
            0, 0, // [5-6]  color map length
            0, // [7]  color map entry size
            0, 0, // [8-9]  x origin
            0, 0, // [10-11] y origin
        ]);
        out.extend_from_slice(&w.to_le_bytes()); // [12-13] width
        out.extend_from_slice(&h.to_le_bytes()); // [14-15] height
        out.push(24); // [16] bits per pixel
        out.push(0); // [17] image descriptor: bottom-left origin

        // Pixel data — bottom row first (TGA default)
        for row in (0..self.height).rev() {
            for col in 0..self.width {
                let px = self.get_pixel(col, row);
                out.push(px[2]); // B
                out.push(px[1]); // G
                out.push(px[0]); // R
            }
        }
        out
    }

    /// Convert to TGA bytes (RGBA 32-bit, bottom-left origin).
    pub fn to_tga_bytes_rgba(&self) -> Vec<u8> {
        let w = self.width as u16;
        let h = self.height as u16;
        let mut out = Vec::with_capacity(18 + self.width * self.height * 4);

        // 18-byte header
        out.extend_from_slice(&[
            0, // [0]  ID length
            0, // [1]  color map type
            2, // [2]  image type: uncompressed true-color
            0, 0, // [3-4]  color map origin
            0, 0, // [5-6]  color map length
            0, // [7]  color map entry size
            0, 0, // [8-9]  x origin
            0, 0, // [10-11] y origin
        ]);
        out.extend_from_slice(&w.to_le_bytes()); // [12-13] width
        out.extend_from_slice(&h.to_le_bytes()); // [14-15] height
        out.push(32); // [16] bits per pixel
        out.push(8); // [17] image descriptor: 8 bits of alpha, bottom-left origin

        // Pixel data — bottom row first
        for row in (0..self.height).rev() {
            for col in 0..self.width {
                let px = self.get_pixel(col, row);
                out.push(px[2]); // B
                out.push(px[1]); // G
                out.push(px[0]); // R
                out.push(px[3]); // A
            }
        }
        out
    }

    /// Create a solid color image.
    pub fn solid_color(width: usize, height: usize, color: [u8; 4]) -> Self {
        Self::new(width, height, color)
    }

    /// Create a horizontal gradient image (lerp from→to along x axis).
    pub fn gradient(width: usize, height: usize, from: [u8; 4], to: [u8; 4]) -> Self {
        let mut img = Self::new(width, height, [0, 0, 0, 255]);
        for y in 0..height {
            for x in 0..width {
                let t = if width <= 1 {
                    0.0f32
                } else {
                    x as f32 / (width - 1) as f32
                };
                let r = (from[0] as f32 + t * (to[0] as f32 - from[0] as f32)) as u8;
                let g = (from[1] as f32 + t * (to[1] as f32 - from[1] as f32)) as u8;
                let b = (from[2] as f32 + t * (to[2] as f32 - from[2] as f32)) as u8;
                let a = (from[3] as f32 + t * (to[3] as f32 - from[3] as f32)) as u8;
                img.set_pixel(x, y, [r, g, b, a]);
            }
        }
        img
    }

    /// Create a checkerboard pattern.
    pub fn checkerboard(width: usize, height: usize, size: usize, a: [u8; 4], b: [u8; 4]) -> Self {
        let mut img = Self::new(width, height, [0, 0, 0, 255]);
        let tile = size.max(1);
        for y in 0..height {
            for x in 0..width {
                let color = if ((x / tile) + (y / tile)).is_multiple_of(2) {
                    a
                } else {
                    b
                };
                img.set_pixel(x, y, color);
            }
        }
        img
    }
}

// ---------------------------------------------------------------------------
// File I/O helpers
// ---------------------------------------------------------------------------

/// Write a TGA file (RGB 24-bit).
pub fn export_tga_rgb(image: &TgaImage, path: &Path) -> anyhow::Result<()> {
    let data = image.to_tga_bytes_rgb();
    fs::write(path, data)?;
    Ok(())
}

/// Write a TGA file (RGBA 32-bit).
pub fn export_tga_rgba(image: &TgaImage, path: &Path) -> anyhow::Result<()> {
    let data = image.to_tga_bytes_rgba();
    fs::write(path, data)?;
    Ok(())
}

/// Export float RGB pixels as a TGA file.
pub fn export_float_rgb_tga(
    pixels: &[[f32; 3]],
    width: usize,
    height: usize,
    path: &Path,
) -> anyhow::Result<()> {
    let image = TgaImage::from_rgb_f32(pixels, width, height);
    export_tga_rgb(&image, path)
}

/// Export float RGBA pixels as a TGA file.
pub fn export_float_rgba_tga(
    pixels: &[[f32; 4]],
    width: usize,
    height: usize,
    path: &Path,
) -> anyhow::Result<()> {
    let image = TgaImage::from_rgba_f32(pixels, width, height);
    export_tga_rgba(&image, path)
}

/// Read only the header of a TGA file. Returns (width, height, bits_per_pixel).
pub fn read_tga_header(path: &Path) -> anyhow::Result<(usize, usize, u8)> {
    let mut file = fs::File::open(path)?;
    let mut header = [0u8; 18];
    file.read_exact(&mut header)?;
    let width = u16::from_le_bytes([header[12], header[13]]) as usize;
    let height = u16::from_le_bytes([header[14], header[15]]) as usize;
    let bpp = header[16];
    Ok((width, height, bpp))
}

/// Validate that a file is an uncompressed TGA (image type byte == 2).
pub fn validate_tga(path: &Path) -> anyhow::Result<bool> {
    let mut file = fs::File::open(path)?;
    let mut header = [0u8; 18];
    file.read_exact(&mut header)?;
    Ok(header[2] == 2)
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use std::path::PathBuf;

    fn tmp(name: &str) -> PathBuf {
        PathBuf::from(format!("/tmp/{name}"))
    }

    #[test]
    fn test_tga_image_new() {
        let img = TgaImage::new(4, 4, [255, 0, 0, 255]);
        assert_eq!(img.width, 4);
        assert_eq!(img.height, 4);
        assert_eq!(img.pixels.len(), 4 * 4 * 4);
        assert_eq!(&img.pixels[0..4], &[255, 0, 0, 255]);
    }

    #[test]
    fn test_tga_image_get_set_pixel() {
        let mut img = TgaImage::new(8, 8, [0, 0, 0, 255]);
        img.set_pixel(3, 5, [10, 20, 30, 40]);
        let px = img.get_pixel(3, 5);
        assert_eq!(px, [10, 20, 30, 40]);
    }

    #[test]
    fn test_tga_to_bytes_rgb_header() {
        let img = TgaImage::new(16, 32, [0, 0, 0, 255]);
        let bytes = img.to_tga_bytes_rgb();
        assert!(bytes.len() >= 18);
        assert_eq!(bytes[0], 0); // ID length
        assert_eq!(bytes[1], 0); // color map type
        assert_eq!(bytes[2], 2); // image type: uncompressed
                                 // width
        let w = u16::from_le_bytes([bytes[12], bytes[13]]);
        assert_eq!(w, 16);
        // height
        let h = u16::from_le_bytes([bytes[14], bytes[15]]);
        assert_eq!(h, 32);
        assert_eq!(bytes[16], 24); // bpp
        assert_eq!(bytes[17], 0); // descriptor
        assert_eq!(bytes.len(), 18 + 16 * 32 * 3);
    }

    #[test]
    fn test_tga_to_bytes_rgba_header() {
        let img = TgaImage::new(8, 8, [0, 0, 0, 255]);
        let bytes = img.to_tga_bytes_rgba();
        assert_eq!(bytes[2], 2); // image type
        assert_eq!(bytes[16], 32); // bpp
        assert_eq!(bytes[17], 8); // descriptor (alpha depth)
        assert_eq!(bytes.len(), 18 + 8 * 8 * 4);
    }

    #[test]
    fn test_tga_pixel_order_bgr() {
        // Single red pixel => TGA RGB should have B=0, G=0, R=255
        let mut img = TgaImage::new(1, 1, [0, 0, 0, 255]);
        img.set_pixel(0, 0, [255, 0, 0, 255]); // red
        let bytes = img.to_tga_bytes_rgb();
        // Pixel starts at offset 18
        assert_eq!(bytes[18], 0); // B
        assert_eq!(bytes[19], 0); // G
        assert_eq!(bytes[20], 255); // R
    }

    #[test]
    fn test_solid_color() {
        let color = [128u8, 64, 32, 200];
        let img = TgaImage::solid_color(10, 10, color);
        for y in 0..10 {
            for x in 0..10 {
                assert_eq!(img.get_pixel(x, y), color);
            }
        }
    }

    #[test]
    fn test_gradient() {
        let from = [0u8, 0, 0, 255];
        let to = [255u8, 0, 0, 255];
        let img = TgaImage::gradient(11, 1, from, to);
        // First pixel should match 'from'
        assert_eq!(img.get_pixel(0, 0)[0], 0);
        // Last pixel should match 'to'
        assert_eq!(img.get_pixel(10, 0)[0], 255);
    }

    #[test]
    fn test_checkerboard() {
        let a = [255u8, 255, 255, 255];
        let b = [0u8, 0, 0, 255];
        let img = TgaImage::checkerboard(4, 4, 2, a, b);
        // (0,0) in tile 0+0=0 => color a
        assert_eq!(img.get_pixel(0, 0), a);
        // (2,0) in tile 1+0=1 => color b
        assert_eq!(img.get_pixel(2, 0), b);
        // (2,2) in tile 1+1=2 => color a
        assert_eq!(img.get_pixel(2, 2), a);
    }

    #[test]
    fn test_from_rgb_f32() {
        let pixels = vec![[1.0f32, 0.5, 0.0]];
        let img = TgaImage::from_rgb_f32(&pixels, 1, 1);
        let px = img.get_pixel(0, 0);
        assert_eq!(px[0], 255);
        assert!((px[1] as i32 - 127).abs() <= 1);
        assert_eq!(px[2], 0);
        assert_eq!(px[3], 255); // alpha filled to 255
    }

    #[test]
    fn test_export_tga_rgb() {
        let path = tmp("test_export_tga_rgb.tga");
        let img = TgaImage::solid_color(16, 16, [255, 128, 64, 255]);
        export_tga_rgb(&img, &path).expect("should succeed");
        let data = fs::read(&path).expect("should succeed");
        assert_eq!(data.len(), 18 + 16 * 16 * 3);
        assert_eq!(data[2], 2);
        assert_eq!(data[16], 24);
    }

    #[test]
    fn test_export_tga_rgba() {
        let path = tmp("test_export_tga_rgba.tga");
        let img = TgaImage::solid_color(8, 8, [0, 255, 0, 128]);
        export_tga_rgba(&img, &path).expect("should succeed");
        let data = fs::read(&path).expect("should succeed");
        assert_eq!(data.len(), 18 + 8 * 8 * 4);
        assert_eq!(data[16], 32);
    }

    #[test]
    fn test_validate_tga() {
        let path = tmp("test_validate_tga.tga");
        let img = TgaImage::solid_color(4, 4, [255, 0, 0, 255]);
        export_tga_rgb(&img, &path).expect("should succeed");
        let ok = validate_tga(&path).expect("should succeed");
        assert!(ok);
    }

    #[test]
    fn test_read_tga_header() {
        let path = tmp("test_read_tga_header.tga");
        let img = TgaImage::solid_color(32, 64, [0, 0, 255, 255]);
        export_tga_rgb(&img, &path).expect("should succeed");
        let (w, h, bpp) = read_tga_header(&path).expect("should succeed");
        assert_eq!(w, 32);
        assert_eq!(h, 64);
        assert_eq!(bpp, 24);
    }
}