blob: e2937fa9ae087835716c98f301021bceee649946 [file] [log] [blame]
// Copyright 2024 The Pigweed Authors
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.
#include "pw_draw/draw.h"
#include "gtest/gtest.h"
#include "pw_color/color.h"
#include "pw_color/colors_pico8.h"
#include "pw_draw/draw.h"
#include "pw_draw/font6x8.h"
#include "pw_draw/text_area.h"
#include "pw_framebuffer/framebuffer.h"
#include "pw_framebuffer/writer.h"
#include "pw_log/log.h"
#include "pw_string/string_builder.h"
using namespace pw::draw;
using namespace pw::framebuffer;
using pw::color::color_rgb565_t;
namespace pw::draw {
namespace {
constexpr color_rgb565_t kBlack = 0x0;
void PrintFramebufferAsANSI(const Framebuffer& fb) {
pw::StringBuffer<4096> line;
pw::StringBuffer<128> color_string;
FramebufferReader reader(fb);
for (int y = 0; y < fb.size().height; y += 2) {
line.clear();
for (int x = 0; x < fb.size().width; x++) {
color_string.clear();
auto row1_color = reader.GetPixel(x, y);
pw::color::ColorRgba row1(row1_color.ok() ? row1_color.value() : kBlack);
auto row2_color = reader.GetPixel(x, y + 1);
if (!row2_color.ok()) {
color_string.Format("[38;2;%d;%d;%dm▀", row1.r, row1.g, row1.b);
} else {
pw::color::ColorRgba row2(row2_color.value());
color_string.Format("[38;2;%d;%d;%dm[48;2;%d;%d;%dm▀",
row1.r,
row1.g,
row1.b,
row2.r,
row2.g,
row2.b);
}
line.append(color_string.data());
}
line.append("\n");
// TODO(tonymd): Log seems to drop characters, maybe a size limit?
// PW_LOG_INFO("%s", line.data());
printf("%s", line.data());
}
}
TEST(DrawLine, Diagonal) {
color_rgb565_t data[4 * 4];
Framebuffer fb(data, PixelFormat::RGB565, {4, 4}, 4 * sizeof(data[0]));
FramebufferWriter writer(fb);
color_rgb565_t indigo = color::kColorsPico8Rgb565[12];
writer.Fill(0);
DrawLine(fb, 0, 0, fb.size().width, fb.size().height, indigo);
PrintFramebufferAsANSI(fb);
// Check the diagonal is set to indigo, everything else should be 0.
Result<color_rgb565_t> c;
for (int x = 0; x < fb.size().width; x++) {
for (int y = 0; y < fb.size().height; y++) {
c = writer.GetPixel(x, y);
ASSERT_TRUE(c.ok());
if (x == y) {
EXPECT_EQ(c.value(), indigo);
} else {
EXPECT_EQ(c.value(), 0);
}
}
}
}
TEST(DrawHLine, Top) {
color_rgb565_t data[4 * 4];
Framebuffer fb(data, PixelFormat::RGB565, {4, 4}, 4 * sizeof(data[0]));
FramebufferWriter writer(fb);
color_rgb565_t indigo = color::kColorsPico8Rgb565[12];
writer.Fill(0);
// Horizonal line at y = 0
DrawHLine(fb, 0, fb.size().width, 0, indigo);
PrintFramebufferAsANSI(fb);
// Check color at y = 0 is indigo, y = 1 should be 0.
Result<color_rgb565_t> c;
for (int x = 0; x < fb.size().width; x++) {
c = writer.GetPixel(x, 0);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), indigo);
c = writer.GetPixel(x, 1);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
}
}
TEST(DrawRect, Empty) {
color_rgb565_t data[5 * 5];
Framebuffer fb(data, PixelFormat::RGB565, {5, 5}, 5 * sizeof(data[0]));
FramebufferWriter writer(fb);
color_rgb565_t indigo = color::kColorsPico8Rgb565[12];
writer.Fill(0);
// 4x4 rectangle, not filled
DrawRect(fb, 0, 0, 3, 3, indigo, false);
PrintFramebufferAsANSI(fb);
Result<color_rgb565_t> c;
c = writer.GetPixel(0, 0);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), indigo);
c = writer.GetPixel(1, 0);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), indigo);
c = writer.GetPixel(2, 0);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), indigo);
c = writer.GetPixel(3, 0);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), indigo);
c = writer.GetPixel(4, 0);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(0, 1);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), indigo);
c = writer.GetPixel(1, 1);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(2, 1);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(3, 1);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), indigo);
c = writer.GetPixel(4, 1);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(0, 2);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), indigo);
c = writer.GetPixel(1, 2);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(2, 2);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(3, 2);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), indigo);
c = writer.GetPixel(4, 2);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(0, 3);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), indigo);
c = writer.GetPixel(1, 3);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), indigo);
c = writer.GetPixel(2, 3);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), indigo);
c = writer.GetPixel(3, 3);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), indigo);
c = writer.GetPixel(4, 3);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(0, 4);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(1, 4);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(2, 4);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(3, 4);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(4, 4);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
}
TEST(DrawRect, Filled) {
color_rgb565_t data[5 * 5];
Framebuffer fb(data, PixelFormat::RGB565, {5, 5}, 5 * sizeof(data[0]));
FramebufferWriter writer(fb);
color_rgb565_t indigo = color::kColorsPico8Rgb565[12];
writer.Fill(0);
// 4x4 rectangle, filled
DrawRect(fb, 0, 0, 3, 3, indigo, true);
PrintFramebufferAsANSI(fb);
Result<color_rgb565_t> c;
c = writer.GetPixel(0, 0);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), indigo);
c = writer.GetPixel(1, 0);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), indigo);
c = writer.GetPixel(2, 0);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), indigo);
c = writer.GetPixel(3, 0);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), indigo);
c = writer.GetPixel(4, 0);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(0, 1);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), indigo);
c = writer.GetPixel(1, 1);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), indigo);
c = writer.GetPixel(2, 1);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), indigo);
c = writer.GetPixel(3, 1);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), indigo);
c = writer.GetPixel(4, 1);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(0, 2);
EXPECT_EQ(c.value(), indigo);
c = writer.GetPixel(1, 2);
EXPECT_EQ(c.value(), indigo);
c = writer.GetPixel(2, 2);
EXPECT_EQ(c.value(), indigo);
c = writer.GetPixel(3, 2);
EXPECT_EQ(c.value(), indigo);
c = writer.GetPixel(4, 2);
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(0, 3);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), indigo);
c = writer.GetPixel(1, 3);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), indigo);
c = writer.GetPixel(2, 3);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), indigo);
c = writer.GetPixel(3, 3);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), indigo);
c = writer.GetPixel(4, 3);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(0, 4);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(1, 4);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(2, 4);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(3, 4);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(4, 4);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
}
TEST(DrawRectWH, WidthHeightCorrect) {
color_rgb565_t data[5 * 5];
Framebuffer fb(data, PixelFormat::RGB565, {5, 5}, 5 * sizeof(data[0]));
FramebufferWriter writer(fb);
color_rgb565_t indigo = color::kColorsPico8Rgb565[12];
writer.Fill(0);
// 4x4 rectangle, not filled
DrawRectWH(fb, 0, 0, 4, 4, indigo, false);
PrintFramebufferAsANSI(fb);
Result<color_rgb565_t> c;
c = writer.GetPixel(0, 0);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), indigo);
c = writer.GetPixel(1, 0);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), indigo);
c = writer.GetPixel(2, 0);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), indigo);
c = writer.GetPixel(3, 0);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), indigo);
c = writer.GetPixel(4, 0);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(0, 1);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), indigo);
c = writer.GetPixel(1, 1);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(2, 1);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(3, 1);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), indigo);
c = writer.GetPixel(4, 1);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(0, 2);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), indigo);
c = writer.GetPixel(1, 2);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(2, 2);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(3, 2);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), indigo);
c = writer.GetPixel(4, 2);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(0, 3);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), indigo);
c = writer.GetPixel(1, 3);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), indigo);
c = writer.GetPixel(2, 3);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), indigo);
c = writer.GetPixel(3, 3);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), indigo);
c = writer.GetPixel(4, 3);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(0, 4);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(1, 4);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(2, 4);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(3, 4);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(4, 4);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
}
TEST(DrawCircle, Empty) {
color_rgb565_t data[7 * 7];
Framebuffer fb(data, PixelFormat::RGB565, {7, 7}, 7 * sizeof(data[0]));
FramebufferWriter writer(fb);
color_rgb565_t indigo = color::kColorsPico8Rgb565[12];
writer.Fill(0);
DrawCircle(fb, 3, 3, 3, indigo, false);
PrintFramebufferAsANSI(fb);
// ..xxx..
// .x...x.
// x.....x
// x.....x
// x.....x
// .x...x.
// ..xxx..
Result<color_rgb565_t> c;
c = writer.GetPixel(0, 0);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(1, 0);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(2, 0);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), indigo);
c = writer.GetPixel(3, 0);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), indigo);
c = writer.GetPixel(4, 0);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), indigo);
c = writer.GetPixel(5, 0);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(6, 0);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(0, 1);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(1, 1);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), indigo);
c = writer.GetPixel(2, 1);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(3, 1);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(4, 1);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(5, 1);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), indigo);
c = writer.GetPixel(6, 1);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(0, 2);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), indigo);
c = writer.GetPixel(1, 2);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(2, 2);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(3, 2);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(4, 2);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(5, 2);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(6, 2);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), indigo);
c = writer.GetPixel(0, 3);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), indigo);
c = writer.GetPixel(1, 3);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(2, 3);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(3, 3);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(4, 3);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(5, 3);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(6, 3);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), indigo);
c = writer.GetPixel(0, 4);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), indigo);
c = writer.GetPixel(1, 4);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(2, 4);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(3, 4);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(4, 4);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(5, 4);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(6, 4);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), indigo);
c = writer.GetPixel(0, 5);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(1, 5);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), indigo);
c = writer.GetPixel(2, 5);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(3, 5);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(4, 5);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(5, 5);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), indigo);
c = writer.GetPixel(6, 5);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(0, 6);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(1, 6);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(2, 6);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), indigo);
c = writer.GetPixel(3, 6);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), indigo);
c = writer.GetPixel(4, 6);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), indigo);
c = writer.GetPixel(5, 6);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
c = writer.GetPixel(6, 6);
ASSERT_TRUE(c.ok());
EXPECT_EQ(c.value(), 0);
}
TEST(DrawText, WithFgBg) {
color_rgb565_t data[(5 * 6) * (3 * 8)];
Framebuffer fb(
data, PixelFormat::RGB565, {5 * 6, 3 * 8}, (5 * 6) * sizeof(data[0]));
FramebufferWriter writer(fb);
writer.Fill(0);
pw::draw::TextArea text_area(fb, GetFont6x8());
text_area.SetForegroundColor(color::kColorsPico8Rgb565[color::kColorPink]);
text_area.SetBackgroundColor(0);
text_area.DrawText("Hi There.\nWorld");
PrintFramebufferAsANSI(fb);
}
} // namespace
} // namespace pw::draw