1bit → 8bit 解压(黑白图像)
std::shared_ptr<unsigned char> Convert1bitTo8bit(const std::shared_ptr<unsigned char>& input, int width, int height)
{
size_t pixelCount = width * height;
std::shared_ptr<unsigned char> output(new unsigned char[pixelCount], std::default_delete<unsigned char[]>());
for (size_t i = 0; i < pixelCount; ++i) {
size_t byteIndex = i / 8;
size_t bitOffset = 7 - (i % 8);
unsigned char bit = (input.get()[byteIndex] >> bitOffset) & 0x01;
output.get()[i] = bit ? 255 : 0;
}
return output;
}
2bit → 8bit 解压(4级灰度)
std::shared_ptr<unsigned char> Convert2bitTo8bit(const std::shared_ptr<unsigned char>& input, int width, int height)
{
size_t pixelCount = width * height;
std::shared_ptr<unsigned char> output(new unsigned char[pixelCount], std::default_delete<unsigned char[]>());
for (size_t i = 0; i < pixelCount; ++i) {
size_t byteIndex = i / 4;
size_t bitOffset = (3 - (i % 4)) * 2;
unsigned char bits = (input.get()[byteIndex] >> bitOffset) & 0x03;
output.get()[i] = bits * 85; // 0→0, 1→85, 2→170, 3→255
}
return output;
}
4bit → 8bit 解压(16级灰度)
std::shared_ptr<unsigned char> Convert4bitTo8bit(const std::shared_ptr<unsigned char>& input, int width, int height)
{
size_t pixelCount = width * height;
std::shared_ptr<unsigned char> output(new unsigned char[pixelCount], std::default_delete<unsigned char[]>());
for (size_t i = 0; i < pixelCount; ++i) {
size_t byteIndex = i / 2;
bool high = (i % 2 == 0);
unsigned char bits = high ? (input.get()[byteIndex] >> 4) : (input.get()[byteIndex] & 0x0F);
output.get()[i] = bits * 17; // 0→0, 1→17, ..., 15→255
}
return output;
}
1万+

被折叠的 条评论
为什么被折叠?



