darkomen 0.5.0

Warhammer: Dark Omen library and CLI in Rust
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
mod decoder;
mod encoder;

#[cfg(feature = "bevy_reflect")]
use bevy_reflect::prelude::*;
use image::{DynamicImage, GenericImage, Rgba};
use serde::Serialize;

pub use decoder::{DecodeError, Decoder};
pub use encoder::{EncodeError, Encoder};

#[derive(Clone, Default, Serialize)]
#[cfg_attr(feature = "debug", derive(Debug))]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
#[cfg_attr(
    all(feature = "bevy_reflect", feature = "debug"),
    reflect(Default, Serialize)
)]
#[cfg_attr(all(feature = "bevy_reflect", feature = "debug"), reflect(Debug))]
pub struct Lightmap {
    pub width: u32,
    pub height: u32,
    /// A list of large blocks for the lightmap.
    #[cfg_attr(feature = "bevy_reflect", reflect(ignore))]
    pub blocks: Vec<LightmapBlock>,
    /// A list of height offsets for an 8x8 block. Each item is a list which
    /// must have exactly 64 (8x8) u8s. A given height offset should be added to
    /// the base height of the block.
    #[cfg_attr(feature = "bevy_reflect", reflect(ignore))]
    pub height_offsets: Vec<Vec<u8>>,
}

impl Lightmap {
    fn normalized_offset_height(offset_height: u8) -> f32 {
        offset_height as f32 / 8.0
    }

    fn min_and_max_normalized_base_height(&self) -> (f32, f32) {
        self.blocks
            .iter()
            .map(|block| block.normalized_base_height())
            .fold((f32::MAX, f32::MIN), |(min, max), val| {
                (min.min(val), max.max(val))
            })
    }

    pub fn image(&self) -> DynamicImage {
        let mut img = DynamicImage::new_rgba8(self.width, self.height);

        let (min_normalized_base_height, max_normalized_base_height) =
            self.min_and_max_normalized_base_height();

        let mut row = 0;
        let mut col = 0;

        for block in &self.blocks {
            let height_offsets = &self.height_offsets[block.height_offsets_index as usize];

            if col * 8 >= self.width {
                col = 0;
                row += 1;
            }

            for y in 0..8 {
                let img_y = row * 8 + y;

                if img_y >= self.height {
                    break;
                }

                for x in 0..8 {
                    let img_x = col * 8 + x;

                    if img_x >= self.width {
                        break;
                    }

                    // The image needs to be flipped horizontally.
                    let img_x = self.width - 1 - img_x;

                    let offset_height = height_offsets[(x + y * 8) as usize];

                    let color = Lightmap::calculate_color(
                        min_normalized_base_height,
                        max_normalized_base_height,
                        block,
                        Lightmap::normalized_offset_height(offset_height),
                    );

                    img.put_pixel(img_x, img_y, Rgba([color, color, color, 255]));
                }
            }

            col += 1;
        }

        img
    }

    fn calculate_color(
        min_normalized_base_height: f32,
        max_normalized_base_height: f32,
        block: &LightmapBlock,
        normalized_offset_height: f32,
    ) -> u8 {
        // The largest value that can be stored for a block's height is u16::MAX
        // because base height is an i32 and u16::MAX is the largest positive
        // value that can be stored in an i32. u16::MAX is then divided by 1024
        // to get the normalized maximum.
        //
        // Technically, if a block's base height was u16::MAX, and an offset
        // height was any value other than 0, the combined height would
        // overflow. But in all the game files, the largest value for a block's
        // base height is below (u16::MAX - u8::MAX) so this is not a concern.
        const MAX_NORMALIZED_HEIGHT: f32 = u16::MAX as f32 / 1024.;

        // The largest value that can be stored for a block's offset height is
        // u8::MAX because offset height is a u8. u8::MAX is then divided by 8
        // to get the normalized maximum.
        const MAX_NORMALIZED_OFFSET_HEIGHT: f32 = u8::MAX as f32 / 8.;

        let normalized_height = block.normalized_base_height() + normalized_offset_height;

        let scaled_value = normalized_height / MAX_NORMALIZED_HEIGHT;

        let min = min_normalized_base_height / MAX_NORMALIZED_HEIGHT;
        let max =
            (max_normalized_base_height + MAX_NORMALIZED_OFFSET_HEIGHT) / MAX_NORMALIZED_HEIGHT;

        let normalized_value = normalize(scaled_value, min, max);

        // Convert the normalized value (between 0 and 1) to a color (between 0
        // and 255).
        let color = normalized_value * 255.;

        // Invert the color, otherwise shadows are white.
        let color = 255. - color;

        color as u8 // truncate any fractional part
    }
}

#[derive(Clone, Serialize)]
#[cfg_attr(feature = "debug", derive(Debug))]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Serialize))]
#[cfg_attr(all(feature = "bevy_reflect", feature = "debug"), reflect(Debug))]
pub struct LightmapBlock {
    /// The base height of all 64 (8x8) values in the block.
    pub base_height: i32,
    /// An index into the height offsets list. Used to get the 64 (8x8) values
    /// that make up the block. The values are height offsets based on the base
    /// height. To get the height at a specific point, combine the base height
    /// with the offset at that point.
    pub height_offsets_index: u32,
}

impl LightmapBlock {
    /// Returns the normalized base height of the block by dividing the stored
    /// integer value by 1024. This conversion reflects the original intention
    /// for the height to be represented as a float.
    pub fn normalized_base_height(&self) -> f32 {
        self.base_height as f32 / 1024.0
    }
}

fn normalize(value: f32, min: f32, max: f32) -> f32 {
    (value - min) / (max - min)
}

#[cfg(test)]
mod tests {
    use std::{
        ffi::{OsStr, OsString},
        fs::File,
        path::{Path, PathBuf},
    };

    use image::{DynamicImage, GenericImageView, RgbaImage};
    use pretty_assertions::assert_eq;

    use super::*;

    macro_rules! test_normalize {
        ($name:ident, $value:expr, $min:expr, $max:expr, $expected:expr) => {
            #[test]
            fn $name() {
                let value = $value;
                let min = $min;
                let max = $max;
                let expected = $expected;

                let result = normalize(value, min, max);
                assert_eq!(result, expected);
            }
        };
    }

    test_normalize!(test_normalize_min, 0.0, 0.0, 1.0, 0.0);
    test_normalize!(test_normalize_max, 1.0, 0.0, 1.0, 1.0);
    test_normalize!(test_normalize_middle, 0.5, 0.0, 1.0, 0.5);
    test_normalize!(test_normalize_negative_min, -1.0, -1.0, 1.0, 0.0);
    test_normalize!(test_normalize_negative_max, 1.0, -1.0, 1.0, 1.0);
    test_normalize!(test_normalize_negative_middle, 0.0, -1.0, 1.0, 0.5);
    test_normalize!(test_normalize_large_range_low_end, 0.5, 0.0, 100.0, 0.005);
    test_normalize!(test_normalize_large_range_middle, 50.0, 0.0, 100.0, 0.5);
    test_normalize!(test_normalize_large_range_high_end, 99.5, 0.0, 100.0, 0.995);

    #[test]
    fn test_min_and_max_normalized_base_height() {
        let lightmap = Lightmap {
            blocks: vec![
                LightmapBlock {
                    base_height: -1024,
                    height_offsets_index: 0,
                },
                LightmapBlock {
                    base_height: 1024,
                    height_offsets_index: 0,
                },
                LightmapBlock {
                    base_height: 2048,
                    height_offsets_index: 0,
                },
            ],
            height_offsets: vec![vec![0; 64]; 1],
            ..Default::default()
        };

        let (min, max) = lightmap.min_and_max_normalized_base_height();
        assert_eq!(min, -1.0);
        assert_eq!(max, 2.0);
    }

    fn roundtrip_test(original_bytes: &[u8], l: &Lightmap) {
        let mut encoded_bytes = Vec::new();
        Encoder::new(&mut encoded_bytes).encode(l).unwrap();

        let original_bytes = original_bytes
            .chunks(16)
            .map(|chunk| {
                chunk
                    .iter()
                    .map(|b| format!("{b:02X}"))
                    .collect::<Vec<_>>()
                    .join(" ")
            })
            .collect::<Vec<_>>()
            .join("\n");

        let encoded_bytes = encoded_bytes
            .chunks(16)
            .map(|chunk| {
                chunk
                    .iter()
                    .map(|b| format!("{b:02X}"))
                    .collect::<Vec<_>>()
                    .join(" ")
            })
            .collect::<Vec<_>>()
            .join("\n");

        assert_eq!(original_bytes, encoded_bytes);
    }

    #[test]
    fn test_decode_b1_01() {
        let d: PathBuf = [
            std::env::var("DARKOMEN_PATH").unwrap().as_str(),
            "DARKOMEN",
            "GAMEDATA",
            "1PBAT",
            "B1_01",
            "B1_01.SHD",
        ]
        .iter()
        .collect();

        let original_bytes = std::fs::read(d.clone()).unwrap();

        let file = File::open(d.clone()).unwrap();
        let lightmap = Decoder::new(file).decode().unwrap();

        assert_eq!(lightmap.width, 184);
        assert_eq!(lightmap.height, 200);
        assert_eq!(lightmap.blocks.len(), 575);
        assert_eq!(lightmap.height_offsets.len(), 484);

        roundtrip_test(&original_bytes, &lightmap);
    }

    #[test]
    fn test_decode_mb4_01() {
        let d: PathBuf = [
            std::env::var("DARKOMEN_PATH").unwrap().as_str(),
            "DARKOMEN",
            "GAMEDATA",
            "1PBAT",
            "B4_01",
            "MB4_01.SHD",
        ]
        .iter()
        .collect();

        let original_bytes = std::fs::read(d.clone()).unwrap();

        let file = File::open(d.clone()).unwrap();
        let lightmap = Decoder::new(file).decode().unwrap();

        roundtrip_test(&original_bytes, &lightmap);
    }

    #[test]
    fn test_decode_b4_09() {
        let d: PathBuf = [
            std::env::var("DARKOMEN_PATH").unwrap().as_str(),
            "DARKOMEN",
            "GAMEDATA",
            "1PBAT",
            "B4_09",
            "B4_09.SHD",
        ]
        .iter()
        .collect();

        let original_bytes = std::fs::read(d.clone()).unwrap();

        let file = File::open(d.clone()).unwrap();
        let lightmap = Decoder::new(file).decode().unwrap();

        roundtrip_test(&original_bytes, &lightmap);
    }

    #[test]
    fn test_decode_b5_01() {
        let d: PathBuf = [
            std::env::var("DARKOMEN_PATH").unwrap().as_str(),
            "DARKOMEN",
            "GAMEDATA",
            "1PBAT",
            "B5_01",
            "B5_01.SHD",
        ]
        .iter()
        .collect();

        let original_bytes = std::fs::read(d.clone()).unwrap();

        let file = File::open(d.clone()).unwrap();
        let lightmap = Decoder::new(file).decode().unwrap();

        roundtrip_test(&original_bytes, &lightmap);
    }

    #[test]
    fn test_decode_all() {
        let d: PathBuf = [
            std::env::var("DARKOMEN_PATH").unwrap().as_str(),
            "DARKOMEN",
            "GAMEDATA",
            "1PBAT",
        ]
        .iter()
        .collect();

        let root_output_dir: PathBuf = [env!("CARGO_MANIFEST_DIR"), "decoded", "shadows"]
            .iter()
            .collect();

        std::fs::create_dir_all(&root_output_dir).unwrap();

        fn visit_dirs(dir: &Path, cb: &mut dyn FnMut(&Path)) {
            println!("Reading dir {:?}", dir.display());

            let mut paths = std::fs::read_dir(dir)
                .unwrap()
                .map(|res| res.map(|e| e.path()))
                .collect::<Result<Vec<_>, std::io::Error>>()
                .unwrap();

            paths.sort();

            for path in paths {
                if path.is_dir() {
                    visit_dirs(&path, cb);
                } else {
                    cb(&path);
                }
            }
        }

        visit_dirs(&d, &mut |path| {
            let Some(ext) = path.extension() else {
                return;
            };
            if ext.to_string_lossy().to_uppercase() != "SHD" {
                return;
            }

            println!("Decoding {:?}", path.file_name().unwrap());

            let original_bytes = std::fs::read(path).unwrap();

            let file = File::open(path).unwrap();
            let lightmap = Decoder::new(file).decode().unwrap();

            roundtrip_test(&original_bytes, &lightmap);

            let has_invalid_height_offsets_index = lightmap
                .blocks
                .iter()
                .any(|block| block.height_offsets_index as usize >= lightmap.height_offsets.len());
            assert!(
                !has_invalid_height_offsets_index,
                "found a block with an invalid height offsets index"
            );

            let img = lightmap.image();

            // Compare against the golden image.
            compare_image(path, img.clone());

            // Write out the decoded data for manual inspection.
            {
                // RON.
                let output_path =
                    append_ext("ron", root_output_dir.join(path.file_name().unwrap()));
                let mut buffer = String::new();
                ron::ser::to_writer_pretty(&mut buffer, &lightmap, Default::default()).unwrap();
                std::fs::write(output_path, buffer).unwrap();

                // Image.
                let output_dir = root_output_dir.join("lightmaps");
                std::fs::create_dir_all(&output_dir).unwrap();

                let output_path = output_dir
                    .join(path.file_stem().unwrap())
                    .with_extension("lightmap.png");
                img.save(output_path).unwrap();
            }
        });
    }

    fn compare_image(path: &Path, img: DynamicImage) {
        let golden_images_path = Path::new(env!("CARGO_MANIFEST_DIR"))
            .join("src")
            .join("shadow")
            .join("testdata")
            .join("images");
        let golden_img_path = golden_images_path
            .join(path.file_name().unwrap())
            .with_extension("golden.png");

        if !Path::new(&golden_img_path).exists() {
            img.save(&golden_img_path).unwrap();
        }

        let golden_img = image::open(&golden_img_path).unwrap();

        assert_eq!(img.dimensions(), golden_img.dimensions());

        let pixels_equal = img
            .pixels()
            .zip(golden_img.clone().pixels())
            .all(|(p1, p2)| p1 == p2);

        if !pixels_equal {
            // Write out the actual image so it can be visually compared against
            // the golden.
            img.save(
                golden_images_path
                    .join(path.file_name().unwrap())
                    .with_extension("actual.png"),
            )
            .unwrap();

            // Write out an image of the diff between the two.
            let diff_bytes = img
                .into_bytes()
                .into_iter()
                .zip(golden_img.clone().into_bytes())
                .map(|(p1, p2)| {
                    if p1 > p2 {
                        return p1 - p2;
                    }
                    p2 - p1
                })
                .map(|p| 255 - p) // inverting the diff fixes alpha going to 0 in the previous map
                .collect::<Vec<_>>();
            let diff_img = DynamicImage::ImageRgba8(
                RgbaImage::from_raw(golden_img.width(), golden_img.height(), diff_bytes).unwrap(),
            );
            diff_img
                .save(
                    golden_images_path
                        .join(path.file_name().unwrap())
                        .with_extension("diff.png"),
                )
                .unwrap();
        }

        assert!(pixels_equal, "pixels do not match");
    }

    fn append_ext(ext: impl AsRef<OsStr>, path: PathBuf) -> PathBuf {
        let mut os_string: OsString = path.into();
        os_string.push(".");
        os_string.push(ext.as_ref());
        os_string.into()
    }
}