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
extern crate byteorder;
use self::byteorder::{ByteOrder, BigEndian};

extern crate image;
use self::image::GenericImage;

extern crate itertools;
use self::itertools::Itertools;

use checksum::checksum;

fn gametitle() -> [u8; 32] {
    let mut gametitle: [u8; 32] = [0x00; 32];
    let fzgx = "F-Zero GX".as_bytes();

    for i in 0..fzgx.len() {
        gametitle[i] = fzgx[i];
    }

    gametitle
}

fn read_block(emblem_data: &mut Vec<u8>, image: &image::DynamicImage,
  alpha_threshold: i8, i: u32, j: u32) {
    let i = i as u32;
    let j = j as u32;

    for x in i..i+4 {
        for y in j..j+4 {
            let pixel = image.get_pixel(x, y).data;
            let r = pixel[0];
            let g = pixel[1];
            let b = pixel[2];
            let a = pixel[3];

            match a < alpha_threshold as u8 {
                true => { emblem_data.push(0x00); },
                false => {
                    let red   = (r / 8) as u16;
                    let green = (g / 8) as u16;
                    let blue  = (b / 8) as u16;
                    let alpha: u16 = 1;
                    let value: u16 = 32768*alpha + 1024*red + 32*green + blue;

                    let mut buf: [u8; 2] = [0x00; 2];
                    byteorder::BigEndian::write_u16(&mut buf, value as u16);

                    for byte in buf.iter() {
                        emblem_data.push(*byte);
                    }
                }
            }
        }
    }
}



const FZGX: [u8; 4] = *b"GFZE";
const SEGA: [u8; 2] = *b"8P";


pub struct Emblem {
  gamecode:     [u8; 4],  // GFZE
  company:      [u8; 2],  // 8P
  reserved01:    u8,      // 0xFF
  banner_fmt:    u8,      // 0x02
  filename:     [u8; 32],
  timestamp:    [u8; 4],
  icon_addr:    [u8; 4], // 0x00 0x00 0x00 0x60
  icon_fmt:     [u8; 2], // 0x00 0x02
  icon_speed:   [u8; 2], // 0x00 0x03
  permission:    u8,
  copy_counter:  u8,
  index:        [u8; 2],
  filesize8:    [u8; 2], // 0x00 0x03
  reserved02:   [u8; 2], // 0xFF 0xFF
  comment_addr: [u8; 4], // 0x00 0x00 0x00 0x04

  pub checksum:     [u8; 2],
  something3:   [u8; 2], // 0x04 0x01
  game_title:   [u8; 32], // "F-ZERO GX" 0x00...
  file_comment: [u8; 60], // "YY/MM/DD HH:MM" 0x00...
  pub banner_data:  [u8; 6144], // banner pixel data (92 x 32 px)
  icon_data:    [u8; 2048], // icon pixel data (64 x 64 px)
  emblem_data:  [u8; 8192], // emblem pixel data (64 x 64 px)
  padding:      [u8; 8096] // 0x00 padding
}

impl Default for Emblem {
    fn default() -> Self {
        let game_title = gametitle();

        Emblem {
          gamecode:      FZGX,
          company:       SEGA,
          reserved01:    0xFF,
          banner_fmt:    0x02,
          filename:     [0x00; 32],
          timestamp:    [0x00; 4],
          icon_addr:    [0x00, 0x00, 0x00, 0x60],
          icon_fmt:     [0x00, 0x02],
          icon_speed:   [0x00, 0x03],
          permission:    0x04,
          copy_counter:  0x00,
          index:        [0x00, 0x00],
          filesize8:    [0x00, 0x03],
          reserved02:   [0xFF, 0xFF],
          comment_addr: [0x00, 0x00, 0x00, 0x04],

          checksum:     [0x00; 2],
          something3:   [0x04, 0x01],
          game_title:   game_title, // "F-ZERO GX" 0x00...
          file_comment: [0x00; 60], // "YY/MM/DD HH:MM" 0x00...

          banner_data:  [0x00; 6144], // banner pixel data (92 x 32 px)
          icon_data:    [0x00; 2048], // icon pixel data (64 x 64 px)
          emblem_data:  [0x00; 8192], // emblem pixel data (64 x 64 px)
          padding:      [0x00; 8096] // 0x00 padding
        }
    }
}

impl Emblem {
    pub fn set_filename(self: &mut Self, filename: String) {
        let bytes = filename.as_bytes();

        for i in 0..bytes.len() {
            self.filename[i] = bytes[i];
        }
    }

    pub fn set_timestamp(self: &mut Self, time: u32) {
        let mut buf = [0x00; 4];
        byteorder::BigEndian::write_u32(&mut buf, time);

        self.timestamp = buf;
    }

    pub fn set_comment(self: &mut Self, comment: String) {
        let comment_bytes = comment.as_bytes();

        for i in 0..comment_bytes.len() {
            self.file_comment[i] = comment_bytes[i];
        }
    }

    pub fn set_emblem_data(self: &mut Self, image: image::DynamicImage, alpha_threshold: i8) {
        let mut v = Vec::new();

        for block_row in (0..image.width()).step(4) {
            for block_col in (0..image.width()).step(4) {
                read_block(&mut v, &image, alpha_threshold, block_row, block_col);
            }
        }

        for i in 0..v.len() {
            self.emblem_data[i] = v[i];
        }
    }

    pub fn set_icon_data(self: &mut Self) {
        let icon = include_bytes!("../data/emblem_icon");

        for i in 0..icon.len() {
            self.icon_data[i] = icon[i];
        }
    }

    pub fn set_banner_data(self: &mut Self, image: image::DynamicImage, alpha_threshold: i8) {
        let mut v: Vec<u8> = Vec::new();
        let banner_file = include_bytes!("../data/emblem_banner_base");
        let mut chunked_banner = banner_file.chunks(0x200);

        for block_row in (0..32).step(4) {
            for chunk in chunked_banner.nth(0) {
                for byte in chunk {
                    v.push(*byte);
                }
            }

            for block_col in (0..32).step(4) {
                read_block(&mut v, &image, alpha_threshold, block_row, block_col);
            }
        }

        for i in 0..v.len() {
            self.banner_data[i] = v[i];
        }
    }

    pub fn set_checksum(self: &mut Self) {
        let mut v = Vec::new();

        for byte in self.something3.iter() {
            v.push(byte);
        }
        for byte in self.game_title.iter() {
            v.push(byte);
        }
        for byte in self.file_comment.iter() {
            v.push(byte);
        }
        for byte in self.banner_data.iter() {
            v.push(byte);
        }
        for byte in self.icon_data.iter() {
            v.push(byte);
        }
        for byte in self.emblem_data.iter() {
            v.push(byte);
        }
        for byte in self.padding.iter() {
            v.push(byte);
        }

        self.checksum = checksum(v);
    }

    pub fn as_bytes(self: Self) -> [u8; 24640] {
        let mut v: [u8; 24640] = [0x00; 24640];
        let mut index = 0;

        for byte in self.gamecode.iter() {
            v[index] = *byte;
            index += 1;
        }
        for byte in self.company.iter() {
            v[index] = *byte;
            index += 1;
        }

        v[index] = self.reserved01;
        index += 1;
        v[index] = self.banner_fmt;
        index += 1;

        for byte in self.filename.iter() {
            v[index] = *byte;
            index += 1;
        }
        for byte in self.timestamp.iter() {
            v[index] = *byte;
            index += 1;
        }
        for byte in self.icon_addr.iter() {
            v[index] = *byte;
            index += 1;
        }
        for byte in self.icon_fmt.iter() {
            v[index] = *byte;
            index += 1;
        }
        for byte in self.icon_speed.iter() {
            v[index] = *byte;
            index += 1;
        }

        v[index] = self.permission;
        index += 1;
        v[index] = self.copy_counter;
        index += 1;

        for byte in self.index.iter() {
            v[index] = *byte;
            index += 1;
        }
        for byte in self.filesize8.iter() {
            v[index] = *byte;
            index += 1;
        }
        for byte in self.reserved02.iter() {
            v[index] = *byte;
            index += 1;
        }
        for byte in self.comment_addr.iter() {
            v[index] = *byte;
            index += 1;
        }
        for byte in self.checksum.iter() {
            v[index] = *byte;
            index += 1;
        }
        for byte in self.something3.iter() {
            v[index] = *byte;
            index += 1;
        }
        for byte in self.game_title.iter() {
            v[index] = *byte;
            index += 1;
        }
        for byte in self.file_comment.iter() {
            v[index] = *byte;
            index += 1;
        }
        for byte in self.banner_data.iter() {
            v[index] = *byte;
            index += 1;
        }
        for byte in self.icon_data.iter() {
            v[index] = *byte;
            index += 1;
        }
        for byte in self.emblem_data.iter() {
            v[index] = *byte;
            index += 1;
        }
        for byte in self.padding.iter() {
            v[index] = *byte;
            index += 1;
        }

        v
    }
}