A . The system IO standard IO with examples -> display RGB composition BMP format images
1. BMP format picture Features
Pure RGB primary colors to compose, without any compression, file relative to the compressed will be relatively large jpeg format.
test.bmp -> 1MB
test.jpg -> 27KB
2. BMP picture since it is RGB composition, then the BMP picture pixel point is how to make up?
Usually BMP pictures are 24, because RGB half and 8, 24 of each pixel is described by a 3-byte byte, descending order is: the BGR
Liquid crystal LCD pixel, in descending order are: an ARGB
Picture Total bytes: 800 * 480 * 3
LCD bytes Total: 800 * 480 * 4
Two . Analysis process
1. test.bmp ordinary files, / dev / FB0 are device files, files require access to a corresponding different the IO .
2. The general procedure is as follows :
1 ) using the standard IO access test.bmp .
2 ) Read test.bmp Data -> each of the read out pixel should
3 ) Use the system IO access LCD the LCD .
4 ) writes the image data to the liquid crystal pixel LCD in. -> Note pixel distribution.
5 ) Close the file and device can be.
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
int main(int argc,char *argv[])
{
char bmp_buf [800 * 480 * 3]; // BMP format picture buffer
char lcd_buf [800 * 480 * 4]; // LCD liquid crystal buffer
int ret,lcd;
int i,j;
// 1. Access BMP picture
FILE *fp = fopen("test.bmp","r");
if(fp == NULL)
printf("fopen error!\n");
2 // Read data BMP picture
ret = fread(bmp_buf,sizeof(bmp_buf),1,fp);
if(ret != 1)
printf("fread error!\n");
// 3. Access the LCD
lcd = open("/dev/fb0",O_WRONLY);
if(lcd < 0)
printf("open error!\n");
for(i=0,j=0;i<800*480*4;i+=4,j+=3)
{
lcd_buf[i] = 0;
lcd_buf[i+1] = bmp_buf[j+2];
lcd_buf[i+2] = bmp_buf[j+1];
lcd_buf[i+3] = bmp_buf[j];
}
// 4 The picture data is written to on the LCD screen
ret = write(lcd,lcd_buf,sizeof(lcd_buf));
if(ret != sizeof(lcd_buf))
printf("write error!\n");
// 5. Turn off the device and file
close(lcd);
fclose(fp);
return 0;
}
result:
With the shape, the wrong color!
lcd_buf [0] -> Blue
lcd_buf [1] -> Green
lcd_buf [2] -> Red
lcd_buf [3] -> Transparency
change into:
for(i=0,j=0;i<800*480*4;i+=4,j+=3)
{
lcd_buf[i] = bmp_buf[j];
lcd_buf[i+1] = bmp_buf[j+1];
lcd_buf[i+2] = bmp_buf[j+2];
lcd_buf[i+3] = 0;
}
3. found LCD some data is not the pixel points at the beginning, it is also read out
800 * 480 * 3 = 1152000--> actual number of bytes of pixel
Picture the actual number of bytes: 1152054--> There are 54 heads of data!
4. vertically flipped
Quit general term formula based on logic
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
int main(int argc,char *argv[])
{
char bmp_buf [800 * 480 * 3]; // BMP format picture buffer
char lcd_buf [800 * 480 * 4]; // LCD liquid crystal buffer
char show_buf[800*480*4];
int ret,lcd;
int i,j,x,y;
// 1. Access BMP picture
FILE *fp = fopen("test.bmp","r");
if(fp == NULL)
printf("fopen error!\n");
2 // Skip the BMP picture 54 data heads
ret = fseek(fp,54,SEEK_SET);
if(ret != 0)
printf("fseek error!\n");
// 3. read data BMP picture
ret = fread(bmp_buf,sizeof(bmp_buf),1,fp);
if(ret != 1)
printf("fread error!\n");
// 4. Access the LCD
lcd = open("/dev/fb0",O_WRONLY);
if(lcd < 0)
printf("open error!\n");
// 5 pixels assignment
for(i=0,j=0;i<800*480*4;i+=4,j+=3)
{
lcd_buf[i] = bmp_buf[j];
lcd_buf[i+1] = bmp_buf[j+1];
lcd_buf[i+2] = bmp_buf[j+2];
lcd_buf[i+3] = 0;
}
// 6 upside down
for(y=0;y<480;y++)
{
for(x=0;x<800*4;x++)
{
show_buf[(479-y)*800*4+x] = lcd_buf[y*800*4+x];
}
}
// 7. To write data to the picture on the LCD screen
ret = write(lcd,show_buf,sizeof(show_buf));
if(ret != sizeof(show_buf))
printf("write error!\n");
// 8. Turn off the device and file
close(lcd);
fclose(fp);
return 0;
}
Three . Directory IO
1. How to access ( open ) directory? -> opendir () -> man 3 opendir
And not the same as Windows , access the directory linux it is only open catalog only, nothing to do with the path switching.
#include <sys/types.h>
#include <dirent.h>
DIR *opendir(const char *name);
Need to open the directory path: name
return value:
Success: directory stream pointer -> Open the directory, pointing to a default directory beginning with the most!
Failed: NULL
Validation: when accessing the directory, and not switch to the directory.
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
int main ()
{
system("pwd");
DIR *dp = opendir("./dir");
if(dp == NULL)
printf("opendir error!\n");
system("pwd");
return 0;
}
2. Close directory? -> closedir () -> man 3 closedir
#include <sys/types.h>
#include <dirent.h>
int closedir(DIR *dirp);
dirp: directory stream pointer
return value:
Success: 0
Failure: -1
3. How to switch to the directory? -> chdir () -> man 2 chdir
#include <unistd.h>
int chdir(const char *path);
path: the directory path to be switched
return value:
Success: 0
Failure: -1
note:
the chdir () parameter is not filled directory stream pointer, but rather fill the directory path.
4. How to read the catalog? -> readdir () -> man 3 readdir
#include <dirent.h>
struct dirent * readdir (DIR * dirp);
dirp: directory stream pointer
return value:
Success: structure pointer struct dirent *
Failure: NULL -> When finished reading the directory, it will return NULL.
struct {told
ino_t d_ino; // index number
off_t d_off; // offset
unsigned short d_reclen; // name of the directory entry length
unsigned char d_type; // type of file
char d_name [256]; // file name
};
d_reclen:
Formula: 12 the X-+
. -> 1 -> 4 -> 12+4=16
.. -> 2 -> 4 -> 12+4=16
1.txt -> 5 -> 8 -> 12+8=20
3a.jpg -> 6 -> 8 -> 12+8=20
abcdef.jpg-> 10 -> 12 -> 12+12=24
d_type:
enum
{
DT_UNKNOWN = 0, -> unknown type
DT_FIFO = 1, -> File conduit
DT_CHR = 2, -> character device file
DT_DIR = 4, -> catalog file
DT_BLK = 6, -> block device
DT_REG = 8, -> common files
DT_LNK = 10, -> Link file
DT_SOCK = 12, -> socket file
};
5. Reset directory pointer? -> rewinddir () -> man 3 rewinddir
#include <sys/types.h>
#include <dirent.h>
void rewinddir(DIR *dirp);
dirp: directory stream pointer
Return Value: None
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
int main ()
{
// 1. Open Directory
DIR *dp = opendir("./dir");
if(dp == NULL)
printf("opendir error!\n");
2 // Switch to the directory
chdir("./dir");
// 3. directory read each item
struct dirent * ep = NULL;
while(1)
{
ep = readdir(dp);
if (ep == NULL) // finished reading
break;
if(ep->d_name[0] == '.')
continue;
printf("ep->d_ino = %d\n",(int)ep->d_ino);
printf("ep->d_off = %d\n",(int)ep->d_off);
printf("ep->d_reclen = %d\n",ep->d_reclen);
printf("ep->d_type = %d\n",ep->d_type);
printf("ep->d_name = %s\n",ep->d_name);
printf("================================\n");
}
// 4. Close Directory
closedir(dp);
return 0;
}
operation:
There is a directory, which a lot of Zhang 800 * 480 bmp format images , after the implementation of the program, to display the first picture, click on the left if let go, is displayed on one, let go if the right, next. Requirements five catalog how many images, the program can run normally.