C 用黑色填充所有位图1bpp
我尝试用黑色填充所有位图1bpp,但仍有一些像素遗漏 我用C编写了这段代码:C 用黑色填充所有位图1bpp,c,bitmap,fill,C,Bitmap,Fill,我尝试用黑色填充所有位图1bpp,但仍有一些像素遗漏 我用C编写了这段代码: void fillBlack(void* img, int width, int height) { int i=0,j=0; for(i=0;i<width;i++) { for(j=0;j<(height);j+=8) { *(char*)(img)=(*((char*)(img))^0xff); img++; } } } void fi
void fillBlack(void* img, int width, int height)
{
int i=0,j=0;
for(i=0;i<width;i++)
{
for(j=0;j<(height);j+=8)
{
*(char*)(img)=(*((char*)(img))^0xff);
img++;
}
}
}
void fillBlack(void*img,int-width,int-height)
{
int i=0,j=0;
对于(i=0;i假设0表示黑色,则XOR将翻转任何区域的值。假设您有一个二进制值为00001000
的1字节区域:
black black black black white black black black
white white white white white white white white
-----------------------------------------------
white white white white black white white white
将区域归零的等效且更有效的方法是:
char *bmp = img;
memset(bmp, 0, width * (height / 8));
如果1表示黑色,0表示白色(出于某种奇怪的原因):
在这两种情况下,您都不希望对值进行异或运算。异或运算会翻转不匹配的值。您只需要执行赋值。因此,如果出于某种原因您没有memset:
void
fillBlack(void *img, int width, int height)
{
char *bmp = img;
int i=0,j=0;
for (i = 0; i < width; i++) {
for(j = 0; j < height; j += 8) {
*bmp++ = 0;
}
}
}
void
fillBlack(空*内模,内模宽度,内模高度)
{
char*bmp=img;
int i=0,j=0;
对于(i=0;i
(同样,如果黑色在您的格式中不是0,则将*bmp++=0;
更改为*bmp++=1;
)
memset(3)
可能在您的系统上进行了优化,可以一次处理多个字节,假设您运行的是现代操作系统,并且有一个支持这类操作的CPU。每行像素必须是4个字节的倍数
Each row of pixels must be a multiple of 4 bytes
suggest:
void fillBlack(void* img, int width, int height)
{
char *pImage = img;
int i=0; // loop counter, image height in pixels
int j=0; // loop counter, image width in pixels
int pad=0; // loop counter, pad bytes at end of each row of pixels
for(i=0;i<height;i++)
{
for(j=0;j<width;j++)
{
*pImage = 0x00;
pImage++; // step to next byte in row
} // end for - width
// there can be some filler bytes on the end of rows
// as rows must be a multiple of 4 bytes
for(pad=0; 0 != (width+pad)%4; pad++)
{
*pImage = 0x00; // set pad byte
pImage++; // step by pad byte
} // end for - pad bytes
} // end for - height
} // end function: fillBlack
建议:
空心填充黑色(空心*img,整数宽度,整数高度)
{
char*pImage=img;
int i=0;//循环计数器,图像高度(像素)
int j=0;//循环计数器,图像宽度(像素)
int pad=0;//循环计数器,在每行像素的末尾填充字节
对于(i=0;i位图图像由带像素的扫描线组成。扫描线向上舍入到最近的字。假设每个字32位:
fillBlack(void *img, int width, int height)
{
char *bmp = img;
int i,j, scanlinebytes;
scanlinebytes= ((width*1)+31) / 32 * 4; // 1 = bits per pixel
for (i = 0; i < height; i++) {
for(j = 0; j < scanlinebytes; j++) {
*bmp++ = 0;
}
}
}
fillBlack(空*img,整数宽度,整数高度)
{
char*bmp=img;
int i,j,扫描线字节;
扫描线字节=((宽度*1)+31)/32*4;//1=每像素位
对于(i=0;i
难道你不认为你滥用了一点偏执狂的用法吗!我想你是想为(j=0;…
,而不是为(j=k;…
。需要更多的括号…;)做,但这不太管用(好主意)因为.BMP图像可以在每行像素的末尾有pad字节。memset可以正常工作,但有2个循环(用于)仍然保留白色像素。什么是pad字节?您是否可以提供一组最少的代码来演示问题,以及一个说明问题的输入位图?恐怕这会导致分割错误。但当我将j++更改为j+=8时,它仍然保留白色像素。
fillBlack(void *img, int width, int height)
{
char *bmp = img;
int i,j, scanlinebytes;
scanlinebytes= ((width*1)+31) / 32 * 4; // 1 = bits per pixel
for (i = 0; i < height; i++) {
for(j = 0; j < scanlinebytes; j++) {
*bmp++ = 0;
}
}
}