在在C BMP文件,填充处理 [英] Dealing with padding in a BMP file in C

查看:155
本文介绍了在在C BMP文件,填充处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编辑C. BMP文件我的code适用于无填充BMP文件,但我有处理填充的麻烦。

有,我看过,但大部分都使用其他语言如C#和Java,所以我没有发现他们非常有用的BMP文件的几个其他问题。

下面是像素阵列看起来像什么,但更大的:

 字符bmpArray [MAX] = {B,G,R,B,G,R,B,G,R,0,0,0,
                      B,G,R,B,G,R,B,G,R,0,0,0,
                      B,G,R,B,G,R,B,G,R,0,0,0}

的零点是填充字节,使每行被4整除,这取决于图像的像素宽度。我所要做的是将这些填充字节他们是在同一个位置,只处理了B,G,R字节的方式。如果我申请编辑的填充值,产生的图像将被扭曲。

我提出,产生的填充字节的基础上的宽度的量的功能。
它使用这个公式 4 - ((宽* 3)%4)和它的作品,因为我与不同宽度的图像进行了测试。

我成功提取了B,G,BMP文件的R数据,并把它变成一个数组所以我只会张贴我有麻烦code的一部分。

  INT C = 0;
对于(int类型的= 0; A<高度; A ++){
    对于(INT B = 0; B<宽* 3; b ++){
        如果(bmpArray〔一个*(宽* 3)+ B]所述; 127){
            bmpArray [一个*(宽* 3)+ B] = 0;
        }其他{
            bmpArray [一个*(宽* 3)+ B] = 255;
        }
        C ++;
    }
    对于(INT垫= 0;垫及LT;填充;垫++){
        bmpArray [C ++] = 0×00;
    }
}

我所试图做的是画输出BMP文件的每一行,然后当我到达行末停产了,就是宽* 3,在那之后才去绘制填充字节下一行像素。

另外,有一种方法,我可以用一个for循环识别填充像素,if语句然后用不修改的填充像素?例如:

 为(int类型的= 0; A< bmpArraySize; A ++){
    paddingBytes =? //例如用于第一行
                       // paddingBytes是1 +宽* 3 + 1
                       //和i +宽* 3 + 2和i +宽* 3 + 3如果填充= 3
    如果(α= paddingBytes){
        bmpArray [α] = 0×00;
    }
    否则,如果(bmpArray [A]< 127){
        bmpArray [一个] = 0;
    }
    其他{
        bmpArray [一个] = 255;
    }
}


解决方案

问题是这部分:

  INT C = 0;
对于(int类型的= 0; A<高度; A ++){
    对于(INT B = 0; B<宽* 3; b ++){
        如果(bmpArray〔一个*(宽* 3)+ B]所述; 127){
            bmpArray [一个*(宽* 3)+ B] = 0;
        }其他{
            bmpArray [一个*(宽* 3)+ B] = 255;
        }
    }
    对于(INT垫= 0;垫及LT;填充;垫++){
        bmpArray [C ++] = 0×00; / *只有在这里是'C'更新! * /
    }
}

在每行的末尾,你填写在填补开始ç,这在 0 开出等覆盖的第一行的前几个字节。然后,每个接下来的行被复制,但你继续从一开始就覆盖(其中 C 最初指向)。

的填充应添加的每行的。在循环中,你调整 A B ,但你忘了调整的填充。

我建议更直接的code(未经测试!):

 为(int类型的= 0; A<高度; A ++){
    对于(INT B = 0; B<宽* 3; b ++){
        如果(bmpArray [A *(宽* 3 +填充)+ B< 127){
            bmpArray [一个*(宽* 3 +填充)+ B] = 0;
        }其他{
            bmpArray [A *(宽* 3 +填充)+ B] = 255;
        }
    }
    对于(INT垫= 0;垫及LT;填充;垫++){
        bmpArray [A *(宽* 3 +填充)+ 3 *宽+ PAD] = 0×00;
    }
}


  

有没有办法,我可以找出填充像素。


是 - 我的循环上面进行填充的调整,它会自动跳过填充本身。您可以安全地在最后删除显式设置填充为0环。

I am trying to edit a BMP file in C. My code works for BMP files with no padding but I am having trouble dealing with padding.

There are a few other questions on BMP files that I have read but most of them use other languages like C# and Java so I didn't find them very useful.

Here is what the pixel array looks like, but much larger:

char bmpArray[MAX] = {B,G,R,B,G,R,B,G,R,0,0,0,
                      B,G,R,B,G,R,B,G,R,0,0,0,
                      B,G,R,B,G,R,B,G,R,0,0,0}

The zeros are for padding bytes to make each row divisible by 4, it depends on the pixel width of the image. What I am trying to do is leave these padding bytes the way they are in the same position and only deal with the B,G,R bytes. If I apply edits to the padding values, the resulting image will be distorted.

I made a function that generates the amount of padding bytes based on the width. It uses this formula 4 - ((width * 3) % 4) and it works as I tested it with images with different width.

I successfully extracted the B, G, R data of the BMP file and put it into an array so I will only post the part of the code I am having trouble with.

int c = 0;
for (int a = 0; a < height; a++) {
    for (int b = 0; b < width*3; b++) {
        if (bmpArray[a*(width*3)+b] < 127) {
            bmpArray[a*(width*3)+b] = 0;
        } else {
            bmpArray[a*(width*3)+b] = 255;
        }
        c++;
    }
    for (int pad = 0; pad < padding; pad++) {
        bmpArray[c++] = 0x00;
    }
}

What I am trying to do is "draw" each row of the output BMP file and then stop as soon as I reach the end of the row, that is width*3, then after that draw the padding bytes before going to the next row of pixels.

Alternatively, is there a way I can identify the padding pixels using a single for loop and then use an if statement to not modify the padding pixels? For example:

for (int a = 0; a < bmpArraySize; a++) {
    paddingBytes = ??? //for example for the first row
                       // paddingBytes are i + width*3 + 1
                       // and i + width*3 + 2 and i + width*3 + 3 if padding = 3
    if (a = paddingBytes) {
        bmpArray[a] = 0x00;
    }
    else if (bmpArray[a] < 127) {
        bmpArray[a] = 0;
    }
    else {
        bmpArray[a] = 255;
    }
}

解决方案

The problem is in this part:

int c = 0;
for (int a = 0; a < height; a++) {
    for (int b = 0; b < width*3; b++) {
        if (bmpArray[a*(width*3)+b] < 127) {
            bmpArray[a*(width*3)+b] = 0;
        } else {
            bmpArray[a*(width*3)+b] = 255;
        }
    }
    for (int pad = 0; pad < padding; pad++) {
        bmpArray[c++] = 0x00;  /* ONLY HERE is 'c' updated! */
    }
}

At the end of each line, you fill out the padding starting at c, which starts out at 0 and so overwrites the first few bytes of the first line. Then, each next line gets copied but you continue overwriting from the start (where c initially pointed to).

The padding should be added on each line. In the loops, you adjust a and b but you forget to adjust for the padding.

I suggest the more straightforward code (untested!):

for (int a = 0; a < height; a++) {
    for (int b = 0; b < width*3; b++) {
        if (bmpArray[a*(width*3 + padding)+b] < 127) {
            bmpArray[a*(width*3 + padding)+b] = 0;
        } else {
            bmpArray[a*(width*3 + padding)+b] = 255;
        }
    }
    for (int pad = 0; pad < padding; pad++) {
        bmpArray[a*(width*3 + padding) + 3*width + pad] = 0x00;
    }
}

is there a way I can identify the padding pixels ..

Yes – in my loop above with adjustments for padding, it automatically skips the padding itself. You can safely remove the explicit 'set padding to 0' loop at the end.

这篇关于在在C BMP文件,填充处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆