-
Notifications
You must be signed in to change notification settings - Fork 1
/
algorithm_1.h
69 lines (55 loc) · 1.38 KB
/
algorithm_1.h
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
#ifndef ALGORITHM_1_H
#define ALGORITHM_1_H
#include "bankopladeformat/bankopladeformat.h"
#include "bitio.h"
void a1_write_4bit(unsigned int c, FILE *out) {
return write_bits(4, c, out);
}
unsigned int a1_read_4bit(FILE *in) {
return read_bits(4, in);
}
void a1_compress(FILE *out, FILE *in) {
struct board board;
struct banko_reader reader;
banko_reader_open(&reader, in);
while (banko_reader_board(&reader, &board) == 0) {
for (int row = 0; row < BOARD_ROWS; row++) {
for (int col = 0; col < BOARD_COLS; col++) {
uint8_t cell = board.cells[row][col];
if (cell == 0) {
a1_write_4bit(0, out);
} else {
a1_write_4bit(cell + 1 - col*10, out);
}
}
}
}
flush_bit(out);
banko_reader_close(&reader);
}
void a1_decompress(FILE *out, FILE *in) {
struct board board;
struct banko_writer writer;
banko_writer_open(&writer, out);
int c;
while (1) {
for (int row = 0; row < BOARD_ROWS; row++) {
for (int col = 0; col < BOARD_COLS; col++) {
uint8_t cell = a1_read_4bit(in);
if (cell != 0) {
board.cells[row][col] = cell - 1 + col*10;
} else {
board.cells[row][col] = 0;
}
}
}
banko_writer_board(&writer, &board);
c = fgetc(in);
ungetc(c, in);
if (c == EOF) {
break;
}
}
banko_writer_close(&writer);
}
#endif