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
//#![no_std]
#![allow(unused)]
#![allow(bad_style)]
#![allow(dead_code)]
#![allow(clippy::match_ref_pats)]
#![allow(clippy::unreadable_literal)]
#![allow(clippy::naive_bytecount)]
#![allow(clippy::single_match)]
#![allow(clippy::cognitive_complexity)]

extern crate alloc;
use alloc::{borrow::Cow, format, string::String, vec, vec::Vec};

use bytemuck::*;

use core::{
  convert::TryFrom,
  mem::{align_of, size_of, size_of_val},
  ops::{Index, IndexMut},
};

// type CowStr = Cow<'static, str>;
// macro_rules! cow_str {
//   ($l:literal) => {
//     // we can borrow on a literal
//     alloc::borrow::Cow::Borrowed($l)
//   };
//   ($i:ident) => {
//     // for an ident we assume it's a const name
//     alloc::borrow::Cow::Borrowed($i)
//   };
//   ($($tokens:tt)*) => {
//     // otherwise we use format on whatever pile of tokens we get
//     alloc::borrow::Cow::Owned(format!($($tokens)*))
//   };
// }

/// Dumps out some bindings to the console.
///
/// * With literal first, uses that as the format string for all bindings to be
///   formatted.
/// * Otherwise it uses `"{:?}"`
macro_rules! dump {
  ($fmt_str:literal, $($n:expr),*) => (if cfg!(debug_assertions) {
    $(println!(
      concat!("{}:{}> ", stringify!($n), ": ", $fmt_str),
      file!(),
      line!(),
      $n);
    )*
  });
  ($($n:expr),*) => (if cfg!(debug_assertions) {
    $(println!(
      concat!("{}:{}> ",stringify!($n),": {:?}"),
      file!(),
      line!(),
      $n);
    )*
  });
}

/// Prints out a message, intended for tracing.
///
/// * With just a literal, prints that message.
/// * With a literal and tokens, uses it as a format string to format the
///   tokens.
macro_rules! trace {
  ($msg:literal) => (if cfg!(debug_assertions) {
    println!(
      concat!("{}:{}> ",$msg),
      file!(),
      line!(),
    );
  });
  ($fmt_str:literal, $($tokens:tt)*) => (if cfg!(debug_assertions) {
    println!(
      concat!("{}:{}> ",$fmt_str),
      file!(),
      line!(),
      $($tokens)*
    );
  });
}

#[cfg(feature = "png")]
pub mod inflate;
#[cfg(feature = "png")]
pub mod png;
#[cfg(feature = "png")]
pub mod zlib;

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
#[repr(C)]
pub struct Pal256 {
  pub i: u8,
}
unsafe impl Zeroable for Pal256 {}
unsafe impl Pod for Pal256 {}

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
#[repr(C)]
pub struct Y8 {
  pub y: u8,
}
unsafe impl Zeroable for Y8 {}
unsafe impl Pod for Y8 {}

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
#[repr(C)]
pub struct YA8 {
  pub y: u8,
  pub a: u8,
}
unsafe impl Zeroable for YA8 {}
unsafe impl Pod for YA8 {}

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
#[repr(C)]
pub struct RGB8 {
  pub r: u8,
  pub g: u8,
  pub b: u8,
}
unsafe impl Zeroable for RGB8 {}
unsafe impl Pod for RGB8 {}
impl RGB8 {
  pub fn as_rgba(self) -> RGBA8 {
    RGBA8 {
      r:self.r,
      g:self.g,
      b:self.b,
      a: 0xFF,
    }
  }
}

/// Red/Green/Blue/Alpha, 8-bit channels.
///
/// Data should be in the `sRGB` colorspace. If you need an alternate colorspace
/// you should newtype this type with a wrapper type to make that clear.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
#[repr(C)]
pub struct RGBA8 {
  pub r: u8,
  pub g: u8,
  pub b: u8,
  pub a: u8,
}
unsafe impl Zeroable for RGBA8 {}
unsafe impl Pod for RGBA8 {}

/// A grid of [`RGBA8`] with a `width` and `height`.
///
/// * Due to conventions, the `(0,0)` position is the TOP LEFT.
#[derive(Debug, Clone)]
pub struct BitmapRGBA8 {
  pub pixels: Vec<RGBA8>,
  pub width: usize,
  pub height: usize,
}
// Index a pixel by (usize,usize)
impl Index<(usize, usize)> for BitmapRGBA8 {
  type Output = RGBA8;
  #[inline]
  fn index(&self, (x, y): (usize, usize)) -> &RGBA8 {
    &self.pixels[x + y * self.width]
  }
}
impl IndexMut<(usize, usize)> for BitmapRGBA8 {
  #[inline]
  fn index_mut(&mut self, (x, y): (usize, usize)) -> &mut RGBA8 {
    &mut self.pixels[x + y * self.width]
  }
}
// Index a pixel by (u32,u32)
#[cfg(any(target_pointer_width = "32", target_pointer_width = "64"))]
impl Index<(u32, u32)> for BitmapRGBA8 {
  type Output = RGBA8;
  #[inline]
  fn index(&self, (x, y): (u32, u32)) -> &RGBA8 {
    &self[(x as usize, y as usize)]
  }
}
#[cfg(any(target_pointer_width = "32", target_pointer_width = "64"))]
impl IndexMut<(u32, u32)> for BitmapRGBA8 {
  #[inline]
  fn index_mut(&mut self, (x, y): (u32, u32)) -> &mut RGBA8 {
    &mut self[(x as usize, y as usize)]
  }
}
// Index a scanline by usize
impl Index<usize> for BitmapRGBA8 {
  type Output = [RGBA8];
  #[inline]
  fn index(&self, y: usize) -> &[RGBA8] {
    &self.pixels[y * self.width..self.width]
  }
}
impl IndexMut<usize> for BitmapRGBA8 {
  #[inline]
  fn index_mut(&mut self, y: usize) -> &mut [RGBA8] {
    &mut self.pixels[y * self.width..self.width]
  }
}
// Index a scanline by u32
#[cfg(any(target_pointer_width = "32", target_pointer_width = "64"))]
impl Index<u32> for BitmapRGBA8 {
  type Output = [RGBA8];
  #[inline]
  fn index(&self, y: u32) -> &[RGBA8] {
    &self[y as usize]
  }
}
#[cfg(any(target_pointer_width = "32", target_pointer_width = "64"))]
impl IndexMut<u32> for BitmapRGBA8 {
  #[inline]
  fn index_mut(&mut self, y: u32) -> &mut [RGBA8] {
    &mut self[y as usize]
  }
}