📄 graybmp.c
字号:
//#include "stdafx.h"#include <stdio.h>#include <stdlib.h>#include "graybmp.h"#include <assert.h>#define GRAYBMP_HEADSIZE 1078;const unsigned char BitmapFlag[2]={'B','M'};typedef struct tagBMPH{ unsigned long bfSize; unsigned short bfReserved1; unsigned short bfReserved2; unsigned long bfOffBits;}BMPH;typedef struct tagBMPI{ unsigned long biSize; unsigned long biWidth; unsigned long biHeight; unsigned short biPlanes; unsigned short biBitCount; unsigned long biCompression; unsigned long biSizeImage; unsigned long biXPelsPerMeter; unsigned long biYPelsPerMeter; unsigned long biClrUsed; unsigned long biClrImportant;}BMPI;typedef struct tagPALETTE{ unsigned char rgbRed; unsigned char rgbGreen; unsigned char rgbBlue; unsigned char rgbReserved;}PALETTE;int WriteGrayBMPFile(char *filename,unsigned char *tempdata, unsigned int width,unsigned int height){ int i,j; FILE *fp; BMPH bitMFH; BMPI bitMIH; PALETTE RgbQuad; if((fp=fopen(filename,"wb"))==NULL) return(-1); fwrite(BitmapFlag,2,1,fp); bitMFH.bfSize = width*height +GRAYBMP_HEADSIZE; bitMFH.bfReserved1 = 0; bitMFH.bfReserved2 = 0; bitMFH.bfOffBits = GRAYBMP_HEADSIZE; fwrite(&bitMFH,sizeof(bitMFH),1,fp); bitMIH.biSize = 40; bitMIH.biWidth = width; bitMIH.biHeight = height; bitMIH.biPlanes = 1; bitMIH.biBitCount = 8; bitMIH.biCompression = 0; bitMIH.biSizeImage = width*height; bitMIH.biXPelsPerMeter = 0; bitMIH.biYPelsPerMeter = 0; bitMIH.biClrUsed = 0; bitMIH.biClrImportant = 0; fwrite(&bitMIH,sizeof(bitMIH),1,fp); for(i = 0;i<256;i++){ RgbQuad.rgbRed = i; RgbQuad.rgbGreen = i; RgbQuad.rgbBlue = i; RgbQuad.rgbReserved = i; fwrite(&RgbQuad,4,1,fp); } //write the data for(i=height-1;i>=0;i--) for(j=0;j<width;j++) fwrite(tempdata+width*i+j,sizeof(unsigned char),1,fp); fclose(fp); return 0;}void* ReadGrayBMPFile(char *FileName, unsigned int * nwidth, unsigned int *nheight){ FILE *fp; int i, j; unsigned char temp; unsigned char *pImageData; int offset, depth, width; if((fp=fopen(FileName, "rb"))==NULL) return 0; fseek(fp, 10, SEEK_SET); fread(&offset, sizeof(long), 1, fp); fseek(fp, 18, SEEK_SET); fread(&width, sizeof(long), 1, fp); fread(&depth, sizeof(long), 1, fp); if((pImageData=(unsigned char *)malloc(sizeof(unsigned char)*width*depth))==0){ fclose(fp); return 0; } fseek(fp,offset,SEEK_SET); for(i=depth-1; i>=0; i--) for(j=0; j<width; j++) { temp=fgetc(fp); *(pImageData+i*width+j)=temp; } fclose(fp); *nwidth=width; *nheight=depth; return(pImageData);}int SliceGrayBMP(unsigned char * olddata,int oldwidth,int oldheight,unsigned char *newdata,int left,int top,int width,int height){ int i; assert(oldwidth>=width+left&&left<=width); assert(oldheight>=height+top&&top<=height); for(i=0;i<height;i++) memcpy(newdata+i*width,olddata+(i+top)*oldwidth+left,width*sizeof(unsigned char)); return(0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -