Skip to content

Commit

Permalink
feat(graph): add LCUI_OverPixel()
Browse files Browse the repository at this point in the history
  • Loading branch information
lc-soft committed Oct 3, 2019
1 parent 1b578be commit d8075d9
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 37 deletions.
42 changes: 42 additions & 0 deletions include/LCUI/graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,48 @@ LCUI_BEGIN_HEADER
(Graph_IsValid(G) && \
((G)->quote.is_valid ? (G)->quote.is_writable : TRUE))

/*
* Pixel over operator with alpha channel
* See more: https://en.wikipedia.org/wiki/Alpha_compositing
*/
INLINE void LCUI_OverPixel(LCUI_ARGB *dst, const LCUI_ARGB *src)
{
double a, out_a, out_r, out_g, out_b, src_a;
/*
* Original formula:
* Co = (Ca * aa + Cb * ab * (1 - aa)) / (aa + ab * (1 - aa))
* ao = aa + ab * (1 - aa)
*
* Variable full name:
* Co => colorOut
* Ca => colorA
* Cb => colorB
* aa => colorA.alpha
* ab => colorB.alpha
* ao => colorOut.alpha
*
* The formula used in the code:
* ai = ab * (1 - aa)
* Co = (Ca * aa + Cb * ai) / (aa + ai)
* ao = aa + ai
*/
src_a = src->a / 255.0;
a = (1.0 - src_a) * dst->a / 255.0;
out_r = src->r * src_a + dst->r * a;
out_g = src->g * src_a + dst->g * a;
out_b = src->b * src_a + dst->b * a;
out_a = src_a + a;
if (out_a > 0) {
out_r /= out_a;
out_g /= out_a;
out_b /= out_a;
}
dst->r = (unsigned char)(out_r + 0.5);
dst->g = (unsigned char)(out_g + 0.5);
dst->b = (unsigned char)(out_b + 0.5);
dst->a = (unsigned char)(255.0 * out_a + 0.5);
}

LCUI_API void Graph_PrintInfo(LCUI_Graph *graph);

LCUI_API void Graph_Init(LCUI_Graph *graph);
Expand Down
39 changes: 19 additions & 20 deletions include/LCUI_Build.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*
* LCUI_Build.h -- Build-related configuration definitions
*
* Copyright (c) 2018, Liu chao <[email protected]> All rights reserved.
*
* Copyright (c) 2019, Liu chao <[email protected]> All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
Expand All @@ -28,26 +28,23 @@
* POSSIBILITY OF SUCH DAMAGE.
*/


#ifndef LCUI_BUILD_H
#define LCUI_BUILD_H

#if defined(__GNUC__)
# define LCUI_API
#elif (defined(_MSC_VER) && _MSC_VER < 800) ||\
(defined(__BORLANDC__) && __BORLANDC__ < 0x500)
/* older Borland and MSC
* compilers used '__export' and required this to be after
* the type.
*/
# define LCUI_API __export
#define LCUI_API
#else /* newer compiler */
# ifdef LCUI_EXPORTS
# define LCUI_API __declspec(dllexport)
# else
# define LCUI_API
# endif
#ifdef LCUI_EXPORTS
#define LCUI_API __declspec(dllexport)
#else
#define LCUI_API
#endif
#endif /* compiler */
#if defined(WIN32) && !defined(__cplusplus)
#define INLINE __inline
#else
#define INLINE static inline
#endif

#ifdef DEBUG
#define DEBUG_MSG _DEBUG_MSG
Expand All @@ -57,7 +54,9 @@

#define LOG Logger_Log
#define LOGW Logger_LogW
#define _DEBUG_MSG(format, ...) Logger_Log(__FILE__" %d: %s(): "format, __LINE__, __FUNCTION__,##__VA_ARGS__)
#define _DEBUG_MSG(format, ...) \
Logger_Log(__FILE__ " %d: %s(): " format, __LINE__, __FUNCTION__, \
##__VA_ARGS__)

#if defined(WIN32) || defined(_WIN32)
#define LCUI_BUILD_IN_WIN32
Expand All @@ -82,10 +81,10 @@
#endif

#ifdef __cplusplus
#define LCUI_BEGIN_HEADER extern "C"{
#define LCUI_END_HEADER }
#define LCUI_BEGIN_HEADER extern "C" {
#define LCUI_END_HEADER }
#else
#define LCUI_BEGIN_HEADER /* nothing */
#define LCUI_BEGIN_HEADER /* nothing */
#define LCUI_END_HEADER
#endif

Expand Down
22 changes: 5 additions & 17 deletions src/font/fontlibrary.c
Original file line number Diff line number Diff line change
Expand Up @@ -722,33 +722,21 @@ static void FontBitmap_MixARGB(LCUI_Graph *graph, LCUI_Rect *write_rect,
LCUI_Rect *read_rect)
{
int x, y;
LCUI_Color tmpColor;
LCUI_ARGB *px, *px_row_des;
uchar_t *byte_ptr, *byte_row_ptr;
double a, ca, out_a, out_r, out_g, out_b, src_a;

byte_row_ptr = bmp->buffer + read_rect->y * bmp->width;
px_row_des = graph->argb + write_rect->y * graph->width;
byte_row_ptr += read_rect->x;
px_row_des += write_rect->x;
ca = color.alpha / 255.0;
for (y = 0; y < read_rect->height; ++y) {
px = px_row_des;
byte_ptr = byte_row_ptr;
for (x = 0; x < read_rect->width; ++x, ++byte_ptr, ++px) {
src_a = *byte_ptr / 255.0 * ca;
a = (1.0 - src_a) * px->a / 255.0;
out_r = px->r * a + color.r * src_a;
out_g = px->g * a + color.g * src_a;
out_b = px->b * a + color.b * src_a;
out_a = src_a + a;
if (out_a > 0) {
out_r /= out_a;
out_g /= out_a;
out_b /= out_a;
}
px->r = (uchar_t)(out_r + 0.5);
px->g = (uchar_t)(out_g + 0.5);
px->b = (uchar_t)(out_b + 0.5);
px->a = (uchar_t)(255.0 * out_a + 0.5);
tmpColor = color;
tmpColor.alpha = *byte_ptr * color.alpha / 255.0;
LCUI_OverPixel(px, &tmpColor);
}
px_row_des += graph->width;
byte_row_ptr += bmp->width;
Expand Down

0 comments on commit d8075d9

Please sign in to comment.