-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfbimg.c
More file actions
136 lines (120 loc) · 4.06 KB
/
fbimg.c
File metadata and controls
136 lines (120 loc) · 4.06 KB
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
/*
This software is under the MIT license, see LICENSE for more details.
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 <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <linux/fb.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
int main(int argc, char** argv)
{
int fbfd;
char *fbp;
struct fb_var_screeninfo vinfo;
struct fb_fix_screeninfo finfo;
long int screensize;
int height, width, n, original_width;
unsigned char* data;
int x, y;
long int location;
unsigned int x_for_loc;
/* Open the file for reading and writing. */
fbfd = open("/dev/fb0", O_RDWR);
if (fbfd == -1)
{
perror("Failed to open framebuffer device");
exit(1);
}
// printf("The framebuffer device was opened successfully.\n");
/* Get fixed screen information. */
if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo) == -1)
{
close(fbfd);
perror("Failed to read fixed information");
exit(2);
}
/* Get variable screen information. */
if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo) == -1)
{
close(fbfd);
perror("Failed to read variable information");
exit(3);
}
// printf("%dx%d, %d bpp\n", vinfo.xres, vinfo.yres, vinfo.bits_per_pixel);
// printf("rotate=%d\n", vinfo.rotate);
// printf("activate=%d\n", vinfo.activate);
/* Figure out the size of the screen in bytes. */
screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;
/* Map the device to memory. */
fbp = (char *) mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, fbfd, 0);
if ((long) fbp == -1)
{
close(fbfd);
perror("Failed to map framebuffer device to memory");
exit(4);
}
// printf("The framebuffer device was mapped to memory successfully.\n");
/* open image */
if (argc < 2)
{
close(fbfd);
munmap(fbp, screensize);
printf("Please give an image as argument\n");
exit(6);
}
data = stbi_load(argv[1], &width, &height, &n, 4);
if (data == NULL)
{
close(fbfd);
munmap(fbp, screensize);
printf("An error occured when reading the image\n");
exit(7);
}
/* print it */
if (height > (int)vinfo.yres-5)
height = vinfo.yres-5;
original_width = width;
if (width > (int)vinfo.xres)
width = vinfo.xres;
for (y = 0; y < height; y++)
{
for (x = 0; x < width; x++)
{
x_for_loc = x + (vinfo.xres-width);
location = (x_for_loc+vinfo.xoffset) * (vinfo.bits_per_pixel/8) +
(y+vinfo.yoffset) * finfo.line_length;
if (vinfo.bits_per_pixel == 32)
{
*(fbp + location) = data[(y*original_width + x)*4 + 2];
*(fbp + location + 1) = data[(y*original_width + x)*4 + 1];
*(fbp + location + 2) = data[(y*original_width + x)*4 + 0];
*(fbp + location + 3) = data[(y*original_width + x)*4 + 3];
} else // Assume 16 bpp.
{
int b = data[(y*original_width + x)*4 + 2];
int g = data[(y*original_width + x)*4 + 1];
int r = data[(y*original_width + x)*4 + 0];
unsigned short int t = r<<11 | g << 5 | b;
*((unsigned short int*)(fbp + location)) = t;
}
}
}
// printf("The framebuffer device was painted successfully.\n");
/* Close memory mapped and file descriptor. */
munmap(fbp, screensize);
close(fbfd);
/* Free the image data */
stbi_image_free(data);
return 0;
}