小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

BMP圖片讀寫函數(shù)

 印度阿三17 2019-03-20

BMP讀函數(shù):

 #include <stdio.h>
 #include <fcntl.h>

typedef unsigned short U16;
typedef unsigned long  U32;
#pragma pack(1) //設(shè)置1字節(jié)對齊模式,pack()將對齊模式取消
/*位圖文件頭*/
typedef struct BMP_FILE_HEADER
{
	U16 bType;             /*  文件標識符          */
	U32 bfSize;            /*  文件的大小,單位字節(jié) */
	U16 bReserved1;        /*  保留值,必須設(shè)置為0  */
	U16 bReserved2;        /*  保留值,必須設(shè)置為0  */
	U32 bOffset;           /*  文件頭的最后到圖像數(shù)據(jù)位開始的偏移量    */
} BMPFILEHEADER;

/*位圖信息頭*/
typedef struct BMP_INFO
{
	U32 bInfoSize;        /*  信息頭的大小             */
	U32 bWidth;           /*  圖像的寬度               */
	U32 bHeight;          /*  圖像的高度               */
	U16 bPlanes;          /*  圖像的位面數(shù)             */
	U16 bBitCount;        /*  每個像素的位數(shù)           */
	U32 bCompression;     /*  壓縮類型                 */
	U32 bmpImageSize;     /*  圖像的大小,以字節(jié)為單位   */
	U32 bXPelsPerMeter;   /*  水平分辨率               */
	U32 bYPelsPerMeter;   /*  垂直分辨率               */
	U32 bClrUsed;         /*  使用的色彩數(shù)             */
	U32 bClrImportant;    /*  重要的顏色數(shù)             */
} BMPINF;
#pragma pack()
/*彩色表*/
typedef struct RGB_QUAD
{
	U16 rgbBlue;         /*  藍色強度  */
	U16 rgbGreen;        /*  綠色強度  */
	U16 rgbRed;          /*  紅色強度  */
	U16 rgbReversed;     /*  保留值    */
} RGBQUAD;


int read_bmp(const char *path, char *img_src_r, char * img_src_g, char * img_src_b, int *width, int *height)
{
	FILE* fp;
	BMPFILEHEADER fileHeader = { 0 };
	BMPINF infoHeader = { 0 };
	long offset, bmpImageSize, bytesPerPixel, size, bitCount;
	int i = 0, j = 0, count = 0;
	int lcount = 0;
	int cl[3] = { 0 };
	U16 c;
	char *bmpData;
	int lineByte = 0;
	fp = fopen(path, "rb");
	if (fp == NULL) {
		return -1;
	}

	fread(&fileHeader, sizeof(BMPFILEHEADER), 1, fp);
	fread(&infoHeader, sizeof(BMPINF), 1, fp);
	//定義變量,計算圖像每行像素所占的字節(jié)數(shù)(必須是4的倍數(shù))  
	lineByte = (infoHeader.bWidth * infoHeader.bBitCount / 8   3) / 4 * 4;

	bmpData = (char *)malloc((infoHeader.bHeight*infoHeader.bWidth * 3) * sizeof(char));
	fseek(fp, fileHeader.bOffset, SEEK_SET);
	fread(bmpData, 1, infoHeader.bHeight*infoHeader.bWidth * 3, fp);

	for (i = 0; i < infoHeader.bHeight;   i) {
		for (j = 0; j < infoHeader.bWidth;   j) {
			img_src_b[i*infoHeader.bWidth   j] = bmpData[i*infoHeader.bWidth * 3   j * 3];
			img_src_g[i*infoHeader.bWidth   j] = bmpData[i*infoHeader.bWidth * 3   j * 3   1];
			img_src_r[i*infoHeader.bWidth   j] = bmpData[i*infoHeader.bWidth * 3   j * 3   2];

		}
	}

	free(bmpData);
	fclose(fp);

	return 0;
}

寫函數(shù)

void saveBmp(char * filename, unsigned char * imageData, int height, int width, int channels)
{
bmpFileHead bh;
bmpHeadInfo bi;
RGBQUAD rq[256];
FILE * fout = fopen(filename, "wb");
unsigned short fileType = 0x4D42;
fwrite(&fileType, sizeof(unsigned short), 1, fout);

int step = channels * width;
int offset = step % 4;
if (offset != 0)
	step  = (4 - offset);

if (channels == 1)
	bh.bfSize = height * step   1078;
else
	bh.bfSize = height * step   54;
bh.bfReserved1 = 0;
bh.bfReserved2 = 0;
if (channels == 1)
	bh.bfOffBits = 1078;
else
	bh.bfOffBits = 54;
fwrite(&bh, sizeof(bmpFileHead), 1, fout);

//  printf("%d\n", (int)bh.bfType);
//  printf("%ld\n", bh.bfSize);
//  printf("%d\n", (int)bh.bfReserved1);
//  printf("%d\n", (int)bh.bfReserved2);
//  printf("%ld\n", bh.bfOffBits);

int r;
for (r = 0; r < 256; r  )
{
	rq[r].rgbBlue = rq[r].rgbGreen = rq[r].rgbRed = r;
	rq[r].rgbReserved = 0;
	//  printf("%d\n", rq[r].rgbBlue);
	//  printf("%d\n", rq[r].rgbGreen);
	//  printf("%d\n", rq[r].rgbRed);
	//  printf("%d\n", rq[r].rgbReserved);
}


bi.biSize = 40;
bi.biWidth = width;
//bi.biHeight =-height;
bi.biHeight = height; //hujianhua
bi.biPlanes = 1;
bi.biBitCount = 8 * channels;
bi.biCompression = 0;
bi.biSizeImage = height*step;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;

//  printf("%ld\n", bi.biSize);
//  printf("%ld\n", bi.biWidth);
//  printf("%ld\n", bi.biHeight);
//  printf("%d\n", bi.biPlanes);
//  printf("%d\n", bi.biBitCount);
//  printf("%ld\n", bi.biCompression);
//  printf("%ld\n", bi.biSizeImage);
//  printf("%ld\n", bi.biXPelsPerMeter);
//  printf("%ld\n", bi.biYPelsPerMeter);
//  printf("%ld\n", bi.biClrUsed);
//  printf("%ld\n", bi.biClrImportant);

fwrite(&bi, sizeof(bmpHeadInfo), 1, fout);
if (channels == 1)
	fwrite(&rq, 4, 256, fout);

//unsigned char dc[channels];
unsigned char def = 0;
//  fseek(fout, 1078, SEEK_SET);

int i, j, c;
for (i = height - 1; i >= 0; i--)
{
	for (j = 0; j < width; j  )
	{
		for (c = 0; c < channels; c  )
		{
			int value = imageData[(i * width   j) * channels   c];
			//  if(value > 255)
			//      printf("%d\t", value);
			fwrite(&imageData[(i * width   j) * channels   c], sizeof(unsigned char), 1, fout);
		}
	}
	if (offset != 0)
	{
		for (j = 0; j < 4 - offset; j  )
		{
			fwrite(&def, sizeof(unsigned char), 1, fout);
		}
	}
}
fclose(fout);

}

來源:http://www./content-4-144351.html

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多