bitmap类C++实现

class BitMap
{
private:
  char *bitmap;
  int gsize;
public:
  BitMap(){
    //默认10000
    gsize = (10000>>3) + 1;
    bitmap = new char[gsize];
    memset(bitmap,0,sizeof(bitmap));
  }
  BitMap(int n){
    gsize = (n>>3) + 1;
    bitmap = new char[gsize];
    memset(bitmap,0,sizeof(bitmap));
  }
  ~BitMap()
  {
    delete [] bitmap;
  }
  int get(int x)
  {
    int cur = x>>3;
    int remainder = x&(7);
    if(cur > gsize)return -1;//越界了不行

    return (bitmap[cur]>>remainder)&1;
  }
  bool set(int x)
  {
    int cur = x>>3;//获取元素位置
    int remainder = x&(7);//获取精确位置
    if(cur > gsize)return 0;
    bitmap[cur] |= 1<<remainder;//赋值
    return true;
  }
};

bitmap其实就是对位运算的扩展。一般的位运算是int变量,一个int元素记录32个元素(int类型有32位),记录对应位的信息(用二进制的0或者1表示)。不过可以用char使得可能的浪费更小。

  所谓bitmap,就是定义成了数组,可以记录不止32位的信息。比如char bitmap[1000]可以记录多少位信息呢?1000×8。修改与查找的思路很简单,对于修改,首先找到一个大范围,位于数组哪个元素,然后对应元素确定该值对应该元素的哪一位的,通过按位或操作赋值1。除8得到哪个元素,对8求余得到精确位置,我用的是位运算比除法和求余运算要快很多(bitmap本来就是为了加速的和节省空间的)。查找操作也是类似。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是一个用于C++ MFC开发的Bitmap图片操作,在文件中叫CBitmapEx,可用于放大,缩小,翻转,过渡和其他有用的功能,有兴趣的朋友可以下载看看。 部分public method: // // void Create(long width, long height); // void Create(CBitmapEx& bitmapEx); // void Load(LPTSTR lpszBitmapFile); // void Save(LPTSTR lpszBitmapFile); // void Scale(long horizontalPercent=100, long verticalPercent=100); // void Rotate(long degrees=0, _PIXEL bgColor=_RGB(0,0,0)); // void FlipHorizontal(); // void FlipVertical(); // void MirrorLeft(); // void MirrorRight(); // void MirrorTop(); // void MirrorBottom(); // void Clear(_PIXEL clearColor=_RGB(0,0,0)); // void Negative(); // void Grayscale(); // void Sepia(long depth=34); // void Emboss(); // void Engrave(); // void Pixelize(long size=4); // void Draw(HDC hDC); // void Draw(long dstX, long dstY, long width, long height, // CBitmapEx& bitmapEx, long srcX, long srcY); // void Draw(long dstX, long dstY, long width, long height, // CBitmapEx& bitmapEx, long srcX, long srcY, long alpha); // void Draw(long dstX, long dstY, long dstWidth, long dstHeight, // CBitmapEx& bitmapEx, long srcX, long srcY, long srcWidth, long srcHeight); // void Draw(long dstX, long dstY, long dstWidth, long dstHeight, CBitmapEx& bitmapEx, // long srcX, long srcY, long srcWidth, long srcHeight, long alpha); // void DrawTransparent(long dstX, long dstY, long width, long height, // CBitmapEx& bitmapEx, long srcX, long srcY, _PIXEL transparentColor=_RGB(0,0,0)); // void DrawTransparent(long dstX, long dstY, long width, long height, // CBitmapEx& bitmapEx, long srcX, long srcY, long alpha, // _PIXEL transparentColor=_RGB(0,0,0)); // void DrawTransparent(long dstX, long dstY, long dstWidth, long dstHeight, // CBitmapEx& bitmapEx, long srcX, long srcY, long srcWidth, long srcHeight, // _PIXEL transparentColor=_RGB(0,0,0)); // void DrawTransparent(long dstX, long dstY, long dstWidth, long dstHeight, // CBitmapEx& bitmapEx, long srcX, long srcY, long srcWidth, long srcHeight, // long alpha, _PIXEL transparentColor=_RGB(0,0,0)); // LPBI

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值