Refactor LC_LZ2 and expand GraphicsEditor

Add CompressV2 and DecompressV2
Add PortablePalette to PaletteEditor
This commit is contained in:
scawful
2023-07-22 15:24:34 -04:00
parent 82dd9dde1b
commit 01802d1a73
9 changed files with 564 additions and 27 deletions

View File

@@ -9,6 +9,7 @@
#include "absl/status/statusor.h"
#include "app/editor/palette_editor.h"
#include "app/gfx/bitmap.h"
#include "app/gfx/compression.h"
#include "app/gfx/snes_tile.h"
#include "app/gui/canvas.h"
#include "app/gui/input.h"
@@ -19,9 +20,10 @@ namespace app {
namespace editor {
absl::Status GraphicsEditor::Update() {
BEGIN_TABLE("#gfxEditTable", 2, gfx_edit_flags)
BEGIN_TABLE("#gfxEditTable", 3, gfx_edit_flags)
SETUP_COLUMN("Graphics Manager")
SETUP_COLUMN("Memory Editor")
SETUP_COLUMN("Preview")
TABLE_HEADERS()
NEXT_COLUMN()
@@ -33,13 +35,14 @@ absl::Status GraphicsEditor::Update() {
RETURN_IF_ERROR(DrawClipboardImport())
END_TAB_ITEM()
END_TAB_BAR()
ImGui::Separator();
ImGui::Text("Graphics");
ImGui::Separator();
RETURN_IF_ERROR(DrawDecompressedData())
RETURN_IF_ERROR(DrawPaletteControls())
NEXT_COLUMN()
RETURN_IF_ERROR(DrawMemoryEditor())
NEXT_COLUMN()
RETURN_IF_ERROR(DrawDecompressedData())
END_TABLE()
return absl::OkStatus();
@@ -95,6 +98,45 @@ absl::Status GraphicsEditor::DrawFileImport() {
return absl::OkStatus();
}
absl::Status GraphicsEditor::DrawPaletteControls() {
ImGui::Separator();
ImGui::Text("Palette");
ImGui::Separator();
ImGui::Combo("Palette", &current_palette_, kPaletteGroupAddressesKeys,
IM_ARRAYSIZE(kPaletteGroupAddressesKeys));
ImGui::InputText("COL File", col_file_path_, sizeof(col_file_path_));
ImGui::SameLine();
// Open the file dialog when the user clicks the "Browse" button
if (ImGui::Button("Browse")) {
ImGuiFileDialog::Instance()->OpenDialog("ImportColKey", "Choose File",
".col\0", ".");
}
if (ImGuiFileDialog::Instance()->Display("ImportColKey")) {
if (ImGuiFileDialog::Instance()->IsOk()) {
strncpy(col_file_path_,
ImGuiFileDialog::Instance()->GetFilePathName().c_str(),
sizeof(col_file_path_));
RETURN_IF_ERROR(temp_rom_.LoadFromFile(col_file_path_, /*z3_load=*/false))
auto col_data_ = gfx::GetColFileData(temp_rom_.data());
col_file_palette_ = gfx::SNESPalette(col_data_);
col_file_ = true;
is_open_ = true;
}
// Close the modal
ImGuiFileDialog::Instance()->Close();
}
if (col_file_palette_.size() != 0) {
palette_editor_.DrawPortablePalette(col_file_palette_);
}
return absl::OkStatus();
}
absl::Status GraphicsEditor::DrawClipboardImport() {
static Bytes clipboard_data;
@@ -114,7 +156,6 @@ absl::Status GraphicsEditor::DrawMemoryEditor() {
std::string title = "Memory Editor";
if (is_open_) {
static MemoryEditor mem_edit;
// mem_edit.DrawWindow(title.data(), (void *)&temp_rom_, temp_rom_.size());
mem_edit.DrawContents(temp_rom_.data(), temp_rom_.size());
}
return absl::OkStatus();
@@ -136,17 +177,22 @@ absl::Status GraphicsEditor::DrawDecompressedData() {
}
absl::Status GraphicsEditor::DecompressImportData(int size) {
ASSIGN_OR_RETURN(import_data_, temp_rom_.Decompress(current_offset_, size))
ASSIGN_OR_RETURN(import_data_, gfx::lc_lz2::DecompressV2(
temp_rom_.data(), current_offset_, size))
std::cout << "Size of import data " << import_data_.size() << std::endl;
Bytes new_sheet;
auto converted_sheet = gfx::SnesTo8bppSheet(import_data_, 3);
bitmap_.Create(core::kTilesheetWidth, 0x2000, core::kTilesheetDepth,
import_data_.data(), size);
converted_sheet.data(), size);
if (rom_.isLoaded()) {
auto palette_group = rom_.GetPaletteGroup("ow_main");
palette_ = palette_group.palettes[current_palette_];
bitmap_.ApplyPalette(palette_);
if (col_file_) {
bitmap_.ApplyPalette(col_file_palette_);
} else {
bitmap_.ApplyPalette(palette_);
}
}
rom_.RenderBitmap(&bitmap_);
@@ -161,7 +207,6 @@ absl::Status GraphicsEditor::DecompressSuperDonkey() {
std::stoi(offset, nullptr, 16); // convert hex string to int
ASSIGN_OR_RETURN(auto decompressed_data,
temp_rom_.Decompress(offset_value, 0x1000))
}
for (const auto& offset : kSuperDonkeySprites) {

View File

@@ -40,13 +40,21 @@ const std::string kSuperDonkeySprites[] = {
"BE115", "BE5C2", "BEB63", "BF0CB", "BF607", "BFA55", "BFD71", "C017D",
"C0567", "C0981", "C0BA7", "C116D", "C166A", "C1FE0", "C24CE", "C2B19"};
constexpr char* kPaletteGroupAddressesKeys[] = {
"ow_main", "ow_aux", "ow_animated", "hud",
"global_sprites", "armors", "swords", "shields",
"sprites_aux1", "sprites_aux2", "sprites_aux3", "dungeon_main",
"grass", "3d_object", "ow_mini_map",
};
class GraphicsEditor {
public:
absl::Status Update();
void SetupROM(ROM &rom) { rom_ = rom; }
void SetupROM(ROM& rom) { rom_ = rom; }
private:
absl::Status DrawFileImport();
absl::Status DrawPaletteControls();
absl::Status DrawClipboardImport();
absl::Status DrawMemoryEditor();
absl::Status DrawDecompressedData();
@@ -61,7 +69,9 @@ class GraphicsEditor {
bool gfx_loaded_ = false;
bool is_open_ = false;
bool super_donkey_ = false;
bool col_file_ = false;
char file_path_[256];
char col_file_path_[256];
ROM rom_;
ROM temp_rom_;
@@ -76,6 +86,7 @@ class GraphicsEditor {
MemoryEditor memory_editor_;
gfx::SNESPalette palette_;
gfx::SNESPalette col_file_palette_;
ImGuiTableFlags gfx_edit_flags = ImGuiTableFlags_Reorderable |
ImGuiTableFlags_Resizable |

View File

@@ -190,6 +190,49 @@ void PaletteEditor::DisplayPalette(gfx::SNESPalette& palette, bool loaded) {
}
}
void PaletteEditor::DrawPortablePalette(gfx::SNESPalette& palette) {
static bool init = false;
if (!init) {
for (int n = 0; n < palette.size_; n++) {
saved_palette_[n].x = palette.GetColor(n).rgb.x / 255;
saved_palette_[n].y = palette.GetColor(n).rgb.y / 255;
saved_palette_[n].z = palette.GetColor(n).rgb.z / 255;
saved_palette_[n].w = 255; // Alpha
}
init = true;
}
if (ImGuiID child_id = ImGui::GetID((void*)(intptr_t)3);
ImGui::BeginChild(child_id, ImGui::GetContentRegionAvail(), true,
ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
ImGui::BeginGroup(); // Lock X position
ImGui::Text("Palette");
for (int n = 0; n < IM_ARRAYSIZE(saved_palette_); n++) {
ImGui::PushID(n);
if ((n % 8) != 0) ImGui::SameLine(0.0f, ImGui::GetStyle().ItemSpacing.y);
if (ImGui::ColorButton("##palette", saved_palette_[n],
palette_button_flags_2, ImVec2(20, 20)))
ImVec4(saved_palette_[n].x, saved_palette_[n].y, saved_palette_[n].z,
1.0f); // Preserve alpha!
if (ImGui::BeginDragDropTarget()) {
if (const ImGuiPayload* payload =
ImGui::AcceptDragDropPayload(IMGUI_PAYLOAD_TYPE_COLOR_3F))
memcpy((float*)&saved_palette_[n], payload->Data, sizeof(float) * 3);
if (const ImGuiPayload* payload =
ImGui::AcceptDragDropPayload(IMGUI_PAYLOAD_TYPE_COLOR_4F))
memcpy((float*)&saved_palette_[n], payload->Data, sizeof(float) * 4);
ImGui::EndDragDropTarget();
}
ImGui::PopID();
}
ImGui::EndGroup();
}
ImGui::EndChild();
}
} // namespace editor
} // namespace app
} // namespace yaze

View File

@@ -31,6 +31,8 @@ class PaletteEditor {
absl::Status Update();
void DisplayPalette(gfx::SNESPalette& palette, bool loaded);
void DrawPortablePalette(gfx::SNESPalette& palette);
auto SetupROM(ROM& rom) { rom_ = rom; }
private:

View File

@@ -48,6 +48,16 @@ void CheckByteRepeat(const uchar* rom_data, DataSizeArray& data_size_taken,
cmd_args[kCommandByteFill][0] = byte_to_repeat;
}
void CheckByteRepeatV2(const uchar* data, uint& src_pos, const uint last_pos,
CompressionCommand& cmd) {
uint i = 0;
while (src_pos + i < last_pos && data[src_pos] == data[src_pos + i]) {
++i;
}
cmd.data_size[kCommandByteFill] = i;
cmd.arguments[kCommandByteFill][0] = data[src_pos];
}
void CheckWordRepeat(const uchar* rom_data, DataSizeArray& data_size_taken,
CommandArgumentArray& cmd_args, uint& src_data_pos,
const uint last_pos) {
@@ -70,6 +80,26 @@ void CheckWordRepeat(const uchar* rom_data, DataSizeArray& data_size_taken,
}
}
void CheckWordRepeatV2(const uchar* data, uint& src_pos, const uint last_pos,
CompressionCommand& cmd) {
if (src_pos + 2 <= last_pos && data[src_pos] != data[src_pos + 1]) {
uint pos = src_pos;
char byte1 = data[pos];
char byte2 = data[pos + 1];
pos += 2;
cmd.data_size[kCommandWordFill] = 2;
while (pos + 1 <= last_pos) {
if (data[pos] == byte1 && data[pos + 1] == byte2)
cmd.data_size[kCommandWordFill] += 2;
else
break;
pos += 2;
}
cmd.arguments[kCommandWordFill][0] = byte1;
cmd.arguments[kCommandWordFill][1] = byte2;
}
}
void CheckIncByte(const uchar* rom_data, DataSizeArray& data_size_taken,
CommandArgumentArray& cmd_args, uint& src_data_pos,
const uint last_pos) {
@@ -86,6 +116,21 @@ void CheckIncByte(const uchar* rom_data, DataSizeArray& data_size_taken,
cmd_args[kCommandIncreasingFill][0] = rom_data[src_data_pos];
}
void CheckIncByteV2(const uchar* rom_data, uint& src_data_pos,
const uint last_pos, CompressionCommand& cmd) {
uint pos = src_data_pos;
char byte = rom_data[pos];
pos++;
cmd.data_size[kCommandIncreasingFill] = 1;
byte++;
while (pos <= last_pos && byte == rom_data[pos]) {
cmd.data_size[kCommandIncreasingFill]++;
byte++;
pos++;
}
cmd.arguments[kCommandIncreasingFill][0] = rom_data[src_data_pos];
}
void CheckIntraCopy(const uchar* rom_data, DataSizeArray& data_size_taken,
CommandArgumentArray& cmd_args, uint& src_data_pos,
const uint last_pos, uint start) {
@@ -120,6 +165,40 @@ void CheckIntraCopy(const uchar* rom_data, DataSizeArray& data_size_taken,
}
}
void CheckIntraCopyV2(const uchar* rom_data, uint& src_data_pos,
const uint last_pos, uint start,
CompressionCommand& cmd) {
if (src_data_pos != start) {
uint searching_pos = start;
uint current_pos_u = src_data_pos;
uint copied_size = 0;
uint search_start = start;
while (searching_pos < src_data_pos && current_pos_u <= last_pos) {
while (rom_data[current_pos_u] != rom_data[searching_pos] &&
searching_pos < src_data_pos)
searching_pos++;
search_start = searching_pos;
while (current_pos_u <= last_pos &&
rom_data[current_pos_u] == rom_data[searching_pos] &&
searching_pos < src_data_pos) {
copied_size++;
current_pos_u++;
searching_pos++;
}
if (copied_size > cmd.data_size[kCommandRepeatingBytes]) {
search_start -= start;
printf("- Found repeat of %d at %d\n", copied_size, search_start);
cmd.data_size[kCommandRepeatingBytes] = copied_size;
cmd.arguments[kCommandRepeatingBytes][0] = search_start & kSnesByteMax;
cmd.arguments[kCommandRepeatingBytes][1] = search_start >> 8;
}
current_pos_u = src_data_pos;
copied_size = 0;
}
}
}
// Check if a command managed to pick up `max_win` or more bytes
// Avoids being even with copy command, since it's possible to merge copy
void ValidateForByteGain(const DataSizeArray& data_size_taken,
@@ -140,11 +219,32 @@ void ValidateForByteGain(const DataSizeArray& data_size_taken,
}
}
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) {
// Table indicating command sizes, in bytes
const std::array<int, 5> kCommandSizes = {1, 2, 2, 2, 3};
// TODO(@scawful): TEST ME
void ValidateForByteGainV2(const CompressionCommand& cmd, uint& max_win,
uint& cmd_with_max) {
for (uint cmd_i = 1; cmd_i < 5; cmd_i++) {
uint cmd_size_taken = cmd.data_size[cmd_i];
// Check if the command size exceeds the maximum win and the size in the
// command sizes table, except for the repeating bytes command when the size
// taken is 3
if (cmd_size_taken > max_win && cmd_size_taken > kCommandSizes[cmd_i] &&
!(cmd_i == kCommandRepeatingBytes && cmd_size_taken == 3)) {
printf("==> C:%d / S:%d\n", cmd_i, cmd_size_taken);
cmd_with_max = cmd_i;
max_win = cmd_size_taken;
}
}
}
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) {
printf("- Ok we get a gain from %d\n", cmd_with_max);
std::string buffer;
buffer.push_back(cmd_args[cmd_with_max][0]);
@@ -174,6 +274,87 @@ void CompressionCommandAlternative(
comp_accumulator = 0;
}
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) {
printf("- Ok we get a gain from %d\n", cmd_with_max);
std::string buffer;
buffer.push_back(cmd.arguments[cmd_with_max][0]);
if (cmd.cmd_size[cmd_with_max] == 2) {
buffer.push_back(cmd.arguments[cmd_with_max][1]);
}
auto new_comp_piece = std::make_shared<CompressionPiece>(
cmd_with_max, max_win, buffer, cmd.cmd_size[cmd_with_max]);
PrintCompressionPiece(new_comp_piece);
// If we let non compressed stuff, we need to add a copy chunk before
if (comp_accumulator != 0) {
std::string copy_buff;
copy_buff.resize(comp_accumulator);
for (int i = 0; i < comp_accumulator; ++i) {
copy_buff[i] = rom_data[i + src_data_pos - comp_accumulator];
}
auto copy_chunk = std::make_shared<CompressionPiece>(
kCommandDirectCopy, comp_accumulator, copy_buff, comp_accumulator);
compressed_chain->next = copy_chunk;
compressed_chain = copy_chunk;
} else {
compressed_chain->next = new_comp_piece;
compressed_chain = new_comp_piece;
}
src_data_pos += max_win;
comp_accumulator = 0;
}
void AddAlternativeCompressionCommand(
const uchar* rom_data, CompressionPiecePointer& compressed_chain,
const CompressionCommand& command, uint& source_data_position,
uint& uncompressed_data_size, uint& best_command, uint& best_command_gain) {
std::cout << "- Identified a gain from command: " << best_command
<< std::endl;
// Create a buffer to store the arguments for the best command.
std::string argument_buffer;
argument_buffer.push_back(command.arguments[best_command][0]);
if (command.cmd_size[best_command] == 2) {
argument_buffer.push_back(command.arguments[best_command][1]);
}
// Create a new compression piece for the best command.
auto new_compression_piece = std::make_shared<CompressionPiece>(
best_command, best_command_gain, argument_buffer,
command.cmd_size[best_command]);
PrintCompressionPiece(new_compression_piece);
// If there is uncompressed data, create a direct copy compression piece for
// it.
if (uncompressed_data_size != 0) {
std::string copy_buffer(uncompressed_data_size, 0);
for (int i = 0; i < uncompressed_data_size; ++i) {
copy_buffer[i] =
rom_data[i + source_data_position - uncompressed_data_size];
}
auto direct_copy_piece = std::make_shared<CompressionPiece>(
kCommandDirectCopy, uncompressed_data_size, copy_buffer,
uncompressed_data_size);
// Append the direct copy piece to the chain.
compressed_chain->next = direct_copy_piece;
compressed_chain = direct_copy_piece;
}
// Append the new compression piece to the chain.
compressed_chain->next = new_compression_piece;
compressed_chain = new_compression_piece;
// Update the position in the source data and reset the uncompressed data
// size.
source_data_position += best_command_gain;
uncompressed_data_size = 0;
}
absl::StatusOr<CompressionPiecePointer> SplitCompressionPiece(
CompressionPiecePointer& piece, int mode) {
CompressionPiecePointer new_piece;
@@ -225,8 +406,7 @@ absl::StatusOr<CompressionPiecePointer> SplitCompressionPiece(
return new_piece;
}
Bytes CreateCompressionString(CompressionPiecePointer& start,
int mode) {
Bytes CreateCompressionString(CompressionPiecePointer& start, int mode) {
uint pos = 0;
auto piece = start;
Bytes output;
@@ -327,6 +507,201 @@ CompressionPiecePointer MergeCopy(CompressionPiecePointer& start) {
return start;
}
// TODO TEST compressed data border for each cmd
absl::StatusOr<Bytes> CompressV2(const uchar* data, const int start,
const int length, int mode, bool check) {
// Surely there's no need to compress zero...
if (length == 0) {
return Bytes();
}
// Worst case should be a copy of the string with extended header
auto compressed_chain = std::make_shared<CompressionPiece>(1, 1, "aaa", 2);
auto compressed_chain_start = compressed_chain;
CompressionCommand current_cmd = {/*argument*/ {{}},
/*cmd_size*/ {0, 1, 2, 1, 2},
/*data_size*/ {0, 0, 0, 0, 0}};
uint src_pos = start;
uint last_pos = start + length - 1;
uint comp_accumulator = 0; // Used when skipping using copy
while (true) {
current_cmd.data_size.fill({});
current_cmd.arguments.fill({{}});
CheckByteRepeatV2(data, src_pos, last_pos, current_cmd);
CheckWordRepeatV2(data, src_pos, last_pos, current_cmd);
CheckIncByteV2(data, src_pos, last_pos, current_cmd);
CheckIntraCopyV2(data, src_pos, last_pos, start, current_cmd);
uint max_win = 2;
uint cmd_with_max = kCommandDirectCopy;
ValidateForByteGainV2(current_cmd, max_win, cmd_with_max);
if (cmd_with_max == kCommandDirectCopy) {
// This is the worst case scenario
// Progress through the next byte, in case there's a different
// compression command we can implement before we hit 32 bytes.
src_pos++;
comp_accumulator++;
// Arbitrary choice to do a 32 bytes grouping for copy.
if (comp_accumulator == 32 || src_pos > last_pos) {
std::string buffer = SetBuffer(data, src_pos, comp_accumulator);
auto new_comp_piece = std::make_shared<CompressionPiece>(
kCommandDirectCopy, comp_accumulator, buffer, comp_accumulator);
compressed_chain->next = new_comp_piece;
compressed_chain = new_comp_piece;
comp_accumulator = 0;
}
} else {
AddAlternativeCompressionCommand(data, compressed_chain, current_cmd,
src_pos, comp_accumulator, cmd_with_max,
max_win);
}
if (src_pos > last_pos) {
printf("Breaking compression loop\n");
break;
}
if (check) {
RETURN_IF_ERROR(ValidateCompressionResult(compressed_chain_start, mode,
start, src_pos))
}
}
// Skipping compression chain header
MergeCopy(compressed_chain_start->next);
PrintCompressionChain(compressed_chain_start);
return CreateCompressionString(compressed_chain_start->next, mode);
}
absl::StatusOr<Bytes> CompressGraphics(const uchar* data, const int pos,
const int length) {
return CompressV2(data, pos, length, kNintendoMode2);
}
absl::StatusOr<Bytes> CompressOverworld(const uchar* data, const int pos,
const int length) {
return CompressV2(data, pos, length, kNintendoMode1);
}
std::string SetBuffer(const uchar* data, int src_pos, int comp_accumulator) {
std::string buffer;
for (int i = 0; i < comp_accumulator; ++i) {
buffer.push_back(data[i + src_pos - comp_accumulator]);
}
return buffer;
}
void memfill(const uchar* data, Bytes& buffer, int buffer_pos, int offset,
int length) {
auto a = data[offset];
auto b = data[offset + 1];
for (int i = 0; i < length; i = i + 2) {
buffer[buffer_pos + i] = a;
if ((i + 1) < length) buffer[buffer_pos + i + 1] = b;
}
}
absl::StatusOr<Bytes> DecompressV2(const uchar* data, int offset, int size,
int mode) {
if (size == 0) {
return Bytes();
}
Bytes buffer(size, 0);
uint length = 0;
uint buffer_pos = 0;
uchar command = 0;
uchar header = data[offset];
while (header != kSnesByteMax) {
if ((header & kExpandedMod) == kExpandedMod) {
// Expanded Command
command = ((header >> 2) & kCommandMod);
length = (((header << 8) | data[offset + 1]) & kExpandedLengthMod);
offset += 2; // Advance 2 bytes in ROM
} else {
// Normal Command
command = ((header >> 5) & kCommandMod);
length = (header & kNormalLengthMod);
offset += 1; // Advance 1 byte in ROM
}
length += 1; // each commands is at least of size 1 even if index 00
switch (command) {
case kCommandDirectCopy: // Does not advance in the ROM
memcpy(buffer.data() + buffer_pos, data + offset, length);
buffer_pos += length;
offset += length;
break;
case kCommandByteFill:
memset(buffer.data() + buffer_pos, (int)(data[offset]), length);
buffer_pos += length;
offset += 1; // Advances 1 byte in the ROM
break;
case kCommandWordFill:
memfill(data, buffer, buffer_pos, offset, length);
buffer_pos += length;
offset += 2; // Advance 2 byte in the ROM
break;
case kCommandIncreasingFill: {
auto inc_byte = data[offset];
for (int i = 0; i < length; i++) {
buffer[buffer_pos] = inc_byte++;
buffer_pos++;
}
offset += 1; // Advance 1 byte in the ROM
} break;
case kCommandRepeatingBytes: {
ushort s1 = ((data[offset + 1] & kSnesByteMax) << 8);
ushort s2 = (data[offset] & kSnesByteMax);
int addr = (s1 | s2);
if (mode == kNintendoMode1) { // Reversed byte order for
// overworld maps
addr = (data[offset + 1] & kSnesByteMax) |
((data[offset] & kSnesByteMax) << 8);
}
if (addr > offset) {
return absl::InternalError(absl::StrFormat(
"Decompress: Offset for command copy exceeds current position "
"(Offset : %#04x | Pos : %#06x)\n",
addr, offset));
}
if (buffer_pos + length >= size) {
size *= 2;
buffer.resize(size);
}
memcpy(buffer.data() + buffer_pos, buffer.data() + addr, length);
buffer_pos += length;
offset += 2;
} break;
default: {
std::cout << absl::StrFormat(
"Decompress: Invalid header (Offset : %#06x, Command: %#04x)\n",
offset, command);
} break;
}
// check next byte
header = data[offset];
}
return buffer;
}
absl::StatusOr<Bytes> DecompressGraphics(const uchar* data, int pos, int size) {
return DecompressV2(data, pos, size, kNintendoMode2);
}
absl::StatusOr<Bytes> DecompressOverworld(const uchar* data, int pos,
int size) {
return DecompressV2(data, pos, size, kNintendoMode1);
}
} // namespace lc_lz2
} // namespace gfx
} // namespace app

View File

@@ -34,9 +34,23 @@ constexpr int kExpandedLengthMod = 0x3FF;
constexpr int kNormalLengthMod = 0x1F;
constexpr int kCompressionStringMod = 7 << 5;
// Represents a command in the compression algorithm.
struct CompressionCommand {
// The command arguments for each possible command.
std::array<std::array<char, 2>, 5> arguments;
// The size of each possible command.
std::array<uint, 5> cmd_size;
// The size of the data processed by each possible command.
std::array<uint, 5> data_size;
};
using CommandArgumentArray = std::array<std::array<char, 2>, 5>;
using CommandSizeArray = std::array<uint, 5>;
using DataSizeArray = std::array<uint, 5>;
// Represents a piece of compressed data.
struct CompressionPiece {
char command;
int length;
@@ -54,6 +68,46 @@ void PrintCompressionPiece(const CompressionPiecePointer& piece);
void PrintCompressionChain(const CompressionPiecePointer& chain_head);
// Compression V2
void CheckByteRepeatV2(const uchar* data, uint& src_pos, const uint last_pos,
CompressionCommand& cmd);
void CheckWordRepeatV2(const uchar* data, uint& src_pos, const uint last_pos,
CompressionCommand& cmd);
void CheckIncByteV2(const uchar* data, uint& src_pos, const uint last_pos,
CompressionCommand& cmd);
void CheckIntraCopyV2(const uchar* data, uint& src_pos, const uint last_pos,
uint start, CompressionCommand& cmd);
void ValidateForByteGainV2(const CompressionCommand& cmd, uint& max_win,
uint& cmd_with_max);
void CompressionCommandAlternativeV2(const uchar* data,
const CompressionCommand& cmd,
CompressionPiecePointer& compressed_chain,
uint& src_pos, uint& comp_accumulator,
uint& cmd_with_max, uint& max_win);
absl::StatusOr<Bytes> CompressV2(const uchar* data, const int start,
const int length, int mode = 1,
bool check = false);
absl::StatusOr<Bytes> CompressGraphics(const int pos, const int length);
absl::StatusOr<Bytes> CompressOverworld(const int pos, const int length);
std::string SetBuffer(const uchar* data, int src_pos, int comp_accumulator);
void memfill(const uchar* data, Bytes& buffer, int buffer_pos, int offset,
int length);
absl::StatusOr<Bytes> DecompressV2(const uchar* data, int offset,
int size = 0x800, int mode = 1);
absl::StatusOr<Bytes> DecompressGraphics(const uchar* data, int pos, int size);
absl::StatusOr<Bytes> DecompressOverworld(const uchar* data, int pos, int size);
// Compression V1
void CheckByteRepeat(const uchar* rom_data, DataSizeArray& data_size_taken,
CommandArgumentArray& cmd_args, uint& src_data_pos,
const uint last_pos);

View File

@@ -72,6 +72,8 @@ SNESColor GetCgxColor(short color) {
std::vector<SNESColor> GetColFileData(uchar* data) {
std::vector<SNESColor> colors;
colors.reserve(256);
colors.resize(256);
for (int i = 0; i < 512; i += 2) {
colors[i / 2] = GetCgxColor((short)((data[i + 1] << 8) + data[i]));

View File

@@ -239,8 +239,9 @@ absl::StatusOr<Bytes> ROM::Load2bppGraphics() {
const uint8_t sheets[] = {113, 114, 218, 219, 220, 221};
for (const auto& sheet_id : sheets) {
auto offset = GetGraphicsAddress(rom_data_.data(), sheet_id);
ASSIGN_OR_RETURN(auto decomp_sheet, Decompress(offset))
auto offset = GetGraphicsAddress(data(), sheet_id);
ASSIGN_OR_RETURN(auto decomp_sheet,
gfx::lc_lz2::DecompressV2(data(), offset))
auto converted_sheet = gfx::SnesTo8bppSheet(decomp_sheet, 2);
for (const auto& each_pixel : converted_sheet) {
sheet.push_back(each_pixel);
@@ -263,7 +264,7 @@ absl::Status ROM::LoadAllGraphicsData() {
for (int i = 0; i < core::NumberOfSheets; i++) {
if (i >= 115 && i <= 126) { // uncompressed sheets
sheet.resize(core::Uncompressed3BPPSize);
auto offset = GetGraphicsAddress(rom_data_.data(), i);
auto offset = GetGraphicsAddress(data(), i);
for (int j = 0; j < core::Uncompressed3BPPSize; j++) {
sheet[j] = rom_data_[j + offset];
}
@@ -271,8 +272,8 @@ absl::Status ROM::LoadAllGraphicsData() {
} else if (i == 113 || i == 114 || i >= 218) {
bpp3 = false;
} else {
auto offset = GetGraphicsAddress(rom_data_.data(), i);
ASSIGN_OR_RETURN(sheet, Decompress(offset))
auto offset = GetGraphicsAddress(data(), i);
ASSIGN_OR_RETURN(sheet, gfx::lc_lz2::DecompressV2(data(), offset))
bpp3 = true;
}
@@ -297,7 +298,8 @@ absl::Status ROM::LoadAllGraphicsData() {
// ============================================================================
absl::Status ROM::LoadFromFile(const absl::string_view& filename) {
absl::Status ROM::LoadFromFile(const absl::string_view& filename,
bool z3_load) {
filename_ = filename;
std::ifstream file(filename.data(), std::ios::binary);
if (!file.is_open()) {
@@ -317,7 +319,9 @@ absl::Status ROM::LoadFromFile(const absl::string_view& filename) {
memcpy(title_, rom_data_.data() + kTitleStringOffset, kTitleStringLength);
file.close();
LoadAllPalettes();
if (z3_load) {
LoadAllPalettes();
}
is_loaded_ = true;
return absl::OkStatus();
}

View File

@@ -85,7 +85,8 @@ class ROM {
// Load functions
absl::StatusOr<Bytes> Load2bppGraphics();
absl::Status LoadAllGraphicsData();
absl::Status LoadFromFile(const absl::string_view& filename);
absl::Status LoadFromFile(const absl::string_view& filename,
bool z3_load = true);
absl::Status LoadFromPointer(uchar* data, size_t length);
absl::Status LoadFromBytes(const Bytes& data);
void LoadAllPalettes();