-
Notifications
You must be signed in to change notification settings - Fork 0
/
fbc.c
222 lines (174 loc) · 4.86 KB
/
fbc.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
/*
* Linux Frame Buffer Device Colour Map Configuration
*
* © Copyright 2011 Daniel Dyer
* (daniel_dyer@axent.com.au)
*
* --------------------------------------------------------------------------
*
* This file is subject to the terms and conditions of the GNU General Public
* License. See the file COPYING in the main directory of the Linux
* distribution for more details.
*
*/
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <math.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include "fb.h"
#define VERSION "Linux Frame Buffer Device Colour Map Configuration " \
"Version 1.0 (19/07/2011)\n" \
"(C) Copyright 2011 Daniel Dyer\n"
#define DEFAULT_FRAMEBUFFER "/dev/fb0"
#define DEFAULT_DEPTH 256
// command-line options
static const char *program_name;
static int opt_version = 0;
static int opt_verbose = 0;
static const char *opt_device = NULL;
static const char *opt_r = NULL;
static const char *opt_g = NULL;
static const char *opt_b = NULL;
static const char *opt_depth = NULL;
static const char *opt_gamma = NULL;
static struct {
const char *name;
const char **value;
} options[] = {
{ "-d", &opt_device },
{ "-r", &opt_r },
{ "-g", &opt_g },
{ "-b", &opt_b },
{ "-n", &opt_depth },
{ NULL, NULL }
};
// function prototypes
static void usage(void);
int main(int argc, char *argv[]);
// print error message and exit
void die(const char *fmt, ...)
{
va_list ap;
fflush(stdout);
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
exit(1);
}
// calculate the colour map
static void calculate_cmap(struct fb_cmap *cmap)
{
float gamma = 0;
unsigned long depth = DEFAULT_DEPTH;
unsigned long r = 0;
unsigned long g = 0;
unsigned long b = 0;
int i, temp;
__u16 value;
if(opt_gamma)
gamma = strtof(opt_gamma, NULL);
if(!opt_gamma || gamma < 0.001)
die("Must supply a valid gamma value\n");
if(opt_depth)
depth = strtoul(opt_depth, NULL, 0);
if(opt_r)
r = strtoul(opt_r, NULL, 0);
if(opt_g)
g = strtoul(opt_g, NULL, 0);
if(opt_b)
b = strtoul(opt_b, NULL, 0);
if(r > depth || g > depth || b > depth)
die("Colour offset too large\n");
cmap->start = 0;
cmap->len = depth;
cmap->transp = NULL;
cmap->red = malloc(depth * sizeof(__u16));
cmap->green = malloc(depth * sizeof(__u16));
cmap->blue = malloc(depth * sizeof(__u16));
for(i = 0; i < depth; i++)
{
temp = (i - r) < 0 ? 0 : i - r;
cmap->red[i] = lround(pow(temp / (float)(depth-1), gamma) * 0xffff);
temp = (i - g) < 0 ? 0 : i - g;
cmap->green[i] = lround(pow(temp / (float)(depth-1), gamma) * 0xffff);
temp = (i - b) < 0 ? 0 : i - b;
cmap->blue[i] = lround(pow(temp / (float)(depth-1), gamma) * 0xffff);
}
return;
}
// print usage and exit
static void usage(void)
{
puts(VERSION);
die("\nUsage: %s [options] gamma\n\n"
"Valid options:\n"
" General options:\n"
" -h, --help : display this usage information\n"
" Frame buffer special device nodes:\n"
" -d <device> : processed frame buffer device\n"
" (default is " DEFAULT_FRAMEBUFFER ")\n"
" Colour map depth:\n"
" -n <value> : number of map entries per colour\n"
" (default is %d)\n"
" Offsets for colour channels:\n"
" -r <value> : offset for red channel\n"
" -g <value> : offset for green channel\n"
" -b <value> : offset for blue channel\n",
program_name, DEFAULT_DEPTH);
}
int main(int argc, char *argv[])
{
struct fb_cmap cmap;
int fh = -1, i;
program_name = argv[0];
// parse command-line options
while (--argc > 0) {
argv++;
if (!strcmp(argv[0], "-h") || !strcmp(argv[0], "--help"))
usage();
else if (!strcmp(argv[0], "-v") || !strcmp(argv[0], "--verbose"))
opt_verbose = 1;
else if (!strcmp(argv[0], "-V") || !strcmp(argv[0], "--version"))
opt_version = 1;
else {
for (i = 0; options[i].name; i++)
if (!strcmp(argv[0], options[i].name))
break;
if (options[i].name) {
if (argc-- > 1) {
*options[i].value = argv[1];
argv++;
} else
usage();
} else if (!opt_gamma) {
opt_gamma = argv[0];
} else
usage();
}
}
if (opt_version || opt_verbose)
puts(VERSION);
if (!opt_device)
opt_device = DEFAULT_FRAMEBUFFER;
// calculate and set the colour map
calculate_cmap(&cmap);
// open the framebuffer device
if (opt_verbose)
printf("Opening framebuffer device '%s'\n", opt_device);
if ((fh = open(opt_device, O_RDONLY)) == -1)
die("open %s: %s\n", opt_device, strerror(errno));
// apply the colour map using ioctl
if (ioctl(fh, FBIOPUTCMAP, &cmap))
die("ioctl FBIOPUTCMAP: %s\n", strerror(errno));
free(cmap.red);
free(cmap.green);
free(cmap.blue);
close(fh);
exit(0);
}