util: refactor drawing functions
authorTomi Valkeinen <tomi.valkeinen@ti.com>
Tue, 7 Jun 2016 13:33:56 +0000 (16:33 +0300)
committerTomi Valkeinen <tomi.valkeinen@ti.com>
Tue, 7 Jun 2016 14:02:08 +0000 (17:02 +0300)
libkms++util/drawing.cpp [new file with mode: 0644]
libkms++util/kms++util.h
libkms++util/testpat.cpp

diff --git a/libkms++util/drawing.cpp b/libkms++util/drawing.cpp
new file mode 100644 (file)
index 0000000..a71baf6
--- /dev/null
@@ -0,0 +1,133 @@
+
+#include <kms++.h>
+#include <kms++util.h>
+
+using namespace std;
+
+namespace kms
+{
+void draw_rgb_pixel(IMappedFramebuffer& buf, unsigned x, unsigned y, RGB color)
+{
+       switch (buf.format()) {
+       case PixelFormat::XRGB8888:
+       case PixelFormat::ARGB8888:
+       {
+               uint32_t *p = (uint32_t*)(buf.map(0) + buf.stride(0) * y + x * 4);
+               *p = color.argb8888();
+               break;
+       }
+       case PixelFormat::XBGR8888:
+       case PixelFormat::ABGR8888:
+       {
+               uint32_t *p = (uint32_t*)(buf.map(0) + buf.stride(0) * y + x * 4);
+               *p = color.abgr8888();
+               break;
+       }
+       case PixelFormat::RGB565:
+       {
+               uint16_t *p = (uint16_t*)(buf.map(0) + buf.stride(0) * y + x * 2);
+               *p = color.rgb565();
+               break;
+       }
+       default:
+               throw std::invalid_argument("invalid pixelformat");
+       }
+}
+
+void draw_yuv422_macropixel(IMappedFramebuffer& buf, unsigned x, unsigned y, YUV yuv1, YUV yuv2)
+{
+       ASSERT((x & 1) == 0);
+
+       uint8_t *p = (uint8_t*)(buf.map(0) + buf.stride(0) * y + x * 2);
+
+       uint8_t y0 = yuv1.y;
+       uint8_t y1 = yuv2.y;
+       uint8_t u = (yuv1.u + yuv2.u) / 2;
+       uint8_t v = (yuv1.v + yuv2.v) / 2;
+
+       switch (buf.format()) {
+       case PixelFormat::UYVY:
+               p[0] = u;
+               p[1] = y0;
+               p[2] = v;
+               p[3] = y1;
+               break;
+
+       case PixelFormat::YUYV:
+               p[0] = y0;
+               p[1] = u;
+               p[2] = y1;
+               p[3] = v;
+               break;
+
+       case PixelFormat::YVYU:
+               p[0] = y0;
+               p[1] = v;
+               p[2] = y1;
+               p[3] = u;
+               break;
+
+       case PixelFormat::VYUY:
+               p[0] = v;
+               p[1] = y0;
+               p[2] = u;
+               p[3] = y1;
+               break;
+
+       default:
+               throw std::invalid_argument("invalid pixelformat");
+       }
+}
+
+void draw_yuv420_macropixel(IMappedFramebuffer& buf, unsigned x, unsigned y,
+                                  YUV yuv1, YUV yuv2, YUV yuv3, YUV yuv4)
+{
+       ASSERT((x & 1) == 0);
+       ASSERT((y & 1) == 0);
+
+       uint8_t *py1 = (uint8_t*)(buf.map(0) + buf.stride(0) * (y + 0) + x);
+       uint8_t *py2 = (uint8_t*)(buf.map(0) + buf.stride(0) * (y + 1) + x);
+
+       uint8_t *puv = (uint8_t*)(buf.map(1) + buf.stride(1) * (y / 2) + x);
+
+       uint8_t y0 = yuv1.y;
+       uint8_t y1 = yuv2.y;
+       uint8_t y2 = yuv3.y;
+       uint8_t y3 = yuv4.y;
+       uint8_t u = (yuv1.u + yuv2.u + yuv3.u + yuv4.u) / 4;
+       uint8_t v = (yuv1.v + yuv2.v + yuv3.v + yuv4.v) / 4;
+
+       switch (buf.format()) {
+       case PixelFormat::NV12:
+               py1[0] = y0;
+               py1[1] = y1;
+               py2[0] = y2;
+               py2[1] = y3;
+               puv[0] = u;
+               puv[1] = v;
+               break;
+
+       case PixelFormat::NV21:
+               py1[0] = y0;
+               py1[1] = y1;
+               py2[0] = y2;
+               py2[1] = y3;
+               puv[0] = v;
+               puv[1] = u;
+               break;
+
+       default:
+               throw std::invalid_argument("invalid pixelformat");
+       }
+}
+
+void draw_rect(IMappedFramebuffer &fb, uint32_t x, uint32_t y, uint32_t w, uint32_t h, RGB color)
+{
+       for (unsigned i = x; i < x + w; ++i) {
+               for (unsigned j = y; j < y + h; ++j) {
+                       draw_rgb_pixel(fb, i, j, color);
+               }
+       }
+}
+
+}
index 00e9fdfd31956fb67cbcdc78584c974cc725f4d3..042887f21883302f39303f3bd78ddcfcd264df9f 100644 (file)
@@ -10,12 +10,16 @@ namespace kms
 {
 class IMappedFramebuffer;
 
+void draw_rgb_pixel(IMappedFramebuffer& buf, unsigned x, unsigned y, RGB color);
+void draw_yuv422_macropixel(IMappedFramebuffer& buf, unsigned x, unsigned y, YUV yuv1, YUV yuv2);
+void draw_yuv420_macropixel(IMappedFramebuffer& buf, unsigned x, unsigned y,
+                                  YUV yuv1, YUV yuv2, YUV yuv3, YUV yuv4);
+void draw_rect(IMappedFramebuffer &fb, uint32_t x, uint32_t y, uint32_t w, uint32_t h, RGB color);
+
 void draw_color_bar(IMappedFramebuffer& buf, int old_xpos, int xpos, int width);
 
 void draw_test_pattern(IMappedFramebuffer &fb);
 
-void draw_rect(IMappedFramebuffer &fb, uint32_t x, uint32_t y, uint32_t w, uint32_t h, RGB color);
-
 Connector* resolve_connector(Card& card, const std::string& str);
 }
 
index 6296b1925156c363d6b1335c61d45d3a0707aafd..b859180c23801155b7913aa76f639d3806089d2b 100644 (file)
 #include <cpuframebuffer.h>
 #include "stopwatch.h"
 
-#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
-
 using namespace std;
 
 namespace kms
 {
-static void draw_rgb_pixel(IMappedFramebuffer& buf, unsigned x, unsigned y, RGB color)
-{
-       switch (buf.format()) {
-       case PixelFormat::XRGB8888:
-       case PixelFormat::ARGB8888:
-       {
-               uint32_t *p = (uint32_t*)(buf.map(0) + buf.stride(0) * y + x * 4);
-               *p = color.argb8888();
-               break;
-       }
-       case PixelFormat::XBGR8888:
-       case PixelFormat::ABGR8888:
-       {
-               uint32_t *p = (uint32_t*)(buf.map(0) + buf.stride(0) * y + x * 4);
-               *p = color.abgr8888();
-               break;
-       }
-       case PixelFormat::RGB565:
-       {
-               uint16_t *p = (uint16_t*)(buf.map(0) + buf.stride(0) * y + x * 2);
-               *p = color.rgb565();
-               break;
-       }
-       default:
-               throw std::invalid_argument("invalid pixelformat");
-       }
-}
-
-static void draw_yuv422_macropixel(IMappedFramebuffer& buf, unsigned x, unsigned y, YUV yuv1, YUV yuv2)
-{
-       ASSERT((x & 1) == 0);
-
-       uint8_t *p = (uint8_t*)(buf.map(0) + buf.stride(0) * y + x * 2);
-
-       uint8_t y0 = yuv1.y;
-       uint8_t y1 = yuv2.y;
-       uint8_t u = (yuv1.u + yuv2.u) / 2;
-       uint8_t v = (yuv1.v + yuv2.v) / 2;
-
-       switch (buf.format()) {
-       case PixelFormat::UYVY:
-               p[0] = u;
-               p[1] = y0;
-               p[2] = v;
-               p[3] = y1;
-               break;
-
-       case PixelFormat::YUYV:
-               p[0] = y0;
-               p[1] = u;
-               p[2] = y1;
-               p[3] = v;
-               break;
-
-       case PixelFormat::YVYU:
-               p[0] = y0;
-               p[1] = v;
-               p[2] = y1;
-               p[3] = u;
-               break;
-
-       case PixelFormat::VYUY:
-               p[0] = v;
-               p[1] = y0;
-               p[2] = u;
-               p[3] = y1;
-               break;
-
-       default:
-               throw std::invalid_argument("invalid pixelformat");
-       }
-}
-
-static void draw_yuv420_macropixel(IMappedFramebuffer& buf, unsigned x, unsigned y,
-                                  YUV yuv1, YUV yuv2, YUV yuv3, YUV yuv4)
-{
-       ASSERT((x & 1) == 0);
-       ASSERT((y & 1) == 0);
-
-       uint8_t *py1 = (uint8_t*)(buf.map(0) + buf.stride(0) * (y + 0) + x);
-       uint8_t *py2 = (uint8_t*)(buf.map(0) + buf.stride(0) * (y + 1) + x);
-
-       uint8_t *puv = (uint8_t*)(buf.map(1) + buf.stride(1) * (y / 2) + x);
-
-       uint8_t y0 = yuv1.y;
-       uint8_t y1 = yuv2.y;
-       uint8_t y2 = yuv3.y;
-       uint8_t y3 = yuv4.y;
-       uint8_t u = (yuv1.u + yuv2.u + yuv3.u + yuv4.u) / 4;
-       uint8_t v = (yuv1.v + yuv2.v + yuv3.v + yuv4.v) / 4;
-
-       switch (buf.format()) {
-       case PixelFormat::NV12:
-               py1[0] = y0;
-               py1[1] = y1;
-               py2[0] = y2;
-               py2[1] = y3;
-               puv[0] = u;
-               puv[1] = v;
-               break;
-
-       case PixelFormat::NV21:
-               py1[0] = y0;
-               py1[1] = y1;
-               py2[0] = y2;
-               py2[1] = y3;
-               puv[0] = v;
-               puv[1] = u;
-               break;
-
-       default:
-               throw std::invalid_argument("invalid pixelformat");
-       }
-}
 
 static RGB get_test_pattern_pixel(IMappedFramebuffer& fb, unsigned x, unsigned y)
 {
@@ -304,13 +188,4 @@ void draw_test_pattern(IMappedFramebuffer &fb)
 #endif
 }
 
-void draw_rect(IMappedFramebuffer &fb, uint32_t x, uint32_t y, uint32_t w, uint32_t h, RGB color)
-{
-       for (unsigned i = x; i < x + w; ++i) {
-               for (unsigned j = y; j < y + h; ++j) {
-                       draw_rgb_pixel(fb, i, j, color);
-               }
-       }
-}
-
 }