c语言实现bitmap

来源:互联网 发布:阿里万网域名注册官网 编辑:程序博客网 时间:2024/04/30 20:45

c语言实现的bitmap,功能可用,已经过测试,如果需要其他操作(字节序转换、清空等),可自行添加。

大家可在此下载源代码:下载源代码

/* bitmap.h * * Copyright (C) 2013, 2013 chashen. All Rights Reserved. * Written by chashen * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version * 2 of the License, or (at your option) any later version. */#ifndef _BITMAP_H_#define _BITMAP_H_typedef struct tagBitmap{    unsigned int uiMemSize;      /* bitmap所占内存大小,不包括本头部 */    unsigned int uiMaxBitID;     /* 最大有效位 */    unsigned char aucPtr[0];     /* bitmap数据区 */}BITMAP_S;BITMAP_S *Bitmap_Create(unsigned int uiMaxBitID);void Bitmap_Destroy(BITMAP_S *pstBitmap);void Bitmap_Set(BITMAP_S *pstBitmap, unsigned int uiPos);void Bitmap_Reset(BITMAP_S *pstBitmap, unsigned int uiPos);int Bitmap_Check(BITMAP_S *pstBitmap, unsigned int uiPos);#endif /* _BITMAP_H_ */

/* bitmap.c * * Copyright (C) 2013, 2013 chashen. All Rights Reserved. * Written by chashen * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version * 2 of the License, or (at your option) any later version. */#include <stdlib.h>#include <string.h>#include "bitmap.h"/******************************************************************** Function Name: Bitmap_Create  Date Created: 2013-11-07        Author: chashen   Description: 创建指定最大有效位的位图         Input: unsigned int uiMaxBitID, 最大有效位        Output: none        Return: BITMAP_S *, 创建成功                NULL,       创建失败       Caution:  ----------------------------------------------------------------    Modifiction History     DATE                NAME                DESCRIPTION     ------------------------------------------------------------    YYYY-MM-DD  ----------------------------------------------------------------********************************************************************/BITMAP_S *Bitmap_Create(unsigned int uiMaxBitID){    BITMAP_S *pstBitmap = NULL;    unsigned int uiLen = sizeof(BITMAP_S);    unsigned int uiMemSize = (uiMaxBitID + 1) / 8;    unsigned int uiRemainder = (uiMaxBitID + 1) % 8;    if (uiRemainder != 0)    {        uiMemSize++;    }        uiLen += uiMemSize;    pstBitmap = malloc(uiLen);    if (pstBitmap != NULL)    {        memset(pstBitmap, 0, uiLen);        pstBitmap->uiMemSize = uiMemSize;        pstBitmap->uiMaxBitID = uiMaxBitID;    }        return pstBitmap;}/******************************************************************** Function Name: Bitmap_Destroy  Date Created: 2013-11-07        Author: chashen   Description: 销毁位图         Input: none        Output: none        Return: void       Caution:  ----------------------------------------------------------------    Modifiction History     DATE                NAME                DESCRIPTION     ------------------------------------------------------------    YYYY-MM-DD  ----------------------------------------------------------------********************************************************************/void Bitmap_Destroy(BITMAP_S *pstBitmap){    free(pstBitmap);    pstBitmap = NULL;}/******************************************************************** Function Name: Bitmap_Set  Date Created: 2013-11-07        Author: chashen   Description: 设置位图的指定位         Input: BITMAP_S *pstBitmap, 位图指针                 unsigned int uiPos,  指定的设置位        Output: none        Return: void       Caution:  ----------------------------------------------------------------    Modifiction History     DATE                NAME                DESCRIPTION    ------------------------------------------------------------    YYYY-MM-DD  ----------------------------------------------------------------********************************************************************/void Bitmap_Set(BITMAP_S *pstBitmap, unsigned int uiPos){    pstBitmap->aucPtr[uiPos / 8] |= (0x80 >> uiPos % 8);}/******************************************************************** Function Name: Bitmap_Reset  Date Created: 2013-11-07        Author: chashen   Description: 重置位图的指定位         Input: BITMAP_S *pstBitmap, 位图指针                 unsigned int uiPos,  指定的重置位        Output: none        Return: void       Caution:  ----------------------------------------------------------------    Modifiction History     DATE                NAME                DESCRIPTION     ------------------------------------------------------------    YYYY-MM-DD  ----------------------------------------------------------------********************************************************************/void Bitmap_Reset(BITMAP_S *pstBitmap, unsigned int uiPos){    pstBitmap->aucPtr[uiPos / 8] &= (~(0x80 >> uiPos % 8));}/******************************************************************** Function Name: Bitmap_Check  Date Created: 2013-11-07        Author: chashen   Description: 检查位图的指定位是否设置                 Input: BITMAP_S *pstBitmap, 位图指针                 unsigned int uiPos,  指定的检查位        Output: none        Return: 1, 指定位设置                0, 指定位没有设置       Caution:  ----------------------------------------------------------------    Modifiction History     DATE                NAME                DESCRIPTION    ------------------------------------------------------------    YYYY-MM-DD  ----------------------------------------------------------------********************************************************************/int Bitmap_Check(BITMAP_S *pstBitmap, unsigned int uiPos){    int iRet = 0;    if ((pstBitmap->aucPtr[uiPos / 8] & (0x80 >> (uiPos % 8))) != 0)    {        iRet = 1;    }        return iRet;}