-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdraw.c
More file actions
402 lines (339 loc) · 11.5 KB
/
draw.c
File metadata and controls
402 lines (339 loc) · 11.5 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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#include "draw.h"
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <linux/input.h>
#include <linux/fb.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <termios.h>
#include <linux/vt.h>
#include <signal.h>
#include <unistd.h>
// Absolute value difference between two unsigned chars
// Used for computing color differences inside transforms
unsigned char absdiff(unsigned char a, unsigned char b) {
if (a > b) {
return a - b;
} else {
return b - a;
}
}
// Free an image
void image_free(image_t * image) {
free(image->data);
image->width = 0;
image->height = 0;
image->data = NULL;
free(image);
}
// Invert an image
void hueify_image(image_t*image, unsigned int mask, int min_level) {
for (int i = 0; i < image->width * image->height; i++) {
int current = image->data[i];
unsigned char r = (0xFF0000 & current) >> 16;
unsigned char g = (0x00FF00 & current) >> 8;
unsigned char b = (0x0000FF & current) >> 0;
int is_rgb = (absdiff(r, g) > min_level || absdiff(g, b) > min_level || absdiff(r, b) > min_level);
// If not an RGB value, skip this cell
if (!is_rgb) { continue; }
// This is *NOT* the same as the input color.
// Since the components are not shifted, this is a bitwise "or" of each color.
// (an easy way to approximate the color intensity)
unsigned char mashed = r | g | b;
// Use the mask to set the color of each slot (R,G,B)
unsigned char newr = (mask & (mashed << 16)) >> 16;
unsigned char newg = (mask & (mashed << 8)) >> 8;
unsigned char newb = (mask & (mashed << 0)) >> 0;
// Write back changes
unsigned int r_part = (0x0000FF & newr) << 16;
unsigned int g_part = (0x0000FF & newg) << 8;
unsigned int b_part = (0x0000FF & newb) << 0;
int newcolor = r_part | g_part | b_part;
image->data[i] = newcolor;
// printf("transform(invert): %d (%#010x) (%u,%u,%u) -> (%u,%u,%u) -> (%d, %d, %d), %#010x\n", i, current, r, g, b, newr, newg, newb, r_part, g_part, b_part, newcolor);
}
}
// Grayscale operation is just a color transform with the difference set to 0.
void grayscale_image(image_t* image) {
hueify_image(image, 0xFFFFFF, 0);
}
// Invert an image
void invert_image(image_t*image) {
for (int i = 0; i < image->width * image->height; i++) {
int current = image->data[i];
unsigned char r = (0xFF0000 & current) >> 16;
unsigned char g = (0x00FF00 & current) >> 8;
unsigned char b = (0x0000FF & current) >> 0;
unsigned char newr = 255 - r;
unsigned char newg = 255 - g;
unsigned char newb = 255 - b;
unsigned int r_part = (0x0000FF & newr) << 16;
unsigned int g_part = (0x0000FF & newg) << 8;
unsigned int b_part = (0x0000FF & newb) << 0;
int newcolor = r_part | g_part | b_part;
image->data[i] = newcolor;
// printf("transform(invert): %d (%#010x) (%u,%u,%u) -> (%u,%u,%u) -> (%d, %d, %d), %#010x\n", i, current, r, g, b, newr, newg, newb, r_part, g_part, b_part, newcolor);
}
}
// Set an individual pixel. This is SLOW for bulk operations.
// Do as little as possible, and memcpy the result.
void set_pixel(int x, int y, context_t * context, int color) {
int write_index = x+y*context->width;
if (write_index < context->width * context->height) {
context->data[x+y*context->width] = color;
} else {
printf("Attempted to set color #%x at x=%d, y=%d). (out of bounds)\n", color, x, y);
exit(1);
}
}
// We scale and crop the image to this new rect.
image_t * scale(image_t*image, int w, int h) {
int sfx = w / image->width;
int sfy = h / image->height;
int crop_x_w = image->width;
int crop_y_h = image->height;
int crop_x = 0;
int crop_y = 0;
if (sfx < sfy) {
crop_x_w = image->height * w / h;
crop_x = (image->width - crop_x_w) / 2;
} else if(sfx > sfy) {
crop_y_h = image->width * h / w;
crop_y = (image->height - crop_y_h) / 2;
}
image_t * new_image = malloc(sizeof(image_t));
new_image->data = malloc(sizeof(int) * w * h);
new_image->width = w;
new_image->height = h;
for(int x = 0; x < w; x++) {
for(int y = 0; y < h; y++) {
int tr_x = ((float) crop_x_w / (float) w) * x + crop_x;
int tr_y = ((float) crop_y_h / (float) h) * y + crop_y;
new_image->data[y * w + x] = image->data[tr_y * image->width + tr_x];
}
}
return new_image;
/*
for(int x = 0; x < w; x++) {
for(int y = 0; y < h; y++) {
int tr_x = ((float) image->width / (float) w) * (float) x;
int tr_y = ((float) image->height / (float) h) * (float) y;
new_image->data[y * w + x] = image->data[tr_y * image->width + tr_x];
}
}
*/
}
// !! This operation is potentially unsafe. Use drawImage. It's harder to mess up.
// X and w are the size of the array.
void draw_array(int x, int y, int w, int h, int* array, context_t* context) {
// Ignore draws out of bounds
if (x > context->width || y > context->height) {
return;
}
// Ignore draws out of bounds
if (x + w < 0 || y + h < 0) {
return;
}
// Column and row correction for partial onscreen images
int cy = 0;
int cx = 0;
// if y is less than 0, trim that many lines off the render.
if (y < 0) {
cy = -y;
}
// If x is less than 0, trim that many pixels off the render line.
if (x < 0) {
cx = -x;
}
// Number of items in a line
int line_width = (w - cx);
// Number of lines total.
// We don't subtract cy because the loop starts with cy already advanced.
int line_count = h;
// If the end of the line goes offscreen, trim that many pixels off the
// row.
if (x + w > context->width) {
line_width -= ((x + w) - context->width);
}
// If the number of rows is more than the height of the context, trim
// them off.
if (y + h > context->height) {
line_count -= ((y + h) - context->height);
}
for (cy; cy < line_count; cy++) {
// Draw each graphics line.
memcpy(
&context->data[context->width * y + context->width * cy + x + cx],
&array[cy * w] + cx,
sizeof(int) * line_width
);
}
}
void draw_image(int x, int y, image_t * image, context_t* context) {
draw_array(x, y, image->width, image->height, image->data, context);
}
void draw_rect(int x, int y, int w, int h, context_t* context, int color) {
// Ignore draws out of bounds
if (x > context->width || y > context->height) {
return;
}
// Ignore draws out of bounds
if(x + w < 0 || y + h < 0) {
return;
}
// Trim offscreen pixels
if (x < 0) {
w += x;
x = 0;
}
// Trim offscreen lines
if (y < 0) {
h += y;
y = 0;
}
// Trim offscreen pixels
if (x + w > context->width) {
w -= ((x + w) - context->width);
}
// Trim offscreen lines.
if (y + h > context->height) {
h -= ((y + h) - context->height);
}
// Set the first line.
for (int rx = x; rx < x+w; rx++) {
set_pixel(rx, y, context, color);
}
// Repeat the first line.
for (int ry = 1; ry < h; ry++) {
memcpy(
&context->data[context->width * y + context->width * ry + x], &
context->data[context->width * y + x],
w*sizeof(int)
);
}
}
void clear_context_color(context_t* context, int color) {
draw_rect(0, 0, context->width, context->height, context, color);
}
void clear_context_gray(context_t* context, unsigned char gray) {
memset(context->data, gray, context->width * context->height * sizeof(int));
}
void clear_context(context_t* context) {
memset(context->data, 0, context->width * context->height * sizeof(int));
}
void test_pattern(context_t* context) {
const unsigned int pattern[8] =
{
0xFFFFFF,
0xFFFF00,
0x00FFFF,
0x00FF00,
0xFF00FF,
0xFF0000,
0x0000FF,
0x000000
};
unsigned columnWidth = context->width / 8;
for(int rx = 0; rx < context->width; rx++) {
set_pixel(rx, 0, context, pattern[rx / columnWidth]);
}
// make it faster: memcpy the first row.
for(int y = 1; y < context->height; y++) {
memcpy(&context[context->width * y], context, context->width*sizeof(int));
}
}
void context_release(context_t * context) {
munmap(context->data, context->width * context->height);
close(context->fb_file_desc);
context->data = NULL;
context->fb_file_desc = 0;
free(context);
}
context_t * context_create() {
char *FB_NAME = "/dev/fb0";
void* mapped_ptr = NULL;
struct fb_fix_screeninfo fb_fixinfo;
struct fb_var_screeninfo fb_varinfo;
int fb_file_desc;
int fb_size = 0;
// Open the framebuffer device in read write
fb_file_desc = open(FB_NAME, O_RDWR);
if (fb_file_desc < 0) {
printf("Unable to open %s.\n", FB_NAME);
return NULL;
}
//Do Ioctl. Retrieve fixed screen info.
if (ioctl(fb_file_desc, FBIOGET_FSCREENINFO, &fb_fixinfo) < 0) {
printf("get fixed screen info failed: %s\n",
strerror(errno));
close(fb_file_desc);
return NULL;
}
// Do Ioctl. Get the variable screen info.
if (ioctl(fb_file_desc, FBIOGET_VSCREENINFO, &fb_varinfo) < 0) {
printf("Unable to retrieve variable screen info: %s\n",
strerror(errno));
close(fb_file_desc);
return NULL;
}
// Calculate the size to mmap
fb_size = fb_fixinfo.line_length * fb_varinfo.yres;
// Now mmap the framebuffer.
mapped_ptr = mmap(NULL, fb_size, PROT_READ | PROT_WRITE, MAP_SHARED, fb_file_desc,0);
if (mapped_ptr == NULL) {
printf("mmap failed:\n");
close(fb_file_desc);
return NULL;
}
context_t * context = malloc(sizeof(context_t));
context->data = (int *) mapped_ptr;
context->width = fb_fixinfo.line_length / 4;
context->height = fb_varinfo.yres;
context->fb_file_desc = fb_file_desc;
context->fb_name = FB_NAME;
return context;
}
context_t * context_get_dimensions() {
char *FB_NAME = "/dev/fb0";
void* mapped_ptr = NULL;
struct fb_fix_screeninfo fb_fixinfo;
struct fb_var_screeninfo fb_varinfo;
int fb_file_desc;
int fb_size = 0;
// Open the framebuffer device in read write
fb_file_desc = open(FB_NAME, O_RDWR);
if (fb_file_desc < 0) {
printf("Unable to open %s.\n", FB_NAME);
return NULL;
}
//Do Ioctl. Retrieve fixed screen info.
if (ioctl(fb_file_desc, FBIOGET_FSCREENINFO, &fb_fixinfo) < 0) {
printf("get fixed screen info failed: %s\n",
strerror(errno));
close(fb_file_desc);
return NULL;
}
// Do Ioctl. Get the variable screen info.
if (ioctl(fb_file_desc, FBIOGET_VSCREENINFO, &fb_varinfo) < 0) {
printf("Unable to retrieve variable screen info: %s\n",
strerror(errno));
close(fb_file_desc);
return NULL;
}
// Calculate the size to mmap
fb_size = fb_fixinfo.line_length * fb_varinfo.yres;
close(fb_file_desc);
context_t * context = malloc(sizeof(context_t));
context->data = NULL;
context->width = fb_fixinfo.line_length / 4;
context->height = fb_varinfo.yres;
context->fb_file_desc = -1; // assign fd to -1
context->fb_name = FB_NAME;
return context;
}