summaryrefslogtreecommitdiff
path: root/framebuffer/fb_plotters.c
blob: c99c63c2ce90d9b492559d5f098432e831377a02 (plain)
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
/*
 * Copyright 2008 Vincent Sanders <vince@simtec.co.uk>
 *
 * This file is part of NetSurf, http://www.netsurf-browser.org/
 *
 * NetSurf is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * NetSurf is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <sys/types.h>
#include <stdint.h>
#include <string.h>
#include <limits.h>

#include "utils/log.h"
#include "utils/utf8.h"
#include "desktop/plotters.h"

#include "framebuffer/fb_gui.h"
#include "framebuffer/fb_plotters.h"
#include "framebuffer/fb_bitmap.h"
#include "framebuffer/fb_font.h"
#include "framebuffer/fb_frontend.h"

/* max height the poly plotter can cope with */
#define WINDOW_HEIGHT (2048)

/* Currently selected ploting routines. */
struct plotter_table plot;

/* Current plotting context */
bbox_t fb_plot_ctx;

enum {
        POINT_LEFTOF_REGION = 1,
        POINT_RIGHTOF_REGION = 2,
        POINT_ABOVE_REGION = 4,
        POINT_BELOW_REGION = 8,
};

#define REGION(x,y,cx1,cx2,cy1,cy2) ( ( (y) > (cy2) ? POINT_BELOW_REGION : 0) |  \
                                      ( (y) < (cy1) ? POINT_ABOVE_REGION : 0) |  \
                                      ( (x) > (cx2) ? POINT_RIGHTOF_REGION : 0) |  \
                                      ( (x) < (cx1) ? POINT_LEFTOF_REGION : 0) )

#define SWAP(a, b) do { int t; t=(a); (a)=(b); (b)=t;  } while(0)

/* clip a rectangle to another rectangle */
bool fb_plotters_clip_rect(const bbox_t *clip,
                           int *x0, int *y0, int *x1, int *y1)
{
        char region1;
        char region2;

	if (*x1 < *x0) SWAP(*x0, *x1);

	if (*y1 < *y0) SWAP(*y0, *y1);

	region1 = REGION(*x0, *y0, clip->x0, clip->x1 - 1, clip->y0, clip->y1 - 1);
	region2 = REGION(*x1, *y1, clip->x0, clip->x1 - 1, clip->y0, clip->y1 - 1);

        /* area lies entirely outside the clipping rectangle */
        if ((region1 | region2) && (region1 & region2))
                return false;

        if (*x0 < clip->x0)
                *x0 = clip->x0;
        if (*x0 > clip->x1)
                *x0 = clip->x1;

        if (*x1 < clip->x0)
                *x1 = clip->x0;
        if (*x1 > clip->x1)
                *x1 = clip->x1;

        if (*y0 < clip->y0)
                *y0 = clip->y0;
        if (*y0 > clip->y1)
                *y0 = clip->y1;

        if (*y1 < clip->y0)
                *y1 = clip->y0;
        if (*y1 > clip->y1)
                *y1 = clip->y1;

        return true;
}

bool fb_plotters_clip_rect_ctx(int *x0, int *y0, int *x1, int *y1)
{
        return fb_plotters_clip_rect(&fb_plot_ctx, x0, y0, x1, y1);
}

/** Clip a line to a bounding box.
 */
bool fb_plotters_clip_line(const bbox_t *clip,
                           int *x0, int *y0, int *x1, int *y1)
{
        char region1;
        char region2;
        region1 = REGION(*x0, *y0, clip->x0, clip->x1 - 1, clip->y0, clip->y1 - 1);
        region2 = REGION(*x1, *y1, clip->x0, clip->x1 - 1, clip->y0, clip->y1 - 1);

        while (region1 | region2) {
                if (region1 & region2) {
                        /* line lies entirely outside the clipping rectangle */
                        return false;
                }

                if (region1) {
                        /* first point */
                        if (region1 & POINT_BELOW_REGION) {
                                /* divide line at bottom */
                                *x0 = (*x0 + (*x1 - *x0) *
                                       (clip->y1 - 1 - *y0) / (*y1-*y0));
                                *y0 = clip->y1 - 1;
                        } else if (region1 & POINT_ABOVE_REGION) {
                                /* divide line at top */
                                *x0 = (*x0 + (*x1 - *x0) *
                                       (clip->y0 - *y0) / (*y1-*y0));
                                *y0 = clip->y0;
                        } else if (region1 & POINT_RIGHTOF_REGION) {
                                /* divide line at right */
                                *y0 = (*y0 + (*y1 - *y0) *
                                       (clip->x1  - 1 - *x0) / (*x1-*x0));
                                *x0 = clip->x1 - 1;
                        } else if (region1 & POINT_LEFTOF_REGION) {
                                /* divide line at right */
                                *y0 = (*y0 + (*y1 - *y0) *
                                       (clip->x0 - *x0) / (*x1-*x0));
                                *x0 = clip->x0;
                        }

                        region1 = REGION(*x0, *y0,
                                         clip->x0, clip->x1 - 1, clip->y0, clip->y1 - 1);
                } else {
                        /* second point */
                        if (region2 & POINT_BELOW_REGION) {
                                /* divide line at bottom*/
                                *x1 = (*x0 + (*x1 - *x0) *
                                       (clip->y1  - 1 - *y0) / (*y1-*y0));
                                *y1 = clip->y1 - 1;
                        } else if (region2 & POINT_ABOVE_REGION) {
                                /* divide line at top*/
                                *x1 = (*x0 + (*x1 - *x0) *
                                       (clip->y0 - *y0) / (*y1-*y0));
                                *y1 = clip->y0;
                        } else if (region2 & POINT_RIGHTOF_REGION) {
                                /* divide line at right*/
                                *y1 = (*y0 + (*y1 - *y0) *
                                       (clip->x1  - 1 - *x0) / (*x1 - *x0));
                                *x1 = clip->x1 - 1;
                        } else if (region2 & POINT_LEFTOF_REGION) {
                                /* divide line at right*/
                                *y1 = (*y0 + (*y1 - *y0) *
                                       (clip->x0 - *x0) / (*x1 - *x0));
                                *x1 = clip->x0;
                        }

                        region2 = REGION(*x1, *y1,
                                         clip->x0, clip->x1 - 1, clip->y0, clip->y1 - 1);
                }
        }

        return true;
}

bool fb_plotters_clip_line_ctx(int *x0, int *y0, int *x1, int *y1)
{
        return fb_plotters_clip_line(&fb_plot_ctx, x0, y0, x1, y1);
}

/* generic setting of clipping rectangle */
bool fb_clip(int x0, int y0, int x1, int y1)
{
        bbox_t clip;
        struct gui_window *g;

        g = window_list;

	if (x1 < x0) SWAP(x0, x1);
	if (y1 < y0) SWAP(y0, y1);

        clip.x0 = g->x;
        clip.y0 = g->y;
        clip.x1 = g->x + g->width;
        clip.y1 = g->y + g->height;

        if (fb_plotters_clip_rect(&clip, &x0, &y0, &x1, &y1)) {
                /* new clipping region is inside the root window */
                fb_plot_ctx.x0 = x0;
                fb_plot_ctx.y0 = y0;
                fb_plot_ctx.x1 = x1;
                fb_plot_ctx.y1 = y1;
        }

        /*LOG(("%d, %d - %d, %d clipped to %d, %d - %d, %d",
             x0,y0,x1,y1,
             fb_plot_ctx.x0, fb_plot_ctx.y0, fb_plot_ctx.x1, fb_plot_ctx.y1)); */

	return true;
}

colour fb_plotters_ablend(colour pixel, colour scrpixel)
{
        int opacity = (pixel >> 24) & 0xFF;
        int r,g,b;

        r = (((pixel & 0xFF) * opacity) >> 8) +
            (((scrpixel & 0xFF) * (0xFF - opacity)) >> 8);

        g = ((((pixel & 0xFF00) >> 8) * opacity) >> 8) +
            ((((scrpixel & 0xFF00) >> 8) * (0xFF - opacity)) >> 8);

        b = ((((pixel & 0xFF0000) >> 16) * opacity) >> 8) +
            ((((scrpixel & 0xFF0000) >> 16) * (0xFF - opacity)) >> 8);

        return r | (g << 8) | (b << 16);
}

typedef bool (linefn_t)(int x0, int y0, int x1, int y1, int width, colour c, bool dotted, bool dashed);

typedef struct dcPt_s {
        int x;
        int y;
} dcPt;

typedef struct tEdge {
        int yUpper;
        float xIntersect, dxPerScan;
        struct tEdge *next;
} Edge;

static void insertEdge(Edge *list, Edge *edge)
{
        Edge *p, *q = list;

        p = q->next;
        while (p != NULL) {
                if (edge->xIntersect < p->xIntersect) {
                        p = NULL;
                } else {
                        q = p;
                        p = p->next;
                }
        }
        edge->next = q->next;
        q->next = edge;
}

static int yNext(int k, int cnt, dcPt *pts)
{
        int j;
        if ((k+1) > (cnt-1))
                j = 0;
        else
                j = k +1;

        while (pts[k].y == pts[j].y) {
                if ((j+1) > (cnt-1))
                        j = 0;
                else
                        j++;
        }
        return (pts[j].y);
}

static void makeEdgeRec(dcPt lower, dcPt upper, int yComp,  Edge **edges)
{
        Edge *edge;
        edge = malloc(sizeof(Edge));

        edge->dxPerScan = (float)(upper.x - lower.x) / (upper.y - lower.y);
        edge->xIntersect = lower.x;

        if (upper.y < yComp)
                edge->yUpper = upper.y - 1;
        else
                edge->yUpper = upper.y;

        if (!fb_plotters_clip_line_ctx(&lower.x, &lower.y,
                                       &upper.x, &edge->yUpper)) {
                free(edge);
        } else {
                insertEdge(edges[lower.y], edge);
        }
}

static void buildEdgeList(int cnt, dcPt *pts, Edge **edges)
{
        dcPt v1, v2;
        int i, yPrev = pts[cnt - 2].y;

        v1.x = pts[cnt - 1].x;
        v1.y = pts[cnt - 1].y;
        for (i = 0; i < cnt; i++) {
                v2 = pts[i];
                if (v1.y != v2.y) {
                        /* line is not horizontal */
                        if (v1.y < v2.y)
                                makeEdgeRec(v1, v2, yNext(i,cnt,pts), edges); /* up going edge */
                        else
                                makeEdgeRec(v2, v1, yPrev, edges); /* down going edge */
                }
                yPrev = v1.y;
                v1 = v2;
        }
}

static void buildActiveList(int scan, Edge *active, Edge **edges)
{
        Edge *p,*q;

        p = edges[scan]->next;

        while (p) {
                q = p->next;
                insertEdge(active, p);
                p = q;
        }
}

static void fillScan(int scan, Edge *active, colour fill, linefn_t linefn)
{
        Edge *p1, *p2;

        p1 = active->next;
        while (p1) {
                p2 = p1->next;
                if (p2 == NULL) {
                        LOG(("only one active edge!"));
                        break;
                } else {
                        linefn(p1->xIntersect, scan,
                               p2->xIntersect, scan,
                               1, fill, false, false);
                }
                p1 = p2->next;
        }
}

static void deleteAfter(Edge *q)
{
        Edge *p = q->next;
        q->next = p->next;
        free (p);
}

static void updateActiveList(int scan, Edge *active)
{
        Edge *q = active, *p = active->next;
        while (p) {
                if (scan >= p->yUpper) {
                        p = p->next;
                        deleteAfter(q);
                } else {
                        p->xIntersect = p->xIntersect + p->dxPerScan;
                        q = p;
                        p = p->next;
                }
        }
}

static void resortActiveList(Edge * active)
{
        Edge *q, *p = active->next;

        active->next = NULL;
        while (p) {
                q = p->next;
                insertEdge(active, p);
                p = q;
        }
}


static void scanFill(int cnt, dcPt *pts, colour fill, linefn_t linefn)
{
        Edge *edges[WINDOW_HEIGHT], *active;
        int i, scan;

        for (i = 0; i < WINDOW_HEIGHT; i++) {
                edges[i] = malloc(sizeof(Edge));
                edges[i]->next = NULL;
        }
        buildEdgeList(cnt, pts, edges);

        active = malloc(sizeof(Edge));
        active->next = NULL;
        for (scan = 0; scan < WINDOW_HEIGHT; scan++) {
                buildActiveList (scan, active, edges);
                if (active->next) {
                        fillScan(scan, active, fill, linefn);
                        updateActiveList (scan, active);
                        resortActiveList(active);
                }
        }
        for (i = 0; i < WINDOW_HEIGHT; i++) {
                free(edges[i]);
        }
        free(active);
}

bool
fb_plotters_polygon(const int *p, unsigned int n, colour fill, linefn_t linefn)
{
        scanFill(n, (dcPt *)p, fill, linefn);
        return true;
}

bool fb_plotters_bitmap_tile(int x, int y,
                             int width, int height,
                             struct bitmap *bitmap, colour bg,
                             bool repeat_x, bool repeat_y,
                             struct content *content,
                             bool (bitmapfn)(int x, int y,
                                             int width, int height,
                                             struct bitmap *bitmap,
                                             colour bg,
                                             struct content *content))
{
	int xf,yf;

	/* x and y define coordinate of top left of of the initial explicitly
	 * placed tile. The width and height are the image scaling and the
	 * bounding box defines the extent of the repeat (which may go in all
	 * four directions from the initial tile).
	 */

	LOG(("x %d, y %d, width %d, height %d, bitmap %p, repx %d repy %d content %p", x,y,width,height,bitmap,repeat_x, repeat_y, content));

	if (!(repeat_x || repeat_y)) {
		/* Not repeating at all, so just pass it on */
		LOG(("Not repeating"));
		return bitmapfn(x, y, width, height, bitmap, bg,content);
	}

	/* get left most tile position */
	if (repeat_x)
		for (; x > fb_plot_ctx.x0; x -= width)
			;

	/* get top most tile position */
	if (repeat_y)
		for (; y > fb_plot_ctx.y0; y -= height)
			;

	/* tile down and across to extents */
	for (xf = x; xf < fb_plot_ctx.x1; xf += width) {
		for (yf = y; yf < fb_plot_ctx.y1; yf += height) {
			bitmapfn(xf, yf, width, height, bitmap, bg, content);
			if (!repeat_y)
				break;
		}
		if (!repeat_x)
	   		break;
	}
	return true;
}

bool fb_plotters_move_block(int srcx, int srcy, int width, int height, int dstx, int dsty)
{
	uint8_t *srcptr = (framebuffer->ptr +
                           (srcy * framebuffer->linelen) +
			   ((srcx * framebuffer->bpp) / 8));

	uint8_t *dstptr = (framebuffer->ptr +
                           (dsty * framebuffer->linelen) +
			   ((dstx * framebuffer->bpp) / 8));

        bbox_t redrawbox;
        int hloop;

	LOG(("from (%d,%d) w %d h %d to (%d,%d)",srcx,srcy,width,height,dstx,dsty));

        if (width == framebuffer->width) {
                /* take shortcut and use memmove */
                memmove(dstptr, srcptr, (width * height * framebuffer->bpp) / 8);
        } else {
                
                for (hloop = height; hloop > 0; hloop--) {
                        memmove(dstptr, srcptr, (width * framebuffer->bpp) / 8);
                        srcptr += framebuffer->linelen;
                        dstptr += framebuffer->linelen;
                }
        }
        /* callback to the os specific routine in case it needs to do something
         * explicit to redraw
         */
        redrawbox.x0 = dstx;
        redrawbox.y0 = dsty;
        redrawbox.x1 = dstx + width;
        redrawbox.y1 = dsty + height;
        fb_os_redraw(&redrawbox);

        return true;
}

/*
 * Local Variables:
 * c-basic-offset:8
 * End:
 */