1 /* GStreamer 2 * Copyright (C) <2011> Stefan Sauer <ensonic@users.sf.net> 3 * 4 * gstdrawhelpers.h: simple drawing helpers 5 * 6 * This library is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU Library General Public 8 * License as published by the Free Software Foundation; either 9 * version 2 of the License, or (at your option) any later version. 10 * 11 * This library is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * Library General Public License for more details. 15 * 16 * You should have received a copy of the GNU Library General Public 17 * License along with this library; if not, write to the 18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, 19 * Boston, MA 02110-1301, USA. 20 */ 21 22 /* FIXME: add versions that don't ignore alpha */ 23 24 #define draw_dot(_vd, _x, _y, _st, _c) G_STMT_START { \ 25 _vd[(_y * _st) + _x] = _c; \ 26 } G_STMT_END 27 28 #define draw_dot_c(_vd, _x, _y, _st, _c) G_STMT_START { \ 29 _vd[(_y * _st) + _x] |= _c; \ 30 } G_STMT_END 31 32 #define draw_dot_aa(_vd, _x, _y, _st, _c, _f) G_STMT_START { \ 33 guint32 _oc, _c1, _c2, _c3; \ 34 \ 35 _oc = _vd[(_y * _st) + _x]; \ 36 _c3 = (_oc & 0xff) + ((_c & 0xff) * _f); \ 37 _c3 = MIN(_c3, 255); \ 38 _c2 = ((_oc & 0xff00) >> 8) + (((_c & 0xff00) >> 8) * _f); \ 39 _c2 = MIN(_c2, 255); \ 40 _c1 = ((_oc & 0xff0000) >> 16) + (((_c & 0xff0000) >> 16) * _f); \ 41 _c1 = MIN(_c1, 255); \ 42 _vd[(_y * _st) + _x] = (_c1 << 16) | (_c2 << 8) | _c3; \ 43 } G_STMT_END 44 45 #define draw_line(_vd, _x1, _x2, _y1, _y2, _st, _c) G_STMT_START { \ 46 guint _i, _j, _x, _y; \ 47 gint _dx = _x2 - _x1, _dy = _y2 - _y1; \ 48 gfloat _f; \ 49 \ 50 _j = abs (_dx) > abs (_dy) ? abs (_dx) : abs (_dy); \ 51 for (_i = 0; _i < _j; _i++) { \ 52 _f = (gfloat) _i / (gfloat) _j; \ 53 _x = _x1 + _dx * _f; \ 54 _y = _y1 + _dy * _f; \ 55 draw_dot (_vd, _x, _y, _st, _c); \ 56 } \ 57 } G_STMT_END 58 59 #define draw_line_aa(_vd, _x1, _x2, _y1, _y2, _st, _c) G_STMT_START { \ 60 guint _i, _j, _x, _y; \ 61 gint _dx = _x2 - _x1, _dy = _y2 - _y1; \ 62 gfloat _f, _rx, _ry, _fx, _fy; \ 63 \ 64 _j = abs (_dx) > abs (_dy) ? abs (_dx) : abs (_dy); \ 65 for (_i = 0; _i < _j; _i++) { \ 66 _f = (gfloat) _i / (gfloat) _j; \ 67 _rx = _x1 + _dx * _f; \ 68 _ry = _y1 + _dy * _f; \ 69 _x = (guint)_rx; \ 70 _y = (guint)_ry; \ 71 _fx = _rx - (gfloat)_x; \ 72 _fy = _ry - (gfloat)_y; \ 73 \ 74 _f = ((1.0 - _fx) + (1.0 - _fy)) / 2.0; \ 75 draw_dot_aa (_vd, _x, _y, _st, _c, _f); \ 76 \ 77 _f = (_fx + (1.0 - _fy)) / 2.0; \ 78 draw_dot_aa (_vd, (_x + 1), _y, _st, _c, _f); \ 79 \ 80 _f = ((1.0 - _fx) + _fy) / 2.0; \ 81 draw_dot_aa (_vd, _x, (_y + 1), _st, _c, _f); \ 82 \ 83 _f = (_fx + _fy) / 2.0; \ 84 draw_dot_aa (_vd, (_x + 1), (_y + 1), _st, _c, _f); \ 85 } \ 86 } G_STMT_END 87 88