📄 bmp.c
字号:
fwrite((void *)&bf->bfType, 1, sizeof(WORD), fp);
fwrite((void *)&bf->bfSize, sizeof(DWORD), 1, fp);
fwrite((void *)&bf->bfReserved1, sizeof(WORD), 1, fp);
fwrite((void *)&bf->bfReserved2, sizeof(WORD), 1, fp);
fwrite((void *)&bf->bfOffBits, sizeof(DWORD), 1, fp);
fwrite((void *)&bi->biSize, sizeof(DWORD), 1, fp);
fwrite((void *)&bi->biWidth, sizeof(LONG), 1, fp);
fwrite((void *)&bi->biHeight, sizeof(LONG), 1, fp);
fwrite((void *)&bi->biPlanes, sizeof(WORD), 1, fp);
fwrite((void *)&bi->biBitCount, sizeof(WORD), 1, fp);
fwrite((void *)&bi->biCompression, sizeof(DWORD), 1, fp);
fwrite((void *)&bi->biSizeImage, sizeof(DWORD), 1, fp);
fwrite((void *)&bi->biXPelsPerMeter, sizeof(LONG), 1, fp);
fwrite((void *)&bi->biYPelsPerMeter, sizeof(LONG), 1, fp);
fwrite((void *)&bi->biClrUsed, sizeof(DWORD), 1, fp);
fwrite((void *)&bi->biClrImportant, sizeof(DWORD), 1, fp);
fseek(fp, HEADEROFFSET, SEEK_SET);
for (i = bi->biHeight-1;i >=0; i--)
{
for (j = 0, k = 0; j < real_width; j++)
{
memset((void *)&buf, 0, sizeof(BYTE));
if (j < image_byte_width)
buf = Data->data[i][k++];
fwrite((void *)&buf, sizeof(BYTE), 1, fp);
}
}
printf("\nBMP8 file write complete!\n");
fclose(fp);
return 0;
}
void ImrectToBMP8 (Imrect* im , const char* filename)
{
FILE *fp;
const WORD colorBits = 8;
Imregion* roi;
int i , j ,byteswrite , lx ,ux ,ly ,uy;
int width ,height , len , pix ;
unsigned char byteBuf[54];
unsigned long* dwordBuf;
unsigned short* wordBuf;
fp = fopen( filename ,"wb");
if (fp == NULL )
{
error("file open error ", warning );
return;
}
/*write file header */
roi = im->region;
if (roi == NULL)
return ;
lx = roi->lx;
ux = roi->ux;
ly = roi->ly;
uy = roi->uy;
width = im->width;
height = im->height;
wordBuf = (unsigned short*)byteBuf;
dwordBuf = ( unsigned long*)&byteBuf[2];
byteBuf[0] = 'B';
byteBuf[1] = 'M';
wordBuf[3] = 0; /*write bfReserved1*/
wordBuf[4] = 0;
dwordBuf[2] = 1078L;/*write offBits */
dwordBuf[3] = 40L;
dwordBuf[4] = width;
dwordBuf[5] = height;
wordBuf[13] = 1;/*write biPlanes*/
wordBuf[14] = colorBits;/*this only write 8 bit image*/
dwordBuf[7] = 0L;/*write commpression mode */
dwordBuf[11]= 0L;
dwordBuf[12] = 0L;
len = fwrite( byteBuf , 1 ,54 ,fp);
if (len != 54 )
{
error("write 8 bit bmp file error " ,warning );
return;
}
byteswrite = 0;
/* write color maps*/
int colorBytes;
colorBytes = 1<< colorBits;
for ( i = 0 ; i < colorBytes ; i++ )
{
fputc( i ,fp );
fputc( i , fp );
fputc( i , fp);
fputc( 0 , fp);
}
/*write image data */
for ( i = height - 1 ;i >= 0 ; i-- )
{
for ( j = 0 ; j < width ; j++ )
{
IM_PIX_GET( im , i , j ,pix);
fputc( pix , fp );
byteswrite ++;
}
while ( byteswrite & 0x03 )
{
fputc( 0 ,fp);
byteswrite++;
}
}
fclose(fp);
printf("=== save BMP8 success!!! ===\n");
}
RGB * getRGB_bmp24(const char * filename, BMPHEADER * header)
{
BITMAPFILEHEADER * bf = &header->BFH;
BITMAPINFOHEADER * bi = &header->BIH;
FILE * fp;
BYTE buf;
int i, j, k;
int cnt = 0;
LONG image_byte_width;
LONG real_width;
DWORD rows, cols;
DATA_MATRIX * data_b, * data_r, * data_g;
RGB * rgb_matrix = (RGB *)malloc(sizeof(RGB));
if(filename == NULL || (fp = fopen(filename, "r")) == NULL) /*Open the file*/
{
printf("File %s not found!\n", filename);
return NULL;
}
if(bi->biClrUsed != 0 || bi->biBitCount != 24) /* Check: whether or not 24-bits*/
{
printf("File %s is not an image of 24 bits!\n", filename);
return NULL;
}
/* Preparing */
rows = bi->biHeight;
cols = bi->biWidth;
data_r = DATA_MATRIX_new(rows, cols);
data_g = DATA_MATRIX_new(rows, cols);
data_b = DATA_MATRIX_new(rows, cols);
rgb_matrix->blue = data_b;
rgb_matrix->green = data_g;
rgb_matrix->red = data_r;
image_byte_width = bi->biWidth * 3;
real_width = WIDTHBYTES(bi->biWidth * bi->biBitCount);
printf("\nThe Image real width =%ld \n",real_width);
printf("\n");
/*Read Image-Data*/
fseek(fp, bf->bfOffBits, SEEK_SET);
for (i = 0; i < rows&&!feof(fp); i++)
{
for (j = 0, k = 0; j < real_width&&!feof(fp); j++)
{
memset((void *)&buf, 0, sizeof(BYTE));
fread((void *)&buf,sizeof(BYTE),1, fp);
if (j < image_byte_width)
{
/* printf("%4d", buf); */ /* print color data */
cnt++;
if ((j + 1) % 3 == 1) data_r->data[rows-i-1][k] = buf;
if ((j + 1) % 3 == 2) data_g->data[rows-i-1][k] = buf;
if ((j + 1) % 3 == 0)
{
data_b->data[rows-i-1][k++] = buf;
/* printf("\t"); */
}
}
}
/* printf("\n"); */
}
printf("\n\n The image color num cnt = %d\n", cnt);
return rgb_matrix;
}
void RGB_print(const RGB * rgb)
{
int i, j;
DATA_MATRIX * b = (DATA_MATRIX *)rgb->blue;
DATA_MATRIX * r = (DATA_MATRIX *)rgb->red;
DATA_MATRIX * g = (DATA_MATRIX *)rgb->green;
if (b->rows != r->rows || b->rows != g->rows || g->rows != r->rows ||
b->cols != r->cols || b->cols != g->cols || g->cols != r->cols)
return;
printf("\nMatrix's rows = %d; Matrix's cols = %d\n", b->rows, b->cols);
printf("=============Matrix's data=================\n");
for (i = 0; i < b->rows; i++) {
for (j = 0; j < b->cols; j++)
printf("%4d%4d%4d\t", r->data[i][j], g->data[i][j], b->data[i][j]);
printf("\n");
}
printf("=================end disp===================\n");
return;
}
int writeBMP24(const BMPHEADER * header,const RGB *rgb_matrix,const char * filename)
{
BMPHEADER * head = (BMPHEADER *)header;
BITMAPFILEHEADER * bf = &head->BFH;
BITMAPINFOHEADER * bi = &head->BIH;
FILE * fp;
BYTE buf;
int i, j, k;
int cnt = 0;
long image_byte_width;
long real_width;
int rows, cols;
DATA_MATRIX * data_b, * data_r, * data_g;
if((head == NULL)||(rgb_matrix == NULL))
{
printf("File read error!\n");
return -1;
}
if(filename == NULL || (fp = fopen(filename, "w")) == NULL)
{
printf("File %s not found!\n", filename);
return -1;
}
rows = bi->biHeight;
cols = bi->biWidth;
data_r = DATA_MATRIX_new(rows, cols);
data_g = DATA_MATRIX_new(rows, cols);
data_b = DATA_MATRIX_new(rows, cols);
data_b = (DATA_MATRIX *)rgb_matrix->blue;
data_g = (DATA_MATRIX *)rgb_matrix->green;
data_r = (DATA_MATRIX *)rgb_matrix->red;
image_byte_width = bi->biWidth * 3;
real_width = WIDTHBYTES(bi->biWidth * bi->biBitCount);
fwrite((void *)&bf->bfType, 1, sizeof(WORD), fp);
fwrite((void *)&bf->bfSize, sizeof(DWORD), 1, fp);
fwrite((void *)&bf->bfReserved1, sizeof(WORD), 1, fp);
fwrite((void *)&bf->bfReserved2, sizeof(WORD), 1, fp);
fwrite((void *)&bf->bfOffBits, sizeof(DWORD), 1, fp);
fwrite((void *)&bi->biSize, sizeof(DWORD), 1, fp);
fwrite((void *)&bi->biWidth, sizeof(LONG), 1, fp);
fwrite((void *)&bi->biHeight, sizeof(LONG), 1, fp);
fwrite((void *)&bi->biPlanes, sizeof(WORD), 1, fp);
fwrite((void *)&bi->biBitCount, sizeof(WORD), 1, fp);
fwrite((void *)&bi->biCompression, sizeof(DWORD), 1, fp);
fwrite((void *)&bi->biSizeImage, sizeof(DWORD), 1, fp);
fwrite((void *)&bi->biXPelsPerMeter, sizeof(LONG), 1, fp);
fwrite((void *)&bi->biYPelsPerMeter, sizeof(LONG), 1, fp);
fwrite((void *)&bi->biClrUsed, sizeof(DWORD), 1, fp);
fwrite((void *)&bi->biClrImportant, sizeof(DWORD), 1, fp);
fseek(fp, HEADEROFFSET, SEEK_SET);
for (i = bi->biHeight-1; i >= 0; i--)
{
k = 0;
for (j = 0; j < real_width; j++)
{
/*printf("lxh2:j = %d image_byte_width=%d\n", j,image_byte_width);*/
/* memset((void *)&buf, 0, sizeof(BYTE)); */
buf = 0;
if (j < image_byte_width)
{
if (j % 3 == 0) buf = data_r->data[i][k];
else if (j % 3 == 1) buf = data_g->data[i][k];
else /*if(j % 3 == 2)*/
{
buf = data_b->data[i][k];
k++;
}
}
fwrite((void *)&buf, sizeof(BYTE), 1, fp);
}
}
printf("\nBmp file write complete!\n");
fclose(fp);
return 0;
}
/* matrix */
DATA_MATRIX * DATA_MATRIX_new(int rows, int cols)
{
int i;
DATA_MATRIX * mat = (DATA_MATRIX *)malloc(sizeof(DATA_MATRIX *));
mat->rows = rows;
mat->cols = cols;
mat->data = malloc(rows * sizeof(float));
for (i = 0; i < rows; i++)
mat->data[i] = malloc(cols * sizeof(float));
DATA_MATRIX_assign(mat, -1);
return (void *)mat;
}
void DATA_MATRIX_free(DATA_MATRIX * mat)
{
int i;
for (i = 0; i < mat->rows; i++)
free(mat->data[i]);
free(mat->data);
free(mat);
}
void DATA_MATRIX_print(DATA_MATRIX * mat)
{
int i, j;
printf("Matrix's rows = %d; Matrix's cols = %d\n", mat->rows, mat->cols);
for (i = 0; i < mat->rows; i++) {
for (j = 0; j < mat->cols; j++)
printf("%d\t", mat->data[i][j]);
printf("\n");
}
}
void DATA_MATRIX_assign(DATA_MATRIX * mat, int value)
{
int i, j;
for (i = 0; i < mat->rows; i++)
for (j = 0; j < mat->cols; j++)
mat->data[i][j] = value;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -