forked from Guazhen/Framebuffer_shot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfamebuffer_shot_bmp.c
223 lines (189 loc) · 5.01 KB
/
famebuffer_shot_bmp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include <linux/fb.h>
#include <stdio.h>
#include <stdint.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <getopt.h>
#include <strings.h>
#include <unistd.h>
typedef unsigned short WORD;
typedef unsigned int DWORD;
typedef unsigned long LONG;
typedef struct tagBITMAPFILEHEADER {
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
} __attribute__((packed)) BITMAPFILEHEADER;
typedef struct tagBITMAPINFOHEADER
{
DWORD biSize; /*info header size in bytes*/
DWORD biWidth; /*widht of image*/
DWORD biHeight;/*height of image*/
WORD biPlanes;/*number of colour planes*/
WORD biBitCount;/*bits per pixel*/
DWORD biCompression;/*compression type*/
DWORD biSizeImage;/*image size meter*/
DWORD biXPelsPerMeter;/*pixels per meter*/
DWORD biYPelsPerMeter;/*pexels per meter*/
DWORD biClrUsed;/*number of colour*/
DWORD biClrImportant;/*important colour*/
} __attribute__((packed)) BITMAPINFOHEADER;
int output_file;
int type_file;
struct fb_fix_screeninfo finfo;
struct fb_var_screeninfo vinfo;
static const struct option long_options[]=
{
{"output",1,NULL,'o'},
{"t",1,NULL,'t'},
{NULL,0,NULL,0}
};
static void usage(void)
{
fprintf(stderr,
"imax6 [option]...\n"
" -o|--output Output the filename.\n"
" -t|--type Output the type of thefilename.\n"
" -h|--help help information.\n"
);
};
static void image_bmp( const char *filename)
{
printf("starting bmp..\n");
char tmpbufilename[126] = {0};
if( NULL != filename)
{
strcpy(tmpbufilename, filename);
strcat(tmpbufilename,".bmp");
}else
{
strcpy(tmpbufilename,"screen.bmp");
}
FILE *fp;
BITMAPFILEHEADER bmfh;
BITMAPINFOHEADER bmih;
((unsigned char *)&bmfh.bfType)[0] = 'B';
((unsigned char *)&bmfh.bfType)[1] = 'M';
bmfh.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + vinfo.yres * vinfo.xres * 4;
bmfh.bfReserved1 = 0;
bmfh.bfReserved2 = 0;
bmfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bmih.biSize = sizeof(BITMAPINFOHEADER);
bmih.biWidth = vinfo.xres;
bmih.biHeight = vinfo.yres;
bmih.biPlanes = 1;
bmih.biBitCount = 32;
bmih.biCompression = 0;
bmih.biSizeImage = 0; /*说明图像的大小,以字节为单位。当用BI_RGB格式时,总设置为0*/
bmih.biXPelsPerMeter = 0; /*缺省值*/
bmih.biYPelsPerMeter = 0;
bmih.biClrUsed = 0; /*说明位图实际使用的调色板索引数,0:使用所有的调色板索引*/
bmih.biClrImportant = 0; /*说明对图像显示有重要影响的颜色索引的数目,如果是0,表示都重要*/
printf("tmpbufilename = %s\n",tmpbufilename);
FILE* image_file = fopen(tmpbufilename,"a");
if( NULL == image_file)
{
printf("image fopen fail\n");
}
fwrite(&bmfh, sizeof(BITMAPFILEHEADER),1,image_file);
fwrite(&bmih, sizeof(BITMAPINFOHEADER),1,image_file);
FILE *raw_file = fopen( "test.raw","rb");
if( NULL == raw_file)
{
printf("rawfile fopen fail..\n");
}
int ch = getc(raw_file);
int x, y;
for( y = vinfo.yres - 1; y >= 0; y--)
{
for(x = 0; x < vinfo.xres; x++)
{
/*字节数*/
long location = (x + vinfo.xoffset) * (vinfo.bits_per_pixel/8) + (y + vinfo.yoffset) * finfo.line_length;
fseek(raw_file, location, SEEK_SET);/*将文件句柄定位到相应的位置*/
/*写入第一个字节*/
ch = fgetc(raw_file);
fputc(ch,image_file);
/*写入第二个字节*/
ch = fgetc(raw_file);
fputc(ch,image_file);
/*写入第三个字节*/
ch = fgetc(raw_file);
fputc(ch,image_file);
/*写入第四个字节*/
ch = fgetc(raw_file);
fputc(ch,image_file);
}
}
fp = popen("rm ./test.raw","r");
pclose(fp);
fclose(raw_file);
fclose(image_file);
printf("ending bmp\n");
}
int main(int argc, char **argv)
{
int opt = 0;
int options_index = 0;
char *tmp = NULL;
char filename[126] = {0};
char type[126] = {0};
if(argc == 1)
{
usage();
return 2;
}
/* 解析命令行参数*/
while((opt=getopt_long(argc,argv,"o:t:h?",long_options,&options_index))!=EOF )
{
switch(opt)
{
case 'o':
output_file=1;
sprintf(filename,"%s",optarg);
break;
case 't':
type_file=1;
sprintf(type,"%s",optarg);
break;
case 'h':
case '?':
usage();return 2;break;
}
}
int fb_fd = open("/dev/fb0",O_RDWR);
if(fb_fd == -1)
{
printf("open fail..\n");
return -1;
}
ioctl(fb_fd, FBIOGET_VSCREENINFO, &vinfo);
ioctl(fb_fd, FBIOGET_FSCREENINFO, &finfo);
long screensize = vinfo.yres_virtual * finfo.line_length;
FILE *fp = popen("bash ./test.sh","r");
pclose(fp);
//bmp
if( output_file && type_file )
{
if( !strcmp(type,"bmp"))
{
image_bmp(filename);
}else if( !strcmp(type,"png"))
{
printf("png type = %s\n",type);
}
else
{
printf("unkown\n");
}
}
else
{
usage();return 2;
}
close(fb_fd);
return 0;
}