Skip to content

Commit

Permalink
Merge pull request #24 from clarkjoe/master
Browse files Browse the repository at this point in the history
add JASC-PAL support when importing palettes
  • Loading branch information
Kawa-oneechan authored Mar 10, 2024
2 parents 33f756f + b74e362 commit 30554eb
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 17 deletions.
2 changes: 1 addition & 1 deletion SCICompanionLib/Src/Dialogs/PaletteDefinitionDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ void CPaletteDefinitionDialog::OnColorClick(BYTE bIndex, int nID, BOOL fLeftClic

bool _GetPaletteFilename(bool open, const std::string &dialogTitle, std::string &filename)
{
CFileDialog fileDialog(open, ".pal", nullptr, OFN_NOCHANGEDIR, "PAL files (*.pal)|*.pal|All Files|*.*|");
CFileDialog fileDialog(open, ".pal", nullptr, OFN_NOCHANGEDIR, "PAL files (*.pal)|*.pal|PspPalette files (*.PspPalette)|*.PspPalette|All Files|*.*|");
fileDialog.m_ofn.lpstrTitle = dialogTitle.c_str();;
if (IDOK == fileDialog.DoModal())
{
Expand Down
100 changes: 84 additions & 16 deletions SCICompanionLib/Src/Resources/PaletteOperations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,26 +70,94 @@ void LoadPALFile(const std::string &filename, PaletteComponent &palette, int sta
sci::streamOwner owner(file.hFile);
sci::istream byteStream = owner.getReader();
uint32_t riff, pal, dataSize, chunk, chunkSize;

const uint32_t JASC = 0x4353414A;

byteStream >> riff;
byteStream >> dataSize;
byteStream >> pal;
byteStream >> chunk;
byteStream >> chunkSize;
uint16_t palVersion, palEntries;
byteStream >> palVersion;
byteStream >> palEntries;

for (uint16_t i = 0; i < palEntries; i++)
{
if ((i + startIndex) < ARRAYSIZE(palette.Colors))
{
byteStream >> palette.Colors[i + startIndex].rgbRed;
byteStream >> palette.Colors[i + startIndex].rgbGreen;
byteStream >> palette.Colors[i + startIndex].rgbBlue;
byteStream >> palette.Colors[i + startIndex].rgbReserved;

if (std::memcmp(&riff, &JASC, sizeof(uint32_t)) == 0) {
// JASC-PAL import
// Skip the "JASC-PAL\n0100\n" parts
for (int i = 0; i < 2; ++i) {
while (byteStream.good() && byteStream.peek() != '\n') {
uint8_t byte;
byteStream >> byte; // Read and ignore each byte until a newline
}
uint8_t newline;
byteStream >> newline; // Read the newline itself
}

// Read the color count dynamically
std::string colorCountStr;
uint8_t ch;
while (byteStream.good() && byteStream.peek() != '\n') {
byteStream >> ch;
colorCountStr += static_cast<char>(ch);
}
byteStream >> ch; // Consume the newline
int colorCount = std::atoi(colorCountStr.c_str());

std::string componentStr;

for (int i = 0; i < colorCount; ++i) {
uint8_t red, green, blue, temp;

// Function to read and convert a color component
auto readColorComponent = [&](uint8_t &colorComponent) {
componentStr.clear();
while (byteStream.good()) {
uint8_t ch = byteStream.peek();
// Check if the next character is a space or newline, which are delimiters.
if (ch == ' ' || ch == '\n' || !byteStream.good()) {
break; // Exit the loop if a delimiter is found or stream is not good.
}
byteStream >> ch; // Read the next character from the stream.
componentStr += static_cast<char>(ch); // Append the character to the string.
}

if (!componentStr.empty()) {
colorComponent = static_cast<uint8_t>(std::atoi(componentStr.c_str())); // Convert string to number.
}

// Consume the delimiter (space or newline) if the stream is still good.
if (byteStream.good()) {
byteStream >> temp; // Assuming 'temp' is declared outside this lambda.
}
};

// Read and convert each color component
readColorComponent(red);
readColorComponent(green);
readColorComponent(blue);

palette.Colors[i + startIndex].rgbRed = red;
palette.Colors[i + startIndex].rgbGreen = green;
palette.Colors[i + startIndex].rgbBlue = blue;
palette.Colors[i + startIndex].rgbReserved = 0x1; // Actually, set this to used.
}
}
else {
// RIFF-PAL import
byteStream >> dataSize;
byteStream >> pal;
byteStream >> chunk;
byteStream >> chunkSize;
uint16_t palVersion, palEntries;
byteStream >> palVersion;
byteStream >> palEntries;

for (uint16_t i = 0; i < palEntries; i++)
{
if ((i + startIndex) < ARRAYSIZE(palette.Colors))
{
byteStream >> palette.Colors[i + startIndex].rgbRed;
byteStream >> palette.Colors[i + startIndex].rgbGreen;
byteStream >> palette.Colors[i + startIndex].rgbBlue;
byteStream >> palette.Colors[i + startIndex].rgbReserved;
palette.Colors[i + startIndex].rgbReserved = 0x1; // Actually, set this to used.
}
}
}
}

PaletteComponent::PaletteComponent() : Compression(PaletteCompression::Unknown), EntryType(PaletteEntryType::Unknown)
Expand Down

0 comments on commit 30554eb

Please sign in to comment.