-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathframebuffer.c
197 lines (164 loc) · 5.71 KB
/
framebuffer.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
#include <fcntl.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <png.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <linux/fb.h>
#include <string.h>
#include "pngimage.h"
#include "framebuffer.h"
/* Open framebuffer device and mmap it to memory.
* fb: pointer to framebuffer struct.
* filename: name of framebuffer device.
*
* If successful, sets fb->descriptor and fb->mem_start.
*/
void new_framebuffer(struct framebuffer *fb, char *filename) {
size_t bytes;
void *mem_ptr;
fb->descriptor = open(filename, O_RDWR);
if (fb->descriptor == -1) {
perror("Could not open framebuffer device");
exit(EXIT_FAILURE);
}
/* Populate screen info struct for framebuffer. */
if (ioctl(fb->descriptor, FBIOGET_VSCREENINFO, &(fb->screeninfo))) {
perror("Unable to get screen info");
exit(EXIT_FAILURE);
}
bytes = screen_size_in_bytes(fb);
mem_ptr = mmap(NULL, bytes, PROT_READ|PROT_WRITE, MAP_SHARED,
fb->descriptor, 0);
if (mem_ptr == MAP_FAILED) {
perror("Failed to mmap framebuffer device to memory");
exit(EXIT_FAILURE);
}
else {
fb->mem_start = (unsigned char *)mem_ptr;
}
return;
}
/* Clean up after we're finished with the framebuffer. Unmap memory
* and close file descriptor.
* fb: pointer to framebuffer struct.
*/
void close_framebuffer(struct framebuffer *fb) {
if (munmap(fb->mem_start, screen_size_in_bytes(fb)) == -1) {
perror("Failed to munmap framebuffer memory");
exit(EXIT_FAILURE);
}
if (close(fb->descriptor) == -1) {
perror("Failed to close framebuffer");
exit(EXIT_FAILURE);
}
return;
}
/* Get the size of the screen in bytes.
* fb: pointer to framebuffer struct.
* Returns: number of bytes used for the screen.
*/
size_t screen_size_in_bytes(struct framebuffer *fb) {
size_t bytes;
bytes = fb->screeninfo.xres
* fb->screeninfo.yres
* fb->screeninfo.bits_per_pixel/8;
/* XXX debug output */
printf("x resolution: %d\n", fb->screeninfo.xres);
printf("y resolution: %d\n", fb->screeninfo.yres);
printf("bpp: %d\n", fb->screeninfo.bits_per_pixel);
printf("Screen size: %d\n", (int)bytes);
return bytes;
}
/* display_png: display a PNG file at the given location on the framebuffer.
* fb: pointer to framebuffer struct.
* filename: name of the file to display.
* x_pos, y_pos: x and y coordinates at which to display it on the framebuffer.
*/
struct image_size display_png(struct framebuffer *fb, char *filename,
int x_pos, int y_pos) {
png_byte header[8];
FILE *fp;
png_structp png_ptr;
png_infop info_ptr;
png_bytepp row_pointers;
struct image_size png_size;
int x, y;
unsigned char *mem_ptr;
fp = fopen(filename, "rb");
if (!fp) {
perror("Failed to open file");
exit(EXIT_FAILURE);
}
fread(header, 1, 8, fp);
if (png_sig_cmp(header, 0, 8)) {
fprintf(stderr, "%s is not a valid PNG file.\n", filename);
exit(EXIT_FAILURE);
}
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
info_ptr = png_create_info_struct(png_ptr);
if (!png_ptr || !info_ptr) {
perror("Failed to create PNG data structures");
exit(EXIT_FAILURE);
}
if (setjmp(png_jmpbuf(png_ptr))) {
perror("setjmp failed");
exit(EXIT_FAILURE);
}
png_init_io(png_ptr, fp);
png_set_sig_bytes(png_ptr, 8);
png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
png_size.x = png_get_image_width(png_ptr, info_ptr);
png_size.y = png_get_image_height(png_ptr, info_ptr);
row_pointers = png_get_rows(png_ptr, info_ptr);
/******** Process and display the image data ********/
/* Move to correct X/Y starting position */
mem_ptr = fb->mem_start;
mem_ptr += x_pos * (fb->screeninfo.bits_per_pixel/8);
mem_ptr += y_pos * (fb->screeninfo.bits_per_pixel/8) * fb->screeninfo.xres;
/* Loop through image data and write each pixel to the framebuffer */
switch (fb->screeninfo.bits_per_pixel) {
case 16:
for (y = 0; y < png_size.y; y++) {
png_bytep row = row_pointers[y];
for (x = 0; x < png_size.x; x++) {
/* Convert 8888 RGBA to 565 RGB */
png_bytep png_pixel = &(row[x*4]);
png_byte r = png_pixel[0];
png_byte g = png_pixel[1];
png_byte b = png_pixel[2];
uint16_t fb_pixel = 0;
r = r >> 3;
g = g >> 2;
b = b >> 3;
fb_pixel = fb_pixel | r;
fb_pixel = fb_pixel << 6;
fb_pixel = fb_pixel | g;
fb_pixel = fb_pixel << 5;
fb_pixel = fb_pixel | b;
/* Write pixel to framebuffer */
*mem_ptr = fb_pixel;
mem_ptr++;
*mem_ptr = (fb_pixel >> 8);
mem_ptr++;
}
/* Move back to starting X coordinate */
mem_ptr -= png_size.x * (fb->screeninfo.bits_per_pixel/8);
/* Move down to the next row of pixels */
mem_ptr += (fb->screeninfo.bits_per_pixel/8) * fb->screeninfo.xres;
}
break;
case 32:
for (y = 0; y < png_size.y; y++) {
memcpy(mem_ptr, row_pointers[y], png_size.x * 4);
/* Move down to the next row of pixels */
mem_ptr += (fb->screeninfo.bits_per_pixel/8) * fb->screeninfo.xres;
}
break;
default:
fprintf(stderr, "Unsupported pixel size: %d bits!\n", fb->screeninfo.bits_per_pixel);
break;
}
return png_size;
}