Skip to content

Commit 29bfb90

Browse files
authored
Fix hline width with custom clip window (#167)
* Fix hline width when using custom clip window * Fix filled circle test to match fixed hline drawing * Fix rectangle test to match fixed hline drawing
1 parent 0dd6d9c commit 29bfb90

File tree

4 files changed

+46
-13
lines changed

4 files changed

+46
-13
lines changed

src/hagl_hline.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ void hagl_draw_hline_xyw(
5252

5353
/* x0 is left of clip window, ignore start part. */
5454
if (x0 < surface->clip.x0) {
55-
width = width + x0;
55+
width = width - (surface->clip.x0 - x0);
5656
x0 = surface->clip.x0;
5757
}
5858

tests/test_circle.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -430,14 +430,16 @@ TEST test_fill_circle_custom_clip(void) {
430430
/* Inside clip: center is filled */
431431
ASSERT_EQ(0xFFFF, hagl_get_pixel(&bitmap, 15, 15));
432432

433-
/* Inside clip: top corners are filled */
433+
/* Inside clip: top-left corner is filled (distance ~7.07 < 10) */
434434
ASSERT_EQ(0xFFFF, hagl_get_pixel(&bitmap, 10, 10));
435-
ASSERT_EQ(0xFFFF, hagl_get_pixel(&bitmap, 25, 10));
435+
436+
/* Outside circle: top-right corner (distance ~11.18 > 10) */
437+
ASSERT_EQ(0x0000, hagl_get_pixel(&bitmap, 25, 10));
436438

437439
/* Inside clip: bottom cardinal */
438440
ASSERT_EQ(0xFFFF, hagl_get_pixel(&bitmap, 15, 25));
439441

440-
/* Inside clip: right edge */
442+
/* Inside clip: right cardinal */
441443
ASSERT_EQ(0xFFFF, hagl_get_pixel(&bitmap, 25, 15));
442444

443445
/* Outside clip: clipped cardinals */
@@ -448,8 +450,8 @@ TEST test_fill_circle_custom_clip(void) {
448450
ASSERT_EQ(0x0000, hagl_get_pixel(&bitmap, 9, 15));
449451
ASSERT_EQ(0x0000, hagl_get_pixel(&bitmap, 15, 9));
450452

451-
/* Total: 242 pixels */
452-
ASSERT_EQ(242, count_pixels(&bitmap, 0xFFFF));
453+
/* Total: 229 pixels */
454+
ASSERT_EQ(229, count_pixels(&bitmap, 0xFFFF));
453455

454456
PASS();
455457
}
@@ -460,7 +462,7 @@ TEST test_fill_circle_custom_clip_regression(void) {
460462

461463
uint32_t crc = crc32(bitmap.buffer, bitmap.size);
462464

463-
ASSERT_EQ(0xD2A7217B, crc);
465+
ASSERT_EQ(0xC468FB33, crc);
464466
PASS();
465467
}
466468

tests/test_hline.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,36 @@ TEST test_draw_hline_xyw_custom_clip_regression(void) {
337337
PASS();
338338
}
339339

340+
/*
341+
* Line clipped by left edge of custom clip window only:
342+
* Line from x=40 to x=59 (width 20) at y=75.
343+
* Clip window (50,50)-(200,200). Only x=50..59 visible.
344+
*
345+
* (40,75)----[50,75)=====(59,75)
346+
* ^clip.x0
347+
*/
348+
TEST test_draw_hline_xyw_custom_clip_left(void) {
349+
hagl_set_clip(&bitmap, 50, 50, 200, 200);
350+
hagl_draw_hline_xyw(&bitmap, 40, 75, 20, 0xFFFF);
351+
352+
/* On line: left edge of clip window */
353+
ASSERT_EQ(0xFFFF, hagl_get_pixel(&bitmap, 50, 75));
354+
355+
/* On line: right endpoint of original line */
356+
ASSERT_EQ(0xFFFF, hagl_get_pixel(&bitmap, 59, 75));
357+
358+
/* Outside: just left of clip window */
359+
ASSERT_EQ(0x0000, hagl_get_pixel(&bitmap, 49, 75));
360+
361+
/* Outside: just right of original line end */
362+
ASSERT_EQ(0x0000, hagl_get_pixel(&bitmap, 60, 75));
363+
364+
/* Total: 10 pixels */
365+
ASSERT_EQ(10, count_pixels(&bitmap, 0xFFFF));
366+
367+
PASS();
368+
}
369+
340370
SUITE(hline_suite) {
341371
SET_SETUP(setup_callback, NULL);
342372
SET_TEARDOWN(teardown_callback, NULL);
@@ -354,6 +384,7 @@ SUITE(hline_suite) {
354384
RUN_TEST(test_draw_hline_xyw_outside);
355385
RUN_TEST(test_draw_hline_xyw_custom_clip);
356386
RUN_TEST(test_draw_hline_xyw_custom_clip_regression);
387+
RUN_TEST(test_draw_hline_xyw_custom_clip_left);
357388
}
358389

359390
GREATEST_MAIN_DEFS();

tests/test_rectangle.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -384,9 +384,9 @@ TEST test_draw_rectangle_xyxy_custom_clip(void) {
384384
ASSERT_EQ(0xFFFF, hagl_get_pixel(&bitmap, 80, 50));
385385
ASSERT_EQ(0xFFFF, hagl_get_pixel(&bitmap, 80, 65));
386386

387-
/* On outline: hline/vline extend to clip boundary */
388-
ASSERT_EQ(0xFFFF, hagl_get_pixel(&bitmap, 100, 80));
389-
ASSERT_EQ(0xFFFF, hagl_get_pixel(&bitmap, 80, 100));
387+
/* On outline: hline/vline end at rectangle edge, not clip boundary */
388+
ASSERT_EQ(0x0000, hagl_get_pixel(&bitmap, 100, 80));
389+
ASSERT_EQ(0x0000, hagl_get_pixel(&bitmap, 80, 100));
390390

391391
/* Inside: interior is empty */
392392
ASSERT_EQ(0x0000, hagl_get_pixel(&bitmap, 65, 65));
@@ -401,8 +401,8 @@ TEST test_draw_rectangle_xyxy_custom_clip(void) {
401401
ASSERT_EQ(0x0000, hagl_get_pixel(&bitmap, 49, 80));
402402
ASSERT_EQ(0x0000, hagl_get_pixel(&bitmap, 80, 49));
403403

404-
/* Total: right vline 51 + bottom hline 51 - corner 1 = 101 */
405-
ASSERT_EQ(101, count_pixels(&bitmap, 0xFFFF));
404+
/* Total: right vline 31 + bottom hline 31 - corner 1 = 61 */
405+
ASSERT_EQ(61, count_pixels(&bitmap, 0xFFFF));
406406

407407
PASS();
408408
}
@@ -413,7 +413,7 @@ TEST test_draw_rectangle_xyxy_custom_clip_regression(void) {
413413

414414
uint32_t crc = crc32(bitmap.buffer, bitmap.size);
415415

416-
ASSERT_EQ(0x876AD58F, crc);
416+
ASSERT_EQ(0x17F93E43, crc);
417417
PASS();
418418
}
419419

0 commit comments

Comments
 (0)