bitcount源码解析(bitcount函数)
本文目录一览:
impixelinfo或者实时提取像素的函数怎么编写
#includestdio.h
int main()
{
int width,height,x,y;
unsigned short bitCount;
int offbits;
int bitPerLine;
unsigned char data;
FILE* bmpfp = fopen("E:\\风景\\风景1.bmp","rb");
fseek(bmpfp,18,SEEK_SET);
fread(width,sizeof(int),1,bmpfp);
fread(height,sizeof(int),1,bmpfp);
printf("width : %d , height : %d\n",width,height);
fseek(bmpfp,2,SEEK_CUR);
fread(bitCount,sizeof(bitCount),1,bmpfp);
fseek(bmpfp,10,SEEK_SET);
fread(offbits,sizeof(int),1,bmpfp);
if(bitCount==24){
bitPerLine = ( (width*3)%4==0 ) ? width*3 : ( (width*3)/4 )*4 + 4;
while(1){
printf("请输出坐标:");
scanf("%d%d",x,y);
if(xwidth||yheight) return 0;
fseek(bmpfp, 18 + offbits + bitPerLine * y + 3*x , SEEK_SET);
fread(data,sizeof(data),1,bmpfp);
printf("该点蓝色分量:%d",data);
fread(data,sizeof(data),1,bmpfp);
printf("该点绿色分量:%d",data);
fread(data,sizeof(data),1,bmpfp);
printf("该点红色分量:%d\n",data);
}
}else{
printf("不是真彩位图!");
}
}
/*
运行结果:
width : 700 , height : 382
请输出坐标:0 0
该点蓝色分量:68该点绿色分量:82该点红色分量:80
游程编码源代码
这个...........楼上的诸位说的都是什么啊。今天刚好看到这个问题,把你的E-mail给我把,我有纯c的源码(RLC)。
算了,直接贴关键部分吧。这个有一点C++成分,很容易改的。
bool CompressRunLength(BYTE *pSrc, int nSrcLen, BYTE *pDes, int nDesLen, int nBitsPerSample, void* nRuns, int nRunCount, int nRunSize);
bool DecompressRunLength(BYTE *pSrc, int nSrcLen, BYTE *pDes, int nDesLen);
#define GetDWORD(buf,bit,mask) ((*(DWORD*)(((BYTE*)buf)+((bit)3)))((bit)7)(mask))
#define GetWORD(buf,bit,mask) ((*(WORD*)(((BYTE*)buf)+((bit)3)))((bit)7)(mask))
int GetBitCount(int n)
{
int nBitCount = 0;
while(n)
n = 1, nBitCount++;
return nBitCount;
}
int BinarySearch(void* pValue, int nVlaueSize, void* pArray, int nCount)
{
int nIndex, nResult, nStart = 0, nEnd = nCount-1;
while(nStart = nEnd)
{
nIndex = (nEnd+nStart)/2;
if((nResult = memcmp((BYTE*)pArray+nIndex*nVlaueSize, pValue, nVlaueSize)) == 0)
return nIndex;
if(nResult 0)
nEnd = nIndex-1;
else
nStart = nIndex+1;
}
return -1;
}
bool CompressRunLength(BYTE *pSrc, int nSrcLen, BYTE *pDes, int nDesLen, int nBitsPerSample, void* nRuns, int nRunCount, int nRunSize)
{
pDes = (BYTE*)malloc(nSrcLen*2);
memset(pDes, 0, nSrcLen*2);
nDesLen = sizeof(DWORD);
*(DWORD*)pDes = nSrcLen; // save source length
*(pDes+nDesLen++) = nBitsPerSample; // save bits per sample
*(pDes+nDesLen++) = nRunCount; // save runs count
*(pDes+nDesLen++) = nRunSize; // save run bytes
memcpy(pDes+nDesLen, nRuns, nRunCount*nRunSize); // save runs
nDesLen += nRunCount*nRunSize;
nDesLen = 3; // bytes to bits
if(nRunCount == 0)
nRunCount = 256, nRunSize = 1, nRuns = NULL;
int nBitsPerTypeIndex = GetBitCount(nRunCount-1);
int nMaxRunLength = (1 nBitsPerSample)-1, nRunLength, nRunIndex, nByte = 0;
// loop in the source buffer
while(nByte nSrcLen)
if((nRuns (nRunIndex = BinarySearch(pSrc+nByte, nRunSize, nRuns, nRunCount)) != -1
memcmp(pSrc+nByte+nRunSize, (BYTE*)nRuns+nRunIndex*nRunSize, nRunSize) == 0) ||
(!nRuns (nRunIndex = *(pSrc+nByte)) == *(pSrc+nByte+1)))
{ // set bit to 1 to indicate type found
*(pDes+(nDesLen3)) |= 1 (nDesLen7);
*(DWORD*)(pDes+(++nDesLen3)) |= nRunIndex (nDesLen7);
nDesLen += nBitsPerTypeIndex;
// skip the two repeated runs
nByte += nRunSize*2;
// get run length - 2 (without the two repeated runs)
nRunLength = 0;
while(nRunLength nMaxRunLength nByte nSrcLen
((nRuns memcmp(pSrc+nByte, (BYTE*)nRuns+nRunIndex*nRunSize, nRunSize) == 0) || (!nRuns (BYTE)nRunIndex == *(pSrc+nByte))))
nRunLength++, nByte += nRunSize;
// save run length and increment destination length by bits per sample
*(DWORD*)(pDes+(nDesLen3)) |= nRunLength (nDesLen7);
nDesLen += nBitsPerSample;
}
else // copy one byte
*(WORD*)(pDes+(++nDesLen3)) |= *(pSrc+nByte++) (nDesLen7), nDesLen += 8;
nDesLen = (nDesLen+7)/8; // bits to bytes
pDes = (BYTE*)realloc(pDes, nDesLen);
return true;
}
bool DecompressRunLength(BYTE *pSrc, int nSrcLen, BYTE *pDes, int nDesLen)
{
if(nSrcLen == 0)
return true;
// allocate destination buffer
nDesLen = *(DWORD*)pSrc;
pDes = (BYTE*)malloc(nDesLen);
memset(pDes, 0, nDesLen);
// copy compression information
int nSrcIndex = sizeof(DWORD);
int nBitsPerSample = *(pSrc+nSrcIndex++);
int nRunCount = *(pSrc+nSrcIndex++);
int nRunSize = *(pSrc+nSrcIndex++);
void* nRuns = pSrc+nSrcIndex;
nSrcIndex += nRunSize*nRunCount;
nSrcIndex = 3; // bytes to bits
if(nRunCount == 0)
nRunCount = 256, nRunSize = 1, nRuns = NULL;
int nBitsPerTypeIndex = GetBitCount(nRunCount-1);
int nMaxTypeIndex = (1 nBitsPerTypeIndex)-1;
int nMaxRunLength = (1 nBitsPerSample)-1;
int nDesIndex = 0, nRunLength, nRunIndex, nRun, nByte;
nSrcLen = 3; // bytes to bits
while(nSrcIndex nSrcLen-8)
if((*(pSrc+(nSrcIndex3)) (nSrcIndex++7)) 1)
{
nRunIndex = GetDWORD(pSrc, nSrcIndex, nMaxTypeIndex), nSrcIndex += nBitsPerTypeIndex;
nRunLength = GetDWORD(pSrc, nSrcIndex, nMaxRunLength)+2, nSrcIndex += nBitsPerSample;
for(nRun = 0; nRun nRunLength; nRun++)
for(nByte = 0; nByte nRunSize; nByte++, nDesIndex += 8)
*(WORD*)(pDes+(nDesIndex3)) |= nRuns ? GetWORD(nRuns+nRunSize*nRunIndex, nByte3, 0xff) : (BYTE)nRunIndex;
}
else // copy one byte
*(WORD*)(pDes+(nDesIndex3)) |= GetWORD(pSrc, nSrcIndex, 0xff), nDesIndex += 8, nSrcIndex += 8;
return true;
}
java中hashMap的默认大小为什么是2的幂
在hashmap的源码中。put方法会调用indexFor(int h, int length)方法,这个方法主要是根据key的hash值找到这个entry在table中的位置,源码如下:
/**
* Returns index for hash code h.
*/
static int indexFor(int h, int length) {
// assert Integer.bitCount(length) == 1 : "length must be a non-zero power of 2";
return h (length-1);
}
注意最后return的是h(length-1)。
如果length不为2的幂,比如15。那么length-1的2进制就会变成1110。在h为随机数的情况下,和1110做操作。尾数永远为0。那么0001、1001、1101等尾数为1的位置就永远不可能被entry占用。这样会造成浪费,不随机等问题。