keychron_qmk_firmware/quantum/painter/qp_draw_ellipse.c
Nick Brassel 1f2b1dedcc
Quantum Painter (#10174)
* Install dependencies before executing unit tests.

* Split out UTF-8 decoder.

* Fixup python formatting rules.

* Add documentation for QGF/QFF and the RLE format used.

* Add CLI commands for converting images and fonts.

* Add stub rules.mk for QP.

* Add stream type.

* Add base driver and comms interfaces.

* Add support for SPI, SPI+D/C comms drivers.

* Include <qp.h> when enabled.

* Add base support for SPI+D/C+RST panels, as well as concrete implementation of ST7789.

* Add support for GC9A01.

* Add support for ILI9341.

* Add support for ILI9163.

* Add support for SSD1351.

* Implement qp_setpixel, including pixdata buffer management.

* Implement qp_line.

* Implement qp_rect.

* Implement qp_circle.

* Implement qp_ellipse.

* Implement palette interpolation.

* Allow for streams to work with either flash or RAM.

* Image loading.

* Font loading.

* QGF palette loading.

* Progressive decoder of pixel data supporting Raw+RLE, 1-,2-,4-,8-bpp monochrome and palette-based images.

* Image drawing.

* Animations.

* Font rendering.

* Check against 256 colours, dump out the loaded palette if debugging enabled.

* Fix build.

* AVR is not the intended audience.

* `qmk format-c`

* Generation fix.

* First batch of docs.

* More docs and examples.

* Review comments.

* Public API documentation.
2022-04-13 18:00:18 +10:00

117 lines
3.7 KiB
C

// Copyright 2021 Paul Cotter (@gr1mr3aver)
// Copyright 2021 Nick Brassel (@tzarc)
// SPDX-License-Identifier: GPL-2.0-or-later
#include "qp_internal.h"
#include "qp_comms.h"
#include "qp_draw.h"
// Utilize 4-way symmetry to draw an ellipse
static bool qp_ellipse_helper_impl(painter_device_t device, uint16_t centerx, uint16_t centery, uint16_t offsetx, uint16_t offsety, bool filled) {
/*
Ellipses have the property of 4-way symmetry, so four pixels can be drawn
for each computed [offsetx,offsety] given the center coordinates
represented by [centerx,centery].
For filled ellipses, we can draw horizontal lines between each pair of
pixels with the same final value of y.
When offsetx == 0 only two pixels can be drawn for filled or unfilled ellipses
*/
int16_t xpx = ((int16_t)centerx) + ((int16_t)offsetx);
int16_t xmx = ((int16_t)centerx) - ((int16_t)offsetx);
int16_t ypy = ((int16_t)centery) + ((int16_t)offsety);
int16_t ymy = ((int16_t)centery) - ((int16_t)offsety);
if (offsetx == 0) {
if (!qp_internal_setpixel_impl(device, xpx, ypy)) {
return false;
}
if (!qp_internal_setpixel_impl(device, xpx, ymy)) {
return false;
}
} else if (filled) {
if (!qp_internal_fillrect_helper_impl(device, xpx, ypy, xmx, ypy)) {
return false;
}
if (offsety > 0 && !qp_internal_fillrect_helper_impl(device, xpx, ymy, xmx, ymy)) {
return false;
}
} else {
if (!qp_internal_setpixel_impl(device, xpx, ypy)) {
return false;
}
if (!qp_internal_setpixel_impl(device, xpx, ymy)) {
return false;
}
if (!qp_internal_setpixel_impl(device, xmx, ypy)) {
return false;
}
if (!qp_internal_setpixel_impl(device, xmx, ymy)) {
return false;
}
}
return true;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Quantum Painter External API: qp_ellipse
bool qp_ellipse(painter_device_t device, uint16_t x, uint16_t y, uint16_t sizex, uint16_t sizey, uint8_t hue, uint8_t sat, uint8_t val, bool filled) {
qp_dprintf("qp_ellipse: entry\n");
struct painter_driver_t *driver = (struct painter_driver_t *)device;
if (!driver->validate_ok) {
qp_dprintf("qp_ellipse: fail (validation_ok == false)\n");
return false;
}
int16_t aa = ((int16_t)sizex) * ((int16_t)sizex);
int16_t bb = ((int16_t)sizey) * ((int16_t)sizey);
int16_t fa = 4 * ((int16_t)aa);
int16_t fb = 4 * ((int16_t)bb);
int16_t dx = 0;
int16_t dy = ((int16_t)sizey);
qp_internal_fill_pixdata(device, QP_MAX(sizex, sizey), hue, sat, val);
if (!qp_comms_start(device)) {
qp_dprintf("qp_ellipse: fail (could not start comms)\n");
return false;
}
bool ret = true;
for (int16_t delta = (2 * bb) + (aa * (1 - (2 * sizey))); bb * dx <= aa * dy; dx++) {
if (!qp_ellipse_helper_impl(device, x, y, dx, dy, filled)) {
ret = false;
break;
}
if (delta >= 0) {
delta += fa * (1 - dy);
dy--;
}
delta += bb * (4 * dx + 6);
}
dx = sizex;
dy = 0;
for (int16_t delta = (2 * aa) + (bb * (1 - (2 * sizex))); aa * dy <= bb * dx; dy++) {
if (!qp_ellipse_helper_impl(device, x, y, dx, dy, filled)) {
ret = false;
break;
}
if (delta >= 0) {
delta += fb * (1 - dx);
dx--;
}
delta += aa * (4 * dy + 6);
}
qp_dprintf("qp_ellipse: %s\n", ret ? "ok" : "fail");
qp_comms_stop(device);
return ret;
}