/* ===============================================================
* Name : show_bmp.c
* Author : wsg
* Version : 1.0
* Date : 2017 09 16
* License : null
* Description : show picture in BMP format on the touch screen
* of arm development board
/* ============================================================= */
#include <stdio.h> //printf
#include <sys/types.h> //open
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h> //write
#define RGB_SIZE 800*480*3
#define LCD_SIZE 800*480
int main(int argc, char const *argv[])
{
if (argc != 2)
{
printf("Eroor! Parameter missing!\n");
return -1;
}
// 1. 打开触摸屏文件
int lcd_fd = open("/dev/fb0", O_RDWR);
if (lcd_fd == -1)
{
printf("Open lcd failed!!\n");
return -1;
}
// 2. 打开bmp图片
int bmp_fd = open(argv[1], O_RDWR);
if (bmp_fd == -1)
{
printf("Open %s filed\n", argv[1]);
return -1;
}
// 3. 偏移bmp格式头, 54个字节
off_t offset = lseek(bmp_fd, 54, SEEK_SET);
if (offset == -1)
{
printf("Offset failed!\n");
return -1;
}
// 4. 读取bmp图片的RGB值,将读到的值存进bmp_rgb数组中
char bmp_buf[RGB_SIZE] = {};
size_t re_ret = read(bmp_fd, bmp_buf, RGB_SIZE);
if (re_ret == -1)
{
printf("Read %s failed!\n", argv[1]);
return -1;
}
// 5. 24位数据-->32位数据:bmp图片rgb占3个字节,lcdargb占4个字节. char占1个字节大小,int占4个字节大小
int lcd_buf[LCD_SIZE] = {};
int i;
for (i=0; i<LCD_SIZE; i++)
{
lcd_buf[i] = bmp_buf[i*3+2]<<16 | bmp_buf[i*3+1]<<8 | bmp_buf[i*3+0]<<0;
}
// 6. 翻转180°
int fli_buf[LCD_SIZE];
int x, y;
for(y = 0; y < 480; y++)
{
for(x = 0; x < 800; x++)
{
fli_buf[y*800+x] = lcd_buf[(479-y)*800+x];
}
}
// 7. 写入LCD
size_t wr_fd = write(lcd_fd, fli_buf, LCD_SIZE);
if (wr_fd == -1)
{
printf("Write data into lcd failed!\n");
return -1;
}
// 8. 关闭文件
close(lcd_fd);
close(bmp_fd);
return 0;
}