summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorSascha Hauer <s.hauer@pengutronix.de>2016-07-11 07:58:34 +0200
committerSascha Hauer <s.hauer@pengutronix.de>2016-07-11 07:58:34 +0200
commitca95c2bab96b4109e3340e79dbc9a584577fea91 (patch)
tree634d5de8d78710fc6125448ae0828528241376ff /lib
parent50543054eb23e9b93b5be4a272d130f04bf8bc4d (diff)
parentc957ace732458d48b5e7da4297c40c160e340993 (diff)
downloadbarebox-ca95c2bab96b4109e3340e79dbc9a584577fea91.tar.gz
barebox-ca95c2bab96b4109e3340e79dbc9a584577fea91.tar.xz
Merge branch 'for-next/video'
Diffstat (limited to 'lib')
-rw-r--r--lib/Makefile1
-rw-r--r--lib/gui/2d-primitives.c199
-rw-r--r--lib/gui/Kconfig3
-rw-r--r--lib/gui/Makefile1
-rw-r--r--lib/gui/graphic_utils.c29
-rw-r--r--lib/int_sqrt.c46
6 files changed, 279 insertions, 0 deletions
diff --git a/lib/Makefile b/lib/Makefile
index 399d896bd9..92404fd331 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -58,3 +58,4 @@ obj-$(CONFIG_BAREBOX_LOGO) += logo/
obj-y += reed_solomon/
obj-$(CONFIG_RATP) += ratp.o
obj-y += list_sort.o
+obj-y += int_sqrt.o
diff --git a/lib/gui/2d-primitives.c b/lib/gui/2d-primitives.c
new file mode 100644
index 0000000000..f3814eea44
--- /dev/null
+++ b/lib/gui/2d-primitives.c
@@ -0,0 +1,199 @@
+#include <common.h>
+#include <fb.h>
+#include <gui/graphic_utils.h>
+#include <linux/stat.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <fs.h>
+#include <malloc.h>
+
+static void __illuminate(struct fb_info *info,
+ int x, int y,
+ u8 r, u8 g, u8 b, u8 a)
+{
+ void *pixel;
+
+ pixel = fb_get_screen_base(info);
+ pixel += y * info->line_length + x * (info->bits_per_pixel >> 3);
+
+ gu_set_rgba_pixel(info, pixel, r, g, b, a);
+}
+
+static void illuminate(struct fb_info *info,
+ bool invert,
+ int x, int y,
+ u8 r, u8 g, u8 b, u8 a)
+{
+ if (invert)
+ __illuminate(info, y, x,
+ r, g, b, a);
+ else
+ __illuminate(info, x, y,
+ r, g, b, a);
+
+}
+
+
+static void draw_simple_line(struct screen *sc,
+ int x1, int y1,
+ int x2, int y2,
+ u8 r, u8 g, u8 b, u8 a,
+ unsigned int dash)
+{
+ int x;
+ bool invert = false;
+ unsigned int pixel = 0;
+
+ BUG_ON(x1 != x2 &&
+ y1 != y2);
+
+ if (x1 == x2) {
+ swap(x1, y1);
+ swap(x2, y2);
+ invert = true;
+ }
+
+ if (x1 > x2) {
+ swap(x1, x2);
+ swap(y1, y2);
+ }
+
+ for (x = x1; x < x2 - 1; x++) {
+ if (!dash ||
+ (++pixel % (2 * dash)) < dash)
+ illuminate(sc->info,
+ invert,
+ x, y1,
+ r, g, b, a);
+ }
+}
+
+/**
+ * gl_draw_line - draw a 2D dashed line between (x1, y1) and (x2,y2)
+ *
+ * @sc: screen to draw on
+ * @x1, @y1: first point defining the line
+ * @x2, @y2: second point defining the line
+ * @r, @g, @b, @a: line's color
+ * @dash: dash length (0 denotes solid line)
+ *
+ * gl_draw_line() implements integer version of Bresenham's algoritm
+ * as can be found here:
+ *
+ * http://www.idav.ucdavis.edu/education/GraphicsNotes/Bresenhams-Algorithm.pdf
+ */
+void gu_draw_line(struct screen *sc,
+ int x1, int y1,
+ int x2, int y2,
+ u8 r, u8 g, u8 b, u8 a,
+ unsigned int dash)
+{
+ int dx;
+ int dy;
+ int i, j, eps;
+ bool invert = false;
+ unsigned int pixel = 0;
+
+ BUG_ON(x1 < 0 || y1 < 0 ||
+ x2 < 0 || y2 < 0);
+
+ if (x1 == x2 || y1 == y2) {
+ draw_simple_line(sc,
+ x1, y1,
+ x2, y2,
+ r, g, b, a, dash);
+ return;
+ }
+
+ dx = abs(x2 - x1);
+ dy = abs(y2 - y1);
+
+ /*
+ * First thing we need to determine "Driving Axis", as can be
+ * seen below if Y-axis projection of the line is bigger than
+ * X-axis projection we swap axes and pretend the X is Y and
+ * vice versa
+ */
+ if (dy > dx) {
+ swap(x1, y1);
+ swap(x2, y2);
+ swap(dx, dy);
+ invert = true;
+ }
+
+ /*
+ * Second, we need to make sure that we will be traversing
+ * driving axis in the direction of increment so we swap point
+ * 1 with point 2 if x1 is greater than x2
+ */
+ if (x1 > x2) {
+ swap(x1, x2);
+ swap(y1, y2);
+ }
+
+ j = y1;
+ eps = dy - dx;
+
+ for (i = x1; i <= x2 - 1; i++) {
+ if (!dash ||
+ (++pixel % (2 * dash)) > dash) {
+ illuminate(sc->info,
+ invert,
+ j, i,
+ r, g, b, a);
+ } else {
+ printf("NOT illuminating pixel: %d\n", pixel);
+ }
+
+ if (eps >= 0) {
+ j += 1;
+ eps -= dx;
+ }
+
+ eps += dy;
+ }
+}
+
+/**
+ * gl_draw_circle - draw a 2D circle with center at (x0, y0)
+ *
+ * @sc: screen to draw on
+ * @x0, @y0: coordinates of circle's center
+ * @radius: circle's radius
+ * @r, @g, @b, @a: circle's color
+
+ *
+ * gu_draw_circle() implements midpoint circle algorithm as can be
+ * found here:
+ *
+ * https://en.wikipedia.org/wiki/Midpoint_circle_algorithm
+ */
+void gu_draw_circle(struct screen *sc,
+ int x0, int y0, int radius,
+ u8 r, u8 g, u8 b, u8 a)
+{
+ int x = radius;
+ int y = 0;
+ int e = 0;
+
+ BUG_ON(x0 < 0 || y0 < 0 || radius < 0);
+
+ while (x >= y) {
+ __illuminate(sc->info, x0 + x, y0 + y, r, g, b, a);
+ __illuminate(sc->info, x0 + y, y0 + x, r, g, b, a);
+ __illuminate(sc->info, x0 - y, y0 + x, r, g, b, a);
+ __illuminate(sc->info, x0 - x, y0 + y, r, g, b, a);
+ __illuminate(sc->info, x0 - x, y0 - y, r, g, b, a);
+ __illuminate(sc->info, x0 - y, y0 - x, r, g, b, a);
+ __illuminate(sc->info, x0 + y, y0 - x, r, g, b, a);
+ __illuminate(sc->info, x0 + x, y0 - y, r, g, b, a);
+
+ y += 1;
+ e += 1 + 2 * y;
+
+ if (2 * (e - x) + 1 > 0) {
+ x -= 1;
+ e += 1 - 2 * x;
+ }
+ }
+}
diff --git a/lib/gui/Kconfig b/lib/gui/Kconfig
index eac9597e2d..2625d9fb2f 100644
--- a/lib/gui/Kconfig
+++ b/lib/gui/Kconfig
@@ -6,6 +6,9 @@ config IMAGE_RENDERER
if IMAGE_RENDERER
+config 2D_PRIMITIVES
+ bool
+
config BMP
bool "bmp"
diff --git a/lib/gui/Makefile b/lib/gui/Makefile
index d4b26c47b8..31e66225d7 100644
--- a/lib/gui/Makefile
+++ b/lib/gui/Makefile
@@ -3,3 +3,4 @@ obj-$(CONFIG_IMAGE_RENDERER) += image_renderer.o graphic_utils.o
obj-$(CONFIG_PNG) += png.o
obj-$(CONFIG_LODEPNG) += png_lode.o lodepng.o
obj-$(CONFIG_PICOPNG) += png_pico.o picopng.o
+obj-$(CONFIG_2D_PRIMITIVES) += 2d-primitives.o
diff --git a/lib/gui/graphic_utils.c b/lib/gui/graphic_utils.c
index 2fe9fa358a..d3f5cce4b7 100644
--- a/lib/gui/graphic_utils.c
+++ b/lib/gui/graphic_utils.c
@@ -336,3 +336,32 @@ void gu_screen_blit(struct screen *sc)
if (info->screen_base_shadow)
memcpy(info->screen_base, info->screen_base_shadow, sc->fbsize);
}
+
+void gu_fill_rectangle(struct screen *sc,
+ int x1, int y1, int x2, int y2,
+ u8 r, u8 g, u8 b, u8 a)
+{
+ int y;
+ void *buf = gui_screen_render_buffer(sc);
+
+ x1 = max(0, x1);
+ y1 = max(0, y1);
+
+ x2 = (x2 < 0) ? sc->info->xres - 1 : x2;
+ y2 = (y2 < 0) ? sc->info->yres - 1 : y2;
+
+ if (x2 < x1)
+ swap(x1, x2);
+ if (y2 < y1)
+ swap(y1, y2);
+
+ for(y = y1; y <= y2; y++) {
+ int x;
+ unsigned char *pixel = buf + y * sc->info->line_length +
+ x1 * (sc->info->bits_per_pixel / 8);
+ for(x = x1; x <= x2; x++) {
+ gu_set_rgba_pixel(sc->info, pixel, r, g, b, a);
+ pixel += sc->info->bits_per_pixel / 8;
+ }
+ }
+}
diff --git a/lib/int_sqrt.c b/lib/int_sqrt.c
new file mode 100644
index 0000000000..30ccafbf9e
--- /dev/null
+++ b/lib/int_sqrt.c
@@ -0,0 +1,46 @@
+/*
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * This program 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; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program 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.
+ *
+ */
+
+#include <common.h>
+
+/**
+ * int_sqrt - rough approximation to sqrt
+ * @x: integer of which to calculate the sqrt
+ *
+ * A very rough approximation to the sqrt() function.
+ */
+unsigned long int_sqrt(unsigned long x)
+{
+ unsigned long b, m, y = 0;
+
+ if (x <= 1)
+ return x;
+
+ m = 1UL << (BITS_PER_LONG - 2);
+ while (m != 0) {
+ b = y + m;
+ y >>= 1;
+
+ if (x >= b) {
+ x -= b;
+ y += m;
+ }
+ m >>= 2;
+ }
+
+ return y;
+}
+EXPORT_SYMBOL(int_sqrt);