Root/
| 1 | /* |
| 2 | * gui_util.c - GUI helper functions |
| 3 | * |
| 4 | * Written 2010 by Werner Almesberger |
| 5 | * Copyright 2010 by Werner Almesberger |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | */ |
| 12 | |
| 13 | |
| 14 | #include <stdint.h> |
| 15 | #include <math.h> |
| 16 | #include <assert.h> |
| 17 | #include <gtk/gtk.h> |
| 18 | |
| 19 | #include "gui_util.h" |
| 20 | |
| 21 | |
| 22 | void hpoint(guchar *rgbbuf, int x, int y, int sx, uint8_t c[3]) |
| 23 | { |
| 24 | guchar *p = rgbbuf+(y*sx+x)*3; |
| 25 | |
| 26 | p[0] = c[0]; |
| 27 | p[1] = c[1]; |
| 28 | p[2] = c[2]; |
| 29 | } |
| 30 | |
| 31 | |
| 32 | void vpoint(guchar *rgbbuf, int y, int x, int sx, uint8_t c[3]) |
| 33 | { |
| 34 | guchar *p = rgbbuf+(y*sx+x)*3; |
| 35 | |
| 36 | p[0] = c[0]; |
| 37 | p[1] = c[1]; |
| 38 | p[2] = c[2]; |
| 39 | } |
| 40 | |
| 41 | |
| 42 | static void alpha(guchar *rgbbuf, int f, int i, int sx, uint8_t c[3], double w, |
| 43 | void (*point)(guchar *rgbbuf, int f, int i, int sx, uint8_t c[3])) |
| 44 | { |
| 45 | uint8_t ct[3]; |
| 46 | int n; |
| 47 | |
| 48 | assert(w >= 0); |
| 49 | assert(w <= 1); |
| 50 | for (n = 0; n != 3; n++) |
| 51 | ct[n] = round(c[n]*w); |
| 52 | point(rgbbuf, f, i, sx, ct); |
| 53 | } |
| 54 | |
| 55 | |
| 56 | void aa_line(guchar *rgbbuf, int i, double fa, double fb, int max, int sx, |
| 57 | uint8_t c[3], |
| 58 | void (*point)(guchar *rgbbuf, int f, int i, int sx, uint8_t c[3])) |
| 59 | { |
| 60 | int f0, f1; |
| 61 | int from, to, n; |
| 62 | |
| 63 | assert(max > 0); |
| 64 | assert(fb >= fa); |
| 65 | f0 = floor(fa); |
| 66 | f1 = ceil(fb); |
| 67 | if (f0 > max || f1 < 0) |
| 68 | return; |
| 69 | if (f0 == f1) { |
| 70 | if (f0 >= 0 && f1 <= max) |
| 71 | point(rgbbuf, f0, i, sx, c); |
| 72 | return; |
| 73 | } |
| 74 | if (f0 >= 0) |
| 75 | alpha(rgbbuf, f0, i, sx, c, 1-(fa-f0), point); |
| 76 | if (f1 <= max) |
| 77 | alpha(rgbbuf, f1, i, sx, c, 1-(f1-fb), point); |
| 78 | from = f0+1 > 0 ? f0+1 : 0; |
| 79 | to = f1-1 < max ? f1-1: max; |
| 80 | for (n = from; n <= to; n++) |
| 81 | point(rgbbuf, n, i, sx, c); |
| 82 | } |
| 83 | |
| 84 | |
| 85 | void draw_circle(GdkDrawable *da, GdkGC *gc, int x, int y, int r) |
| 86 | { |
| 87 | gdk_draw_arc(da, gc, FALSE, x-r, y-r, 2*r, 2*r, 0, 360*64); |
| 88 | } |
| 89 |
Branches:
master
