[英]Writing bitmap in C
This is my code for a bitmap image but it dosen't work and I have no idea why. 这是我的位图图像代码,但是它不起作用,我也不知道为什么。 I previously did something the same but I was reading an original bitmap and I had only to change its colors.
以前我做过同样的事情,但是我正在阅读原始位图,而只需要更改其颜色即可。 The code was exactly the same thought and it did work that time but for reading an image from 0 it seems it didn't work.
该代码是完全相同的思想,并且在那个时候确实起作用,但是对于从0读取图像来说,似乎不起作用。 I searched a lot on the internet for answers and i think maybe it's a problem with data from headers or something about padding.
我在互联网上搜索了很多答案,我认为这可能是标题中的数据或有关填充的问题。 Can you help me, please?
你能帮我吗?
Header: 标题:
#pragma pack(1)
struct bmp_fileheader
{
unsigned char fileMarker1; /* 'B' */
unsigned char fileMarker2; /* 'M' */
unsigned int bfSize; /* File's size */
unsigned short unused1;
unsigned short unused2;
unsigned int imageDataOffset; /* Offset to the start of image data */
};
struct bmp_infoheader
{
unsigned int biSize; /* Size of the info header - 40 bytes */
signed int width; /* Width of the image */
signed int height; /* Height of the image */
unsigned short planes;
unsigned short bitPix;
unsigned int biCompression;
unsigned int biSizeImage; /* Size of the image data */
int biXPelsPerMeter;
int biYPelsPerMeter;
unsigned int biClrUsed;
unsigned int biClrImportant;
};
#pragma pack()
Code: 码:
#include <stdio.h>
#include <stdlib.h>
#include "bmp_header.h"
typedef struct {
unsigned char b;
unsigned char g;
unsigned char r;
}rgb;
int main() {
int i, j, k;
struct bmp_fileheader a;
struct bmp_infoheader b;
FILE * p = fopen("img.bmp", "wb");
a.fileMarker1 = 'B';
a.fileMarker2 = 'M';
a.unused1=0;
a.unused2=0;
a.bfSize = 54+3*40*40;
a.imageDataOffset=54;
b.biSize = 40;
b.width = 40;
b.width = 40;
b.planes = 1;
b.bitPix = 24;
b.biCompression = 0;
b.biSizeImage = 4*40*40;
b.biXPelsPerMeter = 0;
b.biYPelsPerMeter = 0;
b.biClrUsed = 0;
b.biClrImportant = 0;
rgb matrix[40][40];
for (i = 0; i < 10; i++) {
for (j = 0; j < 40; j++) {
matrix[i][j].b = 255;
matrix[i][j].g = 255;
matrix[i][j].r = 255;
}
}
for (i = 10; i < 30; i++) {
for (j = 0; j < 40; j++) {
if (j < 10 || j > 29) {
matrix[i][j].b = 255;
matrix[i][j].g = 255;
matrix[i][j].r = 255;
} else {
matrix[i][j].b = 255;
matrix[i][j].g = 255;
matrix[i][j].r = 0;
}
}
}
for (i = 30; i < 40; i++) {
for (j = 0; j < 40; j++) {
matrix[i][j].b = 255;
matrix[i][j].g = 255;
matrix[i][j].r = 255;
}
}
fwrite( & a, sizeof(a), 1, p);
fwrite( & b, sizeof(b), 1, p);
int t = (4 - (40 * 3)%4)%4;//padding equation found on the internet
for(i = 0; i < 40; i++){
for(j = 0; j < 40; j++)
fwrite(&matrix[i][j], sizeof(matrix[i][j]), 1, p);
for(k = 0; k < t; k++){
putc(0,p); //padding
}
}
fclose(p);
}
The scanline size is calculated as: 扫描线大小的计算公式为:
#define WIDTHBYTES(bits) (((bits) + 31) / 32 * 4)
The biSizeImage
field is computed as: biSizeImage
字段的计算公式为:
b.biSizeImage= WIDTHBYTES(xRes*24) * yRes;
The 24
is the number of bits per pixel. 24
是每个像素的位数。
You also set b.width
twice. 您还要设置
b.width
两次。 The second one should be height
. 第二个应该是
height
。
fheader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof (BITMAPINFOHEADER) + 0/*no palette for 24 bits color*/; fheader.bfSize = fheader.bfOffBits + iheader.biSizeImage;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.