英文:
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->header.height+(padding*2);
int new_width=image->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<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<new_height;i++){
for(int j=0;j<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<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;
}
}
image->header.height=new_height;
image->header.width=new_width;
for(int i=0;i<image->header.height-2*padding;i++){
free(image->pixels[i]);
}
free(image->pixels);
image->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<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;
}
}
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论