OutpostUniverse / OP2Utility

C++ library for working with Outpost 2 related files and tasks.
MIT License
4 stars 0 forks source link

Verify indexed pixels contained on palette (if palette is not at capacity) #202

Open Brett208 opened 5 years ago

Brett208 commented 5 years ago

IndexedBmpWriter will extend the palette, which will ensure all pixels indices are contained on the palette. However, it should be possible to detect if a pixel index is out of range before the palette is extended.

A first draft of the function is:

void IndexedBmpWriter::VerifyPixelsContainedInPalette(uint16_t bitCount, std::size_t paletteEntryCount, const std::vector<uint8_t>& pixels)
{
    // Check if palette is full
    // Use explicit size_t type to avoid compiler warnings for signedness or size
    if (paletteEntryCount == std::size_t{1} << bitCount) {
        return;
    }

    if (bitCount == 1) {
        for (const auto& pixelGroup : pixels) {
            if (pixelGroup > 0) {
                throw (std::runtime_error("A pixel is outside the range of set palette indices."));
            }
        }
    }

    if (bitCount == 4) {
        return; //TODO: Support checking 2 bit images
    }

    for (std::size_t i = 0; i < pixels.size(); ++i) {
        if (pixels[i] >= paletteEntryCount) {
            throw std::runtime_error("Pixel " + std::to_string(i) + " is outside the range of set palette indices.");
        }
    }
}