BMP图像在C中从头开始的填充

huangapple go评论103阅读模式
英文:

BMP Image padding in C from scratch

问题

我试图编写一个小型的C图像处理应用程序。目前,我正在编写一个函数,该函数向图像添加填充。当填充为偶数时,一切正常,图像具有给定数量的填充,包裹图像。如果填充为奇数,则图像会损坏。

以下是问题代码部分,您已经解决了问题:

for(int i=padding;i<image->header.height+padding;i++){
    for(int j=padding;j<image->header.width+padding;j++){
        output_pixels[i][j].red=image->pixels[i-padding][j-padding].red;
        output_pixels[i][j].green=image->pixels[i-padding][j-padding].green;
        output_pixels[i][j].blue=image->pixels[i-padding][j-padding].blue;
    }
}

您已经解决了问题,问题出在4字节对齐上。即使是偶数也总是有效的,无论它们是否对齐4字节,因为填充被添加到图像的每一侧,所以每次都会乘以2。由于偶数显然是偶数,它们至少包含2,因此它们自动对齐4字节。

这个问题已经解决,不需要进一步的翻译。

英文:

I am trying to write a small image processing app in C. Currently I am writing a function thats adds a padding to the image. When padding is even, everything works fine image has given amount of padding that wraps the image. If padding is odd image gets corrupted.

void _add_padding(struct bmp_image* image,int padding){
    //construct new images resolution with added padding.
    int new_height=image-&gt;header.height+(padding*2);
    int new_width=image-&gt;header.width+(padding*2);
    //allocate new image to the memory.
    struct pixel** output_pixels=(struct pixel**)malloc(new_height*sizeof(struct pixel*));
    for(int i=0;i&lt;new_height;i++){
        output_pixels[i]=malloc(new_width*sizeof(struct pixel));
    }
    //initialize new image with all zeros.
    int new_colour=255;
    for(int i=0;i&lt;new_height;i++){
        for(int j=0;j&lt;new_width;j++){
            output_pixels[i][j].red=new_colour;
            output_pixels[i][j].green=new_colour;
            output_pixels[i][j].blue=new_colour;
        }
    }
    for(int i=padding;i&lt;image-&gt;header.height+padding;i++){
        for(int j=padding;j&lt;image-&gt;header.width+padding;j++){
            output_pixels[i][j].red=image-&gt;pixels[i-padding][j-padding].red;
            output_pixels[i][j].green=image-&gt;pixels[i-padding][j-padding].green;
            output_pixels[i][j].blue=image-&gt;pixels[i-padding][j-padding].blue;
        }
    }
    image-&gt;header.height=new_height;
    image-&gt;header.width=new_width;
    for(int i=0;i&lt;image-&gt;header.height-2*padding;i++){
        free(image-&gt;pixels[i]);
    }
    free(image-&gt;pixels);
    image-&gt;pixels=output_pixels;
}

The image output if given padding is even: even padding img

The image output if given padding is odd: odd padding img

I am aware the problem is probably in these lines:

for(int i=padding;i&lt;image-&gt;header.height+padding;i++){
    for(int j=padding;j&lt;image-&gt;header.width+padding;j++){
       output_pixels[i][j].red=image-&gt;pixels[i-padding][j-padding].red;
       output_pixels[i][j].green=image-&gt;pixels[i-padding][j-padding].green;
       output_pixels[i][j].blue=image-&gt;pixels[i-padding][j-padding].blue;
    }
}

I've tried tinkering with the algorithm but this version (even though it produces trash half of the time) still comes as the correct algorithm for the job.

Edit: Solved the problem. Problem was the 4 byte alignment as mentioned below. The reason why even numbers would always work whether they are aligned with 4 bytes or not was simply the result of padding being added to each side of the image so thats a guarenteed multiplication by 2. Since even numbers are -obviously- even, they contain at least a 2 in them so that makes them automaticaly alligned by 4.

答案1

得分: 0

你可能陷入了旧的Windows位图陷阱:每行都必须在4字节边界上对齐,因此行间距必须是4的倍数。

英文:

Hint:

You probably fell in the old Windows Bitmap trap: every row must be aligned on a 4 bytes boundary, so the row pitch must be a multiple of 4.

huangapple
  • 本文由 发表于 2023年8月10日 19:24:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76875278.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定