gboxide 0.2.2

A GameBoy Emulator written 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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
use num_traits::FromPrimitive;

use crate::gameboy::interrupt::{Interrupt, InterruptHandler};

pub const SCREEN_WIDTH: u8 = 160;
pub const SCREEN_HEIGHT: u8 = 144;

#[derive(Clone, Copy, Debug, FromPrimitive)]
pub enum TileDataAddressRange {
    TileDataAddr8800_97FF = 0,
    TileDataAddr8000_8FFF = 1,
}
impl From<u8> for TileDataAddressRange {
    fn from(value: u8) -> TileDataAddressRange {
        FromPrimitive::from_u8(value).expect("invalid tile data address bit")
    }
}
impl From<TileDataAddressRange> for u8 {
    fn from(value: TileDataAddressRange) -> u8 {
        value as u8
    }
}

#[derive(Clone, Copy, Debug, FromPrimitive)]
pub enum TileMapAddressRange {
    TileMapAddr9800_9BFF = 0,
    TileMapAddr9C00_9FFF = 1,
}
impl From<u8> for TileMapAddressRange {
    fn from(value: u8) -> TileMapAddressRange {
        FromPrimitive::from_u8(value).expect("invalid tile map address bit")
    }
}
impl From<TileMapAddressRange> for u8 {
    fn from(value: TileMapAddressRange) -> u8 {
        value as u8
    }
}

#[derive(Clone, Copy, Debug, FromPrimitive)]
pub enum SpriteSizes {
    Size8x8 = 0,
    Size8x16 = 1,
}
impl From<u8> for SpriteSizes {
    fn from(value: u8) -> SpriteSizes {
        FromPrimitive::from_u8(value).expect("invalid sprite size bit")
    }
}
impl From<SpriteSizes> for u8 {
    fn from(value: SpriteSizes) -> u8 {
        value as u8
    }
}

bitfield!{
    struct Control(u8);
    impl Debug;
    // get, set: msb,lsb,count;
    enable, _: 7;
    from into TileMapAddressRange, window_map, _: 6,6;
    window_enable, _: 5;
    from into TileDataAddressRange, tile_data, _: 4,4;
    from into TileMapAddressRange, bg_map, _: 3,3;
    from into SpriteSizes, sprite_size, _: 2,2;
    sprite_enable, _: 1;
    bg_enable, _: 0;
    from into u8, bits, set_bits: 7,0;
}

#[derive(Clone, Copy, Debug, PartialEq, FromPrimitive)]
enum Mode {
    HBlank = 0b00,
    VBlank = 0b01,
    OAMSearch = 0b10,
    Transfer = 0b11,
}
impl From<u8> for Mode {
    fn from(value: u8) -> Mode {
        FromPrimitive::from_u8(value).expect("invalid mode")
    }
}
impl From<Mode> for u8 {
    fn from(value: Mode) -> u8 {
        value as u8
    }
}

bitfield!{
    struct Status(u8);
    impl Debug;
    // get, set: msb,lsb,count;
    ly_coincidence_interrupt, _: 6;
    oam_interrupt, _: 5;
    vblank_interrupt, _: 4;
    hblank_interrupt, _: 3;
    coincidence_flag, set_coincidence_flag: 2;
    from into Mode, mode_flag, set_mode_flag: 1,0;
    from into u8, bits, set_bits: 7,0;
}

bitfield!{
    #[derive(Clone, Copy)]
    struct Attributes(u8);
    impl Debug;
    // get, set: msb,lsb,count;
    obj_to_bg_priority, _: 7;
    y_flip, _: 6;
    x_flip, _: 5;
    u8, palette, _: 4,4;
    // the lower byte is CGB only
    from into u8, bits, set_bits: 7,0;
}

#[derive(Clone, Copy, Debug)]
pub struct OAM {
    y_position: u8,
    x_position: u8,
    tile_number: u8,
    attributes: Attributes,
}

impl OAM {
    fn new() -> OAM {
        OAM {
            y_position: 0x00,
            x_position: 0x00,
            tile_number: 0x00,
            attributes: Attributes(0x00),
        }
    }
}

bitfield!{
    struct Palette(u8);
    impl Debug;
    // get, set: msb,lsb,count;
    from into Shade, colour, set_colour: 1,0,4;
    from into u8, bits, set_bits: 7,0;
}

#[derive(Clone, Copy, Debug, FromPrimitive)]
enum Shade {
    White = 0b00,
    LightGray = 0b01,
    DarkGray = 0b10,
    Black = 0b11,
}
impl From<u8> for Shade {
    fn from(value: u8) -> Shade {
        FromPrimitive::from_u8(value).expect("invalid shade")
    }
}
impl From<Shade> for u8 {
    fn from(value: Shade) -> u8 {
        value as u8
    }
}
impl Shade {
    fn into_pixel(&self) -> &[u8] {
        use Shade::*;
        match *self {
            White => &[0xFF, 0xFF, 0xFF, 0xFF],
            LightGray => &[0xCC, 0xCC, 0xCC, 0xFF],
            DarkGray => &[0x77, 0x77, 0x77, 0xFF],
            Black => &[0x00, 0x00, 0x00, 0xFF],
        }
    }
}

pub struct LCD {
    pub vram_tile_data: [u8; 0x1800], //0x8000-0x97FF
    pub vram_bg_maps: [u8; 0x0800],   //0x9800-0x9FFF
    pub vram_oam: [OAM; 40],          //0xFE00-0xFE9F

    control: Control,
    status: Status,

    scroll_y: u8,
    scroll_x: u8,

    scanline_cycle_count: i16,
    lcd_y: u8, //TODO: more specialised than u8?
    lcd_y_compare: u8,

    bg_palette: Palette,
    sprite_palette_0: Palette,
    sprite_palette_1: Palette,

    window_y: u8,
    window_x: u8,

    frame: [u8; SCREEN_WIDTH as usize * SCREEN_HEIGHT as usize * 4],
    last_frame_hash: u64,

    vblank_set: bool,
}

impl LCD {
    const SCANLINE_CYCLE_TOTAL: i16 = 456; // from the pandocs, total cycles to process one scanline
    const MODE2_CYCLE_RANGE: i16 = LCD::SCANLINE_CYCLE_TOTAL - 80;
    const MODE3_CYCLE_RANGE: i16 = LCD::MODE2_CYCLE_RANGE - 172;

    const VBLANK_HEIGHT: u8 = 154;

    pub fn new() -> LCD {
        LCD {
            vram_tile_data: [0x00; 0x1800],
            vram_bg_maps: [0x00; 0x0800],
            vram_oam: [OAM::new(); 40],

            control: Control(0x80),
            status: Status(0x00),

            scroll_y: 0x00,
            scroll_x: 0x00,

            scanline_cycle_count: LCD::SCANLINE_CYCLE_TOTAL,
            lcd_y: 0x00,
            lcd_y_compare: 0x00,

            bg_palette: Palette(0x00),
            sprite_palette_0: Palette(0x00),
            sprite_palette_1: Palette(0x00),

            window_y: 0x00,
            window_x: 0x00,

            frame: [0x00; SCREEN_WIDTH as usize * SCREEN_HEIGHT as usize * 4],
            last_frame_hash: 0,

            vblank_set: false,
        }
    }

    pub fn read_register(&self, addr: u16) -> u8 {
        match addr {
            0xFF40 => self.control.bits(),
            0xFF41 => self.status.bits(),
            0xFF42 => self.scroll_y,
            0xFF43 => self.scroll_x,
            0xFF44 => self.lcd_y,
            0xFF45 => self.lcd_y_compare,
            0xFF46 => 0xFF, // DMA Transfer // TODO: write-only, I'm assuming the read value here
            0xFF47 => self.bg_palette.bits(), // BG/Window palette
            0xFF48 => self.sprite_palette_0.bits(), // sprite palette 0
            0xFF49 => self.sprite_palette_1.bits(), // sprite palette 1
            0xFF4A => self.window_y,
            0xFF4B => self.window_x,
            _ => unreachable!(), // mmu will only send us addresses in 0xFF40 - 0xFF4B range
        }
    }

    pub fn write_register(&mut self, addr: u16, value: u8) {
        match addr {
            0xFF40 => {
                if self.control.enable() != (value & 0x80 > 0) {
                    self.lcd_y = 0;
                    self.scanline_cycle_count = LCD::SCANLINE_CYCLE_TOTAL;
                    let mode = if value & 0x80 > 0 { Mode::OAMSearch } else { Mode::HBlank };
                    self.status.set_mode_flag(mode);
                }
                self.control.set_bits(value)
            },
            0xFF41 => self.status.set_bits(value),
            0xFF42 => self.scroll_y = value,
            0xFF43 => self.scroll_x = value,
            0xFF44 => self.lcd_y = 0x00, // writing resets this counter TODO: no it doesn't
            0xFF45 => self.lcd_y_compare = value,
            // 0xFF46 => (), // DMA Transfer - done in the mmu
            0xFF47 => self.bg_palette.set_bits(value), // BG/Window palette
            0xFF48 => self.sprite_palette_0.set_bits(value), // sprite palette 0
            0xFF49 => self.sprite_palette_1.set_bits(value), // sprite palette 1
            0xFF4A => self.window_y = value,
            0xFF4B => self.window_x = value,
            _ => unreachable!(), // mmu will only send us addresses in 0xFF40 - 0xFF4B range
        }
    }

    pub fn read_oam(&self, addr: u16) -> u8 {
        let oam_addr = (addr / 4) as usize;
        match addr % 4 {
            0x0 => self.vram_oam[oam_addr].y_position,
            0x1 => self.vram_oam[oam_addr].x_position,
            0x2 => self.vram_oam[oam_addr].tile_number,
            0x3 => self.vram_oam[oam_addr].attributes.bits() as u8,
            _ => unreachable!(),
        }
    }

    pub fn write_oam(&mut self, addr: u16, value: u8) {
        let oam_addr = (addr / 4) as usize;
        match addr % 4 {
            0x0 => self.vram_oam[oam_addr].y_position = value,
            0x1 => self.vram_oam[oam_addr].x_position = value,
            0x2 => self.vram_oam[oam_addr].tile_number = value,
            0x3 => self.vram_oam[oam_addr].attributes.set_bits(value),
            _ => unreachable!(),
        }
    }

    pub fn step(&mut self, ih: &mut InterruptHandler) {
        self.set_status(ih);

        if !self.control.enable() {
            return;
        }

        self.scanline_cycle_count -= 4;
        if self.scanline_cycle_count > 0 {
            return;
        }

        self.scanline_cycle_count = LCD::SCANLINE_CYCLE_TOTAL;
        match self.lcd_y {
            0..=SCREEN_HEIGHT if self.lcd_y < SCREEN_HEIGHT => {
                self.draw_scanline();
                self.lcd_y += 1;
            },
            SCREEN_HEIGHT => {
                ih.set_interrupt(Interrupt::VBlank);
                self.lcd_y += 1;
            },
            // TODO: pad this out to reduce lag?
            // (give the emulated cpu more time than
            // the actual hardware cpu would have had
            // to process each frame)
            LCD::VBLANK_HEIGHT => self.lcd_y = 0,
            _ => self.lcd_y += 1,
        }
    }

    pub fn vblank_reached(&mut self) -> bool {
        if self.vblank_set {
            self.vblank_set = false;
            true
        } else {
            false
        }
    }

    pub fn get_frame(&self) -> &[u8] {
        &self.frame
    }

    fn set_status(&mut self, ih: &mut InterruptHandler) {
        // if the LCD is disabled, reset scanline cycles and y position, and force VBlank mode
        if !self.control.enable() {
            self.scanline_cycle_count = LCD::SCANLINE_CYCLE_TOTAL;
            self.lcd_y = 0;
            self.status.set_mode_flag(Mode::VBlank);
            return;
        }

        // store current mode so we can detect changes
        let prev_mode = self.status.mode_flag();
        // set mode based on scanline y position and cycle count
        if self.lcd_y >= SCREEN_HEIGHT {
            self.status.set_mode_flag(Mode::VBlank);
        } else {
            if self.scanline_cycle_count >= LCD::MODE2_CYCLE_RANGE as i16 {
                self.status.set_mode_flag(Mode::OAMSearch);
            } else if self.scanline_cycle_count >= LCD::MODE3_CYCLE_RANGE as i16 {
                self.status.set_mode_flag(Mode::Transfer);
            } else {
                self.status.set_mode_flag(Mode::HBlank);
            }
        }
        // if mode changed, and interrupts for the new mode are enabled, set LCDC interrupt
        if prev_mode != self.status.mode_flag() {
            match self.status.mode_flag() {
                Mode::HBlank => self.hblank(ih),
                Mode::VBlank => self.vblank(ih),
                Mode::OAMSearch => self.oam_search(ih),
                Mode::Transfer => (),
            }
        }

        // flag and interrupt when we're on the game-specified scanline lcd_y_compare
        if self.lcd_y == self.lcd_y_compare {
            self.status.set_coincidence_flag(true);
            if self.status.ly_coincidence_interrupt() {
                self.lcdc_interrupt(ih)
            }
        } else {
            self.status.set_coincidence_flag(false);
        }
    }

    fn hblank(&self, ih: &mut InterruptHandler) {
        if self.status.hblank_interrupt() {
            self.lcdc_interrupt(ih)
        }
    }

    fn vblank(&mut self, ih: &mut InterruptHandler) {
        if self.status.vblank_interrupt() {
            self.lcdc_interrupt(ih);
        }

        self.vblank_set = true;
        
//        use std::hash::{Hash, Hasher};
//        use std::collections::hash_map::DefaultHasher;
//        let mut hasher = DefaultHasher::new();
//        self.frame.hash(&mut hasher);
//        let frame_hash = hasher.finish();
//        if frame_hash != self.last_frame_hash {
//            self.last_frame_hash = frame_hash;
//            let _ = self.save_frame();
//            self.frame = [0x00; SCREEN_WIDTH as usize * SCREEN_HEIGHT as usize * 4];
//            let _ = self.save_tile_data();
//        }
    }

    fn oam_search(&self, ih: &mut InterruptHandler) {
        if self.status.oam_interrupt() {
            self.lcdc_interrupt(ih);
        }
    }

    fn lcdc_interrupt(&self, ih: &mut InterruptHandler) {
        ih.set_interrupt(Interrupt::LCDC);
    }

    fn draw_scanline(&mut self) {
        if self.control.bg_enable() {
            self.draw_bg();
        }

        if self.control.sprite_enable() {
            self.draw_sprites();
        }
    }

    fn draw_bg(&mut self) {
        use TileDataAddressRange::*;
        use TileMapAddressRange::*;
        let in_window = self.control.window_enable() && self.lcd_y >= self.window_y;

        let tile_data_offset = match self.control.tile_data() {
            TileDataAddr8000_8FFF => 0x0000 as u16,
            TileDataAddr8800_97FF => 0x0800 as u16,
        };

        let map = if in_window { self.control.window_map() } else { self.control.bg_map() };
        let tile_map_offset = match map {
            TileMapAddr9800_9BFF => 0x0000 as u16,
            TileMapAddr9C00_9FFF => 0x0400 as u16,
        };

        let map_y = if in_window {
            self.lcd_y - self.window_y
        } else {
            self.scroll_y.wrapping_add(self.lcd_y)
        };

        let tile_y = (map_y / 8) as u16;

        for pixel_x in 0..SCREEN_WIDTH {
            // TODO: optimize this loop to do blocks of 8 pixels?
            // otherwise we calculate the addresses of and read the same bytes 8 times
            let map_x = if in_window && pixel_x >= self.window_x - 7 {
                // translate to window space if we're in it
                pixel_x - (self.window_x - 7)
            } else {
                pixel_x.wrapping_add(self.scroll_x)
            };

            let tile_x = (map_x / 8) as u16;

            let tile_map_addr = tile_map_offset + (tile_y * 32) + tile_x;

            let tile_id = match self.control.tile_data() {
                TileDataAddr8000_8FFF => self.vram_bg_maps[tile_map_addr as usize] as u16,
                TileDataAddr8800_97FF => {
                    (self.vram_bg_maps[tile_map_addr as usize] as i8 as i16 + 128) as u16
                }
            };

            let tile_data_addr = tile_data_offset + (tile_id * 16);
            let tile_row_offset = ((map_y % 8) * 2) as u16;

            let pixel_start = (tile_data_addr + tile_row_offset) as usize;
            let pixel_end = pixel_start + 2;
            let pixel_data = &self.vram_tile_data[pixel_start..pixel_end];

            let pixel_bit = 7 - (map_x % 8);

            let shade = self.get_shade(pixel_data, pixel_bit, &self.bg_palette);
            let pixel = shade.into_pixel();

            let frame_pixel_start =
                (self.lcd_y as usize * SCREEN_WIDTH as usize * 4) + (pixel_x as usize * 4);
            let frame_pixel_end = frame_pixel_start + 4;
            let pixel_slice = &mut self.frame[frame_pixel_start..frame_pixel_end];
            pixel_slice.clone_from_slice(&pixel[..4]);
        }
    }

    fn draw_sprites(&mut self) {
        // set sprite height from control register
        let y_size = match self.control.sprite_size() {
            SpriteSizes::Size8x8 => 8,
            SpriteSizes::Size8x16 => 16,
        };

        for sprite in self.vram_oam.iter() {
            let y_pos = sprite.y_position;
            // skip over this sprite if the current LCD line doesn't intersect it
            if y_pos <= (self.lcd_y + 16 - y_size) || y_pos > (self.lcd_y + 16) {
                continue;
            }

            // calculate the line within the sprite that the current LCD line intersects
            let sprite_line = if sprite.attributes.y_flip() {
                (y_size - 1) - (self.lcd_y + 16 - y_pos)
            } else {
                self.lcd_y + 16 - y_pos
            };

            let sprite_data_start =
                ((sprite.tile_number as u16 * 16) + (sprite_line as u16 * 2)) as usize;
            let sprite_data_end = sprite_data_start + 2;
            let pixel_data = &self.vram_tile_data[sprite_data_start..sprite_data_end];

            for sprite_column in 0..8 {
                let mut pixel_x = sprite.x_position as u16 + sprite_column as u16;
                if pixel_x < 8 || pixel_x >= SCREEN_WIDTH as u16 + 8 {
                     // offscreen, skip
                    continue;
                }
                pixel_x = pixel_x - 8;

                let pixel_bit = if sprite.attributes.x_flip() {
                    sprite_column
                } else {
                    7 - sprite_column
                };

                let palette = match sprite.attributes.palette() {
                    0 => &self.sprite_palette_0,
                    1 => &self.sprite_palette_1,
                    _ => unreachable!(), // 1 bit field
                };

                let palette_index = self.get_palette_index(pixel_data, pixel_bit);
                // palette index 0 is transparent for sprites
                if palette_index == 0 {
                    continue;
                }

                let shade = palette.colour(palette_index);
                let pixel = shade.into_pixel();

                let frame_pixel_start =
                    (self.lcd_y as usize * SCREEN_WIDTH as usize * 4) + (pixel_x as usize * 4);
                let frame_pixel_end = frame_pixel_start + 4;
                let pixel_slice = &mut self.frame[frame_pixel_start..frame_pixel_end];
                pixel_slice.clone_from_slice(&pixel[..4]);
            }
        }
    }

    fn get_palette_index(&self, pixel_data: &[u8], pixel_bit: u8) -> usize {
        let top_bit = (pixel_data[1] >> pixel_bit) & 0b1;
        let bot_bit = (pixel_data[0] >> pixel_bit) & 0b1;
        ((top_bit << 1) | bot_bit) as usize
    }

    fn get_shade(&self, pixel_data: &[u8], pixel_bit: u8, palette: &Palette) -> Shade {
        let palette_index = self.get_palette_index(pixel_data, pixel_bit);
        palette.colour(palette_index)
    }

    fn save_frame(&self) -> Result<(), png::EncodingError> {
        use std::fs::File;
        use std::io::BufWriter;
        use std::path::Path;
        let path = Path::new(r"./frame.png");
        let file = File::create(path)?;
        let ref mut w = BufWriter::new(file);

        let mut png_encoder =
            png::Encoder::new(w, SCREEN_WIDTH as u32, SCREEN_HEIGHT as u32);
        png_encoder.set_color(png::ColorType::RGBA);
        png_encoder.set_depth(png::BitDepth::Eight);
        let mut writer = png_encoder.write_header()?;
        writer.write_image_data(&self.frame)?;

        Ok(())
    }

    fn save_tile_data(&self) -> Result<(), png::EncodingError> {
        let mut tile_pixels = [0x00; 256 * 96 * 4];
        for line in 0..96 {
            let tile_row_offset = (line % 8) * 2;
            for col in 0..256u16 {
                let tile_id = (line / 8) * 32 + (col / 8);
                let tile_data_offset = tile_id * 16;

                let pixel_start = (tile_data_offset + tile_row_offset) as usize;
                let pixel_end = pixel_start + 2;
                let pixel_data = &self.vram_tile_data[pixel_start..pixel_end];

                let pixel_bit = 7 - (col % 8);

                let shade = self.get_shade(pixel_data, pixel_bit as u8, &self.bg_palette);
                let pixel = shade.into_pixel();

                let pixel_start = (line as usize * 256 as usize * 4) + (col as usize * 4);
                let pixel_end = pixel_start + 4;
                let pixel_slice = &mut tile_pixels[pixel_start..pixel_end];
                pixel_slice.clone_from_slice(&pixel[..4]);
            }
        }
        
        use std::path::Path;
        use std::fs::File;
        use std::io::BufWriter;
        let path = Path::new(r"./tiledata.png");
        let file = File::create(path)?;
        let ref mut w = BufWriter::new(file);

        let mut png_encoder = png::Encoder::new(w, 256, 96);
        png_encoder.set_color(png::ColorType::RGBA);
        png_encoder.set_depth(png::BitDepth::Eight);
        let mut writer = png_encoder.write_header()?;
        writer.write_image_data(&tile_pixels)?;

        Ok(())
    }
}