-
Notifications
You must be signed in to change notification settings - Fork 28
/
fb2png.c
279 lines (218 loc) · 7.3 KB
/
fb2png.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
//-------------------------------------------------------------------------
//
// The MIT License (MIT)
//
// Copyright (c) 2013 Andrew Duncan
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//-------------------------------------------------------------------------
#include <errno.h>
#include <fcntl.h>
#include <png.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <linux/fb.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
int main(int argc, char *argv[])
{
char *program = argv[0];
char *fbdevice = "/dev/fb0";
char *pngname = "fb.png";
int opt = 0;
//--------------------------------------------------------------------
while ((opt = getopt(argc, argv, "d:p:")) != -1)
{
switch (opt)
{
case 'd':
fbdevice = optarg;
break;
case 'p':
pngname = optarg;
break;
default:
fprintf(stderr,
"Usage: %s [-d device] [-p pngname]\n",
program);
exit(EXIT_FAILURE);
break;
}
}
//--------------------------------------------------------------------
int fbfd = open(fbdevice, O_RDWR);
if (fbfd == -1)
{
fprintf(stderr,
"%s: cannot open framebuffer - %s",
program,
strerror(errno));
exit(EXIT_FAILURE);
}
struct fb_fix_screeninfo finfo;
if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo) == -1)
{
fprintf(stderr,
"%s: reading framebuffer fixed information - %s",
program,
strerror(errno));
exit(EXIT_FAILURE);
}
struct fb_var_screeninfo vinfo;
if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo) == -1)
{
fprintf(stderr,
"%s: reading framebuffer variable information - %s",
program,
strerror(errno));
exit(EXIT_FAILURE);
}
if ((vinfo.bits_per_pixel != 16) &&
(vinfo.bits_per_pixel != 24) &&
(vinfo.bits_per_pixel != 32))
{
fprintf(stderr, "%s: only 16, 24 and 32 ", program);
fprintf(stderr, "bits per pixels supported\n");
exit(EXIT_FAILURE);
}
void *memp = mmap(0,
finfo.smem_len,
PROT_READ | PROT_WRITE,
MAP_SHARED,
fbfd,
0);
close(fbfd);
fbfd = -1;
if (memp == MAP_FAILED)
{
fprintf(stderr,
"%s: failed to map framebuffer device to memory - %s",
program,
strerror(errno));
exit(EXIT_FAILURE);
}
uint8_t *fbp = memp;
memp = NULL;
//--------------------------------------------------------------------
png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
NULL,
NULL,
NULL);
if (png_ptr == NULL)
{
fprintf(stderr,
"%s: could not allocate PNG write struct\n",
program);
exit(EXIT_FAILURE);
}
png_infop info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL)
{
fprintf(stderr,
"%s: could not allocate PNG info struct\n",
program);
exit(EXIT_FAILURE);
}
if (setjmp(png_jmpbuf(png_ptr)))
{
png_destroy_write_struct(&png_ptr, &info_ptr);
fprintf(stderr, "%s: error creating PNG\n", program);
exit(EXIT_FAILURE);
}
FILE *pngfp = fopen(pngname, "wb");
if (pngfp == NULL)
{
fprintf(stderr, "%s: Unable to create %s\n", program, pngname);
exit(EXIT_FAILURE);
}
png_init_io(png_ptr, pngfp);
png_set_IHDR(
png_ptr,
info_ptr,
vinfo.xres,
vinfo.yres,
8,
PNG_COLOR_TYPE_RGB,
PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_BASE,
PNG_FILTER_TYPE_BASE);
png_write_info(png_ptr, info_ptr);
png_bytep png_buffer = malloc(vinfo.xres * 3 * sizeof(png_byte));
if (png_buffer == NULL)
{
fprintf(stderr, "%s: Unable to allocate buffer\n", program);
exit(EXIT_FAILURE);
}
//--------------------------------------------------------------------
int r_mask = (1 << vinfo.red.length) - 1;
int g_mask = (1 << vinfo.green.length) - 1;
int b_mask = (1 << vinfo.blue.length) - 1;
int bytes_per_pixel = vinfo.bits_per_pixel / 8;
int y = 0;
for (y = 0; y < vinfo.yres; y++)
{
int x;
for (x = 0; x < vinfo.xres; x++)
{
int pb_offset = 3 * x;
size_t fb_offset = (vinfo.xoffset + x) * (bytes_per_pixel)
+ (vinfo.yoffset + y) * finfo.line_length;
uint32_t pixel = 0;
switch (vinfo.bits_per_pixel)
{
case 16:
pixel = *((uint16_t *)(fbp + fb_offset));
break;
case 24:
pixel += *(fbp + fb_offset);
pixel += *(fbp + fb_offset + 1) << 8;
pixel += *(fbp + fb_offset + 2) << 16;
break;
case 32:
pixel = *((uint32_t *)(fbp + fb_offset));
break;
default:
// nothing to do
break;
}
png_byte r = (pixel >> vinfo.red.offset) & r_mask;
png_byte g = (pixel >> vinfo.green.offset) & g_mask;
png_byte b = (pixel >> vinfo.blue.offset) & b_mask;
png_buffer[pb_offset] = (r * 0xFF) / r_mask;
png_buffer[pb_offset + 1] = (g * 0xFF) / g_mask;
png_buffer[pb_offset + 2] = (b * 0xFF) / b_mask;
}
png_write_row(png_ptr, png_buffer);
}
//--------------------------------------------------------------------
free(png_buffer);
png_buffer = NULL;
png_write_end(png_ptr, NULL);
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(pngfp);
//--------------------------------------------------------------------
munmap(fbp, finfo.smem_len);
//--------------------------------------------------------------------
return 0;
}