yaze 0.2.0
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
compression.h
Go to the documentation of this file.
1#ifndef YAZE_APP_GFX_COMPRESSION_H
2#define YAZE_APP_GFX_COMPRESSION_H
3
4#include <memory>
5#include <string>
6
7#include "absl/status/status.h"
8#include "absl/status/statusor.h"
10
11#define BUILD_HEADER(command, length) (command << 5) + (length - 1)
12
13namespace yaze {
14namespace app {
15namespace gfx {
16
21namespace lc_lz2 {
22
23const int D_NINTENDO_C_MODE1 = 0;
24const int D_NINTENDO_C_MODE2 = 1;
25
26const int D_CMD_COPY = 0;
27const int D_CMD_BYTE_REPEAT = 1;
28const int D_CMD_WORD_REPEAT = 2;
29const int D_CMD_BYTE_INC = 3;
30const int D_CMD_COPY_EXISTING = 4;
31
32const int D_MAX_NORMAL_LENGTH = 32;
33const int D_MAX_LENGTH = 1024;
34
35const int INITIAL_ALLOC_SIZE = 1024;
36
37constexpr int kCommandDirectCopy = 0;
38constexpr int kCommandByteFill = 1;
39constexpr int kCommandWordFill = 2;
40constexpr int kCommandIncreasingFill = 3;
41constexpr int kCommandRepeatingBytes = 4;
42constexpr int kCommandLongLength = 7;
43constexpr int kMaxLengthNormalHeader = 32;
44constexpr int kMaxLengthCompression = 1024;
45constexpr int kNintendoMode1 = 0;
46constexpr int kNintendoMode2 = 1;
47constexpr int kSnesByteMax = 0xFF;
48constexpr int kCommandMod = 0x07;
49constexpr int kExpandedMod = 0xE0;
50constexpr int kExpandedLengthMod = 0x3FF;
51constexpr int kNormalLengthMod = 0x1F;
52constexpr int kCompressionStringMod = 7 << 5;
53
54// Represents a command in the compression algorithm.
56 // The command arguments for each possible command.
57 std::array<std::array<char, 2>, 5> arguments;
58
59 // The size of each possible command.
60 std::array<uint, 5> cmd_size;
61
62 // The size of the data processed by each possible command.
63 std::array<uint, 5> data_size;
64};
65
66using CommandArgumentArray = std::array<std::array<char, 2>, 5>;
67using CommandSizeArray = std::array<uint, 5>;
68using DataSizeArray = std::array<uint, 5>;
69
70// Represents a piece of compressed data.
72 char command;
73 int length;
75 std::string argument;
76 std::shared_ptr<CompressionPiece> next = nullptr;
77 CompressionPiece() = default;
78 CompressionPiece(int cmd, int len, std::string args, int arg_len)
79 : command(cmd), length(len), argument_length(arg_len), argument(args) {}
80};
82using CompressionPiecePointer = std::shared_ptr<CompressionPiece>;
83
85
87
88// Compression V1
89
90void CheckByteRepeat(const uchar* rom_data, DataSizeArray& data_size_taken,
91 CommandArgumentArray& cmd_args, uint& src_data_pos,
92 const uint last_pos);
93
94void CheckWordRepeat(const uchar* rom_data, DataSizeArray& data_size_taken,
95 CommandArgumentArray& cmd_args, uint& src_data_pos,
96 const uint last_pos);
97
98void CheckIncByte(const uchar* rom_data, DataSizeArray& data_size_taken,
99 CommandArgumentArray& cmd_args, uint& src_data_pos,
100 const uint last_pos);
101
102void CheckIntraCopy(const uchar* rom_data, DataSizeArray& data_size_taken,
103 CommandArgumentArray& cmd_args, uint& src_data_pos,
104 const uint last_pos, uint start);
105
106void ValidateForByteGain(const DataSizeArray& data_size_taken,
107 const CommandSizeArray& cmd_size, uint& max_win,
108 uint& cmd_with_max);
109
110void CompressionCommandAlternative(const uchar* rom_data,
111 CompressionPiecePointer& compressed_chain,
112 const CommandSizeArray& cmd_size,
113 const CommandArgumentArray& cmd_args,
114 uint& src_data_pos, uint& comp_accumulator,
115 uint& cmd_with_max, uint& max_win);
116
117// Compression V2
118
119void CheckByteRepeatV2(const uchar* data, uint& src_pos, const uint last_pos,
120 CompressionCommand& cmd);
121
122void CheckWordRepeatV2(const uchar* data, uint& src_pos, const uint last_pos,
123 CompressionCommand& cmd);
124
125void CheckIncByteV2(const uchar* data, uint& src_pos, const uint last_pos,
126 CompressionCommand& cmd);
127
128void CheckIntraCopyV2(const uchar* data, uint& src_pos, const uint last_pos,
129 uint start, CompressionCommand& cmd);
130
131void ValidateForByteGainV2(const CompressionCommand& cmd, uint& max_win,
132 uint& cmd_with_max);
133
135 const CompressionCommand& cmd,
136 CompressionPiecePointer& compressed_chain,
137 uint& src_pos, uint& comp_accumulator,
138 uint& cmd_with_max, uint& max_win);
139
144absl::StatusOr<std::vector<uint8_t>> CompressV2(const uchar* data, const int start,
145 const int length, int mode = 1,
146 bool check = false);
147
148absl::StatusOr<std::vector<uint8_t>> CompressGraphics(const uchar* data, const int pos,
149 const int length);
150absl::StatusOr<std::vector<uint8_t>> CompressOverworld(const uchar* data, const int pos,
151 const int length);
152absl::StatusOr<std::vector<uint8_t>> CompressOverworld(const std::vector<uint8_t> data,
153 const int pos, const int length);
154
155absl::StatusOr<CompressionPiecePointer> SplitCompressionPiece(
156 CompressionPiecePointer& piece, int mode);
157
158std::vector<uint8_t> CreateCompressionString(CompressionPiecePointer& start, int mode);
159
161 int mode, int start, int src_data_pos);
162
164
165// Compression V3
166
168 std::vector<uint8_t> data;
169 std::vector<uint8_t> compressed_data;
170 std::vector<CompressionPiece> compression_pieces;
171 std::vector<uint8_t> compression_string;
179 int mode;
180
181 // Constructor to initialize the context
182 CompressionContext(const std::vector<uint8_t>& data_, const int start,
183 const int length)
184 : data(data_), src_pos(start), last_pos(start + length - 1), mode(0) {}
185
186 // Constructor to initialize the context
187 CompressionContext(const std::vector<uint8_t>& data_, const int start,
188 const int length, int mode_)
189 : data(data_),
190 src_pos(start),
191 last_pos(start + length - 1),
192 mode(mode_) {}
193};
194
195void CheckByteRepeatV3(CompressionContext& context);
196void CheckWordRepeatV3(CompressionContext& context);
197void CheckIncByteV3(CompressionContext& context);
198void CheckIntraCopyV3(CompressionContext& context);
199
200void InitializeCompression(CompressionContext& context);
201void CheckAvailableCompressionCommands(CompressionContext& context);
202void DetermineBestCompression(CompressionContext& context);
203void HandleDirectCopy(CompressionContext& context);
204void AddCompressionToChain(CompressionContext& context);
205absl::Status ValidateCompressionResultV3(const CompressionContext& context);
206
207absl::StatusOr<CompressionPiece> SplitCompressionPieceV3(
208 CompressionPiece& piece, int mode);
209void FinalizeCompression(CompressionContext& context);
210
215absl::StatusOr<std::vector<uint8_t>> CompressV3(const std::vector<uint8_t>& data,
216 const int start, const int length,
217 int mode = 1, bool check = false);
218
219// Hyrule Magic
220uint8_t* Compress(uint8_t const* const src, int const oldsize, int* const size,
221 int const flag);
222
223uint8_t* Uncompress(uint8_t const* src, int* const size,
224 int const p_big_endian);
225
226// Decompression
227
228std::string SetBuffer(const std::vector<uint8_t>& data, int src_pos,
229 int comp_accumulator);
230std::string SetBuffer(const uchar* data, int src_pos, int comp_accumulator);
231void memfill(const uchar* data, std::vector<uint8_t>& buffer, int buffer_pos, int offset,
232 int length);
233
238absl::StatusOr<std::vector<uint8_t>> DecompressV2(const uchar* data, int offset,
239 int size = 0x800, int mode = 1);
240absl::StatusOr<std::vector<uint8_t>> DecompressGraphics(const uchar* data, int pos, int size);
241absl::StatusOr<std::vector<uint8_t>> DecompressOverworld(const uchar* data, int pos, int size);
242absl::StatusOr<std::vector<uint8_t>> DecompressOverworld(const std::vector<uint8_t> data,
243 int pos, int size);
244
245} // namespace lc_lz2
246
247} // namespace gfx
248} // namespace app
249} // namespace yaze
250
251#endif // YAZE_APP_GFX_COMPRESSION_H
unsigned int uint
Definition constants.h:113
unsigned char uchar
Definition constants.h:114
constexpr int kCommandMod
Definition compression.h:48
constexpr int kExpandedMod
Definition compression.h:49
void DetermineBestCompression(CompressionContext &context)
std::vector< uint8_t > CreateCompressionString(CompressionPiecePointer &start, int mode)
absl::StatusOr< std::vector< uint8_t > > CompressOverworld(const uchar *data, const int pos, const int length)
void ValidateForByteGain(const DataSizeArray &data_size_taken, const CommandSizeArray &cmd_size, uint &max_win, uint &cmd_with_max)
constexpr int kNintendoMode2
Definition compression.h:46
constexpr int kSnesByteMax
Definition compression.h:47
void CheckIntraCopyV2(const uchar *rom_data, uint &src_data_pos, const uint last_pos, uint start, CompressionCommand &cmd)
constexpr int kNormalLengthMod
Definition compression.h:51
const int D_CMD_COPY_EXISTING
Definition compression.h:30
constexpr int kCompressionStringMod
Definition compression.h:52
absl::StatusOr< std::vector< uint8_t > > DecompressGraphics(const uchar *data, int pos, int size)
void CheckIntraCopy(const uchar *rom_data, DataSizeArray &data_size_taken, CommandArgumentArray &cmd_args, uint &src_data_pos, const uint last_pos, uint start)
void CheckWordRepeatV3(CompressionContext &context)
absl::StatusOr< std::vector< uint8_t > > CompressV3(const std::vector< uint8_t > &data, const int start, const int length, int mode, bool check)
Compresses a buffer of data using the LC_LZ2 algorithm.
void CheckIncByteV3(CompressionContext &context)
void CheckIncByteV2(const uchar *rom_data, uint &src_data_pos, const uint last_pos, CompressionCommand &cmd)
absl::StatusOr< std::vector< uint8_t > > DecompressOverworld(const uchar *data, int pos, int size)
const int D_CMD_BYTE_REPEAT
Definition compression.h:27
void AddCompressionToChain(CompressionContext &context)
void PrintCompressionPiece(const CompressionPiecePointer &piece)
void InitializeCompression(CompressionContext &context)
constexpr int kCommandDirectCopy
Definition compression.h:37
std::array< uint, 5 > CommandSizeArray
Definition compression.h:67
constexpr int kMaxLengthNormalHeader
Definition compression.h:43
void CheckIntraCopyV3(CompressionContext &context)
void CheckWordRepeat(const uchar *rom_data, DataSizeArray &data_size_taken, CommandArgumentArray &cmd_args, uint &src_data_pos, const uint last_pos)
const int D_MAX_NORMAL_LENGTH
Definition compression.h:32
absl::Status ValidateCompressionResult(CompressionPiecePointer &chain_head, int mode, int start, int src_data_pos)
absl::StatusOr< std::vector< uint8_t > > CompressV2(const uchar *data, const int start, const int length, int mode, bool check)
Compresses a buffer of data using the LC_LZ2 algorithm.
void memfill(const uchar *data, std::vector< uint8_t > &buffer, int buffer_pos, int offset, int length)
CompressionPiecePointer MergeCopy(CompressionPiecePointer &start)
std::array< std::array< char, 2 >, 5 > CommandArgumentArray
Definition compression.h:66
absl::StatusOr< std::vector< uint8_t > > DecompressV2(const uchar *data, int offset, int size, int mode)
Decompresses a buffer of data using the LC_LZ2 algorithm.
void CheckByteRepeat(const uchar *rom_data, DataSizeArray &data_size_taken, CommandArgumentArray &cmd_args, uint &src_data_pos, const uint last_pos)
absl::StatusOr< CompressionPiecePointer > SplitCompressionPiece(CompressionPiecePointer &piece, int mode)
void CheckWordRepeatV2(const uchar *data, uint &src_pos, const uint last_pos, CompressionCommand &cmd)
constexpr int kNintendoMode1
Definition compression.h:45
constexpr int kExpandedLengthMod
Definition compression.h:50
uint8_t * Uncompress(uint8_t const *src, int *const size, int const p_big_endian)
void CheckAvailableCompressionCommands(CompressionContext &context)
uint8_t * Compress(uint8_t const *const src, int const oldsize, int *const size, int const flag)
constexpr int kMaxLengthCompression
Definition compression.h:44
void CheckByteRepeatV2(const uchar *data, uint &src_pos, const uint last_pos, CompressionCommand &cmd)
void CompressionCommandAlternativeV2(const uchar *rom_data, const CompressionCommand &cmd, CompressionPiecePointer &compressed_chain, uint &src_data_pos, uint &comp_accumulator, uint &cmd_with_max, uint &max_win)
std::array< uint, 5 > DataSizeArray
Definition compression.h:68
absl::StatusOr< CompressionPiece > SplitCompressionPieceV3(CompressionPiece &piece, int mode)
const int D_CMD_WORD_REPEAT
Definition compression.h:28
void ValidateForByteGainV2(const CompressionCommand &cmd, uint &max_win, uint &cmd_with_max)
const int INITIAL_ALLOC_SIZE
Definition compression.h:35
void CheckIncByte(const uchar *rom_data, DataSizeArray &data_size_taken, CommandArgumentArray &cmd_args, uint &src_data_pos, const uint last_pos)
absl::StatusOr< std::vector< uint8_t > > CompressGraphics(const uchar *data, const int pos, const int length)
void HandleDirectCopy(CompressionContext &context)
const int D_NINTENDO_C_MODE1
Definition compression.h:23
std::string SetBuffer(const uchar *data, int src_pos, int comp_accumulator)
void CompressionCommandAlternative(const uchar *rom_data, CompressionPiecePointer &compressed_chain, const CommandSizeArray &cmd_size, const CommandArgumentArray &cmd_args, uint &src_data_pos, uint &comp_accumulator, uint &cmd_with_max, uint &max_win)
void FinalizeCompression(CompressionContext &context)
void PrintCompressionChain(const CompressionPiecePointer &chain_head)
const int D_NINTENDO_C_MODE2
Definition compression.h:24
void CheckByteRepeatV3(CompressionContext &context)
std::shared_ptr< CompressionPiece > CompressionPiecePointer
Definition compression.h:82
struct CompressionPiece CompressionPiece
Definition compression.h:81
absl::Status ValidateCompressionResultV3(const CompressionContext &context)
Definition common.cc:22
std::array< std::array< char, 2 >, 5 > arguments
Definition compression.h:57
CompressionContext(const std::vector< uint8_t > &data_, const int start, const int length)
CompressionContext(const std::vector< uint8_t > &data_, const int start, const int length, int mode_)
std::vector< CompressionPiece > compression_pieces
CompressionPiece(int cmd, int len, std::string args, int arg_len)
Definition compression.h:78
std::shared_ptr< CompressionPiece > next
Definition compression.h:76