Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Brett208 opened this issue Sep 29, 2018 · 0 comments
Open
Labels

Comments

@Brett208
Copy link
Member

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.");
		}
	}
}
@Brett208 Brett208 added the BMP label Sep 29, 2018
@Brett208 Brett208 changed the title Allow verifying indexed pixels contained on palette Verify indexed pixels contained on palette (if palette is not at capacity) Oct 8, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant