BMP圖片的複製#顯示到螢幕

周半仙發表於2024-05-14

複製BMP圖片

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <stdlib.h>
/*
 *   複製bmp圖片
 *
 *
 */
#pragma pack(1)

// 定義BMP檔案頭部結構
typedef struct
{
    unsigned short bfType;
    unsigned int bfSize;
    unsigned short bfReserved1;
    unsigned short bfReserved2;
    unsigned int bfOffBits;
} BITMAPFILEHEADER;

typedef struct
{
    unsigned int biSize;
    int biWidth;  // 寬
    int biHeight; // 高
    unsigned short biPlanes;
    unsigned short biBitCount; // 色深
    unsigned int biCompression;
    unsigned int biSizeImage;
    int biXPelsPerMeter;
    int biYPelsPerMeter;
    unsigned int biClrUsed;
    unsigned int biClrImportant;
} BITMAPINFOHEADER;

#pragma pack()

int main(int argc, char const *argv[])
{
    // 1.開啟BMP影像
    FILE *bmp_fp = fopen("1.bmp", "rb");
    if (NULL == bmp_fp)
    {
        return -1;
    }
    // 2
    BITMAPFILEHEADER h1; // 檔案頭    14
    BITMAPINFOHEADER h2; // 資訊頭    40

    fread(&h1, 1, 14, bmp_fp);
    fread(&h2, 1, 40, bmp_fp);

    // 3每次讀一行畫素點顏色分量大小
    int line_size = h2.biWidth * 3;
    char *src_pbuf = (char *)calloc(1, line_size);

    // 4開啟新圖片
    FILE *new_fp = fopen("new.bmp", "wb");
    // 資訊寫入新圖片
    fwrite(&h1, 1, 14, new_fp);
    fwrite(&h2, 1, 40, new_fp);

    // 資料寫入新圖片
    int line_num = h2.biHeight;
    while (line_num--)
    {
        // 讀:空間+1+顏色分量+開啟的原圖
        fread(src_pbuf, 1, line_size, bmp_fp);
        // 寫
        fwrite(src_pbuf, 1, line_size, new_fp);
    }

    return 0;
}

BMP圖片顯示到顯示器

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
/*
 *   把400*240bmp圖片展示到800*480的螢幕中
 *
 *
 */
#pragma pack(1)

// 定義BMP檔案頭部結構
typedef struct{
	unsigned short bfType;
	unsigned int   bfSize;
	unsigned short bfReserved1;
	unsigned short bfReserved2;
	unsigned int   bfOffBits;
}BITMAPFILEHEADER;

typedef struct{
	unsigned int biSize;
	int biWidth;				//寬
	int biHeight;				//高
	unsigned short biPlanes;
	unsigned short biBitCount;	//色深
	unsigned int biCompression;
	unsigned int biSizeImage;
	int biXPelsPerMeter;
	int biYPelsPerMeter;
	unsigned int biClrUsed;
	unsigned int biClrImportant;
}BITMAPINFOHEADER;


#pragma pack()


int main(int argc, char const *argv[])
{
	//1.開啟待顯示的BMP影像  fopen
	FILE * bmp_fp = fopen("1.bmp","rb");
	if (NULL == bmp_fp)
	{
		return -1;
	}

	//2.讀取BMP檔案的影像資訊,獲取BMP的寬和高
	BITMAPINFOHEADER headerinfo;
	fseek(bmp_fp,14,SEEK_SET);
	fread(&headerinfo,1,40,bmp_fp); //讀取40位元組
	printf("bmp width = %d,height = %d\n",headerinfo.biWidth,headerinfo.biHeight);

	//3.讀取BMP圖*片的顏色分量  400* 240*3h
	char bmp_buf[400*240*3] = {0};
	fread(bmp_buf,1,400*240*3,bmp_fp);

	//4.關閉BMP
	fclose(bmp_fp);

	//5.開啟LCD   open  
	int lcd_fd = open("/dev/fb0",O_RDWR);


	//6.對LCD進行記憶體對映  mmap
	int * lcd_mp = (int *)mmap(NULL,800*480*4,PROT_READ|PROT_WRITE,MAP_SHARED,lcd_fd,0);

	//7.迴圈的把BMP影像的顏色分量依次寫入到LCD的畫素點中 
	int i = 0;
	int data = 0;

	for (int y =  240-1; y >= 0; y--)
	{
		for (int x = 0; x < 400 ; ++x)
		{
			//把BMP圖片的一個畫素點的顏色分量轉換為LCD螢幕的一個畫素點的顏色分量格式  ARGB <--- BGR
			data |= bmp_buf[i];			//B
			data |= bmp_buf[i+1]<<8;	//G
			data |= bmp_buf[i+2]<<16;  	//R

			lcd_mp[800*y + x] = data;  //BGR BGR BGR .... 

			i+=3;  
			data = 0;
		}
	}
	
	//8.關閉LCD
	close(lcd_fd);
	munmap(lcd_mp,400*240*4);

	return 0;
}

相關文章