【Cpp】位图Bitmap

  • code
#include 
#include 
#include 
#include 

class Bitmap
{
private:
    std::vector<uint8_t> data; // 存储位图数据的字节数组
    uint32_t size;             // 位图的大小(以位为单位)

public:
    // 构造函数
    Bitmap(uint32_t size) : size(size)
    {
        uint32_t byteSize = (size + 7) / 8;
        data.resize(byteSize, 0); // 初始化数据为0
    }

    // 设置或清除指定位的值
    int setBit(uint32_t pos, bool value)
    {
        if (pos < 0 || pos >= size)
        {
            std::cerr << "Invalid bit position: " << pos << std::endl;
            return -1;
        }

        int bytePos = pos / 8;
        int bitPos = pos % 8;

        if (value)
        {
            data[bytePos] |= (1 << bitPos); // 设置为1
        }
        else
        {
            data[bytePos] &= ~(1 << bitPos); // 清除为0
        }

        return 0;
    }

    // 检查指定位是否为1
    bool getBit(uint32_t pos) const
    {
        if (pos < 0 || pos >= size)
        {
            std::cerr << "Invalid bit position: " << pos << std::endl;
            return 0;
        }

        int bytePos = pos / 8;
        int bitPos = pos % 8;
        return (data[bytePos] & (1 << bitPos)) != 0;
    }

    // 打印位图的每一位
    void printBitmap(void)
    {
        printf("Bitmap :\n");
        for (int i = 0; i < size; i++)
        {
            printf(" %.2d:%d |", i, getBit(i));
            if ((0 == ((i + 1) % 8)) && (0 != i))
            {
                printf("\n");
            }
        }
        printf("\n");
    }

    // 读取数据从位图的指定字节起始位置
    int readData(uint32_t startByte, uint8_t *data, uint32_t dataSize) const
    {
        uint32_t bitmapByteSize = (size + 7) / 8;

        if (startByte < 0 || (startByte + dataSize) > bitmapByteSize)
        {
            std::cerr << "Invalid start byte position or data size: " << startByte << std::endl;
            return -1;
        }

        for (uint32_t i = 0; i < dataSize; i++)
        {
            uint32_t bytePos = startByte + i;
            data[i] = this->data[bytePos];
            std::cout << "readData bytePos [" << bytePos << "] data[" << (int)data[i] << "]" << std::endl;
        }

        return 0;
    }

    // 写入数据到位图的指定字节起始位置
    int writeData(uint32_t startByte, const uint8_t *data, uint32_t dataSize)
    {
        uint32_t bitmapByteSize = (size + 7) / 8;

        if (startByte < 0 || (startByte + dataSize) > bitmapByteSize)
        {
            std::cerr << "Invalid start byte position or data size: " << startByte << std::endl;
            return -1;
        }

        for (uint32_t i = 0; i < dataSize; i++)
        {
            uint32_t bytePos = startByte + i;
            this->data[bytePos] = data[i];
            std::cout << "writeData bytePos [" << bytePos << "] data[" << (int)data[i] << "]" << std::endl;
        }

        return 0;
    }
};

int main()
{
    uint32_t bitmapSize = 32; // 设置位图大小
    Bitmap bitmap(bitmapSize);

    // 设置一些位
    bitmap.setBit(8, true);
    bitmap.setBit(9, true);
    bitmap.setBit(18, true);

    // 打印位图的每一位
    bitmap.printBitmap();

    // 清除位
    bitmap.setBit(18, false);
    std::cout << "Bit 18 is set after clearing: " << bitmap.getBit(18) << std::endl;

    // 打印位图的每一位
    bitmap.printBitmap();

    uint8_t dateRead = 0;
    bitmap.readData(1, &dateRead, sizeof(dateRead));
    std::cout << "dateRead [" << (int)dateRead << "]" << std::endl;

    uint8_t dateWrite = 0xFF;
    bitmap.writeData(0, &dateWrite, sizeof(dateWrite));
    bitmap.printBitmap();

    return 0;
}

  • test【Cpp】位图Bitmap_第1张图片

你可能感兴趣的:(C/C++,c++,c语言)