Skip to content

Commit 122f696

Browse files
authoredFeb 3, 2025··
Merge pull request #98 from weihsinyeh/draw
Move common routines to draw-common.c
2 parents ed0ebb5 + 7c83fa7 commit 122f696

File tree

5 files changed

+55
-89
lines changed

5 files changed

+55
-89
lines changed
 

‎Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ libtwin.a_files-y = \
4444
src/pattern.c \
4545
src/spline.c \
4646
src/work.c \
47+
src/draw-common.c \
4748
src/hull.c \
4849
src/icon.c \
4950
src/pixmap.c \
@@ -61,7 +62,7 @@ libtwin.a_files-$(CONFIG_LOGGING) += src/log.c
6162
libtwin.a_files-$(CONFIG_CURSOR) += src/cursor.c
6263

6364
# Renderer
64-
libtwin.a_files-$(CONFIG_RENDERER_BUILTIN) += src/draw.c
65+
libtwin.a_files-$(CONFIG_RENDERER_BUILTIN) += src/draw-builtin.c
6566
libtwin.a_files-$(CONFIG_RENDERER_PIXMAN) += src/draw-pixman.c
6667
libtwin.a_cflags-$(CONFIG_RENDERER_PIXMAN) += $(shell pkg-config --cflags pixman-1)
6768
ifeq ($(CONFIG_RENDERER_PIXMAN), y)

‎include/twin.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ twin_pixmap_t *twin_make_cursor(int *hx, int *hy);
624624
void twin_dispatch(twin_context_t *ctx);
625625

626626
/*
627-
* draw.c
627+
* draw-*.c
628628
*/
629629

630630
void twin_composite(twin_pixmap_t *dst,
@@ -648,6 +648,10 @@ void twin_fill(twin_pixmap_t *dst,
648648
twin_coord_t right,
649649
twin_coord_t bottom);
650650

651+
/*
652+
* draw-common.c
653+
*/
654+
651655
void twin_premultiply_alpha(twin_pixmap_t *px);
652656

653657
/*

‎src/draw.c ‎src/draw-builtin.c

-43
Original file line numberDiff line numberDiff line change
@@ -681,49 +681,6 @@ void twin_composite(twin_pixmap_t *dst,
681681
msk_y, operator, width, height);
682682
}
683683

684-
static twin_argb32_t _twin_apply_alpha(twin_argb32_t v)
685-
{
686-
uint16_t t1, t2, t3;
687-
twin_a8_t alpha = twin_get_8(v,
688-
#if __BYTE_ORDER == __BIG_ENDIAN
689-
0
690-
#else
691-
24
692-
#endif
693-
);
694-
695-
/* clear RGB data if alpha is zero */
696-
if (!alpha)
697-
return 0;
698-
699-
#if __BYTE_ORDER == __BIG_ENDIAN
700-
/* twin needs ARGB format */
701-
return alpha << 24 | twin_int_mult(twin_get_8(v, 24), alpha, t1) << 16 |
702-
twin_int_mult(twin_get_8(v, 16), alpha, t2) << 8 |
703-
twin_int_mult(twin_get_8(v, 8), alpha, t3) << 0;
704-
#else
705-
return alpha << 24 | twin_int_mult(twin_get_8(v, 0), alpha, t1) << 16 |
706-
twin_int_mult(twin_get_8(v, 8), alpha, t2) << 8 |
707-
twin_int_mult(twin_get_8(v, 16), alpha, t3) << 0;
708-
#endif
709-
}
710-
711-
void twin_premultiply_alpha(twin_pixmap_t *px)
712-
{
713-
int x, y;
714-
twin_pointer_t p;
715-
716-
if (px->format != TWIN_ARGB32)
717-
return;
718-
719-
for (y = 0; y < px->height; y++) {
720-
p.b = px->p.b + y * px->stride;
721-
722-
for (x = 0; x < px->width; x++)
723-
p.argb32[x] = _twin_apply_alpha(p.argb32[x]);
724-
}
725-
}
726-
727684
/*
728685
* array primary index is OVER SOURCE
729686
* array secondary index is ARGB32 RGB16 A8

‎src/draw-common.c

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Twin - A Tiny Window System
3+
* Copyright (c) 2004 Keith Packard <keithp@keithp.com>
4+
* Copyright (c) 2024 National Cheng Kung University, Taiwan
5+
* All rights reserved.
6+
*/
7+
8+
#include "twin_private.h"
9+
10+
static twin_argb32_t _twin_apply_alpha(twin_argb32_t v)
11+
{
12+
uint16_t t1, t2, t3;
13+
twin_a8_t alpha = twin_get_8(v,
14+
#if __BYTE_ORDER == __BIG_ENDIAN
15+
0
16+
#else
17+
24
18+
#endif
19+
);
20+
21+
/* clear RGB data if alpha is zero */
22+
if (!alpha)
23+
return 0;
24+
25+
#if __BYTE_ORDER == __BIG_ENDIAN
26+
/* twin needs ARGB format */
27+
return alpha << 24 | twin_int_mult(twin_get_8(v, 24), alpha, t1) << 16 |
28+
twin_int_mult(twin_get_8(v, 16), alpha, t2) << 8 |
29+
twin_int_mult(twin_get_8(v, 8), alpha, t3) << 0;
30+
#else
31+
return alpha << 24 | twin_int_mult(twin_get_8(v, 0), alpha, t1) << 16 |
32+
twin_int_mult(twin_get_8(v, 8), alpha, t2) << 8 |
33+
twin_int_mult(twin_get_8(v, 16), alpha, t3) << 0;
34+
#endif
35+
}
36+
37+
void twin_premultiply_alpha(twin_pixmap_t *px)
38+
{
39+
if (px->format != TWIN_ARGB32)
40+
return;
41+
42+
for (twin_coord_t y = 0; y < px->height; y++) {
43+
twin_pointer_t p = {.b = px->p.b + y * px->stride};
44+
45+
for (twin_coord_t x = 0; x < px->width; x++)
46+
p.argb32[x] = _twin_apply_alpha(p.argb32[x]);
47+
}
48+
}

‎src/draw-pixman.c

-44
Original file line numberDiff line numberDiff line change
@@ -166,47 +166,3 @@ void twin_fill(twin_pixmap_t *_dst,
166166

167167
pixman_image_unref(dst);
168168
}
169-
170-
/* Same function in draw.c */
171-
static twin_argb32_t _twin_apply_alpha(twin_argb32_t v)
172-
{
173-
uint16_t t1, t2, t3;
174-
twin_a8_t alpha = twin_get_8(v,
175-
#if __BYTE_ORDER == __BIG_ENDIAN
176-
0
177-
#else
178-
24
179-
#endif
180-
);
181-
182-
/* clear RGB data if alpha is zero */
183-
if (!alpha)
184-
return 0;
185-
186-
#if __BYTE_ORDER == __BIG_ENDIAN
187-
/* twin needs ARGB format */
188-
return alpha << 24 | twin_int_mult(twin_get_8(v, 24), alpha, t1) << 16 |
189-
twin_int_mult(twin_get_8(v, 16), alpha, t2) << 8 |
190-
twin_int_mult(twin_get_8(v, 8), alpha, t3) << 0;
191-
#else
192-
return alpha << 24 | twin_int_mult(twin_get_8(v, 0), alpha, t1) << 16 |
193-
twin_int_mult(twin_get_8(v, 8), alpha, t2) << 8 |
194-
twin_int_mult(twin_get_8(v, 16), alpha, t3) << 0;
195-
#endif
196-
}
197-
198-
void twin_premultiply_alpha(twin_pixmap_t *px)
199-
{
200-
int x, y;
201-
twin_pointer_t p;
202-
203-
if (px->format != TWIN_ARGB32)
204-
return;
205-
206-
for (y = 0; y < px->height; y++) {
207-
p.b = px->p.b + y * px->stride;
208-
209-
for (x = 0; x < px->width; x++)
210-
p.argb32[x] = _twin_apply_alpha(p.argb32[x]);
211-
}
212-
}

0 commit comments

Comments
 (0)
Please sign in to comment.