void ColourPalette::loadUserPalette()

in atari_py/ale_interface/src/common/ColourPalette.cpp [151:214]


void ColourPalette::loadUserPalette(const string& paletteFile)
{
    const int bytesPerColor = 3;
    const int NTSCPaletteSize = 128;
    const int PALPaletteSize = 128;
    const int SECAMPaletteSize = 8;

    int expectedFileSize =  NTSCPaletteSize * bytesPerColor +
                            PALPaletteSize * bytesPerColor +
                            SECAMPaletteSize * bytesPerColor;

    ifstream paletteStream(paletteFile.c_str(), ios::binary);
    if(!paletteStream)
        return;

    // Make sure the contains enough data for the NTSC, PAL and SECAM palettes
    // This means 128 colours each for NTSC and PAL, at 3 bytes per pixel
    // and 8 colours for SECAM at 3 bytes per pixel
    paletteStream.seekg(0, ios::end);
    streampos length = paletteStream.tellg();
    paletteStream.seekg(0, ios::beg);

    if(length < expectedFileSize)
    {
        paletteStream.close();
        cerr << "ERROR: invalid palette file " << paletteFile << endl;
        return;
    }

    // Now that we have valid data, create the user-defined palettes
    uInt8 pixbuf[bytesPerColor];  // Temporary buffer for one 24-bit pixel

    for(int i = 0; i < NTSCPaletteSize; i++)  // NTSC palette
    {
        paletteStream.read((char*)pixbuf, bytesPerColor);
        m_userNTSCPalette[(i<<1)] = packRGB(pixbuf[0], pixbuf[1], pixbuf[2]);
        m_userNTSCPalette[(i<<1)+1] = convertGrayscale(m_userNTSCPalette[(i<<1)]);
    }
    for(int i = 0; i < PALPaletteSize; i++)  // PAL palette
    {
        paletteStream.read((char*)pixbuf, bytesPerColor);
        m_userPALPalette[(i<<1)] = packRGB(pixbuf[0], pixbuf[1], pixbuf[2]);
        m_userPALPalette[(i<<1)+1] = convertGrayscale(m_userPALPalette[(i<<1)]);
    }

    uInt32 tmpSecam[SECAMPaletteSize*2];         // All 8 24-bit pixels, plus 8 colorloss pixels
    for(int i = 0; i < SECAMPaletteSize; i++)    // SECAM palette
    {
        paletteStream.read((char*)pixbuf, bytesPerColor);
        tmpSecam[(i<<1)]   = packRGB(pixbuf[0], pixbuf[1], pixbuf[2]);
        tmpSecam[(i<<1)+1] = convertGrayscale(tmpSecam[(i<<1)]);
    }

    uInt32*tmpSECAMPalettePtr = m_userSECAMPalette;
    for(int i = 0; i < 16; ++i)
    {
        memcpy(tmpSECAMPalettePtr, tmpSecam, SECAMPaletteSize*2);
        tmpSECAMPalettePtr += SECAMPaletteSize*2;
    }

    paletteStream.close();

    myUserPaletteDefined = true;
}